FlashPrg.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 PY32F030xx 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 HW32_REG(ADDRESS) ( * ((volatile unsigned int * )(ADDRESS)))
  41. #define HW16_REG(ADDRESS) ( * ((volatile unsigned short int * )(ADDRESS)))
  42. #define HW8_REG(ADDRESS) ( * ((volatile unsigned char * )(ADDRESS)))
  43. #define SET_BIT(REG, BIT) ((REG) |= (BIT))
  44. #define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
  45. #define READ_BIT(REG, BIT) ((REG) & (BIT))
  46. #define CLEAR_REG(REG) ((REG) = (0x0))
  47. #define WRITE_REG(REG, VAL) ((REG) = (VAL))
  48. #define READ_REG(REG) ((REG))
  49. #define MODIFY_REG(REG, CLEARMASK, SETMASK) WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
  50. #define FLASH_BASE (0x08000000UL) /*!< FLASH base address */
  51. #define FLASH_END (0x0801FFFFUL) /*!< FLASH end address */
  52. #define FLASH_SIZE (FLASH_END - FLASH_BASE + 1)
  53. #define FLASH_PAGE_SIZE 0x00000100U /*!< FLASH Page Size, 256 Bytes */
  54. #define FLASH_PAGE_NB (FLASH_SIZE / FLASH_PAGE_SIZE)
  55. #define FLASH_SECTOR_SIZE 0x00002000U /*!< FLASH Sector Size, 8192 Bytes */
  56. #define FLASH_SECTOR_NB (FLASH_SIZE / FLASH_SECTOR_SIZE)
  57. #define SRAM_BASE (0x20000000UL) /*!< SRAM base address */
  58. #define SRAM_END (0x20003FFFUL) /*!< SRAM end address */
  59. #define WWDG_BASE 0x40002C00
  60. #define IWDG_BASE 0x40003000
  61. #define RCC_BASE 0x40021000
  62. #define FLASH_R_BASE 0x40022000 /*!< FLASH registers base address */
  63. #define OB_BASE 0x1FFF3100
  64. #define RCC ((RCC_TypeDef *) RCC_BASE)
  65. #define WWDG ((WWDG_TypeDef *) WWDG_BASE)
  66. #define IWDG ((IWDG_TypeDef *) IWDG_BASE)
  67. #define FLASH ((FLASH_TypeDef *) FLASH_R_BASE)
  68. #define OB ((OB_TypeDef *) OB_BASE)
  69. typedef struct
  70. {
  71. vu32 CR; /*!< RCC Clock Sources Control Register, Address offset: 0x00 */
  72. vu32 ICSCR; /*!< RCC Internal Clock Sources Calibration Register, Address offset: 0x04 */
  73. } RCC_TypeDef;
  74. typedef struct
  75. {
  76. vu32 KR; /*!< IWDG Key register, Address offset: 0x00 */
  77. vu32 PR; /*!< IWDG Prescaler register, Address offset: 0x04 */
  78. vu32 RLR; /*!< IWDG Reload register, Address offset: 0x08 */
  79. vu32 SR; /*!< IWDG Status register, Address offset: 0x0C */
  80. } IWDG_TypeDef;
  81. typedef struct
  82. {
  83. vu32 CR; /*!< WWDG Control register, Address offset: 0x00 */
  84. vu32 CFR; /*!< WWDG Configuration register, Address offset: 0x04 */
  85. vu32 SR; /*!< WWDG Status register, Address offset: 0x08 */
  86. } WWDG_TypeDef;
  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 BORCR; /*!< FLASH BORCR address register, Address offset: 0x24 */
  98. vu32 RESERVED3; /*!< RESERVED3, Address offset: 0x28 */
  99. vu32 WRPR; /*!< FLASH WRP address register, Address offset: 0x2C */
  100. vu32 RESERVED4[(0x90 - 0x2C) / 4 - 1]; /*!< RESERVED4, Address offset: 0x30-0x8C */
  101. vu32 STCR; /*!< FLASH sleep time config register, Address offset: 0x90 */
  102. vu32 RESERVED5[(0x100 - 0x90) / 4 - 1]; /*!< RESERVED5, Address offset: 0x94-0xFC */
  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_Pos (0U)
  115. #define FLASH_KEY1_Msk (0x45670123UL << FLASH_KEY1_Pos) /*!< 0x45670123 */
  116. #define FLASH_KEY1 FLASH_KEY1_Msk /*!< Flash program erase key1 */
  117. #define FLASH_KEY2_Pos (0U)
  118. #define FLASH_KEY2_Msk (0xCDEF89ABUL << FLASH_KEY2_Pos) /*!< 0xCDEF89AB */
  119. #define FLASH_KEY2 FLASH_KEY2_Msk /*!< Flash program erase key2: used with FLASH_PEKEY1
  120. to unlock the write access to the FPEC. */
  121. #define FLASH_OPTKEY1_Pos (0U)
  122. #define FLASH_OPTKEY1_Msk (0x08192A3BUL << FLASH_OPTKEY1_Pos) /*!< 0x08192A3B */
  123. #define FLASH_OPTKEY1 FLASH_OPTKEY1_Msk /*!< Flash option key1 */
  124. #define FLASH_OPTKEY2_Pos (0U)
  125. #define FLASH_OPTKEY2_Msk (0x4C5D6E7FUL << FLASH_OPTKEY2_Pos) /*!< 0x4C5D6E7F */
  126. #define FLASH_OPTKEY2 FLASH_OPTKEY2_Msk /*!< Flash option key2: used with FLASH_OPTKEY1 to
  127. unlock the write access to the option byte block */
  128. /******************* Bits definition for FLASH_SR register ****************/
  129. #define FLASH_SR_EOP_Pos (0U)
  130. #define FLASH_SR_EOP_Msk (0x1UL << FLASH_SR_EOP_Pos) /*!< 0x00000001 */
  131. #define FLASH_SR_EOP FLASH_SR_EOP_Msk
  132. #define FLASH_SR_WRPERR_Pos (4U)
  133. #define FLASH_SR_WRPERR_Msk (0x1UL << FLASH_SR_WRPERR_Pos) /*!< 0x00000010 */
  134. #define FLASH_SR_WRPERR FLASH_SR_WRPERR_Msk
  135. #define FLASH_SR_OPTVERR_Pos (15U)
  136. #define FLASH_SR_OPTVERR_Msk (0x1UL << FLASH_SR_OPTVERR_Pos) /*!< 0x00008000 */
  137. #define FLASH_SR_OPTVERR FLASH_SR_OPTVERR_Msk
  138. #define FLASH_SR_BSY_Pos (16U)
  139. #define FLASH_SR_BSY_Msk (0x1UL << FLASH_SR_BSY_Pos) /*!< 0x00010000 */
  140. #define FLASH_SR_BSY FLASH_SR_BSY_Msk
  141. /******************* Bits definition for FLASH_CR register ****************/
  142. #define FLASH_CR_PG_Pos (0U)
  143. #define FLASH_CR_PG_Msk (0x1UL << FLASH_CR_PG_Pos) /*!< 0x00000001 */
  144. #define FLASH_CR_PG FLASH_CR_PG_Msk
  145. #define FLASH_CR_PER_Pos (1U)
  146. #define FLASH_CR_PER_Msk (0x1UL << FLASH_CR_PER_Pos) /*!< 0x00000002 */
  147. #define FLASH_CR_PER FLASH_CR_PER_Msk
  148. #define FLASH_CR_MER_Pos (2U)
  149. #define FLASH_CR_MER_Msk (0x1UL << FLASH_CR_MER_Pos) /*!< 0x00000004 */
  150. #define FLASH_CR_MER FLASH_CR_MER_Msk
  151. #define FLASH_CR_SER_Pos (11U)
  152. #define FLASH_CR_SER_Msk (0x1UL << FLASH_CR_SER_Pos) /*!< 0x00000800 */
  153. #define FLASH_CR_SER FLASH_CR_SER_Msk
  154. #define FLASH_CR_OPTSTRT_Pos (17U)
  155. #define FLASH_CR_OPTSTRT_Msk (0x1UL << FLASH_CR_OPTSTRT_Pos) /*!< 0x00020000 */
  156. #define FLASH_CR_OPTSTRT FLASH_CR_OPTSTRT_Msk
  157. #define FLASH_CR_PGSTRT_Pos (19U)
  158. #define FLASH_CR_PGSTRT_Msk (0x1UL << FLASH_CR_PGSTRT_Pos) /*!< 0x00080000 */
  159. #define FLASH_CR_PGSTRT FLASH_CR_PGSTRT_Msk
  160. #define FLASH_CR_EOPIE_Pos (24U)
  161. #define FLASH_CR_EOPIE_Msk (0x1UL << FLASH_CR_EOPIE_Pos) /*!< 0x01000000 */
  162. #define FLASH_CR_EOPIE FLASH_CR_EOPIE_Msk
  163. #define FLASH_CR_ERRIE_Pos (25U)
  164. #define FLASH_CR_ERRIE_Msk (0x1UL << FLASH_CR_ERRIE_Pos) /*!< 0x02000000 */
  165. #define FLASH_CR_ERRIE FLASH_CR_ERRIE_Msk
  166. #define FLASH_CR_OBL_LAUNCH_Pos (27U)
  167. #define FLASH_CR_OBL_LAUNCH_Msk (0x1UL << FLASH_CR_OBL_LAUNCH_Pos) /*!< 0x08000000 */
  168. #define FLASH_CR_OBL_LAUNCH FLASH_CR_OBL_LAUNCH_Msk
  169. #define FLASH_CR_OPTLOCK_Pos (30U)
  170. #define FLASH_CR_OPTLOCK_Msk (0x1UL << FLASH_CR_OPTLOCK_Pos) /*!< 0x40000000 */
  171. #define FLASH_CR_OPTLOCK FLASH_CR_OPTLOCK_Msk
  172. #define FLASH_CR_LOCK_Pos (31U)
  173. #define FLASH_CR_LOCK_Msk (0x1UL << FLASH_CR_LOCK_Pos) /*!< 0x80000000 */
  174. #define FLASH_CR_LOCK FLASH_CR_LOCK_Msk
  175. #define FLASH_OPTR_IWDG_SW_Pos (11U)
  176. #define FLASH_OPTR_IWDG_SW_Msk (0x1UL << FLASH_OPTR_IWDG_SW_Pos) /*!< 0x00000800 */
  177. #define FLASH_OPTR_IWDG_SW FLASH_OPTR_IWDG_SW_Msk
  178. #define FLASH_OPTR_WWDG_SW_Pos (12U)
  179. #define FLASH_OPTR_WWDG_SW_Msk (0x1UL << FLASH_OPTR_WWDG_SW_Pos) /*!< 0x00001000 */
  180. #define FLASH_OPTR_WWDG_SW FLASH_OPTR_WWDG_SW_Msk
  181. #define FLASH_OPTR_IWDG_STOP_Pos (15U)
  182. #define FLASH_OPTR_IWDG_STOP_Msk (0x1UL << FLASH_OPTR_IWDG_STOP_Pos) /*!< 0x00008000 */
  183. #define FLASH_OPTR_IWDG_STOP FLASH_OPTR_IWDG_STOP_Msk
  184. /******************** Bit definition for RCC_CR register ******************/
  185. #define RCC_CR_HSION_Pos (8U)
  186. #define RCC_CR_HSION_Msk (0x1UL << RCC_CR_HSION_Pos) /*!< 0x00000100 */
  187. #define RCC_CR_HSION RCC_CR_HSION_Msk /*!< Internal High Speed clock enable */
  188. #define RCC_CR_HSIRDY_Pos (10U)
  189. #define RCC_CR_HSIRDY_Msk (0x1UL << RCC_CR_HSIRDY_Pos) /*!< 0x00000400 */
  190. #define RCC_CR_HSIRDY RCC_CR_HSIRDY_Msk /*!< Internal High Speed clock ready flag */
  191. /******************** Bit definition for RCC_ICSCR register ***************/
  192. #define RCC_ICSCR_HSI_TRIM_Pos (0U)
  193. #define RCC_ICSCR_HSI_TRIM_Msk (0x1FFFUL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00007F00 */
  194. #define RCC_ICSCR_HSI_TRIM RCC_ICSCR_HSI_TRIM_Msk /*!< HSITRIM[14:8] bits */
  195. #define RCC_ICSCR_HSI_TRIM_0 (0x01UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000001 */
  196. #define RCC_ICSCR_HSI_TRIM_1 (0x02UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000002 */
  197. #define RCC_ICSCR_HSI_TRIM_2 (0x04UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000004 */
  198. #define RCC_ICSCR_HSI_TRIM_3 (0x08UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000008 */
  199. #define RCC_ICSCR_HSI_TRIM_4 (0x10UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000010 */
  200. #define RCC_ICSCR_HSI_TRIM_5 (0x20UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000020 */
  201. #define RCC_ICSCR_HSI_TRIM_6 (0x40UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000040 */
  202. #define RCC_ICSCR_HSI_TRIM_7 (0x80UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000080 */
  203. #define RCC_ICSCR_HSI_TRIM_8 (0x100UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000100 */
  204. #define RCC_ICSCR_HSI_TRIM_9 (0x200UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000200 */
  205. #define RCC_ICSCR_HSI_TRIM_10 (0x400UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000400 */
  206. #define RCC_ICSCR_HSI_TRIM_11 (0x800UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00000800 */
  207. #define RCC_ICSCR_HSI_TRIM_12 (0x1000UL << RCC_ICSCR_HSI_TRIM_Pos) /*!< 0x00001000 */
  208. #define RCC_ICSCR_HSI_FS_Pos (13U)
  209. #define RCC_ICSCR_HSI_FS_Msk (0x7UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x0000E000 */
  210. #define RCC_ICSCR_HSI_FS RCC_ICSCR_HSI_FS_Msk /*!< HSIFS[15:13] bits */
  211. #define RCC_ICSCR_HSI_FS_0 (0x01UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00002000 */
  212. #define RCC_ICSCR_HSI_FS_1 (0x02UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00004000 */
  213. #define RCC_ICSCR_HSI_FS_2 (0x04UL << RCC_ICSCR_HSI_FS_Pos) /*!< 0x00008000 */
  214. /******************* Bit definition for WWDG_CR register ********************/
  215. #define WWDG_CR_T_Pos (0U)
  216. #define WWDG_CR_T_Msk (0x7FUL << WWDG_CR_T_Pos) /*!< 0x0000007F */
  217. #define WWDG_CR_T WWDG_CR_T_Msk /*!<T[6:0] bits (7-Bit counter (MSB to LSB)) */
  218. /*!<Activation bit */
  219. /******************* Bit definition for WWDG_CFR register *******************/
  220. #define WWDG_CFR_W_Pos (0U)
  221. #define WWDG_CFR_W_Msk (0x7FUL << WWDG_CFR_W_Pos) /*!< 0x0000007F */
  222. #define WWDG_CFR_W WWDG_CFR_W_Msk /*!<W[6:0] bits (7-bit window value) */
  223. #define WWDG_CFR_WDGTB_Pos (7U)
  224. #define WWDG_CFR_WDGTB_Msk (0x3UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00001800 */
  225. #define WWDG_CFR_WDGTB WWDG_CFR_WDGTB_Msk /*!<WDGTB[1:0] bits (Timer Base) */
  226. #define WWDG_CFR_WDGTB_0 (0x1UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00000800 */
  227. #define WWDG_CFR_WDGTB_1 (0x2UL << WWDG_CFR_WDGTB_Pos) /*!< 0x00001000 */
  228. #define WWDG_CFR_EWI_Pos (9U)
  229. #define WWDG_CFR_EWI_Msk (0x1UL << WWDG_CFR_EWI_Pos) /*!< 0x00000200 */
  230. #define WWDG_CFR_EWI WWDG_CFR_EWI_Msk /*!<Early Wakeup Interrupt */
  231. u32 gdw_HSI_FS;
  232. /* uiOffset = 0(4MHz), 1(8MHz), 2(16MHz), 3(22.12MHz), 4(24MHz) */
  233. void SetFlashParameter(u32 uiOffset)
  234. {
  235. /* WRITE_REG(FLASH->KEYR, FLASH_KEY1); */
  236. /* WRITE_REG(FLASH->KEYR, FLASH_KEY2); */
  237. FLASH->TS0 = ((HW32_REG(0x1FFF3238 + uiOffset * 0x28) >> 0) & 0x000000FF);
  238. FLASH->TS3 = ((HW32_REG(0x1FFF3238 + uiOffset * 0x28) >> 8) & 0x000000FF);
  239. FLASH->TS1 = ((HW32_REG(0x1FFF3238 + uiOffset * 0x28) >> 16) & 0x000001FF);
  240. FLASH->TS2P = ((HW32_REG(0x1FFF3240 + uiOffset * 0x28) >> 0) & 0x000000FF);
  241. FLASH->TPS3 = ((HW32_REG(0x1FFF3240 + uiOffset * 0x28) >> 16) & 0x000007FF);
  242. FLASH->PERTPE = ((HW32_REG(0x1FFF3248 + uiOffset * 0x28) >> 0) & 0x0001FFFF);
  243. FLASH->SMERTPE = ((HW32_REG(0x1FFF3250 + uiOffset * 0x28) >> 0) & 0x0001FFFF);
  244. FLASH->PRGTPE = ((HW32_REG(0x1FFF3258 + uiOffset * 0x28) >> 0) & 0x0000FFFF);
  245. FLASH->PRETPE = ((HW32_REG(0x1FFF3258 + uiOffset * 0x28) >> 16) & 0x0000FFFF);
  246. // SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  247. }
  248. void InitRccAndFlashParam(void)
  249. {
  250. gdw_HSI_FS = READ_BIT(RCC->ICSCR, RCC_ICSCR_HSI_FS);
  251. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3220)); // HSI_24MHz
  252. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY))
  253. ;
  254. SetFlashParameter(4);
  255. }
  256. void UnInitRccAndFlashParam(void)
  257. {
  258. MODIFY_REG(RCC->ICSCR, RCC_ICSCR_HSI_FS, gdw_HSI_FS);
  259. switch (gdw_HSI_FS)
  260. {
  261. case RCC_ICSCR_HSI_FS_2: // 24MHz
  262. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3220));
  263. break;
  264. case (RCC_ICSCR_HSI_FS_1 | RCC_ICSCR_HSI_FS_0): // 22.12MHz
  265. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3218));
  266. break;
  267. case RCC_ICSCR_HSI_FS_1: // 16MHz
  268. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3210));
  269. break;
  270. case RCC_ICSCR_HSI_FS_0: // 8MHz
  271. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3208));
  272. break;
  273. default: // 4MHz
  274. MODIFY_REG(RCC->ICSCR, (RCC_ICSCR_HSI_FS | RCC_ICSCR_HSI_TRIM), M32(0x1FFF3200));
  275. break;
  276. }
  277. while (RCC_CR_HSIRDY != READ_BIT(RCC->CR, RCC_CR_HSIRDY))
  278. ;
  279. }
  280. /*
  281. * Initialize Flash Programming Functions
  282. * Parameter: adr: Device Base Address
  283. * clk: Clock Frequency (Hz)
  284. * fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  285. * Return Value: 0 - OK, 1 - Failed
  286. */
  287. int Init(unsigned long adr, unsigned long clk, unsigned long fnc)
  288. {
  289. /* Unlock Flash */
  290. FLASH->KEYR = FLASH_KEY1;
  291. FLASH->KEYR = FLASH_KEY2;
  292. #ifdef Flash_OB
  293. /* Unlock Option Bytes */
  294. FLASH->OPTKEYR = FLASH_OPTKEY1;
  295. FLASH->OPTKEYR = FLASH_OPTKEY2;
  296. #endif
  297. InitRccAndFlashParam();
  298. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  299. while (FLASH->SR & FLASH_SR_BSY); /* Check FLASH_SR_BSY */
  300. if (FLASH_OPTR_IWDG_SW != READ_BIT(FLASH->OPTR, FLASH_OPTR_IWDG_SW)) /* Test if IWDG is running (IWDG in HW mode) */
  301. {
  302. /* Set IWDG time out to maximum */
  303. IWDG->KR = 0xAAAA; /* Reload IWDG */
  304. IWDG->KR = 0x5555; /* Enable write access to IWDG_PR and IWDG_RLR */
  305. IWDG->PR = 0x06; /* Set prescaler to 256 */
  306. IWDG->RLR = 0xFFF; /* Set reload value to 4095 */
  307. }
  308. if (FLASH_OPTR_WWDG_SW != READ_BIT(FLASH->OPTR, FLASH_OPTR_WWDG_SW)) /* Test if WWDG is running (WWDG in HW mode) */
  309. {
  310. /* Set WWDG time out to maximum */
  311. SET_BIT(WWDG->CFR, WWDG_CFR_WDGTB); /* Set prescaler to maximum */
  312. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  313. }
  314. return (0);
  315. }
  316. /*
  317. * De-Initialize Flash Programming Functions
  318. * Parameter: fnc: Function Code (1 - Erase, 2 - Program, 3 - Verify)
  319. * Return Value: 0 - OK, 1 - Failed
  320. */
  321. int UnInit(unsigned long fnc)
  322. {
  323. UnInitRccAndFlashParam();
  324. /* Unlock Flash */
  325. FLASH->CR |= FLASH_CR_LOCK;
  326. #ifdef Flash_OB
  327. /* Unlock Option Bytes */
  328. FLASH->CR |= FLASH_CR_OPTLOCK;
  329. #endif
  330. return (0);
  331. }
  332. /*
  333. * Erase complete Flash Memory
  334. * Return Value: 0 - OK, 1 - Failed
  335. */
  336. #ifdef FLASH_MEM
  337. int EraseChip(void)
  338. {
  339. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  340. FLASH->CR |= FLASH_CR_MER; /* Mass Erase Enabled */
  341. FLASH->CR |= FLASH_CR_EOPIE;
  342. M32(FLASH_BASE) = 0xFF;
  343. __asm("DSB");
  344. while (FLASH->SR & FLASH_SR_BSY)
  345. {
  346. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  347. WRITE_REG(IWDG->KR, 0x0000AAAAU); /* Reload IWDG */
  348. }
  349. if (FLASH->SR & FLASH_SR_EOP) /* Check for FLASH_SR_EOP */
  350. {
  351. FLASH->SR &= ~FLASH_SR_EOP; /* Clear FLASH_SR_EOP */
  352. }
  353. else
  354. {
  355. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  356. return (1); /* Failed */
  357. }
  358. FLASH->CR &= ~FLASH_CR_MER; /* Mass Erase Disabled */
  359. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  360. return (0); // Done
  361. }
  362. #endif
  363. #ifdef Flash_OB
  364. int EraseChip(void)
  365. {
  366. /* erase chip is not needed for
  367. - Flash Option bytes
  368. - Flash One Time Programmable bytes
  369. */
  370. return (0); // Done
  371. }
  372. #endif
  373. /*
  374. * Erase Sector in Flash Memory
  375. * Parameter: adr: Sector Address
  376. * Return Value: 0 - OK, 1 - Failed
  377. */
  378. #ifdef FLASH_MEM
  379. int EraseSector(unsigned long adr)
  380. {
  381. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  382. FLASH->CR |= FLASH_CR_SER; /* Sector Erase Enabled */
  383. FLASH->CR |= FLASH_CR_EOPIE;
  384. M32(adr) = 0xFF; /* Sector Address */
  385. __asm("DSB");
  386. while (FLASH->SR & FLASH_SR_BSY)
  387. {
  388. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  389. WRITE_REG(IWDG->KR, 0x0000AAAAU); /* Reload IWDG */
  390. }
  391. if (FLASH->SR & FLASH_SR_EOP) /* Check for FLASH_SR_EOP */
  392. {
  393. FLASH->SR &= ~FLASH_SR_EOP; /* Clear FLASH_SR_EOP */
  394. }
  395. else
  396. {
  397. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  398. return (1); /* Failed */
  399. }
  400. FLASH->CR &= ~FLASH_CR_SER; /* Sector Erase Disabled */
  401. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  402. return (0); // Done
  403. }
  404. #endif
  405. #ifdef Flash_OB
  406. int EraseSector(unsigned long adr)
  407. {
  408. /* erase sector is not needed for
  409. - Flash Option bytes
  410. - Flash One Time Programmable bytes
  411. */
  412. return (0); // Done
  413. }
  414. #endif
  415. /*
  416. * Blank Check Checks if Memory is Blank
  417. * Parameter: adr: Block Start Address
  418. * sz: Block Size (in bytes)
  419. * pat: Block Pattern
  420. * Return Value: 0 - OK, 1 - Failed
  421. */
  422. int BlankCheck(unsigned long adr, unsigned long sz, unsigned char pat)
  423. {
  424. return (1); // Always Force Erase
  425. }
  426. /*
  427. * Program Page in Flash Memory
  428. * Parameter: adr: Page Start Address
  429. * sz: Page Size
  430. * buf: Page Data
  431. * Return Value: 0 - OK, 1 - Failed
  432. */
  433. #ifdef FLASH_MEM
  434. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  435. {
  436. sz = (sz + (FLASH_PAGE_SIZE - 1)) & ~(FLASH_PAGE_SIZE - 1); /* Adjust size for 32 Words */
  437. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  438. while (sz)
  439. {
  440. FLASH->CR |= FLASH_CR_PG; /* Programming Enabled */
  441. FLASH->CR |= FLASH_CR_EOPIE;
  442. for (u8 i = 0; i < (FLASH_PAGE_SIZE / 4); i++)
  443. {
  444. M32(adr + i * 4) = *((u32 *)(buf + i * 4)); /* Program the first word of the Double Word */
  445. if (i == (FLASH_PAGE_SIZE / 4 - 2))
  446. {
  447. FLASH->CR |= FLASH_CR_PGSTRT;
  448. }
  449. }
  450. __asm("DSB");
  451. while (FLASH->SR & FLASH_SR_BSY)
  452. {
  453. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  454. WRITE_REG(IWDG->KR, 0x0000AAAAU); /* Reload IWDG */
  455. }
  456. if (FLASH->SR & FLASH_SR_EOP) /* Check for FLASH_SR_EOP */
  457. {
  458. FLASH->SR &= ~FLASH_SR_EOP; /* Clear FLASH_SR_EOP */
  459. }
  460. else
  461. {
  462. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  463. return (1); /* Failed */
  464. }
  465. FLASH->CR &= ~FLASH_CR_PG; /* Programming Disabled */
  466. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  467. adr += FLASH_PAGE_SIZE; /* Go to next Page */
  468. buf += FLASH_PAGE_SIZE;
  469. sz -= FLASH_PAGE_SIZE;
  470. }
  471. return (0); // Done
  472. }
  473. #endif /* PY063xx_ || FLASH_OTP */
  474. #ifdef Flash_OB
  475. int ProgramPage(unsigned long adr, unsigned long sz, unsigned char *buf)
  476. {
  477. u32 optr;
  478. u32 borcr;
  479. u32 wrpr;
  480. optr = *((u32 *)(buf + 0x00));
  481. borcr = *((u32 *)(buf + 0x08));
  482. wrpr = *((u32 *)(buf + 0x18));
  483. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  484. FLASH->OPTR = (optr & 0x0000FFFF); /* Write OPTR values */
  485. FLASH->BORCR = (borcr & 0x0000FFFF); /* Write BORCR values */
  486. FLASH->WRPR = (wrpr & 0x0000FFFF); /* Write WRPR values */
  487. FLASH->CR |= FLASH_CR_OPTSTRT;
  488. FLASH->CR |= FLASH_CR_EOPIE;
  489. M32(0x40022080) = 0xFF; /*Address Write Date*/
  490. __asm("DSB");
  491. while (FLASH->SR & FLASH_SR_BSY)
  492. {
  493. SET_BIT(WWDG->CR, WWDG_CR_T); /* Reload WWDG */
  494. WRITE_REG(IWDG->KR, 0x0000AAAAU); /* Reload IWDG */
  495. }
  496. if (FLASH->SR & FLASH_SR_EOP) /* Check for FLASH_SR_EOP */
  497. {
  498. FLASH->SR &= ~FLASH_SR_EOP; /* Clear FLASH_SR_EOP */
  499. }
  500. else
  501. {
  502. FLASH->SR |= FLASH_SR_EOP; /* Reset FLASH_EOP */
  503. return (1); /* Failed */
  504. }
  505. FLASH->CR &= ~FLASH_CR_OPTSTRT; /* Programming Disabled */
  506. FLASH->CR &= ~FLASH_CR_EOPIE; /* Reset FLASH_EOPIE */
  507. return (0); /* Done */
  508. }
  509. #endif /* FLASH_OB */
  510. /*
  511. * Verify Flash Contents
  512. * Parameter: adr: Start Address
  513. * sz: Size (in bytes)
  514. * buf: Data
  515. * Return Value: (adr+sz) - OK, Failed Address
  516. */
  517. #ifdef Flash_OB
  518. unsigned long Verify(unsigned long adr, unsigned long sz, unsigned char *buf)
  519. {
  520. u32 optr;
  521. u32 sdkr;
  522. u32 wrpr;
  523. optr = *((u32 *)(buf + 0x00));
  524. sdkr = *((u32 *)(buf + 0x08));
  525. wrpr = *((u32 *)(buf + 0x18));
  526. if (M32(adr + 0x00) != optr)
  527. {
  528. return (adr + 0x00);
  529. }
  530. if (M32(adr + 0x08) != sdkr)
  531. {
  532. return (adr + 0x08);
  533. }
  534. if (M32(adr + 0x18) != wrpr)
  535. {
  536. return (adr + 0x18);
  537. }
  538. return (adr + sz);
  539. }
  540. #endif /* Flash_OB */