py32f002b_hal_flash.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /**
  2. ******************************************************************************
  3. * @file py32f002b_hal_flash.h
  4. * @author MCU Application Team
  5. * @brief Header file of FLASH HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 Puya Semiconductor Co.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by Puya under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. * @attention
  19. *
  20. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  21. * All rights reserved.</center></h2>
  22. *
  23. * This software component is licensed by ST under BSD 3-Clause license,
  24. * the "License"; You may not use this file except in compliance with the
  25. * License. You may obtain a copy of the License at:
  26. * opensource.org/licenses/BSD-3-Clause
  27. *
  28. ******************************************************************************
  29. */
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __PY32F002B_HAL_FLASH_H
  32. #define __PY32F002B_HAL_FLASH_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f002b_hal_def.h"
  38. /** @addtogroup PY32F002B_HAL_Driver
  39. * @{
  40. */
  41. /** @addtogroup FLASH
  42. * @{
  43. */
  44. /* Exported types ------------------------------------------------------------*/
  45. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  46. * @{
  47. */
  48. /**
  49. * @brief FLASH Erase structure definition
  50. */
  51. typedef struct
  52. {
  53. uint32_t TypeErase; /*!< Mass erase or page erase.
  54. This parameter can be a value of @ref FLASH_Type_Erase */
  55. uint32_t PageAddress; /*!< PageAdress: Initial FLASH page address to erase when mass erase and sector erase is disabled
  56. This parameter must be a number between Min_Data = FLASH_BASE and Max_Data = FLASH_END */
  57. uint32_t NbPages; /*!< Number of pages to be erased.
  58. This parameter must be a value between 1 and (FLASH_PAGE_NB - value of initial page)*/
  59. uint32_t SectorAddress; /*!< PageAdress: Initial FLASH page address to erase when mass erase and page erase is disabled
  60. This parameter must be a number between Min_Data = FLASH_BASE and Max_Data = FLASH_BANK1_END */
  61. uint32_t NbSectors; /*!< Number of sectors to be erased.
  62. This parameter must be a value between 1 and (FLASH_SECTOR_NB - value of initial sector)*/
  63. } FLASH_EraseInitTypeDef;
  64. /**
  65. * @brief FLASH Option Bytes PROGRAM structure definition
  66. */
  67. typedef struct
  68. {
  69. uint32_t OptionType; /*!< OptionType: Option byte to be configured.
  70. This parameter can be a value of @ref FLASH_Option_Type */
  71. uint32_t WRPSector; /*!< WRPSector: This bitfield specifies the sector (s) which are write protected.
  72. This parameter can be a combination of @ref FLASH_Option_Bytes_Write_Protection */
  73. uint32_t SDKStartAddr; /*!< SDK Start address (used for FLASH_SDKR). It represents first address of start block
  74. to protect. Make sure this parameter is multiple of SDK granularity: 2048 Bytes.*/
  75. uint32_t SDKEndAddr; /*!< SDK End address (used for FLASH_SDKR). It represents first address of end block
  76. to protect. Make sure this parameter is multiple of SDK granularity: 2048 Bytes.*/
  77. uint32_t USERType; /*!< User option byte(s) to be configured (used for OPTIONBYTE_USER).
  78. This parameter can be a combination of @ref FLASH_OB_USER_Type */
  79. uint32_t USERConfig; /*!< Value of the user option byte (used for OPTIONBYTE_USER).
  80. This parameter can be a combination of
  81. @ref FLASH_OB_USER_BOR_ENABLE,
  82. @ref FLASH_OB_USER_BOR_LEVEL,
  83. @ref FLASH_OB_USER_IWDG_SW,
  84. @ref FLASH_OB_USER_IWDG_STOP,
  85. @ref FLASH_OB_USER_SWD_NRST */
  86. } FLASH_OBProgramInitTypeDef;
  87. /**
  88. * @brief FLASH Option Bytes PROGRAM structure definition
  89. */
  90. typedef struct
  91. {
  92. uint32_t BOOTType; /*!< BOOTType: Option byte to be configured.
  93. This parameter can be a value of @ref FLASH_Option_BOOT_TYPE */
  94. uint32_t BOOTSize; /*!< BOOTSize: This bitfield specifies the load boot size.
  95. This parameter can be a value of @ref FLASH_Option_BOOT_SIZE */
  96. } FLASH_OBBootProgramInitTypeDef;
  97. /**
  98. * @brief FLASH handle Structure definition
  99. */
  100. typedef struct
  101. {
  102. HAL_LockTypeDef Lock; /* FLASH locking object */
  103. uint32_t ErrorCode; /* FLASH error code */
  104. uint32_t ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
  105. uint32_t Address; /* Internal variable to save address selected for program in IT context */
  106. uint32_t PageOrSector; /* Internal variable to define the current page or sector which is erasing in IT context */
  107. uint32_t NbPagesSectorsToErase; /* Internal variable to save the remaining pages to erase in IT context */
  108. } FLASH_ProcessTypeDef;
  109. /**
  110. * @}
  111. */
  112. /* Exported constants --------------------------------------------------------*/
  113. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  114. * @{
  115. */
  116. /** @defgroup FLASH_Latency FLASH Latency
  117. * @{
  118. */
  119. #define FLASH_LATENCY_0 0x00000000UL /*!< FLASH Zero wait state */
  120. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY /*!< FLASH One wait state */
  121. /**
  122. * @}
  123. */
  124. /** @defgroup FLASH_Type_Erase FLASH erase type
  125. * @{
  126. */
  127. #define FLASH_TYPEERASE_MASSERASE (0x01U) /*!<Flash mass erase activation*/
  128. #define FLASH_TYPEERASE_PAGEERASE (0x02U) /*!<Flash Pages erase activation*/
  129. #define FLASH_TYPEERASE_SECTORERASE (0x03U)
  130. /**
  131. * @}
  132. */
  133. /** @defgroup FLASH_Flags FLASH Flags Definition
  134. * @{
  135. */
  136. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Operation Busy flag */
  137. #define FLASH_FLAG_OPTVERR FLASH_SR_OPTVERR /*!< FLASH Option validity error flag */
  138. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protection error flag */
  139. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of operation flag */
  140. #define FLASH_FLAG_ALL_ERRORS (FLASH_FLAG_WRPERR | FLASH_FLAG_OPTVERR)
  141. /**
  142. * @}
  143. */
  144. /** @defgroup FLASH_Interrupt_definition FLASH Interrupts Definition
  145. * @brief FLASH Interrupt definition
  146. * @{
  147. */
  148. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  149. #define FLASH_IT_OPERR FLASH_CR_ERRIE /*!< Error Interrupt source */
  150. /**
  151. * @}
  152. */
  153. /** @defgroup FLASH_Error FLASH Error
  154. * @{
  155. */
  156. #define HAL_FLASH_ERROR_NONE 0x00000000U
  157. #define HAL_FLASH_ERROR_WRP FLASH_FLAG_WRPERR
  158. #define HAL_FLASH_ERROR_OPTV FLASH_FLAG_OPTVERR
  159. /**
  160. * @}
  161. */
  162. /** @defgroup FLASH_PROGRAM_ERASE_CLOCK FLASH Program and Erase Clock
  163. * @{
  164. */
  165. #define FLASH_PROGRAM_ERASE_CLOCK_24MHZ 0x00000004U /*!< 24MHz */
  166. #if defined(RCC_HSI48M_SUPPORT)
  167. #define FLASH_PROGRAM_ERASE_CLOCK_48MHZ 0x00000005U /*!< 48MHz */
  168. #endif
  169. /**
  170. * @}
  171. */
  172. /** @defgroup FLASH_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
  173. * @{
  174. */
  175. #define OB_WRP_SECTOR_0 ((uint32_t)0x00000001U) /* Write protection of Sector0 */
  176. #define OB_WRP_SECTOR_1 ((uint32_t)0x00000002U) /* Write protection of Sector1 */
  177. #define OB_WRP_SECTOR_2 ((uint32_t)0x00000004U) /* Write protection of Sector2 */
  178. #define OB_WRP_SECTOR_3 ((uint32_t)0x00000008U) /* Write protection of Sector3 */
  179. #define OB_WRP_SECTOR_4 ((uint32_t)0x00000010U) /* Write protection of Sector4 */
  180. #define OB_WRP_SECTOR_5 ((uint32_t)0x00000020U) /* Write protection of Sector5 */
  181. #define OB_WRP_Pages0to31 ((uint32_t)0x00000001U) /* Write protection from page0 to page31 */
  182. #define OB_WRP_Pages32to63 ((uint32_t)0x00000002U) /* Write protection from page32 to page63 */
  183. #define OB_WRP_Pages64to95 ((uint32_t)0x00000004U) /* Write protection from page64 to page95 */
  184. #define OB_WRP_Pages96to127 ((uint32_t)0x00000008U) /* Write protection from page96 to page127 */
  185. #define OB_WRP_Pages128to159 ((uint32_t)0x00000010U) /* Write protection from page128 to page159 */
  186. #define OB_WRP_Pages160to191 ((uint32_t)0x00000020U) /* Write protection from page160 to page191 */
  187. #define OB_WRP_AllPages ((uint32_t)0x0000003FU) /*!< Write protection of all Sectors */
  188. /**
  189. * @}
  190. */
  191. /** @defgroup FLASH_OB_USER_Type FLASH User Option Type
  192. * @{
  193. */
  194. #define OB_USER_BOR_EN FLASH_OPTR_BOR_EN
  195. #define OB_USER_BOR_LEV FLASH_OPTR_BOR_LEV
  196. #define OB_USER_IWDG_SW FLASH_OPTR_IWDG_SW
  197. #define OB_USER_SWD_NRST_MODE (FLASH_OPTR_SWD_MODE | FLASH_OPTR_NRST_MODE)
  198. #define OB_USER_IWDG_STOP FLASH_OPTR_IWDG_STOP
  199. #define OB_USER_ALL (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_IWDG_SW | \
  200. OB_USER_SWD_NRST_MODE | OB_USER_IWDG_STOP)
  201. /**
  202. * @}
  203. */
  204. /** @defgroup FLASH_Type_Program FLASH type program
  205. * @{
  206. */
  207. #define FLASH_TYPEPROGRAM_PAGE (0x01U) /*!<Program 128bytes at a specified address.*/
  208. /**
  209. * @}
  210. */
  211. /** @defgroup FLASH_OB_USER_BOR_ENABLE FLASH Option Bytes BOR Enable
  212. * @{
  213. */
  214. #define OB_BOR_DISABLE 0x00000000U /*!< BOR Reset set to default */
  215. #define OB_BOR_ENABLE FLASH_OPTR_BOR_EN /*!< Use option byte to define BOR thresholds */
  216. /**
  217. * @}
  218. */
  219. /** @defgroup FLASH_OB_USER_BOR_LEVEL FLASH Option Bytes BOR Level
  220. * @{
  221. */
  222. #define OB_BOR_LEVEL_1p7_1p8 ((uint32_t)0x0000U) /*!< BOR Reset threshold levels for 1.7V - 1.8V VDD power supply */
  223. #define OB_BOR_LEVEL_1p9_2p0 ((uint32_t)FLASH_OPTR_BOR_LEV_0) /*!< BOR Reset threshold levels for 1.9V - 2.0V VDD power supply */
  224. #define OB_BOR_LEVEL_2p1_2p2 ((uint32_t)FLASH_OPTR_BOR_LEV_1) /*!< BOR Reset threshold levels for 2.1V - 2.2V VDD power supply */
  225. #define OB_BOR_LEVEL_2p3_2p4 ((uint32_t)(FLASH_OPTR_BOR_LEV_0 | FLASH_OPTR_BOR_LEV_1)) /*!< BOR Reset threshold levels for 2.3V - 2.4V VDD power supply */
  226. #define OB_BOR_LEVEL_2p5_2p6 ((uint32_t)FLASH_OPTR_BOR_LEV_2) /*!< BOR Reset threshold levels for 2.5V - 2.6V VDD power supply */
  227. #define OB_BOR_LEVEL_2p7_2p8 ((uint32_t)(FLASH_OPTR_BOR_LEV_0 | FLASH_OPTR_BOR_LEV_2)) /*!< BOR Reset threshold levels for 2.7V - 2.8V VDD power supply */
  228. #define OB_BOR_LEVEL_2p9_3p0 ((uint32_t)(FLASH_OPTR_BOR_LEV_1 | FLASH_OPTR_BOR_LEV_2)) /*!< BOR Reset threshold levels for 2.9V - 3.0V VDD power supply */
  229. #define OB_BOR_LEVEL_3p1_3p2 ((uint32_t)(FLASH_OPTR_BOR_LEV_0 | FLASH_OPTR_BOR_LEV_1 | FLASH_OPTR_BOR_LEV_2)) /*!< BOR Reset threshold levels for 3.1V - 3.2V VDD power supply */
  230. /**
  231. * @}
  232. */
  233. /** @defgroup FLASH_WRP_State FLASH WRP State
  234. * @{
  235. */
  236. #define OB_WRPSTATE_DISABLE ((uint32_t)0x00U) /*!<Disable the write protection of the desired sectors*/
  237. #define OB_WRPSTATE_ENABLE ((uint32_t)0x01U) /*!<Enable the write protection of the desired sectors*/
  238. /**
  239. * @}
  240. */
  241. /** @defgroup FLASH_OB_USER_IWDG_SW FLASH Option Bytes IWatchdog
  242. * @{
  243. */
  244. #define OB_IWDG_SW FLASH_OPTR_IWDG_SW /*!< Software IWDG selected */
  245. #define OB_IWDG_HW 0x00000000U /*!< Hardware IWDG selected */
  246. /**
  247. * @}
  248. */
  249. /** @defgroup FLASH_OB_USER_IWDG_STOP FLASH IWDG Counter Freeze in STOP
  250. * @{
  251. */
  252. #define OB_IWDG_STOP_FREEZE 0x00000000U /*!< Freeze IWDG counter in STOP mode */
  253. #define OB_IWDG_STOP_ACTIVE ((uint32_t)FLASH_OPTR_IWDG_STOP) /*!< IWDG counter active in STOP mode */
  254. /**
  255. * @}
  256. */
  257. /** @defgroup FLASH_OB_USER_SWD_NRST FLASH Option Bytes SWD_NRST
  258. * @{
  259. */
  260. #define OB_SWD_PB6_NRST_PC0 0x00000000U /*!< PB6:SWD,PC0:NRST */
  261. #define OB_SWD_PB6_GPIO_PC0 FLASH_OPTR_NRST_MODE /*!< PB6:SWD,PC0:GPIO */
  262. #define OB_SWD_PC0_GPIO_PB6 (FLASH_OPTR_NRST_MODE | FLASH_OPTR_SWD_MODE ) /*!< PC0:SWD,PB6:GPIO */
  263. /**
  264. * @}
  265. */
  266. /** @defgroup FLASH_Option_Type FLASH Option Type
  267. * @{
  268. */
  269. #define OPTIONBYTE_WRP ((uint32_t)0x01U) /*!<WRP option byte configuration*/
  270. #define OPTIONBYTE_SDK ((uint32_t)0x02U) /*!<SDK option byte configuration*/
  271. #define OPTIONBYTE_USER ((uint32_t)0x08U) /*!<USER option byte configuration*/
  272. #define OPTIONBYTE_ALL (OPTIONBYTE_WRP | \
  273. OPTIONBYTE_SDK | \
  274. OPTIONBYTE_USER)
  275. /**
  276. * @}
  277. */
  278. /** @defgroup FLASH_Option_BOOT_TYPE FLASH Option Bytes BOOT TYPE
  279. * @{
  280. */
  281. #define OB_BOOT_MODE_MAINFLASH ((uint32_t)0x00000000U) /*!< BOOT from MainFlash */
  282. #define OB_BOOT_MODE_SRAM ((uint32_t)FLASH_BTCR_BOOT0) /*!< BOOT from SRAM */
  283. #define OB_BOOT_MODE_LOADFLASH ((uint32_t)(FLASH_BTCR_BOOT0 | FLASH_BTCR_NBOOT1)) /*!< BOOT From LOADFLASH */
  284. /**
  285. * @}
  286. */
  287. /** @defgroup FLASH_Option_BOOT_SIZE FLASH Option Bytes BOOT SIZE
  288. * @{
  289. */
  290. #define OB_BOOT_SIZE_NONE ((uint32_t)0x00000000U) /*!< LOAD FLASH SIZE :0K */
  291. #define OB_BOOT_SIZE_1K ((uint32_t)FLASH_BTCR_BOOT_SIZE_0) /*!< LOAD FLASH SIZE :1K*/
  292. #define OB_BOOT_SIZE_2K ((uint32_t)FLASH_BTCR_BOOT_SIZE_1) /*!< LOAD FLASH SIZE :2K*/
  293. #define OB_BOOT_SIZE_3K ((uint32_t)FLASH_BTCR_BOOT_SIZE_0 | FLASH_BTCR_BOOT_SIZE_1) /*!< LOAD FLASH SIZE :3K*/
  294. #define OB_BOOT_SIZE_4K ((uint32_t)FLASH_BTCR_BOOT_SIZE_2) /*!< LOAD FLASH SIZE :4K*/
  295. /**
  296. * @}
  297. */
  298. /**
  299. * @}
  300. */
  301. /* Exported macros -----------------------------------------------------------*/
  302. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  303. * @brief macros to control FLASH features
  304. * @{
  305. */
  306. /**
  307. * @brief Set the FLASH Latency.
  308. * @param __LATENCY__ FLASH Latency
  309. * This parameter can be one of the following values :
  310. * @arg @ref FLASH_LATENCY_0 FLASH Zero wait state
  311. * @arg @ref FLASH_LATENCY_1 FLASH One wait state
  312. * @retval None
  313. */
  314. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (__LATENCY__))
  315. /**
  316. * @brief Get the FLASH Latency.
  317. * @retval FLASH Latency
  318. * Returned value can be one of the following values :
  319. * @arg @ref FLASH_LATENCY_0 FLASH Zero wait state
  320. * @arg @ref FLASH_LATENCY_1 FLASH One wait state
  321. *
  322. */
  323. #define __HAL_FLASH_GET_LATENCY() READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY)
  324. /** @defgroup FLASH_Interrupt FLASH Interrupts Macros
  325. * @brief macros to handle FLASH interrupts
  326. * @{
  327. */
  328. /**
  329. * @brief Enable the specified FLASH interrupt.
  330. * @param __INTERRUPT__ FLASH interrupt
  331. * This parameter can be any combination of the following values:
  332. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  333. * @arg @ref FLASH_IT_OPERR Error Interrupt
  334. * @note (*) availability depends on devices
  335. * @retval none
  336. */
  337. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) SET_BIT(FLASH->CR, (__INTERRUPT__))
  338. /**
  339. * @brief Disable the specified FLASH interrupt.
  340. * @param __INTERRUPT__ FLASH interrupt
  341. * This parameter can be any combination of the following values:
  342. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  343. * @arg @ref FLASH_IT_OPERR Error Interrupt
  344. * @note (*) availability depends on devices
  345. * @retval none
  346. */
  347. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) CLEAR_BIT(FLASH->CR, (__INTERRUPT__))
  348. /**
  349. * @brief Check whether the specified FLASH flag is set or not.
  350. * @param __FLAG__ specifies the FLASH flag to check.
  351. * This parameter can be one of the following values:
  352. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  353. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  354. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  355. * @arg @ref FLASH_FLAG_BSY FLASH write/erase operations in progress flag
  356. * @arg @ref FLASH_FLAG_ALL_ERRORS FLASH All errors flags
  357. * @note (*) availability depends on devices
  358. * @retval The new state of FLASH_FLAG (SET or RESET).
  359. */
  360. #define __HAL_FLASH_GET_FLAG(__FLAG__) (READ_BIT(FLASH->SR, (__FLAG__)) == (__FLAG__))
  361. /**
  362. * @brief Clear the FLASHs pending flags.
  363. * @param __FLAG__ specifies the FLASH flags to clear.
  364. * This parameter can be any combination of the following values:
  365. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  366. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  367. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  368. * @arg @ref FLASH_FLAG_ALL_ERRORS FLASH All errors flags
  369. * @retval None
  370. */
  371. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { WRITE_REG(FLASH->SR, (__FLAG__)); \
  372. } while(0U)
  373. #define __HAL_FLASH_TIME_REG_SET(__EPPARA0__,__EPPARA1__,__EPPARA2__,__EPPARA3__,__EPPARA4__) \
  374. do { \
  375. FLASH->TS0 = (__EPPARA0__)&0x1FF; \
  376. FLASH->TS1 = ((__EPPARA0__)>>18)&0x3FF; \
  377. FLASH->TS3 = ((__EPPARA0__)>>9)&0x1FF; \
  378. FLASH->TS2P = (__EPPARA1__)&0x1FF; \
  379. FLASH->TPS3 = ((__EPPARA1__)>>16)&0xFFF; \
  380. FLASH->PERTPE = (__EPPARA2__)&0x3FFFF; \
  381. FLASH->SMERTPE = (__EPPARA3__)&0x3FFFF; \
  382. FLASH->PRGTPE = (__EPPARA4__)&0xFFFF; \
  383. FLASH->PRETPE = ((__EPPARA4__)>>16)&0x3FFF; \
  384. } while(0U)
  385. #define __HAL_FLASH_IS_INVALID_TIMMING_SEQUENCE(_INDEX_) (((FLASH->TS0) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))&0x1FF)) || \
  386. ((FLASH->TS1) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))>>18)&0x3FF)) || \
  387. ((FLASH->TS3) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))>>9)&0x1FF)) || \
  388. ((FLASH->TS2P) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+4))&0x1FF)) || \
  389. ((FLASH->TPS3) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+4))>>16)&0xFFF)) || \
  390. ((FLASH->PERTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+8))&0x3FFFF)) || \
  391. ((FLASH->SMERTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+12))&0x3FFFF)) || \
  392. ((FLASH->PRGTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+16))&0xFFFF)) || \
  393. ((FLASH->PRETPE) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+16))>>16)&0x3FFF)))
  394. #define __HAL_FLASH_TIMMING_SEQUENCE_CONFIG() do{ \
  395. uint32_t tmpreg = (RCC->ICSCR & RCC_ICSCR_HSI_FS) >> RCC_ICSCR_HSI_FS_Pos; \
  396. if (__HAL_FLASH_IS_INVALID_TIMMING_SEQUENCE(tmpreg)) \
  397. { \
  398. __HAL_FLASH_TIME_REG_SET((*(uint32_t *)(_FlashTimmingParam[tmpreg])), \
  399. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+4)), \
  400. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+8)), \
  401. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+12)), \
  402. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+16))); \
  403. } \
  404. }while(0U)
  405. /**
  406. * @}
  407. */
  408. /**
  409. * @}
  410. */
  411. /* Include FLASH HAL Extended module */
  412. /* Exported variables --------------------------------------------------------*/
  413. /** @defgroup FLASH_Exported_Variables FLASH Exported Variables
  414. * @{
  415. */
  416. extern FLASH_ProcessTypeDef pFlash;
  417. /**
  418. * @}
  419. */
  420. /* Exported functions --------------------------------------------------------*/
  421. /** @addtogroup FLASH_Exported_Functions
  422. * @{
  423. */
  424. /* Program operation functions ***********************************************/
  425. /** @addtogroup FLASH_Exported_Functions_Group1
  426. * @{
  427. */
  428. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t *DataAddr );
  429. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t *DataAddr);
  430. HAL_StatusTypeDef HAL_FLASH_PageProgram(uint32_t Address, uint32_t *DataAddr );
  431. HAL_StatusTypeDef HAL_FLASH_PageProgram_IT(uint32_t Address, uint32_t *DataAddr);
  432. /* FLASH IRQ handler method */
  433. void HAL_FLASH_IRQHandler(void);
  434. /* Callbacks in non blocking modes */
  435. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  436. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  437. HAL_StatusTypeDef HAL_FLASH_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError);
  438. HAL_StatusTypeDef HAL_FLASH_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit);
  439. #define HAL_FLASHEx_Erase HAL_FLASH_Erase
  440. #define HAL_FLASHEx_Erase_IT HAL_FLASH_Erase_IT
  441. /**
  442. * @}
  443. */
  444. /* Peripheral Control functions **********************************************/
  445. /** @addtogroup FLASH_Exported_Functions_Group2
  446. * @{
  447. */
  448. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  449. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  450. /* Option bytes control */
  451. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  452. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  453. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  454. HAL_StatusTypeDef HAL_FLASH_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  455. void HAL_FLASH_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  456. HAL_StatusTypeDef HAL_FLASH_OBBOOTProgram(FLASH_OBBootProgramInitTypeDef *pBOOTInit);
  457. void HAL_FLASH_OBBOOTGetConfig(FLASH_OBBootProgramInitTypeDef *pBOOTInit);
  458. /**
  459. * @}
  460. */
  461. /* Peripheral State functions ************************************************/
  462. /** @addtogroup FLASH_Exported_Functions_Group3
  463. * @{
  464. */
  465. uint32_t HAL_FLASH_GetError(void);
  466. /**
  467. * @}
  468. */
  469. /**
  470. * @}
  471. */
  472. /* Private types --------------------------------------------------------*/
  473. /** @defgroup FLASH_Private_types FLASH Private Types
  474. * @{
  475. */
  476. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  477. /**
  478. * @}
  479. */
  480. /* Private constants --------------------------------------------------------*/
  481. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  482. * @{
  483. */
  484. #define FLASH_TIMEOUT_VALUE 1000U /*!< FLASH Execution Timeout, 1 s */
  485. #define FLASH_TYPENONE 0x00000000U /*!< No Programmation Procedure On Going */
  486. #define FLASH_FLAG_SR_ERROR (FLASH_FLAG_OPTVERR | FLASH_FLAG_WRPERR) /*!< All SR error flags */
  487. #define FLASH_FLAG_SR_CLEAR (FLASH_FLAG_SR_ERROR | FLASH_SR_EOP)
  488. /**
  489. * @}
  490. */
  491. /* Private macros ------------------------------------------------------------*/
  492. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  493. * @{
  494. */
  495. #define IS_FLASH_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  496. #define IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 8UL)))
  497. #define IS_FLASH_PROGRAM_ADDRESS(__ADDRESS__) (IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__))
  498. #define IS_FLASH_NB_PAGES(__ADDRESS__, __VALUE__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__ + (__VALUE__*FLASH_PAGE_SIZE)) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  499. #define IS_FLASH_NB_SECTORS(__ADDRESS__, __VALUE__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__ + (__VALUE__*FLASH_SECTOR_SIZE)) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  500. #define IS_FLASH_FAST_PROGRAM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 256UL)))
  501. #define IS_FLASH_PAGE(__PAGE__) ((__PAGE__) < FLASH_PAGE_NB)
  502. #define IS_FLASH_BANK(__BANK__) ((__BANK__) == 0x00UL)
  503. #define IS_FLASH_TYPEERASE(__VALUE__) (((__VALUE__) == FLASH_TYPEERASE_PAGEERASE) || \
  504. ((__VALUE__) == FLASH_TYPEERASE_SECTORERASE) || \
  505. ((__VALUE__) == FLASH_TYPEERASE_MASSERASE))
  506. #define IS_FLASH_TYPEPROGRAM(__VALUE__) ((__VALUE__) == FLASH_TYPEPROGRAM_PAGE)
  507. #define IS_FLASH_TIMECONFIG_CLOCK(__VALUE__) (((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_24MHZ) || \
  508. ((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_48MHZ))
  509. #define IS_OPTIONBYTE(__VALUE__) ((((__VALUE__) & OPTIONBYTE_ALL) != 0x00U) && \
  510. (((__VALUE__) & ~OPTIONBYTE_ALL) == 0x00U))
  511. #define IS_OB_BOOTTYPE(__VALUE__) (((__VALUE__) == OB_BOOT_MODE_MAINFLASH) || \
  512. ((__VALUE__) == OB_BOOT_MODE_SRAM) || \
  513. ((__VALUE__) == OB_BOOT_MODE_LOADFLASH))
  514. #define IS_OB_BOOTSIZE(__VALUE__) (((__VALUE__) == OB_BOOT_SIZE_NONE) || \
  515. ((__VALUE__) == OB_BOOT_SIZE_1K) || \
  516. ((__VALUE__) == OB_BOOT_SIZE_2K) || \
  517. ((__VALUE__) == OB_BOOT_SIZE_3K) || \
  518. ((__VALUE__) == OB_BOOT_SIZE_4K))
  519. #define IS_OB_USER_TYPE(__TYPE__) ((((__TYPE__) & OB_USER_ALL) != 0x00U) && \
  520. (((__TYPE__) & ~OB_USER_ALL) == 0x00U))
  521. #define IS_OB_USER_CONFIG(__TYPE__,__CONFIG__) ((~(__TYPE__) & (__CONFIG__)) == 0x00U)
  522. #if defined(FLASH_PCROP_SUPPORT)
  523. #define IS_OB_PCROP_CONFIG(__CONFIG__) (((__CONFIG__) & ~(OB_PCROP_ZONE_A | OB_PCROP_ZONE_B | OB_PCROP_RDP_ERASE)) == 0x00U)
  524. #endif
  525. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  526. #define IS_OB_SEC_BOOT_LOCK(__VALUE__) (((__VALUE__) == OB_BOOT_ENTRY_FORCED_NONE) || ((__VALUE__) == OB_BOOT_ENTRY_FORCED_FLASH))
  527. #define IS_OB_SEC_SIZE(__VALUE__) ((__VALUE__) < (FLASH_PAGE_NB + 1U))
  528. #endif
  529. #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
  530. ((__LATENCY__) == FLASH_LATENCY_1))
  531. #define IS_WRPSTATE(__VALUE__) (((__VALUE__) == OB_WRPSTATE_DISABLE) || \
  532. ((__VALUE__) == OB_WRPSTATE_ENABLE))
  533. /**
  534. * @}
  535. */
  536. /**
  537. * @}
  538. */
  539. /**
  540. * @}
  541. */
  542. #ifdef __cplusplus
  543. }
  544. #endif
  545. #endif /* __PY32F002B_HAL_FLASH_H */
  546. /************************ (C) COPYRIGHT Puya *****END OF FILE****/