iap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "iap.h"
  5. #include "can_id_deal.h"
  6. #include "flash.h"
  7. #include "queue.h"
  8. #include <stdint.h>
  9. #include <string.h>
  10. /************************************************************************************************
  11. * Config *
  12. ************************************************************************************************/
  13. extern uint8_t jump_flag;
  14. uint8_t can_rx_flag;
  15. // extern can_receive_message receive_iap_message;
  16. static uint16_t jump_boot_time = 0;
  17. uint8_t send_buff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  18. unsigned char recv_crc[2];
  19. unsigned char recvdata_ema[204];
  20. unsigned char recvdata_buffer[210];
  21. #define SW_VER 0x2446
  22. #define SW_VER_HI_ASCII ((('0' + ((uint8_t)((SW_VER >> 8) & 0x000F))) << 8) | \
  23. ('0' + ((uint8_t)((SW_VER >> 12) & 0x000F))))
  24. #define SW_VER_L0_ASCII ((('0' + ((uint8_t)(SW_VER & 0x000F))) << 8) | \
  25. ('0' + ((uint8_t)((SW_VER >> 4) & 0x000F))))
  26. #define PACK_TYPE 1
  27. #define PACK_TYPE_ASCII ((('0' + PACK_TYPE) << 8) | ('0'))
  28. uint16_t firm_ver[UPGRADE_APP_DATA_SIZE] = {IAP_CMD_JUMP_OVER, SW_VER_HI_ASCII, SW_VER_L0_ASCII, 0xFFFF};
  29. uint16_t firm_ver_buff[UPGRADE_BUFF_DATA_SIZE] = {0};
  30. uint8_t iap_disconnect_count = 0;
  31. /************************************************************************************************
  32. * Data structs *
  33. ************************************************************************************************/
  34. iap_type g_iap;
  35. upgrade_param upgrade;
  36. /************************************************************************************************
  37. * implements *
  38. ************************************************************************************************/
  39. void invert_uint8()
  40. {
  41. }
  42. void iap_connect_clear(void)
  43. {
  44. iap_disconnect_count = 0;
  45. send_buff[0] = 0x00;
  46. send_buff[1] = 0x00;
  47. send_buff[2] = 0x00;
  48. send_buff[3] = 0x00;
  49. send_buff[4] = 0x00;
  50. send_buff[5] = 0x00;
  51. send_buff[6] = 0x00;
  52. send_buff[7] = 0x00;
  53. }
  54. void iap_disconnect_check(void)
  55. {
  56. if (++iap_disconnect_count >= 5)
  57. {
  58. iap_disconnect_count = 5;
  59. }
  60. }
  61. // iap相关参数初始化
  62. void iap_param_init(void)
  63. {
  64. g_iap.recv_len = 0;
  65. g_iap.total_packages = 0;
  66. g_iap.current_packages = 0;
  67. g_iap.crc16 = 0xFFFF;
  68. g_iap.total_bytes = 0;
  69. g_iap.count_bytes = 0;
  70. g_iap.count_packages = 0;
  71. g_iap.firmware_crc16 = 0;
  72. g_iap.firmware_ver = 0;
  73. }
  74. // 清除升级信息标志位
  75. void set_firmver(void)
  76. {
  77. fmc_clear_flag_star();
  78. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  79. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  80. for (uint8_t i = 0; i < UPGRADE_APP_DATA_SIZE; i++)
  81. {
  82. firm_ver_buff[i + UPGRADE_FLAG_DATA_SIZE] = firm_ver[i];
  83. }
  84. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  85. fmc_clear_flag_end();
  86. }
  87. void iap_flag_release(void)
  88. {
  89. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  90. if (upgrade.param.sign == UPGRADE_SIGN)
  91. {
  92. fmc_clear_flag_star();
  93. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  94. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2), firm_ver, UPGRADE_APP_DATA_SIZE);
  95. fmc_clear_flag_end();
  96. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  97. {
  98. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  99. }
  100. can_msg_tx(0x18000001, CAN_Id_Extended, send_buff, 8);
  101. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  102. }
  103. }
  104. uint8_t erase_app_flash(uint32_t start_address, uint32_t end_addr)
  105. {
  106. uint8_t status = 0;
  107. uint16_t page_len = 0;
  108. // page_len = (end_addr - start_address) / FMC_PAGE_SIZE;
  109. if ((start_address == ADDR_FLASH_SECTOR_2))
  110. {
  111. status = fmc_erase_pages(start_address, 5);
  112. }
  113. else if ((start_address == ADDR_FLASH_SECTOR_8))
  114. {
  115. status = fmc_erase_pages(start_address, 3);
  116. }
  117. else
  118. {
  119. status = 1;
  120. }
  121. return status;
  122. }
  123. void jump_boot_time_ctrl(void)
  124. {
  125. if (jump_boot_time > 0)
  126. {
  127. jump_boot_time--;
  128. if (jump_boot_time == 0)
  129. {
  130. // jump_flag = 1;
  131. __set_FAULTMASK(1); // 关闭所有中断
  132. NVIC_SystemReset(); // 复位
  133. }
  134. }
  135. }
  136. void write_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  137. {
  138. static uint32_t w_start_addr = 0;
  139. w_start_addr = pack_num * 6 + APP_START_ADDR;
  140. fmc_write_n_half_word(w_start_addr, buf, pack_len);
  141. }
  142. void read_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  143. {
  144. static uint32_t w_start_addr = 0;
  145. w_start_addr = pack_num * 6 + APP_START_ADDR;
  146. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  147. }
  148. uint8_t write_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  149. {
  150. uint8_t status = 0;
  151. static uint32_t w_start_addr = 0;
  152. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  153. status = fmc_write_n_half_word(w_start_addr, buf, pack_len);
  154. return status;
  155. }
  156. void read_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  157. {
  158. static uint32_t w_start_addr = 0;
  159. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  160. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  161. }
  162. void iap_rec_handler(pdu_tag rec_msg)
  163. {
  164. uint8_t erase_flag = 0x01;
  165. g_can_ota_id = rec_msg.id.r;
  166. if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  167. iap_connect_clear();
  168. if (rec_msg.data.u8_buf[0] >> 7)
  169. {
  170. if (rec_msg.id.b.sa == 0xE1)
  171. {
  172. g_iap.recv_len = rec_msg.reg.dlc - 2;
  173. g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  174. memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  175. if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  176. {
  177. g_iap.count_bytes += g_iap.recv_len;
  178. if ((g_iap.recv_len % 2) != 0)
  179. {
  180. g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  181. g_iap.recv_len++;
  182. }
  183. fmc_clear_flag_star();
  184. erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  185. fmc_clear_flag_end();
  186. g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  187. send_buff[0] = IAP_CMD_DATA;
  188. send_buff[1] = !erase_flag;
  189. g_iap.count_packages++;
  190. }
  191. else
  192. {
  193. send_buff[0] = IAP_CMD_DATA;
  194. send_buff[1] = 0x06;
  195. }
  196. send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  197. send_buff[3] = rec_msg.data.u8_buf[1];
  198. send_buff[4] = g_iap.count_packages >> 8;
  199. send_buff[5] = g_iap.count_packages & 0xFF;
  200. }
  201. }
  202. else
  203. {
  204. switch (rec_msg.data.u8_buf[0])
  205. {
  206. case IAP_CMD_VERSION:
  207. iap_param_init();
  208. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  209. {
  210. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  211. }
  212. send_buff[0] = IAP_CMD_VERSION;
  213. send_buff[1] = 0x01;
  214. break;
  215. case IAP_CMD_INFO:
  216. g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
  217. g_iap.total_bytes <<= 16;
  218. g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
  219. g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
  220. send_buff[0] = IAP_CMD_INFO;
  221. send_buff[1] = 0x01;
  222. break;
  223. case IAP_CMD_ERASE:
  224. g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
  225. if (rec_msg.data.u8_buf[1])
  226. {
  227. // 喂狗
  228. fmc_clear_flag_star();
  229. erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_END_ADDR);
  230. fmc_clear_flag_end();
  231. // 喂狗
  232. g_iap.count_bytes = 0;
  233. g_iap.count_packages = 0;
  234. }
  235. send_buff[0] = IAP_CMD_ERASE;
  236. send_buff[1] = !erase_flag;
  237. break;
  238. case IAP_CMD_CRC:
  239. send_buff[0] = IAP_CMD_CRC;
  240. send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  241. break;
  242. case IAP_CMD_JUMP:
  243. if (g_iap.crc16 != g_iap.firmware_crc16)
  244. {
  245. send_buff[1] = 0x00;
  246. }
  247. else
  248. {
  249. fmc_clear_flag_star();
  250. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  251. upgrade.param.sign = UPGRADE_SIGN;
  252. upgrade.param.crc16 = g_iap.crc16;
  253. upgrade.param.moving_size = g_iap.total_bytes;
  254. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  255. fmc_clear_flag_end();
  256. jump_boot_time = 10;
  257. send_buff[1] = 0x01;
  258. }
  259. send_buff[0] = IAP_CMD_JUMP;
  260. break;
  261. case IAP_CMD_MODEL_TYPE:
  262. send_buff[0] = IAP_CMD_MODEL_TYPE;
  263. send_buff[1] = 0x01;
  264. send_buff[2] = DEVICE_ID >> 8;
  265. send_buff[3] = DEVICE_ID & 0xFF;
  266. }
  267. }
  268. push_can_message_to_queue(get_ota_id(CAN_IAP), 8, send_buff);
  269. }