dev_iap.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "dev_iap.h"
  5. #include "hal_flash.h"
  6. #include "queue.h"
  7. #include <stdint.h>
  8. #include <string.h>
  9. /************************************************************************************************
  10. * Config *
  11. ************************************************************************************************/
  12. extern uint8_t jump_flag;
  13. uint8_t can_rx_flag;
  14. // extern can_receive_message receive_iap_message;
  15. static uint16_t jump_boot_time = 0;
  16. uint8_t send_buff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  17. unsigned char recv_crc[2];
  18. unsigned char recvdata_ema[204];
  19. unsigned char recvdata_buffer[210];
  20. uint16_t firm_ver[UPGRADE_APP_DATA_SIZE] = {IAP_CMD_JUMP_OVER};
  21. uint16_t firm_ver_buff[UPGRADE_BUFF_DATA_SIZE] = {0};
  22. uint8_t iap_disconnect_count = 0;
  23. /************************************************************************************************
  24. * Data structs *
  25. ************************************************************************************************/
  26. iap_type g_iap;
  27. upgrade_param upgrade;
  28. /************************************************************************************************
  29. * implements *
  30. ************************************************************************************************/
  31. void invert_uint8()
  32. {
  33. }
  34. void iap_connect_clear(void)
  35. {
  36. iap_disconnect_count = 0;
  37. send_buff[0] = 0x00;
  38. send_buff[1] = 0x00;
  39. send_buff[2] = 0x00;
  40. send_buff[3] = 0x00;
  41. send_buff[4] = 0x00;
  42. send_buff[5] = 0x00;
  43. send_buff[6] = 0x00;
  44. send_buff[7] = 0x00;
  45. }
  46. void iap_disconnect_check(void)
  47. {
  48. if (++iap_disconnect_count >= 5)
  49. {
  50. iap_disconnect_count = 5;
  51. }
  52. }
  53. // iap相关参数初始化
  54. void iap_param_init(void)
  55. {
  56. g_iap.recv_len = 0;
  57. g_iap.total_packages = 0;
  58. g_iap.current_packages = 0;
  59. g_iap.crc16 = 0xFFFF;
  60. g_iap.total_bytes = 0;
  61. g_iap.count_bytes = 0;
  62. g_iap.count_packages = 0;
  63. g_iap.firmware_crc16 = 0;
  64. g_iap.firmware_ver = 0;
  65. }
  66. // 清除升级信息标志位
  67. void set_firmver(void)
  68. {
  69. fmc_clear_flag_star();
  70. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  71. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  72. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, firm_ver_buff, UPGRADE_BUFF_DATA_SIZE);
  73. fmc_clear_flag_end();
  74. }
  75. void iap_flag_release(void)
  76. {
  77. fmc_read_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  78. if (upgrade.param.sign == UPGRADE_SIGN)
  79. {
  80. fmc_clear_flag_star();
  81. fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  82. fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2), firm_ver, UPGRADE_APP_DATA_SIZE);
  83. fmc_clear_flag_end();
  84. for (uint8_t i = 0; i < (UPGRADE_APP_DATA_SIZE * 2); i++)
  85. {
  86. send_buff[i] = fmc_read_byte(UPGRADE_FLAG_START_ADDR + (UPGRADE_FLAG_DATA_SIZE * 2) + i);
  87. }
  88. // hal_can_msg_tx(0x180000001, CAN_Id_Extended, send_buff, 8);
  89. CAN_ITConfig(CAN1, CAN_IT_TME, ENABLE);
  90. }
  91. }
  92. uint8_t erase_app_flash(uint32_t start_address, uint32_t end_addr)
  93. {
  94. uint8_t status = 0;
  95. uint16_t page_len = 0;
  96. // page_len = (end_addr - start_address) / FMC_PAGE_SIZE;
  97. status = fmc_erase_pages(start_address, page_len);
  98. return status;
  99. }
  100. void write_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  101. {
  102. static uint32_t w_start_addr = 0;
  103. w_start_addr = pack_num * 6 + APP_START_ADDR;
  104. fmc_write_n_half_word(w_start_addr, buf, pack_len);
  105. }
  106. void read_app_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  107. {
  108. static uint32_t w_start_addr = 0;
  109. w_start_addr = pack_num * 6 + APP_START_ADDR;
  110. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  111. }
  112. uint8_t write_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  113. {
  114. uint8_t status = 0;
  115. static uint32_t w_start_addr = 0;
  116. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  117. status = fmc_write_n_half_word(w_start_addr, buf, pack_len);
  118. return status;
  119. }
  120. void read_download_flash(uint16_t *buf, uint16_t pack_len, uint16_t pack_num)
  121. {
  122. static uint32_t w_start_addr = 0;
  123. w_start_addr = pack_num * 6 + DOWNLOAD_START_ADDR;
  124. fmc_read_n_half_word(w_start_addr, buf, pack_len);
  125. }
  126. // void iap_rec_handler(pdu_tag rec_msg)
  127. // {
  128. // uint8_t erase_flag = 0x01;
  129. // // ota_can_id = rec_msg.id.r;
  130. // if (rec_msg.data.u8_buf[0] != IAP_CMD_VERSION)
  131. // iap_connect_clear();
  132. // if (rec_msg.data.u8_buf[0] >> 7)
  133. // {
  134. // if (rec_msg.id.b.sa == 0xE1)
  135. // {
  136. // g_iap.recv_len = rec_msg.reg.dlc - 2;
  137. // g_iap.current_packages = ((uint16_t)(rec_msg.data.u8_buf[0] & 0x7F) << 8) | rec_msg.data.u8_buf[1];
  138. // memcpy(g_iap.flash_data.u8_buf, &rec_msg.data.u8_buf[2], g_iap.recv_len);
  139. // if ((g_iap.count_packages == g_iap.current_packages) && (g_iap.current_packages < g_iap.total_packages))
  140. // {
  141. // g_iap.count_bytes += g_iap.recv_len;
  142. // if ((g_iap.recv_len % 2) != 0)
  143. // {
  144. // g_iap.flash_data.u8_buf[g_iap.recv_len] = 0xFF;
  145. // g_iap.recv_len++;
  146. // }
  147. // fmc_clear_flag_star();
  148. // erase_flag = write_download_flash(g_iap.flash_data.u16_buf, g_iap.recv_len / 2, g_iap.count_packages);
  149. // fmc_clear_flag_end();
  150. // g_iap.crc16 = firmware_crc16_ccitt_false(g_iap.crc16, g_iap.flash_data.u8_buf, g_iap.recv_len);
  151. // send_buff[0] = IAP_CMD_DATA;
  152. // send_buff[1] = !erase_flag;
  153. // g_iap.count_packages++;
  154. // }
  155. // else
  156. // {
  157. // send_buff[0] = IAP_CMD_DATA;
  158. // send_buff[1] = 0x06;
  159. // }
  160. // send_buff[2] = rec_msg.data.u8_buf[0] & 0x7F;
  161. // send_buff[3] = rec_msg.data.u8_buf[1];
  162. // send_buff[4] = g_iap.count_packages >> 8;
  163. // send_buff[5] = g_iap.count_packages & 0xFF;
  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_START_ADDR * 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_END_ADDR / 2);
  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. // case IAP_CMD_CRC:
  201. // send_buff[0] = IAP_CMD_CRC;
  202. // send_buff[1] = (g_iap.crc16 == g_iap.firmware_crc16 ? 1 : 0);
  203. // break;
  204. // case IAP_CMD_JUMP:
  205. // if (g_iap.crc16 != g_iap.firmware_crc16)
  206. // {
  207. // send_buff[1] = 0x00;
  208. // }
  209. // else
  210. // {
  211. // fmc_clear_flag_star();
  212. // fmc_erase_pages(UPGRADE_FLAG_START_ADDR, 1);
  213. // upgrade.param.sign = UPGRADE_SIGN;
  214. // upgrade.param.crc16 = g_iap.crc16;
  215. // upgrade.param.moving_size = g_iap.total_bytes;
  216. // fmc_write_n_half_word(UPGRADE_FLAG_START_ADDR, upgrade.infor_buf, UPGRADE_FLAG_DATA_SIZE);
  217. // fmc_clear_flag_end();
  218. // jump_boot_time = 10;
  219. // send_buff[1] = 0x01;
  220. // }
  221. // break;
  222. // case IAP_CMD_MODEL_TYPE:
  223. // send_buff[0] = IAP_CMD_MODEL_TYPE;
  224. // send_buff[1] = 0x01;
  225. // send_buff[2] = DEVICE_ID >> 8;
  226. // send_buff[3] = DEVICE_ID & 0xFF;
  227. // }
  228. // }
  229. // // push_can_message_to_queue(IAP, 8, send_buff);
  230. // }
  231. // }