dev_iap.c 9.4 KB

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