Fota.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 Fota_update_flag = FALSE; //可以升级标志
  21. void Fota_Func(UINT8 *DataPtr,INT32 connectId,UINT8 FotaType)
  22. {
  23. UINT8 Fota_Answer[43];
  24. UINT8 Fota_Cmd;
  25. INT8 ret;
  26. if(FotaType==0x01)
  27. {
  28. Fota_S.Fota_Flash_Addres = FLASH_FOTA_REGION_START;
  29. }
  30. else if(FotaType == 0x88)
  31. {
  32. Fota_S.Fota_Flash_Addres = FLASH_BMS_FOTA_START_ADDR;
  33. }
  34. if(*(DataPtr+30)==0x01)
  35. {
  36. Fota_Cmd = *(DataPtr+31);
  37. Fota_Answer[0] = TCP_START_SYM1;
  38. Fota_Answer[1] = TCP_START_SYM2;
  39. Fota_Answer[2] = TCP_CONCMD_SYM;
  40. switch (Fota_Cmd)
  41. {
  42. case 0x01:
  43. {
  44. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  45. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  46. if(FotaType==0x01)
  47. {
  48. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_START_ADDR - FLASH_FOTA_REGION_START))
  49. {
  50. Fota_Answer[3] = 0x02;
  51. }
  52. else
  53. {
  54. Fota_Answer[3] = 0x01;
  55. }
  56. }
  57. if(FotaType==0x88)
  58. {
  59. if(Fota_S.Fota_All_Data_Len>=(FLASH_BMS_FOTA_END_ADDR - FLASH_BMS_FOTA_START_ADDR))
  60. {
  61. Fota_Answer[3] = 0x02;
  62. }
  63. else
  64. {
  65. Fota_Answer[3] = 0x01;
  66. }
  67. }
  68. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  69. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  70. Fota_Answer[22] = 0x00;
  71. Fota_Answer[23] = 0x12;
  72. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  73. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  74. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  75. if(Fota_Answer[3] == 0x01)
  76. {
  77. 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
  78. }
  79. break;
  80. }
  81. case 0x02:
  82. {
  83. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  84. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  85. Fota_S.Fota_Recv_Data_Len = *(DataPtr+41);
  86. memset(Fota_S.Fota_Recv_Data,0x00,100);
  87. memcpy(Fota_S.Fota_Recv_Data,(DataPtr+42),*(DataPtr+41));
  88. Fota_S.Fota_CRC = Fota_crc_chk(Fota_S.Fota_Recv_Data,Fota_S.Fota_Recv_Data_Len);
  89. #ifdef USING_PRINTF1
  90. printf("\Data_Add:%x,Crc:%x,%x\n",Fota_S.Fota_Current_Addres,Fota_S.Fota_CRC,*(DataPtr+Fota_S.Fota_Recv_Data_Len+42));
  91. UINT8 temp[5] = {0x01,0x02,0x03,0x04,0x05};
  92. ret = BSP_QSPI_Write_Safe(temp,Fota_S.Fota_Flash_Addres+0xffe,5);
  93. UINT8 tPtr[1];
  94. printf("test:\n");
  95. for(UINT32 i = 0;i<5;i++)
  96. {
  97. BSP_QSPI_Read_Safe(tPtr,Fota_S.Fota_Flash_Addres+0xffe+i,1);
  98. printf("%x ",tPtr[0]);
  99. }
  100. printf("\ndata end\n");
  101. #endif
  102. if(Fota_S.Fota_CRC == *(DataPtr+Fota_S.Fota_Recv_Data_Len+42))
  103. {
  104. if(Fota_S.Fota_Recv_Data_Len%4!=0)
  105. {
  106. Fota_S.Fota_Recv_Data_Len = Fota_S.Fota_Recv_Data_Len + 4-(Fota_S.Fota_Recv_Data_Len%4);
  107. }
  108. #ifdef USING_PRINTF1
  109. printf("\nlen:%x,%xFota_Data_Recv :",Fota_S.Fota_Recv_Data_Len,Fota_S.Fota_Current_Addres);
  110. for(UINT32 i = 0;i<Fota_S.Fota_Recv_Data_Len;i++)
  111. {
  112. printf("%x ",Fota_S.Fota_Recv_Data[i]);
  113. }
  114. printf("\ndata Recv end\n");
  115. #endif
  116. 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);
  117. #ifdef USING_PRINTF1
  118. printf("\nret:%d\n",ret);
  119. #endif
  120. if(ret==QSPI_OK)
  121. {
  122. Fota_Answer[3] = 0x01;
  123. }
  124. else
  125. {
  126. Fota_Answer[3] = 0x02;
  127. }
  128. #ifdef USING_PRINTF1
  129. UINT8 Ptr[1];
  130. printf("\nlen:%x,%xFota_Data :",Fota_S.Fota_Recv_Data_Len,Fota_S.Fota_Current_Addres);
  131. for(UINT32 i = 0;i<Fota_S.Fota_Recv_Data_Len;i++)
  132. {
  133. BSP_QSPI_Read_Safe(Ptr,Fota_S.Fota_Flash_Addres+Fota_S.Fota_Current_Addres+i,1);
  134. printf("%x ",Ptr[0]);
  135. }
  136. printf("\ndata end\n");
  137. #endif
  138. }
  139. else//数据校验失败
  140. {
  141. Fota_Answer[3] = 0x02;
  142. }
  143. memcpy(&Fota_Answer[4],(DataPtr+4),BATT_SN_LEN);
  144. Fota_Answer[21] = TCP_ENCPT_DISABLE;
  145. Fota_Answer[22] = 0x00;
  146. Fota_Answer[23] = 0x12;
  147. memcpy(&Fota_Answer[24],(DataPtr+24),18);
  148. Fota_Answer[42] = bcc_chk_fota(Fota_Answer,42);
  149. tcpipConnectionSend(connectId,Fota_Answer,43,0,0,0);
  150. break;
  151. }
  152. case 0x03:
  153. {
  154. Fota_S.Fota_All_Data_Len = *(DataPtr+33)<<24|*(DataPtr+34)<<16|*(DataPtr+35)<<8|*(DataPtr+36);
  155. Fota_S.Fota_Current_Addres = *(DataPtr+37)<<24|*(DataPtr+38)<<16|*(DataPtr+39)<<8|*(DataPtr+40);
  156. Fota_Answer[3] = 0x01;
  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] = 0x13;
  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_S.Fota_All_Data_Len==Fota_S.Fota_Current_Addres)
  165. {
  166. Fota_update_flag = TRUE;
  167. #ifdef USING_PRINTF1
  168. UINT8 *Ptr;
  169. printf("\nlen:%x,%xFota_Data :",Fota_S.Fota_All_Data_Len,Fota_S.Fota_Current_Addres);
  170. for(UINT32 i = 0;i<Fota_S.Fota_All_Data_Len;i++)
  171. {
  172. BSP_QSPI_Read_Safe(Ptr,Fota_Addres_Begin+i,1);
  173. printf("%x ",*(Ptr));
  174. }
  175. printf("\ndata end\n");
  176. #endif
  177. }
  178. else
  179. {
  180. Fota_update_flag = FALSE;
  181. }
  182. break;
  183. }
  184. default:
  185. break;
  186. }
  187. }
  188. }
  189. static UINT8 bcc_chk_fota(UINT8* data, UINT8 length)
  190. {
  191. UINT8 bcc_chk_return = 0x00;
  192. UINT8 count = 0;
  193. while (count<length)
  194. {
  195. bcc_chk_return^=data[count];
  196. count++;
  197. }
  198. return bcc_chk_return;
  199. }
  200. static UINT8 Fota_crc_chk(UINT8* data,UINT8 length)
  201. {
  202. UINT8 reg_crc=0x00;
  203. while(length--)
  204. {
  205. reg_crc ^= *data++;
  206. }
  207. return reg_crc;
  208. }