Fota.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. if(*(DataPtr+30)==0x01)
  28. {
  29. Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START;
  30. Fota_Cmd = *(DataPtr+31);
  31. Fota_Answer[0] = TCP_START_SYM1;
  32. Fota_Answer[1] = TCP_START_SYM2;
  33. Fota_Answer[2] = TCP_CONCMD_SYM;
  34. switch (Fota_Cmd)
  35. {
  36. case 0x01:
  37. {
  38. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  39. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  40. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START))
  41. {
  42. Fota_Answer[3] = 0x02;
  43. }
  44. else
  45. {
  46. Fota_Answer[3] = 0x01;
  47. }
  48. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  49. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  50. Fota_Answer[22] = 0x00;
  51. Fota_Answer[23] = 0x12;
  52. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  53. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  54. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  55. if(Fota_Answer[3] == 0x01)
  56. {
  57. 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
  58. }
  59. break;
  60. }
  61. case 0x02:
  62. {
  63. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  64. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  65. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  66. memset(Fota_S.Fota_Recv_Data,0x00,100);
  67. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  68. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  69. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
  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. ret = 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. if(ret==QSPI_OK)
  77. {
  78. Fota_Answer[3] = 0x01;
  79. }
  80. else
  81. {
  82. Fota_Answer[3] = 0x02;
  83. }
  84. }
  85. else//数据校验失败
  86. {
  87. Fota_Answer[3] = 0x02;
  88. }
  89. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  90. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  91. Fota_Answer[22] = 0x00;
  92. Fota_Answer[23] = 0x12;
  93. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  94. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  95. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  96. break;
  97. }
  98. case 0x03:
  99. {
  100. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  101. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  102. Fota_Answer[3] = 0x01;
  103. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  104. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  105. Fota_Answer[22] = 0x00;
  106. Fota_Answer[23] = 0x12;
  107. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  108. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  109. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  110. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  111. {
  112. NB_Fota_update_flag = TRUE;
  113. #ifdef USING_PRINTF
  114. printf("\n\n\n");
  115. UINT8 temp[1];
  116. for(int i=0;i<Fota_S.Fota_All_Data_Len;i++)
  117. {
  118. BSP_QSPI_Read_Safe(temp,FLASH_FOTA_REGION_START+i,1);
  119. printf("%x ",temp[0]);
  120. }
  121. printf("\nend data \n\n\n");
  122. #endif
  123. }
  124. else
  125. {
  126. NB_Fota_update_flag = FALSE;
  127. }
  128. break;
  129. }
  130. default:
  131. break;
  132. }
  133. }
  134. else if(*(DataPtr+30)==0x88)//BMS升级文件存放
  135. {
  136. Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
  137. Fota_Cmd = *(DataPtr+31);
  138. Fota_Answer[0] = TCP_START_SYM1;
  139. Fota_Answer[1] = TCP_START_SYM2;
  140. Fota_Answer[2] = TCP_CONCMD_SYM;
  141. switch (Fota_Cmd)
  142. {
  143. case 0x01:
  144. {
  145. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  146. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  147. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR))
  148. {
  149. Fota_Answer[3] = 0x02;
  150. }
  151. else
  152. {
  153. Fota_Answer[3] = 0x01;
  154. }
  155. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  156. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  157. Fota_Answer[22] = 0x00;
  158. Fota_Answer[23] = 0x12;
  159. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  160. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  161. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  162. if(Fota_Answer[3] == 0x01)
  163. {
  164. 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
  165. }
  166. break;
  167. }
  168. case 0x02:
  169. {
  170. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  171. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  172. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  173. memset(Fota_S.Fota_Recv_Data,0x00,100);
  174. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  175. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  176. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
  177. {
  178. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  179. {
  180. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  181. }
  182. ret = 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);
  183. if(ret==QSPI_OK)
  184. {
  185. Fota_Answer[3] = 0x01;
  186. }
  187. else
  188. {
  189. Fota_Answer[3] = 0x02;
  190. }
  191. }
  192. else//数据校验失败
  193. {
  194. Fota_Answer[3] = 0x02;
  195. }
  196. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  197. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  198. Fota_Answer[22] = 0x00;
  199. Fota_Answer[23] = 0x12;
  200. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  201. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  202. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  203. break;
  204. }
  205. case 0x03:
  206. {
  207. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  208. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  209. Fota_Answer[3] = 0x01;
  210. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  211. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  212. Fota_Answer[22] = 0x00;
  213. Fota_Answer[23] = 0x12;
  214. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  215. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  216. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  217. if(Fota_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  218. {
  219. BMS_Fota_update_flag = TRUE;
  220. }
  221. else
  222. {
  223. BMS_Fota_update_flag = FALSE;
  224. }
  225. break;
  226. }
  227. default:
  228. break;
  229. }
  230. }
  231. }
  232. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
  233. {
  234. UINT8 bcc_chk_return = 0x00;
  235. UINT8 count = 0;
  236. while (count<length)
  237. {
  238. bcc_chk_return^=data[count];
  239. count++;
  240. }
  241. return bcc_chk_return;
  242. }
  243. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
  244. {
  245. UINT8 reg_crc=0x00;
  246. while(length--)
  247. {
  248. reg_crc ^= *data++;
  249. }
  250. return reg_crc;
  251. }