dev_iap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. return status;
  98. }
  99. void write_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  100. {
  101. static uint32_t w_start_addr = 0;
  102. w_start_addr = pack_num * 6 + APP_START_ADDR;
  103. fmc_write_n_half_word(w_start_addr, buf, pack_len);
  104. }
  105. void read_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  106. {
  107. static uint32_t w_start_addr = 0;
  108. w_start_addr = pack_num * 6 + APP_START_ADDR;
  109. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  110. }
  111. uint8_t write_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  112. {
  113. uint8_t status = 0;
  114. static uint32_t w_start_addr = 0;
  115. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  116. status = fmc_write_n_half_word(w_start_addr, buf, pack_len);
  117. return status;
  118. }
  119. void read_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  120. {
  121. static uint32_t w_start_addr = 0;
  122. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  123. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  124. }
  125. void iap_rec_handler(pdu_tag rec_msg)
  126. {
  127. uint8_t erase_flag = 0x01;
  128. // ota_can_id = rec_msg.id.r;
  129. if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  130. iap_connect_clear();
  131. if (rec_msg.data.u8_buf[0] >> 7)
  132. {
  133. if (rec_msg.id.b.sa == 0xE1)
  134. {
  135. g_iap.recv_len = rec_msg.reg.dlc - 2;
  136. g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  137. memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  138. if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  139. {
  140. g_iap.count_bytes += g_iap.recv_len;
  141. if ((g_iap.recv_len % 2) != 0)
  142. {
  143. g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  144. g_iap.recv_len++;
  145. }
  146. fmc_clear_flag_star();
  147. erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  148. fmc_clear_flag_end();
  149. g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  150. send_buff[0] = IAP_CMD_DATA;
  151. send_buff[1] = !erase_flag;
  152. g_iap.count_packages++;
  153. }
  154. else
  155. {
  156. send_buff[0] = IAP_CMD_DATA;
  157. send_buff[1] = 0x06;
  158. }
  159. send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  160. send_buff[3] = rec_msg.data.u8_buf[1];
  161. send_buff[4] = g_iap.count_packages >> 8;
  162. send_buff[5] = g_iap.count_packages & 0xFF;
  163. }
  164. }
  165. else
  166. {
  167. switch (rec_msg.data.u8_buf[0])
  168. {
  169. case IAP_CMD_VERSION:
  170. iap_param_init();
  171. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  172. {
  173. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  174. }
  175. send_buff[0] = IAP_CMD_VERSION;
  176. send_buff[1] = 0x01;
  177. break;
  178. case IAP_CMD_INFO:
  179. g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
  180. g_iap.total_bytes <<= 16;
  181. g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
  182. g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
  183. send_buff[0] = IAP_CMD_INFO;
  184. send_buff[1] = 0x01;
  185. break;
  186. case IAP_CMD_ERASE:
  187. g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
  188. if (rec_msg.data.u8_buf[1])
  189. {
  190. // 喂狗
  191. fmc_clear_flag_star();
  192. erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_MIDDLE_ADDR);
  193. fmc_clear_flag_end();
  194. // 喂狗
  195. g_iap.count_bytes = 0;
  196. g_iap.count_packages = 0;
  197. }
  198. send_buff[0] = IAP_CMD_ERASE;
  199. send_buff[1] = !erase_flag;
  200. break;
  201. case IAP_CMD_CRC:
  202. send_buff[0] = IAP_CMD_CRC;
  203. send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  204. break;
  205. case IAP_CMD_JUMP:
  206. if (g_iap.crc16 != g_iap.firmware_crc16)
  207. {
  208. send_buff[1] = 0x00;
  209. }
  210. else
  211. {
  212. fmc_clear_flag_star();
  213. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  214. upgrade.param.sign = UPGRADE_SIGN;
  215. upgrade.param.crc16 = g_iap.crc16;
  216. upgrade.param.moving_size = g_iap.total_bytes;
  217. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  218. fmc_clear_flag_end();
  219. jump_boot_time = 10;
  220. send_buff[1] = 0x01;
  221. }
  222. send_buff[0] = IAP_CMD_JUMP;
  223. break;
  224. case IAP_CMD_MODEL_TYPE:
  225. send_buff[0] = IAP_CMD_MODEL_TYPE;
  226. send_buff[1] = 0x01;
  227. send_buff[2] = DEVICE_ID >> 8;
  228. send_buff[3] = DEVICE_ID & 0xFF;
  229. }
  230. }
  231. push_can_message_to_queue(0x18DFE1F4, 8, send_buff);
  232. }
  233. // void iap_rec_handler(pdu_tag rec_msg)
  234. // {
  235. // uint8_t erase_flag = 0x01;
  236. // // ota_can_id = rec_msg.id.r;
  237. // if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  238. // iap_connect_clear();
  239. // if (rec_msg.data.u8_buf[0] >> 7)
  240. // {
  241. // if (rec_msg.id.b.sa == 0xE1)
  242. // {
  243. // g_iap.recv_len = rec_msg.reg.dlc - 2;
  244. // g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  245. // memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  246. // if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  247. // {
  248. // g_iap.count_bytes += g_iap.recv_len;
  249. // if ((g_iap.recv_len % 2) != 0)
  250. // {
  251. // g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  252. // g_iap.recv_len++;
  253. // }
  254. // fmc_clear_flag_star();
  255. // erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  256. // fmc_clear_flag_end();
  257. // g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  258. // send_buff[0] = IAP_CMD_DATA;
  259. // send_buff[1] = !erase_flag;
  260. // g_iap.count_packages++;
  261. // }
  262. // else
  263. // {
  264. // send_buff[0] = IAP_CMD_DATA;
  265. // send_buff[1] = 0x06;
  266. // }
  267. // send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  268. // send_buff[3] = rec_msg.data.u8_buf[1];
  269. // send_buff[4] = g_iap.count_packages >> 8;
  270. // send_buff[5] = g_iap.count_packages & 0xFF;
  271. // }
  272. // else
  273. // {
  274. // switch (rec_msg.data.u8_buf[0])
  275. // {
  276. // case IAP_CMD_VERSION:
  277. // iap_param_init();
  278. // for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  279. // {
  280. // send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_START_ADDR * 2) + i);
  281. // }
  282. // send_buff[0] = IAP_CMD_VERSION;
  283. // send_buff[1] = 0x01;
  284. // break;
  285. // case IAP_CMD_INFO:
  286. // g_iap.total_bytes = (uint16_t)rec_msg.data.u8_buf[1] << 8 | rec_msg.data.u8_buf[2];
  287. // g_iap.total_bytes <<= 16;
  288. // g_iap.total_bytes |= (uint16_t)rec_msg.data.u8_buf[3] << 8 | rec_msg.data.u8_buf[4];
  289. // g_iap.total_packages = (uint16_t)rec_msg.data.u8_buf[5] << 8 | rec_msg.data.u8_buf[6];
  290. // send_buff[0] = IAP_CMD_INFO;
  291. // send_buff[1] = 0x01;
  292. // break;
  293. // case IAP_CMD_ERASE:
  294. // g_iap.firmware_crc16 = (uint16_t)rec_msg.data.u8_buf[2] << 8 | rec_msg.data.u8_buf[3];
  295. // if (rec_msg.data.u8_buf[1])
  296. // {
  297. // // 喂狗
  298. // fmc_clear_flag_star();
  299. // erase_flag = erase_app_flash(DOWNLOAD_START_ADDR, DOWNLOAD_END_ADDR / 2);
  300. // fmc_clear_flag_end();
  301. // // 喂狗
  302. // g_iap.count_bytes = 0;
  303. // g_iap.count_packages = 0;
  304. // }
  305. // send_buff[0] = IAP_CMD_ERASE;
  306. // send_buff[1] = !erase_flag;
  307. // case IAP_CMD_CRC:
  308. // send_buff[0] = IAP_CMD_CRC;
  309. // send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  310. // break;
  311. // case IAP_CMD_JUMP:
  312. // if (g_iap.crc16 != g_iap.firmware_crc16)
  313. // {
  314. // send_buff[1] = 0x00;
  315. // }
  316. // else
  317. // {
  318. // fmc_clear_flag_star();
  319. // fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  320. // upgrade.param.sign = UPGRADE_SIGN;
  321. // upgrade.param.crc16 = g_iap.crc16;
  322. // upgrade.param.moving_size = g_iap.total_bytes;
  323. // fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  324. // fmc_clear_flag_end();
  325. // jump_boot_time = 10;
  326. // send_buff[1] = 0x01;
  327. // }
  328. // break;
  329. // case IAP_CMD_MODEL_TYPE:
  330. // send_buff[0] = IAP_CMD_MODEL_TYPE;
  331. // send_buff[1] = 0x01;
  332. // send_buff[2] = DEVICE_ID >> 8;
  333. // send_buff[3] = DEVICE_ID & 0xFF;
  334. // }
  335. // }
  336. // // push_can_message_to_queue(IAP, 8, send_buff);
  337. // }
  338. // }