iap.c 11 KB

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