1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_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 __STM32F4xx_HAL_FLASH_H
20 #define __STM32F4xx_HAL_FLASH_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32f4xx_hal_def.h"
28 
29 /** @addtogroup STM32F4xx_HAL_Driver
30   * @{
31   */
32 
33 /** @addtogroup FLASH
34   * @{
35   */
36 
37 /* Exported types ------------------------------------------------------------*/
38 /** @defgroup FLASH_Exported_Types FLASH Exported Types
39   * @{
40   */
41 
42 /**
43   * @brief  FLASH Procedure structure definition
44   */
45 typedef enum
46 {
47   FLASH_PROC_NONE = 0U,
48   FLASH_PROC_SECTERASE,
49   FLASH_PROC_MASSERASE,
50   FLASH_PROC_PROGRAM
51 } FLASH_ProcedureTypeDef;
52 
53 /**
54   * @brief  FLASH handle Structure definition
55   */
56 typedef struct
57 {
58   __IO FLASH_ProcedureTypeDef ProcedureOnGoing;   /*Internal variable to indicate which procedure is ongoing or not in IT context*/
59 
60   __IO uint32_t               NbSectorsToErase;   /*Internal variable to save the remaining sectors to erase in IT context*/
61 
62   __IO uint8_t                VoltageForErase;    /*Internal variable to provide voltage range selected by user in IT context*/
63 
64   __IO uint32_t               Sector;             /*Internal variable to define the current sector which is erasing*/
65 
66   __IO uint32_t               Bank;               /*Internal variable to save current bank selected during mass erase*/
67 
68   __IO uint32_t               Address;            /*Internal variable to save address selected for program*/
69 
70   HAL_LockTypeDef             Lock;               /* FLASH locking object                */
71 
72   __IO uint32_t               ErrorCode;          /* FLASH error code                    */
73 
74 }FLASH_ProcessTypeDef;
75 
76 /**
77   * @}
78   */
79 
80 /* Exported constants --------------------------------------------------------*/
81 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
82   * @{
83   */
84 /** @defgroup FLASH_Error_Code FLASH Error Code
85   * @brief    FLASH Error Code
86   * @{
87   */
88 #define HAL_FLASH_ERROR_NONE         0x00000000U    /*!< No error                      */
89 #define HAL_FLASH_ERROR_RD           0x00000001U    /*!< Read Protection error         */
90 #define HAL_FLASH_ERROR_PGS          0x00000002U    /*!< Programming Sequence error    */
91 #define HAL_FLASH_ERROR_PGP          0x00000004U    /*!< Programming Parallelism error */
92 #define HAL_FLASH_ERROR_PGA          0x00000008U    /*!< Programming Alignment error   */
93 #define HAL_FLASH_ERROR_WRP          0x00000010U    /*!< Write protection error        */
94 #define HAL_FLASH_ERROR_OPERATION    0x00000020U    /*!< Operation Error               */
95 /**
96   * @}
97   */
98 
99 /** @defgroup FLASH_Type_Program FLASH Type Program
100   * @{
101   */
102 #define FLASH_TYPEPROGRAM_BYTE        0x00000000U  /*!< Program byte (8-bit) at a specified address           */
103 #define FLASH_TYPEPROGRAM_HALFWORD    0x00000001U  /*!< Program a half-word (16-bit) at a specified address   */
104 #define FLASH_TYPEPROGRAM_WORD        0x00000002U  /*!< Program a word (32-bit) at a specified address        */
105 #define FLASH_TYPEPROGRAM_DOUBLEWORD  0x00000003U  /*!< Program a double word (64-bit) at a specified address */
106 /**
107   * @}
108   */
109 
110 /** @defgroup FLASH_Flag_definition FLASH Flag definition
111   * @brief Flag definition
112   * @{
113   */
114 #define FLASH_FLAG_EOP                 FLASH_SR_EOP            /*!< FLASH End of Operation flag               */
115 #define FLASH_FLAG_OPERR               FLASH_SR_SOP            /*!< FLASH operation Error flag                */
116 #define FLASH_FLAG_WRPERR              FLASH_SR_WRPERR         /*!< FLASH Write protected error flag          */
117 #define FLASH_FLAG_PGAERR              FLASH_SR_PGAERR         /*!< FLASH Programming Alignment error flag    */
118 #define FLASH_FLAG_PGPERR              FLASH_SR_PGPERR         /*!< FLASH Programming Parallelism error flag  */
119 #define FLASH_FLAG_PGSERR              FLASH_SR_PGSERR         /*!< FLASH Programming Sequence error flag     */
120 #if defined(FLASH_SR_RDERR)
121 #define FLASH_FLAG_RDERR               FLASH_SR_RDERR          /*!< Read Protection error flag (PCROP)        */
122 #endif /* FLASH_SR_RDERR */
123 #define FLASH_FLAG_BSY                 FLASH_SR_BSY            /*!< FLASH Busy flag                           */
124 /**
125   * @}
126   */
127 
128 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
129   * @brief FLASH Interrupt definition
130   * @{
131   */
132 #define FLASH_IT_EOP                   FLASH_CR_EOPIE          /*!< End of FLASH Operation Interrupt source */
133 #define FLASH_IT_ERR                   0x02000000U             /*!< Error Interrupt source                  */
134 /**
135   * @}
136   */
137 
138 /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
139   * @{
140   */
141 #define FLASH_PSIZE_BYTE           0x00000000U
142 #define FLASH_PSIZE_HALF_WORD      0x00000100U
143 #define FLASH_PSIZE_WORD           0x00000200U
144 #define FLASH_PSIZE_DOUBLE_WORD    0x00000300U
145 #define CR_PSIZE_MASK              0xFFFFFCFFU
146 /**
147   * @}
148   */
149 
150 /** @defgroup FLASH_Keys FLASH Keys
151   * @{
152   */
153 #define RDP_KEY                  ((uint16_t)0x00A5)
154 #define FLASH_KEY1               0x45670123U
155 #define FLASH_KEY2               0xCDEF89ABU
156 #define FLASH_OPT_KEY1           0x08192A3BU
157 #define FLASH_OPT_KEY2           0x4C5D6E7FU
158 /**
159   * @}
160   */
161 
162 /**
163   * @}
164   */
165 
166 /* Exported macro ------------------------------------------------------------*/
167 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
168   * @{
169   */
170 /**
171   * @brief  Set the FLASH Latency.
172   * @param  __LATENCY__ FLASH Latency
173   *         The value of this parameter depend on device used within the same series
174   * @retval none
175   */
176 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
177 
178 /**
179   * @brief  Get the FLASH Latency.
180   * @retval FLASH Latency
181   *          The value of this parameter depend on device used within the same series
182   */
183 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
184 
185 /**
186   * @brief  Enable the FLASH prefetch buffer.
187   * @retval none
188   */
189 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()  (FLASH->ACR |= FLASH_ACR_PRFTEN)
190 
191 /**
192   * @brief  Disable the FLASH prefetch buffer.
193   * @retval none
194   */
195 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
196 
197 /**
198   * @brief  Enable the FLASH instruction cache.
199   * @retval none
200   */
201 #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_ICEN)
202 
203 /**
204   * @brief  Disable the FLASH instruction cache.
205   * @retval none
206   */
207 #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_ICEN))
208 
209 /**
210   * @brief  Enable the FLASH data cache.
211   * @retval none
212   */
213 #define __HAL_FLASH_DATA_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_DCEN)
214 
215 /**
216   * @brief  Disable the FLASH data cache.
217   * @retval none
218   */
219 #define __HAL_FLASH_DATA_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_DCEN))
220 
221 /**
222   * @brief  Resets the FLASH instruction Cache.
223   * @note   This function must be used only when the Instruction Cache is disabled.
224   * @retval None
225   */
226 #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_ICRST;  \
227                                                   FLASH->ACR &= ~FLASH_ACR_ICRST; \
228                                                  }while(0U)
229 
230 /**
231   * @brief  Resets the FLASH data Cache.
232   * @note   This function must be used only when the data Cache is disabled.
233   * @retval None
234   */
235 #define __HAL_FLASH_DATA_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_DCRST;  \
236                                            FLASH->ACR &= ~FLASH_ACR_DCRST; \
237                                           }while(0U)
238 /**
239   * @brief  Enable the specified FLASH interrupt.
240   * @param  __INTERRUPT__  FLASH interrupt
241   *         This parameter can be any combination of the following values:
242   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
243   *     @arg FLASH_IT_ERR: Error Interrupt
244   * @retval none
245   */
246 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  (FLASH->CR |= (__INTERRUPT__))
247 
248 /**
249   * @brief  Disable the specified FLASH interrupt.
250   * @param  __INTERRUPT__  FLASH interrupt
251   *         This parameter can be any combination of the following values:
252   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
253   *     @arg FLASH_IT_ERR: Error Interrupt
254   * @retval none
255   */
256 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
257 
258 /**
259   * @brief  Get the specified FLASH flag status.
260   * @param  __FLAG__ specifies the FLASH flags to check.
261   *          This parameter can be any combination of the following values:
262   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag
263   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag
264   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
265   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
266   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
267   *            @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
268   *            @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
269   *            @arg FLASH_FLAG_BSY   : FLASH Busy flag
270   *           (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices
271   * @retval The new state of __FLAG__ (SET or RESET).
272   */
273 #define __HAL_FLASH_GET_FLAG(__FLAG__)   ((FLASH->SR & (__FLAG__)))
274 
275 /**
276   * @brief  Clear the specified FLASH flags.
277   * @param  __FLAG__ specifies the FLASH flags to clear.
278   *          This parameter can be any combination of the following values:
279   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag
280   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag
281   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
282   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
283   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
284   *            @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
285   *            @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
286   *           (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices
287   * @retval none
288   */
289 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   (FLASH->SR = (__FLAG__))
290 /**
291   * @}
292   */
293 
294 /* Include FLASH HAL Extension module */
295 #include "stm32f4xx_hal_flash_ex.h"
296 #include "stm32f4xx_hal_flash_ramfunc.h"
297 
298 /* Exported functions --------------------------------------------------------*/
299 /** @addtogroup FLASH_Exported_Functions
300   * @{
301   */
302 /** @addtogroup FLASH_Exported_Functions_Group1
303   * @{
304   */
305 /* Program operation functions  ***********************************************/
306 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
307 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
308 /* FLASH IRQ handler method */
309 void HAL_FLASH_IRQHandler(void);
310 /* Callbacks in non blocking modes */
311 void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
312 void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
313 /**
314   * @}
315   */
316 
317 /** @addtogroup FLASH_Exported_Functions_Group2
318   * @{
319   */
320 /* Peripheral Control functions  **********************************************/
321 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
322 HAL_StatusTypeDef HAL_FLASH_Lock(void);
323 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
324 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
325 /* Option bytes control */
326 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
327 /**
328   * @}
329   */
330 
331 /** @addtogroup FLASH_Exported_Functions_Group3
332   * @{
333   */
334 /* Peripheral State functions  ************************************************/
335 uint32_t HAL_FLASH_GetError(void);
336 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
337 /**
338   * @}
339   */
340 
341 /**
342   * @}
343   */
344 /* Private types -------------------------------------------------------------*/
345 /* Private variables ---------------------------------------------------------*/
346 /** @defgroup FLASH_Private_Variables FLASH Private Variables
347   * @{
348   */
349 
350 /**
351   * @}
352   */
353 /* Private constants ---------------------------------------------------------*/
354 /** @defgroup FLASH_Private_Constants FLASH Private Constants
355   * @{
356   */
357 
358 /**
359   * @brief   ACR register byte 0 (Bits[7:0]) base address
360   */
361 #define ACR_BYTE0_ADDRESS           0x40023C00U
362 /**
363   * @brief   OPTCR register byte 0 (Bits[7:0]) base address
364   */
365 #define OPTCR_BYTE0_ADDRESS         0x40023C14U
366 /**
367   * @brief   OPTCR register byte 1 (Bits[15:8]) base address
368   */
369 #define OPTCR_BYTE1_ADDRESS         0x40023C15U
370 /**
371   * @brief   OPTCR register byte 2 (Bits[23:16]) base address
372   */
373 #define OPTCR_BYTE2_ADDRESS         0x40023C16U
374 /**
375   * @brief   OPTCR register byte 3 (Bits[31:24]) base address
376   */
377 #define OPTCR_BYTE3_ADDRESS         0x40023C17U
378 
379 /**
380   * @}
381   */
382 
383 /* Private macros ------------------------------------------------------------*/
384 /** @defgroup FLASH_Private_Macros FLASH Private Macros
385   * @{
386   */
387 
388 /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
389   * @{
390   */
391 #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
392                                     ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
393                                     ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
394                                     ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
395 /**
396   * @}
397   */
398 
399 /**
400   * @}
401   */
402 
403 /* Private functions ---------------------------------------------------------*/
404 /** @defgroup FLASH_Private_Functions FLASH Private Functions
405   * @{
406   */
407 
408 /**
409   * @}
410   */
411 
412 /**
413   * @}
414   */
415 
416 /**
417   * @}
418   */
419 
420 #ifdef __cplusplus
421 }
422 #endif
423 
424 #endif /* __STM32F4xx_HAL_FLASH_H */
425 
426