AppTaskUart.c 59 KB

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