stm32l4xx_hal_flash.h 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. * Copyright (c) 2017 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 STM32L4xx_HAL_FLASH_H
  19. #define STM32L4xx_HAL_FLASH_H
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32l4xx_hal_def.h"
  25. /** @addtogroup STM32L4xx_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 disabled
  46. This parameter must be a value between 0 and (max number of pages in the bank - 1)
  47. (eg : 255 for 1MB dual bank) */
  48. uint32_t NbPages; /*!< Number of pages to be erased.
  49. This parameter must be a value between 1 and (max number of pages in the bank - value of initial page)*/
  50. } FLASH_EraseInitTypeDef;
  51. /**
  52. * @brief FLASH Option Bytes Program structure definition
  53. */
  54. typedef struct
  55. {
  56. uint32_t OptionType; /*!< Option byte to be configured.
  57. This parameter can be a combination of the values of @ref FLASH_OB_Type */
  58. uint32_t WRPArea; /*!< Write protection area to be programmed (used for OPTIONBYTE_WRP).
  59. Only one WRP area could be programmed at the same time.
  60. This parameter can be value of @ref FLASH_OB_WRP_Area */
  61. uint32_t WRPStartOffset; /*!< Write protection start offset (used for OPTIONBYTE_WRP).
  62. This parameter must be a value between 0 and (max number of pages in the bank - 1)
  63. (eg : 25 for 1MB dual bank) */
  64. uint32_t WRPEndOffset; /*!< Write protection end offset (used for OPTIONBYTE_WRP).
  65. This parameter must be a value between WRPStartOffset and (max number of pages in the bank - 1) */
  66. uint32_t RDPLevel; /*!< Set the read protection level.. (used for OPTIONBYTE_RDP).
  67. This parameter can be a value of @ref FLASH_OB_Read_Protection */
  68. uint32_t USERType; /*!< User option byte(s) to be configured (used for OPTIONBYTE_USER).
  69. This parameter can be a combination of @ref FLASH_OB_USER_Type */
  70. uint32_t USERConfig; /*!< Value of the user option byte (used for OPTIONBYTE_USER).
  71. This parameter can be a combination of @ref FLASH_OB_USER_BOR_LEVEL,
  72. @ref FLASH_OB_USER_nRST_STOP, @ref FLASH_OB_USER_nRST_STANDBY,
  73. @ref FLASH_OB_USER_nRST_SHUTDOWN, @ref FLASH_OB_USER_IWDG_SW,
  74. @ref FLASH_OB_USER_IWDG_STOP, @ref FLASH_OB_USER_IWDG_STANDBY,
  75. @ref FLASH_OB_USER_WWDG_SW, @ref FLASH_OB_USER_BFB2,
  76. @ref FLASH_OB_USER_DUALBANK, @ref FLASH_OB_USER_nBOOT1,
  77. @ref FLASH_OB_USER_SRAM2_PE, @ref FLASH_OB_USER_SRAM2_RST,
  78. @ref FLASH_OB_USER_nSWBOOT0 and @ref FLASH_OB_USER_nBOOT0 */
  79. uint32_t PCROPConfig; /*!< Configuration of the PCROP (used for OPTIONBYTE_PCROP).
  80. This parameter must be a combination of @ref FLASH_Banks (except FLASH_BANK_BOTH)
  81. and @ref FLASH_OB_PCROP_RDP */
  82. uint32_t PCROPStartAddr; /*!< PCROP Start address (used for OPTIONBYTE_PCROP).
  83. This parameter must be a value between begin and end of bank
  84. => Be careful of the bank swapping for the address */
  85. uint32_t PCROPEndAddr; /*!< PCROP End address (used for OPTIONBYTE_PCROP).
  86. This parameter must be a value between PCROP Start address and end of bank */
  87. } FLASH_OBProgramInitTypeDef;
  88. /**
  89. * @brief FLASH Procedure structure definition
  90. */
  91. typedef enum
  92. {
  93. FLASH_PROC_NONE = 0,
  94. FLASH_PROC_PAGE_ERASE,
  95. FLASH_PROC_MASS_ERASE,
  96. FLASH_PROC_PROGRAM,
  97. FLASH_PROC_PROGRAM_LAST
  98. } FLASH_ProcedureTypeDef;
  99. /**
  100. * @brief FLASH Cache structure definition
  101. */
  102. typedef enum
  103. {
  104. FLASH_CACHE_DISABLED = 0,
  105. FLASH_CACHE_ICACHE_ENABLED,
  106. FLASH_CACHE_DCACHE_ENABLED,
  107. FLASH_CACHE_ICACHE_DCACHE_ENABLED
  108. } FLASH_CacheTypeDef;
  109. /**
  110. * @brief FLASH handle Structure definition
  111. */
  112. typedef struct
  113. {
  114. HAL_LockTypeDef Lock; /* FLASH locking object */
  115. __IO uint32_t ErrorCode; /* FLASH error code */
  116. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
  117. __IO uint32_t Address; /* Internal variable to save address selected for program in IT context */
  118. __IO uint32_t Bank; /* Internal variable to save current bank selected during erase in IT context */
  119. __IO uint32_t Page; /* Internal variable to define the current page which is erasing in IT context */
  120. __IO uint32_t NbPagesToErase; /* Internal variable to save the remaining pages to erase in IT context */
  121. __IO FLASH_CacheTypeDef CacheToReactivate; /* Internal variable to indicate which caches should be reactivated */
  122. }FLASH_ProcessTypeDef;
  123. /**
  124. * @}
  125. */
  126. /* Exported constants --------------------------------------------------------*/
  127. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  128. * @{
  129. */
  130. /** @defgroup FLASH_Error FLASH Error
  131. * @{
  132. */
  133. #define HAL_FLASH_ERROR_NONE 0x00000000U
  134. #define HAL_FLASH_ERROR_OP FLASH_FLAG_OPERR
  135. #define HAL_FLASH_ERROR_PROG FLASH_FLAG_PROGERR
  136. #define HAL_FLASH_ERROR_WRP FLASH_FLAG_WRPERR
  137. #define HAL_FLASH_ERROR_PGA FLASH_FLAG_PGAERR
  138. #define HAL_FLASH_ERROR_SIZ FLASH_FLAG_SIZERR
  139. #define HAL_FLASH_ERROR_PGS FLASH_FLAG_PGSERR
  140. #define HAL_FLASH_ERROR_MIS FLASH_FLAG_MISERR
  141. #define HAL_FLASH_ERROR_FAST FLASH_FLAG_FASTERR
  142. #define HAL_FLASH_ERROR_RD FLASH_FLAG_RDERR
  143. #define HAL_FLASH_ERROR_OPTV FLASH_FLAG_OPTVERR
  144. #define HAL_FLASH_ERROR_ECCC FLASH_FLAG_ECCC
  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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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 (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  395. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  396. /** @defgroup FLASH_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 FLASH_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 (STM32L4P5xx) || defined (STM32L4Q5xx) || 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) || \
  479. defined (STM32L4P5xx) || defined (STM32L4Q5xx) || 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. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || \
  493. defined (STM32L4S7xx) || defined (STM32L4S9xx)
  494. #define FLASH_FLAG_ECCC (FLASH_ECCR_ECCC | FLASH_ECCR_ECCC2) /*!< FLASH ECC correction */
  495. #define FLASH_FLAG_ECCD (FLASH_ECCR_ECCD | FLASH_ECCR_ECCD2) /*!< FLASH ECC detection */
  496. #else
  497. #define FLASH_FLAG_ECCC FLASH_ECCR_ECCC /*!< FLASH ECC correction */
  498. #define FLASH_FLAG_ECCD FLASH_ECCR_ECCD /*!< FLASH ECC detection */
  499. #endif
  500. #define FLASH_FLAG_ECCR_ERRORS (FLASH_FLAG_ECCD | FLASH_FLAG_ECCC)
  501. #define FLASH_FLAG_ALL_ERRORS (FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR | FLASH_FLAG_WRPERR | \
  502. FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_PGSERR | \
  503. FLASH_FLAG_MISERR | FLASH_FLAG_FASTERR | FLASH_FLAG_RDERR | \
  504. FLASH_FLAG_OPTVERR | FLASH_FLAG_ECCR_ERRORS)
  505. /**
  506. * @}
  507. */
  508. /** @defgroup FLASH_Interrupt_definition FLASH Interrupts Definition
  509. * @brief FLASH Interrupt definition
  510. * @{
  511. */
  512. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  513. #define FLASH_IT_OPERR FLASH_CR_ERRIE /*!< Error Interrupt source */
  514. #define FLASH_IT_RDERR FLASH_CR_RDERRIE /*!< PCROP Read Error Interrupt source*/
  515. #define FLASH_IT_ECCC (FLASH_ECCR_ECCIE >> 24) /*!< ECC Correction Interrupt source */
  516. /**
  517. * @}
  518. */
  519. /* Exported macros -----------------------------------------------------------*/
  520. /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
  521. * @brief macros to control FLASH features
  522. * @{
  523. */
  524. /**
  525. * @brief Set the FLASH Latency.
  526. * @param __LATENCY__ FLASH Latency
  527. * This parameter can be one of the following values :
  528. * @arg FLASH_LATENCY_0: FLASH Zero wait state
  529. * @arg FLASH_LATENCY_1: FLASH One wait state
  530. * @arg FLASH_LATENCY_2: FLASH Two wait states
  531. * @arg FLASH_LATENCY_3: FLASH Three wait states
  532. * @arg FLASH_LATENCY_4: FLASH Four wait states
  533. * @retval None
  534. */
  535. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (__LATENCY__)))
  536. /**
  537. * @brief Get the FLASH Latency.
  538. * @retval FLASH Latency
  539. * This parameter can be one of the following values :
  540. * @arg FLASH_LATENCY_0: FLASH Zero wait state
  541. * @arg FLASH_LATENCY_1: FLASH One wait state
  542. * @arg FLASH_LATENCY_2: FLASH Two wait states
  543. * @arg FLASH_LATENCY_3: FLASH Three wait states
  544. * @arg FLASH_LATENCY_4: FLASH Four wait states
  545. */
  546. #define __HAL_FLASH_GET_LATENCY() READ_BIT(FLASH->ACR, FLASH_ACR_LATENCY)
  547. /**
  548. * @brief Enable the FLASH prefetch buffer.
  549. * @retval None
  550. */
  551. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  552. /**
  553. * @brief Disable the FLASH prefetch buffer.
  554. * @retval None
  555. */
  556. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_PRFTEN)
  557. /**
  558. * @brief Enable the FLASH instruction cache.
  559. * @retval none
  560. */
  561. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  562. /**
  563. * @brief Disable the FLASH instruction cache.
  564. * @retval none
  565. */
  566. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICEN)
  567. /**
  568. * @brief Enable the FLASH data cache.
  569. * @retval none
  570. */
  571. #define __HAL_FLASH_DATA_CACHE_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_DCEN)
  572. /**
  573. * @brief Disable the FLASH data cache.
  574. * @retval none
  575. */
  576. #define __HAL_FLASH_DATA_CACHE_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_DCEN)
  577. /**
  578. * @brief Reset the FLASH instruction Cache.
  579. * @note This function must be used only when the Instruction Cache is disabled.
  580. * @retval None
  581. */
  582. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do { SET_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  583. CLEAR_BIT(FLASH->ACR, FLASH_ACR_ICRST); \
  584. } while (0)
  585. /**
  586. * @brief Reset the FLASH data Cache.
  587. * @note This function must be used only when the data Cache is disabled.
  588. * @retval None
  589. */
  590. #define __HAL_FLASH_DATA_CACHE_RESET() do { SET_BIT(FLASH->ACR, FLASH_ACR_DCRST); \
  591. CLEAR_BIT(FLASH->ACR, FLASH_ACR_DCRST); \
  592. } while (0)
  593. /**
  594. * @brief Enable the FLASH power down during Low-power run mode.
  595. * @note Writing this bit to 0 this bit, automatically the keys are
  596. * loss and a new unlock sequence is necessary to re-write it to 1.
  597. */
  598. #define __HAL_FLASH_POWER_DOWN_ENABLE() do { WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY1); \
  599. WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY2); \
  600. SET_BIT(FLASH->ACR, FLASH_ACR_RUN_PD); \
  601. } while (0)
  602. /**
  603. * @brief Disable the FLASH power down during Low-power run mode.
  604. * @note Writing this bit to 0 this bit, automatically the keys are
  605. * loss and a new unlock sequence is necessary to re-write it to 1.
  606. */
  607. #define __HAL_FLASH_POWER_DOWN_DISABLE() do { WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY1); \
  608. WRITE_REG(FLASH->PDKEYR, FLASH_PDKEY2); \
  609. CLEAR_BIT(FLASH->ACR, FLASH_ACR_RUN_PD); \
  610. } while (0)
  611. /**
  612. * @brief Enable the FLASH power down during Low-Power sleep mode
  613. * @retval none
  614. */
  615. #define __HAL_FLASH_SLEEP_POWERDOWN_ENABLE() SET_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
  616. /**
  617. * @brief Disable the FLASH power down during Low-Power sleep mode
  618. * @retval none
  619. */
  620. #define __HAL_FLASH_SLEEP_POWERDOWN_DISABLE() CLEAR_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
  621. /**
  622. * @}
  623. */
  624. /** @defgroup FLASH_Interrupt FLASH Interrupts Macros
  625. * @brief macros to handle FLASH interrupts
  626. * @{
  627. */
  628. /**
  629. * @brief Enable the specified FLASH interrupt.
  630. * @param __INTERRUPT__ FLASH interrupt
  631. * This parameter can be any combination of the following values:
  632. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  633. * @arg FLASH_IT_OPERR: Error Interrupt
  634. * @arg FLASH_IT_RDERR: PCROP Read Error Interrupt
  635. * @arg FLASH_IT_ECCC: ECC Correction Interrupt
  636. * @retval none
  637. */
  638. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & FLASH_IT_ECCC) != 0U) { SET_BIT(FLASH->ECCR, FLASH_ECCR_ECCIE); }\
  639. if(((__INTERRUPT__) & (~FLASH_IT_ECCC)) != 0U) { SET_BIT(FLASH->CR, ((__INTERRUPT__) & (~FLASH_IT_ECCC))); }\
  640. } while(0)
  641. /**
  642. * @brief Disable the specified FLASH interrupt.
  643. * @param __INTERRUPT__ FLASH interrupt
  644. * This parameter can be any combination of the following values:
  645. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  646. * @arg FLASH_IT_OPERR: Error Interrupt
  647. * @arg FLASH_IT_RDERR: PCROP Read Error Interrupt
  648. * @arg FLASH_IT_ECCC: ECC Correction Interrupt
  649. * @retval none
  650. */
  651. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) do { if(((__INTERRUPT__) & FLASH_IT_ECCC) != 0U) { CLEAR_BIT(FLASH->ECCR, FLASH_ECCR_ECCIE); }\
  652. if(((__INTERRUPT__) & (~FLASH_IT_ECCC)) != 0U) { CLEAR_BIT(FLASH->CR, ((__INTERRUPT__) & (~FLASH_IT_ECCC))); }\
  653. } while(0)
  654. /**
  655. * @brief Check whether the specified FLASH flag is set or not.
  656. * @param __FLAG__ specifies the FLASH flag to check.
  657. * This parameter can be one of the following values:
  658. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  659. * @arg FLASH_FLAG_OPERR: FLASH Operation error flag
  660. * @arg FLASH_FLAG_PROGERR: FLASH Programming error flag
  661. * @arg FLASH_FLAG_WRPERR: FLASH Write protection error flag
  662. * @arg FLASH_FLAG_PGAERR: FLASH Programming alignment error flag
  663. * @arg FLASH_FLAG_SIZERR: FLASH Size error flag
  664. * @arg FLASH_FLAG_PGSERR: FLASH Programming sequence error flag
  665. * @arg FLASH_FLAG_MISERR: FLASH Fast programming data miss error flag
  666. * @arg FLASH_FLAG_FASTERR: FLASH Fast programming error flag
  667. * @arg FLASH_FLAG_RDERR: FLASH PCROP read error flag
  668. * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
  669. * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
  670. * @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)
  671. * @arg FLASH_FLAG_ECCC: FLASH one ECC error has been detected and corrected
  672. * @arg FLASH_FLAG_ECCD: FLASH two ECC errors have been detected
  673. * @retval The new state of FLASH_FLAG (SET or RESET).
  674. */
  675. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((((__FLAG__) & FLASH_FLAG_ECCR_ERRORS) != 0U) ? \
  676. (READ_BIT(FLASH->ECCR, (__FLAG__)) != 0U) : \
  677. (READ_BIT(FLASH->SR, (__FLAG__)) != 0U))
  678. /**
  679. * @brief Clear the FLASH's pending flags.
  680. * @param __FLAG__ specifies the FLASH flags to clear.
  681. * This parameter can be any combination of the following values:
  682. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  683. * @arg FLASH_FLAG_OPERR: FLASH Operation error flag
  684. * @arg FLASH_FLAG_PROGERR: FLASH Programming error flag
  685. * @arg FLASH_FLAG_WRPERR: FLASH Write protection error flag
  686. * @arg FLASH_FLAG_PGAERR: FLASH Programming alignment error flag
  687. * @arg FLASH_FLAG_SIZERR: FLASH Size error flag
  688. * @arg FLASH_FLAG_PGSERR: FLASH Programming sequence error flag
  689. * @arg FLASH_FLAG_MISERR: FLASH Fast programming data miss error flag
  690. * @arg FLASH_FLAG_FASTERR: FLASH Fast programming error flag
  691. * @arg FLASH_FLAG_RDERR: FLASH PCROP read error flag
  692. * @arg FLASH_FLAG_OPTVERR: FLASH Option validity error flag
  693. * @arg FLASH_FLAG_ECCC: FLASH one ECC error has been detected and corrected
  694. * @arg FLASH_FLAG_ECCD: FLASH two ECC errors have been detected
  695. * @arg FLASH_FLAG_ALL_ERRORS: FLASH All errors flags
  696. * @retval None
  697. */
  698. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) do { if(((__FLAG__) & FLASH_FLAG_ECCR_ERRORS) != 0U) { SET_BIT(FLASH->ECCR, ((__FLAG__) & FLASH_FLAG_ECCR_ERRORS)); }\
  699. if(((__FLAG__) & ~(FLASH_FLAG_ECCR_ERRORS)) != 0U) { WRITE_REG(FLASH->SR, ((__FLAG__) & ~(FLASH_FLAG_ECCR_ERRORS))); }\
  700. } while(0)
  701. /**
  702. * @}
  703. */
  704. /* Include FLASH HAL Extended module */
  705. #include "stm32l4xx_hal_flash_ex.h"
  706. #include "stm32l4xx_hal_flash_ramfunc.h"
  707. /* Exported functions --------------------------------------------------------*/
  708. /** @addtogroup FLASH_Exported_Functions
  709. * @{
  710. */
  711. /* Program operation functions ***********************************************/
  712. /** @addtogroup FLASH_Exported_Functions_Group1
  713. * @{
  714. */
  715. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  716. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  717. /* FLASH IRQ handler method */
  718. void HAL_FLASH_IRQHandler(void);
  719. /* Callbacks in non blocking modes */
  720. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  721. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  722. /**
  723. * @}
  724. */
  725. /* Peripheral Control functions **********************************************/
  726. /** @addtogroup FLASH_Exported_Functions_Group2
  727. * @{
  728. */
  729. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  730. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  731. /* Option bytes control */
  732. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  733. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  734. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  735. /**
  736. * @}
  737. */
  738. /* Peripheral State functions ************************************************/
  739. /** @addtogroup FLASH_Exported_Functions_Group3
  740. * @{
  741. */
  742. uint32_t HAL_FLASH_GetError(void);
  743. /**
  744. * @}
  745. */
  746. /**
  747. * @}
  748. */
  749. /* Private variables ---------------------------------------------------------*/
  750. /** @addtogroup FLASH_Private_Variables FLASH Private Variables
  751. * @{
  752. */
  753. extern FLASH_ProcessTypeDef pFlash;
  754. /**
  755. * @}
  756. */
  757. /* Private function ----------------------------------------------------------*/
  758. /** @addtogroup FLASH_Private_Functions FLASH Private Functions
  759. * @{
  760. */
  761. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  762. /**
  763. * @}
  764. */
  765. /* Private constants --------------------------------------------------------*/
  766. /** @defgroup FLASH_Private_Constants FLASH Private Constants
  767. * @{
  768. */
  769. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  770. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  771. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  772. #define FLASH_BANK_SIZE (FLASH_SIZE >> 1U)
  773. #else
  774. #define FLASH_BANK_SIZE (FLASH_SIZE)
  775. #endif
  776. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  777. #define FLASH_PAGE_SIZE ((uint32_t)0x1000)
  778. #define FLASH_PAGE_SIZE_128_BITS ((uint32_t)0x2000)
  779. #else
  780. #define FLASH_PAGE_SIZE ((uint32_t)0x800)
  781. #endif
  782. #define FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
  783. /**
  784. * @}
  785. */
  786. /* Private macros ------------------------------------------------------------*/
  787. /** @defgroup FLASH_Private_Macros FLASH Private Macros
  788. * @{
  789. */
  790. #define IS_FLASH_TYPEERASE(VALUE) (((VALUE) == FLASH_TYPEERASE_PAGES) || \
  791. ((VALUE) == FLASH_TYPEERASE_MASSERASE))
  792. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  793. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  794. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  795. #define IS_FLASH_BANK(BANK) (((BANK) == FLASH_BANK_1) || \
  796. ((BANK) == FLASH_BANK_2) || \
  797. ((BANK) == FLASH_BANK_BOTH))
  798. #define IS_FLASH_BANK_EXCLUSIVE(BANK) (((BANK) == FLASH_BANK_1) || \
  799. ((BANK) == FLASH_BANK_2))
  800. #else
  801. #define IS_FLASH_BANK(BANK) ((BANK) == FLASH_BANK_1)
  802. #define IS_FLASH_BANK_EXCLUSIVE(BANK) ((BANK) == FLASH_BANK_1)
  803. #endif
  804. #define IS_FLASH_TYPEPROGRAM(VALUE) (((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD) || \
  805. ((VALUE) == FLASH_TYPEPROGRAM_FAST) || \
  806. ((VALUE) == FLASH_TYPEPROGRAM_FAST_AND_LAST))
  807. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  808. #define IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS) (((ADDRESS) >= (FLASH_BASE)) && ((ADDRESS) <= (FLASH_BASE+0x1FFFFFU)))
  809. #else
  810. #define IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS) (((ADDRESS) >= (FLASH_BASE)) && ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x400U) ? \
  811. ((ADDRESS) <= (FLASH_BASE+0xFFFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? \
  812. ((ADDRESS) <= (FLASH_BASE+0x7FFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? \
  813. ((ADDRESS) <= (FLASH_BASE+0x3FFFFU)) : ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x80U) ? \
  814. ((ADDRESS) <= (FLASH_BASE+0x1FFFFU)) : ((ADDRESS) <= (FLASH_BASE+0xFFFFFU)))))))
  815. #endif
  816. #define IS_FLASH_OTP_ADDRESS(ADDRESS) (((ADDRESS) >= 0x1FFF7000U) && ((ADDRESS) <= 0x1FFF73FFU))
  817. #define IS_FLASH_PROGRAM_ADDRESS(ADDRESS) ((IS_FLASH_MAIN_MEM_ADDRESS(ADDRESS)) || (IS_FLASH_OTP_ADDRESS(ADDRESS)))
  818. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  819. #define IS_FLASH_PAGE(PAGE) ((PAGE) < 256U)
  820. #elif defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  821. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x400U) ? ((PAGE) < 256U) : \
  822. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? ((PAGE) < 128U) : \
  823. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 64U) : \
  824. ((PAGE) < 256U)))))
  825. #elif defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
  826. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x200U) ? ((PAGE) < 256U) : \
  827. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 128U) : \
  828. ((PAGE) < 256U))))
  829. #else
  830. #define IS_FLASH_PAGE(PAGE) (((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x100U) ? ((PAGE) < 128U) : \
  831. ((((*((uint16_t *)FLASH_SIZE_DATA_REGISTER)) & (0x0FFFU)) == 0x80U) ? ((PAGE) < 64U) : \
  832. ((PAGE) < 128U))))
  833. #endif
  834. #define IS_OPTIONBYTE(VALUE) (((VALUE) <= (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_PCROP)))
  835. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  836. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  837. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  838. #define IS_OB_WRPAREA(VALUE) (((VALUE) == OB_WRPAREA_BANK1_AREAA) || ((VALUE) == OB_WRPAREA_BANK1_AREAB) || \
  839. ((VALUE) == OB_WRPAREA_BANK2_AREAA) || ((VALUE) == OB_WRPAREA_BANK2_AREAB))
  840. #else
  841. #define IS_OB_WRPAREA(VALUE) (((VALUE) == OB_WRPAREA_BANK1_AREAA) || ((VALUE) == OB_WRPAREA_BANK1_AREAB))
  842. #endif
  843. #define IS_OB_RDP_LEVEL(LEVEL) (((LEVEL) == OB_RDP_LEVEL_0) ||\
  844. ((LEVEL) == OB_RDP_LEVEL_1)/* ||\
  845. ((LEVEL) == OB_RDP_LEVEL_2)*/)
  846. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  847. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0xFFFFU) && ((TYPE) != 0U))
  848. #elif defined(STM32L471xx) || defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || defined(STM32L496xx) || defined(STM32L4A6xx)
  849. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0x1FFFU) && ((TYPE) != 0U))
  850. #else
  851. #define IS_OB_USER_TYPE(TYPE) (((TYPE) <= (uint32_t)0x7E7FU) && ((TYPE) != 0U) && (((TYPE)&0x0180U) == 0U))
  852. #endif
  853. #define IS_OB_USER_BOR_LEVEL(LEVEL) (((LEVEL) == OB_BOR_LEVEL_0) || ((LEVEL) == OB_BOR_LEVEL_1) || \
  854. ((LEVEL) == OB_BOR_LEVEL_2) || ((LEVEL) == OB_BOR_LEVEL_3) || \
  855. ((LEVEL) == OB_BOR_LEVEL_4))
  856. #define IS_OB_USER_STOP(VALUE) (((VALUE) == OB_STOP_RST) || ((VALUE) == OB_STOP_NORST))
  857. #define IS_OB_USER_STANDBY(VALUE) (((VALUE) == OB_STANDBY_RST) || ((VALUE) == OB_STANDBY_NORST))
  858. #define IS_OB_USER_SHUTDOWN(VALUE) (((VALUE) == OB_SHUTDOWN_RST) || ((VALUE) == OB_SHUTDOWN_NORST))
  859. #define IS_OB_USER_IWDG(VALUE) (((VALUE) == OB_IWDG_HW) || ((VALUE) == OB_IWDG_SW))
  860. #define IS_OB_USER_IWDG_STOP(VALUE) (((VALUE) == OB_IWDG_STOP_FREEZE) || ((VALUE) == OB_IWDG_STOP_RUN))
  861. #define IS_OB_USER_IWDG_STDBY(VALUE) (((VALUE) == OB_IWDG_STDBY_FREEZE) || ((VALUE) == OB_IWDG_STDBY_RUN))
  862. #define IS_OB_USER_WWDG(VALUE) (((VALUE) == OB_WWDG_HW) || ((VALUE) == OB_WWDG_SW))
  863. #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
  864. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  865. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  866. #define IS_OB_USER_BFB2(VALUE) (((VALUE) == OB_BFB2_DISABLE) || ((VALUE) == OB_BFB2_ENABLE))
  867. #define IS_OB_USER_DUALBANK(VALUE) (((VALUE) == OB_DUALBANK_SINGLE) || ((VALUE) == OB_DUALBANK_DUAL))
  868. #endif
  869. #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  870. #define IS_OB_USER_DBANK(VALUE) (((VALUE) == OB_DBANK_128_BITS) || ((VALUE) == OB_DBANK_64_BITS))
  871. #endif
  872. #define IS_OB_USER_BOOT1(VALUE) (((VALUE) == OB_BOOT1_SRAM) || ((VALUE) == OB_BOOT1_SYSTEM))
  873. #define IS_OB_USER_SRAM2_PARITY(VALUE) (((VALUE) == OB_SRAM2_PARITY_ENABLE) || ((VALUE) == OB_SRAM2_PARITY_DISABLE))
  874. #define IS_OB_USER_SRAM2_RST(VALUE) (((VALUE) == OB_SRAM2_RST_ERASE) || ((VALUE) == OB_SRAM2_RST_NOT_ERASE))
  875. #if defined (STM32L412xx) || defined (STM32L422xx) || defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || \
  876. defined (STM32L442xx) || defined (STM32L443xx) || defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx) || \
  877. defined (STM32L496xx) || defined (STM32L4A6xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || \
  878. defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  879. #define IS_OB_USER_SWBOOT0(VALUE) (((VALUE) == OB_BOOT0_FROM_OB) || ((VALUE) == OB_BOOT0_FROM_PIN))
  880. #define IS_OB_USER_BOOT0(VALUE) (((VALUE) == OB_BOOT0_RESET) || ((VALUE) == OB_BOOT0_SET))
  881. #endif
  882. #define IS_OB_PCROP_RDP(VALUE) (((VALUE) == OB_PCROP_RDP_NOT_ERASE) || ((VALUE) == OB_PCROP_RDP_ERASE))
  883. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) || defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
  884. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || ((LATENCY) == FLASH_LATENCY_1) || \
  885. ((LATENCY) == FLASH_LATENCY_2) || ((LATENCY) == FLASH_LATENCY_3) || \
  886. ((LATENCY) == FLASH_LATENCY_4) || ((LATENCY) == FLASH_LATENCY_5) || \
  887. ((LATENCY) == FLASH_LATENCY_6) || ((LATENCY) == FLASH_LATENCY_7) || \
  888. ((LATENCY) == FLASH_LATENCY_8) || ((LATENCY) == FLASH_LATENCY_9) || \
  889. ((LATENCY) == FLASH_LATENCY_10) || ((LATENCY) == FLASH_LATENCY_11) || \
  890. ((LATENCY) == FLASH_LATENCY_12) || ((LATENCY) == FLASH_LATENCY_13) || \
  891. ((LATENCY) == FLASH_LATENCY_14) || ((LATENCY) == FLASH_LATENCY_15))
  892. #else
  893. #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || \
  894. ((LATENCY) == FLASH_LATENCY_1) || \
  895. ((LATENCY) == FLASH_LATENCY_2) || \
  896. ((LATENCY) == FLASH_LATENCY_3) || \
  897. ((LATENCY) == FLASH_LATENCY_4))
  898. #endif
  899. /**
  900. * @}
  901. */
  902. /**
  903. * @}
  904. */
  905. /**
  906. * @}
  907. */
  908. /**
  909. * @}
  910. */
  911. #ifdef __cplusplus
  912. }
  913. #endif
  914. #endif /* STM32L4xx_HAL_FLASH_H */