iap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include "iap.h"
  2. #include "stm32f4xx_flash.h"
  3. // #include "mcp2515.h"
  4. #include "bootloader.h"
  5. #include "can.h"
  6. #include "dwt.h"
  7. #include "global.h"
  8. #include <string.h>
  9. static u8 iap_index = 0;
  10. static u16 iap_buf[IAP_BUF_SIZE] = {0};
  11. extern SqQueue CanQueueImportant; // CAN重要数据队列
  12. uint16_t STMFLASH_GetFlashSector(u32 addr)
  13. {
  14. if (addr < ADDR_FLASH_SECTOR_1)
  15. return FLASH_Sector_0;
  16. else if (addr < ADDR_FLASH_SECTOR_2)
  17. return FLASH_Sector_1;
  18. else if (addr < ADDR_FLASH_SECTOR_3)
  19. return FLASH_Sector_2;
  20. else if (addr < ADDR_FLASH_SECTOR_4)
  21. return FLASH_Sector_3;
  22. else if (addr < ADDR_FLASH_SECTOR_5)
  23. return FLASH_Sector_4;
  24. else if (addr < ADDR_FLASH_SECTOR_6)
  25. return FLASH_Sector_5;
  26. else if (addr < ADDR_FLASH_SECTOR_7)
  27. return FLASH_Sector_6;
  28. else if (addr < ADDR_FLASH_SECTOR_8)
  29. return FLASH_Sector_7;
  30. else if (addr < ADDR_FLASH_SECTOR_9)
  31. return FLASH_Sector_8;
  32. else if (addr < ADDR_FLASH_SECTOR_10)
  33. return FLASH_Sector_9;
  34. else if (addr < ADDR_FLASH_SECTOR_11)
  35. return FLASH_Sector_10;
  36. return FLASH_Sector_11;
  37. }
  38. u8 iap_init(void)
  39. {
  40. FLASH_Status status = FLASH_COMPLETE;
  41. FLASH_Unlock();
  42. FLASH_DataCacheCmd(DISABLE);
  43. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  44. status = FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3);
  45. if (status != FLASH_COMPLETE)
  46. {
  47. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  48. FLASH_Lock(); // 上锁
  49. return 1;
  50. }
  51. status = FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3);
  52. if (status != FLASH_COMPLETE)
  53. {
  54. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  55. FLASH_Lock(); // 上锁
  56. return 1;
  57. }
  58. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  59. FLASH_Lock(); // 上锁
  60. return 0;
  61. }
  62. u8 STMFLASH_ReadByte(u32 faddr)
  63. {
  64. return *(vu8 *)faddr;
  65. }
  66. u16 STMFLASH_ReadHalfWord(u32 faddr)
  67. {
  68. return *(vu16 *)faddr;
  69. }
  70. u32 STMFLASH_ReadWord(u32 faddr)
  71. {
  72. return *(vu32 *)faddr;
  73. }
  74. void copy_to_runaddr(u32 end_addr)
  75. {
  76. FLASH_Status status = FLASH_COMPLETE;
  77. u32 write_addr1 = IAP_RUN_START_SECTOR, read_addr = IAP_DOWN_BEGIN_ADDR;
  78. // u8 erase_sector = 0, index = 0;
  79. u32 temp_value = 0;
  80. // erase_sector = STMFLASH_GetFlashSector(IAP_RUN_START_SECTOR);
  81. FLASH_Unlock(); // 解锁
  82. FLASH_DataCacheCmd(DISABLE); // FLASH擦除期间,必须禁止数据缓存
  83. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  84. status = FLASH_EraseSector(FLASH_Sector_4, VoltageRange_3);
  85. if (status != FLASH_COMPLETE)
  86. {
  87. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  88. FLASH_Lock(); // 上锁
  89. return;
  90. }
  91. status = FLASH_EraseSector(FLASH_Sector_5, VoltageRange_3);
  92. if (status != FLASH_COMPLETE)
  93. {
  94. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  95. FLASH_Lock(); // 上锁
  96. return;
  97. }
  98. while (end_addr > read_addr)
  99. {
  100. temp_value = STMFLASH_ReadWord(read_addr);
  101. if (FLASH_ProgramWord(write_addr1, temp_value) != FLASH_COMPLETE) // 写入数据
  102. {
  103. break; // 写入异常
  104. }
  105. write_addr1 += 4;
  106. read_addr += 4;
  107. }
  108. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  109. FLASH_Lock(); // 上锁
  110. }
  111. void iap_send_dataresp(u8 res, u32 addr, u32 sum)
  112. {
  113. u8 send_buf[8] = {0};
  114. send_buf[0] = IAP_CMD_DOWNLOAD;
  115. send_buf[1] = addr;
  116. send_buf[2] = addr >> 8;
  117. send_buf[3] = addr >> 16;
  118. send_buf[4] = res;
  119. send_buf[5] = addr >> 24;
  120. /*send_buf[1] = addr >> 16;
  121. send_buf[2] = addr >> 8;
  122. send_buf[3] = addr;
  123. send_buf[4] = res;
  124. send_buf[5] = 0xFF;*/
  125. send_buf[6] = (u8)(sum >> 8);
  126. send_buf[7] = (u8)sum;
  127. CAN2_Send_Msg(send_buf, DLC_8, IAP_MCU_DATA_FRAME);
  128. }
  129. void iap_send_datafinish(u16 page_num)
  130. {
  131. u8 send_buf[8] = {0};
  132. send_buf[0] = IAP_CMD_DOWNLOAD_END;
  133. send_buf[1] = (u8)page_num;
  134. send_buf[2] = (u8)(page_num >> 8);
  135. send_buf[3] = 0xFF;
  136. send_buf[4] = 0xFF;
  137. send_buf[5] = 0xFF;
  138. send_buf[6] = 0xFF;
  139. send_buf[7] = 0xFF;
  140. CAN2_Send_Msg(send_buf, DLC_8, IAP_MCU_DATA_FRAME);
  141. }
  142. u8 stm_flash_write(u32 addr, u16 *array, u32 len)
  143. {
  144. u8 res = TRUE;
  145. u32 index = 0, temp_addr = addr;
  146. FLASH_Status status = FLASH_COMPLETE;
  147. FLASH_Unlock();
  148. FLASH_DataCacheCmd(DISABLE);
  149. for (index = 0; index < len;)
  150. {
  151. if (STMFLASH_ReadHalfWord(temp_addr) != 0xFFFF)
  152. {
  153. status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
  154. if (status != FLASH_COMPLETE)
  155. {
  156. res = FALSE;
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. index++;
  163. temp_addr += 2;
  164. }
  165. }
  166. if (status == FLASH_COMPLETE)
  167. {
  168. for (index = 0; index < len; index++)
  169. {
  170. if (FLASH_ProgramHalfWord(addr, array[index]) != FLASH_COMPLETE) // 写入数据
  171. {
  172. res = FALSE;
  173. break; // 写入异常
  174. }
  175. addr += 2;
  176. // array++;
  177. }
  178. }
  179. FLASH_DataCacheCmd(ENABLE);
  180. FLASH_Lock();
  181. return res;
  182. }
  183. void stm_flash_write_word(u32 addr, u32 value)
  184. {
  185. u32 temp_addr = addr;
  186. FLASH_Status status = FLASH_COMPLETE;
  187. FLASH_Unlock();
  188. FLASH_DataCacheCmd(DISABLE);
  189. if (STMFLASH_ReadWord(temp_addr) != 0xFFFFFFFF)
  190. {
  191. status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
  192. if (status != FLASH_COMPLETE)
  193. {
  194. return;
  195. }
  196. }
  197. if (status == FLASH_COMPLETE)
  198. {
  199. if (FLASH_ProgramWord(addr, value) != FLASH_COMPLETE) // 写入数据
  200. {
  201. return;
  202. }
  203. }
  204. FLASH_DataCacheCmd(ENABLE);
  205. FLASH_Lock();
  206. }
  207. void stm_flash_write_byte(u32 addr, u8 value)
  208. {
  209. u32 temp_addr = addr;
  210. FLASH_Status status = FLASH_COMPLETE;
  211. FLASH_Unlock();
  212. FLASH_DataCacheCmd(DISABLE);
  213. if (STMFLASH_ReadByte(temp_addr) != 0xFF)
  214. {
  215. status = FLASH_EraseSector(STMFLASH_GetFlashSector(temp_addr), VoltageRange_3);
  216. if (status != FLASH_COMPLETE)
  217. {
  218. return;
  219. }
  220. }
  221. if (status == FLASH_COMPLETE)
  222. {
  223. if (FLASH_ProgramByte(addr, value) != FLASH_COMPLETE) // 写入数据
  224. {
  225. return;
  226. }
  227. }
  228. FLASH_DataCacheCmd(ENABLE);
  229. FLASH_Lock();
  230. }
  231. void iap_process(CanData_TypeDef *rx_msg)
  232. {
  233. // 握手帧回复,最后一个字节为前7个字节累加和
  234. u8 handle_buf[DLC_8] = {'S', 'U', 'C', 'C', 'E', 'S', 'S', 0x19};
  235. u8 pc_frame[DLC_8] = {'A', 'P', 'P', 'L', 'O', 'A', 'D', 0x01};
  236. u8 index = 0, pc_cmd = 0, pack_num = 0, res = 0;
  237. u16 rec_page_num = 0;
  238. u32 temp_addr = 0;
  239. // FLASH_Status flash_res = FLASH_COMPLETE;
  240. static u16 cnt = 0;
  241. static u16 total_page_num = 0;
  242. static u32 iap_sum = 0;
  243. static u32 pack_receive = 0;
  244. static u32 write_addr = 0, old_write_addr = 0;
  245. // static u32 write_test = IAP_DOWN_BEGIN_ADDR;
  246. if (rx_msg->can_id == IAP_PC_BEGINE_FRAME)
  247. {
  248. cnt = 0;
  249. if (memcmp(rx_msg->buf, pc_frame, 7) == 0)
  250. {
  251. // res = iap_init();
  252. total_page_num = 0;
  253. if (res == 0)
  254. {
  255. InitQueue(&CanQueueImportant);
  256. CAN2_Send_Msg(handle_buf, DLC_8, IAP_MCU_BIGEN_FRAME);
  257. write_addr = IAP_RUN_START_SECTOR; // IAP_DOWN_BEGIN_ADDR;
  258. }
  259. else
  260. {
  261. return;
  262. }
  263. }
  264. }
  265. else if (rx_msg->can_id == IAP_PC_DATA_FRAME)
  266. {
  267. cnt = 0;
  268. pc_cmd = rx_msg->buf[0];
  269. switch (pc_cmd)
  270. {
  271. case IAP_CMD_DOWNLOAD:
  272. pack_num = rx_msg->buf[1];
  273. if (pack_num == 0x01)
  274. {
  275. iap_sum = 0;
  276. iap_index = 0;
  277. pack_receive = 0;
  278. pack_receive |= (1 << (pack_num - 1));
  279. write_addr = (rx_msg->buf[6] << 24) + (rx_msg->buf[5] << 16) + (rx_msg->buf[4] << 8) + rx_msg->buf[3];
  280. }
  281. else if ((pack_num > 0x01) && (pack_num < 0x17))
  282. {
  283. if (((pack_receive & 0x00000001) != 0x00000001) || ((pack_receive & (1 << (pack_num - 1))) == (1 << (pack_num - 1))))
  284. {
  285. break;
  286. }
  287. iap_index = (pack_num - 2) * 3;
  288. for (index = 0; index < 3; index++)
  289. {
  290. if (iap_index < (IAP_BUF_SIZE - 1))
  291. {
  292. pack_receive |= 1 << (pack_num - 1);
  293. iap_buf[iap_index] = ((u16)rx_msg->buf[2 * index + 3] << 8) + rx_msg->buf[2 * index + 2];
  294. iap_sum += iap_buf[iap_index];
  295. iap_index++;
  296. }
  297. }
  298. }
  299. else if (pack_num == 0x17)
  300. {
  301. if (((pack_receive & 0x00000001) != 0x00000001) || ((pack_receive & (1 << (pack_num - 1))) == (1 << (pack_num - 1))))
  302. {
  303. break;
  304. }
  305. iap_index = 63;
  306. pack_receive |= 1 << (pack_num - 1);
  307. iap_buf[iap_index] = ((u16)rx_msg->buf[3] << 8) + rx_msg->buf[2];
  308. iap_sum += iap_buf[iap_index];
  309. }
  310. if ((pack_receive & 0x007FFFFF) == 0x007FFFFF)
  311. {
  312. // stm_flash_write(write_test, iap_buf, iap_index+1);
  313. // write_test += 2 * (iap_index+1);
  314. temp_addr = write_addr;
  315. if ((write_addr >= ADDR_FLASH_SECTOR_4) && (write_addr <= ADDR_FLASH_SECTOR_END) && (stm_flash_write(temp_addr, iap_buf, IAP_BUF_SIZE) == TRUE))
  316. {
  317. iap_send_dataresp(MCU_DOWNLOAD_SUCC, write_addr, iap_sum);
  318. if (write_addr != old_write_addr)
  319. {
  320. total_page_num++;
  321. old_write_addr = write_addr;
  322. }
  323. }
  324. else
  325. {
  326. iap_send_dataresp(MCU_DOWNLOAD_FAIL, write_addr, iap_sum);
  327. }
  328. iap_sum = 0;
  329. iap_index = 0;
  330. pack_receive = 0;
  331. }
  332. break;
  333. case IAP_CMD_DOWNLOAD_END:
  334. rec_page_num = rx_msg->buf[2];
  335. rec_page_num = (rec_page_num << 8) | rx_msg->buf[1];
  336. if (rec_page_num != total_page_num)
  337. {
  338. break;
  339. }
  340. iap_send_datafinish(total_page_num);
  341. // copy_to_runaddr(write_addr-2);
  342. // copy_to_runaddr(write_test-2);
  343. write_addr = IAP_RUN_START_SECTOR; // IAP_DOWN_BEGIN_ADDR;
  344. total_page_num = 0;
  345. bsp_DelayMS(300);
  346. boot_goto_app(APP_ADDRESS);
  347. break;
  348. }
  349. }
  350. else
  351. {
  352. cnt++;
  353. bsp_DelayMS(10);
  354. if (cnt > 50)
  355. {
  356. boot_goto_app(APP_ADDRESS);
  357. }
  358. }
  359. }
  360. u8 flash_up_is_ok(void)
  361. {
  362. u32 len = 0;
  363. len = STMFLASH_ReadWord(IAP_FLASH_UP_LEN);
  364. if ((len > 0) && (len < 455680)) // 445K
  365. {
  366. return TRUE;
  367. }
  368. else
  369. {
  370. return FALSE;
  371. }
  372. }
  373. void Iap_from_Flash(void)
  374. {
  375. u32 value = 0, read_add = IAP_FLASH_UP_ADDR, write_add = IAP_RUN_START_SECTOR; // IAP_DOWN_BEGIN_ADDR;
  376. u32 len = 0;
  377. len = STMFLASH_ReadWord(IAP_FLASH_UP_LEN);
  378. while (len >= 4)
  379. {
  380. value = STMFLASH_ReadWord(read_add);
  381. stm_flash_write_word(write_add, value);
  382. read_add += 4;
  383. write_add += 4;
  384. len -= 4;
  385. }
  386. // if(len != 0)
  387. // {
  388. // value = STMFLASH_ReadWord(read_add);
  389. // stm_flash_write_word(write_add, value);
  390. // }
  391. for (; len > 0; len--)
  392. {
  393. value = STMFLASH_ReadByte(read_add);
  394. stm_flash_write_byte(write_add, (u8)value);
  395. write_add++;
  396. }
  397. FLASH_Unlock();
  398. // FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR
  399. // | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  400. FLASH_DataCacheCmd(DISABLE);
  401. FLASH_EraseSector(FLASH_Sector_8, VoltageRange_3);
  402. FLASH_EraseSector(FLASH_Sector_9, VoltageRange_3);
  403. FLASH_EraseSector(FLASH_Sector_10, VoltageRange_3);
  404. FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
  405. FLASH_DataCacheCmd(ENABLE);
  406. FLASH_Lock();
  407. }