1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal_flash.h
4   * @author  MCD Application Team
5   * @brief   Header file of Flash HAL 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_H
20 #define __STM32L0xx_HAL_FLASH_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 FLASH
34   * @{
35   */
36 
37 /** @addtogroup FLASH_Private_Constants
38   * @{
39   */
40 #define FLASH_TIMEOUT_VALUE      (50000U) /* 50 s */
41 #define FLASH_SIZE_DATA_REGISTER FLASHSIZE_BASE
42 /**
43   * @}
44   */
45 
46 /** @addtogroup FLASH_Private_Macros
47   * @{
48   */
49 
50 #define IS_FLASH_TYPEPROGRAM(_VALUE_)   ((_VALUE_) == FLASH_TYPEPROGRAM_WORD)
51 
52 #define IS_FLASH_LATENCY(__LATENCY__) (((__LATENCY__) == FLASH_LATENCY_0) || \
53                                        ((__LATENCY__) == FLASH_LATENCY_1))
54 
55 /**
56   * @}
57   */
58 
59 /* Exported types ------------------------------------------------------------*/
60 /** @defgroup FLASH_Exported_Types FLASH Exported Types
61   * @{
62   */
63 
64 /**
65   * @brief  FLASH Procedure structure definition
66   */
67 typedef enum
68 {
69   FLASH_PROC_NONE              = 0,
70   FLASH_PROC_PAGEERASE         = 1,
71   FLASH_PROC_PROGRAM           = 2,
72 } FLASH_ProcedureTypeDef;
73 
74 /**
75   * @brief  FLASH handle Structure definition
76   */
77 typedef struct
78 {
79   __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */
80 
81   __IO uint32_t               NbPagesToErase;   /*!< Internal variable to save the remaining sectors to erase in IT context*/
82 
83   __IO uint32_t               Address;          /*!< Internal variable to save address selected for program or erase */
84 
85   __IO uint32_t               Page;             /*!< Internal variable to define the current page which is erasing */
86 
87   HAL_LockTypeDef             Lock;             /*!< FLASH locking object                */
88 
89   __IO uint32_t               ErrorCode;        /*!< FLASH error code
90                                                      This parameter can be a value of @ref FLASH_Error_Codes  */
91 } FLASH_ProcessTypeDef;
92 
93 /**
94   * @}
95   */
96 
97 /* Exported constants --------------------------------------------------------*/
98 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
99   * @{
100   */
101 
102 /** @defgroup FLASH_Error_Codes FLASH Error Codes
103   * @{
104   */
105 
106 #define HAL_FLASH_ERROR_NONE      0x00U  /*!< No error */
107 #define HAL_FLASH_ERROR_PGA       0x01U  /*!< Programming alignment error */
108 #define HAL_FLASH_ERROR_WRP       0x02U  /*!< Write protection error */
109 #define HAL_FLASH_ERROR_OPTV      0x04U  /*!< Option validity error */
110 #define HAL_FLASH_ERROR_SIZE      0x08U  /*!<  */
111 #define HAL_FLASH_ERROR_RD        0x10U  /*!< Read protected error */
112 #define HAL_FLASH_ERROR_FWWERR    0x20U  /*!< FLASH Write or Erase operation aborted */
113 #define HAL_FLASH_ERROR_NOTZERO   0x40U  /*!< FLASH Write operation is done in a not-erased region */
114 
115 /**
116   * @}
117   */
118 
119 /** @defgroup FLASH_Page_Size FLASH size information
120   * @{
121   */
122 
123 #define FLASH_SIZE                (uint32_t)((*((uint32_t *)FLASHSIZE_BASE)&0xFFFF) * 1024U)
124 #define FLASH_PAGE_SIZE           (128U)  /*!< FLASH Page Size in bytes */
125 
126 #define FLASH_END                 (FLASH_BASE + FLASH_SIZE - 1)    /*!< FLASH end address in the alias region */
127 
128 #if defined (STM32L071xx) || defined (STM32L072xx) || defined (STM32L073xx) || defined (STM32L081xx) || defined (STM32L082xx) || defined (STM32L083xx)
129 #define FLASH_BANK2_BASE          (FLASH_BASE + (FLASH_SIZE >> 1)) /*!< FLASH BANK2 base address in the alias region */
130 #define FLASH_BANK1_END           (FLASH_BANK2_BASE - 1)           /*!< Program end FLASH BANK1 address */
131 #define FLASH_BANK2_END           (FLASH_END)                      /*!< Program end FLASH BANK2 address */
132 #endif
133 
134 /**
135   * @}
136   */
137 
138 /** @defgroup FLASH_Type_Program FLASH Type Program
139   * @{
140   */
141 #define FLASH_TYPEPROGRAM_WORD       (0x02U)  /*!<Program a word (32-bit) at a specified address.*/
142 
143 /**
144   * @}
145   */
146 
147 /** @defgroup FLASH_Latency FLASH Latency
148   * @{
149   */
150 #define FLASH_LATENCY_0            (0x00000000U)    /*!< FLASH Zero Latency cycle */
151 #define FLASH_LATENCY_1            FLASH_ACR_LATENCY         /*!< FLASH One Latency cycle */
152 
153 /**
154   * @}
155   */
156 
157 /** @defgroup FLASH_Interrupts FLASH Interrupts
158   * @{
159   */
160 
161 #define FLASH_IT_EOP               FLASH_PECR_EOPIE  /*!< End of programming interrupt source */
162 #define FLASH_IT_ERR               FLASH_PECR_ERRIE  /*!< Error interrupt source */
163 /**
164   * @}
165   */
166 
167 /** @defgroup FLASH_Flags FLASH Flags
168   * @{
169   */
170 
171 #define FLASH_FLAG_BSY             FLASH_SR_BSY        /*!< FLASH Busy flag */
172 #define FLASH_FLAG_EOP             FLASH_SR_EOP        /*!< FLASH End of Programming flag */
173 #define FLASH_FLAG_ENDHV           FLASH_SR_HVOFF      /*!< FLASH End of High Voltage flag */
174 #define FLASH_FLAG_READY           FLASH_SR_READY      /*!< FLASH Ready flag after low power mode */
175 #define FLASH_FLAG_WRPERR          FLASH_SR_WRPERR     /*!< FLASH Write protected error flag */
176 #define FLASH_FLAG_PGAERR          FLASH_SR_PGAERR     /*!< FLASH Programming Alignment error flag */
177 #define FLASH_FLAG_SIZERR          FLASH_SR_SIZERR     /*!< FLASH Size error flag  */
178 #define FLASH_FLAG_OPTVERR         FLASH_SR_OPTVERR    /*!< FLASH Option Validity error flag  */
179 #define FLASH_FLAG_RDERR           FLASH_SR_RDERR      /*!< FLASH Read protected error flag */
180 #define FLASH_FLAG_FWWERR          FLASH_SR_FWWERR     /*!< FLASH Write or Errase operation aborted */
181 #define FLASH_FLAG_NOTZEROERR      FLASH_SR_NOTZEROERR /*!< FLASH Read protected error flag */
182 
183 /**
184   * @}
185   */
186 
187 /** @defgroup FLASH_Keys FLASH Keys
188   * @{
189   */
190 
191 #define FLASH_PDKEY1               (0x04152637U) /*!< Flash power down key1 */
192 #define FLASH_PDKEY2               (0xFAFBFCFDU) /*!< Flash power down key2: used with FLASH_PDKEY1
193                                                     to unlock the RUN_PD bit in FLASH_ACR */
194 
195 #define FLASH_PEKEY1               (0x89ABCDEFU) /*!< Flash program erase key1 */
196 #define FLASH_PEKEY2               (0x02030405U) /*!< Flash program erase key: used with FLASH_PEKEY2
197                                                      to unlock the write access to the FLASH_PECR register and
198                                                      data EEPROM */
199 
200 #define FLASH_PRGKEY1              (0x8C9DAEBFU) /*!< Flash program memory key1 */
201 #define FLASH_PRGKEY2              (0x13141516U) /*!< Flash program memory key2: used with FLASH_PRGKEY2
202                                                      to unlock the program memory */
203 
204 #define FLASH_OPTKEY1              (0xFBEAD9C8U) /*!< Flash option key1 */
205 #define FLASH_OPTKEY2              (0x24252627U) /*!< Flash option key2: used with FLASH_OPTKEY1 to
206                                                               unlock the write access to the option byte block */
207 /**
208   * @}
209   */
210 
211 /* CMSIS_Legacy */
212 
213 #if defined ( __ICCARM__ )
214 #define InterruptType_ACTLR_DISMCYCINT_Msk         IntType_ACTLR_DISMCYCINT_Msk
215 #endif
216 
217 /**
218   * @}
219   */
220 
221 /* Exported macro ------------------------------------------------------------*/
222 
223 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
224  *  @brief macros to control FLASH features
225  *  @{
226  */
227 
228 
229 /** @defgroup FLASH_Interrupt FLASH Interrupts
230  *  @brief macros to handle FLASH interrupts
231  * @{
232  */
233 
234 /**
235   * @brief  Enable the specified FLASH interrupt.
236   * @param  __INTERRUPT__  FLASH interrupt
237   *         This parameter can be any combination of the following values:
238   *     @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
239   *     @arg @ref FLASH_IT_ERR Error Interrupt
240   * @retval none
241   */
242 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  SET_BIT((FLASH->PECR), (__INTERRUPT__))
243 
244 /**
245   * @brief  Disable the specified FLASH interrupt.
246   * @param  __INTERRUPT__  FLASH interrupt
247   *         This parameter can be any combination of the following values:
248   *     @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
249   *     @arg @ref FLASH_IT_ERR Error Interrupt
250   * @retval none
251   */
252 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  CLEAR_BIT((FLASH->PECR), (uint32_t)(__INTERRUPT__))
253 
254 /**
255   * @brief  Get the specified FLASH flag status.
256   * @param  __FLAG__ specifies the FLASH flag to check.
257   *          This parameter can be one of the following values:
258   *            @arg @ref FLASH_FLAG_BSY         FLASH Busy flag
259   *            @arg @ref FLASH_FLAG_EOP         FLASH End of Operation flag
260   *            @arg @ref FLASH_FLAG_ENDHV       FLASH End of High Voltage flag
261   *            @arg @ref FLASH_FLAG_READY       FLASH Ready flag after low power mode
262   *            @arg @ref FLASH_FLAG_PGAERR      FLASH Programming Alignment error flag
263   *            @arg @ref FLASH_FLAG_SIZERR      FLASH Size error flag
264   *            @arg @ref FLASH_FLAG_OPTVERR     FLASH Option validity error flag (not valid with STM32L031xx/STM32L041xx)
265   *            @arg @ref FLASH_FLAG_RDERR       FLASH Read protected error flag
266   *            @arg @ref FLASH_FLAG_WRPERR      FLASH Write protected error flag
267   *            @arg @ref FLASH_FLAG_FWWERR      FLASH Fetch While Write Error flag
268   *            @arg @ref FLASH_FLAG_NOTZEROERR  Not Zero area error flag
269   * @retval The new state of __FLAG__ (SET or RESET).
270   */
271 #define __HAL_FLASH_GET_FLAG(__FLAG__)   (((FLASH->SR) & (__FLAG__)) == (__FLAG__))
272 
273 /**
274   * @brief  Clear the specified FLASH flag.
275   * @param  __FLAG__ specifies the FLASH flags to clear.
276   *          This parameter can be any combination of the following values:
277   *            @arg @ref FLASH_FLAG_EOP         FLASH End of Operation flag
278   *            @arg @ref FLASH_FLAG_PGAERR      FLASH Programming Alignment error flag
279   *            @arg @ref FLASH_FLAG_SIZERR      FLASH Size error flag
280   *            @arg @ref FLASH_FLAG_OPTVERR     FLASH Option validity error flag (not valid with STM32L031xx/STM32L041xx)
281   *            @arg @ref FLASH_FLAG_RDERR       FLASH Read protected error flag
282   *            @arg @ref FLASH_FLAG_WRPERR      FLASH Write protected error flag
283   *            @arg @ref FLASH_FLAG_FWWERR      FLASH Fetch While Write Error flag
284   *            @arg @ref FLASH_FLAG_NOTZEROERR  Not Zero area error flag
285   * @retval none
286   */
287 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   ((FLASH->SR) = (__FLAG__))
288 
289 /**
290   * @}
291   */
292 
293 /**
294   * @}
295   */
296 
297 /* Include FLASH HAL Extended module */
298 #include "stm32l0xx_hal_flash_ex.h"
299 #include "stm32l0xx_hal_flash_ramfunc.h"
300 
301 /* Exported functions --------------------------------------------------------*/
302 /** @addtogroup FLASH_Exported_Functions
303   * @{
304   */
305 
306 /** @addtogroup FLASH_Exported_Functions_Group1
307   * @{
308   */
309 /* IO operation functions *****************************************************/
310 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
311 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint32_t Data);
312 
313 /* FLASH IRQ handler function */
314 void       HAL_FLASH_IRQHandler(void);
315 /* Callbacks in non blocking modes */
316 void       HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
317 void       HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
318 
319 /**
320   * @}
321   */
322 
323 /** @addtogroup FLASH_Exported_Functions_Group2
324   * @{
325   */
326 /* Peripheral Control functions ***********************************************/
327 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
328 HAL_StatusTypeDef HAL_FLASH_Lock(void);
329 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
330 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
331 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
332 
333 /**
334   * @}
335   */
336 
337 /** @addtogroup FLASH_Exported_Functions_Group3
338   * @{
339   */
340 /* Peripheral State and Error functions ***************************************/
341 uint32_t HAL_FLASH_GetError(void);
342 
343 /**
344   * @}
345   */
346 
347 /**
348   * @}
349   */
350 
351 /** @addtogroup FLASH_Private_Variables
352   * @{
353   */
354 /* Variables used for Erase pages under interruption*/
355 extern FLASH_ProcessTypeDef pFlash;
356 
357 /**
358   * @}
359   */
360 
361 /* Private function -------------------------------------------------*/
362 /** @addtogroup FLASH_Private_Functions
363  * @{
364  */
365 HAL_StatusTypeDef       FLASH_WaitForLastOperation(uint32_t Timeout);
366 
367 /**
368   * @}
369   */
370 
371 /**
372   * @}
373   */
374 
375 /**
376   * @}
377   */
378 
379 #ifdef __cplusplus
380 }
381 #endif
382 
383 #endif /* __STM32L0xx_HAL_FLASH_H */
384 
385 
386