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