FlashPrg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 PY32F0xx 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. // Peripheral Memory Map
  48. #define FLASH_BASE (0x08000000UL) /*!< FLASH base address */
  49. #define FLASH_END (0x0800FFFFUL) /*!< FLASH end address */
  50. #define FLASH_SIZE (FLASH_END - FLASH_BASE + 1)
  51. #define FLASH_PAGE_SIZE 0x00000080U /*!< FLASH Page Size, 128 Bytes */
  52. #define FLASH_PAGE_NB (FLASH_SIZE / FLASH_PAGE_SIZE)
  53. #define FLASH_SECTOR_SIZE 0x00001000U /*!< FLASH Sector Size, 4096 Bytes */
  54. #define FLASH_SECTOR_NB (FLASH_SIZE / FLASH_SECTOR_SIZE)
  55. #define SRAM_BASE (0x20000000UL) /*!< SRAM base address */
  56. #define SRAM_END (0x20001FFFUL) /*!< SRAM end address */
  57. #define RCC_BASE 0x40021000
  58. #define WWDG_BASE 0x40002C00
  59. #define IWDG_BASE 0x40003000
  60. #define FLASH_R_BASE 0x40022000
  61. #define OB_BASE 0x1FFF0E80UL /*!< FLASH Option Bytes base address */
  62. #define RCC ((RCC_TypeDef*) RCC_BASE)
  63. #define WWDG ((WWDG_TypeDef *) WWDG_BASE)
  64. #define IWDG ((IWDG_TypeDef *) IWDG_BASE)
  65. #define FLASH ((FLASH_TypeDef*) FLASH_R_BASE)
  66. #define OB ((OB_TypeDef *) OB_BASE)
  67. typedef struct
  68. {
  69. vu32 CR; /*!< RCC Clock Sources Control Register, Address offset: 0x00 */
  70. vu32 ICSCR; /*!< RCC Internal Clock Sources Calibration Register, Address offset: 0x04 */
  71. } RCC_TypeDef;
  72. typedef struct
  73. {
  74. vu32 CR; /*!< WWDG Control register, Address offset: 0x00 */
  75. vu32 CFR; /*!< WWDG Configuration register, Address offset: 0x04 */
  76. vu32 SR; /*!< WWDG Status register, Address offset: 0x08 */
  77. } WWDG_TypeDef;
  78. /* Independent WATCHDOG*/
  79. typedef struct
  80. {
  81. vu32 KR; /*!< IWDG Key register, Address offset: 0x00 */
  82. vu32 PR; /*!< IWDG Prescaler register, Address offset: 0x04 */
  83. vu32 RLR; /*!< IWDG Reload register, Address offset: 0x08 */
  84. vu32 SR; /*!< IWDG Status register, Address offset: 0x0C */
  85. } IWDG_TypeDef;
  86. /* Flash Registers*/
  87. typedef struct
  88. {
  89. vu32 ACR; /*!< FLASH Access Control register, Address offset: 0x00 */
  90. vu32 RESERVED1; /*!< Reserved1, Address offset: 0x04 */
  91. vu32 KEYR; /*!< FLASH Key register, Address offset: 0x08 */
  92. vu32 OPTKEYR; /*!< FLASH Option Key register, Address offset: 0x0C */
  93. vu32 SR; /*!< FLASH Status register, Address offset: 0x10 */
  94. vu32 CR; /*!< FLASH Control register, Address offset: 0x14 */
  95. vu32 RESERVED2[2]; /*!< Reserved2, Address offset: 0x18-0x1C */
  96. vu32 OPTR; /*!< FLASH Option register, Address offset: 0x20 */
  97. vu32 SDKR; /*!< FLASH SDK address register, Address offset: 0x24 */
  98. vu32 RESERVED3; /*!< Reserved2, Address offset: 0x28 */
  99. vu32 WRPR; /*!< FLASH WRP address register, Address offset: 0x2C */
  100. vu32 RESERVED4[(0x90 - 0x2C) / 4 - 1];
  101. vu32 STCR; /*!< FLASH sleep time config register, Address offset: 0x90 */
  102. vu32 RESERVED5[(0x100 - 0x90) / 4 - 1];
  103. vu32 TS0; /*!< FLASH TS0 register, Address offset: 0x100 */
  104. vu32 TS1; /*!< FLASH TS1 register, Address offset: 0x104 */
  105. vu32 TS2P; /*!< FLASH TS2P register, Address offset: 0x108 */
  106. vu32 TPS3; /*!< FLASH TPS3 register, Address offset: 0x10C */
  107. vu32 TS3; /*!< FLASH TS3 register, Address offset: 0x110 */
  108. vu32 PERTPE; /*!< FLASH PERTPE register, Address offset: 0x114 */
  109. vu32 SMERTPE; /*!< FLASH SMERTPE register, Address offset: 0x118 */
  110. vu32 PRGTPE; /*!< FLASH PRGTPE register, Address offset: 0x11C */
  111. vu32 PRETPE; /*!< FLASH PRETPE register, Address offset: 0x120 */
  112. } FLASH_TypeDef;
  113. /* Flash Keys*/
  114. #define FLASH_KEY1 ((unsigned int)0x45670123)
  115. #define FLASH_KEY2 ((unsigned int)0xCDEF89AB)
  116. #define FLASH_OPTKEY1 ((unsigned int)0x08192A3B)
  117. #define FLASH_OPTKEY2 ((unsigned int)0x4C5D6E7F)
  118. /* Flash Control Register definitions*/
  119. #define FLASH_CR_PG_Pos (0U)
  120. #define FLASH_CR_PG_Msk (0x1UL << FLASH_CR_PG_Pos) /*!< 0x00000001 */
  121. #define FLASH_CR_PG FLASH_CR_PG_Msk
  122. #define FLASH_CR_PER_Pos (1U)
  123. #define FLASH_CR_PER_Msk (0x1UL << FLASH_CR_PER_Pos) /*!< 0x00000002 */
  124. #define FLASH_CR_PER FLASH_CR_PER_Msk
  125. #define FLASH_CR_MER_Pos (2U)
  126. #define FLASH_CR_MER_Msk (0x1UL << FLASH_CR_MER_Pos) /*!< 0x00000004 */
  127. #define FLASH_CR_MER FLASH_CR_MER_Msk
  128. #define FLASH_CR_SER_Pos (11U)
  129. #define FLASH_CR_SER_Msk (0x1UL << FLASH_CR_SER_Pos) /*!< 0x00000800 */
  130. #define FLASH_CR_SER FLASH_CR_SER_Msk
  131. #define FLASH_CR_OPTSTRT_Pos (17U)
  132. #define FLASH_CR_OPTSTRT_Msk (0x1UL << FLASH_CR_OPTSTRT_Pos) /*!< 0x00020000 */
  133. #define FLASH_CR_OPTSTRT FLASH_CR_OPTSTRT_Msk
  134. #define FLASH_CR_PGSTRT_Pos (19U)
  135. #define FLASH_CR_PGSTRT_Msk (0x1UL << FLASH_CR_PGSTRT_Pos) /*!< 0x00080000 */
  136. #define FLASH_CR_PGSTRT FLASH_CR_PGSTRT_Msk
  137. #define FLASH_CR_EOPIE_Pos (24U)
  138. #define FLASH_CR_EOPIE_Msk (0x1UL << FLASH_CR_EOPIE_Pos) /*!< 0x01000000 */
  139. #define FLASH_CR_EOPIE FLASH_CR_EOPIE_Msk
  140. #define FLASH_CR_ERRIE_Pos (25U)
  141. #define FLASH_CR_ERRIE_Msk (0x1UL << FLASH_CR_ERRIE_Pos) /*!< 0x02000000 */
  142. #define FLASH_CR_ERRIE FLASH_CR_ERRIE_Msk
  143. #define FLASH_CR_OBL_LAUNCH_Pos (27U)
  144. #define FLASH_CR_OBL_LAUNCH_Msk (0x1UL << FLASH_CR_OBL_LAUNCH_Pos) /*!< 0x08000000 */
  145. #define FLASH_CR_OBL_LAUNCH FLASH_CR_OBL_LAUNCH_Msk
  146. #define FLASH_CR_OPTLOCK_Pos (30U)
  147. #define FLASH_CR_OPTLOCK_Msk (0x1UL << FLASH_CR_OPTLOCK_Pos) /*!< 0x40000000 */
  148. #define FLASH_CR_OPTLOCK FLASH_CR_OPTLOCK_Msk
  149. #define FLASH_CR_LOCK_Pos (31U)
  150. #define FLASH_CR_LOCK_Msk (0x1UL << FLASH_CR_LOCK_Pos) /*!< 0x80000000 */
  151. #define FLASH_CR_LOCK FLASH_CR_LOCK_Msk
  152. /* Flash Status Register definitions*/
  153. #define FLASH_SR_EOP_Pos (0U)
  154. #define FLASH_SR_EOP_Msk (0x1UL << FLASH_SR_EOP_Pos) /*!< 0x00000001 */
  155. #define FLASH_SR_EOP FLASH_SR_EOP_Msk
  156. #define FLASH_SR_WRPERR_Pos (4U)
  157. #define FLASH_SR_WRPERR_Msk (0x1UL << FLASH_SR_WRPERR_Pos) /*!< 0x00000010 */
  158. #define FLASH_SR_WRPERR FLASH_SR_WRPERR_Msk
  159. #define FLASH_SR_OPTVERR_Pos (15U)
  160. #define FLASH_SR_OPTVERR_Msk (0x1UL << FLASH_SR_OPTVERR_Pos) /*!< 0x00008000 */
  161. #define FLASH_SR_OPTVERR FLASH_SR_OPTVERR_Msk
  162. #define FLASH_SR_BSY_Pos (16U)
  163. #define FLASH_SR_BSY_Msk (0x1UL << FLASH_SR_BSY_Pos) /*!< 0x00010000 */
  164. #define FLASH_SR_BSY FLASH_SR_BSY_Msk
  165. #define FLASH_OPTR_IWDG_SW_Pos (12U)
  166. #define FLASH_OPTR_IWDG_SW_Msk (0x1UL << FLASH_OPTR_IWDG_SW_Pos) /*!< 0x00010000 */
  167. #define FLASH_OPTR_IWDG_SW FLASH_OPTR_IWDG_SW_Msk
  168. #define FLASH_OPTR_WWDG_SW_Pos (13U)
  169. #define FLASH_OPTR_WWDG_SW_Msk (0x1UL << FLASH_OPTR_WWDG_SW_Pos) /*!< 0x00080000 */
  170. #define FLASH_OPTR_WWDG_SW FLASH_OPTR_WWDG_SW_Msk
  171. /******************* Bit definition for WWDG_CR register ********************/
  172. #define WWDG_CR_T_Pos (0U)
  173. #define WWDG_CR_T_Msk (0x7FUL << WWDG_CR_T_Pos) /*!< 0x0000007F */
  174. #define WWDG_CR_T WWDG_CR_T_Msk
  175. #define WWDG_CFR_WDGTB_Pos (7U)
  176. #define WWDG_CFR_WDGTB_Msk (0x3UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00001800 */
  177. #define WWDG_CFR_WDGTB WWDG_CFR_WDGTB_Msk /*!<WDGTB[1:0] bits (Timer Base) */
  178. #define WWDG_CFR_WDGTB_0 (0x1UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00000800 */
  179. #define WWDG_CFR_WDGTB_1 (0x2UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00001000 */
  180. u32 RCC_ICSCR_HSI_FS_RESTORE;
  181. void InitRccAndFlashParam(void);
  182. void UnInitRccAndFlashParam(void);
  183. /*
  184. * Initialize Flash Programming Functions
  185. * Parameter: adr: Device Base Address
  186. * clk: Clock Frequency (Hz)
  187. * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  188. * Return Value: 0 - OK, 1 - Failed
  189. */
  190. int Init(unsigned long adr, unsigned long clk, unsigned long fnc)
  191. {
  192. FLASH->KEYR = FLASH_KEY1; /* Unlock Flash */
  193. FLASH->KEYR = FLASH_KEY2;
  194. InitRccAndFlashParam();
  195. #ifdef FLASH_OB
  196. FLASH->OPTKEYR = FLASH_OPTKEY1; /* Unlock Option Bytes */
  197. FLASH->OPTKEYR = FLASH_OPTKEY2;
  198. #endif /* FLASH_OB */
  199. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  200. while (FLASH->SR & FLASH_SR_BSY); /* Check FLASH_SR_BSY */
  201. if ((FLASH->OPTR & FLASH_OPTR_IWDG_SW) == 0x00) /* Test if IWDG is running (IWDG in HW mode) */
  202. {
  203. /* Set IWDG time out to ~32.768 second */
  204. IWDG->KR = 0xAAAA; /* Reload IWDG */
  205. IWDG->KR = 0x5555; /* Enable write access to IWDG_PR and IWDG_RLR */
  206. IWDG->PR = 0x06; /* Set prescaler to 256 */
  207. IWDG->RLR = 0xFFF; /* Set reload value to 4095 */
  208. }
  209. if (FLASH_OPTR_WWDG_SW != READ_BIT(FLASH->OPTR, FLASH_OPTR_WWDG_SW)) /* Test if WWDG is running (WWDG in HW mode) */
  210. {
  211. /* Set WWDG time out to maximum */
  212. SET_BIT(WWDG->CFR, WWDG_CFR_WDGTB); /* Set prescaler to maximum */
  213. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  214. }
  215. return (0);
  216. }
  217. /*
  218. * De-Initialize Flash Programming Functions
  219. * Parameter: fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  220. * Return Value: 0 - OK, 1 - Failed
  221. */
  222. int UnInit(unsigned long fnc)
  223. {
  224. UnInitRccAndFlashParam();
  225. FLASH->CR |= FLASH_CR_LOCK; /* Lock Flash */
  226. #ifdef FLASH_OB
  227. FLASH->CR |= FLASH_CR_OPTLOCK; /* Lock Option Bytes */
  228. #endif
  229. return (0);
  230. }
  231. /*
  232. * Erase complete Flash Memory
  233. * Return Value: 0 - OK, 1 - Failed
  234. */
  235. #ifdef FLASH_MEM
  236. int EraseChip(void)
  237. {
  238. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  239. FLASH->CR |= FLASH_CR_MER; /* Mass Erase Enabled */
  240. FLASH->CR |= FLASH_CR_EOPIE;
  241. M32(FLASH_BASE) = 0xFF;
  242. __asm("DSB");
  243. while (FLASH->SR & FLASH_SR_BSY)
  244. {
  245. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  246. IWDG->KR = 0xAAAA; /* Reload IWDG */
  247. }
  248. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  249. {
  250. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  251. return (1); /* Failed */
  252. }
  253. FLASH->CR &= ~FLASH_CR_MER; /* Mass Erase Disabled */
  254. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  255. return (0); /* Done */
  256. }
  257. #endif
  258. #ifdef FLASH_OB
  259. int EraseChip(void)
  260. {
  261. /* erase chip is not needed for
  262. - Flash Option bytes
  263. - Flash One Time Programmable bytes
  264. */
  265. return (0); /* Done */
  266. }
  267. #endif
  268. /*
  269. * Erase Sector in Flash Memory
  270. * Parameter: adr: Sector Address
  271. * Return Value: 0 - OK, 1 - Failed
  272. */
  273. #ifdef FLASH_MEM
  274. int EraseSector(unsigned long adr)
  275. {
  276. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  277. FLASH->CR |= FLASH_CR_SER; /* Sector Erase Enabled */
  278. FLASH->CR |= FLASH_CR_EOPIE;
  279. M32(adr) = 0xFF; /* Sector Address */
  280. __asm("DSB");
  281. while (FLASH->SR & FLASH_SR_BSY)
  282. {
  283. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  284. IWDG->KR = 0xAAAA; /* Reload IWDG */
  285. }
  286. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  287. {
  288. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  289. return (1); /* Failed */
  290. }
  291. FLASH->CR &= ~FLASH_CR_SER; /* Sector Erase Disabled */
  292. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  293. return (0); /* Done */
  294. }
  295. #endif
  296. #ifdef FLASH_OB
  297. int EraseSector(unsigned long adr)
  298. {
  299. /* erase sector is not needed for
  300. - Flash Option bytes
  301. - Flash One Time Programmable bytes
  302. */
  303. return (0); /* Done */
  304. }
  305. #endif
  306. /*
  307. * Blank Check Checks if Memory is Blank
  308. * Parameter: adr: Block Start Address
  309. * sz: Block Size (in bytes)
  310. * pat: Block Pattern
  311. * Return Value: 0 - OK, 1 - Failed
  312. */
  313. int BlankCheck(unsigned long adr, unsigned long sz, unsigned char pat)
  314. {
  315. return (1); /* Always Force Erase */
  316. }
  317. /*
  318. * Program Page in Flash Memory
  319. * Parameter: adr: Page Start Address
  320. * sz: Page Size
  321. * buf: Page Data
  322. * Return Value: 0 - OK, 1 - Failed
  323. */
  324. #ifdef FLASH_MEM
  325. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  326. {
  327. sz = (sz + (FLASH_PAGE_SIZE - 1)) & ~(FLASH_PAGE_SIZE - 1); /* Adjust size for 32 Words */
  328. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  329. while (sz)
  330. {
  331. FLASH->CR |= FLASH_CR_PG; /* Programming Enabled */
  332. FLASH->CR |= FLASH_CR_EOPIE;
  333. for (u8 i = 0; i < (FLASH_PAGE_SIZE / 4); i++)
  334. {
  335. M32(adr + i * 4) = *((u32 *)(buf + i * 4)); /* Program the first word of the Double Word */
  336. if (i == (FLASH_PAGE_SIZE / 4 - 2))
  337. {
  338. FLASH->CR |= FLASH_CR_PGSTRT;
  339. }
  340. }
  341. __asm("DSB");
  342. while (FLASH->SR & FLASH_SR_BSY)
  343. {
  344. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  345. IWDG->KR = 0xAAAA; /* Reload IWDG */
  346. }
  347. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  348. {
  349. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  350. return (1); /* Failed */
  351. }
  352. FLASH->CR &= ~FLASH_CR_PG; /* Programming Disabled */
  353. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  354. adr += FLASH_PAGE_SIZE; /* Go to next Page */
  355. buf += FLASH_PAGE_SIZE;
  356. sz -= FLASH_PAGE_SIZE;
  357. }
  358. return (0); /* Done */
  359. }
  360. #endif /* FLASH_MEM */
  361. #ifdef FLASH_OB
  362. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  363. {
  364. u32 optr;
  365. u32 sdkr;
  366. u32 wrpr;
  367. optr = *((u32 *)(buf + 0x00));
  368. sdkr = *((u32 *)(buf + 0x04));
  369. wrpr = *((u32 *)(buf + 0x0C));
  370. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  371. FLASH->OPTR = (optr & 0x0000FFFF); /* Write OPTR values */
  372. FLASH->SDKR = (sdkr & 0x0000FFFF); /* Write SDKR values */
  373. FLASH->WRPR = (wrpr & 0x0000FFFF); /* Write WRPR values */
  374. FLASH->CR |= FLASH_CR_OPTSTRT;
  375. FLASH->CR |= FLASH_CR_EOPIE;
  376. M32(0x40022080) = 0xFF; /*Address Write Date*/
  377. __asm("DSB");
  378. while (FLASH->SR & FLASH_SR_BSY)
  379. {
  380. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  381. IWDG->KR = 0xAAAA; /* Reload IWDG */
  382. }
  383. if (FLASH_SR_EOP != (FLASH->SR & FLASH_SR_EOP)) /* Check for FLASH_SR_EOP */
  384. {
  385. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  386. return (1); /* Failed */
  387. }
  388. FLASH->CR &= ~FLASH_CR_OPTSTRT; /* Programming Disabled */
  389. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  390. return (0); /* Done */
  391. }
  392. #endif /* FLASH_OB */
  393. /*
  394. * Verify Flash Contents
  395. * Parameter: adr: Start Address
  396. * sz: Size (in bytes)
  397. * buf: Data
  398. * Return Value: (adr+sz) - OK, Failed Address
  399. */
  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 RCC_CR_HSIRDY_Pos (10U)
  425. #define RCC_CR_HSIRDY_Msk (0x1UL << RCC_CR_HSIRDY_Pos) /*!< 0x00000400 */
  426. #define RCC_CR_HSIRDY RCC_CR_HSIRDY_Msk /*!< Internal High Speed clock ready flag */
  427. #define RCC_ICSCR_HSI_TRIM_Pos (0U)
  428. #define RCC_ICSCR_HSI_TRIM_Msk (0x1FFFUL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00001FFF */
  429. #define RCC_ICSCR_HSI_TRIM RCC_ICSCR_HSI_TRIM_Msk /*!< HSITRIM[14:8] bits */
  430. #define RCC_ICSCR_HSI_FS_Pos (13U)
  431. #define RCC_ICSCR_HSI_FS_Msk (0x7UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x0000E000 */
  432. #define RCC_ICSCR_HSI_FS RCC_ICSCR_HSI_FS_Msk /*!< HSIFS[15:13] bits */
  433. #define RCC_ICSCR_HSI_FS_0 (0x01UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00002000 */
  434. #define RCC_ICSCR_HSI_FS_1 (0x02UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00004000 */
  435. #define RCC_ICSCR_HSI_FS_2 (0x04UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00008000 */
  436. void InitRccAndFlashParam(void)
  437. {
  438. RCC_ICSCR_HSI_FS_RESTORE = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS);
  439. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_FS, RCC_ICSCR_HSI_FS_2); /* HSI_24MHz */
  440. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F10)&RCC_ICSCR_HSI_TRIM);
  441. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY));
  442. FLASH->TS0 = ((M32(0x1FFF0F1C + 4 * 0x14) >> 0) & 0x000000FF);
  443. FLASH->TS3 = ((M32(0x1FFF0F1C + 4 * 0x14) >> 8) & 0x000000FF);
  444. FLASH->TS1 = ((M32(0x1FFF0F1C + 4 * 0x14) >> 16) & 0x000001FF);
  445. FLASH->TS2P = ((M32(0x1FFF0F20 + 4 * 0x14) >> 0) & 0x000000FF);
  446. FLASH->TPS3 = ((M32(0x1FFF0F20 + 4 * 0x14) >> 16) & 0x000007FF);
  447. FLASH->PERTPE = ((M32(0x1FFF0F24 + 4 * 0x14) >> 0) & 0x0001FFFF);
  448. FLASH->SMERTPE = ((M32(0x1FFF0F28 + 4 * 0x14) >> 0) & 0x0001FFFF);
  449. FLASH->PRGTPE = ((M32(0x1FFF0F2C + 4 * 0x14) >> 0) & 0x0000FFFF);
  450. FLASH->PRETPE = ((M32(0x1FFF0F2C + 4 * 0x14) >> 16) & 0x0000FFFF);
  451. }
  452. void UnInitRccAndFlashParam(void)
  453. {
  454. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_FS, RCC_ICSCR_HSI_FS_RESTORE);
  455. switch (RCC_ICSCR_HSI_FS_RESTORE)
  456. {
  457. case RCC_ICSCR_HSI_FS_2://24MHz
  458. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F10)&RCC_ICSCR_HSI_TRIM);
  459. break;
  460. case (RCC_ICSCR_HSI_FS_1|RCC_ICSCR_HSI_FS_0)://22.12MHz
  461. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F0C)&RCC_ICSCR_HSI_TRIM);
  462. break;
  463. case RCC_ICSCR_HSI_FS_1://16MHz
  464. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F08)&RCC_ICSCR_HSI_TRIM);
  465. break;
  466. case RCC_ICSCR_HSI_FS_0://8MHz
  467. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F04)&RCC_ICSCR_HSI_TRIM);
  468. break;
  469. default://4MHz
  470. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_TRIM, M32(0x1FFF0F00)&RCC_ICSCR_HSI_TRIM);
  471. break;
  472. }
  473. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY));
  474. }