FlashPrg.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2014 - 2019 ARM Ltd.
  3. *
  4. * This software is provided 'as-is', without any express or implied warranty.
  5. * In no event will the authors be held liable for any damages arising from
  6. * the use of this software. Permission is granted to anyone to use this
  7. * software for any purpose, including commercial applications, and to alter
  8. * it and redistribute it freely, subject to the following restrictions:
  9. *
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software in
  12. * a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. *
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. *
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. *
  20. *
  21. * $Date: 2021-7-1
  22. * $Revision: V1.0.0
  23. *
  24. * Project: Flash Programming Functions for Puya PY32F002Cxx Flash
  25. * --------------------------------------------------------------------------- */
  26. /* History:
  27. * Version 1.0.0
  28. * Initial release
  29. */
  30. #include "FlashOS.h" /* FlashOS Structures */
  31. typedef volatile unsigned char vu8;
  32. typedef unsigned char u8;
  33. typedef volatile unsigned short vu16;
  34. typedef unsigned short u16;
  35. typedef volatile unsigned long vu32;
  36. typedef unsigned long u32;
  37. #define M8(adr) (*((vu8 *)(adr)))
  38. #define M16(adr) (*((vu16 *)(adr)))
  39. #define M32(adr) (*((vu32 *)(adr)))
  40. #define SET_BIT(REG, BIT) ((REG) |= (BIT))
  41. #define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
  42. #define READ_BIT(REG, BIT) ((REG) & (BIT))
  43. #define CLEAR_REG(REG) ((REG) = (0x0))
  44. #define WRITE_REG(REG, VAL) ((REG) = (VAL))
  45. #define READ_REG(REG) ((REG))
  46. #define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
  47. #define FLASH_BASE (0x08000000UL) /*!< FLASH base address */
  48. #define FLASH_END (0x08003FFFUL) /*!< FLASH end address */
  49. #define FLASH_SIZE (FLASH_END - FLASH_BASE + 1)
  50. #define FLASH_PAGE_SIZE 0x00000040U /*!< FLASH Page Size, 64 Bytes */
  51. #define FLASH_PAGE_NB (FLASH_SIZE / FLASH_PAGE_SIZE)
  52. #define FLASH_SECTOR_SIZE 0x00001000U /*!< FLASH Sector Size, 4096 Bytes */
  53. #define FLASH_SECTOR_NB (FLASH_SIZE / FLASH_SECTOR_SIZE)
  54. /* Peripheral Memory Map */
  55. #define OTP_BASE 0x1FFF01C0
  56. #define RCC_BASE 0x40021000
  57. #define OB_BASE 0x1FFF0040 /*!< FLASH Option Bytes base address */
  58. #define IWDG_BASE 0x40003000
  59. #define FLASH_R_BASE 0x40022000
  60. #define FLASHSIZE_BASE (0x1FFF01FCUL) /*!< FLASH Size register base address */
  61. #define RCC ((RCC_TypeDef *)RCC_BASE)
  62. #define IWDG ((IWDG_TypeDef *)IWDG_BASE)
  63. #define FLASH ((FLASH_TypeDef *)FLASH_R_BASE)
  64. #define OB ((OB_TypeDef *) OB_BASE)
  65. typedef struct
  66. {
  67. vu32 CR; /*!< RCC Clock Sources Control Register, Address offset: 0x00 */
  68. vu32 ICSCR; /*!< RCC Internal Clock Sources Calibration Register, Address offset: 0x04 */
  69. } RCC_TypeDef;
  70. /* Independent WATCHDOG */
  71. typedef struct
  72. {
  73. vu32 KR; /*!< IWDG Key register, Address offset: 0x00 */
  74. vu32 PR; /*!< IWDG Prescaler register, Address offset: 0x04 */
  75. vu32 RLR; /*!< IWDG Reload register, Address offset: 0x08 */
  76. vu32 SR; /*!< IWDG Status register, Address offset: 0x0C */
  77. } IWDG_TypeDef;
  78. /* Flash Registers */
  79. typedef struct
  80. {
  81. vu32 RESERVED1[2]; /*!< Reserved1, Address offset: 0x00 */
  82. vu32 KEYR; /*!< FLASH Key register, Address offset: 0x08 */
  83. vu32 OPTKEYR; /*!< FLASH Option Key register, Address offset: 0x0C */
  84. vu32 SR; /*!< FLASH Status register, Address offset: 0x10 */
  85. vu32 CR; /*!< FLASH Control register, Address offset: 0x14 */
  86. vu32 RESERVED2[2]; /*!< Reserved2, Address offset: 0x18-0x1C */
  87. vu32 OPTR; /*!< FLASH Option register, Address offset: 0x20 */
  88. vu32 SDKR; /*!< FLASH SDK address register, Address offset: 0x24 */
  89. vu32 BTCR; /*!< FLASH boot control Address offset: 0x28 */
  90. vu32 WRPR; /*!< FLASH WRP address register, Address offset: 0x2C */
  91. vu32 RESERVED4[(0x90 - 0x2C) / 4 - 1];
  92. vu32 STCR; /*!< FLASH sleep time config register, Address offset: 0x90 */
  93. vu32 RESERVED5[(0x100 - 0x90) / 4 - 1];
  94. vu32 TS0; /*!< FLASH TS0 register, Address offset: 0x100 */
  95. vu32 TS1; /*!< FLASH TS1 register, Address offset: 0x104 */
  96. vu32 TS2P; /*!< FLASH TS2P register, Address offset: 0x108 */
  97. vu32 TPS3; /*!< FLASH TPS3 register, Address offset: 0x10C */
  98. vu32 TS3; /*!< FLASH TS3 register, Address offset: 0x110 */
  99. vu32 PERTPE; /*!< FLASH PERTPE register, Address offset: 0x114 */
  100. vu32 SMERTPE; /*!< FLASH SMERTPE register, Address offset: 0x118 */
  101. vu32 PRGTPE; /*!< FLASH PRGTPE register, Address offset: 0x11C */
  102. vu32 PRETPE; /*!< FLASH PRETPE register, Address offset: 0x120 */
  103. } FLASH_TypeDef;
  104. /****************** FLASH Keys **********************************************/
  105. #define FLASH_KEY1_Pos (0U)
  106. #define FLASH_KEY1_Msk (0x45670123UL << FLASH_KEY1_Pos) /*!< 0x45670123 */
  107. #define FLASH_KEY1 FLASH_KEY1_Msk /*!< Flash program erase key1 */
  108. #define FLASH_KEY2_Pos (0U)
  109. #define FLASH_KEY2_Msk (0xCDEF89ABUL << FLASH_KEY2_Pos) /*!< 0xCDEF89AB */
  110. #define FLASH_KEY2 FLASH_KEY2_Msk /*!< Flash program erase key2: used with FLASH_PEKEY1
  111. to unlock the write access to the FPEC. */
  112. #define FLASH_OPTKEY1_Pos (0U)
  113. #define FLASH_OPTKEY1_Msk (0x08192A3BUL << FLASH_OPTKEY1_Pos) /*!< 0x08192A3B */
  114. #define FLASH_OPTKEY1 FLASH_OPTKEY1_Msk /*!< Flash option key1 */
  115. #define FLASH_OPTKEY2_Pos (0U)
  116. #define FLASH_OPTKEY2_Msk (0x4C5D6E7FUL << FLASH_OPTKEY2_Pos) /*!< 0x4C5D6E7F */
  117. #define FLASH_OPTKEY2 FLASH_OPTKEY2_Msk /*!< Flash option key2: used with FLASH_OPTKEY1 to
  118. unlock the write access to the option byte block */
  119. /* Flash Control Register definitions */
  120. #define FLASH_CR_PG_Pos (0U)
  121. #define FLASH_CR_PG_Msk (0x1UL << FLASH_CR_PG_Pos) /*!< 0x00000001 */
  122. #define FLASH_CR_PG FLASH_CR_PG_Msk
  123. #define FLASH_CR_PER_Pos (1U)
  124. #define FLASH_CR_PER_Msk (0x1UL << FLASH_CR_PER_Pos) /*!< 0x00000002 */
  125. #define FLASH_CR_PER FLASH_CR_PER_Msk
  126. #define FLASH_CR_MER_Pos (2U)
  127. #define FLASH_CR_MER_Msk (0x1UL << FLASH_CR_MER_Pos) /*!< 0x00000004 */
  128. #define FLASH_CR_MER FLASH_CR_MER_Msk
  129. #define FLASH_CR_SER_Pos (11U)
  130. #define FLASH_CR_SER_Msk (0x1UL << FLASH_CR_SER_Pos) /*!< 0x00000800 */
  131. #define FLASH_CR_SER FLASH_CR_SER_Msk
  132. #define FLASH_CR_OPTSTRT_Pos (17U)
  133. #define FLASH_CR_OPTSTRT_Msk (0x1UL << FLASH_CR_OPTSTRT_Pos) /*!< 0x00020000 */
  134. #define FLASH_CR_OPTSTRT FLASH_CR_OPTSTRT_Msk
  135. #define FLASH_CR_PGSTRT_Pos (19U)
  136. #define FLASH_CR_PGSTRT_Msk (0x1UL << FLASH_CR_PGSTRT_Pos) /*!< 0x00080000 */
  137. #define FLASH_CR_PGSTRT FLASH_CR_PGSTRT_Msk
  138. #define FLASH_CR_EOPIE_Pos (24U)
  139. #define FLASH_CR_EOPIE_Msk (0x1UL << FLASH_CR_EOPIE_Pos) /*!< 0x01000000 */
  140. #define FLASH_CR_EOPIE FLASH_CR_EOPIE_Msk
  141. #define FLASH_CR_ERRIE_Pos (25U)
  142. #define FLASH_CR_ERRIE_Msk (0x1UL << FLASH_CR_ERRIE_Pos) /*!< 0x02000000 */
  143. #define FLASH_CR_ERRIE FLASH_CR_ERRIE_Msk
  144. #define FLASH_CR_OBL_LAUNCH_Pos (27U)
  145. #define FLASH_CR_OBL_LAUNCH_Msk (0x1UL << FLASH_CR_OBL_LAUNCH_Pos) /*!< 0x08000000 */
  146. #define FLASH_CR_OBL_LAUNCH FLASH_CR_OBL_LAUNCH_Msk
  147. #define FLASH_CR_OPTLOCK_Pos (30U)
  148. #define FLASH_CR_OPTLOCK_Msk (0x1UL << FLASH_CR_OPTLOCK_Pos) /*!< 0x40000000 */
  149. #define FLASH_CR_OPTLOCK FLASH_CR_OPTLOCK_Msk
  150. #define FLASH_CR_LOCK_Pos (31U)
  151. #define FLASH_CR_LOCK_Msk (0x1UL << FLASH_CR_LOCK_Pos) /*!< 0x80000000 */
  152. #define FLASH_CR_LOCK FLASH_CR_LOCK_Msk
  153. /* Flash Status Register definitions */
  154. #define FLASH_SR_EOP_Pos (0U)
  155. #define FLASH_SR_EOP_Msk (0x1UL << FLASH_SR_EOP_Pos) /*!< 0x00000001 */
  156. #define FLASH_SR_EOP FLASH_SR_EOP_Msk
  157. #define FLASH_SR_WRPERR_Pos (4U)
  158. #define FLASH_SR_WRPERR_Msk (0x1UL << FLASH_SR_WRPERR_Pos) /*!< 0x00000010 */
  159. #define FLASH_SR_WRPERR FLASH_SR_WRPERR_Msk
  160. #define FLASH_SR_OPTVERR_Pos (15U)
  161. #define FLASH_SR_OPTVERR_Msk (0x1UL << FLASH_SR_OPTVERR_Pos) /*!< 0x00008000 */
  162. #define FLASH_SR_OPTVERR FLASH_SR_OPTVERR_Msk
  163. #define FLASH_SR_BSY_Pos (16U)
  164. #define FLASH_SR_BSY_Msk (0x1UL << FLASH_SR_BSY_Pos) /*!< 0x00010000 */
  165. #define FLASH_SR_BSY FLASH_SR_BSY_Msk
  166. #define FLASH_OPTR_IWDG_SW_Pos (12U)
  167. #define FLASH_OPTR_IWDG_SW_Msk (0x1UL << FLASH_OPTR_IWDG_SW_Pos) /*!< 0x00010000 */
  168. #define FLASH_OPTR_IWDG_SW FLASH_OPTR_IWDG_SW_Msk
  169. #define FLASH_OPTR_WWDG_SW_Pos (13U)
  170. #define FLASH_OPTR_WWDG_SW_Msk (0x1UL << FLASH_OPTR_WWDG_SW_Pos) /*!< 0x00080000 */
  171. #define FLASH_OPTR_WWDG_SW FLASH_OPTR_WWDG_SW_Msk
  172. u32 RCC_ICSCR_HSI_FS_RESTORE;
  173. void InitRccAndFlashParam(void);
  174. void UnInitRccAndFlashParam(void);
  175. int ErasePage(unsigned long adr);
  176. /*
  177. * Initialize Flash Programming Functions
  178. * Parameter: adr: Device Base Address
  179. * clk: Clock Frequency (Hz)
  180. * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  181. * Return Value: 0 - OK, 1 - Failed
  182. */
  183. int Init(unsigned long adr, unsigned long clk, unsigned long fnc)
  184. {
  185. FLASH->KEYR = FLASH_KEY1; /* Unlock Flash */
  186. FLASH->KEYR = FLASH_KEY2;
  187. InitRccAndFlashParam();
  188. #ifdef FLASH_OB
  189. FLASH->OPTKEYR = FLASH_OPTKEY1; /* Unlock Option Bytes */
  190. FLASH->OPTKEYR = FLASH_OPTKEY2;
  191. #endif
  192. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  193. while (FLASH->SR & FLASH_SR_BSY); /* Check FLASH_SR_BSY */
  194. if ((FLASH->OPTR & FLASH_OPTR_IWDG_SW) == 0x00) /* Test if IWDG is running (IWDG in HW mode) */
  195. {
  196. /* Set IWDG time out to ~32.768 second */
  197. IWDG->KR = 0xAAAA; /* Reload IWDG */
  198. IWDG->KR = 0x5555; /* Enable write access to IWDG_PR and IWDG_RLR */
  199. IWDG->PR = 0x06; /* Set prescaler to 256 */
  200. IWDG->RLR = 0xFFF; /* Set reload value to 4095 */
  201. }
  202. return (0);
  203. }
  204. /*
  205. * De-Initialize Flash Programming Functions
  206. * Parameter: fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  207. * Return Value: 0 - OK, 1 - Failed
  208. */
  209. int UnInit(unsigned long fnc)
  210. {
  211. UnInitRccAndFlashParam();
  212. FLASH->CR |= FLASH_CR_LOCK; /* Lock Flash */
  213. #ifdef FLASH_OB
  214. FLASH->CR |= FLASH_CR_OPTLOCK; /* Lock Option Bytes */
  215. #endif
  216. return (0);
  217. }
  218. /*
  219. * Erase complete Flash Memory
  220. * Return Value: 0 - OK, 1 - Failed
  221. */
  222. #ifdef FLASH_MEM
  223. int EraseChip(void)
  224. {
  225. #ifdef FLASH_OTP
  226. ErasePage(OTP_BASE);
  227. #else
  228. FLASH->SR |= FLASH_SR_EOP; // Reset FLASH_EOP
  229. FLASH->CR |= FLASH_CR_MER; // Mass Erase Enabled
  230. FLASH->CR |= FLASH_CR_EOPIE;
  231. M32(0x08000000) = 0xFF;
  232. __asm("DSB");
  233. while (FLASH->SR & FLASH_SR_BSY)
  234. {
  235. IWDG->KR = 0xAAAA; // Reload IWDG
  236. }
  237. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  238. {
  239. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  240. return (1); /* Failed */
  241. }
  242. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  243. FLASH->CR &= ~FLASH_CR_MER; // Mass Erase Disabled
  244. FLASH->CR &= ~FLASH_CR_EOPIE; // Reset FLASH_EOPIE
  245. #endif // FLASH_OTP
  246. return (0); // Done
  247. }
  248. #endif /* FLASH_MEM */
  249. #ifdef FLASH_OB
  250. int EraseChip(void)
  251. {
  252. /* erase chip is not needed for
  253. - Flash Option bytes
  254. - Flash One Time Programmable bytes
  255. */
  256. return (0); /* Done */
  257. }
  258. #endif
  259. /*
  260. * Erase Sector in Flash Memory
  261. * Parameter: adr: Sector Address
  262. * Return Value: 0 - OK, 1 - Failed
  263. */
  264. #ifdef FLASH_MEM
  265. int EraseSector(unsigned long adr)
  266. {
  267. #ifdef FLASH_OTP
  268. ErasePage(OTP_BASE);
  269. #else
  270. FLASH->SR |= FLASH_SR_EOP; // Reset FLASH_EOP
  271. FLASH->CR |= FLASH_CR_SER; // Sector Erase Enabled
  272. FLASH->CR |= FLASH_CR_EOPIE;
  273. M32(adr) = 0xFF; // Sector Address
  274. __asm("DSB");
  275. while (FLASH->SR & FLASH_SR_BSY)
  276. {
  277. IWDG->KR = 0xAAAA; // Reload IWDG
  278. }
  279. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  280. {
  281. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  282. return (1); /* Failed */
  283. }
  284. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  285. FLASH->CR &= ~FLASH_CR_SER; // Sector Erase Disabled
  286. FLASH->CR &= ~FLASH_CR_EOPIE; // Reset FLASH_EOPIE
  287. #endif /* FLASH_OTP */
  288. return (0); // Done
  289. }
  290. #endif /* FLASH_MEM */
  291. #ifdef FLASH_OB
  292. int EraseSector(unsigned long adr)
  293. {
  294. /* erase sector is not needed for
  295. - Flash Option bytes
  296. - Flash One Time Programmable bytes
  297. */
  298. return (0); /* Done */
  299. }
  300. #endif
  301. #ifdef FLASH_MEM
  302. int ErasePage(unsigned long adr)
  303. {
  304. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  305. FLASH->CR |= FLASH_CR_PER; /* page Erase Enabled */
  306. FLASH->CR |= FLASH_CR_EOPIE;
  307. M32(adr) = 0xFF; /* Sector Address */
  308. __asm("DSB");
  309. while (FLASH->SR & FLASH_SR_BSY)
  310. {
  311. IWDG->KR = 0xAAAA; /* Reload IWDG */
  312. }
  313. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  314. {
  315. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  316. return (1); /* Failed */
  317. }
  318. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  319. FLASH->CR &= ~FLASH_CR_PER; /* page Erase Disabled */
  320. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  321. return (0); /* Done */
  322. }
  323. #endif /* FLASH_MEM */
  324. /*
  325. * Program Page in Flash Memory
  326. * Parameter: adr: Page Start Address
  327. * sz: Page Size
  328. * buf: Page Data
  329. * Return Value: 0 - OK, 1 - Failed
  330. */
  331. #ifdef FLASH_MEM
  332. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  333. {
  334. sz = (sz + FLASH_PAGE_SIZE - 1) & ~(FLASH_PAGE_SIZE - 1); // Adjust size for 32 Words
  335. FLASH->SR |= FLASH_SR_EOP; // Reset FLASH_EOP
  336. while (sz)
  337. {
  338. FLASH->CR |= FLASH_CR_PG; // Programming Enabled
  339. FLASH->CR |= FLASH_CR_EOPIE;
  340. for (u8 i = 0; i < FLASH_PAGE_SIZE / 4; i++)
  341. {
  342. M32(adr + i * 4) = *((u32 *)(buf + i * 4)); // Program the first word of the Double Word
  343. if (i == (FLASH_PAGE_SIZE / 4 - 2))
  344. {
  345. FLASH->CR |= FLASH_CR_PGSTRT;
  346. }
  347. }
  348. __asm("DSB");
  349. while (FLASH->SR & FLASH_SR_BSY)
  350. {
  351. IWDG->KR = 0xAAAA; // Reload IWDG
  352. }
  353. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  354. {
  355. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  356. return (1); /* Failed */
  357. }
  358. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  359. FLASH->CR &= ~FLASH_CR_PG; /* Programming Disabled */
  360. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  361. adr += FLASH_PAGE_SIZE; // Go to next Page
  362. buf += FLASH_PAGE_SIZE;
  363. sz -= FLASH_PAGE_SIZE;
  364. }
  365. return (0); // Done
  366. }
  367. #endif /* FLASH_MEM */
  368. #ifdef FLASH_OB
  369. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  370. {
  371. u32 optr;
  372. u32 sdkr;
  373. u32 wrpr;
  374. optr = *((u32 *)(buf + 0x00));
  375. sdkr = *((u32 *)(buf + 0x04));
  376. wrpr = *((u32 *)(buf + 0x0C));
  377. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  378. FLASH->OPTR = (optr & 0x0000FFFF); /* Write OPTR values */
  379. FLASH->SDKR = (sdkr & 0x0000FFFF); /* Write SDKR values */
  380. FLASH->WRPR = (wrpr & 0x0000FFFF); /* Write WRPR values */
  381. FLASH->CR |= FLASH_CR_OPTSTRT;
  382. FLASH->CR |= FLASH_CR_EOPIE;
  383. M32(0x40022080) = 0xFF; /*Address Write Date*/
  384. __asm("DSB");
  385. while (FLASH->SR & FLASH_SR_BSY)
  386. {
  387. IWDG->KR = 0xAAAA; /* Reload IWDG */
  388. }
  389. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  390. {
  391. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  392. return (1); /* Failed */
  393. }
  394. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  395. FLASH->CR &= ~FLASH_CR_OPTSTRT; /* Programming Disabled */
  396. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  397. return (0); /* Done */
  398. }
  399. #endif /* FLASH_OB */
  400. #ifdef FLASH_OB
  401. unsigned long Verify(unsigned long adr, unsigned long sz, unsigned char *buf)
  402. {
  403. u32 optr;
  404. u32 sdkr;
  405. u32 wrpr;
  406. optr = *((u32 *)(buf + 0x00));
  407. sdkr = *((u32 *)(buf + 0x04));
  408. wrpr = *((u32 *)(buf + 0x0C));
  409. if (M32(adr + 0x00) != optr)
  410. {
  411. return (adr + 0x00);
  412. }
  413. if (M32(adr + 0x04) != sdkr)
  414. {
  415. return (adr + 0x04);
  416. }
  417. if (M32(adr + 0x0C) != wrpr)
  418. {
  419. return (adr + 0x0C);
  420. }
  421. return (adr + sz);
  422. }
  423. #endif /* FLASH_OB */
  424. #define HSI_HSICALIBRATION_24MHz ((*(vu32 *)(0x1FFF0088)) & 0xFFFF) /* 24MHz HSI calibration trimming value */
  425. #define HSI_HSICALIBRATION_8MHz ((*(vu32 *)(0x1FFF0080)) & 0xFFFF) /* 8MHz HSI calibration trimming value */
  426. #define RCC_CR_HSIRDY_Pos (10U)
  427. #define RCC_CR_HSIRDY_Msk (0x1UL << RCC_CR_HSIRDY_Pos) /*!< 0x00000400 */
  428. #define RCC_CR_HSIRDY RCC_CR_HSIRDY_Msk /*!< Internal High Speed clock ready flag */
  429. /******************** Bit definition for RCC_ICSCR register ***************/
  430. #define RCC_ICSCR_HSI_ABS_TRIMCR_Pos (0U)
  431. #define RCC_ICSCR_HSI_ABS_TRIMCR_Msk (0xFFFUL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000FFF */
  432. #define RCC_ICSCR_HSI_ABS_TRIMCR RCC_ICSCR_HSI_ABS_TRIMCR_Msk /*!< HSI_ABS_TRIM[11:0] bits */
  433. #define RCC_ICSCR_HSI_ABS_TRIMCR_0 (0x01UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000001 */
  434. #define RCC_ICSCR_HSI_ABS_TRIMCR_1 (0x02UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000002 */
  435. #define RCC_ICSCR_HSI_ABS_TRIMCR_2 (0x04UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000004 */
  436. #define RCC_ICSCR_HSI_ABS_TRIMCR_3 (0x08UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000008 */
  437. #define RCC_ICSCR_HSI_ABS_TRIMCR_4 (0x10UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000010 */
  438. #define RCC_ICSCR_HSI_ABS_TRIMCR_5 (0x20UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000020 */
  439. #define RCC_ICSCR_HSI_ABS_TRIMCR_6 (0x40UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000040 */
  440. #define RCC_ICSCR_HSI_ABS_TRIMCR_7 (0x80UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000080 */
  441. #define RCC_ICSCR_HSI_ABS_TRIMCR_8 (0x100UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000100 */
  442. #define RCC_ICSCR_HSI_ABS_TRIMCR_9 (0x200UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000200 */
  443. #define RCC_ICSCR_HSI_ABS_TRIMCR_10 (0x400UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000400 */
  444. #define RCC_ICSCR_HSI_ABS_TRIMCR_11 (0x800UL << RCC_ICSCR_HSI_ABS_TRIMCR_Pos) /*!< 0x00000800 */
  445. #define RCC_ICSCR_HSI_TC_TRIMCR_Pos (12U)
  446. #define RCC_ICSCR_HSI_TC_TRIMCR_Msk (0xFUL << RCC_ICSCR_HSI_TC_TRIMCR_Pos) /*!< 0x0000F000 */
  447. #define RCC_ICSCR_HSI_TC_TRIMCR RCC_ICSCR_HSI_TC_TRIMCR_Msk /*!< HSI_TC_TRIM[3:0] bits */
  448. #define RCC_ICSCR_HSI_TC_TRIMCR_0 (0x01UL << RCC_ICSCR_HSI_TC_TRIMCR_Pos) /*!< 0x00000001 */
  449. #define RCC_ICSCR_HSI_TC_TRIMCR_1 (0x02UL << RCC_ICSCR_HSI_TC_TRIMCR_Pos) /*!< 0x00000002 */
  450. #define RCC_ICSCR_HSI_TC_TRIMCR_2 (0x04UL << RCC_ICSCR_HSI_TC_TRIMCR_Pos) /*!< 0x00000004 */
  451. #define RCC_ICSCR_HSI_TC_TRIMCR_3 (0x08UL << RCC_ICSCR_HSI_TC_TRIMCR_Pos) /*!< 0x00000008 */
  452. #define RCC_ICSCR_HSI_FS_CR_Pos (16U)
  453. #define RCC_ICSCR_HSI_FS_CR_Msk (0x1UL << RCC_ICSCR_HSI_FS_CR_Pos) /*!< 0x00010000 */
  454. #define RCC_ICSCR_HSI_FS_CR RCC_ICSCR_HSI_FS_CR_Msk /*!< HSIFS bit */
  455. void InitRccAndFlashParam(void)
  456. {
  457. RCC_ICSCR_HSI_FS_RESTORE = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS_CR);
  458. RCC->ICSCR = (RCC->ICSCR & 0xFFFE0000) | (0x1 << RCC_ICSCR_HSI_FS_CR_Pos) | \
  459. HSI_HSICALIBRATION_24MHz; /* Set HSI 24M and Triming Data */
  460. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY));
  461. }
  462. void UnInitRccAndFlashParam(void)
  463. {
  464. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_FS_CR, RCC_ICSCR_HSI_FS_RESTORE);
  465. switch (RCC_ICSCR_HSI_FS_RESTORE)
  466. {
  467. case RCC_ICSCR_HSI_FS_CR://24MHz
  468. RCC->ICSCR = (RCC->ICSCR & 0xFFFE0000) | (0x1 << RCC_ICSCR_HSI_FS_CR_Pos) | \
  469. HSI_HSICALIBRATION_24MHz; /* Set HSI 24M and Triming Data */
  470. break;
  471. default://8MHz
  472. RCC->ICSCR = (RCC->ICSCR & 0xFFFE0000) | (0x0 << RCC_ICSCR_HSI_FS_CR_Pos) | \
  473. HSI_HSICALIBRATION_8MHz; /* Set HSI 8M and Triming Data */
  474. break;
  475. }
  476. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY));
  477. }