py32f0xx_hal_flash.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /**
  2. ******************************************************************************
  3. * @file py32f0xx_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 __PY32F0xx_HAL_FLASH_H
  32. #define __PY32F0xx_HAL_FLASH_H
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "py32f0xx_hal_def.h"
  38. /** @addtogroup PY32F0xx_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 RDPLevel; /*!< RDPLevel: Set the read protection level.
  78. This parameter can be a value of @ref FLASH_OB_Read_Protection */
  79. uint32_t USERType; /*!< User option byte(s) to be configured (used for OPTIONBYTE_USER).
  80. This parameter can be a combination of @ref FLASH_OB_USER_Type */
  81. uint32_t USERConfig; /*!< Value of the user option byte (used for OPTIONBYTE_USER).
  82. This parameter can be a combination of
  83. @ref FLASH_OB_USER_BOR_ENABLE,
  84. @ref FLASH_OB_USER_BOR_LEVEL,
  85. @ref FLASH_OB_USER_RESET_CONFIG,
  86. @ref FLASH_OB_USER_IWDG_SW,
  87. @ref FLASH_OB_USER_WWDG_SW,
  88. @ref FLASH_OB_USER_nBOOT1 */
  89. } FLASH_OBProgramInitTypeDef;
  90. /**
  91. * @brief FLASH handle Structure definition
  92. */
  93. typedef struct
  94. {
  95. HAL_LockTypeDef Lock; /* FLASH locking object */
  96. uint32_t ErrorCode; /* FLASH error code */
  97. uint32_t ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
  98. uint32_t Address; /* Internal variable to save address selected for program in IT context */
  99. uint32_t PageOrSector; /* Internal variable to define the current page or sector which is erasing in IT context */
  100. uint32_t NbPagesSectorsToErase; /* Internal variable to save the remaining pages to erase in IT context */
  101. } FLASH_ProcessTypeDef;
  102. /**
  103. * @}
  104. */
  105. /* Exported constants --------------------------------------------------------*/
  106. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  107. * @{
  108. */
  109. /** @defgroup FLASH_Latency FLASH Latency
  110. * @{
  111. */
  112. #define FLASH_LATENCY_0 0x00000000UL /*!< FLASH Zero wait state */
  113. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY /*!< FLASH One wait state */
  114. /**
  115. * @}
  116. */
  117. /** @defgroup FLASH_Type_Erase FLASH erase type
  118. * @{
  119. */
  120. #define FLASH_TYPEERASE_MASSERASE FLASH_CR_MER /*!<Flash mass erase activation*/
  121. #define FLASH_TYPEERASE_PAGEERASE FLASH_CR_PER /*!<Flash Pages erase activation*/
  122. #define FLASH_TYPEERASE_SECTORERASE FLASH_CR_SER /*!<Flash Sector erase activation*/
  123. /**
  124. * @}
  125. */
  126. /** @defgroup FLASH_Flags FLASH Flags Definition
  127. * @{
  128. */
  129. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Operation Busy flag */
  130. #define FLASH_FLAG_OPTVERR FLASH_SR_OPTVERR /*!< FLASH Option validity error flag */
  131. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protection error flag */
  132. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of operation flag */
  133. #define FLASH_FLAG_ALL_ERRORS (FLASH_FLAG_WRPERR | FLASH_FLAG_OPTVERR)
  134. /**
  135. * @}
  136. */
  137. /** @defgroup FLASH_Interrupt_definition FLASH Interrupts Definition
  138. * @brief FLASH Interrupt definition
  139. * @{
  140. */
  141. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  142. #define FLASH_IT_OPERR FLASH_CR_ERRIE /*!< Error Interrupt source */
  143. /**
  144. * @}
  145. */
  146. /** @defgroup FLASH_Error FLASH Error
  147. * @{
  148. */
  149. #define HAL_FLASH_ERROR_NONE 0x00000000U
  150. #define HAL_FLASH_ERROR_WRP FLASH_FLAG_WRPERR
  151. #define HAL_FLASH_ERROR_OPTV FLASH_FLAG_OPTVERR
  152. /**
  153. * @}
  154. */
  155. /** @defgroup FLASH_PROGRAM_ERASE_CLOCK FLASH Program and Erase Clock
  156. * @{
  157. */
  158. #define FLASH_PROGRAM_ERASE_CLOCK_4MHZ 0x00000000U /*!< 4MHz */
  159. #define FLASH_PROGRAM_ERASE_CLOCK_8MHZ 0x00000001U /*!< 8MHz */
  160. #define FLASH_PROGRAM_ERASE_CLOCK_16MHZ 0x00000002U /*!< 16MHz */
  161. #define FLASH_PROGRAM_ERASE_CLOCK_22p12MHZ 0x00000003U /*!< 22.12MHz */
  162. #define FLASH_PROGRAM_ERASE_CLOCK_24MHZ 0x00000004U /*!< 24MHz */
  163. /**
  164. * @}
  165. */
  166. /** @defgroup FLASH_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
  167. * @{
  168. */
  169. #define OB_WRP_SECTOR_0 ((uint32_t)0x00000001U) /* Write protection of Sector0 */
  170. #define OB_WRP_SECTOR_1 ((uint32_t)0x00000002U) /* Write protection of Sector1 */
  171. #define OB_WRP_SECTOR_2 ((uint32_t)0x00000004U) /* Write protection of Sector2 */
  172. #define OB_WRP_SECTOR_3 ((uint32_t)0x00000008U) /* Write protection of Sector3 */
  173. #define OB_WRP_SECTOR_4 ((uint32_t)0x00000010U) /* Write protection of Sector4 */
  174. #define OB_WRP_SECTOR_5 ((uint32_t)0x00000020U) /* Write protection of Sector5 */
  175. #define OB_WRP_SECTOR_6 ((uint32_t)0x00000040U) /* Write protection of Sector6 */
  176. #define OB_WRP_SECTOR_7 ((uint32_t)0x00000080U) /* Write protection of Sector7 */
  177. #define OB_WRP_SECTOR_8 ((uint32_t)0x00000100U) /* Write protection of Sector8 */
  178. #define OB_WRP_SECTOR_9 ((uint32_t)0x00000200U) /* Write protection of Sector9 */
  179. #define OB_WRP_SECTOR_10 ((uint32_t)0x00000400U) /* Write protection of Sector10 */
  180. #define OB_WRP_SECTOR_11 ((uint32_t)0x00000800U) /* Write protection of Sector11 */
  181. #define OB_WRP_SECTOR_12 ((uint32_t)0x00001000U) /* Write protection of Sector12 */
  182. #define OB_WRP_SECTOR_13 ((uint32_t)0x00002000U) /* Write protection of Sector13 */
  183. #define OB_WRP_SECTOR_14 ((uint32_t)0x00004000U) /* Write protection of Sector14 */
  184. #define OB_WRP_SECTOR_15 ((uint32_t)0x00008000U) /* Write protection of Sector15 */
  185. #define OB_WRP_Pages0to31 ((uint32_t)0x00000001U) /* Write protection from page0 to page31 */
  186. #define OB_WRP_Pages32to63 ((uint32_t)0x00000002U) /* Write protection from page32 to page63 */
  187. #define OB_WRP_Pages64to95 ((uint32_t)0x00000004U) /* Write protection from page64 to page95 */
  188. #define OB_WRP_Pages96to127 ((uint32_t)0x00000008U) /* Write protection from page96 to page127 */
  189. #define OB_WRP_Pages128to159 ((uint32_t)0x00000010U) /* Write protection from page128 to page159 */
  190. #define OB_WRP_Pages160to191 ((uint32_t)0x00000020U) /* Write protection from page160 to page191 */
  191. #define OB_WRP_Pages192to223 ((uint32_t)0x00000040U) /* Write protection from page192 to page223 */
  192. #define OB_WRP_Pages224to255 ((uint32_t)0x00000080U) /* Write protection from page224 to page255 */
  193. #define OB_WRP_Pages256to287 ((uint32_t)0x00000100U) /* Write protection from page256 to page287 */
  194. #define OB_WRP_Pages288to319 ((uint32_t)0x00000200U) /* Write protection from page288 to page319 */
  195. #define OB_WRP_Pages320to351 ((uint32_t)0x00000400U) /* Write protection from page320 to page351 */
  196. #define OB_WRP_Pages352to383 ((uint32_t)0x00000800U) /* Write protection from page352 to page383 */
  197. #define OB_WRP_Pages384to415 ((uint32_t)0x00001000U) /* Write protection from page384 to page415 */
  198. #define OB_WRP_Pages416to447 ((uint32_t)0x00002000U) /* Write protection from page416 to page447 */
  199. #define OB_WRP_Pages448to479 ((uint32_t)0x00004000U) /* Write protection from page448 to page479 */
  200. #define OB_WRP_Pages480to511 ((uint32_t)0x00008000U) /* Write protection from page480 to page511 */
  201. #define OB_WRP_AllPages ((uint32_t)0x0000FFFFU) /*!< Write protection of all Sectors */
  202. /**
  203. * @}
  204. */
  205. /** @defgroup FLASH_OB_Read_Protection FLASH Option Bytes Read Protection
  206. * @{
  207. */
  208. #define OB_RDP_LEVEL_0 ((uint8_t)0xAAU)
  209. #define OB_RDP_LEVEL_1 ((uint8_t)0x55U)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup FLASH_OB_USER_Type FLASH User Option Type
  214. * @{
  215. */
  216. #define OB_USER_BOR_EN FLASH_OPTR_BOR_EN
  217. #define OB_USER_BOR_LEV FLASH_OPTR_BOR_LEV
  218. #define OB_USER_IWDG_SW FLASH_OPTR_IWDG_SW
  219. #if defined(FLASH_OPTR_WWDG_SW)
  220. #define OB_USER_WWDG_SW FLASH_OPTR_WWDG_SW
  221. #endif
  222. #define OB_USER_NRST_MODE FLASH_OPTR_NRST_MODE
  223. #define OB_USER_nBOOT1 FLASH_OPTR_nBOOT1
  224. #if defined(FLASH_OPTR_WWDG_SW)
  225. #define OB_USER_ALL (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_IWDG_SW | \
  226. OB_USER_WWDG_SW | OB_USER_NRST_MODE | OB_USER_nBOOT1)
  227. #else
  228. #define OB_USER_ALL (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_IWDG_SW | \
  229. OB_USER_NRST_MODE | OB_USER_nBOOT1)
  230. #endif
  231. /**
  232. * @}
  233. */
  234. /** @defgroup FLASH_Type_Program FLASH type program
  235. * @{
  236. */
  237. #define FLASH_TYPEPROGRAM_PAGE FLASH_CR_PG /*!<Program 128bytes at a specified address.*/
  238. /**
  239. * @}
  240. */
  241. /** @defgroup FLASH_OB_USER_BOR_ENABLE FLASH Option Bytes BOR Level
  242. * @{
  243. */
  244. #define OB_BOR_DISABLE 0x00000000U /*!< BOR Reset set to default */
  245. #define OB_BOR_ENABLE FLASH_OPTR_BOR_EN /*!< Use option byte to define BOR thresholds */
  246. /**
  247. * @}
  248. */
  249. /** @defgroup FLASH_OB_USER_BOR_LEVEL FLASH Option Bytes BOR Level
  250. * @{
  251. */
  252. #define OB_BOR_LEVEL_1p7_1p8 ((uint32_t)0x0000U) /*!< BOR Reset threshold levels for 1.7V - 1.8V VDD power supply */
  253. #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 */
  254. #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 */
  255. #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 */
  256. #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 */
  257. #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 */
  258. #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 */
  259. #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 */
  260. /**
  261. * @}
  262. */
  263. /** @defgroup FLASH_Option_Type FLASH Option Type
  264. * @{
  265. */
  266. #define OPTIONBYTE_WRP ((uint32_t)0x01U) /*!<WRP option byte configuration*/
  267. #define OPTIONBYTE_SDK ((uint32_t)0x02U) /*!<SDK option byte configuration*/
  268. #define OPTIONBYTE_RDP ((uint32_t)0x04U) /*!<RDP option byte configuration*/
  269. #define OPTIONBYTE_USER ((uint32_t)0x08U) /*!<USER option byte configuration*/
  270. #define OPTIONBYTE_ALL (OPTIONBYTE_WRP | \
  271. OPTIONBYTE_SDK | \
  272. OPTIONBYTE_RDP | \
  273. OPTIONBYTE_USER)
  274. /**
  275. * @}
  276. */
  277. /** @defgroup FLASH_WRP_State FLASH WRP State
  278. * @{
  279. */
  280. #define OB_WRPSTATE_DISABLE ((uint32_t)0x00U) /*!<Disable the write protection of the desired sectors*/
  281. #define OB_WRPSTATE_ENABLE ((uint32_t)0x01U) /*!<Enable the write protection of the desired sectors*/
  282. /**
  283. * @}
  284. */
  285. /** @defgroup FLASH_OB_USER_IWDG_SW FLASH Option Bytes IWatchdog
  286. * @{
  287. */
  288. #define OB_IWDG_SW FLASH_OPTR_IWDG_SW /*!< Software IWDG selected */
  289. #define OB_IWDG_HW 0x00000000U /*!< Hardware IWDG selected */
  290. /**
  291. * @}
  292. */
  293. #if defined(FLASH_OPTR_WWDG_SW)
  294. /** @defgroup FLASH_OB_USER_WWDG_SW FLASH Option Bytes WWatchdog
  295. * @{
  296. */
  297. #define OB_WWDG_SW FLASH_OPTR_WWDG_SW /*!< Software WWDG selected */
  298. #define OB_WWDG_HW 0x00000000U /*!< Hardware WWDG selected */
  299. /**
  300. * @}
  301. */
  302. #endif
  303. /** @defgroup FLASH_OB_USER_RESET_CONFIG FLASH Option Bytes User reset config bit
  304. * @{
  305. */
  306. #define OB_RESET_MODE_RESET 0x00000000U /*!< Reset pin is in Reset input mode only */
  307. #define OB_RESET_MODE_GPIO FLASH_OPTR_NRST_MODE /*!< Reset pin is in GPIO mode mode only */
  308. /**
  309. * @}
  310. */
  311. /** @defgroup FLASH_OB_USER_nBOOT1 FLASH Option Bytes BOOT Bit1 Setup
  312. * @{
  313. */
  314. #define OB_BOOT1_SRAM 0x00000000U /*!< BOOT Bit 1 Reset */
  315. #define OB_BOOT1_SYSTEM FLASH_OPTR_nBOOT1 /*!< BOOT Bit 1 Set */
  316. /**
  317. * @}
  318. */
  319. /**
  320. * @}
  321. */
  322. /* Exported macros -----------------------------------------------------------*/
  323. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  324. * @brief macros to control FLASH features
  325. * @{
  326. */
  327. /**
  328. * @brief Set the FLASH Latency.
  329. * @param __LATENCY__ FLASH Latency
  330. * This parameter can be one of the following values :
  331. *
  332. * @arg @ref FLASH_ACR_LATENCY FLASH One wait state
  333. *
  334. * @retval None
  335. */
  336. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (__LATENCY__))
  337. /**
  338. * @brief Get the FLASH Latency.
  339. * @retval FLASH Latency
  340. * Returned value can be one of the following values :
  341. * @arg @ref FLASH_LATENCY_0 FLASH Zero wait state
  342. * @arg @ref FLASH_LATENCY_1 FLASH One wait state
  343. *
  344. */
  345. #define __HAL_FLASH_GET_LATENCY() READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY)
  346. /** @defgroup FLASH_Interrupt FLASH Interrupts Macros
  347. * @brief macros to handle FLASH interrupts
  348. * @{
  349. */
  350. /**
  351. * @brief Enable the specified FLASH interrupt.
  352. * @param __INTERRUPT__ FLASH interrupt
  353. * This parameter can be any combination of the following values:
  354. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  355. * @arg @ref FLASH_IT_OPERR Error Interrupt
  356. * @note (*) availability depends on devices
  357. * @retval none
  358. */
  359. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) SET_BIT(FLASH->CR, (__INTERRUPT__))
  360. /**
  361. * @brief Disable the specified FLASH interrupt.
  362. * @param __INTERRUPT__ FLASH interrupt
  363. * This parameter can be any combination of the following values:
  364. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  365. * @arg @ref FLASH_IT_OPERR Error Interrupt
  366. * @note (*) availability depends on devices
  367. * @retval none
  368. */
  369. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) CLEAR_BIT(FLASH->CR, (__INTERRUPT__))
  370. /**
  371. * @brief Check whether the specified FLASH flag is set or not.
  372. * @param __FLAG__ specifies the FLASH flag to check.
  373. * This parameter can be one of the following values:
  374. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  375. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  376. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  377. * @arg @ref FLASH_FLAG_BSY FLASH write/erase operations in progress flag
  378. * @arg @ref FLASH_FLAG_ALL_ERRORS FLASH All errors flags
  379. * @note (*) availability depends on devices
  380. * @retval The new state of FLASH_FLAG (SET or RESET).
  381. */
  382. #define __HAL_FLASH_GET_FLAG(__FLAG__) (READ_BIT(FLASH->SR, (__FLAG__)) == (__FLAG__))
  383. /**
  384. * @brief Clear the FLASHs pending flags.
  385. * @param __FLAG__ specifies the FLASH flags to clear.
  386. * This parameter can be any combination of the following values:
  387. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  388. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  389. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  390. * @arg @ref FLASH_FLAG_ALL_ERRORS FLASH All errors flags
  391. * @retval None
  392. */
  393. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { WRITE_REG(FLASH->SR, (__FLAG__)); \
  394. } while(0U)
  395. #define __HAL_FLASH_TIME_REG_SET(__EPPARA0__,__EPPARA1__,__EPPARA2__,__EPPARA3__,__EPPARA4__) \
  396. do { \
  397. FLASH->TS0 = (__EPPARA0__)&0xFF; \
  398. FLASH->TS1 = ((__EPPARA0__)>>16)&0x1FF; \
  399. FLASH->TS3 = ((__EPPARA0__)>>8)&0xFF; \
  400. FLASH->TS2P = (__EPPARA1__)&0xFF; \
  401. FLASH->TPS3 = ((__EPPARA1__)>>16)&0x7FF; \
  402. FLASH->PERTPE = (__EPPARA2__)&0x1FFFF; \
  403. FLASH->SMERTPE = (__EPPARA3__)&0x1FFFF; \
  404. FLASH->PRGTPE = (__EPPARA4__)&0xFFFF; \
  405. FLASH->PRETPE = ((__EPPARA4__)>>16)&0x3FFF; \
  406. } while(0U)
  407. #define __HAL_FLASH_IS_INVALID_TIMMING_SEQUENCE(_INDEX_) (((FLASH->TS0) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))&0xFF)) || \
  408. ((FLASH->TS1) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))>>16)&0x1FF)) || \
  409. ((FLASH->TS3) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]))>>8)&0xFF)) || \
  410. ((FLASH->TS2P) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+4))&0xFF)) || \
  411. ((FLASH->TPS3) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+4))>>16)&0x7FF)) || \
  412. ((FLASH->PERTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+8))&0x1FFFF)) || \
  413. ((FLASH->SMERTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+12))&0x1FFFF)) || \
  414. ((FLASH->PRGTPE) != ((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+16))&0xFFFF)) || \
  415. ((FLASH->PRETPE) != (((*(uint32_t *)(_FlashTimmingParam[_INDEX_]+16))>>16)&0x3FFF)))
  416. #define __HAL_FLASH_TIMMING_SEQUENCE_CONFIG() do{ \
  417. uint32_t tmpreg = (RCC->ICSCR & RCC_ICSCR_HSI_FS) >> RCC_ICSCR_HSI_FS_Pos; \
  418. if (__HAL_FLASH_IS_INVALID_TIMMING_SEQUENCE(tmpreg)) \
  419. { \
  420. __HAL_FLASH_TIME_REG_SET((*(uint32_t *)(_FlashTimmingParam[tmpreg])), \
  421. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+4)), \
  422. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+8)), \
  423. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+12)), \
  424. (*(uint32_t *)(_FlashTimmingParam[tmpreg]+16))); \
  425. } \
  426. }while(0U)
  427. /**
  428. * @}
  429. */
  430. /**
  431. * @}
  432. */
  433. /* Include FLASH HAL Extended module */
  434. /* Exported variables --------------------------------------------------------*/
  435. /** @defgroup FLASH_Exported_Variables FLASH Exported Variables
  436. * @{
  437. */
  438. extern FLASH_ProcessTypeDef pFlash;
  439. /**
  440. * @}
  441. */
  442. /* Exported functions --------------------------------------------------------*/
  443. /** @addtogroup FLASH_Exported_Functions
  444. * @{
  445. */
  446. /* Program operation functions ***********************************************/
  447. /** @addtogroup FLASH_Exported_Functions_Group1
  448. * @{
  449. */
  450. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t *DataAddr );
  451. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t *DataAddr);
  452. HAL_StatusTypeDef HAL_FLASH_PageProgram(uint32_t Address, uint32_t *DataAddr );
  453. HAL_StatusTypeDef HAL_FLASH_PageProgram_IT(uint32_t Address, uint32_t *DataAddr);
  454. /* FLASH IRQ handler method */
  455. void HAL_FLASH_IRQHandler(void);
  456. /* Callbacks in non blocking modes */
  457. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  458. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  459. HAL_StatusTypeDef HAL_FLASH_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError);
  460. HAL_StatusTypeDef HAL_FLASH_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit);
  461. #define HAL_FLASHEx_Erase HAL_FLASH_Erase
  462. #define HAL_FLASHEx_Erase_IT HAL_FLASH_Erase_IT
  463. /**
  464. * @}
  465. */
  466. /* Peripheral Control functions **********************************************/
  467. /** @addtogroup FLASH_Exported_Functions_Group2
  468. * @{
  469. */
  470. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  471. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  472. /* Option bytes control */
  473. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  474. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  475. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  476. HAL_StatusTypeDef HAL_FLASH_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
  477. void HAL_FLASH_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
  478. HAL_StatusTypeDef HAL_FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel);
  479. /**
  480. * @}
  481. */
  482. /* Peripheral State functions ************************************************/
  483. /** @addtogroup FLASH_Exported_Functions_Group3
  484. * @{
  485. */
  486. uint32_t HAL_FLASH_GetError(void);
  487. /**
  488. * @}
  489. */
  490. /**
  491. * @}
  492. */
  493. /* Private constants --------------------------------------------------------*/
  494. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  495. * @{
  496. */
  497. #define FLASH_TIMEOUT_VALUE 1000U /*!< FLASH Execution Timeout, 1 s */
  498. #define FLASH_TYPENONE 0x00000000U /*!< No Programmation Procedure On Going */
  499. #define FLASH_FLAG_SR_ERROR (FLASH_FLAG_OPTVERR | FLASH_FLAG_WRPERR) /*!< All SR error flags */
  500. #define FLASH_FLAG_SR_CLEAR (FLASH_FLAG_SR_ERROR | FLASH_SR_EOP)
  501. /**
  502. * @}
  503. */
  504. /* Private macros ------------------------------------------------------------*/
  505. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  506. * @{
  507. */
  508. #define IS_FLASH_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  509. #define IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 8UL)))
  510. #define IS_FLASH_PROGRAM_ADDRESS(__ADDRESS__) (IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__))
  511. #define IS_FLASH_NB_PAGES(__ADDRESS__, __VALUE__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__ + (__VALUE__*FLASH_PAGE_SIZE) - 1) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  512. #define IS_FLASH_NB_SECTORS(__ADDRESS__, __VALUE__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__ + (__VALUE__*FLASH_SECTOR_SIZE) - 1) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  513. #define IS_FLASH_FAST_PROGRAM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE)) && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 256UL)))
  514. #define IS_FLASH_PAGE(__PAGE__) ((__PAGE__) < FLASH_PAGE_NB)
  515. #define IS_FLASH_BANK(__BANK__) ((__BANK__) == 0x00UL)
  516. #define IS_FLASH_TYPEERASE(__VALUE__) (((__VALUE__) == FLASH_TYPEERASE_PAGEERASE) || \
  517. ((__VALUE__) == FLASH_TYPEERASE_SECTORERASE) || \
  518. ((__VALUE__) == FLASH_TYPEERASE_MASSERASE))
  519. #define IS_FLASH_TYPEPROGRAM(__VALUE__) ((__VALUE__) == FLASH_TYPEPROGRAM_PAGE)
  520. #define IS_FLASH_TIMECONFIG_CLOCK(__VALUE__) (((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_4MHZ) || \
  521. ((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_8MHZ) || \
  522. ((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_16MHZ) || \
  523. ((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_22p12MHZ) || \
  524. ((__VALUE__) == FLASH_PROGRAM_ERASE_CLOCK_24MHZ))
  525. #define IS_OPTIONBYTE(__VALUE__) ((((__VALUE__) & OPTIONBYTE_ALL) != 0x00U) && \
  526. (((__VALUE__) & ~OPTIONBYTE_ALL) == 0x00U))
  527. #define IS_OB_RDP_LEVEL(__LEVEL__) (((__LEVEL__) == OB_RDP_LEVEL_0) ||\
  528. ((__LEVEL__) == OB_RDP_LEVEL_1))
  529. #define IS_OB_USER_TYPE(__TYPE__) ((((__TYPE__) & OB_USER_ALL) != 0x00U) && \
  530. (((__TYPE__) & ~OB_USER_ALL) == 0x00U))
  531. #define IS_OB_USER_CONFIG(__TYPE__,__CONFIG__) ((~(__TYPE__) & (__CONFIG__)) == 0x00U)
  532. #if defined(FLASH_PCROP_SUPPORT)
  533. #define IS_OB_PCROP_CONFIG(__CONFIG__) (((__CONFIG__) & ~(OB_PCROP_ZONE_A | OB_PCROP_ZONE_B | OB_PCROP_RDP_ERASE)) == 0x00U)
  534. #endif
  535. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  536. #define IS_OB_SEC_BOOT_LOCK(__VALUE__) (((__VALUE__) == OB_BOOT_ENTRY_FORCED_NONE) || ((__VALUE__) == OB_BOOT_ENTRY_FORCED_FLASH))
  537. #define IS_OB_SEC_SIZE(__VALUE__) ((__VALUE__) < (FLASH_PAGE_NB + 1U))
  538. #endif
  539. #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
  540. ((__LATENCY__) == FLASH_LATENCY_1))
  541. #define IS_WRPSTATE(__VALUE__) (((__VALUE__) == OB_WRPSTATE_DISABLE) || \
  542. ((__VALUE__) == OB_WRPSTATE_ENABLE))
  543. /**
  544. * @}
  545. */
  546. /**
  547. * @}
  548. */
  549. /**
  550. * @}
  551. */
  552. #ifdef __cplusplus
  553. }
  554. #endif
  555. #endif /* __PY32F0xx_HAL_FLASH_H */
  556. /************************ (C) COPYRIGHT Puya *****END OF FILE****/