1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_flash_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of Flash HAL Extended module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 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 
18 /* Define to prevent recursive inclusion -------------------------------------*/
19 #ifndef __STM32L0xx_HAL_FLASH_EX_H
20 #define __STM32L0xx_HAL_FLASH_EX_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32l0xx_hal_def.h"
28 
29 /** @addtogroup STM32L0xx_HAL_Driver
30   * @{
31   */
32 
33 /** @addtogroup FLASHEx
34   * @{
35   */
36 
37 /** @addtogroup FLASHEx_Private_Constants
38   * @{
39   */
40 
41 #define FLASH_NBPAGES_MAX (FLASH_SIZE / FLASH_PAGE_SIZE)
42 
43 #define WRP_MASK_LOW                 (0x0000FFFFU)
44 #define WRP_MASK_HIGH                (0xFFFF0000U)
45 
46 /**
47   * @}
48   */
49 
50 /** @addtogroup FLASHEx_Private_Macros
51   * @{
52   */
53 
54 #define IS_FLASH_TYPEERASE(__VALUE__)   (((__VALUE__) == FLASH_TYPEERASE_PAGES))
55 
56 #define IS_OPTIONBYTE(__VALUE__) (((__VALUE__) <= (OPTIONBYTE_WRP  | OPTIONBYTE_RDP | \
57                                                   OPTIONBYTE_USER | OPTIONBYTE_BOR | OPTIONBYTE_BOOT_BIT1)))
58 
59 #define IS_WRPSTATE(__VALUE__)          (((__VALUE__) == OB_WRPSTATE_DISABLE) || \
60                                          ((__VALUE__) == OB_WRPSTATE_ENABLE))
61 
62 #define IS_OB_WRP(__PAGE__)             (((__PAGE__) != 0x0000000U))
63 
64 #define IS_OB_RDP(__LEVEL__)            (((__LEVEL__) == OB_RDP_LEVEL_0) ||\
65                                          ((__LEVEL__) == OB_RDP_LEVEL_1) ||\
66                                          ((__LEVEL__) == OB_RDP_LEVEL_2))
67 
68 #define IS_OB_BOR_LEVEL(__LEVEL__)      (((__LEVEL__) == OB_BOR_OFF)     || \
69                                          ((__LEVEL__) == OB_BOR_LEVEL1)  || \
70                                          ((__LEVEL__) == OB_BOR_LEVEL2)  || \
71                                          ((__LEVEL__) == OB_BOR_LEVEL3)  || \
72                                          ((__LEVEL__) == OB_BOR_LEVEL4)  || \
73                                          ((__LEVEL__) == OB_BOR_LEVEL5))
74 
75 #define IS_OB_IWDG_SOURCE(__SOURCE__)   (((__SOURCE__) == OB_IWDG_SW) || ((__SOURCE__) == OB_IWDG_HW))
76 
77 #define IS_OB_STOP_SOURCE(__SOURCE__)   (((__SOURCE__) == OB_STOP_NORST) || ((__SOURCE__) == OB_STOP_RST))
78 
79 #define IS_OB_STDBY_SOURCE(__SOURCE__)  (((__SOURCE__) == OB_STDBY_NORST) || ((__SOURCE__) == OB_STDBY_RST))
80 
81 #if defined(FLASH_OPTR_WPRMOD) && defined(FLASH_OPTR_BFB2)
82 
83 #define IS_OBEX(__VALUE__)              (((__VALUE__) <= (OPTIONBYTE_PCROP | OPTIONBYTE_BOOTCONFIG)) && ((__VALUE__) != 0U))
84 
85 #elif defined(FLASH_OPTR_WPRMOD) && !defined(FLASH_OPTR_BFB2)
86 
87 #define IS_OBEX(__VALUE__)              ((__VALUE__) == OPTIONBYTE_PCROP)
88 
89 #elif !defined(FLASH_OPTR_WPRMOD) && defined(FLASH_OPTR_BFB2)
90 
91 #define IS_OBEX(__VALUE__)              ((__VALUE__) == OPTIONBYTE_BOOTCONFIG)
92 
93 #endif /* FLASH_OPTR_WPRMOD && FLASH_OPTR_BFB2 */
94 
95 #if defined(FLASH_OPTR_WPRMOD)
96 
97 #define IS_PCROPSTATE(__VALUE__)        (((__VALUE__) == OB_PCROP_STATE_DISABLE) || \
98                                          ((__VALUE__) == OB_PCROP_STATE_ENABLE))
99 
100 #define IS_OB_PCROP(__PAGE__)           (((__PAGE__) != 0x0000000U))
101 #endif /* FLASH_OPTR_WPRMOD */
102 
103 #if defined(FLASH_OPTR_BFB2)
104 
105 #define IS_OB_BOOT_BANK(__BANK__)     (((__BANK__) == OB_BOOT_BANK2) || ((__BANK__) == OB_BOOT_BANK1))
106 
107 #endif /* FLASH_OPTR_BFB2 */
108 
109 #define IS_OB_BOOT1(__BOOT_BIT1__)  (((__BOOT_BIT1__) == OB_BOOT_BIT1_RESET) || ((__BOOT_BIT1__) == OB_BOOT_BIT1_SET))
110 #define IS_TYPEPROGRAMDATA(__VALUE__)   (((__VALUE__) == FLASH_TYPEPROGRAMDATA_BYTE)     || \
111                                          ((__VALUE__) == FLASH_TYPEPROGRAMDATA_HALFWORD) || \
112                                          ((__VALUE__) == FLASH_TYPEPROGRAMDATA_WORD))
113 
114 #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
115 
116 #define IS_FLASH_DATA_ADDRESS(__ADDRESS__)          (((__ADDRESS__) >= DATA_EEPROM_BASE) && ((__ADDRESS__) <= DATA_EEPROM_BANK2_END))
117 #define IS_FLASH_DATA_BANK1_ADDRESS(__ADDRESS__)    (((__ADDRESS__) >= DATA_EEPROM_BASE) && ((__ADDRESS__) <= DATA_EEPROM_BANK1_END))
118 #define IS_FLASH_DATA_BANK2_ADDRESS(__ADDRESS__)    (((__ADDRESS__) >= DATA_EEPROM_BANK2_BASE) && ((__ADDRESS__) <= DATA_EEPROM_BANK2_END))
119 #define IS_FLASH_PROGRAM_ADDRESS(__ADDRESS__)       (((__ADDRESS__) >= FLASH_BASE)       && ((__ADDRESS__) <  (FLASH_BASE + FLASH_SIZE)))
120 #define IS_FLASH_PROGRAM_BANK1_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= FLASH_BASE)       && ((__ADDRESS__) <  (FLASH_BASE + (FLASH_SIZE >> 1))))
121 #define IS_FLASH_PROGRAM_BANK2_ADDRESS(__ADDRESS__) (((__ADDRESS__) >= FLASH_BANK2_BASE) && ((__ADDRESS__) <  (FLASH_BASE + FLASH_SIZE)))
122 #else
123 #define IS_FLASH_DATA_ADDRESS(__ADDRESS__)          (((__ADDRESS__) >= DATA_EEPROM_BASE) && ((__ADDRESS__) <= DATA_EEPROM_END))
124 #define IS_FLASH_PROGRAM_ADDRESS(__ADDRESS__)       (((__ADDRESS__) >= FLASH_BASE)       && ((__ADDRESS__) <  (FLASH_BASE + FLASH_SIZE)))
125 #endif
126 
127 #define IS_NBPAGES(__PAGES__) (((__PAGES__) >= 1) && ((__PAGES__) <= FLASH_NBPAGES_MAX))
128 
129 /**
130   * @}
131   */
132 
133 /** @addtogroup FLASHEx_Private_Functions
134  * @{
135  */
136 
137 void       FLASH_PageErase(uint32_t PageAddress);
138 
139 /**
140   * @}
141   */
142 
143 /* Exported types ------------------------------------------------------------*/
144 
145 /** @defgroup FLASHEx_Exported_Types FLASHEx Exported Types
146   * @{
147   */
148 
149 /**
150   * @brief  FLASH Erase structure definition
151   */
152 typedef struct
153 {
154   uint32_t TypeErase;   /*!< TypeErase: Page Erase only.
155                              This parameter can be a value of @ref FLASHEx_Type_Erase */
156 
157   uint32_t PageAddress; /*!< PageAddress: Initial FLASH address to be erased
158                              This parameter must be a value belonging to FLASH Program address (depending on the devices)  */
159 
160   uint32_t NbPages;     /*!< NbPages: Number of pages to be erased.
161                              This parameter must be a value between 1 and (max number of pages - value of Initial page)*/
162 
163 } FLASH_EraseInitTypeDef;
164 
165 /**
166   * @brief  FLASH Option Bytes PROGRAM structure definition
167   */
168 typedef struct
169 {
170   uint32_t  OptionType;       /*!< OptionType: Option byte to be configured.
171                                    This parameter can be a value of @ref FLASHEx_Option_Type */
172 
173   uint32_t  WRPState;         /*!< WRPState: Write protection activation or deactivation.
174                                    This parameter can be a value of @ref FLASHEx_WRP_State */
175 
176   uint32_t WRPSector;         /*!< WRPSector: This bitfield specifies the sector (s) which are write protected.
177                                    This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection */
178 
179 #if defined(STM32L071xx) || defined(STM32L072xx) || defined(STM32L073xx) || defined(STM32L081xx) || defined(STM32L082xx) || defined(STM32L083xx)
180   uint32_t WRPSector2;        /*!< WRPSector2 : This bitfield specifies the sector(s) upper Sector31 which are write protected.
181                                    This parameter can be a combination of @ref FLASHEx_Option_Bytes_Write_Protection2 */
182 #endif
183 
184   uint8_t   RDPLevel;         /*!< RDPLevel: Set the read protection level.
185                                    This parameter can be a value of @ref FLASHEx_Option_Bytes_Read_Protection */
186 
187   uint8_t   BORLevel;         /*!< BORLevel: Set the BOR Level.
188                                    This parameter can be a value of @ref FLASHEx_Option_Bytes_BOR_Level */
189 
190   uint8_t   USERConfig;       /*!< USERConfig: Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
191                                    This parameter can be a combination of @ref FLASHEx_Option_Bytes_IWatchdog,
192                                    @ref FLASHEx_Option_Bytes_nRST_STOP and @ref FLASHEx_Option_Bytes_nRST_STDBY*/
193 
194   uint8_t BOOTBit1Config;     /*!< BOOT1Config: Together with input pad Boot0, this bit selects the boot source, flash, ram or system memory
195                                    This parameter can be a value of @ref FLASHEx_Option_Bytes_BOOTBit1 */
196 } FLASH_OBProgramInitTypeDef;
197 
198 #if defined(FLASH_OPTR_WPRMOD) || defined(FLASH_OPTR_BFB2)
199 /**
200   * @brief  FLASH Advanced Option Bytes Program structure definition
201   */
202 typedef struct
203 {
204   uint32_t OptionType;          /*!< OptionType: Option byte to be configured for extension .
205                                      This parameter can be a value of @ref FLASHEx_OptionAdv_Type */
206 
207 #if defined(FLASH_OPTR_WPRMOD)
208   uint32_t PCROPState;          /*!< PCROPState: PCROP activation or deactivation.
209                                      This parameter can be a value of @ref FLASHEx_PCROP_State */
210 
211   uint32_t PCROPSector;        /*!< PCROPSector : This bitfield specifies the sector(s) which are read/write protected.
212                                     This parameter can be a combination of @ref FLASHEx_Option_Bytes_PC_ReadWrite_Protection */
213 
214 #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
215   uint32_t PCROPSector2;       /*!< PCROPSector : This bitfield specifies the sector(s) upper Sector31 which are read/write protected.
216                                     This parameter can be a combination of @ref FLASHEx_Option_Bytes_PC_ReadWrite_Protection2 */
217 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
218 #endif /* FLASH_OPTR_WPRMOD */
219 
220 #if defined(FLASH_OPTR_BFB2)
221   uint16_t BootConfig;          /*!< BootConfig: specifies Option bytes for boot config
222                                      This parameter can be a value of @ref FLASHEx_Option_Bytes_BOOT */
223 #endif /* FLASH_OPTR_BFB2*/
224 } FLASH_AdvOBProgramInitTypeDef;
225 
226 /**
227   * @}
228   */
229 #endif /* FLASH_OPTR_WPRMOD || FLASH_OPTR_BFB2 */
230 
231 /* Exported constants --------------------------------------------------------*/
232 
233 
234 /** @defgroup FLASHEx_Exported_Constants FLASHEx Exported Constants
235   * @{
236   */
237 
238 /** @defgroup FLASHEx_Type_Erase FLASHEx_Type_Erase
239   * @{
240   */
241 #define FLASH_TYPEERASE_PAGES           (0x00U)  /*!<Page erase only*/
242 
243 /**
244   * @}
245   */
246 
247 /** @defgroup FLASHEx_Option_Type FLASHEx Option Type
248   * @{
249   */
250 #define OPTIONBYTE_WRP            (0x01U)  /*!<WRP option byte configuration*/
251 #define OPTIONBYTE_RDP            (0x02U)  /*!<RDP option byte configuration*/
252 #define OPTIONBYTE_USER           (0x04U)  /*!<USER option byte configuration*/
253 #define OPTIONBYTE_BOR            (0x08U)  /*!<BOR option byte configuration*/
254 #define OPTIONBYTE_BOOT_BIT1      (0x10U)  /*!< BOOT PIN1 option byte configuration*/
255 
256 /**
257   * @}
258   */
259 
260 /** @defgroup FLASHEx_WRP_State FLASHEx WRP State
261   * @{
262   */
263 #define OB_WRPSTATE_DISABLE        (0x00U)  /*!<Disable the write protection of the desired sectors*/
264 #define OB_WRPSTATE_ENABLE         (0x01U)  /*!<Enable the write protection of the desired sectors*/
265 
266 /**
267   * @}
268   */
269 
270 #if defined (STM32L011xx) || defined (STM32L021xx) || defined (STM32L031xx) || defined (STM32L041xx)
271 /** @defgroup FLASHEx_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
272   * @{
273   */
274 #define OB_WRP_Pages0to31          (0x00000001U) /* Write protection of Sector0 */
275 #define OB_WRP_Pages32to63         (0x00000002U) /* Write protection of Sector1 */
276 #define OB_WRP_Pages64to95         (0x00000004U) /* Write protection of Sector2 */
277 #define OB_WRP_Pages96to127        (0x00000008U) /* Write protection of Sector3 */
278 #define OB_WRP_Pages128to159       (0x00000010U) /* Write protection of Sector4 */
279 #define OB_WRP_Pages160to191       (0x00000020U) /* Write protection of Sector5 */
280 #define OB_WRP_Pages192to223       (0x00000040U) /* Write protection of Sector6 */
281 #define OB_WRP_Pages224to255       (0x00000080U) /* Write protection of Sector7 */
282 #define OB_WRP_AllPages            (0x000000FFU) /*!< Write protection of all Sectors */
283 /**
284   * @}
285   */
286 #elif defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L062xx) || defined (STM32L063xx)
287 /** @defgroup FLASHEx_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
288   * @{
289   */
290 #define OB_WRP_Pages0to31          (0x00000001U) /* Write protection of Sector0 */
291 #define OB_WRP_Pages32to63         (0x00000002U) /* Write protection of Sector1 */
292 #define OB_WRP_Pages64to95         (0x00000004U) /* Write protection of Sector2 */
293 #define OB_WRP_Pages96to127        (0x00000008U) /* Write protection of Sector3 */
294 #define OB_WRP_Pages128to159       (0x00000010U) /* Write protection of Sector4 */
295 #define OB_WRP_Pages160to191       (0x00000020U) /* Write protection of Sector5 */
296 #define OB_WRP_Pages192to223       (0x00000040U) /* Write protection of Sector6 */
297 #define OB_WRP_Pages224to255       (0x00000080U) /* Write protection of Sector7 */
298 #define OB_WRP_Pages256to287       (0x00000100U) /* Write protection of Sector8 */
299 #define OB_WRP_Pages288to319       (0x00000200U) /* Write protection of Sector9 */
300 #define OB_WRP_Pages320to351       (0x00000400U) /* Write protection of Sector10 */
301 #define OB_WRP_Pages352to383       (0x00000800U) /* Write protection of Sector11 */
302 #define OB_WRP_Pages384to415       (0x00001000U) /* Write protection of Sector12 */
303 #define OB_WRP_Pages416to447       (0x00002000U) /* Write protection of Sector13 */
304 #define OB_WRP_Pages448to479       (0x00004000U) /* Write protection of Sector14 */
305 #define OB_WRP_Pages480to511       (0x00008000U) /* Write protection of Sector15 */
306 #define OB_WRP_AllPages            (0x0000FFFFU) /*!< Write protection of all Sectors */
307 /**
308   * @}
309   */
310 
311 #elif defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
312 /** @defgroup FLASHEx_Option_Bytes_Write_Protection FLASH Option Bytes Write ProtectionP
313   * @{
314   */
315 #define OB_WRP_Pages0to31          (0x00000001U) /* Write protection of Sector0 */
316 #define OB_WRP_Pages32to63         (0x00000002U) /* Write protection of Sector1 */
317 #define OB_WRP_Pages64to95         (0x00000004U) /* Write protection of Sector2 */
318 #define OB_WRP_Pages96to127        (0x00000008U) /* Write protection of Sector3 */
319 #define OB_WRP_Pages128to159       (0x00000010U) /* Write protection of Sector4 */
320 #define OB_WRP_Pages160to191       (0x00000020U) /* Write protection of Sector5 */
321 #define OB_WRP_Pages192to223       (0x00000040U) /* Write protection of Sector6 */
322 #define OB_WRP_Pages224to255       (0x00000080U) /* Write protection of Sector7 */
323 #define OB_WRP_Pages256to287       (0x00000100U) /* Write protection of Sector8 */
324 #define OB_WRP_Pages288to319       (0x00000200U) /* Write protection of Sector9 */
325 #define OB_WRP_Pages320to351       (0x00000400U) /* Write protection of Sector10 */
326 #define OB_WRP_Pages352to383       (0x00000800U) /* Write protection of Sector11 */
327 #define OB_WRP_Pages384to415       (0x00001000U) /* Write protection of Sector12 */
328 #define OB_WRP_Pages416to447       (0x00002000U) /* Write protection of Sector13 */
329 #define OB_WRP_Pages448to479       (0x00004000U) /* Write protection of Sector14 */
330 #define OB_WRP_Pages480to511       (0x00008000U) /* Write protection of Sector15 */
331 #define OB_WRP_Pages512to543       (0x00010000U) /* Write protection of Sector16 */
332 #define OB_WRP_Pages544to575       (0x00020000U) /* Write protection of Sector17 */
333 #define OB_WRP_Pages576to607       (0x00040000U) /* Write protection of Sector18 */
334 #define OB_WRP_Pages608to639       (0x00080000U) /* Write protection of Sector19 */
335 #define OB_WRP_Pages640to671       (0x00100000U) /* Write protection of Sector20 */
336 #define OB_WRP_Pages672to703       (0x00200000U) /* Write protection of Sector21 */
337 #define OB_WRP_Pages704to735       (0x00400000U) /* Write protection of Sector22 */
338 #define OB_WRP_Pages736to767       (0x00800000U) /* Write protection of Sector23 */
339 #define OB_WRP_Pages768to799       (0x01000000U) /* Write protection of Sector24 */
340 #define OB_WRP_Pages800to831       (0x02000000U) /* Write protection of Sector25 */
341 #define OB_WRP_Pages832to863       (0x04000000U) /* Write protection of Sector26 */
342 #define OB_WRP_Pages864to895       (0x08000000U) /* Write protection of Sector27 */
343 #define OB_WRP_Pages896to927       (0x10000000U) /* Write protection of Sector28 */
344 #define OB_WRP_Pages928to959       (0x20000000U) /* Write protection of Sector29 */
345 #define OB_WRP_Pages960to991       (0x40000000U) /* Write protection of Sector30 */
346 #define OB_WRP_Pages992to1023      (0x80000000U) /* Write protection of Sector31 */
347 #define OB_WRP_AllPages            (0xFFFFFFFFU) /*!<Write  protection of all Sectors */
348 /**
349   * @}
350   */
351 
352 /** @defgroup FLASHEx_Option_Bytes_Write_Protection2 FLASH Option Bytes Write Protection
353   * @{
354   */
355 #define OB_WRP2_Pages1024to1055    (0x00000001U) /* Write protection of Sector32 */
356 #define OB_WRP2_Pages1056to1087    (0x00000002U) /* Write protection of Sector33 */
357 #define OB_WRP2_Pages1088to1119    (0x00000004U) /* Write protection of Sector34 */
358 #define OB_WRP2_Pages1120to1151    (0x00000008U) /* Write protection of Sector35 */
359 #define OB_WRP2_Pages1152to1183    (0x00000010U) /* Write protection of Sector36 */
360 #define OB_WRP2_Pages1184to1215    (0x00000020U) /* Write protection of Sector37 */
361 #define OB_WRP2_Pages1216to1247    (0x00000040U) /* Write protection of Sector38 */
362 #define OB_WRP2_Pages1248to1279    (0x00000080U) /* Write protection of Sector39 */
363 #define OB_WRP2_Pages1280to1311    (0x00000100U) /* Write protection of Sector40 */
364 #define OB_WRP2_Pages1312to1343    (0x00000200U) /* Write protection of Sector41 */
365 #define OB_WRP2_Pages1344to1375    (0x00000400U) /* Write protection of Sector42 */
366 #define OB_WRP2_Pages1376to1407    (0x00000800U) /* Write protection of Sector43 */
367 #define OB_WRP2_Pages1408to1439    (0x00001000U) /* Write protection of Sector44 */
368 #define OB_WRP2_Pages1440to1471    (0x00002000U) /* Write protection of Sector45 */
369 #define OB_WRP2_Pages1472to1503    (0x00004000U) /* Write protection of Sector46 */
370 #define OB_WRP2_Pages1504to1535    (0x00008000U) /* Write protection of Sector47 */
371 #define OB_WRP2_AllPages           (0x0000FFFFU) /*!< Write protection of all Sectors WRP2 */
372 /**
373   * @}
374   */
375 #endif /* STM32L071xx || STM32L072xx || (STM32L073xx) || (STM32L081xx) || (STM32L082xx) || (STM32L083xx) */
376 
377 /** @defgroup FLASHEx_Option_Bytes_Read_Protection FLASHEx Option Bytes Read Protection
378   * @{
379   */
380 #define OB_RDP_LEVEL_0         ((uint8_t)0xAA)
381 #define OB_RDP_LEVEL_1         ((uint8_t)0xBB)
382 #define OB_RDP_LEVEL_2         ((uint8_t)0xCC) /* Warning: When enabling read protection level 2
383                                                 it is no more possible to go back to level 1 or 0 */
384 
385 /**
386   * @}
387   */
388 
389 /** @defgroup FLASHEx_Option_Bytes_BOR_Level FLASHEx Option Bytes BOR Level
390   * @{
391   */
392 
393 #define OB_BOR_OFF       ((uint8_t)0x00) /*!< BOR is disabled at power down, the reset is asserted when the VDD
394                                              power supply reaches the PDR(Power Down Reset) threshold (1.5V) */
395 #define OB_BOR_LEVEL1    ((uint8_t)0x08) /*!< BOR Reset threshold levels for 1.7V - 1.8V VDD power supply    */
396 #define OB_BOR_LEVEL2    ((uint8_t)0x09) /*!< BOR Reset threshold levels for 1.9V - 2.0V VDD power supply    */
397 #define OB_BOR_LEVEL3    ((uint8_t)0x0A) /*!< BOR Reset threshold levels for 2.3V - 2.4V VDD power supply    */
398 #define OB_BOR_LEVEL4    ((uint8_t)0x0B) /*!< BOR Reset threshold levels for 2.55V - 2.65V VDD power supply  */
399 #define OB_BOR_LEVEL5    ((uint8_t)0x0C) /*!< BOR Reset threshold levels for 2.8V - 2.9V VDD power supply    */
400 
401 /**
402   * @}
403   */
404 
405 /** @defgroup FLASHEx_Option_Bytes_IWatchdog FLASHEx Option Bytes IWatchdog
406   * @{
407   */
408 
409 #define OB_IWDG_SW                     ((uint8_t)0x10)  /*!< Software WDG selected */
410 #define OB_IWDG_HW                     ((uint8_t)0x00)  /*!< Hardware WDG selected */
411 
412 /**
413   * @}
414   */
415 
416 /** @defgroup FLASHEx_Option_Bytes_nRST_STOP FLASHEx Option Bytes nRST_STOP
417   * @{
418   */
419 
420 #define OB_STOP_NORST                  ((uint8_t)0x20) /*!< No reset generated when entering in STOP */
421 #define OB_STOP_RST                    ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
422 /**
423   * @}
424   */
425 
426 /** @defgroup FLASHEx_Option_Bytes_nRST_STDBY FLASHEx Option Bytes nRST_STDBY
427   * @{
428   */
429 
430 #define OB_STDBY_NORST                 ((uint8_t)0x40) /*!< No reset generated when entering in STANDBY */
431 #define OB_STDBY_RST                   ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
432 
433 /**
434   * @}
435   */
436 
437 #if defined(FLASH_OPTR_WPRMOD)
438 
439 /** @defgroup FLASHEx_OptionAdv_Type FLASHEx Option Advanced Type
440   * @{
441   */
442 
443 #define OPTIONBYTE_PCROP        (0x01U)  /*!<PCROP option byte configuration*/
444 
445 /**
446   * @}
447   */
448 
449 #endif /* FLASH_OPTR_WPRMOD */
450 
451 #if defined(FLASH_OPTR_BFB2)
452 
453 /** @defgroup FLASHEx_OptionAdv_Type FLASHEx Option Advanced Type
454   * @{
455   */
456 
457 #define OPTIONBYTE_BOOTCONFIG   (0x02U)  /*!<BOOTConfig option byte configuration*/
458 
459 /**
460   * @}
461   */
462 
463 #endif /* FLASH_OPTR_BFB2 */
464 
465 #if defined(FLASH_OPTR_WPRMOD)
466 
467 /** @defgroup  FLASHEx_PCROP_State FLASHEx PCROP State
468   * @{
469   */
470 #define OB_PCROP_STATE_DISABLE        (0x00U)  /*!<Disable PCROP for selected sectors */
471 #define OB_PCROP_STATE_ENABLE         (0x01U)  /*!<Enable PCROP for selected sectors */
472 
473 /**
474   * @}
475   */
476 
477 /** @defgroup  FLASHEx_Selection_Protection_Mode FLASHEx Selection Protection Mode
478   * @{
479   */
480 #define OB_PCROP_DESELECTED     ((uint16_t)0x0000)            /*!< Disabled PCROP, nWPRi bits used for Write Protection on sector i */
481 #define OB_PCROP_SELECTED       ((uint16_t)FLASH_OPTR_WPRMOD)  /*!< Enable PCROP, nWPRi bits used for PCRoP Protection on sector i   */
482 
483 /**
484   * @}
485   */
486 #endif /* FLASH_OPTR_WPRMOD */
487 
488 #if defined (STM32L011xx) || defined (STM32L021xx) ||  defined (STM32L031xx) || defined (STM32L041xx)
489 /** @defgroup FLASHEx_Option_Bytes_PC_ReadWrite_Protection FLASHEx Option Bytes PC Read/Write Protection
490   * @{
491   */
492 #define OB_PCROP_Pages0to31          (0x00000001U) /* PC Read/Write protection of Sector0 */
493 #define OB_PCROP_Pages32to63         (0x00000002U) /* PC Read/Write protection of Sector1 */
494 #define OB_PCROP_Pages64to95         (0x00000004U) /* PC Read/Write protection of Sector2 */
495 #define OB_PCROP_Pages96to127        (0x00000008U) /* PC Read/Write protection of Sector3 */
496 #define OB_PCROP_Pages128to159       (0x00000010U) /* PC Read/Write protection of Sector4 */
497 #define OB_PCROP_Pages160to191       (0x00000020U) /* PC Read/Write protection of Sector5 */
498 #define OB_PCROP_Pages192to223       (0x00000040U) /* PC Read/Write protection of Sector6 */
499 #define OB_PCROP_Pages224to255       (0x00000080U) /* PC Read/Write protection of Sector7 */
500 #define OB_PCROP_AllPages            (0x000000FFU) /*!< PC Read/Write protection of all Sectors */
501 /**
502   * @}
503   */
504 #elif defined (STM32L051xx) || defined (STM32L052xx) || defined (STM32L053xx) || defined (STM32L062xx) || defined (STM32L063xx)
505 /** @defgroup FLASHEx_Option_Bytes_PC_ReadWrite_Protection FLASHEx Option Bytes PC Read/Write Protection
506   * @{
507   */
508 #define OB_PCROP_Pages0to31          (0x00000001U) /* PC Read/Write protection of Sector0 */
509 #define OB_PCROP_Pages32to63         (0x00000002U) /* PC Read/Write protection of Sector1 */
510 #define OB_PCROP_Pages64to95         (0x00000004U) /* PC Read/Write protection of Sector2 */
511 #define OB_PCROP_Pages96to127        (0x00000008U) /* PC Read/Write protection of Sector3 */
512 #define OB_PCROP_Pages128to159       (0x00000010U) /* PC Read/Write protection of Sector4 */
513 #define OB_PCROP_Pages160to191       (0x00000020U) /* PC Read/Write protection of Sector5 */
514 #define OB_PCROP_Pages192to223       (0x00000040U) /* PC Read/Write protection of Sector6 */
515 #define OB_PCROP_Pages224to255       (0x00000080U) /* PC Read/Write protection of Sector7 */
516 #define OB_PCROP_Pages256to287       (0x00000100U) /* PC Read/Write protection of Sector8 */
517 #define OB_PCROP_Pages288to319       (0x00000200U) /* PC Read/Write protection of Sector9 */
518 #define OB_PCROP_Pages320to351       (0x00000400U) /* PC Read/Write protection of Sector10 */
519 #define OB_PCROP_Pages352to383       (0x00000800U) /* PC Read/Write protection of Sector11 */
520 #define OB_PCROP_Pages384to415       (0x00001000U) /* PC Read/Write protection of Sector12 */
521 #define OB_PCROP_Pages416to447       (0x00002000U) /* PC Read/Write protection of Sector13 */
522 #define OB_PCROP_Pages448to479       (0x00004000U) /* PC Read/Write protection of Sector14 */
523 #define OB_PCROP_Pages480to511       (0x00008000U) /* PC Read/Write protection of Sector15 */
524 #define OB_PCROP_AllPages            (0x0000FFFFU) /*!< PC Read/Write protection of all Sectors */
525 /**
526   * @}
527   */
528 #endif
529 
530 #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
531 /** @defgroup FLASHEx_Option_Bytes_PC_ReadWrite_Protection FLASH Option Bytes PC Read/Write Protection
532   * @{
533   */
534 #define OB_PCROP_Pages0to31          (0x00000001U) /* PC Read/Write protection of Sector0 */
535 #define OB_PCROP_Pages32to63         (0x00000002U) /* PC Read/Write protection of Sector1 */
536 #define OB_PCROP_Pages64to95         (0x00000004U) /* PC Read/Write protection of Sector2 */
537 #define OB_PCROP_Pages96to127        (0x00000008U) /* PC Read/Write protection of Sector3 */
538 #define OB_PCROP_Pages128to159       (0x00000010U) /* PC Read/Write protection of Sector4 */
539 #define OB_PCROP_Pages160to191       (0x00000020U) /* PC Read/Write protection of Sector5 */
540 #define OB_PCROP_Pages192to223       (0x00000040U) /* PC Read/Write protection of Sector6 */
541 #define OB_PCROP_Pages224to255       (0x00000080U) /* PC Read/Write protection of Sector7 */
542 #define OB_PCROP_Pages256to287       (0x00000100U) /* PC Read/Write protection of Sector8 */
543 #define OB_PCROP_Pages288to319       (0x00000200U) /* PC Read/Write protection of Sector9 */
544 #define OB_PCROP_Pages320to351       (0x00000400U) /* PC Read/Write protection of Sector10 */
545 #define OB_PCROP_Pages352to383       (0x00000800U) /* PC Read/Write protection of Sector11 */
546 #define OB_PCROP_Pages384to415       (0x00001000U) /* PC Read/Write protection of Sector12 */
547 #define OB_PCROP_Pages416to447       (0x00002000U) /* PC Read/Write protection of Sector13 */
548 #define OB_PCROP_Pages448to479       (0x00004000U) /* PC Read/Write protection of Sector14 */
549 #define OB_PCROP_Pages480to511       (0x00008000U) /* PC Read/Write protection of Sector15 */
550 #define OB_PCROP_Pages512to543       (0x00010000U) /* PC Read/Write protection of Sector16 */
551 #define OB_PCROP_Pages544to575       (0x00020000U) /* PC Read/Write protection of Sector17 */
552 #define OB_PCROP_Pages576to607       (0x00040000U) /* PC Read/Write protection of Sector18 */
553 #define OB_PCROP_Pages608to639       (0x00080000U) /* PC Read/Write protection of Sector19 */
554 #define OB_PCROP_Pages640to671       (0x00100000U) /* PC Read/Write protection of Sector20 */
555 #define OB_PCROP_Pages672to703       (0x00200000U) /* PC Read/Write protection of Sector21 */
556 #define OB_PCROP_Pages704to735       (0x00400000U) /* PC Read/Write protection of Sector22 */
557 #define OB_PCROP_Pages736to767       (0x00800000U) /* PC Read/Write protection of Sector23 */
558 #define OB_PCROP_Pages768to799       (0x01000000U) /* PC Read/Write protection of Sector24 */
559 #define OB_PCROP_Pages800to831       (0x02000000U) /* PC Read/Write protection of Sector25 */
560 #define OB_PCROP_Pages832to863       (0x04000000U) /* PC Read/Write protection of Sector26 */
561 #define OB_PCROP_Pages864to895       (0x08000000U) /* PC Read/Write protection of Sector27 */
562 #define OB_PCROP_Pages896to927       (0x10000000U) /* PC Read/Write protection of Sector28 */
563 #define OB_PCROP_Pages928to959       (0x20000000U) /* PC Read/Write protection of Sector29 */
564 #define OB_PCROP_Pages960to991       (0x40000000U) /* PC Read/Write protection of Sector30 */
565 #define OB_PCROP_Pages992to1023      (0x80000000U) /* PC Read/Write protection of Sector31 */
566 #define OB_PCROP_AllPages            (0xFFFFFFFFU) /*!<PC Read/Write  protection of all Sectors */
567 /**
568   * @}
569   */
570 
571 /** @defgroup FLASHEx_Option_Bytes_PC_ReadWrite_Protection2 FLASH Option Bytes PC Read/Write Protection (Sector 2)
572   * @{
573   */
574 #define OB_PCROP2_Pages1024to1055    (0x00000001U) /* PC Read/Write protection of Sector32 */
575 #define OB_PCROP2_Pages1056to1087    (0x00000002U) /* PC Read/Write protection of Sector33 */
576 #define OB_PCROP2_Pages1088to1119    (0x00000004U) /* PC Read/Write protection of Sector34 */
577 #define OB_PCROP2_Pages1120to1151    (0x00000008U) /* PC Read/Write protection of Sector35 */
578 #define OB_PCROP2_Pages1152to1183    (0x00000010U) /* PC Read/Write protection of Sector36 */
579 #define OB_PCROP2_Pages1184to1215    (0x00000020U) /* PC Read/Write protection of Sector37 */
580 #define OB_PCROP2_Pages1216to1247    (0x00000040U) /* PC Read/Write protection of Sector38 */
581 #define OB_PCROP2_Pages1248to1279    (0x00000080U) /* PC Read/Write protection of Sector39 */
582 #define OB_PCROP2_Pages1280to1311    (0x00000100U) /* PC Read/Write protection of Sector40 */
583 #define OB_PCROP2_Pages1312to1343    (0x00000200U) /* PC Read/Write protection of Sector41 */
584 #define OB_PCROP2_Pages1344to1375    (0x00000400U) /* PC Read/Write protection of Sector42 */
585 #define OB_PCROP2_Pages1376to1407    (0x00000800U) /* PC Read/Write protection of Sector43 */
586 #define OB_PCROP2_Pages1408to1439    (0x00001000U) /* PC Read/Write protection of Sector44 */
587 #define OB_PCROP2_Pages1440to1471    (0x00002000U) /* PC Read/Write protection of Sector45 */
588 #define OB_PCROP2_Pages1472to1503    (0x00004000U) /* PC Read/Write protection of Sector46 */
589 #define OB_PCROP2_Pages1504to1535    (0x00008000U) /* PC Read/Write protection of Sector47 */
590 #define OB_PCROP2_AllPages           (0x0000FFFFU) /*!< PC Read/Write protection of all Sectors PCROP2 */
591 /**
592   * @}
593   */
594 #endif /* STM32L071xx || STM32L072xx || STM32L073xx || STM32L081xx || STM32L082xx || STM32L083xx */
595 
596 /** @defgroup FLASHEx_Option_Bytes_BOOTBit1 FLASH Option Bytes BOOT Bit1 Setup
597   * @{
598   */
599 #define OB_BOOT_BIT1_RESET      (uint8_t)(0x00) /*!< BOOT Bit 1 Reset */
600 #define OB_BOOT_BIT1_SET        (uint8_t)(0x01) /*!< BOOT Bit 1 Set */
601 /**
602   * @}
603   */
604 
605 /** @defgroup FLASHEx_Type_Program_Data FLASHEx Type Program Data
606   * @{
607   */
608 #define FLASH_TYPEPROGRAMDATA_BYTE            (0x00U)  /*!<Program byte (8-bit) at a specified address.*/
609 #define FLASH_TYPEPROGRAMDATA_HALFWORD        (0x01U)  /*!<Program a half-word (16-bit) at a specified address.*/
610 #define FLASH_TYPEPROGRAMDATA_WORD            (0x02U)  /*!<Program a word (32-bit) at a specified address.*/
611 
612 /**
613   * @}
614   */
615 
616 #if defined(FLASH_OPTR_BFB2)
617 
618 /** @defgroup FLASHEx_Option_Bytes_BOOT FLASHEx Option Bytes BOOT
619   * @{
620   */
621 
622 #define OB_BOOT_BANK1                 ((uint8_t)0x00) /*!<  At startup, if boot pin 0 and BOOT1 bit are set in boot from user Flash position
623                                                             and this parameter is selected the device will boot from Bank 1 (Default)*/
624 #define OB_BOOT_BANK2                 ((uint8_t)(FLASH_OPTR_BFB2 >> 16)) /*!< At startup, if boot pin 0 and BOOT1 bit are set in boot from user Flash position
625                                                             and this parameter is selected the device will boot from Bank 2 */
626 
627 /**
628   * @}
629   */
630 #endif /* FLASH_OPTR_BFB2 */
631 
632 /**
633   * @}
634   */
635 
636 /* Exported macro ------------------------------------------------------------*/
637 
638 /** @defgroup FLASHEx_Exported_Macros FLASHEx Exported Macros
639  *  @{
640  */
641 
642 /**
643   * @brief  Set the FLASH Latency.
644   * @param  __LATENCY__ FLASH Latency
645   *          This parameter can be one of the following values:
646   *            @arg @ref FLASH_LATENCY_0  FLASH Zero Latency cycle
647   *            @arg @ref FLASH_LATENCY_1  FLASH One Latency cycle
648   * @retval none
649   */
650 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) \
651                   MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(__LATENCY__))
652 
653 /**
654   * @brief  Get the FLASH Latency.
655   * @retval FLASH Latency
656   *          This parameter can be one of the following values:
657   *            @arg @ref FLASH_LATENCY_0  FLASH Zero Latency cycle
658   *            @arg @ref FLASH_LATENCY_1  FLASH One Latency cycle
659   */
660 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
661 
662 /**
663   * @brief  Enable the FLASH prefetch buffer.
664   * @retval none
665   */
666 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()      SET_BIT((FLASH->ACR), FLASH_ACR_PRFTEN)
667 
668 /**
669   * @brief  Disable the FLASH prefetch buffer.
670   * @retval none
671   */
672 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()     CLEAR_BIT((FLASH->ACR), FLASH_ACR_PRFTEN)
673 
674 /**
675   * @brief  Enable the FLASH Buffer cache.
676   * @retval none
677   */
678 #define __HAL_FLASH_BUFFER_CACHE_ENABLE()         CLEAR_BIT((FLASH->ACR), FLASH_ACR_DISAB_BUF)
679 
680 /**
681   * @brief  Disable the FLASH Buffer cache.
682   * @retval none
683   */
684 #define __HAL_FLASH_BUFFER_CACHE_DISABLE()        SET_BIT((FLASH->ACR), FLASH_ACR_DISAB_BUF)
685 
686 /**
687   * @brief  Enable the FLASH preread buffer.
688   * @retval none
689   */
690 #define __HAL_FLASH_PREREAD_BUFFER_ENABLE()       SET_BIT((FLASH->ACR), FLASH_ACR_PRE_READ)
691 
692 /**
693   * @brief  Disable the FLASH preread buffer.
694   * @retval none
695   */
696 #define __HAL_FLASH_PREREAD_BUFFER_DISABLE()      CLEAR_BIT((FLASH->ACR), FLASH_ACR_PRE_READ)
697 
698 /**
699   * @brief  Enable the FLASH power down during Sleep mode
700   * @retval none
701   */
702 #define __HAL_FLASH_SLEEP_POWERDOWN_ENABLE()      SET_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
703 
704 /**
705   * @brief  Disable the FLASH power down during Sleep mode
706   * @retval none
707   */
708 #define __HAL_FLASH_SLEEP_POWERDOWN_DISABLE()     CLEAR_BIT(FLASH->ACR, FLASH_ACR_SLEEP_PD)
709 
710 /**
711   * @brief  Enable the Flash Run power down mode.
712   * @note   Writing this bit  to 0 this bit, automatically the keys are
713   *         loss and a new unlock sequence is necessary to re-write it to 1.
714   */
715 #define __HAL_FLASH_POWER_DOWN_ENABLE() do { FLASH->PDKEYR = FLASH_PDKEY1;    \
716                                              FLASH->PDKEYR = FLASH_PDKEY2;    \
717                                              SET_BIT((FLASH->ACR), FLASH_ACR_RUN_PD);  \
718                                            } while (0)
719 
720 /**
721   * @brief  Disable the Flash Run power down mode.
722   * @note   Writing this bit to 0 this bit, automatically the keys are
723   *         loss and a new unlock sequence is necessary to re-write it to 1.
724   */
725 #define __HAL_FLASH_POWER_DOWN_DISABLE() do { FLASH->PDKEYR = FLASH_PDKEY1;    \
726                                               FLASH->PDKEYR = FLASH_PDKEY2;    \
727                                              CLEAR_BIT((FLASH->ACR), FLASH_ACR_RUN_PD);  \
728                                             } while (0)
729 
730 /**
731   * @}
732   */
733 
734 /* Exported functions --------------------------------------------------------*/
735 
736 /** @addtogroup FLASHEx_Exported_Functions
737   * @{
738   */
739 
740 /** @addtogroup FLASHEx_Exported_Functions_Group1
741   * @{
742   */
743 
744 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *PageError);
745 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit);
746 
747 /**
748   * @}
749   */
750 
751 /** @addtogroup FLASHEx_Exported_Functions_Group2
752   * @{
753   */
754 
755 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
756 void              HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
757 
758 #if defined(FLASH_OPTR_WPRMOD) || defined(FLASH_OPTR_BFB2)
759 
760 HAL_StatusTypeDef HAL_FLASHEx_AdvOBProgram (FLASH_AdvOBProgramInitTypeDef *pAdvOBInit);
761 void              HAL_FLASHEx_AdvOBGetConfig(FLASH_AdvOBProgramInitTypeDef *pAdvOBInit);
762 
763 #endif /* FLASH_OPTR_WPRMOD || FLASH_OPTR_BFB2 */
764 
765 #if defined(FLASH_OPTR_WPRMOD)
766 
767 HAL_StatusTypeDef HAL_FLASHEx_OB_SelectPCROP(void);
768 HAL_StatusTypeDef HAL_FLASHEx_OB_DeSelectPCROP(void);
769 
770 #endif /* FLASH_OPTR_WPRMOD */
771 
772 /**
773   * @}
774   */
775 
776 /** @addtogroup FLASHEx_Exported_Functions_Group3
777   * @{
778   */
779 
780 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Unlock(void);
781 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Lock(void);
782 
783 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Erase(uint32_t Address);
784 HAL_StatusTypeDef HAL_FLASHEx_DATAEEPROM_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
785 void              HAL_FLASHEx_DATAEEPROM_EnableFixedTimeProgram(void);
786 void              HAL_FLASHEx_DATAEEPROM_DisableFixedTimeProgram(void);
787 
788 /**
789   * @}
790   */
791 
792 /**
793   * @}
794   */
795 
796 /**
797   * @}
798   */
799 
800 /**
801   * @}
802   */
803 
804 #ifdef __cplusplus
805 }
806 #endif
807 
808 #endif /* __STM32L0xx_HAL_FLASH_EX_H */
809 
810