dev_iap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "dev_iap.h"
  5. #include "drv_flash.h"
  6. #include <stdint.h>
  7. #include <string.h>
  8. /************************************************************************************************
  9. * Config *
  10. ************************************************************************************************/
  11. extern uint8_t jump_flag;
  12. uint8_t can_rx_flag;
  13. // extern can_receive_message receive_iap_message;
  14. static uint16_t jump_boot_time = 0;
  15. uint8_t send_buff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  16. unsigned char recv_crc[2];
  17. unsigned char recvdata_ema[204];
  18. unsigned char recvdata_buffer[210];
  19. uint16_t firm_ver[UPGRADE_APP_DATA_SIZE] = {IAP_CMD_JUMP_OVER};
  20. uint16_t firm_ver_buff[UPGRADE_BUFF_DATA_SIZE] = {0};
  21. uint8_t iap_disconnect_count = 0;
  22. /************************************************************************************************
  23. * Data structs *
  24. ************************************************************************************************/
  25. iap_type g_iap;
  26. upgrade_param upgrade;
  27. /************************************************************************************************
  28. * implements *
  29. ************************************************************************************************/
  30. void invert_uint8()
  31. {
  32. }
  33. void iap_connect_clear(void)
  34. {
  35. iap_disconnect_count = 0;
  36. send_buff[0] = 0x00;
  37. send_buff[1] = 0x00;
  38. send_buff[2] = 0x00;
  39. send_buff[3] = 0x00;
  40. send_buff[4] = 0x00;
  41. send_buff[5] = 0x00;
  42. send_buff[6] = 0x00;
  43. send_buff[7] = 0x00;
  44. }
  45. void iap_disconnect_check(void)
  46. {
  47. if (++iap_disconnect_count >= 5)
  48. {
  49. iap_disconnect_count = 5;
  50. }
  51. }
  52. // iap相关参数初始化
  53. void iap_param_init(void)
  54. {
  55. g_iap.recv_len = 0;
  56. g_iap.total_packages = 0;
  57. g_iap.current_packages = 0;
  58. g_iap.crc16 = 0xFFFF;
  59. g_iap.total_bytes = 0;
  60. g_iap.count_bytes = 0;
  61. g_iap.count_packages = 0;
  62. g_iap.firmware_crc16 = 0;
  63. g_iap.firmware_ver = 0;
  64. }
  65. // 清除升级信息标志位
  66. void set_firmver(void)
  67. {
  68. fmc_clear_flag_star();
  69. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  70. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  71. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  72. fmc_clear_flag_end();
  73. }
  74. void iap_flag_release(void)
  75. {
  76. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  77. if (upgrade.param.sign == UPGRADE_SIGN)
  78. {
  79. fmc_clear_flag_star();
  80. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  81. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2), firm_ver, UPGRADE_APP_DATA_SIZE);
  82. fmc_clear_flag_end();
  83. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  84. {
  85. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  86. }
  87. // hal_can_msg_tx(0x180000001, CAN_Id_Extended, send_buff, 8);
  88. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  89. }
  90. }
  91. uint8_t erase_app_flash(uint32_t start_address, uint32_t end_addr)
  92. {
  93. uint8_t status = 0;
  94. uint16_t page_len = 0;
  95. // page_len = (end_addr - start_address) / FMC_PAGE_SIZE;
  96. // status = fmc_erase_pages(start_address, page_len);
  97. if ((start_address == ADDR_FLASH_SECTOR_2))
  98. {
  99. status = fmc_erase_pages(start_address, 5);
  100. }
  101. else if ((start_address == ADDR_FLASH_SECTOR_8))
  102. {
  103. status = fmc_erase_pages(start_address, 3);
  104. }
  105. else
  106. {
  107. status = 1;
  108. }
  109. return status;
  110. }
  111. void jump_boot_time_ctrl(void)
  112. {
  113. if (jump_boot_time > 0)
  114. {
  115. jump_boot_time--;
  116. if (jump_boot_time == 0)
  117. {
  118. // jump_flag = 1;
  119. __set_FAULTMASK(1); // 关闭所有中断
  120. NVIC_SystemReset(); // 复位
  121. }
  122. }
  123. }
  124. void write_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  125. {
  126. static uint32_t w_start_addr = 0;
  127. w_start_addr = pack_num * 6 + APP_START_ADDR;
  128. fmc_write_n_half_word(w_start_addr, buf, pack_len);
  129. }
  130. void read_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  131. {
  132. static uint32_t w_start_addr = 0;
  133. w_start_addr = pack_num * 6 + APP_START_ADDR;
  134. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  135. }
  136. uint8_t write_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  137. {
  138. uint8_t status = 0;
  139. static uint32_t w_start_addr = 0;
  140. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  141. status = fmc_write_n_half_word(w_start_addr, buf, pack_len);
  142. return status;
  143. }
  144. void read_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  145. {
  146. static uint32_t w_start_addr = 0;
  147. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  148. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  149. }
  150. void iap_rec_handler(pdu_tag rec_msg)
  151. {
  152. uint8_t erase_flag = 0x01;
  153. // ota_can_id = rec_msg.id.r;
  154. if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  155. iap_connect_clear();
  156. if (rec_msg.data.u8_buf[0] >> 7)
  157. {
  158. if (rec_msg.id.b.sa == 0xE1)
  159. {
  160. g_iap.recv_len = rec_msg.reg.dlc - 2;
  161. g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  162. memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  163. if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  164. {
  165. g_iap.count_bytes += g_iap.recv_len;
  166. if ((g_iap.recv_len % 2) != 0)
  167. {
  168. g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  169. g_iap.recv_len++;
  170. }
  171. fmc_clear_flag_star();
  172. erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  173. fmc_clear_flag_end();
  174. g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  175. send_buff[0] = IAP_CMD_DATA;
  176. send_buff[1] = !erase_flag;
  177. g_iap.count_packages++;
  178. }
  179. else
  180. {
  181. send_buff[0] = IAP_CMD_DATA;
  182. send_buff[1] = 0x06;
  183. }
  184. send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  185. send_buff[3] = rec_msg.data.u8_buf[1];
  186. send_buff[4] = g_iap.count_packages >> 8;
  187. send_buff[5] = g_iap.count_packages & 0xFF;
  188. }
  189. }
  190. else
  191. {
  192. switch (rec_msg.data.u8_buf[0])
  193. {
  194. case IAP_CMD_VERSION:
  195. iap_param_init();
  196. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  197. {
  198. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  199. }
  200. send_buff[0] = IAP_CMD_VERSION;
  201. send_buff[1] = 0x01;
  202. break;
  203. case IAP_CMD_INFO:
  204. g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
  205. g_iap.total_bytes <<= 16;
  206. g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
  207. g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
  208. send_buff[0] = IAP_CMD_INFO;
  209. send_buff[1] = 0x01;
  210. break;
  211. case IAP_CMD_ERASE:
  212. g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
  213. if (rec_msg.data.u8_buf[1])
  214. {
  215. // 喂狗
  216. fmc_clear_flag_star();
  217. erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_END_ADDR);
  218. fmc_clear_flag_end();
  219. // 喂狗
  220. g_iap.count_bytes = 0;
  221. g_iap.count_packages = 0;
  222. }
  223. send_buff[0] = IAP_CMD_ERASE;
  224. send_buff[1] = !erase_flag;
  225. break;
  226. case IAP_CMD_CRC:
  227. send_buff[0] = IAP_CMD_CRC;
  228. send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  229. break;
  230. case IAP_CMD_JUMP:
  231. if (g_iap.crc16 != g_iap.firmware_crc16)
  232. {
  233. send_buff[1] = 0x00;
  234. }
  235. else
  236. {
  237. fmc_clear_flag_star();
  238. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  239. upgrade.param.sign = UPGRADE_SIGN;
  240. upgrade.param.crc16 = g_iap.crc16;
  241. upgrade.param.moving_size = g_iap.total_bytes;
  242. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  243. fmc_clear_flag_end();
  244. jump_boot_time = 10;
  245. send_buff[1] = 0x01;
  246. }
  247. send_buff[0] = IAP_CMD_JUMP;
  248. break;
  249. case IAP_CMD_MODEL_TYPE:
  250. send_buff[0] = IAP_CMD_MODEL_TYPE;
  251. send_buff[1] = 0x01;
  252. send_buff[2] = DEVICE_ID >> 8;
  253. send_buff[3] = DEVICE_ID & 0xFF;
  254. }
  255. }
  256. push_can_message_to_queue(0x18DFE1F4, 8, send_buff);
  257. }
  258. // void iap_rec_handler(pdu_tag rec_msg)
  259. // {
  260. // uint8_t erase_flag = 0x01;
  261. // // ota_can_id = rec_msg.id.r;
  262. // if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  263. // iap_connect_clear();
  264. // if (rec_msg.data.u8_buf[0] >> 7)
  265. // {
  266. // if (rec_msg.id.b.sa == 0xE1)
  267. // {
  268. // g_iap.recv_len = rec_msg.reg.dlc - 2;
  269. // g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  270. // memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  271. // if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  272. // {
  273. // g_iap.count_bytes += g_iap.recv_len;
  274. // if ((g_iap.recv_len % 2) != 0)
  275. // {
  276. // g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  277. // g_iap.recv_len++;
  278. // }
  279. // fmc_clear_flag_star();
  280. // erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  281. // fmc_clear_flag_end();
  282. // g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  283. // send_buff[0] = IAP_CMD_DATA;
  284. // send_buff[1] = !erase_flag;
  285. // g_iap.count_packages++;
  286. // }
  287. // else
  288. // {
  289. // send_buff[0] = IAP_CMD_DATA;
  290. // send_buff[1] = 0x06;
  291. // }
  292. // send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  293. // send_buff[3] = rec_msg.data.u8_buf[1];
  294. // send_buff[4] = g_iap.count_packages >> 8;
  295. // send_buff[5] = g_iap.count_packages & 0xFF;
  296. // }
  297. // else
  298. // {
  299. // switch (rec_msg.data.u8_buf[0])
  300. // {
  301. // case IAP_CMD_VERSION:
  302. // iap_param_init();
  303. // for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  304. // {
  305. // send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_START_ADDR * 2) + i);
  306. // }
  307. // send_buff[0] = IAP_CMD_VERSION;
  308. // send_buff[1] = 0x01;
  309. // break;
  310. // case IAP_CMD_INFO:
  311. // g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
  312. // g_iap.total_bytes <<= 16;
  313. // g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
  314. // g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
  315. // send_buff[0] = IAP_CMD_INFO;
  316. // send_buff[1] = 0x01;
  317. // break;
  318. // case IAP_CMD_ERASE:
  319. // g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
  320. // if (rec_msg.data.u8_buf[1])
  321. // {
  322. // // 喂狗
  323. // fmc_clear_flag_star();
  324. // erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_END_ADDR / 2);
  325. // fmc_clear_flag_end();
  326. // // 喂狗
  327. // g_iap.count_bytes = 0;
  328. // g_iap.count_packages = 0;
  329. // }
  330. // send_buff[0] = IAP_CMD_ERASE;
  331. // send_buff[1] = !erase_flag;
  332. // case IAP_CMD_CRC:
  333. // send_buff[0] = IAP_CMD_CRC;
  334. // send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  335. // break;
  336. // case IAP_CMD_JUMP:
  337. // if (g_iap.crc16 != g_iap.firmware_crc16)
  338. // {
  339. // send_buff[1] = 0x00;
  340. // }
  341. // else
  342. // {
  343. // fmc_clear_flag_star();
  344. // fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  345. // upgrade.param.sign = UPGRADE_SIGN;
  346. // upgrade.param.crc16 = g_iap.crc16;
  347. // upgrade.param.moving_size = g_iap.total_bytes;
  348. // fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  349. // fmc_clear_flag_end();
  350. // jump_boot_time = 10;
  351. // send_buff[1] = 0x01;
  352. // }
  353. // break;
  354. // case IAP_CMD_MODEL_TYPE:
  355. // send_buff[0] = IAP_CMD_MODEL_TYPE;
  356. // send_buff[1] = 0x01;
  357. // send_buff[2] = DEVICE_ID >> 8;
  358. // send_buff[3] = DEVICE_ID & 0xFF;
  359. // }
  360. // }
  361. // // push_can_message_to_queue(IAP, 8, send_buff);
  362. // }
  363. // }