AppTaskUart.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * @Author : ChenJie
  3. * @Date : 2021-10-14 09:27:15
  4. * @LastEditors : ChenJie
  5. * @LastEditTime : 2021-10-27 11:03:53
  6. * @Description : file content
  7. * @FilePath : \PLAT\project\ec616_0h00\apps\qx_app\src\AppTaskUart.c
  8. */
  9. #include "AppTaskUart.h"
  10. extern QueueHandle_t uartDataHandle;
  11. static StaticTask_t gProcess_Uart_Task_t;
  12. static UINT8 gProcess_Uart_TaskStack[PROC_UART_TASK_STACK_SIZE];
  13. static osThreadId_t UartTaskId = NULL;
  14. static process_Uart gProcess_Uart_Task = PROCESS_UART_STATE_IDLE;
  15. #define PROC_UART_STATE_SWITCH(a) (gProcess_Uart_Task = a)
  16. static UINT8 Uart_WriteCmd_func(UartWriteData_S UartWriteData);
  17. static UINT16 crc_chk(UINT8 *data, UINT8 length);
  18. UINT8 Uart_Encrypt_Send(void);
  19. void Uart_Cmd_Control(QueueHandle_t UartWriteCmdHandle, UartBuffer UartAnswerData);
  20. /**
  21. * @brief : Uart串口线程运行主函数
  22. * @param {void} *arg
  23. * @return {*}
  24. */
  25. static void UartTask(void *arg)
  26. {
  27. UINT16 Reg_Num = 0;
  28. UINT16 Uart_Recv_LEN;
  29. UINT8 UartRecvFlagCounter = 0;
  30. UartQueryType Uart_Read_Msg; //发送结构体初始化
  31. memset(&(Uart_Read_Msg), 0x00, sizeof(Uart_Read_Msg));
  32. UartBuffer UartAnswerData; //应答数据初始化
  33. memset(&(UartAnswerData), 0x00, sizeof(UartBuffer));
  34. UartWriteData_S UartWriteDataHandleRecv;
  35. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
  36. UINT8 ret = 0x00;
  37. if (UartWriteCmdHandle == NULL) //Uart控制命令传输指针
  38. {
  39. UartWriteCmdHandle = osMessageQueueNew(1, sizeof(UartWriteData_S), NULL);
  40. }
  41. while (1)
  42. {
  43. switch (gProcess_Uart_Task)
  44. {
  45. case PROCESS_UART_STATE_INTI:
  46. {
  47. hal_uart_hardware_config_t hwConfig = {
  48. ARM_POWER_FULL,
  49. ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 |
  50. ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 |
  51. ARM_USART_FLOW_CONTROL_NONE,
  52. 9600U};
  53. HAL_UART_ResetUartSetting(PORT_USART_1, &hwConfig, TRUE);
  54. osDelay(100);
  55. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
  56. break;
  57. }
  58. case PROCESS_UART_STATE_ENCRYPT:
  59. {
  60. UINT8 EncryptFlag = 0x00;
  61. UINT8 EncryptCount = 0;
  62. while (EncryptFlag != 0x01 && EncryptCount <= 3)
  63. {
  64. EncryptFlag = Uart_Encrypt_Send();
  65. EncryptCount++;
  66. }
  67. #ifdef USING_PRINTF
  68. printf("EncryptFlag:%d\n", EncryptFlag);
  69. #endif
  70. if (EncryptFlag == 0x01)
  71. {
  72. ihd_st_authFaild = 0;
  73. }
  74. else
  75. {
  76. ihd_st_authFaild = 1;
  77. }
  78. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  79. break;
  80. }
  81. case PROCESS_UART_STATE_IDLE:
  82. {
  83. osDelay(100);
  84. if (UartRecvFlag == 1) //接收到数据才进行控制判定
  85. {
  86. Uart_Cmd_Control(UartWriteCmdHandle, UartAnswerData); //电池锁定,继电器锁定,加热控制
  87. }
  88. if (TimeCounter % 10 == 0 && gProcess_app == WORK)
  89. {
  90. ECOMM_TRACE(UNILOG_PLA_APP, UartAppTask_83, P_SIG, 0, "Uart work begin:%02x", PadInterrupt);
  91. memset(&(UartWriteDataHandleRecv), 0x00, sizeof(UartWriteDataHandleRecv));
  92. if (osMessageQueueGet(UartWriteCmdHandle, &UartWriteDataHandleRecv, 0, 10) == osOK && UartRecvFlag == 1)
  93. {
  94. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_WRITE);
  95. break;
  96. }
  97. else
  98. {
  99. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
  100. break;
  101. }
  102. }
  103. else if (gProcess_app == LISTEN)
  104. {
  105. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
  106. break;
  107. }
  108. if (BMS_Fota_update_flag)
  109. {
  110. if (BattWorkStateDelay == 0x00)
  111. {
  112. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_UPDATE);
  113. break;
  114. }
  115. }
  116. break;
  117. }
  118. case PROCESS_UART_STATE_READ:
  119. {
  120. UINT16 CRC_chk_buffer;
  121. Reg_Num = 0x21 + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP; //按照协议里面的0x21+X+N的结束地址
  122. Uart_Read_Msg.Bms_Address = BMS_ADDRESS_CODE;
  123. Uart_Read_Msg.Bms_Funcode = UART_READ_CODE;
  124. Uart_Read_Msg.Reg_Begin_H = 0x00;
  125. Uart_Read_Msg.Reg_Begin_L = 0x00;
  126. Uart_Read_Msg.Reg_Num_H = Reg_Num >> 8;
  127. Uart_Read_Msg.Reg_Num_L = Reg_Num;
  128. memset((UINT8 *)&(UartAnswerData), 0x00, sizeof(UartBuffer));
  129. CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Read_Msg, 6);
  130. Uart_Read_Msg.CRC_L = CRC_chk_buffer;
  131. Uart_Read_Msg.CRC_H = CRC_chk_buffer >> 8;
  132. Uart_Recv_LEN = UartAppTrasmit((UINT8 *)&Uart_Read_Msg, sizeof(Uart_Read_Msg), (UINT8 *)&UartAnswerData, Reg_Num * 2 + 5, 1000);
  133. #ifdef USING_PRINTF1
  134. printf("[%d]Uart_Recv_buffer-%d: ", __LINE__, Uart_Recv_LEN);
  135. #endif
  136. #ifdef USING_PRINTF1
  137. printf("[%d]Uart_Recv_buffer-%d: ", __LINE__, Uart_Recv_LEN);
  138. for (int i = 0; i < Uart_Recv_LEN - 5; i++)
  139. {
  140. printf("%2x - %x ", i, *((UINT8 *)&UartAnswerData.data + i));
  141. }
  142. printf("\n");
  143. #endif
  144. if (Uart_Recv_LEN > 5)
  145. {
  146. UartErrorFlag = 0;
  147. UartRecvFlagCounter = 0;
  148. UartRecvFlag = 1;
  149. uartBattInfoDecode(UartAnswerData.data);
  150. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  151. break;
  152. }
  153. else if (Uart_Recv_LEN == 1) //接收的数据校验不过
  154. {
  155. UartRecvFlag = 0;
  156. UartRecvFlagCounter++;
  157. }
  158. else //没有接收到数据
  159. {
  160. UartRecvFlag = 0;
  161. UartRecvFlagCounter++;
  162. }
  163. if (UartRecvFlagCounter >= 10)
  164. {
  165. UartRecvFlagCounter = 0;
  166. uartBattInfoDecode(UartAnswerData.data);
  167. UartErrorFlag = 1;
  168. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
  169. break;
  170. }
  171. break;
  172. }
  173. case PROCESS_UART_STATE_WRITE:
  174. {
  175. UartCmdRecvFlag = Uart_WriteCmd_func(UartWriteDataHandleRecv);
  176. osDelay(500);
  177. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_READ);
  178. break;
  179. }
  180. case PROCESS_UART_STATE_UPDATE:
  181. {
  182. BMSupdatestatus = 0xFF;
  183. UartRecvFlag = 0;
  184. #if BMS_MANUFACTURE == 1
  185. BMSupdatestatus = SP_BMS_Update_Service();
  186. #elif BMS_MANUFACTURE == 2
  187. BMSupdatestatus = MS_BMS_Update_Service();
  188. #endif
  189. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  190. BMS_Fota_update_flag = FALSE;
  191. break;
  192. }
  193. case PROCESS_UART_STATE_SLEEP:
  194. {
  195. #ifdef USING_PRINTF
  196. printf("Uart silence begin\n");
  197. #endif
  198. UartRecvFlag = 0;
  199. while (true)
  200. {
  201. osDelay(100);
  202. if (gProcess_app == WORK)
  203. {
  204. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_INTI);
  205. break;
  206. }
  207. }
  208. break;
  209. }
  210. default:
  211. {
  212. PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
  213. break;
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * @brief : 串口写入发送函数
  220. * @param {UartWriteData_S} UartWriteData
  221. * @return {*}
  222. */
  223. static UINT8 Uart_WriteCmd_func(UartWriteData_S UartWriteData)
  224. {
  225. UartWriteMsgType Uart_Write_Msg;
  226. UINT16 RegAddress = 0x0000;
  227. UINT16 CRC_chk_buffer, Uart_Recv_LEN = 0;
  228. UINT8 Uart_Recv_Buffer[16] = {0};
  229. switch (UartWriteData.WriteCmd)
  230. {
  231. case 0x01: //是否锁定
  232. {
  233. RegAddress = 0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
  234. break;
  235. }
  236. case 0x02: //是否加热
  237. {
  238. RegAddress = 0x1C + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
  239. break;
  240. }
  241. case 0x04: //是否继电器控制
  242. {
  243. RegAddress = 0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP;
  244. break;
  245. }
  246. default:
  247. {
  248. UartWriteData.WriteCmd = 0x00;
  249. return 0;
  250. }
  251. }
  252. Uart_Write_Msg.Bms_Address = BMS_ADDRESS_CODE;
  253. Uart_Write_Msg.Bms_Funcode = UART_WRITE_CODE;
  254. Uart_Write_Msg.Reg_Begin_H = RegAddress >> 8;
  255. Uart_Write_Msg.Reg_Begin_L = RegAddress;
  256. Uart_Write_Msg.Reg_Num_H = 0x00;
  257. Uart_Write_Msg.Reg_Num_L = 0x01;
  258. Uart_Write_Msg.Data_Count = 0x02; //要写入的字节数
  259. memcpy(Uart_Write_Msg.Data, UartWriteData.Data, 2);
  260. CRC_chk_buffer = crc_chk((UINT8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg) - 2);
  261. Uart_Write_Msg.CRC_L = CRC_chk_buffer;
  262. Uart_Write_Msg.CRC_H = CRC_chk_buffer >> 8;
  263. Uart_Recv_LEN = UartAppTrasmit((UINT8 *)&Uart_Write_Msg, sizeof(Uart_Write_Msg), (UINT8 *)&Uart_Recv_Buffer, 8, 1000);
  264. if (Uart_Recv_Buffer[1] == 0x10)
  265. {
  266. return UartWriteData.WriteCmd;
  267. }
  268. else
  269. {
  270. return 0;
  271. }
  272. }
  273. /**
  274. * @brief : 串口控制函数判断,含加热开启关闭,充电禁止允许,放电禁止允许,继电器禁止允许
  275. * @param {QueueHandle_t} UartWriteCmdHandle
  276. * @param {UartBuffer} UartAnswerData
  277. * @return {*}
  278. */
  279. void Uart_Cmd_Control(QueueHandle_t UartWriteCmdHandle, UartBuffer UartAnswerData)
  280. {
  281. UartWriteData_S UartWriteData;
  282. memset(&(UartWriteData), 0x00, sizeof(UartWriteData_S)); //Uart控制命令初始化
  283. UINT8 HeatSwitch = 0;
  284. UINT8 ChargePermitState = 1;
  285. UINT8 DischargePermitState = 1;
  286. UINT8 RelayControlState = 0;
  287. ChargePermitState = getbit((UartAnswerData.data[(0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 1); //充电允许状态,1-允许,0禁止
  288. DischargePermitState = getbit((UartAnswerData.data[(0x1B + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 0); //放电允许状态,1-允许,0禁止
  289. RelayControlState = getbit((UartAnswerData.data[(0x09 + AppDataInfo.BattCellCount + AppDataInfo.BattTempCount + BMS_OTHER_TEMP) * 2 + 1]), 0); //继电器状态,1-继电器断开,0-继电器吸合
  290. //控制集中处理
  291. UINT8 BcuFltAct = 0;
  292. BcuFltAct = sfmd_st_fltAct;
  293. //控制充电禁止
  294. if (getbit(BcuFltAct, 6) == 0)
  295. {
  296. if ((maxCellVol > 4190 && maxCellVol < 5000) && battSOC >= 99)
  297. {
  298. ChargeForbiddenControl = 1;
  299. }
  300. else if (maxCellVol < 4150 && maxCellVol > 0)
  301. {
  302. ChargeForbiddenControl = 0;
  303. }
  304. }
  305. else if (getbit(BcuFltAct, 6) == 1)
  306. {
  307. ChargeForbiddenControl = 1;
  308. }
  309. //控制放电禁止
  310. if (getbit(BcuFltAct, 5) == 0)
  311. {
  312. if (AppDataInfo.BattLock == FALSE)
  313. {
  314. DisChargeForbiddenControl = 0;
  315. }
  316. else if (AppDataInfo.BattLock == TRUE)
  317. {
  318. DisChargeForbiddenControl = 1;
  319. }
  320. }
  321. else if (getbit(BcuFltAct, 5) == 1)
  322. {
  323. DisChargeForbiddenControl = 1;
  324. }
  325. //控制继电器断开
  326. if (RelayForceControl == 0)
  327. {
  328. if (AppDataInfo.RelayControl == TRUE)
  329. {
  330. RelayForbiddenControl = 1;
  331. }
  332. else if (AppDataInfo.RelayControl == FALSE)
  333. {
  334. if (getbit(BcuFltAct, 3) == 0)
  335. {
  336. RelayForbiddenControl = 0;
  337. }
  338. else if (getbit(BcuFltAct, 3) == 1)
  339. {
  340. RelayForbiddenControl = 1;
  341. }
  342. }
  343. }
  344. else if (RelayForceControl == 1) //close
  345. {
  346. RelayForbiddenControl = 0;
  347. AppDataInfo.appDataModify = TRUE;
  348. AppDataInfo.RelayControl = FALSE;
  349. }
  350. else if (RelayForceControl == 2) //open
  351. {
  352. RelayForbiddenControl = 1;
  353. AppDataInfo.appDataModify = TRUE;
  354. AppDataInfo.RelayControl = TRUE;
  355. }
  356. //控制命令发送
  357. if (TimeCounter % 10 == 0 && BattHeaterSwitch(&HeatSwitch, HeatForceControl) == TRUE)
  358. {
  359. UartWriteData.WriteCmd = 0x02;
  360. UartWriteData.Data[0] = 0x00;
  361. UartWriteData.Data[1] = HeatSwitch & 0xFF;
  362. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  363. }
  364. if (ChargeForbiddenControl == 0x01 && (ChargePermitState != 0x00)) //try to lock lock the charge
  365. {
  366. #ifdef USING_PRINTF1
  367. printf("[%d]try to lock charge \n", __LINE__);
  368. #endif
  369. UartWriteData.WriteCmd = 0x01;
  370. UartWriteData.Data[0] = 0x00 | (RelayControlState << 7);
  371. UartWriteData.Data[1] = 0x00 | DischargePermitState;
  372. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  373. }
  374. else if ((ChargeForbiddenControl == 0x00) && (ChargePermitState != 0x01)) //try to unlock lock the charge
  375. {
  376. #ifdef USING_PRINTF1
  377. printf("[%d]try to unlock charge \n", __LINE__);
  378. #endif
  379. UartWriteData.WriteCmd = 0x01;
  380. UartWriteData.Data[0] = 0x00 | (RelayControlState << 7);
  381. UartWriteData.Data[1] = 0x02 | DischargePermitState;
  382. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  383. }
  384. if ((BattWorkStateDelay == BATT_IDLE_SYM) && (DisChargeForbiddenControl == 1) && (DischargePermitState) != 0x00) //try to lock lock the discharge
  385. {
  386. UartWriteData.WriteCmd = 0x01;
  387. UartWriteData.Data[0] = (RelayControlState << 7) | 0x00;
  388. UartWriteData.Data[1] = (ChargePermitState << 1) | 0x00;
  389. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  390. }
  391. else if ((DisChargeForbiddenControl == 0) && (DischargePermitState) != 0x01) // try to unlock
  392. {
  393. UartWriteData.WriteCmd = 0x01;
  394. UartWriteData.Data[0] = (RelayControlState << 7) | 0x00;
  395. UartWriteData.Data[1] = (ChargePermitState << 1) | 0x01;
  396. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  397. }
  398. if (RELAYCONFIG == 1)
  399. {
  400. if ((RelayForbiddenControl == 1) && (RelayControlState == 0x00)) //将继电器断开
  401. {
  402. #ifdef USING_PRINTF1
  403. printf("[%d]try to cut relay \n", __LINE__);
  404. #endif
  405. UartWriteData.WriteCmd = 0x04;
  406. UartWriteData.Data[0] = 0x80;
  407. UartWriteData.Data[1] = 0x00 | (ChargePermitState << 1) | DischargePermitState;
  408. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  409. }
  410. else if ((RelayForbiddenControl == 0) && (RelayControlState == 0x01)) //继电器闭合
  411. {
  412. #ifdef USING_PRINTF1
  413. printf("[%d]try to close relay \n", __LINE__);
  414. #endif
  415. UartWriteData.WriteCmd = 0x04;
  416. UartWriteData.Data[0] = 0x00;
  417. UartWriteData.Data[1] = 0x00 | (ChargePermitState << 1) | DischargePermitState;
  418. osMessageQueuePut(UartWriteCmdHandle, &UartWriteData, 0, 10);
  419. }
  420. }
  421. }
  422. /**
  423. * @brief :串口传输函数
  424. * @param {UINT8} *pSend
  425. * @param {UINT32} sendLen
  426. * @param {UINT8} *pRead
  427. * @param {UINT32} readLen
  428. * @param {UINT32} timeout
  429. * @return {*}
  430. */
  431. UINT8 UartAppTrasmit(UINT8 *pSend, UINT32 sendLen, UINT8 *pRead, UINT32 readLen, UINT32 timeout)
  432. {
  433. UINT16 CRC_Rece_buffer = 0x00;
  434. UINT16 CRC_chk_buffer = 0xff;
  435. HAL_UART_SendStr(PORT_USART_1, pSend, sendLen);
  436. if (readLen > 0)
  437. {
  438. UartBuffer UartData;
  439. osStatus_t ret = osMessageQueueGet(uartDataHandle, &UartData, 0, timeout);
  440. if (ret == osOK && readLen == UartData.len)
  441. {
  442. memcpy(pRead, (UINT8 *)&UartData, UartData.len);
  443. CRC_Rece_buffer = *(pRead + UartData.len - 1) << 8 | *(pRead + UartData.len - 2);
  444. CRC_chk_buffer = crc_chk(pRead, UartData.len - 2);
  445. if (*(pRead + 0) != 0x01 || CRC_Rece_buffer != CRC_chk_buffer)
  446. {
  447. hal_uart_hardware_config_t hwConfig = {
  448. ARM_POWER_FULL,
  449. ARM_USART_MODE_ASYNCHRONOUS | ARM_USART_DATA_BITS_8 |
  450. ARM_USART_PARITY_NONE | ARM_USART_STOP_BITS_1 |
  451. ARM_USART_FLOW_CONTROL_NONE,
  452. 9600U};
  453. HAL_UART_ResetUartSetting(PORT_USART_1, &hwConfig, TRUE);
  454. memset(pRead, 0x00, readLen);
  455. return 0;
  456. }
  457. #ifdef USING_PRINTF1
  458. printf("Uart recv:%d-%d\n", UartData.len, readLen);
  459. for (UINT8 i = 0; i < UartData.len; i++)
  460. {
  461. printf("%x ", *(pRead + i));
  462. }
  463. printf("\n");
  464. #endif
  465. return UartData.len;
  466. }
  467. else
  468. {
  469. memset(pRead, 0x00, readLen);
  470. return 0;
  471. }
  472. }
  473. else
  474. {
  475. return 1;
  476. }
  477. }
  478. UINT8 Uart_Encrypt_Send()
  479. {
  480. UINT8 SeedNumberArrray[4] = {0x38, 0x56, 0xfe, 0xac};
  481. UINT16 EncodeNumberArray[4];
  482. UINT8 UartEncryptBuffer[17];
  483. UINT8 UartDecryptBuffer[5];
  484. UINT16 CRC_chk_buffer;
  485. UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
  486. UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
  487. UartEncryptBuffer[2] = 0x0c;
  488. for (int i = 0; i < 4; i++)
  489. {
  490. SeedNumberArrray[i] = rand();
  491. EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
  492. UartEncryptBuffer[i + 3] = SeedNumberArrray[i];
  493. UartEncryptBuffer[i * 2 + 7] = EncodeNumberArray[i] >> 8;
  494. UartEncryptBuffer[i * 2 + 8] = EncodeNumberArray[i];
  495. }
  496. CRC_chk_buffer = crc_chk(UartEncryptBuffer, 17 - 2);
  497. UartEncryptBuffer[15] = CRC_chk_buffer;
  498. UartEncryptBuffer[16] = CRC_chk_buffer >> 8;
  499. UartAppTrasmit(UartEncryptBuffer, 17, (UINT8 *)&UartDecryptBuffer, 5, 1000);
  500. #ifdef USING_PRINTF1
  501. printf("UartDecryptBuffer:\n");
  502. for (UINT8 i = 0; i < 5; i++)
  503. {
  504. printf("%x ", UartDecryptBuffer[i]);
  505. }
  506. printf("\n\n\n");
  507. #endif
  508. return UartDecryptBuffer[2];
  509. }
  510. static UINT16 crc_chk(UINT8 *data, UINT8 length)
  511. {
  512. UINT8 j;
  513. UINT16 reg_crc = 0xFFFF;
  514. while (length--)
  515. {
  516. reg_crc ^= *data++;
  517. for (j = 0; j < 8; j++)
  518. {
  519. if (reg_crc & 0x01)
  520. {
  521. reg_crc = (reg_crc >> 1) ^ 0xA001;
  522. }
  523. else
  524. {
  525. reg_crc = reg_crc >> 1;
  526. }
  527. }
  528. }
  529. return reg_crc;
  530. }
  531. void AppTaskUartInit(void *arg)
  532. {
  533. if (uartDataHandle == NULL)
  534. {
  535. uartDataHandle = osMessageQueueNew(1, sizeof(UartBuffer), NULL);
  536. if (uartDataHandle == NULL)
  537. return;
  538. }
  539. Usart1Handler(9600U);
  540. osThreadAttr_t task_attr;
  541. memset(&task_attr, 0, sizeof(task_attr));
  542. memset(gProcess_Uart_TaskStack, 0xA5, PROC_UART_TASK_STACK_SIZE);
  543. task_attr.name = "Uart_Task";
  544. task_attr.stack_mem = gProcess_Uart_TaskStack;
  545. task_attr.stack_size = PROC_UART_TASK_STACK_SIZE;
  546. task_attr.priority = osPriorityBelowNormal7;
  547. task_attr.cb_mem = &gProcess_Uart_Task_t;
  548. task_attr.cb_size = sizeof(StaticTask_t);
  549. UartTaskId = osThreadNew(UartTask, NULL, &task_attr);
  550. }
  551. void AppTaskUartDeInit(void *arg)
  552. {
  553. osThreadTerminate(UartTaskId);
  554. UartTaskId = NULL;
  555. }
  556. /*-----------------------------------------------------------------------------*/
  557. void SP_BMS_Update_Service() //超力源BMS升级服务
  558. {
  559. UINT8 errorCount = 0;
  560. UINT8 resetCount = 0;
  561. UINT16 currentPackage = 0;
  562. UINT32 updateDataTotalByteLen = 0;
  563. UpdateStep updateStep = UPDATE_STEP_CHECK_VERSION;
  564. UINT8 i, j, ret = 0;
  565. UINT8 dataLen = 0;
  566. UINT8 pUpdateMsgSend[80];
  567. UINT32 updateMsgSendLen = 0;
  568. UINT32 currentPackageStartAddr = 0;
  569. BMS_Update_Recv_Msg_Type pUpdateMsgRecv;
  570. UINT8 bmsUpdateFlag = 1;
  571. //BMS_Update_Recv_Msg_Type bmsMsg;
  572. //static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
  573. UINT8 Cycle_conut = 0;
  574. while (bmsUpdateFlag && Cycle_conut < 2)
  575. {
  576. switch (updateStep)
  577. {
  578. case UPDATE_STEP_CHECK_VERSION:
  579. dataLen = 0;
  580. updateMsgSendLen = 7;
  581. pUpdateMsgSend[0] = 0xEB; //start flag
  582. pUpdateMsgSend[1] = 0x01; //add flag
  583. pUpdateMsgSend[2] = 0x01; //read
  584. pUpdateMsgSend[3] = 0x03; //data len
  585. pUpdateMsgSend[4] = 0x90; //cmd
  586. pUpdateMsgSend[5] = 0x93; //checksum
  587. pUpdateMsgSend[6] = 0xF5; //end flag
  588. //printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
  589. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  590. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  591. //printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
  592. if (ret != 0)
  593. {
  594. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  595. {
  596. if (pUpdateMsgRecv.cmd == 0x90)
  597. {
  598. if (pUpdateMsgRecv.data != 0xFF)
  599. {
  600. updateStep = UPDATE_STEP_REQUEST_UPDATE;
  601. errorCount = 0;
  602. }
  603. else
  604. {
  605. updateStep = UPDATE_STEP_SET_BAUD_RATE;
  606. errorCount = 0;
  607. }
  608. }
  609. else
  610. {
  611. errorCount++;
  612. }
  613. }
  614. else
  615. {
  616. errorCount++;
  617. }
  618. }
  619. else
  620. {
  621. errorCount++;
  622. }
  623. #ifdef USING_PRINTF1
  624. //printf("update step:%d\n",updateStep);
  625. printf("query:");
  626. for (j = 0; j < updateMsgSendLen; j++)
  627. {
  628. printf("%x ", pUpdateMsgSend[j]);
  629. }
  630. printf("\nanswer:");
  631. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  632. {
  633. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  634. }
  635. printf("\n");
  636. printf("next update step:%d\n", updateStep);
  637. #endif
  638. if (errorCount > 10)
  639. {
  640. updateStep = UPDATE_STEP_RESET;
  641. errorCount = 0;
  642. }
  643. osDelay(50);
  644. break;
  645. case UPDATE_STEP_REQUEST_UPDATE:
  646. dataLen = 1;
  647. updateMsgSendLen = 8;
  648. pUpdateMsgSend[0] = 0xEB; //start flag
  649. pUpdateMsgSend[1] = 0x01; //add flag
  650. pUpdateMsgSend[2] = 0x00; //write
  651. pUpdateMsgSend[3] = 0x04; //data len
  652. pUpdateMsgSend[4] = 0x80; //cmd
  653. pUpdateMsgSend[5] = 0x22; //data
  654. pUpdateMsgSend[6] = 0xA6; //check
  655. pUpdateMsgSend[7] = 0xF5; //end flag
  656. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  657. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  658. if (ret != 0)
  659. {
  660. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  661. {
  662. if (pUpdateMsgRecv.cmd == 0x80)
  663. {
  664. if (pUpdateMsgRecv.data == 0x33)
  665. {
  666. updateStep = UPDATE_STEP_START_UPDATE;
  667. errorCount = 0;
  668. }
  669. else
  670. {
  671. errorCount++;
  672. }
  673. }
  674. else
  675. {
  676. errorCount++;
  677. }
  678. }
  679. else
  680. {
  681. errorCount++;
  682. }
  683. }
  684. else
  685. {
  686. errorCount++;
  687. }
  688. if (errorCount > 10)
  689. {
  690. updateStep = UPDATE_STEP_RESET;
  691. errorCount = 0;
  692. }
  693. #ifdef USING_PRINTF1
  694. printf("update step:%d\n", updateStep);
  695. printf("query:");
  696. for (j = 0; j < updateMsgSendLen; j++)
  697. {
  698. printf("%x ", pUpdateMsgSend[j]);
  699. }
  700. printf("\nanswer:");
  701. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  702. {
  703. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  704. }
  705. printf("\n");
  706. printf("next update step:%d\n", updateStep);
  707. #endif
  708. osDelay(50);
  709. break;
  710. case UPDATE_STEP_START_UPDATE:
  711. dataLen = 1;
  712. updateMsgSendLen = 8;
  713. pUpdateMsgSend[0] = 0xEB; //start flag
  714. pUpdateMsgSend[1] = 0x01; //add flag
  715. pUpdateMsgSend[2] = 0x00; //write
  716. pUpdateMsgSend[3] = 0x04; //data len
  717. pUpdateMsgSend[4] = 0x80; //cmd
  718. pUpdateMsgSend[5] = 0x55; //data
  719. pUpdateMsgSend[6] = 0xD9; //check
  720. pUpdateMsgSend[7] = 0xF5; //end flag
  721. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  722. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
  723. //updateStep = UPDATE_STEP_SET_BAUD_RATE;
  724. updateStep = UPDATE_STEP_CHECK_VERSION_AGAIN; //2021-04-09跳过波特率设置
  725. #ifdef USING_PRINTF1
  726. printf("query:");
  727. for (j = 0; j < updateMsgSendLen; j++)
  728. {
  729. printf("%x ", pUpdateMsgSend[j]);
  730. }
  731. printf("\nanswer:");
  732. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  733. {
  734. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  735. }
  736. printf("\n");
  737. printf("next update step:%d\n", updateStep);
  738. #endif
  739. break;
  740. case UPDATE_STEP_CHECK_VERSION_AGAIN:
  741. dataLen = 0;
  742. updateMsgSendLen = 7;
  743. pUpdateMsgSend[0] = 0xEB; //start flag
  744. pUpdateMsgSend[1] = 0x01; //add flag
  745. pUpdateMsgSend[2] = 0x01; //read
  746. pUpdateMsgSend[3] = 0x03; //data len
  747. pUpdateMsgSend[4] = 0x90; //cmd
  748. pUpdateMsgSend[5] = 0x93; //checksum
  749. pUpdateMsgSend[6] = 0xF5; //end flag
  750. //printf("updateMsgSendLen0 = %x\n",updateMsgSendLen);
  751. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  752. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 100);
  753. //printf("updateMsgSendLen1 = %x\n",updateMsgSendLen);
  754. if (ret != 0)
  755. {
  756. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  757. {
  758. if (pUpdateMsgRecv.cmd == 0x90)
  759. {
  760. if (pUpdateMsgRecv.data != 0xFF)
  761. {
  762. updateStep = UPDATE_STEP_RESET;
  763. errorCount = 0;
  764. }
  765. else
  766. {
  767. updateStep = UPDATE_STEP_SET_BAUD_RATE;
  768. errorCount = 0;
  769. }
  770. }
  771. else
  772. {
  773. errorCount++;
  774. }
  775. }
  776. else
  777. {
  778. errorCount++;
  779. }
  780. }
  781. else
  782. {
  783. errorCount++;
  784. }
  785. #ifdef USING_PRINTF1
  786. //printf("update step:%d\n",updateStep);
  787. printf("query:");
  788. for (j = 0; j < updateMsgSendLen; j++)
  789. {
  790. printf("%x ", pUpdateMsgSend[j]);
  791. }
  792. printf("\nanswer:");
  793. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  794. {
  795. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  796. }
  797. printf("\n");
  798. printf("next update step:%d\n", updateStep);
  799. #endif
  800. if (errorCount > 10)
  801. {
  802. updateStep = UPDATE_STEP_RESET;
  803. errorCount = 0;
  804. }
  805. osDelay(50);
  806. break;
  807. case UPDATE_STEP_SET_BAUD_RATE:
  808. printf("start step %d\n", updateStep);
  809. dataLen = 4;
  810. updateMsgSendLen = 12;
  811. pUpdateMsgSend[0] = 0xEB; //start flag
  812. pUpdateMsgSend[1] = 0x01; //add flag
  813. pUpdateMsgSend[2] = 0x00; //write
  814. pUpdateMsgSend[3] = 0x08; //data len
  815. pUpdateMsgSend[4] = 0x81; //cmd
  816. pUpdateMsgSend[5] = 0x33; //data
  817. pUpdateMsgSend[6] = 0x00; //baud rate:9600
  818. pUpdateMsgSend[7] = 0x00;
  819. pUpdateMsgSend[8] = 0x25;
  820. pUpdateMsgSend[9] = 0x80;
  821. pUpdateMsgSend[10] = 0x61; //check
  822. pUpdateMsgSend[11] = 0xF5; //end flag
  823. #ifdef USING_PRINTF1
  824. printf("query:");
  825. for (j = 0; j < updateMsgSendLen; j++)
  826. {
  827. printf("%x ", pUpdateMsgSend[j]);
  828. }
  829. printf("\n");
  830. #endif
  831. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  832. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  833. printf("ret = %d\n", ret);
  834. if (ret != 0)
  835. {
  836. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  837. {
  838. if (pUpdateMsgRecv.cmd == 0x81)
  839. {
  840. if (pUpdateMsgRecv.data == 0x11)
  841. {
  842. updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
  843. errorCount = 0;
  844. }
  845. else
  846. {
  847. errorCount++;
  848. }
  849. }
  850. else
  851. {
  852. errorCount++;
  853. }
  854. }
  855. else
  856. {
  857. errorCount++;
  858. }
  859. }
  860. else
  861. {
  862. errorCount++;
  863. }
  864. if (errorCount > 10)
  865. {
  866. updateStep = UPDATE_STEP_RESET;
  867. errorCount = 0;
  868. }
  869. #ifdef USING_PRINTF1
  870. //printf("update step:%d\n",updateStep);
  871. printf("query:");
  872. for (j = 0; j < updateMsgSendLen; j++)
  873. {
  874. printf("%x ", pUpdateMsgSend[j]);
  875. }
  876. printf("\nanswer:");
  877. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  878. {
  879. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  880. }
  881. printf("\n");
  882. printf("next update step:%d\n", updateStep);
  883. #endif
  884. osDelay(50);
  885. break;
  886. case UPDATE_STEP_PREPARE_SEND_DATA_LEN:
  887. printf("start step %d\n", updateStep);
  888. dataLen = 1;
  889. updateMsgSendLen = 8;
  890. pUpdateMsgSend[0] = 0xEB; //start flag
  891. pUpdateMsgSend[1] = 0x01; //add flag
  892. pUpdateMsgSend[2] = 0x00; //write
  893. pUpdateMsgSend[3] = 0x04; //data len
  894. pUpdateMsgSend[4] = 0x81; //cmd
  895. pUpdateMsgSend[5] = 0x44; //data
  896. pUpdateMsgSend[6] = 0xC9; //check
  897. pUpdateMsgSend[7] = 0xF5; //end flag
  898. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  899. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  900. if (ret != 0)
  901. {
  902. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  903. {
  904. if (pUpdateMsgRecv.cmd == 0x81)
  905. {
  906. if (pUpdateMsgRecv.data == 0x11)
  907. {
  908. updateStep = UPDATE_STEP_SEND_DATA_LEN;
  909. errorCount = 0;
  910. }
  911. else
  912. {
  913. errorCount++;
  914. }
  915. }
  916. else
  917. {
  918. errorCount++;
  919. }
  920. }
  921. else
  922. {
  923. errorCount++;
  924. }
  925. }
  926. else
  927. {
  928. errorCount++;
  929. }
  930. if (errorCount > 10)
  931. {
  932. updateStep = UPDATE_STEP_RESET;
  933. errorCount = 0;
  934. }
  935. #ifdef USING_PRINTF1
  936. //printf("update step:%d\n",updateStep);
  937. printf("query:");
  938. for (j = 0; j < updateMsgSendLen; j++)
  939. {
  940. printf("%x ", pUpdateMsgSend[j]);
  941. }
  942. printf("\nanswer:");
  943. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  944. {
  945. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  946. }
  947. printf("\n");
  948. printf("next update step:%d\n", updateStep);
  949. #endif
  950. osDelay(50);
  951. break;
  952. case UPDATE_STEP_SEND_DATA_LEN:
  953. dataLen = 4;
  954. BSP_QSPI_Read_Safe(&updateDataTotalByteLen, FLASH_BMS_FOTA_START_ADDR, 4);
  955. updateDataTotalByteLen = (((updateDataTotalByteLen)&0xFF) << 24) | (((updateDataTotalByteLen >> 8) & 0xFF) << 16) | (((updateDataTotalByteLen >> 16) & 0xFF) << 8) | (((updateDataTotalByteLen >> 24) & 0xFF));
  956. updateMsgSendLen = 11;
  957. pUpdateMsgSend[0] = 0xEB; //start flag
  958. pUpdateMsgSend[1] = 0x01; //add flag
  959. pUpdateMsgSend[2] = 0x00; //write
  960. pUpdateMsgSend[3] = 0x07; //data len
  961. pUpdateMsgSend[4] = 0x82; //cmd
  962. pUpdateMsgSend[5] = (updateDataTotalByteLen >> 24) & 0xFF; //data: package byte len
  963. pUpdateMsgSend[6] = (updateDataTotalByteLen >> 16) & 0xFF;
  964. pUpdateMsgSend[7] = (updateDataTotalByteLen >> 8) & 0xFF;
  965. pUpdateMsgSend[8] = (updateDataTotalByteLen)&0xFF;
  966. pUpdateMsgSend[9] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen + 2); //check sum
  967. pUpdateMsgSend[10] = 0xF5; //end flag
  968. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  969. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  970. if (ret != 0)
  971. {
  972. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  973. {
  974. if (pUpdateMsgRecv.cmd == 0x81)
  975. {
  976. if (pUpdateMsgRecv.data == 0x11)
  977. {
  978. updateStep = UPDATE_STEP_PREPARE_SEND_UPDATE_DATA;
  979. errorCount = 0;
  980. }
  981. else
  982. {
  983. errorCount++;
  984. }
  985. }
  986. else
  987. {
  988. errorCount++;
  989. }
  990. }
  991. else
  992. {
  993. errorCount++;
  994. }
  995. }
  996. else
  997. {
  998. errorCount++;
  999. }
  1000. if (errorCount > 10)
  1001. {
  1002. updateStep = UPDATE_STEP_RESET;
  1003. errorCount = 0;
  1004. }
  1005. #ifdef USING_PRINTF1
  1006. //printf("update step:%d\n",updateStep);
  1007. printf("query:");
  1008. for (j = 0; j < updateMsgSendLen; j++)
  1009. {
  1010. printf("%x ", pUpdateMsgSend[j]);
  1011. }
  1012. printf("\nanswer:");
  1013. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  1014. {
  1015. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  1016. }
  1017. printf("\n");
  1018. printf("next update step:%d\n", updateStep);
  1019. #endif
  1020. osDelay(50);
  1021. break;
  1022. case UPDATE_STEP_PREPARE_SEND_UPDATE_DATA:
  1023. dataLen = 1;
  1024. updateMsgSendLen = 8;
  1025. pUpdateMsgSend[0] = 0xEB; //start flag
  1026. pUpdateMsgSend[1] = 0x01; //add flag
  1027. pUpdateMsgSend[2] = 0x00; //write
  1028. pUpdateMsgSend[3] = 0x04; //data len
  1029. pUpdateMsgSend[4] = 0x81; //cmd
  1030. pUpdateMsgSend[5] = 0x55; //data
  1031. pUpdateMsgSend[6] = 0xDA; //check
  1032. pUpdateMsgSend[7] = 0xF5; //end flag
  1033. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1034. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  1035. if (ret != 0)
  1036. {
  1037. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  1038. {
  1039. if (pUpdateMsgRecv.cmd == 0x81)
  1040. {
  1041. if (pUpdateMsgRecv.data == 0x11)
  1042. {
  1043. updateStep = UPDATE_STEP_SEND_UPDATE_DATA;
  1044. errorCount = 0;
  1045. }
  1046. else
  1047. {
  1048. errorCount++;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. errorCount++;
  1054. }
  1055. }
  1056. else
  1057. {
  1058. errorCount++;
  1059. }
  1060. }
  1061. else
  1062. {
  1063. errorCount++;
  1064. }
  1065. if (errorCount > 10)
  1066. {
  1067. updateStep = UPDATE_STEP_RESET;
  1068. errorCount = 0;
  1069. }
  1070. #ifdef USING_PRINTF1
  1071. //printf("update step:%d\n",updateStep);
  1072. printf("query:");
  1073. for (j = 0; j < updateMsgSendLen; j++)
  1074. {
  1075. printf("%x ", pUpdateMsgSend[j]);
  1076. }
  1077. printf("\nanswer:");
  1078. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  1079. {
  1080. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  1081. }
  1082. printf("\n");
  1083. printf("next update step:%d\n", updateStep);
  1084. #endif
  1085. osDelay(50);
  1086. break;
  1087. case UPDATE_STEP_SEND_UPDATE_DATA:
  1088. dataLen = 64;
  1089. updateMsgSendLen = 75;
  1090. for (currentPackage = 0; currentPackage < updateDataTotalByteLen / 64; currentPackage++)
  1091. {
  1092. currentPackageStartAddr = currentPackage * 64;
  1093. pUpdateMsgSend[0] = 0xEB; //start flag
  1094. pUpdateMsgSend[1] = 0x01; //add flag
  1095. pUpdateMsgSend[2] = 0x00; //write
  1096. pUpdateMsgSend[3] = 0x47; //data len
  1097. pUpdateMsgSend[4] = 0x82; //cmd
  1098. pUpdateMsgSend[5] = (currentPackageStartAddr >> 24) & 0xFF;
  1099. pUpdateMsgSend[6] = (currentPackageStartAddr >> 16) & 0xFF;
  1100. pUpdateMsgSend[7] = (currentPackageStartAddr >> 8) & 0xFF;
  1101. pUpdateMsgSend[8] = currentPackageStartAddr & 0xFF;
  1102. BSP_QSPI_Read_Safe(&pUpdateMsgSend[9], FLASH_BMS_FOTA_START_ADDR + 4 + currentPackage * dataLen, dataLen); //data
  1103. pUpdateMsgSend[8 + dataLen + 1] = SP_BMS_Update_CheckSUM(&pUpdateMsgSend[3], dataLen + 6); //check sum
  1104. pUpdateMsgSend[8 + dataLen + 2] = 0xF5; //end flag
  1105. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1106. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  1107. if (ret != 0)
  1108. {
  1109. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  1110. {
  1111. if (pUpdateMsgRecv.cmd == 0x81)
  1112. {
  1113. if (pUpdateMsgRecv.data == 0x11)
  1114. {
  1115. if (currentPackage + 1 == updateDataTotalByteLen / 64)
  1116. {
  1117. updateStep = UPDATE_STEP_SEND_DATA_END;
  1118. }
  1119. errorCount = 0;
  1120. }
  1121. else
  1122. {
  1123. errorCount++;
  1124. }
  1125. }
  1126. else
  1127. {
  1128. errorCount++;
  1129. }
  1130. }
  1131. else
  1132. {
  1133. errorCount++;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. errorCount++;
  1139. }
  1140. if (errorCount > 10)
  1141. {
  1142. updateStep = UPDATE_STEP_RESET;
  1143. errorCount = 0;
  1144. break;
  1145. }
  1146. #ifdef USING_PRINTF1
  1147. //printf("update step:%d\n",updateStep);
  1148. printf("query:");
  1149. for (j = 0; j < updateMsgSendLen; j++)
  1150. {
  1151. printf("%x ", pUpdateMsgSend[j]);
  1152. }
  1153. printf("\nanswer:");
  1154. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  1155. {
  1156. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  1157. }
  1158. printf("\n");
  1159. printf("next update step:%d\n", updateStep);
  1160. #endif
  1161. }
  1162. osDelay(50);
  1163. break;
  1164. case UPDATE_STEP_SEND_DATA_END:
  1165. dataLen = 1;
  1166. updateMsgSendLen = 8;
  1167. pUpdateMsgSend[0] = 0xEB; //start flag
  1168. pUpdateMsgSend[1] = 0x01; //add flag
  1169. pUpdateMsgSend[2] = 0x00; //write
  1170. pUpdateMsgSend[3] = 0x04; //data len
  1171. pUpdateMsgSend[4] = 0x81; //cmd
  1172. pUpdateMsgSend[5] = 0x66; //data
  1173. pUpdateMsgSend[6] = 0xEB; //check
  1174. pUpdateMsgSend[7] = 0xF5; //end flag
  1175. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1176. ret = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), sizeof(BMS_Update_Recv_Msg_Type), 500);
  1177. if (ret != 0)
  1178. {
  1179. if (pUpdateMsgRecv.startFlag == 0xEB && pUpdateMsgRecv.endFlag == 0xF5)
  1180. {
  1181. if (pUpdateMsgRecv.cmd == 0x81)
  1182. {
  1183. if (pUpdateMsgRecv.data == 0x11)
  1184. {
  1185. updateStep = UPDATE_STEP_START_INSTALL;
  1186. errorCount = 0;
  1187. }
  1188. else
  1189. {
  1190. errorCount++;
  1191. }
  1192. }
  1193. else
  1194. {
  1195. errorCount++;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. errorCount++;
  1201. }
  1202. }
  1203. else
  1204. {
  1205. errorCount++;
  1206. }
  1207. if (errorCount > 10)
  1208. {
  1209. updateStep = UPDATE_STEP_RESET;
  1210. errorCount = 0;
  1211. }
  1212. #ifdef USING_PRINTF1
  1213. //printf("update step:%d\n",updateStep);
  1214. printf("query:");
  1215. for (j = 0; j < updateMsgSendLen; j++)
  1216. {
  1217. printf("%x ", pUpdateMsgSend[j]);
  1218. }
  1219. printf("\nanswer:");
  1220. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  1221. {
  1222. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  1223. }
  1224. printf("\n");
  1225. printf("next update step:%d\n", updateStep);
  1226. #endif
  1227. osDelay(50);
  1228. break;
  1229. case UPDATE_STEP_START_INSTALL:
  1230. dataLen = 1;
  1231. updateMsgSendLen = 8;
  1232. pUpdateMsgSend[0] = 0xEB; //start flag
  1233. pUpdateMsgSend[1] = 0x01; //add flag
  1234. pUpdateMsgSend[2] = 0x00; //write
  1235. pUpdateMsgSend[3] = 0x04; //data len
  1236. pUpdateMsgSend[4] = 0x81; //cmd
  1237. pUpdateMsgSend[5] = 0x99; //data
  1238. pUpdateMsgSend[6] = 0x1E; //check
  1239. pUpdateMsgSend[7] = 0xF5; //end flag
  1240. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1241. UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
  1242. updateStep = UPDATE_STEP_END;
  1243. #ifdef USING_PRINTF1
  1244. //printf("update step:%d\n",updateStep);
  1245. printf("query:");
  1246. for (j = 0; j < updateMsgSendLen; j++)
  1247. {
  1248. printf("%x ", pUpdateMsgSend[j]);
  1249. }
  1250. printf("\nanswer:");
  1251. for (j = 0; j < sizeof(BMS_Update_Recv_Msg_Type); j++)
  1252. {
  1253. printf("%x ", *(((UINT8 *)&pUpdateMsgRecv) + j));
  1254. }
  1255. printf("\n");
  1256. printf("next update step:%d\n", updateStep);
  1257. #endif
  1258. osDelay(50);
  1259. break;
  1260. case UPDATE_STEP_END:
  1261. updateStep = UPDATE_STEP_CHECK_VERSION;
  1262. printf("update end\n");
  1263. bmsUpdateFlag = 0;
  1264. break;
  1265. case UPDATE_STEP_RESET:
  1266. dataLen = 1;
  1267. updateMsgSendLen = 8;
  1268. pUpdateMsgSend[0] = 0xEB; //start flag
  1269. pUpdateMsgSend[1] = 0x01; //add flag
  1270. pUpdateMsgSend[2] = 0x00; //write
  1271. pUpdateMsgSend[3] = 0x04; //data len
  1272. pUpdateMsgSend[4] = 0x81; //cmd
  1273. pUpdateMsgSend[5] = 0xAA; //data
  1274. pUpdateMsgSend[6] = 0x2F; //check
  1275. pUpdateMsgSend[7] = 0xF5; //end flag
  1276. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1277. UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
  1278. osDelay(50);
  1279. resetCount++;
  1280. if (resetCount >= 2)
  1281. {
  1282. updateStep = UPDATE_STEP_DOWNLOAD_BREAK_OFF;
  1283. resetCount = 0;
  1284. }
  1285. else
  1286. {
  1287. updateStep = UPDATE_STEP_PREPARE_SEND_DATA_LEN;
  1288. }
  1289. #ifdef USING_PRINTF
  1290. printf("update error!!\n rest and start send data lenth again!!\n continue update!\n");
  1291. #endif
  1292. break;
  1293. case UPDATE_STEP_DOWNLOAD_BREAK_OFF:
  1294. dataLen = 1;
  1295. updateMsgSendLen = 8;
  1296. pUpdateMsgSend[0] = 0xEB; //start flag
  1297. pUpdateMsgSend[1] = 0x01; //add flag
  1298. pUpdateMsgSend[2] = 0x00; //write
  1299. pUpdateMsgSend[3] = 0x04; //data len
  1300. pUpdateMsgSend[4] = 0x81; //cmd
  1301. pUpdateMsgSend[5] = 0xBB; //data
  1302. pUpdateMsgSend[6] = 0x40; //check
  1303. pUpdateMsgSend[7] = 0xF5; //end flag
  1304. memset((UINT8 *)(&pUpdateMsgRecv), 0, sizeof(BMS_Update_Recv_Msg_Type));
  1305. UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(&pUpdateMsgRecv), 0, 500);
  1306. osDelay(50);
  1307. updateStep = UPDATE_STEP_CHECK_VERSION;
  1308. Cycle_conut++;
  1309. break;
  1310. case UPDATE_STEP_ERROR:
  1311. updateStep = UPDATE_STEP_CHECK_VERSION;
  1312. printf("update error end\n");
  1313. bmsUpdateFlag = 0;
  1314. break;
  1315. default:
  1316. updateStep = UPDATE_STEP_CHECK_VERSION;
  1317. printf("update default end\n");
  1318. bmsUpdateFlag = 0;
  1319. break;
  1320. }
  1321. }
  1322. }
  1323. UINT8 SP_BMS_Update_CheckSUM(UINT8 *pSendData, UINT8 len)
  1324. {
  1325. UINT8 ret = 0;
  1326. UINT8 i = 0;
  1327. for (i = 0; i < len; i++)
  1328. {
  1329. ret += *(pSendData + i);
  1330. }
  1331. return ret & 0xFF;
  1332. }
  1333. //________________________________________________________________________________
  1334. updateBMSStatus MS_BMS_Update_Service() //美顺BMS升级服务
  1335. {
  1336. #ifdef USING_PRINTF
  1337. UINT8 ii = 0;
  1338. #endif
  1339. UINT8 errorCount = 0;
  1340. UINT16 currentPackage = 0;
  1341. UINT32 updateDataTotalByteLen = 0;
  1342. UINT16 updateDataPackageCount = 0;
  1343. UINT8 ReadNVMTemp[64];
  1344. UpdateStep_MS_BMS updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER;
  1345. UINT16 i, j = 0;
  1346. UINT8 dataLen = 0;
  1347. UINT8 ret0 = 0;
  1348. updateBMSStatus ret = updateFailed;
  1349. UINT8 pUpdateMsgSend[80];
  1350. UINT32 updateMsgSendLen = 0;
  1351. UINT32 updateMsgReadLen = 0;
  1352. BOOL bmsUpdateFlag = TRUE;
  1353. UINT8 bmsAnswerMsg[8];
  1354. //static UpdateStep step = UPDATE_STEP_CHECK_VERSION;
  1355. UINT8 Cycle_conut = 0;
  1356. UINT16 CRCtemp = 0;
  1357. UINT8 headerLen = 5;
  1358. UINT8 checkSum = 0x00;
  1359. UINT8 checkSumCal = 0x00;
  1360. UINT8 tempLen = 0x00;
  1361. BSP_QSPI_Read_Safe(&checkSum, FLASH_BMS_FOTA_START_ADDR, 1);
  1362. memset(ReadNVMTemp, 0, 64);
  1363. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1, 4); //data
  1364. updateDataTotalByteLen = ((ReadNVMTemp[0] << 24) & 0xFF000000) | ((ReadNVMTemp[1] << 16) & 0xFF0000) | ((ReadNVMTemp[2] << 8) & 0xFF00) | (ReadNVMTemp[3] & 0xFF);
  1365. updateDataPackageCount = (updateDataTotalByteLen + (64 - 1)) / 64; //进一法 e = (a+(b-1))/b
  1366. for (i = 0; i < ((updateDataTotalByteLen + headerLen - 1) + (64 - 1)) / 64; i++) //
  1367. {
  1368. memset(ReadNVMTemp, 0, 64);
  1369. if ((i + 1) * 64 <= (updateDataTotalByteLen + headerLen - 1))
  1370. {
  1371. tempLen = 64;
  1372. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1 + i * 64, 64);
  1373. }
  1374. else
  1375. {
  1376. tempLen = (updateDataTotalByteLen + headerLen - 1) - i * 64;
  1377. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + 1 + i * 64, tempLen);
  1378. }
  1379. for (j = 0; j < tempLen; j++)
  1380. {
  1381. checkSumCal = (checkSumCal + ReadNVMTemp[j]) & 0xFF;
  1382. }
  1383. osDelay(10);
  1384. }
  1385. if (checkSum != checkSumCal)
  1386. {
  1387. #ifdef USING_PRINTF
  1388. printf("checksum error: checksum = %x, checksumCal = %x\n", checkSum, checkSumCal);
  1389. #endif
  1390. ret = updateErrorCheckSumError;
  1391. return ret;
  1392. }
  1393. else
  1394. {
  1395. #ifdef USING_PRINTF
  1396. printf("checksum OK: checksum = %x, checksumCal = %x\n", checkSum, checkSumCal);
  1397. #endif
  1398. }
  1399. #ifdef USING_PRINTF
  1400. printf(" bmsUpdateFlag = %x, Cycle_conut = %x\n", bmsUpdateFlag, Cycle_conut);
  1401. #endif
  1402. while (bmsUpdateFlag && Cycle_conut < 2)
  1403. {
  1404. #ifdef USING_PRINTF
  1405. printf("update ms bms step %d\n:", updateStep);
  1406. #endif
  1407. switch (updateStep)
  1408. {
  1409. case MS_UPDATE_STEP_SEND_FIRMWARE_UPDATE_REQUEST_AND_JUMP_TO_BOOTLOADER: //0x01
  1410. dataLen = 0x00;
  1411. updateMsgSendLen = 6 + dataLen;
  1412. updateMsgReadLen = 8;
  1413. pUpdateMsgSend[0] = 0x01; //node byte
  1414. pUpdateMsgSend[1] = 0x40; //func byte
  1415. pUpdateMsgSend[2] = updateStep; //cmd byte
  1416. pUpdateMsgSend[3] = dataLen; //data len
  1417. //no data type
  1418. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
  1419. pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
  1420. pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
  1421. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1422. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1423. #ifdef USING_PRINTF
  1424. printf("update step 1 answer,updateMsgReadLen = %x:\n", updateMsgReadLen);
  1425. for (ii = 0; ii < updateMsgReadLen; ii++)
  1426. printf("%x ", bmsAnswerMsg[ii]);
  1427. printf("\nret0 = %d", ret0);
  1428. printf("\n");
  1429. #endif
  1430. if (ret0 != 0)
  1431. {
  1432. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1433. {
  1434. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_UPDATE_REQUEST_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x02, answer data len:0x02
  1435. {
  1436. if (bmsAnswerMsg[4] == 0x00) //answer data byte1
  1437. {
  1438. if (bmsAnswerMsg[5] == 0x00) //answer data byte2
  1439. {
  1440. updateStep = MS_UPDATE_STEP_SEND_FIRMWARE_INFO;
  1441. errorCount = 0;
  1442. }
  1443. }
  1444. else if (bmsAnswerMsg[4] == 0x01) //不允许升级
  1445. {
  1446. if (bmsAnswerMsg[5] == 0x01) //电量过低
  1447. {
  1448. updateStep = MS_UPDATE_STEP_ERROR;
  1449. ret = updateErrorBMSPowerLow;
  1450. }
  1451. else if (bmsAnswerMsg[5] == 0x02) //电池存在保护状态不允许升级
  1452. {
  1453. updateStep = MS_UPDATE_STEP_ERROR;
  1454. ret = updateErrorBMSWarningProtect;
  1455. }
  1456. else if (bmsAnswerMsg[5] == 0x03) //不支持升级
  1457. {
  1458. updateStep = MS_UPDATE_STEP_ERROR;
  1459. ret = updateErrorBMSNotSurport;
  1460. }
  1461. else if (bmsAnswerMsg[5] == 0x04) //当前电池处于充放电状态
  1462. {
  1463. updateStep = MS_UPDATE_STEP_ERROR;
  1464. ret = updateErrorBMSWorkState;
  1465. }
  1466. else
  1467. {
  1468. errorCount++;
  1469. }
  1470. }
  1471. }
  1472. else
  1473. {
  1474. errorCount++;
  1475. }
  1476. }
  1477. else
  1478. {
  1479. errorCount++;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. errorCount++;
  1485. }
  1486. if (errorCount > 10)
  1487. {
  1488. updateStep = MS_UPDATE_STEP_ERROR;
  1489. errorCount = 0;
  1490. }
  1491. osDelay(50);
  1492. printf(" step 1 ret = %d\n", ret);
  1493. break;
  1494. case MS_UPDATE_STEP_SEND_FIRMWARE_INFO: //0x03
  1495. dataLen = 52;
  1496. updateMsgSendLen = 6 + dataLen;
  1497. updateMsgReadLen = 7;
  1498. pUpdateMsgSend[0] = 0x01; //node byte
  1499. pUpdateMsgSend[1] = 0x40; //func byte
  1500. pUpdateMsgSend[2] = updateStep; //cmd byte
  1501. pUpdateMsgSend[3] = dataLen; //data len
  1502. memset(ReadNVMTemp, 0, 64);
  1503. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen, 16); //data
  1504. MEMCPY(&pUpdateMsgSend[4], ReadNVMTemp, 16); //厂家信息,未开启校验
  1505. MEMCPY(&pUpdateMsgSend[4 + 16], ReadNVMTemp, 16); //保护板硬件序列号,未开启校验
  1506. pUpdateMsgSend[4 + 16 * 2 + 0] = (updateDataTotalByteLen >> 24) & 0xFF; //固件包大小
  1507. pUpdateMsgSend[4 + 16 * 2 + 1] = (updateDataTotalByteLen >> 16) & 0xFF;
  1508. pUpdateMsgSend[4 + 16 * 2 + 2] = (updateDataTotalByteLen >> 8) & 0xFF;
  1509. pUpdateMsgSend[4 + 16 * 2 + 3] = (updateDataTotalByteLen)&0xFF;
  1510. MEMCPY(&pUpdateMsgSend[4 + 16 * 2 + 4], ReadNVMTemp, 16); // 固件包头信息,未开启校验
  1511. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
  1512. pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
  1513. pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
  1514. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1515. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1516. #ifdef USING_PRINTF
  1517. printf("update step 3 answer:\n");
  1518. for (ii = 0; ii < updateMsgReadLen; ii++)
  1519. printf("%x ", bmsAnswerMsg[ii]);
  1520. printf("\nret0 = %d", ret0);
  1521. printf("\n");
  1522. #endif
  1523. if (ret0 != 0)
  1524. {
  1525. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1526. {
  1527. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_FIRMWARE_INFO_CHECK_AND_UPDATE_REQEST_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
  1528. {
  1529. if (bmsAnswerMsg[4] == 0x00) //answer data byte1
  1530. {
  1531. updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
  1532. errorCount = 0;
  1533. }
  1534. else if (bmsAnswerMsg[4] == 0x01) //厂家信息错误
  1535. {
  1536. errorCount++;
  1537. ret = updateErrorFirmwareInfoError;
  1538. }
  1539. else if (bmsAnswerMsg[4] == 0x02) //硬件序列号不匹配
  1540. {
  1541. errorCount++;
  1542. ret = updateErrorFirmwareInfoError;
  1543. }
  1544. else if (bmsAnswerMsg[4] == 0x03) //固件大小超出范围
  1545. {
  1546. errorCount++;
  1547. ret = updateErrorFirmwareSizeError;
  1548. }
  1549. else if (bmsAnswerMsg[4] == 0x04) //固件包头信息错误
  1550. {
  1551. errorCount++;
  1552. ret = updateErrorFirmwareInfoError;
  1553. }
  1554. else
  1555. {
  1556. errorCount++;
  1557. }
  1558. }
  1559. else
  1560. {
  1561. errorCount++;
  1562. }
  1563. }
  1564. else
  1565. {
  1566. errorCount++;
  1567. }
  1568. }
  1569. else
  1570. {
  1571. errorCount++;
  1572. }
  1573. if (errorCount > 10)
  1574. {
  1575. updateStep = MS_UPDATE_STEP_ERROR;
  1576. errorCount = 0;
  1577. }
  1578. printf(" step 3 ret = %d\n", ret);
  1579. osDelay(50);
  1580. break;
  1581. case MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST: //0x05
  1582. dataLen = 0;
  1583. updateMsgSendLen = 6 + dataLen;
  1584. updateMsgReadLen = 8;
  1585. pUpdateMsgSend[0] = 0x01; //node byte
  1586. pUpdateMsgSend[1] = 0x40; //func byte
  1587. pUpdateMsgSend[2] = updateStep; //cmd byte
  1588. pUpdateMsgSend[3] = dataLen; //data len
  1589. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
  1590. pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
  1591. pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
  1592. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1593. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1594. #ifdef USING_PRINTF
  1595. printf("update step 5 answer:\n");
  1596. for (ii = 0; ii < updateMsgReadLen; ii++)
  1597. printf("%x ", bmsAnswerMsg[ii]);
  1598. printf("\nret0 = %d", ret0);
  1599. printf("\n");
  1600. #endif
  1601. if (ret0 != 0)
  1602. {
  1603. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1604. {
  1605. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_EREASE_FLASH_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x06, answer data len:0x02
  1606. {
  1607. if (bmsAnswerMsg[4] == 0x00) //answer data byte1, erease successed
  1608. {
  1609. updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA; //0x07
  1610. errorCount = 0;
  1611. }
  1612. else if (bmsAnswerMsg[4] == 0x01) //擦除失败
  1613. {
  1614. errorCount++;
  1615. ret = updateErrorAppErease;
  1616. }
  1617. else
  1618. {
  1619. errorCount++;
  1620. }
  1621. }
  1622. else
  1623. {
  1624. errorCount++;
  1625. }
  1626. }
  1627. else
  1628. {
  1629. errorCount++;
  1630. }
  1631. }
  1632. else
  1633. {
  1634. errorCount++;
  1635. }
  1636. if (errorCount > 10)
  1637. {
  1638. updateStep = MS_UPDATE_STEP_ERROR;
  1639. errorCount = 0;
  1640. }
  1641. osDelay(50);
  1642. break;
  1643. case MS_UPDATE_STEP_SEND_UPDATE_DATA: //0x07
  1644. updateMsgReadLen = 7;
  1645. pUpdateMsgSend[0] = 0x01; //node byte
  1646. pUpdateMsgSend[1] = 0x40; //func byte
  1647. pUpdateMsgSend[2] = updateStep; //cmd byte
  1648. for (i = 0; i < updateDataPackageCount; i++)
  1649. {
  1650. memset(ReadNVMTemp, 0, 64);
  1651. if ((i + 1) * 64 <= (updateDataTotalByteLen))
  1652. {
  1653. tempLen = 64;
  1654. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen + i * 64, 64);
  1655. }
  1656. else
  1657. {
  1658. tempLen = (updateDataTotalByteLen)-i * 64; //
  1659. BSP_QSPI_Read_Safe(ReadNVMTemp, FLASH_BMS_FOTA_START_ADDR + headerLen + i * 64, tempLen);
  1660. }
  1661. CRCtemp = MS_BMS_Update_CRC16(ReadNVMTemp, tempLen);
  1662. dataLen = tempLen + 6; //data len =count(2+2 byte) + crc(2byte) + update data len
  1663. updateMsgSendLen = 6 + dataLen; // updateMsgSendLen = data len + header len(6byte)
  1664. pUpdateMsgSend[3] = dataLen; //data len
  1665. pUpdateMsgSend[4] = ((i + 1) >> 8) & 0xFF; //当前包序号,大端模式
  1666. pUpdateMsgSend[5] = (i + 1) & 0xFF;
  1667. pUpdateMsgSend[6] = (updateDataPackageCount >> 8) & 0xFF;
  1668. pUpdateMsgSend[7] = updateDataPackageCount & 0xFF;
  1669. pUpdateMsgSend[8] = (CRCtemp >> 8) & 0xFF; // data CRC High
  1670. pUpdateMsgSend[9] = CRCtemp & 0xFF; //data CRC Low
  1671. MEMCPY(&pUpdateMsgSend[4 + 6], ReadNVMTemp, 64); //升级数据,64字节
  1672. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4 + dataLen);
  1673. pUpdateMsgSend[4 + dataLen] = (CRCtemp >> 8) & 0xFF; // CRC High
  1674. pUpdateMsgSend[5 + dataLen] = CRCtemp & 0xFF; //CRC Low
  1675. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1676. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1677. #ifdef USING_PRINTF
  1678. printf("update step 7 answer:\n");
  1679. for (ii = 0; ii < updateMsgReadLen; ii++)
  1680. printf("%x ", bmsAnswerMsg[ii]);
  1681. printf("\nret0 = %d", ret0);
  1682. printf("\n");
  1683. #endif
  1684. if (ret0 != 0)
  1685. {
  1686. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1687. {
  1688. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_UPDATE_DATA_WRITE_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x04, answer data len:0x01
  1689. {
  1690. if (bmsAnswerMsg[4] == 0x00) //answer data byte1,接收并操作成功
  1691. {
  1692. updateStep = MS_UPDATE_STEP_EREASE_APP_FLASH_REQUEST;
  1693. errorCount = 0;
  1694. }
  1695. else if (bmsAnswerMsg[4] == 0x01) //固件块校验失败
  1696. {
  1697. errorCount = 10;
  1698. ret = updateErrorPackageCRC;
  1699. }
  1700. else if (bmsAnswerMsg[4] == 0x02) //烧写失败
  1701. {
  1702. errorCount = 10;
  1703. ret = updateErrorPackageWrite;
  1704. }
  1705. else if (bmsAnswerMsg[4] == 0x03) //固件块编号异常
  1706. {
  1707. errorCount = 10;
  1708. ret = updateErrorPackageNo;
  1709. }
  1710. else
  1711. {
  1712. errorCount = 10;
  1713. }
  1714. }
  1715. else
  1716. {
  1717. errorCount = 10;
  1718. }
  1719. }
  1720. else
  1721. {
  1722. errorCount = 10;
  1723. }
  1724. }
  1725. else
  1726. {
  1727. errorCount = 10;
  1728. }
  1729. if (errorCount >= 10)
  1730. {
  1731. updateStep = MS_UPDATE_STEP_ERROR;
  1732. errorCount = 0;
  1733. i--;
  1734. break;
  1735. }
  1736. osDelay(50);
  1737. }
  1738. if (i == updateDataPackageCount)
  1739. {
  1740. updateStep = MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP;
  1741. }
  1742. break;
  1743. case MS_UPDATE_STEP_SEND_UPDATE_DATA_END_AND_JUMP_TO_APP: //0x09
  1744. dataLen = 0x00;
  1745. updateMsgSendLen = 6 + dataLen;
  1746. updateMsgReadLen = 7;
  1747. pUpdateMsgSend[0] = 0x01; //node byte
  1748. pUpdateMsgSend[1] = 0x40; //func byte
  1749. pUpdateMsgSend[2] = updateStep; //cmd byte
  1750. pUpdateMsgSend[3] = dataLen; //data len
  1751. //no data type
  1752. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
  1753. pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
  1754. pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
  1755. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1756. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1757. #ifdef USING_PRINTF
  1758. printf("update step 9 answer:\n");
  1759. for (ii = 0; ii < updateMsgReadLen; ii++)
  1760. printf("%x ", bmsAnswerMsg[ii]);
  1761. printf("\nret0 = %d", ret0);
  1762. printf("\n");
  1763. #endif
  1764. if (ret0 != 0)
  1765. {
  1766. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1767. {
  1768. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_JUMP_TO_APP_ANSWER && bmsAnswerMsg[3] == 0x01) //answer cmd byte:0x0A, answer data len:0x01
  1769. {
  1770. if (bmsAnswerMsg[4] == 0x00) //answer data byte1, update succeed
  1771. {
  1772. errorCount = 0;
  1773. updateStep = MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE; //0x0B
  1774. }
  1775. else if (bmsAnswerMsg[4] == 0x01) //升级失败
  1776. {
  1777. errorCount = 10;
  1778. ret = updateFailed;
  1779. }
  1780. }
  1781. else
  1782. {
  1783. errorCount++;
  1784. }
  1785. }
  1786. else
  1787. {
  1788. errorCount++;
  1789. }
  1790. }
  1791. else
  1792. {
  1793. errorCount++;
  1794. }
  1795. if (errorCount >= 10)
  1796. {
  1797. updateStep = MS_UPDATE_STEP_ERROR;
  1798. errorCount = 0;
  1799. }
  1800. osDelay(50);
  1801. break;
  1802. case MS_UPDATE_STEP_READ_CURRENT_RUNNING_MODE: //0x0B
  1803. dataLen = 0x00;
  1804. updateMsgSendLen = 6 + dataLen;
  1805. updateMsgReadLen = 8;
  1806. pUpdateMsgSend[0] = 0x01; //node byte
  1807. pUpdateMsgSend[1] = 0x40; //func byte
  1808. pUpdateMsgSend[2] = updateStep; //cmd byte
  1809. pUpdateMsgSend[3] = dataLen; //data len
  1810. //no data type
  1811. CRCtemp = MS_BMS_Update_CRC16(pUpdateMsgSend, 4);
  1812. pUpdateMsgSend[4] = (CRCtemp >> 8) & 0xFF; // CRC High
  1813. pUpdateMsgSend[5] = CRCtemp & 0xFF; //CRC Low
  1814. memset((UINT8 *)(bmsAnswerMsg), 0, 8);
  1815. ret0 = UartAppTrasmit(pUpdateMsgSend, updateMsgSendLen, (UINT8 *)(bmsAnswerMsg), updateMsgReadLen, 1000);
  1816. #ifdef USING_PRINTF
  1817. printf("update step A answer:\n");
  1818. for (ii = 0; ii < updateMsgReadLen; ii++)
  1819. printf("%x ", bmsAnswerMsg[ii]);
  1820. printf("\nret0 = %d", ret0);
  1821. printf("\n");
  1822. #endif
  1823. if (ret0 != 0)
  1824. {
  1825. if (bmsAnswerMsg[0] == 0x01 && bmsAnswerMsg[1] == 0x40) // node and func byte
  1826. {
  1827. if (bmsAnswerMsg[2] == MS_UPDATE_STEP_CURRENT_RUNNING_MODE_ANSWER && bmsAnswerMsg[3] == 0x02) //answer cmd byte:0x0C, answer data len:0x02
  1828. {
  1829. if (bmsAnswerMsg[4] == 0x01) //answer data byte1, update succeed, app is running
  1830. {
  1831. errorCount = 0;
  1832. updateStep = MS_UPDATE_STEP_END;
  1833. }
  1834. else if (bmsAnswerMsg[4] == 0x00) //update failed , boot is running,error
  1835. {
  1836. errorCount = 10;
  1837. }
  1838. }
  1839. else
  1840. {
  1841. errorCount++;
  1842. }
  1843. }
  1844. else
  1845. {
  1846. errorCount++;
  1847. }
  1848. }
  1849. else
  1850. {
  1851. errorCount++;
  1852. }
  1853. if (errorCount >= 3)
  1854. {
  1855. updateStep = MS_UPDATE_STEP_ERROR;
  1856. errorCount = 0;
  1857. }
  1858. osDelay(50);
  1859. break;
  1860. case MS_UPDATE_STEP_END: //0x0D
  1861. errorCount = 0;
  1862. bmsUpdateFlag = FALSE;
  1863. ret = updateOK;
  1864. break;
  1865. case MS_UPDATE_STEP_ERROR: //0x0E
  1866. errorCount = 0;
  1867. bmsUpdateFlag = true;
  1868. Cycle_conut++;
  1869. if (Cycle_conut > 2)
  1870. {
  1871. ret = updateErrorTimeout;
  1872. bmsUpdateFlag = FALSE;
  1873. }
  1874. break;
  1875. default:
  1876. bmsUpdateFlag = FALSE;
  1877. break;
  1878. }
  1879. }
  1880. #ifdef USING_PRINTF
  1881. printf("last ret = %x\n", ret);
  1882. #endif
  1883. return ret;
  1884. }
  1885. static void __invert_uint8(UINT8 *dBuf, UINT8 *srcBuf)
  1886. {
  1887. int i;
  1888. UINT8 tmp[4];
  1889. tmp[0] = 0;
  1890. for (i = 0; i < 8; i++)
  1891. {
  1892. if (srcBuf[0] & (1 << i))
  1893. {
  1894. tmp[0] |= 1 << (7 - i);
  1895. }
  1896. }
  1897. dBuf[0] = tmp[0];
  1898. }
  1899. static void __invert_uint16(UINT16 *dBuf, UINT16 *srcBuf)
  1900. {
  1901. int i;
  1902. UINT16 tmp[4];
  1903. tmp[0] = 0;
  1904. for (i = 0; i < 16; i++)
  1905. {
  1906. if (srcBuf[0] & (1 << i))
  1907. {
  1908. tmp[0] |= 1 << (15 - i);
  1909. }
  1910. }
  1911. dBuf[0] = tmp[0];
  1912. }
  1913. UINT16 MS_BMS_Update_CRC16(UINT8 *pSendData, UINT16 len)
  1914. {
  1915. UINT16 wCRCin = 0xFFFF;
  1916. UINT16 wCPoly = 0x8005;
  1917. UINT8 wChar = 0;
  1918. UINT16 crc_rslt = 0;
  1919. int i;
  1920. while (len--)
  1921. {
  1922. wChar = *(pSendData++);
  1923. __invert_uint8(&wChar, &wChar);
  1924. wCRCin ^= (wChar << 8);
  1925. for (i = 0; i < 8; i++)
  1926. {
  1927. if (wCRCin & 0x8000)
  1928. {
  1929. wCRCin = (wCRCin << 1) ^ wCPoly;
  1930. }
  1931. else
  1932. {
  1933. wCRCin = wCRCin << 1;
  1934. }
  1935. }
  1936. }
  1937. __invert_uint16(&wCRCin, &wCRCin);
  1938. crc_rslt = ((wCRCin << 8) & 0xFF00) | ((wCRCin >> 8) & 0x00FF);
  1939. return (crc_rslt);
  1940. }