stm32l4xx_hal_flash.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_flash.h
  4. * @author MCD Application Team
  5. * @brief Header file of FLASH HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST 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. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef __STM32L4xx_HAL_FLASH_H
  21. #define __STM32L4xx_HAL_FLASH_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32l4xx_hal_def.h"
  27. /** @addtogroup STM32L4xx_HAL_Driver
  28. * @{
  29. */
  30. /** @addtogroup FLASH
  31. * @{
  32. */
  33. /* Exported types ------------------------------------------------------------*/
  34. /** @defgroup FLASH_Exported_Types FLASH Exported Types
  35. * @{
  36. */
  37. /**
  38. * @brief FLASH Erase structure definition
  39. */
  40. typedef struct
  41. {
  42. uint32_t TypeErase; /*!< Mass erase or page erase.
  43. This parameter can be a value of @ref FLASH_Type_Erase */
  44. uint32_t Banks; /*!< Select bank to erase.
  45. This parameter must be a value of @ref FLASH_Banks
  46. (FLASH_BANK_BOTH should be used only for mass erase) */
  47. uint32_t Page; /*!< Initial Flash page to erase when page erase is disabled
  48. This parameter must be a value between 0 and (max number of pages in the bank - 1)
  49. (eg : 255 for 1MB dual bank) */
  50. uint32_t NbPages; /*!< Number of pages to be erased.
  51. This parameter must be a value between 1 and (max number of pages in the bank - value of initial page)*/
  52. } FLASH_EraseInitTypeDef;
  53. /**
  54. * @brief FLASH Option Bytes Program structure definition
  55. */
  56. typedef struct
  57. {
  58. uint32_t OptionType; /*!< Option byte to be configured.
  59. This parameter can be a combination of the values of @ref FLASH_OB_Type */
  60. uint32_t WRPArea; /*!< Write protection area to be programmed (used for OPTIONBYTE_WRP).
  61. Only one WRP area could be programmed at the same time.
  62. This parameter can be value of @ref FLASH_OB_WRP_Area */
  63. uint32_t WRPStartOffset; /*!< Write protection start offset (used for OPTIONBYTE_WRP).
  64. This parameter must be a value between 0 and (max number of pages in the bank - 1)
  65. (eg : 25 for 1MB dual bank) */
  66. uint32_t WRPEndOffset; /*!< Write protection end offset (used for OPTIONBYTE_WRP).
  67. This parameter must be a value between WRPStartOffset and (max number of pages in the bank - 1) */
  68. uint32_t RDPLevel; /*!< Set the read protection level.. (used for OPTIONBYTE_RDP).
  69. This parameter can be a value of @ref FLASH_OB_Read_Protection */
  70. uint32_t USERType; /*!< User option byte(s) to be configured (used for OPTIONBYTE_USER).
  71. This parameter can be a combination of @ref FLASH_OB_USER_Type */
  72. uint32_t USERConfig; /*!< Value of the user option byte (used for OPTIONBYTE_USER).
  73. This parameter can be a combination of @ref FLASH_OB_USER_BOR_LEVEL,
  74. @ref FLASH_OB_USER_nRST_STOP, @ref FLASH_OB_USER_nRST_STANDBY,
  75. @ref FLASH_OB_USER_nRST_SHUTDOWN, @ref FLASH_OB_USER_IWDG_SW,
  76. @ref FLASH_OB_USER_IWDG_STOP, @ref FLASH_OB_USER_IWDG_STANDBY,
  77. @ref FLASH_OB_USER_WWDG_SW, @ref FLASH_OB_USER_BFB2,
  78. @ref FLASH_OB_USER_DUALBANK, @ref FLASH_OB_USER_nBOOT1,
  79. @ref FLASH_OB_USER_SRAM2_PE and @ref FLASH_OB_USER_SRAM2_RST */
  80. uint32_t PCROPConfig; /*!< Configuration of the PCROP (used for OPTIONBYTE_PCROP).
  81. This parameter must be a combination of @ref FLASH_Banks (except FLASH_BANK_BOTH)
  82. and @ref FLASH_OB_PCROP_RDP */
  83. uint32_t PCROPStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP).
  84. This parameter must be a value between begin and end of bank
  85. => Be careful of the bank swapping for the address */
  86. uint32_t PCROPEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP).
  87. This parameter must be a value between PCROP Start address and end of bank */
  88. } FLASH_OBProgramInitTypeDef;
  89. /**
  90. * @brief FLASH Procedure structure definition
  91. */
  92. typedef enum
  93. {
  94. FLASH_PROC_NONE = 0,
  95. FLASH_PROC_PAGE_ERASE,
  96. FLASH_PROC_MASS_ERASE,
  97. FLASH_PROC_PROGRAM,
  98. FLASH_PROC_PROGRAM_LAST
  99. } FLASH_ProcedureTypeDef;
  100. /**
  101. * @brief FLASH Cache structure definition
  102. */
  103. typedef enum
  104. {
  105. FLASH_CACHE_DISABLED = 0,
  106. FLASH_CACHE_ICACHE_ENABLED,
  107. FLASH_CACHE_DCACHE_ENABLED,
  108. FLASH_CACHE_ICACHE_DCACHE_ENABLED
  109. } FLASH_CacheTypeDef;
  110. /**
  111. * @brief FLASH handle Structure definition
  112. */
  113. typedef struct
  114. {
  115. HAL_LockTypeDef Lock; /* FLASH locking object */
  116. __IO uint32_t ErrorCode; /* FLASH error code */
  117. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
  118. __IO uint32_t Address; /* Internal variable to save address selected for program in IT context */
  119. __IO uint32_t Bank; /* Internal variable to save current bank selected during erase in IT context */
  120. __IO uint32_t Page; /* Internal variable to define the current page which is erasing in IT context */
  121. __IO uint32_t NbPagesToErase; /* Internal variable to save the remaining pages to erase in IT context */
  122. __IO FLASH_CacheTypeDef CacheToReactivate; /* Internal variable to indicate which caches should be reactivated */
  123. }FLASH_ProcessTypeDef;
  124. /**
  125. * @}
  126. */
  127. /* Exported constants --------------------------------------------------------*/
  128. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  129. * @{
  130. */
  131. /** @defgroup FLASH_Error FLASH Error
  132. * @{
  133. */
  134. #define HAL_FLASH_ERROR_NONE 0x00000000U
  135. #define HAL_FLASH_ERROR_OP FLASH_FLAG_OPERR
  136. #define HAL_FLASH_ERROR_PROG FLASH_FLAG_PROGERR
  137. #define HAL_FLASH_ERROR_WRP FLASH_FLAG_WRPERR
  138. #define HAL_FLASH_ERROR_PGA FLASH_FLAG_PGAERR
  139. #define HAL_FLASH_ERROR_SIZ FLASH_FLAG_SIZERR
  140. #define HAL_FLASH_ERROR_PGS FLASH_FLAG_PGSERR
  141. #define HAL_FLASH_ERROR_MIS FLASH_FLAG_MISERR
  142. #define HAL_FLASH_ERROR_FAST FLASH_FLAG_FASTERR
  143. #define HAL_FLASH_ERROR_RD FLASH_FLAG_RDERR
  144. #define HAL_FLASH_ERROR_OPTV FLASH_FLAG_OPTVERR
  145. #define HAL_FLASH_ERROR_ECCD FLASH_FLAG_ECCD
  146. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || \
  147. defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
  148. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || \
  149. defined (STM32L4S7xx) || defined (STM32L4S9xx)
  150. #define HAL_FLASH_ERROR_PEMPTY FLASH_FLAG_PEMPTY
  151. #endif
  152. /**
  153. * @}
  154. */
  155. /** @defgroup FLASH_Type_Erase FLASH Erase Type
  156. * @{
  157. */
  158. #define FLASH_TYPEERASE_PAGES ((uint32_t)0x00) /*!<Pages erase only*/
  159. #define FLASH_TYPEERASE_MASSERASE ((uint32_t)0x01) /*!<Flash mass erase activation*/
  160. /**
  161. * @}
  162. */
  163. /** @defgroup FLASH_Banks FLASH Banks
  164. * @{
  165. */
  166. #define FLASH_BANK_1 ((uint32_t)0x01) /*!< Bank 1 */
  167. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  168. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  169. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  170. #define FLASH_BANK_2 ((uint32_t)0x02) /*!< Bank 2 */
  171. #define FLASH_BANK_BOTH ((uint32_t)(FLASH_BANK_1 | FLASH_BANK_2)) /*!< Bank1 and Bank2 */
  172. #else
  173. #define FLASH_BANK_BOTH ((uint32_t)(FLASH_BANK_1)) /*!< Bank 1 */
  174. #endif
  175. /**
  176. * @}
  177. */
  178. /** @defgroup FLASH_Type_Program FLASH Program Type
  179. * @{
  180. */
  181. #define FLASH_TYPEPROGRAM_DOUBLEWORD ((uint32_t)0x00) /*!<Program a double-word (64-bit) at a specified address.*/
  182. #define FLASH_TYPEPROGRAM_FAST ((uint32_t)0x01) /*!<Fast program a 32 row double-word (64-bit) at a specified address.
  183. And another 32 row double-word (64-bit) will be programmed */
  184. #define FLASH_TYPEPROGRAM_FAST_AND_LAST ((uint32_t)0x02) /*!<Fast program a 32 row double-word (64-bit) at a specified address.
  185. And this is the last 32 row double-word (64-bit) programmed */
  186. /**
  187. * @}
  188. */
  189. /** @defgroup FLASH_OB_Type FLASH Option Bytes Type
  190. * @{
  191. */
  192. #define OPTIONBYTE_WRP ((uint32_t)0x01) /*!< WRP option byte configuration */
  193. #define OPTIONBYTE_RDP ((uint32_t)0x02) /*!< RDP option byte configuration */
  194. #define OPTIONBYTE_USER ((uint32_t)0x04) /*!< USER option byte configuration */
  195. #define OPTIONBYTE_PCROP ((uint32_t)0x08) /*!< PCROP option byte configuration */
  196. /**
  197. * @}
  198. */
  199. /** @defgroup FLASH_OB_WRP_Area FLASH WRP Area
  200. * @{
  201. */
  202. #define OB_WRPAREA_BANK1_AREAA ((uint32_t)0x00) /*!< Flash Bank 1 Area A */
  203. #define OB_WRPAREA_BANK1_AREAB ((uint32_t)0x01) /*!< Flash Bank 1 Area B */
  204. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  205. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  206. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  207. #define OB_WRPAREA_BANK2_AREAA ((uint32_t)0x02) /*!< Flash Bank 2 Area A */
  208. #define OB_WRPAREA_BANK2_AREAB ((uint32_t)0x04) /*!< Flash Bank 2 Area B */
  209. #endif
  210. /**
  211. * @}
  212. */
  213. /** @defgroup FLASH_OB_Read_Protection FLASH Option Bytes Read Protection
  214. * @{
  215. */
  216. #define OB_RDP_LEVEL_0 ((uint32_t)0xAA)
  217. #define OB_RDP_LEVEL_1 ((uint32_t)0xBB)
  218. #define OB_RDP_LEVEL_2 ((uint32_t)0xCC) /*!< Warning: When enabling read protection level 2
  219. it's no more possible to go back to level 1 or 0 */
  220. /**
  221. * @}
  222. */
  223. /** @defgroup FLASH_OB_USER_Type FLASH Option Bytes User Type
  224. * @{
  225. */
  226. #define OB_USER_BOR_LEV ((uint32_t)0x0001) /*!< BOR reset Level */
  227. #define OB_USER_nRST_STOP ((uint32_t)0x0002) /*!< Reset generated when entering the stop mode */
  228. #define OB_USER_nRST_STDBY ((uint32_t)0x0004) /*!< Reset generated when entering the standby mode */
  229. #define OB_USER_IWDG_SW ((uint32_t)0x0008) /*!< Independent watchdog selection */
  230. #define OB_USER_IWDG_STOP ((uint32_t)0x0010) /*!< Independent watchdog counter freeze in stop mode */
  231. #define OB_USER_IWDG_STDBY ((uint32_t)0x0020) /*!< Independent watchdog counter freeze in standby mode */
  232. #define OB_USER_WWDG_SW ((uint32_t)0x0040) /*!< Window watchdog selection */
  233. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  234. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  235. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  236. #define OB_USER_BFB2 ((uint32_t)0x0080) /*!< Dual-bank boot */
  237. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  238. #define OB_USER_DUALBANK ((uint32_t)0x0100) /*!< Dual-Bank on 1MB or 512kB Flash memory devices */
  239. #else
  240. #define OB_USER_DUALBANK ((uint32_t)0x0100) /*!< Dual-Bank on 512KB or 256KB Flash memory devices */
  241. #endif
  242. #endif
  243. #define OB_USER_nBOOT1 ((uint32_t)0x0200) /*!< Boot configuration */
  244. #define OB_USER_SRAM2_PE ((uint32_t)0x0400) /*!< SRAM2 parity check enable */
  245. #define OB_USER_SRAM2_RST ((uint32_t)0x0800) /*!< SRAM2 Erase when system reset */
  246. #define OB_USER_nRST_SHDW ((uint32_t)0x1000) /*!< Reset generated when entering the shutdown mode */
  247. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || \
  248. defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  249. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  250. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  251. #define OB_USER_nSWBOOT0 ((uint32_t)0x2000) /*!< Software BOOT0 */
  252. #define OB_USER_nBOOT0 ((uint32_t)0x4000) /*!< nBOOT0 option bit */
  253. #endif
  254. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  255. #define OB_USER_DBANK ((uint32_t)0x8000) /*!< Single bank with 128-bits data or two banks with 64-bits data */
  256. #endif
  257. /**
  258. * @}
  259. */
  260. /** @defgroup FLASH_OB_USER_BOR_LEVEL FLASH Option Bytes User BOR Level
  261. * @{
  262. */
  263. #define OB_BOR_LEVEL_0 ((uint32_t)FLASH_OPTR_BOR_LEV_0) /*!< Reset level threshold is around 1.7V */
  264. #define OB_BOR_LEVEL_1 ((uint32_t)FLASH_OPTR_BOR_LEV_1) /*!< Reset level threshold is around 2.0V */
  265. #define OB_BOR_LEVEL_2 ((uint32_t)FLASH_OPTR_BOR_LEV_2) /*!< Reset level threshold is around 2.2V */
  266. #define OB_BOR_LEVEL_3 ((uint32_t)FLASH_OPTR_BOR_LEV_3) /*!< Reset level threshold is around 2.5V */
  267. #define OB_BOR_LEVEL_4 ((uint32_t)FLASH_OPTR_BOR_LEV_4) /*!< Reset level threshold is around 2.8V */
  268. /**
  269. * @}
  270. */
  271. /** @defgroup FLASH_OB_USER_nRST_STOP FLASH Option Bytes User Reset On Stop
  272. * @{
  273. */
  274. #define OB_STOP_RST ((uint32_t)0x0000) /*!< Reset generated when entering the stop mode */
  275. #define OB_STOP_NORST ((uint32_t)FLASH_OPTR_nRST_STOP) /*!< No reset generated when entering the stop mode */
  276. /**
  277. * @}
  278. */
  279. /** @defgroup FLASH_OB_USER_nRST_STANDBY FLASH Option Bytes User Reset On Standby
  280. * @{
  281. */
  282. #define OB_STANDBY_RST ((uint32_t)0x0000) /*!< Reset generated when entering the standby mode */
  283. #define OB_STANDBY_NORST ((uint32_t)FLASH_OPTR_nRST_STDBY) /*!< No reset generated when entering the standby mode */
  284. /**
  285. * @}
  286. */
  287. /** @defgroup FLASH_OB_USER_nRST_SHUTDOWN FLASH Option Bytes User Reset On Shutdown
  288. * @{
  289. */
  290. #define OB_SHUTDOWN_RST ((uint32_t)0x0000) /*!< Reset generated when entering the shutdown mode */
  291. #define OB_SHUTDOWN_NORST ((uint32_t)FLASH_OPTR_nRST_SHDW) /*!< No reset generated when entering the shutdown mode */
  292. /**
  293. * @}
  294. */
  295. /** @defgroup FLASH_OB_USER_IWDG_SW FLASH Option Bytes User IWDG Type
  296. * @{
  297. */
  298. #define OB_IWDG_HW ((uint32_t)0x00000) /*!< Hardware independent watchdog */
  299. #define OB_IWDG_SW ((uint32_t)FLASH_OPTR_IWDG_SW) /*!< Software independent watchdog */
  300. /**
  301. * @}
  302. */
  303. /** @defgroup FLASH_OB_USER_IWDG_STOP FLASH Option Bytes User IWDG Mode On Stop
  304. * @{
  305. */
  306. #define OB_IWDG_STOP_FREEZE ((uint32_t)0x00000) /*!< Independent watchdog counter is frozen in Stop mode */
  307. #define OB_IWDG_STOP_RUN ((uint32_t)FLASH_OPTR_IWDG_STOP) /*!< Independent watchdog counter is running in Stop mode */
  308. /**
  309. * @}
  310. */
  311. /** @defgroup FLASH_OB_USER_IWDG_STANDBY FLASH Option Bytes User IWDG Mode On Standby
  312. * @{
  313. */
  314. #define OB_IWDG_STDBY_FREEZE ((uint32_t)0x00000) /*!< Independent watchdog counter is frozen in Standby mode */
  315. #define OB_IWDG_STDBY_RUN ((uint32_t)FLASH_OPTR_IWDG_STDBY) /*!< Independent watchdog counter is running in Standby mode */
  316. /**
  317. * @}
  318. */
  319. /** @defgroup FLASH_OB_USER_WWDG_SW FLASH Option Bytes User WWDG Type
  320. * @{
  321. */
  322. #define OB_WWDG_HW ((uint32_t)0x00000) /*!< Hardware window watchdog */
  323. #define OB_WWDG_SW ((uint32_t)FLASH_OPTR_WWDG_SW) /*!< Software window watchdog */
  324. /**
  325. * @}
  326. */
  327. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  328. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  329. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  330. /** @defgroup FLASH_OB_USER_BFB2 FLASH Option Bytes User BFB2 Mode
  331. * @{
  332. */
  333. #define OB_BFB2_DISABLE ((uint32_t)0x000000) /*!< Dual-bank boot disable */
  334. #define OB_BFB2_ENABLE ((uint32_t)FLASH_OPTR_BFB2) /*!< Dual-bank boot enable */
  335. /**
  336. * @}
  337. */
  338. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  339. /** @defgroup FLASH_OB_USER_DUALBANK FLASH Option Bytes User Dual-bank Type
  340. * @{
  341. */
  342. #define OB_DUALBANK_SINGLE ((uint32_t)0x000000) /*!< 1 MB/512 kB Single-bank Flash */
  343. #define OB_DUALBANK_DUAL ((uint32_t)FLASH_OPTR_DB1M) /*!< 1 MB/512 kB Dual-bank Flash */
  344. /**
  345. * @}
  346. */
  347. #else
  348. /** @defgroup FLASH_OB_USER_DUALBANK FLASH Option Bytes User Dual-bank Type
  349. * @{
  350. */
  351. #define OB_DUALBANK_SINGLE ((uint32_t)0x000000) /*!< 256 KB/512 KB Single-bank Flash */
  352. #define OB_DUALBANK_DUAL ((uint32_t)FLASH_OPTR_DUALBANK) /*!< 256 KB/512 KB Dual-bank Flash */
  353. /**
  354. * @}
  355. */
  356. #endif
  357. #endif
  358. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  359. /** @defgroup FLASH_OB_USER_DBANK FLASH Option Bytes User DBANK Type
  360. * @{
  361. */
  362. #define OB_DBANK_128_BITS ((uint32_t)0x000000) /*!< Single-bank with 128-bits data */
  363. #define OB_DBANK_64_BITS ((uint32_t)FLASH_OPTR_DBANK) /*!< Dual-bank with 64-bits data */
  364. #endif
  365. /**
  366. * @}
  367. */
  368. /** @defgroup FLASH_OB_USER_nBOOT1 FLASH Option Bytes User BOOT1 Type
  369. * @{
  370. */
  371. #define OB_BOOT1_SRAM ((uint32_t)0x000000) /*!< Embedded SRAM1 is selected as boot space (if BOOT0=1) */
  372. #define OB_BOOT1_SYSTEM ((uint32_t)FLASH_OPTR_nBOOT1) /*!< System memory is selected as boot space (if BOOT0=1) */
  373. /**
  374. * @}
  375. */
  376. /** @defgroup FLASH_OB_USER_SRAM2_PE FLASH Option Bytes User SRAM2 Parity Check Type
  377. * @{
  378. */
  379. #define OB_SRAM2_PARITY_ENABLE ((uint32_t)0x0000000) /*!< SRAM2 parity check enable */
  380. #define OB_SRAM2_PARITY_DISABLE ((uint32_t)FLASH_OPTR_SRAM2_PE) /*!< SRAM2 parity check disable */
  381. /**
  382. * @}
  383. */
  384. /** @defgroup FLASH_OB_USER_SRAM2_RST FLASH Option Bytes User SRAM2 Erase On Reset Type
  385. * @{
  386. */
  387. #define OB_SRAM2_RST_ERASE ((uint32_t)0x0000000) /*!< SRAM2 erased when a system reset occurs */
  388. #define OB_SRAM2_RST_NOT_ERASE ((uint32_t)FLASH_OPTR_SRAM2_RST) /*!< SRAM2 is not erased when a system reset occurs */
  389. /**
  390. * @}
  391. */
  392. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || \
  393. defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  394. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  395. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  396. /** @defgroup OB_USER_nSWBOOT0 FLASH Option Bytes User Software BOOT0
  397. * @{
  398. */
  399. #define OB_BOOT0_FROM_OB ((uint32_t)0x0000000) /*!< BOOT0 taken from the option bit nBOOT0 */
  400. #define OB_BOOT0_FROM_PIN ((uint32_t)FLASH_OPTR_nSWBOOT0) /*!< BOOT0 taken from PH3/BOOT0 pin */
  401. /**
  402. * @}
  403. */
  404. /** @defgroup OB_USER_nBOOT0 FLASH Option Bytes User nBOOT0 option bit
  405. * @{
  406. */
  407. #define OB_BOOT0_RESET ((uint32_t)0x0000000) /*!< nBOOT0 = 0 */
  408. #define OB_BOOT0_SET ((uint32_t)FLASH_OPTR_nBOOT0) /*!< nBOOT0 = 1 */
  409. /**
  410. * @}
  411. */
  412. #endif
  413. /** @defgroup FLASH_OB_PCROP_RDP FLASH Option Bytes PCROP On RDP Level Type
  414. * @{
  415. */
  416. #define OB_PCROP_RDP_NOT_ERASE ((uint32_t)0x00000000) /*!< PCROP area is not erased when the RDP level
  417. is decreased from Level 1 to Level 0 */
  418. #define OB_PCROP_RDP_ERASE ((uint32_t)FLASH_PCROP1ER_PCROP_RDP) /*!< PCROP area is erased when the RDP level is
  419. decreased from Level 1 to Level 0 (full mass erase) */
  420. /**
  421. * @}
  422. */
  423. /** @defgroup FLASH_Latency FLASH Latency
  424. * @{
  425. */
  426. #define FLASH_LATENCY_0 FLASH_ACR_LATENCY_0WS /*!< FLASH Zero wait state */
  427. #define FLASH_LATENCY_1 FLASH_ACR_LATENCY_1WS /*!< FLASH One wait state */
  428. #define FLASH_LATENCY_2 FLASH_ACR_LATENCY_2WS /*!< FLASH Two wait states */
  429. #define FLASH_LATENCY_3 FLASH_ACR_LATENCY_3WS /*!< FLASH Three wait states */
  430. #define FLASH_LATENCY_4 FLASH_ACR_LATENCY_4WS /*!< FLASH Four wait states */
  431. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  432. #define FLASH_LATENCY_5 FLASH_ACR_LATENCY_5WS /*!< FLASH Five wait state */
  433. #define FLASH_LATENCY_6 FLASH_ACR_LATENCY_6WS /*!< FLASH Six wait state */
  434. #define FLASH_LATENCY_7 FLASH_ACR_LATENCY_7WS /*!< FLASH Seven wait states */
  435. #define FLASH_LATENCY_8 FLASH_ACR_LATENCY_8WS /*!< FLASH Eight wait states */
  436. #define FLASH_LATENCY_9 FLASH_ACR_LATENCY_9WS /*!< FLASH Nine wait states */
  437. #define FLASH_LATENCY_10 FLASH_ACR_LATENCY_10WS /*!< FLASH Ten wait state */
  438. #define FLASH_LATENCY_11 FLASH_ACR_LATENCY_11WS /*!< FLASH Eleven wait state */
  439. #define FLASH_LATENCY_12 FLASH_ACR_LATENCY_12WS /*!< FLASH Twelve wait states */
  440. #define FLASH_LATENCY_13 FLASH_ACR_LATENCY_13WS /*!< FLASH Thirteen wait states */
  441. #define FLASH_LATENCY_14 FLASH_ACR_LATENCY_14WS /*!< FLASH Fourteen wait states */
  442. #define FLASH_LATENCY_15 FLASH_ACR_LATENCY_15WS /*!< FLASH Fifteen wait states */
  443. #endif
  444. /**
  445. * @}
  446. */
  447. /** @defgroup FLASH_Keys FLASH Keys
  448. * @{
  449. */
  450. #define FLASH_KEY1 0x45670123U /*!< Flash key1 */
  451. #define FLASH_KEY2 0xCDEF89ABU /*!< Flash key2: used with FLASH_KEY1
  452. to unlock the FLASH registers access */
  453. #define FLASH_PDKEY1 0x04152637U /*!< Flash power down key1 */
  454. #define FLASH_PDKEY2 0xFAFBFCFDU /*!< Flash power down key2: used with FLASH_PDKEY1
  455. to unlock the RUN_PD bit in FLASH_ACR */
  456. #define FLASH_OPTKEY1 0x08192A3BU /*!< Flash option byte key1 */
  457. #define FLASH_OPTKEY2 0x4C5D6E7FU /*!< Flash option byte key2: used with FLASH_OPTKEY1
  458. to allow option bytes operations */
  459. /**
  460. * @}
  461. */
  462. /** @defgroup FLASH_Flags FLASH Flags Definition
  463. * @{
  464. */
  465. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of operation flag */
  466. #define FLASH_FLAG_OPERR FLASH_SR_OPERR /*!< FLASH Operation error flag */
  467. #define FLASH_FLAG_PROGERR FLASH_SR_PROGERR /*!< FLASH Programming error flag */
  468. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protection error flag */
  469. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming alignment error flag */
  470. #define FLASH_FLAG_SIZERR FLASH_SR_SIZERR /*!< FLASH Size error flag */
  471. #define FLASH_FLAG_PGSERR FLASH_SR_PGSERR /*!< FLASH Programming sequence error flag */
  472. #define FLASH_FLAG_MISERR FLASH_SR_MISERR /*!< FLASH Fast programming data miss error flag */
  473. #define FLASH_FLAG_FASTERR FLASH_SR_FASTERR /*!< FLASH Fast programming error flag */
  474. #define FLASH_FLAG_RDERR FLASH_SR_RDERR /*!< FLASH PCROP read error flag */
  475. #define FLASH_FLAG_OPTVERR FLASH_SR_OPTVERR /*!< FLASH Option validity error flag */
  476. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  477. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || \
  478. defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
  479. defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || \
  480. defined (STM32L4S7xx) || defined (STM32L4S9xx)
  481. #define FLASH_FLAG_PEMPTY FLASH_SR_PEMPTY /*!< FLASH Program empty */
  482. #define FLASH_FLAG_SR_ERRORS (FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR | FLASH_FLAG_WRPERR | \
  483. FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_PGSERR | \
  484. FLASH_FLAG_MISERR | FLASH_FLAG_FASTERR | FLASH_FLAG_RDERR | \
  485. FLASH_FLAG_OPTVERR | FLASH_FLAG_PEMPTY)
  486. #else
  487. #define FLASH_FLAG_SR_ERRORS (FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR | FLASH_FLAG_WRPERR | \
  488. FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_PGSERR | \
  489. FLASH_FLAG_MISERR | FLASH_FLAG_FASTERR | FLASH_FLAG_RDERR | \
  490. FLASH_FLAG_OPTVERR)
  491. #endif
  492. #define FLASH_FLAG_ECCC FLASH_ECCR_ECCC /*!< FLASH ECC correction */
  493. #define FLASH_FLAG_ECCD FLASH_ECCR_ECCD /*!< FLASH ECC detection */
  494. #define FLASH_FLAG_ALL_ERRORS (FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR | FLASH_FLAG_WRPERR | \
  495. FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_PGSERR | \
  496. FLASH_FLAG_MISERR | FLASH_FLAG_FASTERR | FLASH_FLAG_RDERR | \
  497. FLASH_FLAG_OPTVERR | FLASH_FLAG_ECCD)
  498. /**
  499. * @}
  500. */
  501. /** @defgroup FLASH_Interrupt_definition FLASH Interrupts Definition
  502. * @brief FLASH Interrupt definition
  503. * @{
  504. */
  505. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  506. #define FLASH_IT_OPERR FLASH_CR_ERRIE /*!< Error Interrupt source */
  507. #define FLASH_IT_RDERR FLASH_CR_RDERRIE /*!< PCROP Read Error Interrupt source*/
  508. #define FLASH_IT_ECCC (FLASH_ECCR_ECCIE >> 24) /*!< ECC Correction Interrupt source */
  509. /**
  510. * @}
  511. */
  512. /* Exported macros -----------------------------------------------------------*/
  513. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  514. * @brief macros to control FLASH features
  515. * @{
  516. */
  517. /**
  518. * @brief Set the FLASH Latency.
  519. * @param __LATENCY__: FLASH Latency
  520. * This parameter can be one of the following values :
  521. * @arg FLASH_LATENCY_0: FLASH Zero wait state
  522. * @arg FLASH_LATENCY_1: FLASH One wait state
  523. * @arg FLASH_LATENCY_2: FLASH Two wait states
  524. * @arg FLASH_LATENCY_3: FLASH Three wait states
  525. * @arg FLASH_LATENCY_4: FLASH Four wait states
  526. * @retval None
  527. */
  528. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (__LATENCY__)))
  529. /**
  530. * @brief Get the FLASH Latency.
  531. * @retval FLASH Latency
  532. * This parameter can be one of the following values :
  533. * @arg FLASH_LATENCY_0: FLASH Zero wait state
  534. * @arg FLASH_LATENCY_1: FLASH One wait state
  535. * @arg FLASH_LATENCY_2: FLASH Two wait states
  536. * @arg FLASH_LATENCY_3: FLASH Three wait states
  537. * @arg FLASH_LATENCY_4: FLASH Four wait states
  538. */
  539. #define __HAL_FLASH_GET_LATENCY() READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY)
  540. /**
  541. * @brief Enable the FLASH prefetch buffer.
  542. * @retval None
  543. */
  544. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  545. /**
  546. * @brief Disable the FLASH prefetch buffer.
  547. * @retval None
  548. */
  549. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  550. /**
  551. * @brief Enable the FLASH instruction cache.
  552. * @retval none
  553. */
  554. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  555. /**
  556. * @brief Disable the FLASH instruction cache.
  557. * @retval none
  558. */
  559. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  560. /**
  561. * @brief Enable the FLASH data cache.
  562. * @retval none
  563. */
  564. #define __HAL_FLASH_DATA_CACHE_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_DCEN)
  565. /**
  566. * @brief Disable the FLASH data cache.
  567. * @retval none
  568. */
  569. #define __HAL_FLASH_DATA_CACHE_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_DCEN)
  570. /**
  571. * @brief Reset the FLASH instruction Cache.
  572. * @note This function must be used only when the Instruction Cache is disabled.
  573. * @retval None
  574. */
  575. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do { SET_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  576. CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  577. } while (0)
  578. /**
  579. * @brief Reset the FLASH data Cache.
  580. * @note This function must be used only when the data Cache is disabled.
  581. * @retval None
  582. */
  583. #define __HAL_FLASH_DATA_CACHE_RESET() do { SET_BIT(FLASH->ACR, FLASH_ACR_DCRST); \
  584. CLEAR_BIT(FLASH->ACR, FLASH_ACR_DCRST); \
  585. } while (0)
  586. /**
  587. * @brief Enable the FLASH power down during Low-power run mode.
  588. * @note Writing this bit to 0 this bit, automatically the keys are
  589. * loss and a new unlock sequence is necessary to re-write it to 1.
  590. */
  591. #define __HAL_FLASH_POWER_DOWN_ENABLE() do { WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY1); \
  592. WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY2); \
  593. SET_BIT(FLASH->ACR, FLASH_ACR_RUN_PD); \
  594. } while (0)
  595. /**
  596. * @brief Disable the FLASH power down during Low-power run mode.
  597. * @note Writing this bit to 0 this bit, automatically the keys are
  598. * loss and a new unlock sequence is necessary to re-write it to 1.
  599. */
  600. #define __HAL_FLASH_POWER_DOWN_DISABLE() do { WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY1); \
  601. WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY2); \
  602. CLEAR_BIT(FLASH->ACR, FLASH_ACR_RUN_PD); \
  603. } while (0)
  604. /**
  605. * @brief Enable the FLASH power down during Low-Power sleep mode
  606. * @retval none
  607. */
  608. #define __HAL_FLASH_SLEEP_POWERDOWN_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
  609. /**
  610. * @brief Disable the FLASH power down during Low-Power sleep mode
  611. * @retval none
  612. */
  613. #define __HAL_FLASH_SLEEP_POWERDOWN_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
  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 any combination of the following values:
  625. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  626. * @arg FLASH_IT_OPERR: Error Interrupt
  627. * @arg FLASH_IT_RDERR: PCROP Read Error Interrupt
  628. * @arg FLASH_IT_ECCC: ECC Correction Interrupt
  629. * @retval none
  630. */
  631. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & FLASH_IT_ECCC) != 0U) { SET_BIT(FLASH->ECCR, FLASH_ECCR_ECCIE); }\
  632. if(((__INTERRUPT__) & (~FLASH_IT_ECCC)) != 0U) { SET_BIT(FLASH->CR, ((__INTERRUPT__) & (~FLASH_IT_ECCC))); }\
  633. } while(0)
  634. /**
  635. * @brief Disable the specified FLASH interrupt.
  636. * @param __INTERRUPT__: FLASH interrupt
  637. * This parameter can be any combination of the following values:
  638. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  639. * @arg FLASH_IT_OPERR: Error Interrupt
  640. * @arg FLASH_IT_RDERR: PCROP Read Error Interrupt
  641. * @arg FLASH_IT_ECCC: ECC Correction Interrupt
  642. * @retval none
  643. */
  644. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & FLASH_IT_ECCC) != 0U) { CLEAR_BIT(FLASH->ECCR, FLASH_ECCR_ECCIE); }\
  645. if(((__INTERRUPT__) & (~FLASH_IT_ECCC)) != 0U) { CLEAR_BIT(FLASH->CR, ((__INTERRUPT__) & (~FLASH_IT_ECCC))); }\
  646. } while(0)
  647. /**
  648. * @brief Check whether the specified FLASH flag is set or not.
  649. * @param __FLAG__: specifies the FLASH flag to check.
  650. * This parameter can be one of the following values:
  651. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  652. * @arg FLASH_FLAG_OPERR: FLASH Operation error flag
  653. * @arg FLASH_FLAG_PROGERR: FLASH Programming error flag
  654. * @arg FLASH_FLAG_WRPERR: FLASH Write protection error flag
  655. * @arg FLASH_FLAG_PGAERR: FLASH Programming alignment error flag
  656. * @arg FLASH_FLAG_SIZERR: FLASH Size error flag
  657. * @arg FLASH_FLAG_PGSERR: FLASH Programming sequence error flag
  658. * @arg FLASH_FLAG_MISERR: FLASH Fast programming data miss error flag
  659. * @arg FLASH_FLAG_FASTERR: FLASH Fast programming error flag
  660. * @arg FLASH_FLAG_RDERR: FLASH PCROP read error flag
  661. * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
  662. * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
  663. * @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)
  664. * @arg FLASH_FLAG_ECCC: FLASH one ECC error has been detected and corrected
  665. * @arg FLASH_FLAG_ECCD: FLASH two ECC errors have been detected
  666. * @retval The new state of FLASH_FLAG (SET or RESET).
  667. */
  668. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((((__FLAG__) & (FLASH_FLAG_ECCC | FLASH_FLAG_ECCD)) != 0U) ? \
  669. (READ_BIT(FLASH->ECCR, (__FLAG__)) == (__FLAG__)) : \
  670. (READ_BIT(FLASH->SR, (__FLAG__)) == (__FLAG__)))
  671. /**
  672. * @brief Clear the FLASH's pending flags.
  673. * @param __FLAG__: specifies the FLASH flags to clear.
  674. * This parameter can be any combination of the following values:
  675. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  676. * @arg FLASH_FLAG_OPERR: FLASH Operation error flag
  677. * @arg FLASH_FLAG_PROGERR: FLASH Programming error flag
  678. * @arg FLASH_FLAG_WRPERR: FLASH Write protection error flag
  679. * @arg FLASH_FLAG_PGAERR: FLASH Programming alignment error flag
  680. * @arg FLASH_FLAG_SIZERR: FLASH Size error flag
  681. * @arg FLASH_FLAG_PGSERR: FLASH Programming sequence error flag
  682. * @arg FLASH_FLAG_MISERR: FLASH Fast programming data miss error flag
  683. * @arg FLASH_FLAG_FASTERR: FLASH Fast programming error flag
  684. * @arg FLASH_FLAG_RDERR: FLASH PCROP read error flag
  685. * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
  686. * @arg FLASH_FLAG_ECCC: FLASH one ECC error has been detected and corrected
  687. * @arg FLASH_FLAG_ECCD: FLASH two ECC errors have been detected
  688. * @arg FLASH_FLAG_ALL_ERRORS: FLASH All errors flags
  689. * @retval None
  690. */
  691. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { if(((__FLAG__) & (FLASH_FLAG_ECCC | FLASH_FLAG_ECCD)) != 0U) { SET_BIT(FLASH->ECCR, ((__FLAG__) & (FLASH_FLAG_ECCC | FLASH_FLAG_ECCD))); }\
  692. if(((__FLAG__) & ~(FLASH_FLAG_ECCC | FLASH_FLAG_ECCD)) != 0U) { WRITE_REG(FLASH->SR, ((__FLAG__) & ~(FLASH_FLAG_ECCC | FLASH_FLAG_ECCD))); }\
  693. } while(0)
  694. /**
  695. * @}
  696. */
  697. /* Include FLASH HAL Extended module */
  698. #include "stm32l4xx_hal_flash_ex.h"
  699. #include "stm32l4xx_hal_flash_ramfunc.h"
  700. /* Exported functions --------------------------------------------------------*/
  701. /** @addtogroup FLASH_Exported_Functions
  702. * @{
  703. */
  704. /* Program operation functions ***********************************************/
  705. /** @addtogroup FLASH_Exported_Functions_Group1
  706. * @{
  707. */
  708. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  709. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  710. /* FLASH IRQ handler method */
  711. void HAL_FLASH_IRQHandler(void);
  712. /* Callbacks in non blocking modes */
  713. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  714. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  715. /**
  716. * @}
  717. */
  718. /* Peripheral Control functions **********************************************/
  719. /** @addtogroup FLASH_Exported_Functions_Group2
  720. * @{
  721. */
  722. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  723. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  724. /* Option bytes control */
  725. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  726. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  727. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  728. /**
  729. * @}
  730. */
  731. /* Peripheral State functions ************************************************/
  732. /** @addtogroup FLASH_Exported_Functions_Group3
  733. * @{
  734. */
  735. uint32_t HAL_FLASH_GetError(void);
  736. /**
  737. * @}
  738. */
  739. /**
  740. * @}
  741. */
  742. /* Private variables ---------------------------------------------------------*/
  743. /** @addtogroup FLASH_Private_Variables FLASH Private Variables
  744. * @{
  745. */
  746. extern FLASH_ProcessTypeDef pFlash;
  747. /**
  748. * @}
  749. */
  750. /* Private function ----------------------------------------------------------*/
  751. /** @addtogroup FLASH_Private_Functions FLASH Private Functions
  752. * @{
  753. */
  754. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  755. /**
  756. * @}
  757. */
  758. /* Private constants --------------------------------------------------------*/
  759. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  760. * @{
  761. */
  762. #define FLASH_SIZE_DATA_REGISTER ((uint32_t)0x1FFF75E0)
  763. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  764. #define FLASH_SIZE (((((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) == 0x0000FFFFU)) ? (0x800U << 10U) : \
  765. (((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) << 10U))
  766. #elif defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  767. #define FLASH_SIZE (((((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) == 0x0000FFFFU)) ? (0x200U << 10U) : \
  768. (((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) << 10U))
  769. #elif defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx)
  770. #define FLASH_SIZE (((((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) == 0x0000FFFFU)) ? (0x100U << 10U) : \
  771. (((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) << 10U))
  772. #elif defined (STM32L412xx) || defined (STM32L422xx)
  773. #define FLASH_SIZE (((((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) == 0x0000FFFFU)) ? (0x80U << 10U) : \
  774. (((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) << 10U))
  775. #else
  776. #define FLASH_SIZE (((((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU))== 0x0000FFFFU)) ? (0x400U << 10U) : \
  777. (((*((uint32_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0000FFFFU)) << 10U))
  778. #endif
  779. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  780. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  781. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  782. #define FLASH_BANK_SIZE (FLASH_SIZE >> 1U)
  783. #else
  784. #define FLASH_BANK_SIZE (FLASH_SIZE)
  785. #endif
  786. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  787. #define FLASH_PAGE_SIZE ((uint32_t)0x1000)
  788. #define FLASH_PAGE_SIZE_128_BITS ((uint32_t)0x2000)
  789. #else
  790. #define FLASH_PAGE_SIZE ((uint32_t)0x800)
  791. #endif
  792. #define FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
  793. /**
  794. * @}
  795. */
  796. /* Private macros ------------------------------------------------------------*/
  797. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  798. * @{
  799. */
  800. #define IS_FLASH_TYPEERASE(VALUE) (((VALUE) == FLASH_TYPEERASE_PAGES) || \
  801. ((VALUE) == FLASH_TYPEERASE_MASSERASE))
  802. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  803. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  804. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  805. #define IS_FLASH_BANK(BANK) (((BANK) == FLASH_BANK_1) || \
  806. ((BANK) == FLASH_BANK_2) || \
  807. ((BANK) == FLASH_BANK_BOTH))
  808. #define IS_FLASH_BANK_EXCLUSIVE(BANK) (((BANK) == FLASH_BANK_1) || \
  809. ((BANK) == FLASH_BANK_2))
  810. #else
  811. #define IS_FLASH_BANK(BANK) ((BANK) == FLASH_BANK_1)
  812. #define IS_FLASH_BANK_EXCLUSIVE(BANK) ((BANK) == FLASH_BANK_1)
  813. #endif
  814. #define IS_FLASH_TYPEPROGRAM(VALUE) (((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD) || \
  815. ((VALUE) == FLASH_TYPEPROGRAM_FAST) || \
  816. ((VALUE) == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  817. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  818. #define IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS) (((ADDRESS) >= (FLASH_BASE)) && ((ADDRESS) <= (FLASH_BASE+0x1FFFFFU)))
  819. #else
  820. #define IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS) (((ADDRESS) >= (FLASH_BASE)) && ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x400U) ? \
  821. ((ADDRESS) <= (FLASH_BASE+0xFFFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? \
  822. ((ADDRESS) <= (FLASH_BASE+0x7FFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? \
  823. ((ADDRESS) <= (FLASH_BASE+0x3FFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x80U) ? \
  824. ((ADDRESS) <= (FLASH_BASE+0x1FFFFU)) : ((ADDRESS) <= (FLASH_BASE+0xFFFFFU)))))))
  825. #endif
  826. #define IS_FLASH_OTP_ADDRESS(ADDRESS) (((ADDRESS) >= 0x1FFF7000U) && ((ADDRESS) <= 0x1FFF73FFU))
  827. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) ((IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS)) || (IS_FLASH_OTP_ADDRESS(ADDRESS)))
  828. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  829. #define IS_FLASH_PAGE(PAGE) ((PAGE) < 256U)
  830. #elif defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  831. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x400U) ? ((PAGE) < 256U) : \
  832. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? ((PAGE) < 128U) : \
  833. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 64U) : \
  834. ((PAGE) < 256U)))))
  835. #elif defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  836. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? ((PAGE) < 256U) : \
  837. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 128U) : \
  838. ((PAGE) < 256U))))
  839. #else
  840. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 128U) : \
  841. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x80U) ? ((PAGE) < 64U) : \
  842. ((PAGE) < 128U))))
  843. #endif
  844. #define IS_OPTIONBYTE(VALUE) (((VALUE) <= (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_PCROP)))
  845. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  846. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  847. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  848. #define IS_OB_WRPAREA(VALUE) (((VALUE) == OB_WRPAREA_BANK1_AREAA) || ((VALUE) == OB_WRPAREA_BANK1_AREAB) || \
  849. ((VALUE) == OB_WRPAREA_BANK2_AREAA) || ((VALUE) == OB_WRPAREA_BANK2_AREAB))
  850. #else
  851. #define IS_OB_WRPAREA(VALUE) (((VALUE) == OB_WRPAREA_BANK1_AREAA) || ((VALUE) == OB_WRPAREA_BANK1_AREAB))
  852. #endif
  853. #define IS_OB_RDP_LEVEL(LEVEL) (((LEVEL) == OB_RDP_LEVEL_0) ||\
  854. ((LEVEL) == OB_RDP_LEVEL_1)/* ||\
  855. ((LEVEL) == OB_RDP_LEVEL_2)*/)
  856. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  857. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0xFFFFU) && ((TYPE) != 0U))
  858. #elif defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  859. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0x1FFFU) && ((TYPE) != 0U))
  860. #else
  861. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0x7E7FU) && ((TYPE) != 0U) && (((TYPE)&0x0180U) == 0U))
  862. #endif
  863. #define IS_OB_USER_BOR_LEVEL(LEVEL) (((LEVEL) == OB_BOR_LEVEL_0) || ((LEVEL) == OB_BOR_LEVEL_1) || \
  864. ((LEVEL) == OB_BOR_LEVEL_2) || ((LEVEL) == OB_BOR_LEVEL_3) || \
  865. ((LEVEL) == OB_BOR_LEVEL_4))
  866. #define IS_OB_USER_STOP(VALUE) (((VALUE) == OB_STOP_RST) || ((VALUE) == OB_STOP_NORST))
  867. #define IS_OB_USER_STANDBY(VALUE) (((VALUE) == OB_STANDBY_RST) || ((VALUE) == OB_STANDBY_NORST))
  868. #define IS_OB_USER_SHUTDOWN(VALUE) (((VALUE) == OB_SHUTDOWN_RST) || ((VALUE) == OB_SHUTDOWN_NORST))
  869. #define IS_OB_USER_IWDG(VALUE) (((VALUE) == OB_IWDG_HW) || ((VALUE) == OB_IWDG_SW))
  870. #define IS_OB_USER_IWDG_STOP(VALUE) (((VALUE) == OB_IWDG_STOP_FREEZE) || ((VALUE) == OB_IWDG_STOP_RUN))
  871. #define IS_OB_USER_IWDG_STDBY(VALUE) (((VALUE) == OB_IWDG_STDBY_FREEZE) || ((VALUE) == OB_IWDG_STDBY_RUN))
  872. #define IS_OB_USER_WWDG(VALUE) (((VALUE) == OB_WWDG_HW) || ((VALUE) == OB_WWDG_SW))
  873. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  874. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  875. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  876. #define IS_OB_USER_BFB2(VALUE) (((VALUE) == OB_BFB2_DISABLE) || ((VALUE) == OB_BFB2_ENABLE))
  877. #define IS_OB_USER_DUALBANK(VALUE) (((VALUE) == OB_DUALBANK_SINGLE) || ((VALUE) == OB_DUALBANK_DUAL))
  878. #endif
  879. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  880. #define IS_OB_USER_DBANK(VALUE) (((VALUE) == OB_DBANK_128_BITS) || ((VALUE) == OB_DBANK_64_BITS))
  881. #endif
  882. #define IS_OB_USER_BOOT1(VALUE) (((VALUE) == OB_BOOT1_SRAM) || ((VALUE) == OB_BOOT1_SYSTEM))
  883. #define IS_OB_USER_SRAM2_PARITY(VALUE) (((VALUE) == OB_SRAM2_PARITY_ENABLE) || ((VALUE) == OB_SRAM2_PARITY_DISABLE))
  884. #define IS_OB_USER_SRAM2_RST(VALUE) (((VALUE) == OB_SRAM2_RST_ERASE) || ((VALUE) == OB_SRAM2_RST_NOT_ERASE))
  885. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || \
  886. defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  887. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4R5xx) || \
  888. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  889. #define IS_OB_USER_SWBOOT0(VALUE) (((VALUE) == OB_BOOT0_FROM_OB) || ((VALUE) == OB_BOOT0_FROM_PIN))
  890. #define IS_OB_USER_BOOT0(VALUE) (((VALUE) == OB_BOOT0_RESET) || ((VALUE) == OB_BOOT0_SET))
  891. #endif
  892. #define IS_OB_PCROP_RDP(VALUE) (((VALUE) == OB_PCROP_RDP_NOT_ERASE) || ((VALUE) == OB_PCROP_RDP_ERASE))
  893. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  894. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || ((LATENCY) == FLASH_LATENCY_1) || \
  895. ((LATENCY) == FLASH_LATENCY_2) || ((LATENCY) == FLASH_LATENCY_3) || \
  896. ((LATENCY) == FLASH_LATENCY_4) || ((LATENCY) == FLASH_LATENCY_5) || \
  897. ((LATENCY) == FLASH_LATENCY_6) || ((LATENCY) == FLASH_LATENCY_7) || \
  898. ((LATENCY) == FLASH_LATENCY_8) || ((LATENCY) == FLASH_LATENCY_9) || \
  899. ((LATENCY) == FLASH_LATENCY_10) || ((LATENCY) == FLASH_LATENCY_11) || \
  900. ((LATENCY) == FLASH_LATENCY_12) || ((LATENCY) == FLASH_LATENCY_13) || \
  901. ((LATENCY) == FLASH_LATENCY_14) || ((LATENCY) == FLASH_LATENCY_15))
  902. #else
  903. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || \
  904. ((LATENCY) == FLASH_LATENCY_1) || \
  905. ((LATENCY) == FLASH_LATENCY_2) || \
  906. ((LATENCY) == FLASH_LATENCY_3) || \
  907. ((LATENCY) == FLASH_LATENCY_4))
  908. #endif
  909. /**
  910. * @}
  911. */
  912. /**
  913. * @}
  914. */
  915. /**
  916. * @}
  917. */
  918. /**
  919. * @}
  920. */
  921. #ifdef __cplusplus
  922. }
  923. #endif
  924. #endif /* __STM32L4xx_HAL_FLASH_H */
  925. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/