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