stm32g0xx_hal_flash.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_hal_flash.h
  4. * @author MCD Application Team
  5. * @brief Header file of FLASH HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2018 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file in
  13. * the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. ******************************************************************************
  16. */
  17. /* Define to prevent recursive inclusion -------------------------------------*/
  18. #ifndef STM32G0xx_HAL_FLASH_H
  19. #define STM32G0xx_HAL_FLASH_H
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32g0xx_hal_def.h"
  25. /** @addtogroup STM32G0xx_HAL_Driver
  26. * @{
  27. */
  28. /** @addtogroup FLASH
  29. * @{
  30. */
  31. /* Exported types ------------------------------------------------------------*/
  32. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  33. * @{
  34. */
  35. /**
  36. * @brief FLASH Erase structure definition
  37. */
  38. typedef struct
  39. {
  40. uint32_t TypeErase; /*!< Mass erase or page erase.
  41. This parameter can be a value of @ref FLASH_Type_Erase */
  42. uint32_t Banks; /*!< Select bank to erase.
  43. This parameter must be a value of @ref FLASH_Banks
  44. (FLASH_BANK_BOTH should be used only for mass erase) */
  45. uint32_t Page; /*!< Initial Flash page to erase when page erase is enabled
  46. This parameter must be a value between 0 and (FLASH_PAGE_NB - 1) */
  47. uint32_t NbPages; /*!< Number of pages to be erased.
  48. This parameter must be a value between 1 and (FLASH_PAGE_NB - value of initial page)*/
  49. } FLASH_EraseInitTypeDef;
  50. /**
  51. * @brief FLASH Option Bytes Program structure definition
  52. */
  53. typedef struct
  54. {
  55. uint32_t OptionType; /*!< Option byte to be configured.
  56. This parameter can be a combination of the values of @ref FLASH_OB_Type */
  57. uint32_t WRPArea; /*!< Write protection area to be programmed (used for OPTIONBYTE_WRP).
  58. Only one WRP area could be programmed at the same time.
  59. This parameter can be value of @ref FLASH_OB_WRP_Area */
  60. uint32_t WRPStartOffset; /*!< Write protection start offset (used for OPTIONBYTE_WRP).
  61. This parameter must be a value between 0 and [FLASH_PAGE_NB - 1]*/
  62. uint32_t WRPEndOffset; /*!< Write protection end offset (used for OPTIONBYTE_WRP).
  63. This parameter must be a value between WRPStartOffset and [FLASH_PAGE_NB - 1] */
  64. uint32_t RDPLevel; /*!< Set the read protection level (used for OPTIONBYTE_RDP).
  65. This parameter can be a value of @ref FLASH_OB_Read_Protection */
  66. uint32_t USERType; /*!< User option byte(s) to be configured (used for OPTIONBYTE_USER).
  67. This parameter can be a combination of @ref FLASH_OB_USER_Type */
  68. uint32_t USERConfig; /*!< Value of the user option byte (used for OPTIONBYTE_USER).
  69. This parameter can be a combination of
  70. @ref FLASH_OB_USER_BOR_ENABLE(*),
  71. @ref FLASH_OB_USER_BOR_LEVEL(*),
  72. @ref FLASH_OB_USER_RESET_CONFIG(*),
  73. @ref FLASH_OB_USER_nRST_STOP,
  74. @ref FLASH_OB_USER_nRST_STANDBY,
  75. @ref FLASH_OB_USER_nRST_SHUTDOWN(*),
  76. @ref FLASH_OB_USER_IWDG_SW,
  77. @ref FLASH_OB_USER_IWDG_STOP,
  78. @ref FLASH_OB_USER_IWDG_STANDBY,
  79. @ref FLASH_OB_USER_WWDG_SW,
  80. @ref FLASH_OB_USER_SRAM_PARITY,
  81. @ref FLASH_OB_USER_BANK_SWAP(*),
  82. @ref FLASH_OB_USER_DUAL_BANK(*),
  83. @ref FLASH_OB_USER_nBOOT_SEL,
  84. @ref FLASH_OB_USER_nBOOT1,
  85. @ref FLASH_OB_USER_nBOOT0,
  86. @ref FLASH_OB_USER_INPUT_RESET_HOLDER(*)
  87. @note (*) availability depends on devices */
  88. #if defined(FLASH_PCROP_SUPPORT)
  89. uint32_t PCROPConfig; /*!< Configuration of the PCROP (used for OPTIONBYTE_PCROP).
  90. This parameter must be a combination of @ref FLASH_OB_PCROP_ZONE
  91. and @ref FLASH_OB_PCROP_RDP. Note that once set, Pcrop erase on RDP level 1 regression
  92. (PCROP_RDP bit) can not be reset. It will be reset by mass erase */
  93. uint32_t PCROP1AStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP). It represents first address of start block
  94. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  95. uint32_t PCROP1AEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP). It represents first address of end block
  96. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  97. uint32_t PCROP1BStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP). It represents first address of start block
  98. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  99. uint32_t PCROP1BEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP). It represents first address of end block
  100. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  101. #if defined(FLASH_DBANK_SUPPORT)
  102. uint32_t PCROP2AStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP). It represents first address of start block
  103. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  104. uint32_t PCROP2AEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP). It represents first address of end block
  105. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  106. uint32_t PCROP2BStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP). It represents first address of start block
  107. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  108. uint32_t PCROP2BEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP). It represents first address of end block
  109. to protect. Make sure this parameter is multiple of PCROP granularity: 512 Bytes.*/
  110. #endif /* FLASH_DBANK_SUPPORT */
  111. #endif /* FLASH_PCROP_SUPPORT */
  112. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  113. uint32_t BootEntryPoint; /*!< Allow to force a unique boot entry point to Flash or system Flash */
  114. uint32_t SecSize; /*!< This parameter defines securable memory area width in number of pages starting from Flash base address.
  115. This parameter must be a value between [0] and [FLASH_PAGE_NB],
  116. [0] meaning no secure area defined, [1] meaning first page only protected, etc... */
  117. #if defined(FLASH_DBANK_SUPPORT)
  118. uint32_t SecSize2; /*!< This parameter defines securable memory area width in number of pages starting from 2nd Bank start address.
  119. This parameter must be a value between [0] and [FLASH_PAGE_NB],
  120. [0] meaning no secure area defined, [1] meaning first page only protected, etc... */
  121. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  122. #endif /* FLASH_DBANK_SUPPORT */
  123. } FLASH_OBProgramInitTypeDef;
  124. /**
  125. * @brief FLASH handle Structure definition
  126. */
  127. typedef struct
  128. {
  129. HAL_LockTypeDef Lock; /* FLASH locking object */
  130. uint32_t ErrorCode; /* FLASH error code */
  131. uint32_t ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
  132. uint32_t Address; /* Internal variable to save address selected for program in IT context */
  133. uint32_t Banks; /* Internal variable to save current bank selected during erase in IT context */
  134. uint32_t Page; /* Internal variable to define the current page which is erasing in IT context */
  135. uint32_t NbPagesToErase; /* Internal variable to save the remaining pages to erase in IT context */
  136. } FLASH_ProcessTypeDef;
  137. /**
  138. * @}
  139. */
  140. /* Exported constants --------------------------------------------------------*/
  141. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  142. * @{
  143. */
  144. /** @defgroup FLASH_Keys FLASH Keys
  145. * @{
  146. */
  147. #define FLASH_KEY1 0x45670123U /*!< Flash key1 */
  148. #define FLASH_KEY2 0xCDEF89ABU /*!< Flash key2: used with FLASH_KEY1
  149. to unlock the FLASH registers access */
  150. #define FLASH_OPTKEY1 0x08192A3BU /*!< Flash option byte key1 */
  151. #define FLASH_OPTKEY2 0x4C5D6E7FU /*!< Flash option byte key2: used with FLASH_OPTKEY1
  152. to allow option bytes operations */
  153. /**
  154. * @}
  155. */
  156. /** @defgroup FLASH_Latency FLASH Latency
  157. * @{
  158. */
  159. #define FLASH_LATENCY_0 0x00000000UL /*!< FLASH Zero wait state */
  160. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY_0 /*!< FLASH One wait state */
  161. #define FLASH_LATENCY_2 FLASH_ACR_LATENCY_1 /*!< FLASH Two wait states */
  162. /**
  163. * @}
  164. */
  165. /** @defgroup FLASH_Flags FLASH Flags Definition
  166. * @{
  167. */
  168. #define FLASH_FLAG_EOP ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_EOP_Pos) /*!< FLASH End of operation flag */
  169. #define FLASH_FLAG_OPERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_OPERR_Pos) /*!< FLASH Operation error flag */
  170. #define FLASH_FLAG_PROGERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_PROGERR_Pos) /*!< FLASH Programming error flag */
  171. #define FLASH_FLAG_WRPERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_WRPERR_Pos) /*!< FLASH Write protection error flag */
  172. #define FLASH_FLAG_PGAERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_PGAERR_Pos) /*!< FLASH Programming alignment error flag */
  173. #define FLASH_FLAG_SIZERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_SIZERR_Pos) /*!< FLASH Size error flag */
  174. #define FLASH_FLAG_PGSERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_PGSERR_Pos) /*!< FLASH Programming sequence error flag */
  175. #define FLASH_FLAG_MISERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_MISERR_Pos) /*!< FLASH Fast programming data miss error flag */
  176. #define FLASH_FLAG_FASTERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_FASTERR_Pos) /*!< FLASH Fast programming error flag */
  177. #if defined(FLASH_PCROP_SUPPORT)
  178. #define FLASH_FLAG_RDERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_RDERR_Pos) /*!< FLASH PCROP read error flag */
  179. #endif /* FLASH_PCROP_SUPPORT */
  180. #define FLASH_FLAG_OPTVERR ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_OPTVERR_Pos) /*!< FLASH Option validity error flag */
  181. #define FLASH_FLAG_BSY1 ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_BSY1_Pos) /*!< FLASH Operation Busy flag for Bank 1 */
  182. #if defined(FLASH_DBANK_SUPPORT)
  183. #define FLASH_FLAG_BSY2 ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_BSY2_Pos) /*!< FLASH Operation Busy flag for Bank 2 */
  184. #endif /* FLASH_DBANK_SUPPORT */
  185. #define FLASH_FLAG_BSY FLASH_FLAG_BSY1 /*!< FLASH Operation Busy flag - legacy name for single bank */
  186. #define FLASH_FLAG_CFGBSY ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_CFGBSY_Pos) /*!< FLASH Configuration Busy flag */
  187. #if defined(FLASH_DBANK_SUPPORT)
  188. #define FLASH_FLAG_PESD ((FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS) | FLASH_SR_PESD_Pos) /*!< FLASH Programming/erase operation suspended */
  189. #endif /* FLASH_DBANK_SUPPORT */
  190. #define FLASH_FLAG_ECCC1 ((FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS) | FLASH_ECCR_ECCC_Pos) /*!< FLASH ECC correction on bank 1 */
  191. #define FLASH_FLAG_ECCD1 ((FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS) | FLASH_ECCR_ECCD_Pos) /*!< FLASH ECC detection on bank 1 */
  192. #if defined(FLASH_DBANK_SUPPORT)
  193. #define FLASH_FLAG_ECCC2 ((FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS) | FLASH_ECC2R_ECCC_Pos) /*!< FLASH ECC correction on bank 2 */
  194. #define FLASH_FLAG_ECCD2 ((FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS) | FLASH_ECC2R_ECCD_Pos) /*!< FLASH ECC detection on bank 2 */
  195. #endif /* FLASH_DBANK_SUPPORT */
  196. #define FLASH_FLAG_ECCC FLASH_FLAG_ECCC1 /*!< FLASH ECC correction - legacy name for single bank */
  197. #define FLASH_FLAG_ECCD FLASH_FLAG_ECCD1 /*!< FLASH ECC detection - legacy name for single bank */
  198. /**
  199. * @}
  200. */
  201. /** @defgroup FLASH_Interrupt_definition FLASH Interrupts Definition
  202. * @brief FLASH Interrupt definition
  203. * @{
  204. */
  205. #define FLASH_IT_EOP ((FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS) | FLASH_CR_EOPIE_Pos) /*!< End of FLASH Operation Interrupt source */
  206. #define FLASH_IT_OPERR ((FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS) | FLASH_CR_ERRIE_Pos) /*!< Error Interrupt source */
  207. #if defined(FLASH_PCROP_SUPPORT)
  208. #define FLASH_IT_RDERR ((FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS) | FLASH_CR_RDERRIE_Pos) /*!< PCROP Read Error Interrupt source*/
  209. #endif /* FLASH_PCROP_SUPPORT */
  210. #define FLASH_IT_ECCC1 ((FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS) | FLASH_ECCR_ECCCIE_Pos) /*!< ECC Correction on Bank 1 Interrupt source */
  211. #if defined(FLASH_DBANK_SUPPORT)
  212. #define FLASH_IT_ECCC2 ((FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS) | FLASH_ECC2R_ECCCIE_Pos) /*!< ECC Correction on Bank 2 Interrupt source */
  213. #endif /* FLASH_DBANK_SUPPORT */
  214. #define FLASH_IT_ECCC FLASH_IT_ECCC1 /*!< ECC Correction - legacy name for single bank */
  215. /**
  216. * @}
  217. */
  218. /** @defgroup FLASH_Error FLASH Error
  219. * @{
  220. */
  221. #define HAL_FLASH_ERROR_NONE 0x00000000U
  222. #define HAL_FLASH_ERROR_OP FLASH_SR_OPERR
  223. #define HAL_FLASH_ERROR_PROG FLASH_SR_PROGERR
  224. #define HAL_FLASH_ERROR_WRP FLASH_SR_WRPERR
  225. #define HAL_FLASH_ERROR_PGA FLASH_SR_PGAERR
  226. #define HAL_FLASH_ERROR_SIZ FLASH_SR_SIZERR
  227. #define HAL_FLASH_ERROR_PGS FLASH_SR_PGSERR
  228. #define HAL_FLASH_ERROR_MIS FLASH_SR_MISERR
  229. #define HAL_FLASH_ERROR_FAST FLASH_SR_FASTERR
  230. #if defined(FLASH_PCROP_SUPPORT)
  231. #define HAL_FLASH_ERROR_RD FLASH_SR_RDERR
  232. #endif /* FLASH_PCROP_SUPPORT */
  233. #define HAL_FLASH_ERROR_OPTV FLASH_SR_OPTVERR
  234. #define HAL_FLASH_ERROR_ECCD FLASH_ECCR_ECCD
  235. /**
  236. * @}
  237. */
  238. /** @defgroup FLASH_Type_Erase FLASH Erase Type
  239. * @{
  240. */
  241. #define FLASH_TYPEERASE_PAGES FLASH_CR_PER /*!< Pages erase only */
  242. #define FLASH_TYPEERASE_MASS FLASH_CR_MER1 /*!< Flash mass erase activation */
  243. /**
  244. * @}
  245. */
  246. /** @defgroup FLASH_Banks FLASH Banks
  247. * @{
  248. */
  249. #define FLASH_BANK_1 FLASH_CR_MER1 /*!< Bank 1 */
  250. #if defined(FLASH_DBANK_SUPPORT)
  251. #define FLASH_BANK_2 FLASH_CR_MER2 /*!< Bank 2 */
  252. #endif /* FLASH_DBANK_SUPPORT */
  253. /**
  254. * @}
  255. */
  256. /** @defgroup FLASH_Type_Program FLASH Program Type
  257. * @{
  258. */
  259. #define FLASH_TYPEPROGRAM_DOUBLEWORD FLASH_CR_PG /*!< Program a double-word (64-bit) at a specified address */
  260. #define FLASH_TYPEPROGRAM_FAST FLASH_CR_FSTPG /*!< Fast program a 32 row double-word (64-bit) at a specified address */
  261. /**
  262. * @}
  263. */
  264. /** @defgroup FLASH_OB_Type FLASH Option Bytes Type
  265. * @{
  266. */
  267. #define OPTIONBYTE_WRP 0x00000001U /*!< WRP option byte configuration */
  268. #define OPTIONBYTE_RDP 0x00000002U /*!< RDP option byte configuration */
  269. #define OPTIONBYTE_USER 0x00000004U /*!< USER option byte configuration */
  270. #if defined(FLASH_PCROP_SUPPORT)
  271. #define OPTIONBYTE_PCROP 0x00000008U /*!< PCROP option byte configuration */
  272. #endif /* FLASH_PCROP_SUPPORT */
  273. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  274. #define OPTIONBYTE_SEC 0x00000010U /*!< SEC option byte configuration */
  275. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  276. #if defined(FLASH_PCROP_SUPPORT) && defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  277. #define OPTIONBYTE_ALL (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | \
  278. OPTIONBYTE_PCROP | OPTIONBYTE_SEC) /*!< All option byte configuration */
  279. #else
  280. #define OPTIONBYTE_ALL (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER) /*!< All option byte configuration */
  281. #endif /* FLASH_PCROP_SUPPORT && FLASH_SECURABLE_MEMORY_SUPPORT */
  282. /**
  283. * @}
  284. */
  285. /** @defgroup FLASH_OB_WRP_Area FLASH WRP Area
  286. * @{
  287. */
  288. #define OB_WRPAREA_ZONE_A 0x00000001U /*!< Flash Zone A */
  289. #define OB_WRPAREA_ZONE_B 0x00000002U /*!< Flash Zone B */
  290. #if defined(FLASH_DBANK_SUPPORT)
  291. #define OB_WRPAREA_ZONE2_A 0x00000004U /*!< Flash Bank 2 Zone A */
  292. #define OB_WRPAREA_ZONE2_B 0x00000008U /*!< Flash Bank 2 Zone B */
  293. #endif /* FLASH_DBANK_SUPPORT */
  294. /**
  295. * @}
  296. */
  297. /** @defgroup FLASH_OB_Read_Protection FLASH Option Bytes Read Protection
  298. * @{
  299. */
  300. #define OB_RDP_LEVEL_0 0x000000AAU
  301. #define OB_RDP_LEVEL_1 0x000000BBU
  302. #define OB_RDP_LEVEL_2 0x000000CCU /*!< Warning: When enabling read protection level 2
  303. it is no more possible to go back to level 1 or 0 */
  304. /**
  305. * @}
  306. */
  307. /** @defgroup FLASH_OB_USER_Type FLASH Option Bytes User Type
  308. * @{
  309. */
  310. #if defined(PWR_BOR_SUPPORT)
  311. #define OB_USER_BOR_EN FLASH_OPTR_BOR_EN /*!< BOR reset enable */
  312. #define OB_USER_BOR_LEV (FLASH_OPTR_BORF_LEV | FLASH_OPTR_BORR_LEV) /*!< BOR reset Level */
  313. #endif /* PWR_BOR_SUPPORT */
  314. #define OB_USER_nRST_STOP FLASH_OPTR_nRST_STOP /*!< Reset generated when entering the stop mode */
  315. #define OB_USER_nRST_STDBY FLASH_OPTR_nRST_STDBY /*!< Reset generated when entering the standby mode */
  316. #if defined(PWR_SHDW_SUPPORT)
  317. #define OB_USER_nRST_SHDW FLASH_OPTR_nRST_SHDW /*!< Reset generated when entering the shutdown mode */
  318. #endif /* PWR_SHDW_SUPPORT */
  319. #define OB_USER_IWDG_SW FLASH_OPTR_IWDG_SW /*!< Independent watchdog selection */
  320. #define OB_USER_IWDG_STOP FLASH_OPTR_IWDG_STOP /*!< Independent watchdog counter freeze in stop mode */
  321. #define OB_USER_IWDG_STDBY FLASH_OPTR_IWDG_STDBY /*!< Independent watchdog counter freeze in standby mode */
  322. #define OB_USER_WWDG_SW FLASH_OPTR_WWDG_SW /*!< Window watchdog selection */
  323. #if defined(FLASH_DBANK_SUPPORT)
  324. #define OB_USER_BANK_SWAP FLASH_OPTR_nSWAP_BANK /*!< Swap bank memory addresses */
  325. #define OB_USER_DUAL_BANK FLASH_OPTR_DUAL_BANK /*!< Select single or dual bank (depending of device memory size) */
  326. #endif /* FLASH_DBANK_SUPPORT */
  327. #define OB_USER_RAM_PARITY_CHECK FLASH_OPTR_RAM_PARITY_CHECK /*!< Sram parity check control */
  328. #define OB_USER_nBOOT_SEL FLASH_OPTR_nBOOT_SEL /*!< Boot Selection */
  329. #define OB_USER_nBOOT1 FLASH_OPTR_nBOOT1 /*!< nBoot1 configuration */
  330. #define OB_USER_nBOOT0 FLASH_OPTR_nBOOT0 /*!< nBoot0 configuration */
  331. #if defined(GPIO_NRST_CONFIG_SUPPORT)
  332. #define OB_USER_NRST_MODE FLASH_OPTR_NRST_MODE /*!< Reset pin configuration */
  333. #endif /* GPIO_NRST_CONFIG_SUPPORT */
  334. #if defined(FLASH_OPTR_IRHEN)
  335. #define OB_USER_INPUT_RESET_HOLDER FLASH_OPTR_IRHEN /*!< Internal reset holder enable */
  336. #endif /* FLASH_OPTR_IRHEN */
  337. #if defined(FLASH_DBANK_SUPPORT)
  338. #if defined(PWR_BOR_SUPPORT) && defined(PWR_SHDW_SUPPORT) && defined(GPIO_NRST_CONFIG_SUPPORT)
  339. #define OB_USER_ALL (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_nRST_STOP | \
  340. OB_USER_nRST_STDBY | OB_USER_nRST_SHDW | OB_USER_IWDG_SW | \
  341. OB_USER_IWDG_STOP | OB_USER_IWDG_STDBY | OB_USER_WWDG_SW | \
  342. OB_USER_BANK_SWAP | OB_USER_DUAL_BANK | \
  343. OB_USER_RAM_PARITY_CHECK | OB_USER_nBOOT_SEL | OB_USER_nBOOT1 | \
  344. OB_USER_nBOOT0 | OB_USER_NRST_MODE | OB_USER_INPUT_RESET_HOLDER) /*!< all option bits */
  345. #else
  346. #define OB_USER_ALL ( OB_USER_nRST_STOP | \
  347. OB_USER_nRST_STDBY | OB_USER_IWDG_SW | \
  348. OB_USER_IWDG_STOP | OB_USER_IWDG_STDBY | OB_USER_WWDG_SW | \
  349. OB_USER_BANK_SWAP | OB_USER_DUAL_BANK | \
  350. OB_USER_RAM_PARITY_CHECK | OB_USER_nBOOT_SEL | OB_USER_nBOOT1 | \
  351. OB_USER_nBOOT0) /*!< all option bits */
  352. #endif /* PWR_BOR_SUPPORT && PWR_SHDW_SUPPORT && GPIO_NRST_CONFIG_SUPPORT */
  353. #else
  354. #if defined(PWR_BOR_SUPPORT) && defined(PWR_SHDW_SUPPORT) && defined(GPIO_NRST_CONFIG_SUPPORT)
  355. #define OB_USER_ALL (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_nRST_STOP | \
  356. OB_USER_nRST_STDBY | OB_USER_nRST_SHDW | OB_USER_IWDG_SW | \
  357. OB_USER_IWDG_STOP | OB_USER_IWDG_STDBY | OB_USER_WWDG_SW | \
  358. OB_USER_RAM_PARITY_CHECK | OB_USER_nBOOT_SEL | OB_USER_nBOOT1 | \
  359. OB_USER_nBOOT0 | OB_USER_NRST_MODE | OB_USER_INPUT_RESET_HOLDER) /*!< all option bits */
  360. #else
  361. #define OB_USER_ALL ( OB_USER_nRST_STOP | \
  362. OB_USER_nRST_STDBY | OB_USER_IWDG_SW | \
  363. OB_USER_IWDG_STOP | OB_USER_IWDG_STDBY | OB_USER_WWDG_SW | \
  364. OB_USER_RAM_PARITY_CHECK | OB_USER_nBOOT_SEL | OB_USER_nBOOT1 | \
  365. OB_USER_nBOOT0) /*!< all option bits */
  366. #endif /* PWR_BOR_SUPPORT && PWR_SHDW_SUPPORT && GPIO_NRST_CONFIG_SUPPORT */
  367. #endif /* FLASH_DBANK_SUPPORT */
  368. /**
  369. * @}
  370. */
  371. #if defined(PWR_BOR_SUPPORT)
  372. /** @defgroup FLASH_OB_USER_BOR_ENABLE FLASH Option Bytes User BOR enable
  373. * @{
  374. */
  375. #define OB_BOR_DISABLE 0x00000000U /*!< BOR Reset set to default */
  376. #define OB_BOR_ENABLE FLASH_OPTR_BOR_EN /*!< Use option byte to define BOR thresholds */
  377. /**
  378. * @}
  379. */
  380. /** @defgroup FLASH_OB_USER_BOR_LEVEL FLASH Option Bytes User BOR Level
  381. * @{
  382. */
  383. #define OB_BOR_LEVEL_FALLING_0 0x00000000U /*!< BOR falling level 1 with threshold around 2.0V */
  384. #define OB_BOR_LEVEL_FALLING_1 FLASH_OPTR_BORF_LEV_0 /*!< BOR falling level 2 with threshold around 2.2V */
  385. #define OB_BOR_LEVEL_FALLING_2 FLASH_OPTR_BORF_LEV_1 /*!< BOR falling level 3 with threshold around 2.5V */
  386. #define OB_BOR_LEVEL_FALLING_3 (FLASH_OPTR_BORF_LEV_0 | FLASH_OPTR_BORF_LEV_1) /*!< BOR falling level 4 with threshold around 2.8V */
  387. #define OB_BOR_LEVEL_RISING_0 0x00000000U /*!< BOR rising level 1 with threshold around 2.1V */
  388. #define OB_BOR_LEVEL_RISING_1 FLASH_OPTR_BORR_LEV_0 /*!< BOR rising level 2 with threshold around 2.3V */
  389. #define OB_BOR_LEVEL_RISING_2 FLASH_OPTR_BORR_LEV_1 /*!< BOR rising level 3 with threshold around 2.6V */
  390. #define OB_BOR_LEVEL_RISING_3 (FLASH_OPTR_BORR_LEV_0 | FLASH_OPTR_BORR_LEV_1) /*!< BOR rising level 4 with threshold around 2.9V */
  391. /**
  392. * @}
  393. */
  394. #endif /* PWR_BOR_SUPPORT */
  395. /** @defgroup FLASH_OB_USER_nRST_STOP FLASH Option Bytes User Reset On Stop
  396. * @{
  397. */
  398. #define OB_STOP_RST 0x00000000U /*!< Reset generated when entering the stop mode */
  399. #define OB_STOP_NORST FLASH_OPTR_nRST_STOP /*!< No reset generated when entering the stop mode */
  400. /**
  401. * @}
  402. */
  403. /** @defgroup FLASH_OB_USER_nRST_STANDBY FLASH Option Bytes User Reset On Standby
  404. * @{
  405. */
  406. #define OB_STANDBY_RST 0x00000000U /*!< Reset generated when entering the standby mode */
  407. #define OB_STANDBY_NORST FLASH_OPTR_nRST_STDBY /*!< No reset generated when entering the standby mode */
  408. /**
  409. * @}
  410. */
  411. #if defined(PWR_SHDW_SUPPORT)
  412. /** @defgroup FLASH_OB_USER_nRST_SHUTDOWN FLASH Option Bytes User Reset On Shutdown
  413. * @{
  414. */
  415. #define OB_SHUTDOWN_RST 0x00000000U /*!< Reset generated when entering the shutdown mode */
  416. #define OB_SHUTDOWN_NORST FLASH_OPTR_nRST_SHDW /*!< No reset generated when entering the shutdown mode */
  417. /**
  418. * @}
  419. */
  420. #endif /* PWR_SHDW_SUPPORT */
  421. /** @defgroup FLASH_OB_USER_IWDG_SW FLASH Option Bytes User IWDG Type
  422. * @{
  423. */
  424. #define OB_IWDG_HW 0x00000000U /*!< Hardware independent watchdog */
  425. #define OB_IWDG_SW FLASH_OPTR_IWDG_SW /*!< Software independent watchdog */
  426. /**
  427. * @}
  428. */
  429. /** @defgroup FLASH_OB_USER_IWDG_STOP FLASH Option Bytes User IWDG Mode On Stop
  430. * @{
  431. */
  432. #define OB_IWDG_STOP_FREEZE 0x00000000U /*!< Independent watchdog counter is frozen in Stop mode */
  433. #define OB_IWDG_STOP_RUN FLASH_OPTR_IWDG_STOP /*!< Independent watchdog counter is running in Stop mode */
  434. /**
  435. * @}
  436. */
  437. /** @defgroup FLASH_OB_USER_IWDG_STANDBY FLASH Option Bytes User IWDG Mode On Standby
  438. * @{
  439. */
  440. #define OB_IWDG_STDBY_FREEZE 0x00000000U /*!< Independent watchdog counter is frozen in Standby mode */
  441. #define OB_IWDG_STDBY_RUN FLASH_OPTR_IWDG_STDBY /*!< Independent watchdog counter is running in Standby mode */
  442. /**
  443. * @}
  444. */
  445. /** @defgroup FLASH_OB_USER_WWDG_SW FLASH Option Bytes User WWDG Type
  446. * @{
  447. */
  448. #define OB_WWDG_HW 0x00000000U /*!< Hardware window watchdog */
  449. #define OB_WWDG_SW FLASH_OPTR_WWDG_SW /*!< Software window watchdog */
  450. /**
  451. * @}
  452. */
  453. #if defined(FLASH_DBANK_SUPPORT)
  454. /** @defgroup FLASH_OB_USER_BANK_SWAP FLASH Option Bytes User bank swap Type
  455. * @{
  456. */
  457. #define OB_USER_DUALBANK_SWAP_ENABLE 0x00000000U /*!< Enable bank swap */
  458. #define OB_USER_DUALBANK_SWAP_DISABLE FLASH_OPTR_nSWAP_BANK /*!< Disable bank swap */
  459. /**
  460. * @}
  461. */
  462. /** @defgroup FLASH_OB_USER_DUAL_BANK FLASH Option Bytes User dual bank enable Type
  463. * @{
  464. */
  465. #define OB_USER_DUALBANK_DISABLE 0x00000000U /*!< Disable dual bank */
  466. #define OB_USER_DUALBANK_ENABLE FLASH_OPTR_DUAL_BANK /*!< Enable dual bank */
  467. /**
  468. * @}
  469. */
  470. #endif /* FLASH_DBANK_SUPPORT */
  471. /** @defgroup FLASH_OB_USER_SRAM_PARITY FLASH Option Bytes User SRAM parity
  472. * @{
  473. */
  474. #define OB_SRAM_PARITY_ENABLE 0x00000000U /*!< Sram parity enable */
  475. #define OB_SRAM_PARITY_DISABLE FLASH_OPTR_RAM_PARITY_CHECK /*!< Sram parity disable */
  476. /**
  477. * @}
  478. */
  479. /** @defgroup FLASH_OB_USER_nBOOT_SEL FLASH Option Bytes User Boot0 Selection
  480. * @{
  481. */
  482. #define OB_BOOT0_FROM_PIN 0x00000000U /*!< BOOT0 signal is defined by PA14/BOOT0 pin value */
  483. #define OB_BOOT0_FROM_OB FLASH_OPTR_nBOOT_SEL /*!< BOOT0 signal is defined by nBOOT0 option bit */
  484. /**
  485. * @}
  486. */
  487. /** @defgroup FLASH_OB_USER_nBOOT1 FLASH Option Bytes User BOOT1 Type
  488. * @{
  489. */
  490. #define OB_BOOT1_SRAM 0x00000000U /*!< Embedded SRAM is selected as boot space (if nBOOT0=0 or BOOT0_pin=1) */
  491. #define OB_BOOT1_SYSTEM FLASH_OPTR_nBOOT1 /*!< System memory is selected as boot space (if nBOOT0=0 or BOOT0_pin=1) */
  492. /**
  493. * @}
  494. */
  495. /** @defgroup FLASH_OB_USER_nBOOT0 FLASH Option Bytes User nBOOT0 option bit
  496. * @{
  497. */
  498. #define OB_nBOOT0_RESET 0x00000000U /*!< nBOOT0 = 0 */
  499. #define OB_nBOOT0_SET FLASH_OPTR_nBOOT0 /*!< nBOOT0 = 1 */
  500. /**
  501. * @}
  502. */
  503. #if defined(GPIO_NRST_CONFIG_SUPPORT)
  504. /** @defgroup FLASH_OB_USER_RESET_CONFIG FLASH Option Bytes User reset config bit
  505. * @{
  506. */
  507. #define OB_RESET_MODE_INPUT_ONLY FLASH_OPTR_NRST_MODE_0 /*!< Reset pin is in Reset input mode only */
  508. #define OB_RESET_MODE_GPIO FLASH_OPTR_NRST_MODE_1 /*!< Reset pin is in GPIO mode mode only */
  509. #define OB_RESET_MODE_INPUT_OUTPUT FLASH_OPTR_NRST_MODE /*!< Reset pin is in reset input and output mode */
  510. /**
  511. * @}
  512. */
  513. #endif /* GPIO_NRST_CONFIG_SUPPORT */
  514. #if defined(FLASH_OPTR_IRHEN)
  515. /** @defgroup FLASH_OB_USER_INPUT_RESET_HOLDER FLASH Option Bytes User input reset holder bit
  516. * @{
  517. */
  518. #define OB_IRH_ENABLE 0x00000000U /*!< Internal Reset handler enable */
  519. #define OB_IRH_DISABLE FLASH_OPTR_IRHEN /*!< Internal Reset handler disable */
  520. /**
  521. * @}
  522. */
  523. #endif /* FLASH_OPTR_IRHEN */
  524. #if defined(FLASH_PCROP_SUPPORT)
  525. /** @defgroup FLASH_OB_PCROP_ZONE FLASH Option Bytes PCROP ZONE
  526. * @{
  527. */
  528. #define OB_PCROP_ZONE_A 0x00000001U /*!< PCROP Zone A */
  529. #define OB_PCROP_ZONE_B 0x00000002U /*!< PCROP Zone B */
  530. #if defined(FLASH_DBANK_SUPPORT)
  531. #define OB_PCROP_ZONE2_A 0x00000004U /*!< PCROP Bank 2 Zone A */
  532. #define OB_PCROP_ZONE2_B 0x00000008U /*!< PCROP Bank 2 Zone B */
  533. #endif /* FLASH_DBANK_SUPPORT */
  534. /**
  535. * @}
  536. */
  537. /** @defgroup FLASH_OB_PCROP_RDP FLASH Option Bytes PCROP On RDP Level Type
  538. * @{
  539. */
  540. #define OB_PCROP_RDP_NOT_ERASE 0x00000000U /*!< PCROP area is not erased when the RDP level
  541. is decreased from Level 1 to Level 0 */
  542. #define OB_PCROP_RDP_ERASE FLASH_PCROP1AER_PCROP_RDP /*!< PCROP area is erased when the RDP level is
  543. decreased from Level 1 to Level 0 (full mass erase).
  544. Once this bit is set only, it will be reset by mass erase */
  545. /**
  546. * @}
  547. */
  548. #endif /* FLASH_PCROP_SUPPORT */
  549. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  550. /** @defgroup FLASH_OB_SEC_BOOT_LOCK FLASH Option Bytes Secure boot lock
  551. * @{
  552. */
  553. #define OB_BOOT_ENTRY_FORCED_NONE 0x00000000U /*!< Boot entry is free */
  554. #define OB_BOOT_ENTRY_FORCED_FLASH FLASH_SECR_BOOT_LOCK /*!< Boot entry is forced to Flash or System Flash */
  555. /**
  556. * @}
  557. */
  558. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  559. /**
  560. * @}
  561. */
  562. /* Exported macros -----------------------------------------------------------*/
  563. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  564. * @brief macros to control FLASH features
  565. * @{
  566. */
  567. /**
  568. * @brief Set the FLASH Latency.
  569. * @param __LATENCY__ FLASH Latency
  570. * This parameter can be one of the following values :
  571. * @arg @ref FLASH_LATENCY_0 FLASH Zero wait state
  572. * @arg @ref FLASH_LATENCY_1 FLASH One wait state
  573. * @arg @ref FLASH_LATENCY_2 FLASH Two wait states
  574. * @retval None
  575. */
  576. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (__LATENCY__))
  577. /**
  578. * @brief Get the FLASH Latency.
  579. * @retval FLASH Latency
  580. * Returned value can be one of the following values :
  581. * @arg @ref FLASH_LATENCY_0 FLASH Zero wait state
  582. * @arg @ref FLASH_LATENCY_1 FLASH One wait state
  583. * @arg @ref FLASH_LATENCY_2 FLASH Two wait states
  584. */
  585. #define __HAL_FLASH_GET_LATENCY() READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY)
  586. /**
  587. * @brief Enable the FLASH prefetch buffer.
  588. * @retval None
  589. */
  590. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  591. /**
  592. * @brief Disable the FLASH prefetch buffer.
  593. * @retval None
  594. */
  595. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  596. /**
  597. * @brief Enable the FLASH instruction cache.
  598. * @retval none
  599. */
  600. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  601. /**
  602. * @brief Disable the FLASH instruction cache.
  603. * @retval none
  604. */
  605. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  606. /**
  607. * @brief Reset the FLASH instruction Cache.
  608. * @note This function must be used only when the Instruction Cache is disabled.
  609. * @retval None
  610. */
  611. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do { SET_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  612. CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  613. } while (0U)
  614. /**
  615. * @}
  616. */
  617. /** @defgroup FLASH_Interrupt FLASH Interrupts Macros
  618. * @brief macros to handle FLASH interrupts
  619. * @{
  620. */
  621. /**
  622. * @brief Enable the specified FLASH interrupt.
  623. * @param __INTERRUPT__ FLASH interrupt
  624. * This parameter can be one of the following values :
  625. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  626. * @arg @ref FLASH_IT_OPERR Error Interrupt
  627. * @arg @ref FLASH_IT_RDERR PCROP Read Error Interrupt(*)
  628. * @arg @ref FLASH_IT_ECCC1 ECC Correction Interrupt on bank 1
  629. * @arg @ref FLASH_IT_ECCC2 ECC Correction Interrupt on bank 2(*)
  630. * @arg @ref FLASH_IT_ECCC ECC Correction Interrupt - legacy name for single bank
  631. * @note (*) availability depends on devices
  632. * @retval none
  633. */
  634. #if defined(FLASH_DBANK_SUPPORT)
  635. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & (FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS)) != 0U) { SET_BIT(FLASH->CR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  636. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { SET_BIT(FLASH->ECCR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  637. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS)) != 0U) { SET_BIT(FLASH->ECC2R, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  638. } while(0U)
  639. #else
  640. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & (FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS)) != 0U) { SET_BIT(FLASH->CR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  641. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { SET_BIT(FLASH->ECCR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  642. } while(0U)
  643. #endif /* FLASH_DBANK_SUPPORT */
  644. /**
  645. * @brief Disable the specified FLASH interrupt.
  646. * @param __INTERRUPT__ FLASH interrupt
  647. * This parameter can be one of the following values :
  648. * @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
  649. * @arg @ref FLASH_IT_OPERR Error Interrupt
  650. * @arg @ref FLASH_IT_RDERR PCROP Read Error Interrupt(*)
  651. * @arg @ref FLASH_IT_ECCC1 ECC Correction Interrupt on bank 1
  652. * @arg @ref FLASH_IT_ECCC2 ECC Correction Interrupt on bank 2(*)
  653. * @arg @ref FLASH_IT_ECCC ECC Correction Interrupt - legacy name for single bank
  654. * @note (*) availability depends on devices
  655. * @retval none
  656. */
  657. #if defined(FLASH_DBANK_SUPPORT)
  658. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & (FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS)) != 0U) { CLEAR_BIT(FLASH->CR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  659. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { CLEAR_BIT(FLASH->ECCR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  660. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS)) != 0U) { CLEAR_BIT(FLASH->ECC2R, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  661. } while(0U)
  662. #else
  663. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & (FLASH_FLAG_CR_ID << FLASH_FLAG_REG_POS)) != 0U) { CLEAR_BIT(FLASH->CR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  664. else if(((__INTERRUPT__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { CLEAR_BIT(FLASH->ECCR, (1uL << ((__INTERRUPT__) & 0x1Fu))); } \
  665. } while(0U)
  666. #endif /* FLASH_DBANK_SUPPORT */
  667. /**
  668. * @brief Check whether the specified FLASH flag is set or not.
  669. * @param __FLAG__ specifies the FLASH flag to check.
  670. * This parameter can be one of the following values :
  671. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  672. * @arg @ref FLASH_FLAG_OPERR FLASH Operation error flag
  673. * @arg @ref FLASH_FLAG_PROGERR FLASH Programming error flag
  674. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  675. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming alignment error flag
  676. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  677. * @arg @ref FLASH_FLAG_PGSERR FLASH Programming sequence error flag
  678. * @arg @ref FLASH_FLAG_MISERR FLASH Fast programming data miss error flag
  679. * @arg @ref FLASH_FLAG_FASTERR FLASH Fast programming error flag
  680. * @arg @ref FLASH_FLAG_RDERR FLASH PCROP read error flag(*)
  681. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  682. * @arg @ref FLASH_FLAG_BSY1 FLASH bank 1 write/erase operations in progress flag
  683. * @arg @ref FLASH_FLAG_BSY2 FLASH bank 2 write/erase operations in progress flag(*)
  684. * @arg @ref FLASH_FLAG_BSY FLASH write/erase operations in progress flag - legacy name for single bank
  685. * @arg @ref FLASH_FLAG_CFGBSY FLASH configuration is busy : program or erase setting are used.
  686. * @arg @ref FLASH_FLAG_ECCC1 FLASH one ECC error has been detected and corrected
  687. * @arg @ref FLASH_FLAG_ECCD1 FLASH two ECC errors have been detected on bank 1
  688. * @arg @ref FLASH_FLAG_ECCC2 FLASH one ECC error has been detected and corrected on bank 2(*)
  689. * @arg @ref FLASH_FLAG_ECCD2 FLASH two ECC errors have been detected on bank 2(*)
  690. * @arg @ref FLASH_FLAG_ECCC FLASH one ECC error has been detected and corrected - legacy name for single bank
  691. * @arg @ref FLASH_FLAG_ECCD FLASH two ECC errors have been detected - legacy name for single bank
  692. * @note (*) availability depends on devices
  693. * @retval The state of FLASH_FLAG (SET or RESET).
  694. */
  695. #if defined(FLASH_DBANK_SUPPORT)
  696. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((((__FLAG__) & (FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS)) != 0U) ? \
  697. (READ_BIT(FLASH->SR, (1uL << ((__FLAG__) & 0x1Fu))) != 0x00u) : \
  698. ((((__FLAG__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) ? \
  699. (READ_BIT(FLASH->ECCR, (1uL << ((__FLAG__) & 0x1Fu))) != 0x00u) : \
  700. (READ_BIT(FLASH->ECC2R, (1uL << ((__FLAG__) & 0x1Fu))) != 0x00u)))
  701. #else
  702. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((((__FLAG__) & (FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS)) != 0U) ? \
  703. (READ_BIT(FLASH->SR, (1uL << ((__FLAG__) & 0x1Fu))) != 0x00u) : \
  704. (READ_BIT(FLASH->ECCR, (1uL << ((__FLAG__) & 0x1Fu))) != 0x00u))
  705. #endif /* FLASH_DBANK_SUPPORT */
  706. /**
  707. * @brief Clear the FLASH pending flag.
  708. * @param __FLAG__ specifies the FLASH flag to clear.
  709. * This parameter can be one of the following values :
  710. * @arg @ref FLASH_FLAG_EOP FLASH End of Operation flag
  711. * @arg @ref FLASH_FLAG_OPERR FLASH Operation error flag
  712. * @arg @ref FLASH_FLAG_PROGERR FLASH Programming error flag
  713. * @arg @ref FLASH_FLAG_WRPERR FLASH Write protection error flag
  714. * @arg @ref FLASH_FLAG_PGAERR FLASH Programming alignment error flag
  715. * @arg @ref FLASH_FLAG_SIZERR FLASH Size error flag
  716. * @arg @ref FLASH_FLAG_PGSERR FLASH Programming sequence error flag
  717. * @arg @ref FLASH_FLAG_MISERR FLASH Fast programming data miss error flag
  718. * @arg @ref FLASH_FLAG_FASTERR FLASH Fast programming error flag
  719. * @arg @ref FLASH_FLAG_RDERR FLASH PCROP read error flag
  720. * @arg @ref FLASH_FLAG_OPTVERR FLASH Option validity error flag
  721. * @arg @ref FLASH_FLAG_ECCC1 FLASH one ECC error has been detected and corrected
  722. * @arg @ref FLASH_FLAG_ECCD1 FLASH two ECC errors have been detected on bank 1
  723. * @arg @ref FLASH_FLAG_ECCC2 FLASH one ECC error has been detected and corrected on bank 2(*)
  724. * @arg @ref FLASH_FLAG_ECCD2 FLASH two ECC errors have been detected on bank 2(*)
  725. * @arg @ref FLASH_FLAG_ECCC FLASH one ECC error has been detected and corrected - legacy name for single bank
  726. * @arg @ref FLASH_FLAG_ECCD FLASH two ECC errors have been detected - legacy name for single bank
  727. * @note (*) availability depends on devices
  728. * @retval None
  729. */
  730. #if defined(FLASH_DBANK_SUPPORT)
  731. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { if(((__FLAG__) & (FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS)) != 0U) { FLASH->SR = (1uL << ((__FLAG__) & 0x1Fu)); } \
  732. else if(((__FLAG__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { FLASH->ECCR |= (1uL << ((__FLAG__) & 0x1Fu)); } \
  733. else if(((__FLAG__) & (FLASH_FLAG_ECCR2_ID << FLASH_FLAG_REG_POS)) != 0U) { FLASH->ECC2R |= (1uL << ((__FLAG__) & 0x1Fu)); } \
  734. } while(0U)
  735. #else
  736. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { if(((__FLAG__) & (FLASH_FLAG_SR_ID << FLASH_FLAG_REG_POS)) != 0U) { FLASH->SR = (1uL << ((__FLAG__) & 0x1Fu)); } \
  737. else if(((__FLAG__) & (FLASH_FLAG_ECCR1_ID << FLASH_FLAG_REG_POS)) != 0U) { FLASH->ECCR |= (1uL << ((__FLAG__) & 0x1Fu)); } \
  738. } while(0U)
  739. #endif /* FLASH_DBANK_SUPPORT */
  740. /**
  741. * @}
  742. */
  743. /* Include FLASH HAL Extended module */
  744. #include "stm32g0xx_hal_flash_ex.h"
  745. /* Exported variables --------------------------------------------------------*/
  746. /** @defgroup FLASH_Exported_Variables FLASH Exported Variables
  747. * @{
  748. */
  749. extern FLASH_ProcessTypeDef pFlash;
  750. /**
  751. * @}
  752. */
  753. /* Exported functions --------------------------------------------------------*/
  754. /** @addtogroup FLASH_Exported_Functions
  755. * @{
  756. */
  757. /* Program operation functions ***********************************************/
  758. /** @addtogroup FLASH_Exported_Functions_Group1
  759. * @{
  760. */
  761. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  762. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  763. /* FLASH IRQ handler method */
  764. void HAL_FLASH_IRQHandler(void);
  765. /* Callbacks in non blocking modes */
  766. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  767. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  768. /**
  769. * @}
  770. */
  771. /* Peripheral Control functions **********************************************/
  772. /** @addtogroup FLASH_Exported_Functions_Group2
  773. * @{
  774. */
  775. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  776. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  777. /* Option bytes control */
  778. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  779. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  780. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  781. /**
  782. * @}
  783. */
  784. /* Peripheral State functions ************************************************/
  785. /** @addtogroup FLASH_Exported_Functions_Group3
  786. * @{
  787. */
  788. uint32_t HAL_FLASH_GetError(void);
  789. /**
  790. * @}
  791. */
  792. /**
  793. * @}
  794. */
  795. /* Private types --------------------------------------------------------*/
  796. /** @defgroup FLASH_Private_types FLASH Private Types
  797. * @{
  798. */
  799. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  800. /**
  801. * @}
  802. */
  803. /* Private constants --------------------------------------------------------*/
  804. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  805. * @{
  806. */
  807. #define FLASH_SIZE_DATA_REGISTER FLASHSIZE_BASE
  808. #if defined(FLASH_DBANK_SUPPORT)
  809. #define OB_DUAL_BANK_BASE (FLASH_R_BASE + 0x20U) /*!< Not use cmsis FLASH alias to avoid iar warning about volatile reading sequence */
  810. #define FLASH_SALES_TYPE_Pos (24U)
  811. #define FLASH_SALES_TYPE (0x3UL << FLASH_SALES_TYPE_Pos) /*!< 0x000001E0 */
  812. #define FLASH_SALES_TYPE_0 (0x1UL << FLASH_SALES_TYPE_Pos) /*!< 0x01000000 */
  813. #define FLASH_SALES_TYPE_1 (0x2UL << FLASH_SALES_TYPE_Pos) /*!< 0x02000000 */
  814. #define FLASH_SALES_VALUE ((*((uint32_t *)PACKAGE_BASE)) & (FLASH_SALES_TYPE))
  815. #define OB_DUAL_BANK_VALUE ((*((uint32_t *)OB_DUAL_BANK_BASE)) & (FLASH_OPTR_DUAL_BANK))
  816. #define FLASH_BANK_NB (((FLASH_SALES_VALUE == 0U)\
  817. || ((FLASH_SALES_VALUE == FLASH_SALES_TYPE_0) && (OB_DUAL_BANK_VALUE == 0U)))?1U:2U)
  818. #define FLASH_BANK_SIZE ((FLASH_BANK_NB==1U)?(FLASH_SIZE):(FLASH_SIZE >> 1U)) /*!< FLASH Bank Size. Divided by 2 if 2 Banks */
  819. #else /* FLASH_DBANK_SUPPORT */
  820. #define FLASH_BANK_SIZE (FLASH_SIZE) /*!< FLASH Bank Size */
  821. #endif /* FLASH_DBANK_SUPPORT */
  822. #define FLASH_PAGE_SIZE 0x00000800U /*!< FLASH Page Size, 2 KBytes */
  823. #define FLASH_PAGE_NB (FLASH_BANK_SIZE/FLASH_PAGE_SIZE) /* Number of pages per bank */
  824. #define FLASH_TIMEOUT_VALUE 1000U /*!< FLASH Execution Timeout, 1 s */
  825. #define FLASH_TYPENONE 0x00000000U /*!< No programming Procedure On Going */
  826. #if defined(FLASH_PCROP_SUPPORT)
  827. #define FLASH_SR_ERRORS (FLASH_SR_OPERR | FLASH_SR_PROGERR | FLASH_SR_WRPERR | \
  828. FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PGSERR | \
  829. FLASH_SR_MISERR | FLASH_SR_FASTERR | FLASH_SR_RDERR | \
  830. FLASH_SR_OPTVERR) /*!< All SR error flags */
  831. #else
  832. #define FLASH_SR_ERRORS (FLASH_SR_OPERR | FLASH_SR_PROGERR | FLASH_SR_WRPERR | \
  833. FLASH_SR_PGAERR | FLASH_SR_SIZERR | FLASH_SR_PGSERR | \
  834. FLASH_SR_MISERR | FLASH_SR_FASTERR | \
  835. FLASH_SR_OPTVERR) /*!< All SR error flags */
  836. #endif /* FLASH_PCROP_SUPPORT */
  837. #if defined(FLASH_DBANK_SUPPORT)
  838. #define FLASH_SR_CLEAR (FLASH_SR_ERRORS | FLASH_SR_EOP | FLASH_SR_PESD)
  839. #else
  840. #define FLASH_SR_CLEAR (FLASH_SR_ERRORS | FLASH_SR_EOP)
  841. #endif /* FLASH_DBANK_SUPPORT */
  842. /* Internal defines for HAL macro usage */
  843. #define FLASH_FLAG_REG_POS 16u
  844. #define FLASH_FLAG_SR_ID 1u
  845. #define FLASH_FLAG_CR_ID 2u
  846. #define FLASH_FLAG_ECCR1_ID 4u
  847. #define FLASH_FLAG_ECCR2_ID 8u
  848. /**
  849. * @}
  850. */
  851. /* Private macros ------------------------------------------------------------*/
  852. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  853. * @{
  854. */
  855. #define IS_FLASH_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE))\
  856. && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  857. #define IS_FLASH_MAIN_FIRSTHALF_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE))\
  858. && ((__ADDRESS__) <= (FLASH_BASE + FLASH_BANK_SIZE - 1UL)))
  859. #if defined(FLASH_DBANK_SUPPORT)
  860. #define IS_FLASH_MAIN_SECONDHALF_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE + FLASH_BANK_SIZE))\
  861. && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 1UL)))
  862. #endif /* FLASH_DBANK_SUPPORT */
  863. #define IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE))\
  864. && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 8UL)))
  865. #define IS_FLASH_PROGRAM_OTP_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= 0x1FFF7000U)\
  866. && ((__ADDRESS__) <= (0x1FFF7400U - 8UL)))
  867. #define IS_FLASH_PROGRAM_ADDRESS(__ADDRESS__) ((IS_FLASH_PROGRAM_MAIN_MEM_ADDRESS(__ADDRESS__))\
  868. || (IS_FLASH_PROGRAM_OTP_ADDRESS(__ADDRESS__)))
  869. #define IS_FLASH_FAST_PROGRAM_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= (FLASH_BASE))\
  870. && ((__ADDRESS__) <= (FLASH_BASE + FLASH_SIZE - 256UL)))
  871. #define IS_FLASH_PAGE(__PAGE__) ((__PAGE__) < FLASH_PAGE_NB)
  872. #if defined(FLASH_DBANK_SUPPORT)
  873. #define IS_FLASH_BANK(__BANK__) \
  874. ((FLASH_BANK_NB == 2U) ? \
  875. (((__BANK__) == FLASH_BANK_1) || \
  876. ((__BANK__) == FLASH_BANK_2) || \
  877. ((__BANK__) == (FLASH_BANK_2 | FLASH_BANK_1))): \
  878. ((__BANK__) == FLASH_BANK_1))
  879. #else
  880. #define IS_FLASH_BANK(__BANK__) ((__BANK__) == FLASH_BANK_1)
  881. #endif /* FLASH_DBANK_SUPPORT */
  882. #define IS_FLASH_TYPEERASE(__VALUE__) (((__VALUE__) == FLASH_TYPEERASE_PAGES) || \
  883. ((__VALUE__) == FLASH_TYPEERASE_MASS))
  884. #define IS_FLASH_TYPEPROGRAM(__VALUE__) (((__VALUE__) == FLASH_TYPEPROGRAM_DOUBLEWORD) || \
  885. ((__VALUE__) == FLASH_TYPEPROGRAM_FAST))
  886. #define IS_OPTIONBYTE(__VALUE__) ((((__VALUE__) & OPTIONBYTE_ALL) != 0x00U) && \
  887. (((__VALUE__) & ~OPTIONBYTE_ALL) == 0x00U))
  888. #if defined(FLASH_DBANK_SUPPORT)
  889. #define IS_OB_WRPAREA(__VALUE__) \
  890. ((FLASH_BANK_NB == 2U) ? \
  891. (((__VALUE__) == OB_WRPAREA_ZONE_A) || ((__VALUE__) == OB_WRPAREA_ZONE_B) || \
  892. ((__VALUE__) == OB_WRPAREA_ZONE2_A) || ((__VALUE__) == OB_WRPAREA_ZONE2_B)) : \
  893. (((__VALUE__) == OB_WRPAREA_ZONE_A) || ((__VALUE__) == OB_WRPAREA_ZONE_B)))
  894. #else
  895. #define IS_OB_WRPAREA(__VALUE__) (((__VALUE__) == OB_WRPAREA_ZONE_A)\
  896. || ((__VALUE__) == OB_WRPAREA_ZONE_B))
  897. #endif /* FLASH_DBANK_SUPPORT */
  898. #define IS_OB_RDP_LEVEL(__LEVEL__) (((__LEVEL__) == OB_RDP_LEVEL_0) ||\
  899. ((__LEVEL__) == OB_RDP_LEVEL_1) ||\
  900. ((__LEVEL__) == OB_RDP_LEVEL_2))
  901. #define IS_OB_USER_TYPE(__TYPE__) ((((__TYPE__) & OB_USER_ALL) != 0x00U) && \
  902. (((__TYPE__) & ~OB_USER_ALL) == 0x00U))
  903. #define IS_OB_USER_CONFIG(__TYPE__,__CONFIG__) ((~(__TYPE__) & (__CONFIG__)) == 0x00U)
  904. #if defined(FLASH_PCROP_SUPPORT)
  905. #if defined(FLASH_DBANK_SUPPORT)
  906. #define IS_OB_PCROP_CONFIG(__CONFIG__) \
  907. ((FLASH_BANK_NB == 2U) ? \
  908. (((__CONFIG__) & ~(OB_PCROP_ZONE_A | OB_PCROP_ZONE_B | \
  909. OB_PCROP_ZONE2_A | OB_PCROP_ZONE2_B | OB_PCROP_RDP_ERASE)) == 0x00U): \
  910. (((__CONFIG__) & ~(OB_PCROP_ZONE_A | OB_PCROP_ZONE_B | OB_PCROP_RDP_ERASE)) == 0x00U))
  911. #else
  912. #define IS_OB_PCROP_CONFIG(__CONFIG__) (((__CONFIG__)\
  913. & ~(OB_PCROP_ZONE_A | OB_PCROP_ZONE_B | OB_PCROP_RDP_ERASE)) == 0x00U)
  914. #endif /* FLASH_DBANK_SUPPORT */
  915. #endif /* FLASH_PCROP_SUPPORT */
  916. #if defined(FLASH_SECURABLE_MEMORY_SUPPORT)
  917. #define IS_OB_SEC_BOOT_LOCK(__VALUE__) (((__VALUE__) == OB_BOOT_ENTRY_FORCED_NONE)\
  918. || ((__VALUE__) == OB_BOOT_ENTRY_FORCED_FLASH))
  919. #define IS_OB_SEC_SIZE(__VALUE__) ((__VALUE__) < (FLASH_PAGE_NB + 1U))
  920. #endif /* FLASH_SECURABLE_MEMORY_SUPPORT */
  921. #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
  922. ((__LATENCY__) == FLASH_LATENCY_1) || \
  923. ((__LATENCY__) == FLASH_LATENCY_2))
  924. /**
  925. * @}
  926. */
  927. /**
  928. * @}
  929. */
  930. /**
  931. * @}
  932. */
  933. #ifdef __cplusplus
  934. }
  935. #endif
  936. #endif /* STM32G0xx_HAL_FLASH_H */