1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035 |
- /*
- * @Author : ChenJie
- * @Date : 2021-10-14 09:27:15
- * @LastEditors : ChenJie
- * @LastEditTime : 2021-11-16 21:38:19
- * @Description : file content
- * @FilePath : \PLAT\project\ec616_0h00\apps\qx_app\src\AppTaskUart.c
- */
- #include "AppTaskUart.h"
- // #include "AppFunc.h"
- extern QueueHandle_t uartDataHandle;
- static StaticTask_t gProcess_Uart_Task_t;
- static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
- static osThreadId_t UartTaskId = NULL;
- static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
- #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
- static UINT8 Uart_WriteCmd_func(UartWriteData_S UartWriteData);
- static UINT16 crc_chk(UINT8 *data, UINT8 length);
- UINT8 Uart_Encrypt_Send(void);
- void Uart_Cmd_Control(QueueHandle_t UartWriteCmdHandle, UartBuffer UartAnswerData);
- /**
- * @brief : Uart串口线程运行主函数
- * @param {void} *arg
- * @return {*}
- */
- static void UartTask(void *arg)
- {
- UINT16 Reg_Num = 0;
- UINT16 Uart_Recv_LEN;
- UINT8 UartRecvFlagCounter = 0;
- UartQueryType Uart_Read_Msg; //发送结构体初始化
- memset(&(Uart_Read_Msg), 0x00, sizeof(Uart_Read_Msg));
- UartBuffer UartAnswerData; //应答数据初始化
- memset(&(UartAnswerData), 0x00, sizeof(UartBuffer));
- UartWriteData_S UartWriteDataHandleRecv;
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
- UINT8 ret = 0x00;
- UINT8 EncryptFlag = 0x00;
- UINT8 EncryptCount = 0;
- if (UartWriteCmdHandle == NULL) //Uart控制命令传输指针
- {
- UartWriteCmdHandle = osMessageQueueNew(1, sizeof(UartWriteData_S), NULL);
- }
- while (1)
- {
- switch (gProcess_Uart_Task)
- {
- case PROCESS_UART_STATE_INTI:
- {
- hal_uart_hardware_config_t hwConfig = {
- ARM_POWER_FULL,
- ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 |
- ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 |
- ARM_USART_FLOW_CONTROL_NONE,
- 9600U};
- HAL_UART_ResetUartSetting(PORT_USART_1, &hwConfig, TRUE);
- osDelay(100);
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
- break;
- }
- case PROCESS_UART_STATE_ENCRYPT:
- {
- EncryptFlag = 0x00;
- EncryptCount = 0;
- while (EncryptFlag != 0x01 && EncryptCount <= 3)
- {
- EncryptFlag = Uart_Encrypt_Send();
- EncryptCount++;
- }
- #ifdef USING_PRINTF1
- printf("EncryptFlag:%d\n", EncryptFlag);
- #endif
- if (EncryptFlag == 0x01)
- {
- ihd_st_authFaild = 0;
- }
- else
- {
- ihd_st_authFaild = 1;
- }
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- break;
- }
- case PROCESS_UART_STATE_IDLE:
- {
- osDelay(100);
- if (UartRecvFlag == 1) //接收到数据才进行控制判定
- {
- Uart_Cmd_Control(UartWriteCmdHandle, UartAnswerData); //电池锁定,继电器锁定,加热控制
- }
- if (TimeCounter % 10 == 0 && gProcess_app == WORK)
- {
- //ECOMM_TRACE(UNILOG_PLA_APP, UartAppTask_83, P_SIG, 0, "Uart work begin:%02x", PadInterrupt);
- memset(&(UartWriteDataHandleRecv), 0x00, sizeof(UartWriteDataHandleRecv));
- if (osMessageQueueGet(UartWriteCmdHandle, &UartWriteDataHandleRecv, 0, 10) == osOK && UartRecvFlag == 1)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
- break;
- }
- else
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
- break;
- }
- }
- else if (gProcess_app == LISTEN)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
- break;
- }
- if (BMS_Fota_update_flag)
- {
- if (BattWorkStateDelay == 0x00)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_UPDATE);
- break;
- }
- }
- break;
- }
- case PROCESS_UART_STATE_READ:
- {
- UINT16 CRC_chk_buffer;
- Reg_Num = 0x21 + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP; //按照协议里面的0x21+X+N的结束地址
- Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
- Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
- Uart_Read_Msg.Reg_Begin_H = 0x00;
- Uart_Read_Msg.Reg_Begin_L = 0x00;
- Uart_Read_Msg.Reg_Num_H = Reg_Num >> 8;
- Uart_Read_Msg.Reg_Num_L = Reg_Num;
- memset((UINT8 *)&(UartAnswerData), 0x00, sizeof(UartBuffer));
- CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Read_Msg, 6);
- Uart_Read_Msg.CRC_L = CRC_chk_buffer;
- Uart_Read_Msg.CRC_H = CRC_chk_buffer >> 8;
- Uart_Recv_LEN = UartAppTrasmit((UINT8 *)&Uart_Read_Msg, sizeof(Uart_Read_Msg), (UINT8 *)&UartAnswerData, Reg_Num * 2 + 5, 1000);
- #ifdef USING_PRINTF1
- printf("[%d]Uart_Recv_buffer-%d: ", __LINE__, Uart_Recv_LEN);
- #endif
- #ifdef USING_PRINTF1
- printf("[%d]Uart_Recv_buffer-%d: ", __LINE__, Uart_Recv_LEN);
- for (int i = 0; i < Uart_Recv_LEN - 5; i++)
- {
- printf("%2x - %x ", i, *((UINT8 *)&UartAnswerData.data + i));
- }
- printf("\n");
- #endif
- if (Uart_Recv_LEN > 5)
- {
- uartBattInfoDecode(UartAnswerData.data);
- UartErrorFlag = 0;
- UartRecvFlagCounter = 0;
- UartRecvFlag = 1;
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- break;
- }
- else //没有接收到数据或数据有误
- {
- UartRecvFlag = 0;
- UartRecvFlagCounter++;
- }
- if (UartRecvFlagCounter >= 10)
- {
- UartRecvFlagCounter = 0;
- UartErrorFlag = 1;
- uartBattInfoDecode(UartAnswerData.data);
- break;
- }
- break;
- }
- case PROCESS_UART_STATE_WRITE:
- {
- UartCmdRecvFlag = Uart_WriteCmd_func(UartWriteDataHandleRecv);
- osDelay(500);
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
- break;
- }
- case PROCESS_UART_STATE_UPDATE:
- {
- BMSupdatestatus = 0xFF;
- UartRecvFlag = 0;
- #if BMS_MANUFACTURE == 1
- BMSupdatestatus = SP_BMS_Update_Service();
- #elif BMS_MANUFACTURE == 2
- BMSupdatestatus = MS_BMS_Update_Service();
- #endif
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- BMS_Fota_update_flag = FALSE;
- break;
- }
- case PROCESS_UART_STATE_SLEEP:
- {
- #ifdef USING_PRINTF
- printf("Uart silence begin\n");
- #endif
- UartRecvFlag = 0;
- while (true)
- {
- osDelay(100);
- if (gProcess_app == WORK)
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
- break;
- }
- }
- break;
- }
- default:
- {
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- break;
- }
- }
- }
- }
- /**
- * @brief : 串口写入发送函数
- * @param {UartWriteData_S} UartWriteData
- * @return {*}
- */
- static UINT8 Uart_WriteCmd_func(UartWriteData_S UartWriteData)
- {
- UartWriteMsgType Uart_Write_Msg;
- UINT16 RegAddress = 0x0000;
- UINT16 CRC_chk_buffer, Uart_Recv_LEN = 0;
- UINT8 Uart_Recv_Buffer[16] = {0};
- switch (UartWriteData.WriteCmd)
- {
- case 0x01: //是否锁定
- {
- RegAddress = 0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
- break;
- }
- case 0x02: //是否加热
- {
- RegAddress = 0x1C + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
- break;
- }
- case 0x04: //是否继电器控制
- {
- RegAddress = 0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
- break;
- }
- default:
- {
- UartWriteData.WriteCmd = 0x00;
- return 0;
- }
- }
- Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
- Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
- Uart_Write_Msg.Reg_Begin_H = RegAddress >> 8;
- Uart_Write_Msg.Reg_Begin_L = RegAddress;
- Uart_Write_Msg.Reg_Num_H = 0x00;
- Uart_Write_Msg.Reg_Num_L = 0x01;
- Uart_Write_Msg.Data_Count = 0x02; //要写入的字节数
- memcpy(Uart_Write_Msg.Data, UartWriteData.Data, 2);
- CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg) - 2);
- Uart_Write_Msg.CRC_L = CRC_chk_buffer;
- Uart_Write_Msg.CRC_H = CRC_chk_buffer >> 8;
- Uart_Recv_LEN = UartAppTrasmit((UINT8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg), (UINT8 *)&Uart_Recv_Buffer, 8, 1000);
- if (Uart_Recv_Buffer[1] == 0x10)
- {
- return UartWriteData.WriteCmd;
- }
- else
- {
- return 0;
- }
- }
- /**
- * @brief : 串口控制函数判断,含加热开启关闭,充电禁止允许,放电禁止允许,继电器禁止允许
- * @param {QueueHandle_t} UartWriteCmdHandle
- * @param {UartBuffer} UartAnswerData
- * @return {*}
- */
- void Uart_Cmd_Control(QueueHandle_t UartWriteCmdHandle, UartBuffer UartAnswerData)
- {
- UartWriteData_S UartWriteData;
- memset(&(UartWriteData), 0x00, sizeof(UartWriteData_S)); //Uart控制命令初始化
- UINT8 HeatSwitch = 0;
- UINT8 ChargePermitState = 1;
- UINT8 DischargePermitState = 1;
- UINT8 RelayControlState = 0;
- UINT8 DischargeProtectState = 0;
- UINT8 ChargeProtectState = 0;
- BmsProtectStateDecode(&DischargeProtectState, &ChargeProtectState);
- ChargePermitState = getbit((UartAnswerData.data[(0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 1); //充电允许状态,1-允许,0禁止
- DischargePermitState = getbit((UartAnswerData.data[(0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 0); //放电允许状态,1-允许,0禁止
- RelayControlState = getbit((UartAnswerData.data[(0x09 + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 0); //继电器状态,1-继电器断开,0-继电器吸合
- //控制集中处理
- UINT8 BcuFltAct = 0;
- BcuFltAct = sfmd_st_fltAct & 0X00; //暂时屏蔽故障动作
- //控制充电禁止
- if (getbit(BcuFltAct, 6) == 0)
- {
- if ((maxCellVol > 4190 && maxCellVol < 5000) && battSOC >= 99)
- {
- ChargeForbiddenControl = 1;
- }
- else if (maxCellVol < 4150 && maxCellVol > 0)
- {
- ChargeForbiddenControl = 0;
- }
- }
- else if (getbit(BcuFltAct, 6) == 1)
- {
- ChargeForbiddenControl = 1;
- }
- //控制放电禁止
- if (getbit(BcuFltAct, 5) == 0)
- {
- if (AppDataInfo.BattLock == FALSE)
- {
- DisChargeForbiddenControl = 0;
- }
- else if (AppDataInfo.BattLock == TRUE)
- {
- DisChargeForbiddenControl = 1;
- }
- }
- else if (getbit(BcuFltAct, 5) == 1)
- {
- DisChargeForbiddenControl = 1;
- }
- //控制继电器断开
- if (RelayForceControl == 0)
- {
- if (AppDataInfo.RelayControl == TRUE)
- {
- RelayForbiddenControl = 1;
- }
- else if (AppDataInfo.RelayControl == FALSE)
- {
- if (getbit(BcuFltAct, 3) == 0)
- {
- RelayForbiddenControl = 0;
- }
- else if (getbit(BcuFltAct, 3) == 1)
- {
- RelayForbiddenControl = 1;
- }
- }
- }
- else if (RelayForceControl == 1) //close
- {
- RelayForbiddenControl = 0;
- AppDataInfo.appDataModify = TRUE;
- AppDataInfo.RelayControl = FALSE;
- }
- else if (RelayForceControl == 2) //open
- {
- RelayForbiddenControl = 1;
- AppDataInfo.appDataModify = TRUE;
- AppDataInfo.RelayControl = TRUE;
- }
- //控制命令发送
- if (TimeCounter % 10 == 0 && BattHeaterSwitch(&HeatSwitch, HeatForceControl) == TRUE)
- {
- UartWriteData.WriteCmd = 0x02;
- UartWriteData.Data[0] = 0x00;
- UartWriteData.Data[1] = HeatSwitch & 0xFF;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- if (ChargeForbiddenControl == 0x01 && (ChargePermitState != 0x00)) //try to lock lock the charge
- {
- #ifdef USING_PRINTF1
- printf("[%d]try to lock charge \n", __LINE__);
- #endif
- UartWriteData.WriteCmd = 0x01;
- UartWriteData.Data[0] = 0x00 | (RelayControlState << 7);
- UartWriteData.Data[1] = 0x00 | DischargePermitState;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- else if ((ChargeForbiddenControl == 0x00) && (ChargePermitState != 0x01) && (ChargeProtectState == 0)) //try to unlock lock the charge,
- {
- #ifdef USING_PRINTF1
- printf("[%d]try to unlock charge \n", __LINE__);
- #endif
- UartWriteData.WriteCmd = 0x01;
- UartWriteData.Data[0] = 0x00 | (RelayControlState << 7);
- UartWriteData.Data[1] = 0x02 | DischargePermitState;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- if ((BattWorkStateDelay == BATT_IDLE_SYM) && (DisChargeForbiddenControl == 1) && (DischargePermitState) != 0x00) //try to lock lock the discharge
- {
- UartWriteData.WriteCmd = 0x01;
- UartWriteData.Data[0] = (RelayControlState << 7) | 0x00;
- UartWriteData.Data[1] = (ChargePermitState << 1) | 0x00;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- else if ((DisChargeForbiddenControl == 0) && (DischargePermitState) != 0x01 && (DischargeProtectState == 0)) // try to unlock
- {
- UartWriteData.WriteCmd = 0x01;
- UartWriteData.Data[0] = (RelayControlState << 7) | 0x00;
- UartWriteData.Data[1] = (ChargePermitState << 1) | 0x01;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- if (RELAYCONFIG == 1)
- {
- if ((RelayForbiddenControl == 1) && (RelayControlState == 0x00)) //将继电器断开
- {
- #ifdef USING_PRINTF1
- printf("[%d]try to cut relay \n", __LINE__);
- #endif
- UartWriteData.WriteCmd = 0x04;
- UartWriteData.Data[0] = 0x80;
- UartWriteData.Data[1] = 0x00 | (ChargePermitState << 1) | DischargePermitState;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- else if ((RelayForbiddenControl == 0) && (RelayControlState == 0x01)) //继电器闭合
- {
- #ifdef USING_PRINTF1
- printf("[%d]try to close relay \n", __LINE__);
- #endif
- UartWriteData.WriteCmd = 0x04;
- UartWriteData.Data[0] = 0x00;
- UartWriteData.Data[1] = 0x00 | (ChargePermitState << 1) | DischargePermitState;
- osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
- }
- }
- }
- /**
- * @brief :串口传输函数
- * @param {UINT8} *pSend
- * @param {UINT32} sendLen
- * @param {UINT8} *pRead
- * @param {UINT32} readLen
- * @param {UINT32} timeout
- * @return {*}
- */
- UINT8 UartAppTrasmit(UINT8 *pSend, UINT32 sendLen, UINT8 *pRead, UINT32 readLen, UINT32 timeout)
- {
- UINT16 CRC_Rece_buffer = 0x00;
- UINT16 CRC_chk_buffer = 0xff;
- HAL_UART_SendStr(PORT_USART_1, pSend, sendLen);
- if (readLen > 0)
- {
- UartBuffer UartData;
- osStatus_t ret = osMessageQueueGet(uartDataHandle, &UartData, 0, timeout);
- if (ret == osOK)
- {
- memcpy(pRead, (UINT8 *)&UartData, UartData.len);
- CRC_Rece_buffer = *(pRead + UartData.len - 1) << 8 | *(pRead + UartData.len - 2);
- CRC_chk_buffer = crc_chk(pRead, UartData.len - 2);
- if (*(pRead + 0) != 0x01 || CRC_Rece_buffer != CRC_chk_buffer)
- {
- hal_uart_hardware_config_t hwConfig = {ARM_POWER_FULL,
- ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 |
- ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 |
- ARM_USART_FLOW_CONTROL_NONE,
- 9600U};
- HAL_UART_ResetUartSetting(PORT_USART_1, &hwConfig, TRUE);
- osDelay(500);
- memset(pRead, 0x00, readLen);
- return 1;
- }
- #ifdef USING_PRINTF1
- printf("Uart recv:%d-%d\n", UartData.len, readLen);
- for (UINT8 i = 0; i < UartData.len; i++)
- {
- printf("%x ", *(pRead + i));
- }
- printf("\n");
- #endif
- return UartData.len;
- }
- else
- {
- memset(pRead, 0x00, readLen);
- return 0;
- }
- }
- else
- {
- return 3;
- }
- }
- UINT8 Uart_Encrypt_Send()
- {
- UINT8 SeedNumberArrray[4] = {0x38, 0x56, 0xfe, 0xac};
- UINT16 EncodeNumberArray[4];
- UINT8 UartEncryptBuffer[17];
- UINT8 UartDecryptBuffer[5];
- UINT16 CRC_chk_buffer;
- UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
- UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
- UartEncryptBuffer[2] = 0x0c;
- for (int i = 0; i < 4; i++)
- {
- SeedNumberArrray[i] = rand();
- EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
- UartEncryptBuffer[i + 3] = SeedNumberArrray[i];
- UartEncryptBuffer[i * 2 + 7] = EncodeNumberArray[i] >> 8;
- UartEncryptBuffer[i * 2 + 8] = EncodeNumberArray[i];
- }
- CRC_chk_buffer = crc_chk(UartEncryptBuffer, 17 - 2);
- UartEncryptBuffer[15] = CRC_chk_buffer;
- UartEncryptBuffer[16] = CRC_chk_buffer >> 8;
- UartAppTrasmit(UartEncryptBuffer, 17, (UINT8 *)&UartDecryptBuffer, 5, 1000);
- #ifdef USING_PRINTF1
- printf("UartDecryptBuffer:\n");
- for (UINT8 i = 0; i < 5; i++)
- {
- printf("%x ", UartDecryptBuffer[i]);
- }
- printf("\n\n\n");
- #endif
- return UartDecryptBuffer[2];
- }
- static UINT16 crc_chk(UINT8 *data, UINT8 length)
- {
- UINT8 j;
- UINT16 reg_crc = 0xFFFF;
- while (length--)
- {
- reg_crc ^= *data++;
- for (j = 0; j < 8; j++)
- {
- if (reg_crc & 0x01)
- {
- reg_crc = (reg_crc >> 1) ^ 0xA001;
- }
- else
- {
- reg_crc = reg_crc >> 1;
- }
- }
- }
- return reg_crc;
- }
- void AppTaskUartInit(void *arg)
- {
- if (uartDataHandle == NULL)
- {
- uartDataHandle = osMessageQueueNew(1, sizeof(UartBuffer), NULL);
- if (uartDataHandle == NULL)
- return;
- }
- Usart1Handler(9600U);
- osThreadAttr_t task_attr;
- memset(&task_attr, 0, sizeof(task_attr));
- memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
- task_attr.name = "Uart_Task";
- task_attr.stack_mem = gProcess_Uart_TaskStack;
- task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
- task_attr.priority = osPriorityBelowNormal7;
- task_attr.cb_mem = &gProcess_Uart_Task_t;
- task_attr.cb_size = sizeof(StaticTask_t);
- UartTaskId = osThreadNew(UartTask, NULL, &task_attr);
- }
- void AppTaskUartDeInit(void *arg)
- {
- osThreadTerminate(UartTaskId);
- UartTaskId = NULL;
- }
- /*-----------------------------------------------------------------------------*/
- void SP_BMS_Update_Service() //超力源BMS升级服务
- {
- UINT8 errorCount = 0;
- UINT8 resetCount = 0;
- UINT16 currentPackage = 0;
- UINT32 updateDataTotalByteLen = 0;
- UpdateStep updateStep = UPDATE_STEP_CHECK_VERSION;
- UINT8 i, j, ret = 0;
- UINT8 dataLen = 0;
- UINT8 pUpdateMsgSend[80];
- UINT32 updateMsgSendLen = 0;
- UINT32 currentPackageStartAddr = 0;
- BMS_Update_Recv_Msg_Type pUpdateMsgRecv;
- UINT8 bmsUpdateFlag = 1;
- //BMS_Update_Recv_Msg_Type bmsMsg;
- //static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
- UINT8 Cycle_conut = 0;
- while (bmsUpdateFlag && Cycle_conut < 2)
- {
- switch (updateStep)
- {
- case UPDATE_STEP_CHECK_VERSION:
- dataLen = 0;
- updateMsgSendLen = 7;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x01; //read
- pUpdateMsgSend[3] = 0x03; //data len
- pUpdateMsgSend[4] = 0x90; //cmd
- pUpdateMsgSend[5] = 0x93; //checksum
- pUpdateMsgSend[6] = 0xF5; //end flag
- //printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- //printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x90)
- {
- if (pUpdateMsgRecv.data != 0xFF)
- {
- updateStep = UPDATE_STEP_REQUEST_UPDATE;
- errorCount = 0;
- }
- else
- {
- updateStep = UPDATE_STEP_SET_BAUD_RATE;
- errorCount = 0;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- osDelay(50);
- break;
- case UPDATE_STEP_REQUEST_UPDATE:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x80; //cmd
- pUpdateMsgSend[5] = 0x22; //data
- pUpdateMsgSend[6] = 0xA6; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x80)
- {
- if (pUpdateMsgRecv.data == 0x33)
- {
- updateStep = UPDATE_STEP_START_UPDATE;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- printf("update step:%d\n", updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_START_UPDATE:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x80; //cmd
- pUpdateMsgSend[5] = 0x55; //data
- pUpdateMsgSend[6] = 0xD9; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
- //updateStep = UPDATE_STEP_SET_BAUD_RATE;
- updateStep = UPDATE_STEP_CHECK_VERSION_AGAIN; //2021-04-09跳过波特率设置
- #ifdef USING_PRINTF1
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- break;
- case UPDATE_STEP_CHECK_VERSION_AGAIN:
- dataLen = 0;
- updateMsgSendLen = 7;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x01; //read
- pUpdateMsgSend[3] = 0x03; //data len
- pUpdateMsgSend[4] = 0x90; //cmd
- pUpdateMsgSend[5] = 0x93; //checksum
- pUpdateMsgSend[6] = 0xF5; //end flag
- //printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 100);
- //printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x90)
- {
- if (pUpdateMsgRecv.data != 0xFF)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- else
- {
- updateStep = UPDATE_STEP_SET_BAUD_RATE;
- errorCount = 0;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- osDelay(50);
- break;
- case UPDATE_STEP_SET_BAUD_RATE:
- printf("start step %d\n", updateStep);
- dataLen = 4;
- updateMsgSendLen = 12;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x08; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0x33; //data
- pUpdateMsgSend[6] = 0x00; //baud rate:9600
- pUpdateMsgSend[7] = 0x00;
- pUpdateMsgSend[8] = 0x25;
- pUpdateMsgSend[9] = 0x80;
- pUpdateMsgSend[10] = 0x61; //check
- pUpdateMsgSend[11] = 0xF5; //end flag
- #ifdef USING_PRINTF1
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\n");
- #endif
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- printf("ret = %d\n", ret);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_PREPARE_SEND_DATA_LEN:
- printf("start step %d\n", updateStep);
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0x44; //data
- pUpdateMsgSend[6] = 0xC9; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- updateStep = UPDATE_STEP_SEND_DATA_LEN;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_SEND_DATA_LEN:
- dataLen = 4;
- BSP_QSPI_Read_Safe(&updateDataTotalByteLen, FLASH_BMS_FOTA_START_ADDR, 4);
- updateDataTotalByteLen = (((updateDataTotalByteLen)&0xFF) << 24) | (((updateDataTotalByteLen >> 8) & 0xFF) << 16) | (((updateDataTotalByteLen >> 16) & 0xFF) << 8) | (((updateDataTotalByteLen >> 24) & 0xFF));
- updateMsgSendLen = 11;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x07; //data len
- pUpdateMsgSend[4] = 0x82; //cmd
- pUpdateMsgSend[5] = (updateDataTotalByteLen >> 24) & 0xFF; //data: package byte len
- pUpdateMsgSend[6] = (updateDataTotalByteLen >> 16) & 0xFF;
- pUpdateMsgSend[7] = (updateDataTotalByteLen >> 8) & 0xFF;
- pUpdateMsgSend[8] = (updateDataTotalByteLen)&0xFF;
- pUpdateMsgSend[9] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen + 2); //check sum
- pUpdateMsgSend[10] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- updateStep = UPDATE_STEP_PREPARE_SEND_UPDATE_DATA;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_PREPARE_SEND_UPDATE_DATA:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0x55; //data
- pUpdateMsgSend[6] = 0xDA; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- updateStep = UPDATE_STEP_SEND_UPDATE_DATA;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_SEND_UPDATE_DATA:
- dataLen = 64;
- updateMsgSendLen = 75;
- for (currentPackage = 0; currentPackage < updateDataTotalByteLen / 64; currentPackage++)
- {
- currentPackageStartAddr = currentPackage * 64;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x47; //data len
- pUpdateMsgSend[4] = 0x82; //cmd
- pUpdateMsgSend[5] = (currentPackageStartAddr >> 24) & 0xFF;
- pUpdateMsgSend[6] = (currentPackageStartAddr >> 16) & 0xFF;
- pUpdateMsgSend[7] = (currentPackageStartAddr >> 8) & 0xFF;
- pUpdateMsgSend[8] = currentPackageStartAddr & 0xFF;
- BSP_QSPI_Read_Safe(&pUpdateMsgSend[9], FLASH_BMS_FOTA_START_ADDR + 4 + currentPackage * dataLen, dataLen); //data
- pUpdateMsgSend[8 + dataLen + 1] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen + 6); //check sum
- pUpdateMsgSend[8 + dataLen + 2] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- if (currentPackage + 1 == updateDataTotalByteLen / 64)
- {
- updateStep = UPDATE_STEP_SEND_DATA_END;
- }
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- break;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- }
- osDelay(50);
- break;
- case UPDATE_STEP_SEND_DATA_END:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0x66; //data
- pUpdateMsgSend[6] = 0xEB; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
- if (ret != 0)
- {
- if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
- {
- if (pUpdateMsgRecv.cmd == 0x81)
- {
- if (pUpdateMsgRecv.data == 0x11)
- {
- updateStep = UPDATE_STEP_START_INSTALL;
- errorCount = 0;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = UPDATE_STEP_RESET;
- errorCount = 0;
- }
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_START_INSTALL:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0x99; //data
- pUpdateMsgSend[6] = 0x1E; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
- updateStep = UPDATE_STEP_END;
- #ifdef USING_PRINTF1
- //printf("update step:%d\n",updateStep);
- printf("query:");
- for (j = 0; j < updateMsgSendLen; j++)
- {
- printf("%x ", pUpdateMsgSend[j]);
- }
- printf("\nanswer:");
- for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
- {
- printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
- }
- printf("\n");
- printf("next update step:%d\n", updateStep);
- #endif
- osDelay(50);
- break;
- case UPDATE_STEP_END:
- updateStep = UPDATE_STEP_CHECK_VERSION;
- printf("update end\n");
- bmsUpdateFlag = 0;
- break;
- case UPDATE_STEP_RESET:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0xAA; //data
- pUpdateMsgSend[6] = 0x2F; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
- osDelay(50);
- resetCount++;
- if (resetCount >= 2)
- {
- updateStep = UPDATE_STEP_DOWNLOAD_BREAK_OFF;
- resetCount = 0;
- }
- else
- {
- updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
- }
- #ifdef USING_PRINTF
- printf("update error!!\n rest and start send data lenth again!!\n continue update!\n");
- #endif
- break;
- case UPDATE_STEP_DOWNLOAD_BREAK_OFF:
- dataLen = 1;
- updateMsgSendLen = 8;
- pUpdateMsgSend[0] = 0xEB; //start flag
- pUpdateMsgSend[1] = 0x01; //add flag
- pUpdateMsgSend[2] = 0x00; //write
- pUpdateMsgSend[3] = 0x04; //data len
- pUpdateMsgSend[4] = 0x81; //cmd
- pUpdateMsgSend[5] = 0xBB; //data
- pUpdateMsgSend[6] = 0x40; //check
- pUpdateMsgSend[7] = 0xF5; //end flag
- memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
- UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
- osDelay(50);
- updateStep = UPDATE_STEP_CHECK_VERSION;
- Cycle_conut++;
- break;
- case UPDATE_STEP_ERROR:
- updateStep = UPDATE_STEP_CHECK_VERSION;
- printf("update error end\n");
- bmsUpdateFlag = 0;
- break;
- default:
- updateStep = UPDATE_STEP_CHECK_VERSION;
- printf("update default end\n");
- bmsUpdateFlag = 0;
- break;
- }
- }
- }
- UINT8 SP_BMS_Update_CheckSUM(UINT8 *pSendData, UINT8 len)
- {
- UINT8 ret = 0;
- UINT8 i = 0;
- for (i = 0; i < len; i++)
- {
- ret += *(pSendData + i);
- }
- return ret & 0xFF;
- }
- //________________________________________________________________________________
- updateBMSStatus MS_BMS_Update_Service() //美顺BMS升级服务
- {
- #ifdef USING_PRINTF
- UINT8 ii = 0;
- #endif
- UINT8 errorCount = 0;
- UINT16 currentPackage = 0;
- UINT32 updateDataTotalByteLen = 0;
- UINT16 updateDataPackageCount = 0;
- UINT8 ReadNVMTemp[64];
- UpdateStep_MS_BMS updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER;
- UINT16 i, j = 0;
- UINT8 dataLen = 0;
- UINT8 ret0 = 0;
- updateBMSStatus ret = updateFailed;
- UINT8 pUpdateMsgSend[80];
- UINT32 updateMsgSendLen = 0;
- UINT32 updateMsgReadLen = 0;
- BOOL bmsUpdateFlag = TRUE;
- UINT8 bmsAnswerMsg[8];
- //static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
- UINT8 Cycle_conut = 0;
- UINT16 CRCtemp = 0;
- UINT8 headerLen = 5;
- UINT8 checkSum = 0x00;
- UINT8 checkSumCal = 0x00;
- UINT8 tempLen = 0x00;
- BSP_QSPI_Read_Safe(&checkSum, FLASH_BMS_FOTA_START_ADDR, 1);
- memset(ReadNVMTemp, 0, 64);
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1, 4); //data
- updateDataTotalByteLen = ((ReadNVMTemp[0] << 24) & 0xFF000000) | ((ReadNVMTemp[1] << 16) & 0xFF0000) | ((ReadNVMTemp[2] << 8) & 0xFF00) | (ReadNVMTemp[3] & 0xFF);
- updateDataPackageCount = (updateDataTotalByteLen + (64 - 1)) / 64; //进一法 e = (a+(b-1))/b
- for (i = 0; i < ((updateDataTotalByteLen + headerLen - 1) + (64 - 1)) / 64; i++) //
- {
- memset(ReadNVMTemp, 0, 64);
- if ((i + 1) * 64 <= (updateDataTotalByteLen + headerLen - 1))
- {
- tempLen = 64;
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1 + i * 64, 64);
- }
- else
- {
- tempLen = (updateDataTotalByteLen + headerLen - 1) - i * 64;
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1 + i * 64, tempLen);
- }
- for (j = 0; j < tempLen; j++)
- {
- checkSumCal = (checkSumCal + ReadNVMTemp[j]) & 0xFF;
- }
- osDelay(10);
- }
- if (checkSum != checkSumCal)
- {
- #ifdef USING_PRINTF
- printf("checksum error: checksum = %x, checksumCal = %x\n", checkSum, checkSumCal);
- #endif
- ret = updateErrorCheckSumError;
- return ret;
- }
- else
- {
- #ifdef USING_PRINTF
- printf("checksum OK: checksum = %x, checksumCal = %x\n", checkSum, checkSumCal);
- #endif
- }
- #ifdef USING_PRINTF
- printf(" bmsUpdateFlag = %x, Cycle_conut = %x\n", bmsUpdateFlag, Cycle_conut);
- #endif
- while (bmsUpdateFlag && Cycle_conut < 2)
- {
- #ifdef USING_PRINTF
- printf("update ms bms step %d\n:", updateStep);
- #endif
- switch (updateStep)
- {
- case MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER: //0x01
- dataLen = 0x00;
- updateMsgSendLen = 6 + dataLen;
- updateMsgReadLen = 8;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- pUpdateMsgSend[3] = dataLen; //data len
- //no data type
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
- pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step 1 answer,updateMsgReadLen = %x:\n", updateMsgReadLen);
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_UPDATE_REQUEST_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x02, answer data len:0x02
- {
- if (bmsAnswerMsg[4] == 0x00) //answer data byte1
- {
- if (bmsAnswerMsg[5] == 0x00) //answer data byte2
- {
- updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_INFO;
- errorCount = 0;
- }
- }
- else if (bmsAnswerMsg[4] == 0x01) //不允许升级
- {
- if (bmsAnswerMsg[5] == 0x01) //电量过低
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- ret = updateErrorBMSPowerLow;
- }
- else if (bmsAnswerMsg[5] == 0x02) //电池存在保护状态不允许升级
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- ret = updateErrorBMSWarningProtect;
- }
- else if (bmsAnswerMsg[5] == 0x03) //不支持升级
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- ret = updateErrorBMSNotSurport;
- }
- else if (bmsAnswerMsg[5] == 0x04) //当前电池处于充放电状态
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- ret = updateErrorBMSWorkState;
- }
- else
- {
- errorCount++;
- }
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- }
- osDelay(50);
- printf(" step 1 ret = %d\n", ret);
- break;
- case MS_UPDATE_STEP_SEND_FIRMWARE_INFO: //0x03
- dataLen = 52;
- updateMsgSendLen = 6 + dataLen;
- updateMsgReadLen = 7;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- pUpdateMsgSend[3] = dataLen; //data len
- memset(ReadNVMTemp, 0, 64);
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen, 16); //data
- MEMCPY(&pUpdateMsgSend[4], ReadNVMTemp, 16); //厂家信息,未开启校验
- MEMCPY(&pUpdateMsgSend[4 + 16], ReadNVMTemp, 16); //保护板硬件序列号,未开启校验
- pUpdateMsgSend[4 + 16 * 2 + 0] = (updateDataTotalByteLen >> 24) & 0xFF; //固件包大小
- pUpdateMsgSend[4 + 16 * 2 + 1] = (updateDataTotalByteLen >> 16) & 0xFF;
- pUpdateMsgSend[4 + 16 * 2 + 2] = (updateDataTotalByteLen >> 8) & 0xFF;
- pUpdateMsgSend[4 + 16 * 2 + 3] = (updateDataTotalByteLen)&0xFF;
- MEMCPY(&pUpdateMsgSend[4 + 16 * 2 + 4], ReadNVMTemp, 16); // 固件包头信息,未开启校验
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
- pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step 3 answer:\n");
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_INFO_CHECK_AND_UPDATE_REQEST_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
- {
- if (bmsAnswerMsg[4] == 0x00) //answer data byte1
- {
- updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
- errorCount = 0;
- }
- else if (bmsAnswerMsg[4] == 0x01) //厂家信息错误
- {
- errorCount++;
- ret = updateErrorFirmwareInfoError;
- }
- else if (bmsAnswerMsg[4] == 0x02) //硬件序列号不匹配
- {
- errorCount++;
- ret = updateErrorFirmwareInfoError;
- }
- else if (bmsAnswerMsg[4] == 0x03) //固件大小超出范围
- {
- errorCount++;
- ret = updateErrorFirmwareSizeError;
- }
- else if (bmsAnswerMsg[4] == 0x04) //固件包头信息错误
- {
- errorCount++;
- ret = updateErrorFirmwareInfoError;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- }
- printf(" step 3 ret = %d\n", ret);
- osDelay(50);
- break;
- case MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST: //0x05
- dataLen = 0;
- updateMsgSendLen = 6 + dataLen;
- updateMsgReadLen = 8;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- pUpdateMsgSend[3] = dataLen; //data len
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
- pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step 5 answer:\n");
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_EREASE_FLASH_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x06, answer data len:0x02
- {
- if (bmsAnswerMsg[4] == 0x00) //answer data byte1, erease successed
- {
- updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA; //0x07
- errorCount = 0;
- }
- else if (bmsAnswerMsg[4] == 0x01) //擦除失败
- {
- errorCount++;
- ret = updateErrorAppErease;
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount > 10)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- }
- osDelay(50);
- break;
- case MS_UPDATE_STEP_SEND_UPDATE_DATA: //0x07
- updateMsgReadLen = 7;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- for (i = 0; i < updateDataPackageCount; i++)
- {
- memset(ReadNVMTemp, 0, 64);
- if ((i + 1) * 64 <= (updateDataTotalByteLen))
- {
- tempLen = 64;
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen + i * 64, 64);
- }
- else
- {
- tempLen = (updateDataTotalByteLen)-i * 64; //
- BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen + i * 64, tempLen);
- }
- CRCtemp = MS_BMS_Update_CRC16(ReadNVMTemp, tempLen);
- dataLen = tempLen + 6; //data len =count(2+2 byte) + crc(2byte) + update data len
- updateMsgSendLen = 6 + dataLen; // updateMsgSendLen = data len + header len(6byte)
- pUpdateMsgSend[3] = dataLen; //data len
- pUpdateMsgSend[4] = ((i + 1) >> 8) & 0xFF; //当前包序号,大端模式
- pUpdateMsgSend[5] = (i + 1) & 0xFF;
- pUpdateMsgSend[6] = (updateDataPackageCount >> 8) & 0xFF;
- pUpdateMsgSend[7] = updateDataPackageCount & 0xFF;
- pUpdateMsgSend[8] = (CRCtemp >> 8) & 0xFF; // data CRC High
- pUpdateMsgSend[9] = CRCtemp & 0xFF; //data CRC Low
- MEMCPY(&pUpdateMsgSend[4 + 6], ReadNVMTemp, 64); //升级数据,64字节
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
- pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step 7 answer:\n");
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_UPDATE_DATA_WRITE_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
- {
- if (bmsAnswerMsg[4] == 0x00) //answer data byte1,接收并操作成功
- {
- updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
- errorCount = 0;
- }
- else if (bmsAnswerMsg[4] == 0x01) //固件块校验失败
- {
- errorCount = 10;
- ret = updateErrorPackageCRC;
- }
- else if (bmsAnswerMsg[4] == 0x02) //烧写失败
- {
- errorCount = 10;
- ret = updateErrorPackageWrite;
- }
- else if (bmsAnswerMsg[4] == 0x03) //固件块编号异常
- {
- errorCount = 10;
- ret = updateErrorPackageNo;
- }
- else
- {
- errorCount = 10;
- }
- }
- else
- {
- errorCount = 10;
- }
- }
- else
- {
- errorCount = 10;
- }
- }
- else
- {
- errorCount = 10;
- }
- if (errorCount >= 10)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- i--;
- break;
- }
- osDelay(50);
- }
- if (i == updateDataPackageCount)
- {
- updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP;
- }
- break;
- case MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP: //0x09
- dataLen = 0x00;
- updateMsgSendLen = 6 + dataLen;
- updateMsgReadLen = 7;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- pUpdateMsgSend[3] = dataLen; //data len
- //no data type
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
- pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step 9 answer:\n");
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_JUMP_TO_APP_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x0A, answer data len:0x01
- {
- if (bmsAnswerMsg[4] == 0x00) //answer data byte1, update succeed
- {
- errorCount = 0;
- updateStep = MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE; //0x0B
- }
- else if (bmsAnswerMsg[4] == 0x01) //升级失败
- {
- errorCount = 10;
- ret = updateFailed;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount >= 10)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- }
- osDelay(50);
- break;
- case MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE: //0x0B
- dataLen = 0x00;
- updateMsgSendLen = 6 + dataLen;
- updateMsgReadLen = 8;
- pUpdateMsgSend[0] = 0x01; //node byte
- pUpdateMsgSend[1] = 0x40; //func byte
- pUpdateMsgSend[2] = updateStep; //cmd byte
- pUpdateMsgSend[3] = dataLen; //data len
- //no data type
- CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
- pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
- pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
- memset((UINT8 *)(bmsAnswerMsg), 0, 8);
- ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
- #ifdef USING_PRINTF
- printf("update step A answer:\n");
- for (ii = 0; ii < updateMsgReadLen; ii++)
- printf("%x ", bmsAnswerMsg[ii]);
- printf("\nret0 = %d", ret0);
- printf("\n");
- #endif
- if (ret0 != 0)
- {
- if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
- {
- if (bmsAnswerMsg[2] == MS_UPDATE_STEP_CURRENT_RUNNING_MODE_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x0C, answer data len:0x02
- {
- if (bmsAnswerMsg[4] == 0x01) //answer data byte1, update succeed, app is running
- {
- errorCount = 0;
- updateStep = MS_UPDATE_STEP_END;
- }
- else if (bmsAnswerMsg[4] == 0x00) //update failed , boot is running,error
- {
- errorCount = 10;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- }
- else
- {
- errorCount++;
- }
- if (errorCount >= 3)
- {
- updateStep = MS_UPDATE_STEP_ERROR;
- errorCount = 0;
- }
- osDelay(50);
- break;
- case MS_UPDATE_STEP_END: //0x0D
- errorCount = 0;
- bmsUpdateFlag = FALSE;
- ret = updateOK;
- break;
- case MS_UPDATE_STEP_ERROR: //0x0E
- errorCount = 0;
- bmsUpdateFlag = true;
- Cycle_conut++;
- if (Cycle_conut > 2)
- {
- ret = updateErrorTimeout;
- bmsUpdateFlag = FALSE;
- }
- break;
- default:
- bmsUpdateFlag = FALSE;
- break;
- }
- }
- #ifdef USING_PRINTF
- printf("last ret = %x\n", ret);
- #endif
- return ret;
- }
- static void __invert_uint8(UINT8 *dBuf, UINT8 *srcBuf)
- {
- int i;
- UINT8 tmp[4];
- tmp[0] = 0;
- for (i = 0; i < 8; i++)
- {
- if (srcBuf[0] & (1 << i))
- {
- tmp[0] |= 1 << (7 - i);
- }
- }
- dBuf[0] = tmp[0];
- }
- static void __invert_uint16(UINT16 *dBuf, UINT16 *srcBuf)
- {
- int i;
- UINT16 tmp[4];
- tmp[0] = 0;
- for (i = 0; i < 16; i++)
- {
- if (srcBuf[0] & (1 << i))
- {
- tmp[0] |= 1 << (15 - i);
- }
- }
- dBuf[0] = tmp[0];
- }
- UINT16 MS_BMS_Update_CRC16(UINT8 *pSendData, UINT16 len)
- {
- UINT16 wCRCin = 0xFFFF;
- UINT16 wCPoly = 0x8005;
- UINT8 wChar = 0;
- UINT16 crc_rslt = 0;
- int i;
- while (len--)
- {
- wChar = *(pSendData++);
- __invert_uint8(&wChar, &wChar);
- wCRCin ^= (wChar << 8);
- for (i = 0; i < 8; i++)
- {
- if (wCRCin & 0x8000)
- {
- wCRCin = (wCRCin << 1) ^ wCPoly;
- }
- else
- {
- wCRCin = wCRCin << 1;
- }
- }
- }
- __invert_uint16(&wCRCin, &wCRCin);
- crc_rslt = ((wCRCin << 8) & 0xFF00) | ((wCRCin >> 8) & 0x00FF);
- return (crc_rslt);
- }
|