Fota.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Fota_Type Fota_S;
  18. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length);
  19. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length);
  20. volatile bool NB_Fota_update_flag = FALSE; //NB可以升级标志
  21. volatile bool BMS_Fota_update_flag = FALSE; //NB可以升级标志
  22. void Fota_Func(UINT8 *DataPtr,INT32 connectId)
  23. {
  24. UINT8 Fota_Answer[43];
  25. UINT8 Fota_Cmd;
  26. INT8 ret;
  27. UINT8* Data_Read_Buffer;
  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,Fota_S.Fota_All_Data_Len + 4 - (Fota_S.Fota_All_Data_Len%4)); //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. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
  69. {
  70. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  71. {
  72. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  73. }
  74. 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);
  75. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  76. BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  77. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
  78. #ifdef USING_PRINTF
  79. printf("\n\n\n");
  80. UINT8 temp[1];
  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\n\n");
  86. #endif
  87. free(Data_Read_Buffer);
  88. if(Data_Read_Crc==Fota_S.Fota_CRC )
  89. {
  90. Fota_Answer[3] = 0x01;
  91. }
  92. else
  93. {
  94. Fota_Answer[3] = 0x02;
  95. }
  96. }
  97. else//数据校验失败
  98. {
  99. Fota_Answer[3] = 0x02;
  100. }
  101. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  102. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  103. Fota_Answer[22] = 0x00;
  104. Fota_Answer[23] = 0x12;
  105. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  106. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  107. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  108. break;
  109. }
  110. case 0x03:
  111. {
  112. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  113. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  114. Fota_Answer[3] = 0x01;
  115. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  116. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  117. Fota_Answer[22] = 0x00;
  118. Fota_Answer[23] = 0x12;
  119. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  120. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  121. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  122. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  123. {
  124. NB_Fota_update_flag = TRUE;
  125. }
  126. else
  127. {
  128. NB_Fota_update_flag = FALSE;
  129. }
  130. break;
  131. }
  132. default:
  133. break;
  134. }
  135. }
  136. else if(*(DataPtr+30)==0x88)//BMS升级文件存放
  137. {
  138. Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
  139. Fota_Cmd = *(DataPtr+31);
  140. Fota_Answer[0] = TCP_START_SYM1;
  141. Fota_Answer[1] = TCP_START_SYM2;
  142. Fota_Answer[2] = TCP_CONCMD_SYM;
  143. switch (Fota_Cmd)
  144. {
  145. case 0x01:
  146. {
  147. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  148. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  149. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR))
  150. {
  151. Fota_Answer[3] = 0x02;
  152. }
  153. else
  154. {
  155. Fota_Answer[3] = 0x01;
  156. }
  157. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  158. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  159. Fota_Answer[22] = 0x00;
  160. Fota_Answer[23] = 0x12;
  161. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  162. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  163. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  164. if(Fota_Answer[3] == 0x01)
  165. {
  166. BSP_QSPI_Erase_Safe(Fota_S.Fota_Flash_Addres,Fota_S.Fota_All_Data_Len + 4 - (Fota_S.Fota_All_Data_Len%4)); //512k-32k = 480k -> 0x75300 0x78000
  167. }
  168. break;
  169. }
  170. case 0x02:
  171. {
  172. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  173. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  174. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  175. memset(Fota_S.Fota_Recv_Data,0x00,100);
  176. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  177. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  178. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
  179. {
  180. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  181. {
  182. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  183. }
  184. 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);
  185. Data_Read_Buffer = malloc(Fota_S.Fota_Recv_Data_Len);
  186. BSP_QSPI_Read_Safe(Data_Read_Buffer,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres,Fota_S.Fota_Recv_Data_Len);
  187. Data_Read_Crc = Fota_crc_chk(Data_Read_Buffer,Fota_S.Fota_Recv_Data_Len);
  188. #ifdef USING_PRINTF1
  189. printf("\n\n\n");
  190. UINT8 temp[1];
  191. for(int i=0;i<Fota_S.Fota_Recv_Data_Len;i++)
  192. {
  193. printf("%x ",*(Data_Read_Buffer+i));
  194. }
  195. printf("\n\n\n");
  196. #endif
  197. free(Data_Read_Buffer);
  198. if(Data_Read_Crc==Fota_S.Fota_CRC )
  199. {
  200. Fota_Answer[3] = 0x01;
  201. }
  202. else
  203. {
  204. Fota_Answer[3] = 0x02;
  205. }
  206. }
  207. else//数据校验失败
  208. {
  209. Fota_Answer[3] = 0x02;
  210. }
  211. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  212. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  213. Fota_Answer[22] = 0x00;
  214. Fota_Answer[23] = 0x12;
  215. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  216. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  217. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  218. break;
  219. }
  220. case 0x03:
  221. {
  222. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  223. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  224. Fota_Answer[3] = 0x01;
  225. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  226. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  227. Fota_Answer[22] = 0x00;
  228. Fota_Answer[23] = 0x12;
  229. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  230. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  231. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  232. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  233. {
  234. BMS_Fota_update_flag = TRUE;
  235. }
  236. else
  237. {
  238. BMS_Fota_update_flag = FALSE;
  239. }
  240. break;
  241. }
  242. default:
  243. break;
  244. }
  245. }
  246. }
  247. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
  248. {
  249. UINT8 bcc_chk_return = 0x00;
  250. UINT8 count = 0;
  251. while (count<length)
  252. {
  253. bcc_chk_return^=data[count];
  254. count++;
  255. }
  256. return bcc_chk_return;
  257. }
  258. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
  259. {
  260. UINT8 reg_crc=0x00;
  261. while(length--)
  262. {
  263. reg_crc ^= *data++;
  264. }
  265. return reg_crc;
  266. }