12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036 |
- /*
- * @Author : ChenJie
- * @Date : 2021-10-14 09:27:15
- * @LastEditors : ChenJie
- * @LastEditTime : 2021-10-27 11:03:53
- * @Description : file content
- * @FilePath : \PLAT\project\ec616_0h00\apps\qx_app\src\AppTaskUart.c
- */
- #include "AppTaskUart.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;
- 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:
- {
- UINT8 EncryptFlag = 0x00;
- UINT8 EncryptCount = 0;
- while (EncryptFlag != 0x01 && EncryptCount <= 3)
- {
- EncryptFlag = Uart_Encrypt_Send();
- EncryptCount++;
- }
- #ifdef USING_PRINTF
- 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)
- {
- UartErrorFlag = 0;
- UartRecvFlagCounter = 0;
- UartRecvFlag = 1;
- uartBattInfoDecode(UartAnswerData.data);
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
- break;
- }
- else if (Uart_Recv_LEN == 1) //接收的数据校验不过
- {
- UartRecvFlag = 0;
- UartRecvFlagCounter++;
- }
- else //没有接收到数据
- {
- UartRecvFlag = 0;
- UartRecvFlagCounter++;
- }
- if (UartRecvFlagCounter >= 10)
- {
- UartRecvFlagCounter = 0;
- uartBattInfoDecode(UartAnswerData.data);
- UartErrorFlag = 1;
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
- 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;
- 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;
- //控制充电禁止
- 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)) //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) // 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 && readLen == UartData.len)
- {
- 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);
- memset(pRead, 0x00, readLen);
- return 0;
- }
- #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 1;
- }
- }
- 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);
- }
|