bl_main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. extern const uint32_t __isr_start_address;
  23. extern const uint32_t __os_heap_start;
  24. extern const uint32_t __ram_end;
  25. uint32_t SystemCoreClock;
  26. #define FW_UPGRADE_START_TIME 300
  27. #define FW_UPGRADE_ALL_TIME 5000
  28. #define FW_UPGRADE_DATA_TIME 50
  29. #define FW_OTA_FLASH_BUF_LEN 4096
  30. enum
  31. {
  32. FW_UPGRADE_STATE_IDLE,
  33. FW_UPGRADE_STATE_START,
  34. FW_UPGRADE_STATE_RUN,
  35. FW_UPGRADE_STATE_WAIT_END,
  36. };
  37. typedef struct
  38. {
  39. Buffer_Struct FWDataBuffer;
  40. uint32_t FWStartAddress;
  41. uint32_t FWTotalLen;
  42. uint32_t FWCRC32;
  43. uint32_t NextAddress;
  44. uint32_t XferLen;
  45. uint8_t *CurFWProgramData;
  46. uint32_t CurSPIFlashStart;
  47. uint32_t NextSPIFlashStart;
  48. uint8_t *CurSPIFlashData;
  49. uint8_t *NextSPIFlashData;
  50. uint8_t State;
  51. uint8_t ForceOut;
  52. uint8_t SPIFlashReadDone;
  53. }BL_CtrlStuct;
  54. #define BL_DBG DBG_INFO
  55. //#define BL_DBG(X, Y...)
  56. typedef void (*pFunction)(void);
  57. static uint32_t CRC32_Table[256];
  58. static BL_CtrlStuct prvBL;
  59. static SPIFlash_CtrlStruct prvSPIFlash;
  60. void Jump_AppRun(uint32_t Address)
  61. {
  62. /* Jump to user application */
  63. pFunction Jump_To_Application;
  64. uint32_t JumpAddress;
  65. __disable_irq();
  66. DBG_INFO("jump to 0x%x !", Address);
  67. JumpAddress = *(__IO uint32_t*) (Address + 4);
  68. Jump_To_Application = (pFunction) JumpAddress;
  69. /* Initialize user application's Stack Pointer */
  70. __set_MSP(*(__IO uint32_t*) Address);
  71. /* Jump to application */
  72. Jump_To_Application();
  73. }
  74. uint8_t BL_CheckFlashPage(uint32_t Page, uint32_t Address)
  75. {
  76. uint32_t EndAddress = Page * __FLASH_SECTOR_SIZE__ ;
  77. if (Address >= EndAddress)
  78. {
  79. return 1;
  80. }
  81. else
  82. {
  83. return 0;
  84. }
  85. }
  86. uint32_t BL_GetFlashPage(uint32_t Address)
  87. {
  88. return Address/__FLASH_SECTOR_SIZE__;
  89. }
  90. void BL_UnlockFlash(void)
  91. {
  92. }
  93. void BL_LockFlash(void)
  94. {
  95. }
  96. void BL_EraseSector(uint32_t address)
  97. {
  98. Flash_EraseSector(address, 0);
  99. // BL_DBG("%x", address);
  100. // FLASH_EraseSector(address);
  101. // CACHE_CleanAll(CACHE);
  102. }
  103. static void BL_OTAErase(uint32_t Address, uint32_t Len)
  104. {
  105. uint32_t Pos = 0;
  106. while(Pos < Len)
  107. {
  108. Flash_EraseSector(Address + Pos, 0);
  109. Pos += __FLASH_SECTOR_SIZE__;
  110. }
  111. }
  112. static void BL_OTAWrite(uint32_t Address, uint8_t *Data, uint32_t Len)
  113. {
  114. uint32_t Pos = 0;
  115. while(Pos < Len)
  116. {
  117. if ((Len - Pos) > __FLASH_PAGE_SIZE__)
  118. {
  119. Flash_ProgramData(Address + Pos, Data + Pos, __FLASH_PAGE_SIZE__, 0);
  120. Pos += __FLASH_PAGE_SIZE__;
  121. }
  122. else
  123. {
  124. Flash_ProgramData(Address + Pos, Data + Pos, Len - Pos, 0);
  125. Pos += Len - Pos;
  126. }
  127. }
  128. }
  129. void FileSystem_Init(void)
  130. {
  131. }
  132. int32_t BL_StartNewDownload(uint32_t Address, uint32_t TotalLen, uint32_t CRC32)
  133. {
  134. if (prvBL.State != FW_UPGRADE_STATE_IDLE)
  135. {
  136. return -1;
  137. }
  138. prvBL.FWStartAddress = Address;
  139. prvBL.FWTotalLen = TotalLen;
  140. prvBL.FWCRC32 = CRC32;
  141. prvBL.State = FW_UPGRADE_STATE_START;
  142. prvBL.NextAddress = Address;
  143. prvBL.XferLen = 0;
  144. return 0;
  145. }
  146. int BL_DownloadAddData(uint32_t PacketSn, uint8_t *Data, uint32_t Len, uint32_t *NextPacketSn)
  147. {
  148. uint32_t RestLen = prvBL.FWDataBuffer.MaxLen - prvBL.FWDataBuffer.Pos;
  149. if (prvBL.NextAddress != PacketSn)
  150. {
  151. *NextPacketSn = prvBL.NextAddress;
  152. return -1;
  153. }
  154. else
  155. {
  156. prvBL.XferLen += Len;
  157. prvBL.NextAddress += Len;
  158. *NextPacketSn = prvBL.NextAddress;
  159. if (Len < RestLen)
  160. {
  161. Buffer_StaticWrite(&prvBL.FWDataBuffer, Data, Len);
  162. }
  163. else
  164. {
  165. Buffer_StaticWrite(&prvBL.FWDataBuffer, Data, RestLen);
  166. Len -= RestLen;
  167. prvBL.CurFWProgramData = prvBL.FWDataBuffer.Data;
  168. OS_InitBuffer(&prvBL.FWDataBuffer, SPI_FLASH_BLOCK_SIZE);
  169. Buffer_StaticWrite(&prvBL.FWDataBuffer, Data + RestLen, Len);
  170. }
  171. if (prvBL.CurFWProgramData && (prvBL.FWDataBuffer.Pos > 60000))
  172. {
  173. return prvBL.FWDataBuffer.Pos;
  174. }
  175. else
  176. {
  177. return 1;
  178. }
  179. }
  180. }
  181. uint8_t BL_DownloadEnd(void)
  182. {
  183. if (prvBL.State != FW_UPGRADE_STATE_RUN)
  184. {
  185. return ERROR_OPERATION_FAILED;
  186. }
  187. prvBL.State = FW_UPGRADE_STATE_WAIT_END;
  188. return ERROR_NONE;
  189. }
  190. uint8_t BL_RunAPP(void)
  191. {
  192. prvBL.ForceOut = 1;
  193. }
  194. void Local_Upgrade(void)
  195. {
  196. uint8_t PageData[256];
  197. uint32_t FinishLen = 0;
  198. uint32_t ProgramLen, CRC32, i;
  199. uint64_t AllToTick = GetSysTick() + FW_UPGRADE_START_TIME * CORE_TICK_1MS;
  200. uint64_t DataToTick = GetSysTick() + FW_UPGRADE_DATA_TIME * CORE_TICK_1MS;
  201. uint8_t ConnectCnt = 0;
  202. uint8_t ConnectOK = 0;
  203. DBG_Response(DBG_DEVICE_FW_UPGRADE_READY, ERROR_NONE, &ConnectCnt, 1);
  204. while(!prvBL.ForceOut)
  205. {
  206. WDT_Feed();
  207. switch(prvBL.State)
  208. {
  209. case FW_UPGRADE_STATE_IDLE:
  210. if (DataToTick <= GetSysTick())
  211. {
  212. ConnectCnt++;
  213. DataToTick = GetSysTick() + FW_UPGRADE_DATA_TIME * CORE_TICK_1MS;
  214. DBG_Response(DBG_DEVICE_FW_UPGRADE_READY, ERROR_NONE, &ConnectCnt, 1);
  215. }
  216. break;
  217. case FW_UPGRADE_STATE_START:
  218. AllToTick = GetSysTick() + FW_UPGRADE_ALL_TIME * CORE_TICK_1MS;
  219. prvBL.State = FW_UPGRADE_STATE_RUN;
  220. prvBL.XferLen = 0;
  221. FinishLen = 0;
  222. ConnectOK = 1;
  223. break;
  224. case FW_UPGRADE_STATE_RUN:
  225. case FW_UPGRADE_STATE_WAIT_END:
  226. if (prvBL.XferLen < prvBL.FWTotalLen)
  227. {
  228. if (prvBL.CurFWProgramData)
  229. {
  230. Flash_Erase(prvBL.FWStartAddress + FinishLen, __FLASH_BLOCK_SIZE__);
  231. Flash_Program(prvBL.FWStartAddress + FinishLen, prvBL.CurFWProgramData, __FLASH_BLOCK_SIZE__);
  232. free(prvBL.CurFWProgramData);
  233. prvBL.CurFWProgramData = NULL;
  234. FinishLen += __FLASH_BLOCK_SIZE__;
  235. DataToTick = GetSysTick() + FW_UPGRADE_DATA_TIME * CORE_TICK_1MS;
  236. AllToTick = GetSysTick() + FW_UPGRADE_ALL_TIME * CORE_TICK_1MS;
  237. }
  238. }
  239. else
  240. {
  241. if (prvBL.CurFWProgramData)
  242. {
  243. Flash_Erase(prvBL.FWStartAddress + FinishLen, __FLASH_BLOCK_SIZE__);
  244. Flash_Program(prvBL.FWStartAddress + FinishLen, prvBL.CurFWProgramData, __FLASH_BLOCK_SIZE__);
  245. free(prvBL.CurFWProgramData);
  246. prvBL.CurFWProgramData = NULL;
  247. FinishLen += __FLASH_BLOCK_SIZE__;
  248. }
  249. if (prvBL.FWDataBuffer.Pos)
  250. {
  251. Flash_Erase(prvBL.FWStartAddress + FinishLen, __FLASH_BLOCK_SIZE__);
  252. Flash_Program(prvBL.FWStartAddress + FinishLen, prvBL.FWDataBuffer.Data, prvBL.FWDataBuffer.Pos);
  253. FinishLen += prvBL.FWDataBuffer.Pos;
  254. prvBL.FWDataBuffer.Pos = 0;
  255. }
  256. CACHE_CleanAll(CACHE);
  257. CRC32 = CRC32_Cal(CRC32_Table, prvBL.FWStartAddress, prvBL.FWTotalLen, CRC32_START);
  258. if (CRC32 != prvBL.FWCRC32)
  259. {
  260. DBG_Response(DBG_DEVICE_FW_UPGRADE_RESULT, ERROR_PROTOCL, &CRC32, 4);
  261. }
  262. else
  263. {
  264. DBG_Response(DBG_DEVICE_FW_UPGRADE_RESULT, ERROR_NONE, &CRC32, 4);
  265. }
  266. AllToTick = GetSysTick() + FW_UPGRADE_START_TIME * CORE_TICK_1MS;
  267. prvBL.State = FW_UPGRADE_STATE_IDLE;
  268. DataToTick = GetSysTick() + FW_UPGRADE_DATA_TIME * CORE_TICK_1MS;
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. if (AllToTick <= GetSysTick())
  275. {
  276. prvBL.ForceOut = 1;
  277. DBG_Response(DBG_DEVICE_FW_UPGRADE_RESULT, ERROR_TIMEOUT, &ConnectCnt, 1);
  278. }
  279. }
  280. if (ConnectOK)
  281. {
  282. BL_EraseSector(__FLASH_OTA_INFO_ADDR__);
  283. }
  284. }
  285. static int32_t BL_OTAReadDataInFlash(void *pData, void *pParam)
  286. {
  287. Buffer_Struct *pBuffer = (Buffer_Struct *)pParam;
  288. memcpy(pBuffer->Data, pData, pBuffer->Pos);
  289. return 0;
  290. }
  291. static int32_t BL_OTAReadDataInSpiFlash(void *pData, void *pParam)
  292. {
  293. uint32_t StartAddress = (uint32_t)pData;
  294. Buffer_Struct *pBuffer = (Buffer_Struct *)pParam;
  295. // DBG_INFO("%x,%x,%x,%x,%x", StartAddress, prvBL.CurSPIFlashStart, prvBL.NextSPIFlashStart, prvBL.CurSPIFlashData, prvBL.NextSPIFlashData);
  296. if (prvBL.CurSPIFlashData)
  297. {
  298. if (StartAddress >= prvBL.NextSPIFlashStart)
  299. {
  300. if (!prvBL.SPIFlashReadDone)
  301. {
  302. while(!SPIFlash_WaitOpDone(&prvSPIFlash))
  303. {
  304. ;
  305. }
  306. prvBL.SPIFlashReadDone = 1;
  307. }
  308. free(prvBL.CurSPIFlashData);
  309. prvBL.CurSPIFlashStart = prvBL.NextSPIFlashStart;
  310. prvBL.CurSPIFlashData = prvBL.NextSPIFlashData;
  311. prvBL.NextSPIFlashData = malloc(FW_OTA_FLASH_BUF_LEN);
  312. prvBL.NextSPIFlashStart += FW_OTA_FLASH_BUF_LEN;
  313. SPIFlash_Read(&prvSPIFlash, prvBL.NextSPIFlashStart, prvBL.NextSPIFlashData, FW_OTA_FLASH_BUF_LEN, 1);
  314. prvBL.SPIFlashReadDone = 0;
  315. }
  316. }
  317. else
  318. {
  319. prvBL.CurSPIFlashData = malloc(FW_OTA_FLASH_BUF_LEN);
  320. prvBL.NextSPIFlashData = malloc(FW_OTA_FLASH_BUF_LEN);
  321. SPIFlash_Read(&prvSPIFlash, prvBL.CurSPIFlashStart, prvBL.CurSPIFlashData, FW_OTA_FLASH_BUF_LEN, 1);
  322. while(!SPIFlash_WaitOpDone(&prvSPIFlash))
  323. {
  324. ;
  325. }
  326. SPIFlash_Read(&prvSPIFlash, prvBL.NextSPIFlashStart, prvBL.NextSPIFlashData, FW_OTA_FLASH_BUF_LEN, 1);
  327. prvBL.SPIFlashReadDone = 0;
  328. }
  329. memcpy(pBuffer->Data, prvBL.CurSPIFlashData, pBuffer->Pos);
  330. return 0;
  331. }
  332. static int32_t BL_OTAReadDataInFS(void *pData, void *pParam)
  333. {
  334. Buffer_Struct *pBuffer = (Buffer_Struct *)pParam;
  335. }
  336. static void BL_SpiInit(uint8_t SpiID, uint8_t *Pin)
  337. {
  338. switch(SpiID)
  339. {
  340. case HSPI_ID0:
  341. GPIO_Iomux(GPIOC_12,3);
  342. GPIO_Iomux(GPIOC_13,3);
  343. GPIO_Iomux(GPIOC_15,3);
  344. SYSCTRL->CG_CTRL1 |= SYSCTRL_APBPeriph_HSPI;
  345. break;
  346. case SPI_ID0:
  347. switch(Pin[0])
  348. {
  349. case GPIOB_14:
  350. GPIO_Iomux(GPIOB_14,0);
  351. break;
  352. case GPIOC_14:
  353. GPIO_Iomux(GPIOC_14,2);
  354. break;
  355. }
  356. switch(Pin[1])
  357. {
  358. case GPIOB_15:
  359. GPIO_Iomux(GPIOB_15,0);
  360. break;
  361. case GPIOC_12:
  362. GPIO_Iomux(GPIOC_12,2);
  363. break;
  364. }
  365. switch(Pin[2])
  366. {
  367. case GPIOB_12:
  368. GPIO_Iomux(GPIOB_12,0);
  369. break;
  370. case GPIOC_12:
  371. GPIO_Iomux(GPIOC_12,2);
  372. break;
  373. }
  374. SYSCTRL->CG_CTRL1 |= SYSCTRL_APBPeriph_SPI0;
  375. break;
  376. case SPI_ID1:
  377. GPIO_Iomux(GPIOA_06,3);
  378. GPIO_Iomux(GPIOA_08,3);
  379. GPIO_Iomux(GPIOA_09,3);
  380. SYSCTRL->CG_CTRL1 |= SYSCTRL_APBPeriph_SPI1;
  381. break;
  382. case SPI_ID2:
  383. GPIO_Iomux(GPIOB_02,0);
  384. GPIO_Iomux(GPIOB_04,0);
  385. GPIO_Iomux(GPIOB_05,0);
  386. SYSCTRL->CG_CTRL1 |= SYSCTRL_APBPeriph_SPI2;
  387. break;
  388. }
  389. }
  390. void Remote_Upgrade(void)
  391. {
  392. CoreUpgrade_HeadStruct Head;
  393. Buffer_Struct ReadBuffer;
  394. CBFuncEx_t pReadFunc;
  395. PV_Union uPV, uPin;
  396. uint32_t Check;
  397. uint32_t DoneLen;
  398. int32_t Result;
  399. uint8_t Reboot = 0;
  400. OS_InitBuffer(&ReadBuffer, FW_OTA_FLASH_BUF_LEN);
  401. memcpy(&Head, __FLASH_OTA_INFO_ADDR__, sizeof(CoreUpgrade_HeadStruct));
  402. Check = CRC32_Cal(CRC32_Table, &Head.Param1, sizeof(Head) - 8, 0xffffffff);
  403. if (Head.MaigcNum != __APP_START_MAGIC__)
  404. {
  405. DBG_INFO("no ota info");
  406. return;
  407. }
  408. if (Check != Head.CRC32)
  409. {
  410. DBG_INFO("ota info error");
  411. return;
  412. }
  413. uPV.u32 = Head.Param1;
  414. switch(uPV.u8[1])
  415. {
  416. case CORE_OTA_IN_FLASH:
  417. Check = CRC32_Cal(CRC32_Table, Head.DataStartAddress, Head.DataLen, 0xffffffff);
  418. if (Check != Head.DataCRC32)
  419. {
  420. DBG_INFO("ota file CRC32: %x,%x", Check, Head.DataCRC32);
  421. goto OTA_END;
  422. }
  423. pReadFunc = BL_OTAReadDataInFlash;
  424. break;
  425. case CORE_OTA_OUT_SPI_FLASH:
  426. memset(&prvSPIFlash, 0, sizeof(prvSPIFlash));
  427. prvSPIFlash.SpiID = uPV.u8[2];
  428. uPin.u32 = Head.Param2;
  429. BL_SpiInit(prvSPIFlash.SpiID, uPin.u8);
  430. prvSPIFlash.CSPin = uPin.u8[3];
  431. GPIO_Config(prvSPIFlash.CSPin, 0, 1);
  432. SPI_MasterInit(prvSPIFlash.SpiID, 8, SPI_MODE_0, 24000000, NULL, NULL);
  433. prvSPIFlash.IsBlockMode = 1;
  434. prvSPIFlash.IsSpiDMAMode = 1;
  435. SPI_DMATxInit(prvSPIFlash.SpiID, FLASH_SPI_TX_DMA_STREAM, 0);
  436. SPI_DMARxInit(prvSPIFlash.SpiID, FLASH_SPI_RX_DMA_STREAM, 0);
  437. SPIFlash_Init(&prvSPIFlash, NULL);
  438. prvBL.CurSPIFlashStart = Head.DataStartAddress;
  439. prvBL.NextSPIFlashStart = Head.DataStartAddress + FW_OTA_FLASH_BUF_LEN;
  440. prvBL.CurSPIFlashData = NULL;
  441. prvBL.NextSPIFlashData = NULL;
  442. pReadFunc = BL_OTAReadDataInSpiFlash;
  443. DoneLen = 0;
  444. Check = 0xffffffff;
  445. while(DoneLen < Head.DataLen)
  446. {
  447. if ((Head.DataLen - DoneLen) < SPI_FLASH_BLOCK_SIZE)
  448. {
  449. SPIFlash_Read(&prvSPIFlash, Head.DataStartAddress + DoneLen, prvBL.FWDataBuffer.Data, (Head.DataLen - DoneLen), 0);
  450. Check = CRC32_Cal(CRC32_Table, prvBL.FWDataBuffer.Data, (Head.DataLen - DoneLen), Check);
  451. DoneLen = Head.DataLen;
  452. }
  453. else
  454. {
  455. SPIFlash_Read(&prvSPIFlash, Head.DataStartAddress + DoneLen, prvBL.FWDataBuffer.Data, SPI_FLASH_BLOCK_SIZE, 0);
  456. Check = CRC32_Cal(CRC32_Table, prvBL.FWDataBuffer.Data, SPI_FLASH_BLOCK_SIZE, Check);
  457. DoneLen += SPI_FLASH_BLOCK_SIZE;
  458. }
  459. }
  460. if (Check != Head.DataCRC32)
  461. {
  462. DBG_INFO("ota file CRC32: %x,%x", Check, Head.DataCRC32);
  463. goto OTA_END;
  464. }
  465. break;
  466. default:
  467. DBG_INFO("core ota storage mode %u not support", uPV.u8[1]);
  468. return;
  469. break;
  470. }
  471. switch(uPV.u8[0])
  472. {
  473. case CORE_OTA_MODE_FULL:
  474. goto OTA_FULL;
  475. break;
  476. default:
  477. DBG_INFO("core ota mode %u not support", uPV.u8[0]);
  478. return;
  479. break;
  480. }
  481. goto OTA_END;
  482. OTA_FULL:
  483. DoneLen = 0;
  484. while(DoneLen < Head.DataLen)
  485. {
  486. ReadBuffer.Pos = ((Head.DataLen - DoneLen) > ReadBuffer.MaxLen)?ReadBuffer.MaxLen:(Head.DataLen - DoneLen);
  487. Result = pReadFunc(Head.DataStartAddress + DoneLen, &ReadBuffer);
  488. if (Result < 0)
  489. {
  490. Reboot = 1;
  491. DBG_INFO("core ota read data fail");
  492. goto OTA_END;
  493. }
  494. if (memcmp(__FLASH_APP_START_ADDR__ + DoneLen, ReadBuffer.Data, ReadBuffer.Pos))
  495. {
  496. DBG_INFO("ota %x", __FLASH_APP_START_ADDR__ + DoneLen);
  497. BL_OTAErase(__FLASH_APP_START_ADDR__ + DoneLen, ReadBuffer.Pos);
  498. BL_OTAWrite(__FLASH_APP_START_ADDR__ + DoneLen, ReadBuffer.Data, ReadBuffer.Pos);
  499. CACHE_CleanAll(CACHE);
  500. WDT_Feed();
  501. }
  502. DoneLen += ReadBuffer.Pos;
  503. }
  504. Check = CRC32_Cal(CRC32_Table, __FLASH_APP_START_ADDR__, Head.DataLen, 0xffffffff);
  505. if (Head.DataCRC32 != Check)
  506. {
  507. Reboot = 1;
  508. DBG_INFO("core ota final check crc32 fail %x %x", Check, Head.DataCRC32);
  509. goto OTA_END;
  510. }
  511. goto OTA_END;
  512. OTA_DIFF:
  513. goto OTA_END;
  514. OTA_END:
  515. if (Reboot)
  516. {
  517. NVIC_SystemReset();
  518. while(1){;}
  519. }
  520. else
  521. {
  522. BL_EraseSector(__FLASH_OTA_INFO_ADDR__);
  523. }
  524. }
  525. void SystemInit(void)
  526. {
  527. SYSCTRL->LDO25_CR = (1 << 5);
  528. SCB->VTOR = (uint32_t)(&__isr_start_address);
  529. #ifdef __USE_XTL__
  530. SYSCTRL->FREQ_SEL = 0x20000000 | SYSCTRL_FREQ_SEL_HCLK_DIV_1_2 | (1 << 13) | SYSCTRL_FREQ_SEL_CLOCK_SOURCE_EXT | SYSCTRL_FREQ_SEL_XTAL_192Mhz;
  531. #else
  532. SYSCTRL->FREQ_SEL = 0x20000000 | SYSCTRL_FREQ_SEL_HCLK_DIV_1_2 | (1 << 13) | SYSCTRL_FREQ_SEL_CLOCK_SOURCE_INC | SYSCTRL_FREQ_SEL_XTAL_192Mhz;
  533. #endif
  534. WDT_SetTimeout(__WDT_TO_MS__);
  535. WDT_ModeConfig(WDT_Mode_Interrupt);
  536. WDT_Enable();
  537. // QSPI->DEVICE_PARA = (QSPI->DEVICE_PARA & 0xFFFF) | (68 << 16);
  538. #if (__FPU_PRESENT) && (__FPU_USED == 1)
  539. SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11*2));
  540. #endif
  541. SYSCTRL->CG_CTRL1 = SYSCTRL_APBPeriph_UART0|SYSCTRL_APBPeriph_GPIO|SYSCTRL_APBPeriph_TIMM0;
  542. SYSCTRL->CG_CTRL2 = SYSCTRL_AHBPeriph_DMA|SYSCTRL_AHBPeriph_USB;
  543. SYSCTRL->LOCK_R &= ~SYSCTRL_USB_RESET;
  544. SYSCTRL->SOFT_RST2 |= SYSCTRL_USB_RESET;
  545. QSPI->DEVICE_PARA = (QSPI->DEVICE_PARA & 0x0000fff0) | (16 << 16) |(0x0a);
  546. // QSPI_Init(NULL);
  547. // QSPI_SetLatency(0);
  548. }
  549. void SystemCoreClockUpdate (void) /* Get Core Clock Frequency */
  550. {
  551. SystemCoreClock = HSE_VALUE * (((SYSCTRL->FREQ_SEL & SYSCTRL_FREQ_SEL_XTAL_Mask) >> SYSCTRL_FREQ_SEL_XTAL_Pos) + 1);
  552. }
  553. int main(void)
  554. {
  555. uint32_t AppInfo[4];
  556. uint32_t prvHeapLen;
  557. prvHeapLen = (uint32_t)(&__ram_end) - (uint32_t)(&__os_heap_start);
  558. bpool((uint32_t)(&__os_heap_start), prvHeapLen);
  559. CRC32_CreateTable(CRC32_Table, CRC32_GEN);
  560. __NVIC_SetPriorityGrouping(7 - __NVIC_PRIO_BITS);//对于freeRTOS必须这样配置
  561. SystemCoreClockUpdate();
  562. CoreTick_Init();
  563. cm_backtrace_init(NULL, NULL, NULL);
  564. Uart_GlobalInit();
  565. DMA_GlobalInit();
  566. DBG_Init(0);
  567. FileSystem_Init();
  568. OS_InitBuffer(&prvBL.FWDataBuffer, SPI_FLASH_BLOCK_SIZE);
  569. LOCAL_UPGRADE_START:
  570. Local_Upgrade();
  571. Uart_BaseInit(DBG_UART_ID, DBG_UART_BR, 0, UART_DATA_BIT8, UART_PARITY_NONE, UART_STOP_BIT1, NULL);
  572. #ifndef __RAMRUN__
  573. Remote_Upgrade();
  574. // __disable_irq();
  575. BL_LockFlash();
  576. memcpy(AppInfo, __FLASH_APP_START_ADDR__, sizeof(AppInfo));
  577. if (__APP_START_MAGIC__ == AppInfo[0])
  578. {
  579. #ifdef __DEBUG__
  580. DBG_INFO("bootloader build debug %s %s %x!", __DATE__, __TIME__, QSPI->DEVICE_PARA);
  581. #else
  582. DBG_INFO("bootloader build release %s %s!", __DATE__, __TIME__, QSPI->DEVICE_PARA);
  583. #endif
  584. Jump_AppRun(__FLASH_APP_START_ADDR__ + AppInfo[1]);
  585. }
  586. else
  587. {
  588. goto LOCAL_UPGRADE_START;
  589. }
  590. // i = i / j; //只是测试一下backtrace功能
  591. #endif
  592. NVIC_SystemReset();
  593. }