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