1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_flash_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of FLASH HAL Extension module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2017 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file in
13   * the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   ******************************************************************************
16   */
17 
18 /* Define to prevent recursive inclusion -------------------------------------*/
19 #ifndef __STM32F2xx_HAL_FLASH_EX_H
20 #define __STM32F2xx_HAL_FLASH_EX_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32f2xx_hal_def.h"
28 
29 /** @addtogroup STM32F2xx_HAL_Driver
30   * @{
31   */
32 
33 /** @addtogroup FLASHEx
34   * @{
35   */
36 
37 /* Exported types ------------------------------------------------------------*/
38 /** @defgroup FLASHEx_Exported_Types FLASH Exported Types
39   * @{
40   */
41 
42 /**
43   * @brief  FLASH Erase structure definition
44   */
45 typedef struct
46 {
47   uint32_t TypeErase;   /*!< Mass erase or sector Erase.
48                              This parameter can be a value of @ref FLASHEx_Type_Erase */
49 
50   uint32_t Banks;       /*!< Select banks to erase when Mass erase is enabled.
51                              This parameter must be a value of @ref FLASHEx_Banks */
52 
53   uint32_t Sector;      /*!< Initial FLASH sector to erase when Mass erase is disabled
54                              This parameter must be a value of @ref FLASHEx_Sectors */
55 
56   uint32_t NbSectors;   /*!< Number of sectors to be erased.
57                              This parameter must be a value between 1 and (max number of sectors - value of Initial sector)*/
58 
59   uint32_t VoltageRange;/*!< The device voltage range which defines the erase parallelism
60                              This parameter must be a value of @ref FLASHEx_Voltage_Range */
61 
62 } FLASH_EraseInitTypeDef;
63 
64 /**
65   * @brief  FLASH Option Bytes Program structure definition
66   */
67 typedef struct
68 {
69   uint32_t OptionType;   /*!< Option byte to be configured.
70                               This parameter can be a value of @ref FLASHEx_Option_Type */
71 
72   uint32_t WRPState;     /*!< Write protection activation or deactivation.
73                               This parameter can be a value of @ref FLASHEx_WRP_State */
74 
75   uint32_t WRPSector;         /*!< Specifies the sector(s) to be write protected.
76                               The value of this parameter depend on device used within the same series */
77 
78   uint32_t Banks;        /*!< Select banks for WRP activation/deactivation of all sectors.
79                               This parameter must be a value of @ref FLASHEx_Banks */
80 
81   uint32_t RDPLevel;     /*!< Set the read protection level.
82                               This parameter can be a value of @ref FLASHEx_Option_Bytes_Read_Protection */
83 
84   uint32_t BORLevel;     /*!< Set the BOR Level.
85                               This parameter can be a value of @ref FLASHEx_BOR_Reset_Level */
86 
87   uint8_t  USERConfig;   /*!< Program the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY. */
88 
89 } FLASH_OBProgramInitTypeDef;
90 
91 /* Exported constants --------------------------------------------------------*/
92 
93 /** @defgroup FLASHEx_Exported_Constants FLASH Exported Constants
94   * @{
95   */
96 
97 /** @defgroup FLASHEx_Type_Erase FLASH Type Erase
98   * @{
99   */
100 #define FLASH_TYPEERASE_SECTORS         0x00000000U  /*!< Sectors erase only          */
101 #define FLASH_TYPEERASE_MASSERASE       0x00000001U  /*!< Flash Mass erase activation */
102 /**
103   * @}
104   */
105 
106 /** @defgroup FLASHEx_Voltage_Range FLASH Voltage Range
107   * @{
108   */
109 #define FLASH_VOLTAGE_RANGE_1        0x00000000U  /*!< Device operating range: 1.8V to 2.1V                */
110 #define FLASH_VOLTAGE_RANGE_2        0x00000001U  /*!< Device operating range: 2.1V to 2.7V                */
111 #define FLASH_VOLTAGE_RANGE_3        0x00000002U  /*!< Device operating range: 2.7V to 3.6V                */
112 #define FLASH_VOLTAGE_RANGE_4        0x00000003U  /*!< Device operating range: 2.7V to 3.6V + External Vpp */
113 /**
114   * @}
115   */
116 
117 /** @defgroup FLASHEx_WRP_State FLASH WRP State
118   * @{
119   */
120 #define OB_WRPSTATE_DISABLE       0x00000000U  /*!< Disable the write protection of the desired bank 1 sectors */
121 #define OB_WRPSTATE_ENABLE        0x00000001U  /*!< Enable the write protection of the desired bank 1 sectors  */
122 /**
123   * @}
124   */
125 
126 /** @defgroup FLASHEx_Option_Type FLASH Option Type
127   * @{
128   */
129 #define OPTIONBYTE_WRP        0x00000001U  /*!< WRP option byte configuration  */
130 #define OPTIONBYTE_RDP        0x00000002U  /*!< RDP option byte configuration  */
131 #define OPTIONBYTE_USER       0x00000004U  /*!< USER option byte configuration */
132 #define OPTIONBYTE_BOR        0x00000008U  /*!< BOR option byte configuration  */
133 /**
134   * @}
135   */
136 
137 /** @defgroup FLASHEx_Option_Bytes_Read_Protection FLASH Option Bytes Read Protection
138   * @{
139   */
140 #define OB_RDP_LEVEL_0   ((uint8_t)0xAA)
141 #define OB_RDP_LEVEL_1   ((uint8_t)0x55)
142 #define OB_RDP_LEVEL_2   ((uint8_t)0xCC) /*!< Warning: When enabling read protection level 2
143                                               it s no more possible to go back to level 1 or 0 */
144 /**
145   * @}
146   */
147 
148 /** @defgroup FLASHEx_Option_Bytes_IWatchdog FLASH Option Bytes IWatchdog
149   * @{
150   */
151 #define OB_IWDG_SW                     ((uint8_t)0x20)  /*!< Software IWDG selected */
152 #define OB_IWDG_HW                     ((uint8_t)0x00)  /*!< Hardware IWDG selected */
153 /**
154   * @}
155   */
156 
157 /** @defgroup FLASHEx_Option_Bytes_nRST_STOP FLASH Option Bytes nRST_STOP
158   * @{
159   */
160 #define OB_STOP_NO_RST                 ((uint8_t)0x40) /*!< No reset generated when entering in STOP */
161 #define OB_STOP_RST                    ((uint8_t)0x00) /*!< Reset generated when entering in STOP    */
162 /**
163   * @}
164   */
165 
166 
167 /** @defgroup FLASHEx_Option_Bytes_nRST_STDBY FLASH Option Bytes nRST_STDBY
168   * @{
169   */
170 #define OB_STDBY_NO_RST                ((uint8_t)0x80) /*!< No reset generated when entering in STANDBY */
171 #define OB_STDBY_RST                   ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY    */
172 /**
173   * @}
174   */
175 
176 /** @defgroup FLASHEx_BOR_Reset_Level FLASH BOR Reset Level
177   * @{
178   */
179 #define OB_BOR_LEVEL3          ((uint8_t)0x00)  /*!< Supply voltage ranges from 2.70 to 3.60 V */
180 #define OB_BOR_LEVEL2          ((uint8_t)0x04)  /*!< Supply voltage ranges from 2.40 to 2.70 V */
181 #define OB_BOR_LEVEL1          ((uint8_t)0x08)  /*!< Supply voltage ranges from 2.10 to 2.40 V */
182 #define OB_BOR_OFF             ((uint8_t)0x0C)  /*!< Supply voltage ranges from 1.62 to 2.10 V */
183 /**
184   * @}
185   */
186 
187 
188 /**
189   * @}
190   */
191 
192 /** @defgroup FLASH_Latency FLASH Latency
193   * @{
194   */
195 #define FLASH_LATENCY_0                FLASH_ACR_LATENCY_0WS   /*!< FLASH Zero Latency cycle      */
196 #define FLASH_LATENCY_1                FLASH_ACR_LATENCY_1WS   /*!< FLASH One Latency cycle       */
197 #define FLASH_LATENCY_2                FLASH_ACR_LATENCY_2WS   /*!< FLASH Two Latency cycles      */
198 #define FLASH_LATENCY_3                FLASH_ACR_LATENCY_3WS   /*!< FLASH Three Latency cycles    */
199 #define FLASH_LATENCY_4                FLASH_ACR_LATENCY_4WS   /*!< FLASH Four Latency cycles     */
200 #define FLASH_LATENCY_5                FLASH_ACR_LATENCY_5WS   /*!< FLASH Five Latency cycles     */
201 #define FLASH_LATENCY_6                FLASH_ACR_LATENCY_6WS   /*!< FLASH Six Latency cycles      */
202 #define FLASH_LATENCY_7                FLASH_ACR_LATENCY_7WS   /*!< FLASH Seven Latency cycles    */
203 
204 /**
205   * @}
206   */
207 
208 
209 /** @defgroup FLASHEx_Banks FLASH Banks
210   * @{
211   */
212 #define FLASH_BANK_1     1U /*!< Bank 1   */
213 /**
214   * @}
215   */
216 
217 /** @defgroup FLASHEx_MassErase_bit FLASH Mass Erase bit
218   * @{
219   */
220 #define FLASH_MER_BIT     FLASH_CR_MER /*!< only 1 MER Bit */
221 /**
222   * @}
223   */
224 
225 /** @defgroup FLASHEx_Sectors FLASH Sectors
226   * @{
227   */
228 #define FLASH_SECTOR_0     0U  /*!< Sector Number 0   */
229 #define FLASH_SECTOR_1     1U  /*!< Sector Number 1   */
230 #define FLASH_SECTOR_2     2U  /*!< Sector Number 2   */
231 #define FLASH_SECTOR_3     3U  /*!< Sector Number 3   */
232 #define FLASH_SECTOR_4     4U  /*!< Sector Number 4   */
233 #define FLASH_SECTOR_5     5U  /*!< Sector Number 5   */
234 #define FLASH_SECTOR_6     6U  /*!< Sector Number 6   */
235 #define FLASH_SECTOR_7     7U  /*!< Sector Number 7   */
236 #define FLASH_SECTOR_8     8U  /*!< Sector Number 8   */
237 #define FLASH_SECTOR_9     9U  /*!< Sector Number 9   */
238 #define FLASH_SECTOR_10    10U /*!< Sector Number 10  */
239 #define FLASH_SECTOR_11    11U /*!< Sector Number 11  */
240 
241 
242 
243 /**
244   * @}
245   */
246 
247 /** @defgroup FLASHEx_Option_Bytes_Write_Protection FLASH Option Bytes Write Protection
248   * @{
249   */
250 #define OB_WRP_SECTOR_0       0x00000001U /*!< Write protection of Sector0 */
251 #define OB_WRP_SECTOR_1       0x00000002U /*!< Write protection of Sector1 */
252 #define OB_WRP_SECTOR_2       0x00000004U /*!< Write protection of Sector2 */
253 #define OB_WRP_SECTOR_3       0x00000008U /*!< Write protection of Sector3 */
254 #define OB_WRP_SECTOR_4       0x00000010U /*!< Write protection of Sector4 */
255 #define OB_WRP_SECTOR_5       0x00000020U /*!< Write protection of Sector5 */
256 #define OB_WRP_SECTOR_6       0x00000040U /*!< Write protection of Sector6 */
257 #define OB_WRP_SECTOR_7       0x00000080U /*!< Write protection of Sector7 */
258 #define OB_WRP_SECTOR_8       0x00000100U /*!< Write protection of Sector8 */
259 #define OB_WRP_SECTOR_9       0x00000200U /*!< Write protection of Sector9 */
260 #define OB_WRP_SECTOR_10      0x00000400U /*!< Write protection of Sector10 */
261 #define OB_WRP_SECTOR_11      0x00000800U /*!< Write protection of Sector11 */
262 #define OB_WRP_SECTOR_All     0x00000FFFU /*!< Write protection of all Sectors */
263 
264 
265 /**
266   * @}
267   */
268 
269 /**
270   * @}
271   */
272 
273 /* Exported macro ------------------------------------------------------------*/
274 
275 /* Exported functions --------------------------------------------------------*/
276 /** @addtogroup FLASHEx_Exported_Functions
277   * @{
278   */
279 
280 /** @addtogroup FLASHEx_Exported_Functions_Group1
281   * @{
282   */
283 /* Extension Program operation functions  *************************************/
284 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, uint32_t *SectorError);
285 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit);
286 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit);
287 void              HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit);
288 
289 /**
290   * @}
291   */
292 
293 /**
294   * @}
295   */
296 /* Private types -------------------------------------------------------------*/
297 /* Private variables ---------------------------------------------------------*/
298 /** @defgroup FLASHEx_Private_Variables FLASH Private Variables
299   * @{
300   */
301 
302 /**
303   * @}
304   */
305 /* Private constants ---------------------------------------------------------*/
306 /** @defgroup FLASHEx_Private_Constants FLASH Private Constants
307   * @{
308   */
309 
310 #define FLASH_SECTOR_TOTAL  12U
311 
312 /**
313   * @}
314   */
315 
316 /* Private macros ------------------------------------------------------------*/
317 /** @defgroup FLASHEx_Private_Macros FLASH Private Macros
318   * @{
319   */
320 
321 /** @defgroup FLASHEx_IS_FLASH_Definitions FLASH Private macros to check input parameters
322   * @{
323   */
324 
325 #define IS_FLASH_TYPEERASE(VALUE)(((VALUE) == FLASH_TYPEERASE_SECTORS) || \
326                                   ((VALUE) == FLASH_TYPEERASE_MASSERASE))
327 
328 #define IS_VOLTAGERANGE(RANGE)(((RANGE) == FLASH_VOLTAGE_RANGE_1) || \
329                                ((RANGE) == FLASH_VOLTAGE_RANGE_2) || \
330                                ((RANGE) == FLASH_VOLTAGE_RANGE_3) || \
331                                ((RANGE) == FLASH_VOLTAGE_RANGE_4))
332 
333 #define IS_WRPSTATE(VALUE)(((VALUE) == OB_WRPSTATE_DISABLE) || \
334                            ((VALUE) == OB_WRPSTATE_ENABLE))
335 
336 #define IS_OPTIONBYTE(VALUE)(((VALUE) <= (OPTIONBYTE_WRP|OPTIONBYTE_RDP|OPTIONBYTE_USER|OPTIONBYTE_BOR)))
337 
338 #define IS_OB_RDP_LEVEL(LEVEL) (((LEVEL) == OB_RDP_LEVEL_0)   ||\
339                                 ((LEVEL) == OB_RDP_LEVEL_1)   ||\
340                                 ((LEVEL) == OB_RDP_LEVEL_2))
341 
342 #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
343 
344 #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NO_RST) || ((SOURCE) == OB_STOP_RST))
345 
346 #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NO_RST) || ((SOURCE) == OB_STDBY_RST))
347 
348 #define IS_OB_BOR_LEVEL(LEVEL) (((LEVEL) == OB_BOR_LEVEL1) || ((LEVEL) == OB_BOR_LEVEL2) ||\
349                                 ((LEVEL) == OB_BOR_LEVEL3) || ((LEVEL) == OB_BOR_OFF))
350 
351 
352 #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0)  || \
353                                    ((LATENCY) == FLASH_LATENCY_1)  || \
354                                    ((LATENCY) == FLASH_LATENCY_2)  || \
355                                    ((LATENCY) == FLASH_LATENCY_3)  || \
356                                    ((LATENCY) == FLASH_LATENCY_4)  || \
357                                    ((LATENCY) == FLASH_LATENCY_5)  || \
358                                    ((LATENCY) == FLASH_LATENCY_6)  || \
359                                    ((LATENCY) == FLASH_LATENCY_7))
360 #define IS_FLASH_BANK(BANK) (((BANK) == FLASH_BANK_1))
361 #define IS_FLASH_SECTOR(SECTOR) (((SECTOR) == FLASH_SECTOR_0)   || ((SECTOR) == FLASH_SECTOR_1)   ||\
362                                  ((SECTOR) == FLASH_SECTOR_2)   || ((SECTOR) == FLASH_SECTOR_3)   ||\
363                                  ((SECTOR) == FLASH_SECTOR_4)   || ((SECTOR) == FLASH_SECTOR_5)   ||\
364                                  ((SECTOR) == FLASH_SECTOR_6)   || ((SECTOR) == FLASH_SECTOR_7)   ||\
365                                  ((SECTOR) == FLASH_SECTOR_8)   || ((SECTOR) == FLASH_SECTOR_9)   ||\
366                                  ((SECTOR) == FLASH_SECTOR_10)  || ((SECTOR) == FLASH_SECTOR_11))
367 
368 
369 
370 #define IS_FLASH_ADDRESS(ADDRESS) ((((ADDRESS) >= FLASH_BASE) && ((ADDRESS) <= FLASH_END)) || \
371                                    (((ADDRESS) >= FLASH_OTP_BASE) && ((ADDRESS) <= FLASH_OTP_END)))
372 #define IS_FLASH_NBSECTORS(NBSECTORS) (((NBSECTORS) != 0U) && ((NBSECTORS) <= FLASH_SECTOR_TOTAL))
373 #define IS_OB_WRP_SECTOR(SECTOR)((((SECTOR) & 0xFFFFF000U) == 0x00000000U) && ((SECTOR) != 0x00000000U))
374 
375 /**
376   * @}
377   */
378 
379 /**
380   * @}
381   */
382 
383 /* Private functions ---------------------------------------------------------*/
384 /** @defgroup FLASHEx_Private_Functions FLASH Private Functions
385   * @{
386   */
387 void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange);
388 void FLASH_FlushCaches(void);
389 /**
390   * @}
391   */
392 
393 /**
394   * @}
395   */
396 
397 /**
398   * @}
399   */
400 
401 #ifdef __cplusplus
402 }
403 #endif
404 
405 #endif /* __STM32F2xx_HAL_FLASH_EX_H */
406 
407