1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_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 __STM32F0xx_HAL_FLASH_H
20 #define __STM32F0xx_HAL_FLASH_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32f0xx_hal_def.h"
28 
29 /** @addtogroup STM32F0xx_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_HALFWORD) || \
50                                       ((VALUE) == FLASH_TYPEPROGRAM_WORD)     || \
51                                       ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
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_MASSERASE         = 2U,
73   FLASH_PROC_PROGRAMHALFWORD   = 3U,
74   FLASH_PROC_PROGRAMWORD       = 4U,
75   FLASH_PROC_PROGRAMDOUBLEWORD = 5U
76 } FLASH_ProcedureTypeDef;
77 
78 /**
79   * @brief  FLASH handle Structure definition
80   */
81 typedef struct
82 {
83   __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*!< Internal variable to indicate which procedure is ongoing or not in IT context */
84 
85   __IO uint32_t               DataRemaining;    /*!< Internal variable to save the remaining pages to erase or half-word to program in IT context */
86 
87   __IO uint32_t               Address;          /*!< Internal variable to save address selected for program or erase */
88 
89   __IO uint64_t               Data;             /*!< Internal variable to save data to be programmed */
90 
91   HAL_LockTypeDef             Lock;             /*!< FLASH locking object                */
92 
93   __IO uint32_t               ErrorCode;        /*!< FLASH error code
94                                                      This parameter can be a value of @ref FLASH_Error_Codes  */
95 } FLASH_ProcessTypeDef;
96 
97 /**
98   * @}
99   */
100 
101 /* Exported constants --------------------------------------------------------*/
102 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
103   * @{
104   */
105 
106 /** @defgroup FLASH_Error_Codes FLASH Error Codes
107   * @{
108   */
109 
110 #define HAL_FLASH_ERROR_NONE      0x00U  /*!< No error */
111 #define HAL_FLASH_ERROR_PROG      0x01U  /*!< Programming error */
112 #define HAL_FLASH_ERROR_WRP       0x02U  /*!< Write protection error */
113 
114 /**
115   * @}
116   */
117 
118 /** @defgroup FLASH_Type_Program FLASH Type Program
119   * @{
120   */
121 #define FLASH_TYPEPROGRAM_HALFWORD   (0x01U)  /*!<Program a half-word (16-bit) at a specified address.*/
122 #define FLASH_TYPEPROGRAM_WORD       (0x02U)  /*!<Program a word (32-bit) at a specified address.*/
123 #define FLASH_TYPEPROGRAM_DOUBLEWORD (0x03U)  /*!<Program a double word (64-bit) at a specified address*/
124 
125 /**
126   * @}
127   */
128 
129 /** @defgroup FLASH_Latency FLASH Latency
130   * @{
131   */
132 #define FLASH_LATENCY_0            (0x00000000U)    /*!< FLASH Zero Latency cycle */
133 #define FLASH_LATENCY_1            FLASH_ACR_LATENCY         /*!< FLASH One Latency cycle */
134 
135 /**
136   * @}
137   */
138 
139 
140 /** @defgroup FLASH_Flag_definition FLASH Flag definition
141   * @{
142   */
143 #define FLASH_FLAG_BSY             FLASH_SR_BSY            /*!< FLASH Busy flag                           */
144 #define FLASH_FLAG_PGERR           FLASH_SR_PGERR          /*!< FLASH Programming error flag    */
145 #define FLASH_FLAG_WRPERR          FLASH_SR_WRPERR         /*!< FLASH Write protected error flag          */
146 #define FLASH_FLAG_EOP             FLASH_SR_EOP            /*!< FLASH End of Operation flag               */
147 /**
148   * @}
149   */
150 
151 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
152   * @{
153   */
154 #define FLASH_IT_EOP               FLASH_CR_EOPIE          /*!< End of FLASH Operation Interrupt source */
155 #define FLASH_IT_ERR               FLASH_CR_ERRIE  /*!< Error Interrupt source */
156 /**
157   * @}
158   */
159 
160 /**
161   * @}
162   */
163 
164 /* Exported macro ------------------------------------------------------------*/
165 
166 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
167  *  @brief macros to control FLASH features
168  *  @{
169  */
170 
171 
172 /** @defgroup FLASH_EM_Latency FLASH Latency
173  *  @brief macros to handle FLASH Latency
174  * @{
175  */
176 
177 /**
178   * @brief  Set the FLASH Latency.
179   * @param  __LATENCY__ FLASH Latency
180   *         The value of this parameter depend on device used within the same series
181   * @retval None
182   */
183 #define __HAL_FLASH_SET_LATENCY(__LATENCY__)    (FLASH->ACR = (FLASH->ACR&(~FLASH_ACR_LATENCY)) | (__LATENCY__))
184 
185 
186 /**
187   * @brief  Get the FLASH Latency.
188   * @retval FLASH Latency
189   *         The value of this parameter depend on device used within the same series
190   */
191 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
192 
193 /**
194   * @}
195   */
196 
197 /** @defgroup FLASH_Prefetch FLASH Prefetch
198  *  @brief macros to handle FLASH Prefetch buffer
199  * @{
200  */
201 /**
202   * @brief  Enable the FLASH prefetch buffer.
203   * @retval None
204   */
205 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()    (FLASH->ACR |= FLASH_ACR_PRFTBE)
206 
207 /**
208   * @brief  Disable the FLASH prefetch buffer.
209   * @retval None
210   */
211 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_PRFTBE))
212 
213 /**
214   * @}
215   */
216 
217 /** @defgroup FLASH_Interrupt FLASH Interrupts
218  *  @brief macros to handle FLASH interrupts
219  * @{
220  */
221 
222 /**
223   * @brief  Enable the specified FLASH interrupt.
224   * @param  __INTERRUPT__  FLASH interrupt
225   *         This parameter can be any combination of the following values:
226   *     @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
227   *     @arg @ref FLASH_IT_ERR Error Interrupt
228   * @retval none
229   */
230 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  SET_BIT((FLASH->CR), (__INTERRUPT__))
231 
232 /**
233   * @brief  Disable the specified FLASH interrupt.
234   * @param  __INTERRUPT__  FLASH interrupt
235   *         This parameter can be any combination of the following values:
236   *     @arg @ref FLASH_IT_EOP End of FLASH Operation Interrupt
237   *     @arg @ref FLASH_IT_ERR Error Interrupt
238   * @retval none
239   */
240 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  CLEAR_BIT((FLASH->CR), (uint32_t)(__INTERRUPT__))
241 
242 /**
243   * @brief  Get the specified FLASH flag status.
244   * @param  __FLAG__ specifies the FLASH flag to check.
245   *          This parameter can be one of the following values:
246   *            @arg @ref FLASH_FLAG_BSY         FLASH Busy flag
247   *            @arg @ref FLASH_FLAG_EOP         FLASH End of Operation flag
248   *            @arg @ref FLASH_FLAG_WRPERR      FLASH Write protected error flag
249   *            @arg @ref FLASH_FLAG_PGERR       FLASH Programming error flag
250   * @retval The new state of __FLAG__ (SET or RESET).
251   */
252 #define __HAL_FLASH_GET_FLAG(__FLAG__)   (((FLASH->SR) & (__FLAG__)) == (__FLAG__))
253 
254 /**
255   * @brief  Clear the specified FLASH flag.
256   * @param  __FLAG__ specifies the FLASH flags to clear.
257   *          This parameter can be any combination of the following values:
258   *            @arg @ref FLASH_FLAG_EOP         FLASH End of Operation flag
259   *            @arg @ref FLASH_FLAG_WRPERR      FLASH Write protected error flag
260   *            @arg @ref FLASH_FLAG_PGERR       FLASH Programming error flag
261   * @retval none
262   */
263 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   ((FLASH->SR) = (__FLAG__))
264 
265 /**
266   * @}
267   */
268 
269 /**
270   * @}
271   */
272 
273 /* Include FLASH HAL Extended module */
274 #include "stm32f0xx_hal_flash_ex.h"
275 
276 /* Exported functions --------------------------------------------------------*/
277 /** @addtogroup FLASH_Exported_Functions
278   * @{
279   */
280 
281 /** @addtogroup FLASH_Exported_Functions_Group1
282   * @{
283   */
284 /* IO operation functions *****************************************************/
285 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
286 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
287 
288 /* FLASH IRQ handler function */
289 void       HAL_FLASH_IRQHandler(void);
290 /* Callbacks in non blocking modes */
291 void       HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
292 void       HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
293 
294 /**
295   * @}
296   */
297 
298 /** @addtogroup FLASH_Exported_Functions_Group2
299   * @{
300   */
301 /* Peripheral Control functions ***********************************************/
302 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
303 HAL_StatusTypeDef HAL_FLASH_Lock(void);
304 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
305 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
306 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
307 
308 /**
309   * @}
310   */
311 
312 /** @addtogroup FLASH_Exported_Functions_Group3
313   * @{
314   */
315 /* Peripheral State and Error functions ***************************************/
316 uint32_t HAL_FLASH_GetError(void);
317 
318 /**
319   * @}
320   */
321 
322 /**
323   * @}
324   */
325 
326 /* Private function -------------------------------------------------*/
327 /** @addtogroup FLASH_Private_Functions
328  * @{
329  */
330 HAL_StatusTypeDef       FLASH_WaitForLastOperation(uint32_t Timeout);
331 
332 /**
333   * @}
334   */
335 
336 /**
337   * @}
338   */
339 
340 /**
341   * @}
342   */
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 #endif /* __STM32F0xx_HAL_FLASH_H */
349 
350 
351