Fota.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /****************************************************************************
  2. *
  3. * Copy right: Qx.
  4. * File name: Fota.c
  5. * Description: Fota升级函数
  6. * History: 2021-03-15
  7. *
  8. ****************************************************************************/
  9. #include "Fota.h"
  10. #include "ec_tcpip_api.h"
  11. #include "ps_lib_api.h"
  12. #include "MainTask.h"
  13. #include "TcpTask.h"
  14. #include "os_exception.h"
  15. #include "flash_ec616_rt.h"
  16. extern AppNVMDataType AppNVMData;
  17. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length);
  18. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length);
  19. volatile bool NB_Fota_update_flag = FALSE; //NB可以升级标志
  20. volatile bool BMS_Fota_update_flag = FALSE; //NB可以升级标志
  21. void Fota_Func(UINT8 *DataPtr,INT32 connectId)
  22. {
  23. Fota_Type Fota_S;
  24. UINT8 Fota_Answer[43];
  25. UINT8 Fota_Cmd;
  26. INT8 ret;
  27. UINT8* Data_Read_Buffer=PNULL;
  28. UINT8 Data_Read_Crc;
  29. if(*(DataPtr+30)==0x01)
  30. {
  31. Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START;
  32. Fota_Cmd = *(DataPtr+31);
  33. Fota_Answer[0] = TCP_START_SYM1;
  34. Fota_Answer[1] = TCP_START_SYM2;
  35. Fota_Answer[2] = TCP_CONCMD_SYM;
  36. switch (Fota_Cmd)
  37. {
  38. case 0x01:
  39. {
  40. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  41. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  42. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START))
  43. {
  44. Fota_Answer[3] = 0x02;
  45. }
  46. else
  47. {
  48. Fota_Answer[3] = 0x01;
  49. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,(FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START)); //512k-32k = 480k -> 0x75300 0x78000
  50. }
  51. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  52. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  53. Fota_Answer[22] = 0x00;
  54. Fota_Answer[23] = 0x12;
  55. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  56. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  57. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  58. break;
  59. }
  60. case 0x02:
  61. {
  62. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  63. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  64. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  65. memset(Fota_S.Fota_Recv_Data,0x00,100);
  66. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  67. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  68. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len+1);
  69. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42)||Data_Read_Buffer!=PNULL)
  70. {
  71. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  72. {
  73. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  74. }
  75. BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  76. memset(Data_Read_Buffer,0x00,Fota_S.Fota_Recv_Data_Len);
  77. BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  78. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
  79. #ifdef USING_PRINTF1
  80. printf("Data_Read_Buffer:\n");
  81. for(int i=0;i<Fota_S.Fota_Recv_Data_Len;i++)
  82. {
  83. printf("%x ",*(Data_Read_Buffer+i));
  84. }
  85. printf("\n");
  86. #endif
  87. if(Data_Read_Crc==Fota_S.Fota_CRC )
  88. {
  89. Fota_Answer[3] = 0x01;
  90. }
  91. else
  92. {
  93. Fota_Answer[3] = 0x02;
  94. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  95. }
  96. }
  97. else//数据校验失败
  98. {
  99. Fota_Answer[3] = 0x02;
  100. }
  101. if(Data_Read_Buffer!=PNULL)
  102. free(Data_Read_Buffer);
  103. Data_Read_Buffer =PNULL;
  104. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  105. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  106. Fota_Answer[22] = 0x00;
  107. Fota_Answer[23] = 0x12;
  108. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  109. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  110. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  111. break;
  112. }
  113. case 0x03:
  114. {
  115. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  116. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  117. Fota_Answer[3] = 0x01;
  118. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  119. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  120. Fota_Answer[22] = 0x00;
  121. Fota_Answer[23] = 0x12;
  122. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  123. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  124. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  125. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  126. {
  127. NB_Fota_update_flag = TRUE;
  128. }
  129. else
  130. {
  131. NB_Fota_update_flag = FALSE;
  132. }
  133. break;
  134. }
  135. default:
  136. {
  137. Fota_Answer[3] = 0x02;
  138. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  139. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  140. Fota_Answer[22] = 0x00;
  141. Fota_Answer[23] = 0x12;
  142. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  143. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  144. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  145. break;
  146. }
  147. }
  148. }
  149. else if(*(DataPtr+30)==0x88)//BMS升级文件存放
  150. {
  151. Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
  152. Fota_Cmd = *(DataPtr+31);
  153. Fota_Answer[0] = TCP_START_SYM1;
  154. Fota_Answer[1] = TCP_START_SYM2;
  155. Fota_Answer[2] = TCP_CONCMD_SYM;
  156. switch (Fota_Cmd)
  157. {
  158. case 0x01:
  159. {
  160. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  161. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  162. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR))
  163. {
  164. Fota_Answer[3] = 0x02;
  165. }
  166. else
  167. {
  168. Fota_Answer[3] = 0x01;
  169. }
  170. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  171. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  172. Fota_Answer[22] = 0x00;
  173. Fota_Answer[23] = 0x12;
  174. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  175. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  176. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  177. if(Fota_Answer[3] == 0x01)
  178. {
  179. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR)); //512k-32k = 480k -> 0x75300 0x78000
  180. }
  181. break;
  182. }
  183. case 0x02:
  184. {
  185. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  186. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  187. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  188. memset(Fota_S.Fota_Recv_Data,0x00,100);
  189. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  190. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  191. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  192. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42)||Data_Read_Buffer!=PNULL)
  193. {
  194. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  195. {
  196. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  197. }
  198. BSP_QSPI_Write_Safe(Fota_S.Fota_Recv_Data,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  199. BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  200. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
  201. #ifdef USING_PRINTF1
  202. printf("\n\n\n");
  203. UINT8 temp[1];
  204. for(int i=0;i<Fota_S.Fota_Recv_Data_Len;i++)
  205. {
  206. printf("%x ",*(Data_Read_Buffer+i));
  207. }
  208. printf("\n\n\n");
  209. #endif
  210. if(Data_Read_Crc==Fota_S.Fota_CRC )
  211. {
  212. Fota_Answer[3] = 0x01;
  213. }
  214. else
  215. {
  216. Fota_Answer[3] = 0x02;
  217. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  218. }
  219. }
  220. else//数据校验失败
  221. {
  222. Fota_Answer[3] = 0x02;
  223. }
  224. if(Data_Read_Buffer!=PNULL)
  225. free(Data_Read_Buffer);
  226. Data_Read_Buffer =PNULL;
  227. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  228. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  229. Fota_Answer[22] = 0x00;
  230. Fota_Answer[23] = 0x12;
  231. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  232. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  233. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  234. break;
  235. }
  236. case 0x03:
  237. {
  238. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  239. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  240. Fota_Answer[3] = 0x01;
  241. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  242. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  243. Fota_Answer[22] = 0x00;
  244. Fota_Answer[23] = 0x12;
  245. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  246. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  247. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  248. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  249. {
  250. BMS_Fota_update_flag = TRUE;
  251. }
  252. else
  253. {
  254. BMS_Fota_update_flag = FALSE;
  255. }
  256. break;
  257. }
  258. default:
  259. {
  260. Fota_Answer[3] = 0x02;
  261. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  262. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  263. Fota_Answer[22] = 0x00;
  264. Fota_Answer[23] = 0x12;
  265. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  266. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  267. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
  274. {
  275. UINT8 bcc_chk_return = 0x00;
  276. UINT8 count = 0;
  277. while (count<length)
  278. {
  279. bcc_chk_return^=data[count];
  280. count++;
  281. }
  282. return bcc_chk_return;
  283. }
  284. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
  285. {
  286. UINT8 reg_crc=0x00;
  287. while(length--)
  288. {
  289. reg_crc ^= *data++;
  290. }
  291. return reg_crc;
  292. }