stm32l4xx_hal_flash.h 53 KB

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