1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_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 __STM32F7xx_HAL_FLASH_H
20 #define __STM32F7xx_HAL_FLASH_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32f7xx_hal_def.h"
28 
29 /** @addtogroup STM32F7xx_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 /**
55   * @brief  FLASH handle Structure definition
56   */
57 typedef struct
58 {
59   __IO FLASH_ProcedureTypeDef ProcedureOnGoing;   /* Internal variable to indicate which procedure is ongoing or not in IT context */
60 
61   __IO uint32_t               NbSectorsToErase;   /* Internal variable to save the remaining sectors to erase in IT context        */
62 
63   __IO uint8_t                VoltageForErase;    /* Internal variable to provide voltage range selected by user in IT context     */
64 
65   __IO uint32_t               Sector;             /* Internal variable to define the current sector which is erasing               */
66 
67   __IO uint32_t               Address;            /* Internal variable to save address selected for program                        */
68 
69   HAL_LockTypeDef             Lock;               /* FLASH locking object                                                          */
70 
71   __IO uint32_t               ErrorCode;          /* FLASH error code                                                              */
72 
73 }FLASH_ProcessTypeDef;
74 
75 /**
76   * @}
77   */
78 
79 /* Exported constants --------------------------------------------------------*/
80 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
81   * @{
82   */
83 
84 /** @defgroup FLASH_Error_Code FLASH Error Code
85   * @brief    FLASH Error Code
86   * @{
87   */
88 #define HAL_FLASH_ERROR_NONE         ((uint32_t)0x00000000U)    /*!< No error                      */
89 #define HAL_FLASH_ERROR_ERS          ((uint32_t)0x00000002U)    /*!< Programming Sequence error    */
90 #define HAL_FLASH_ERROR_PGP          ((uint32_t)0x00000004U)    /*!< Programming Parallelism error */
91 #define HAL_FLASH_ERROR_PGA          ((uint32_t)0x00000008U)    /*!< Programming Alignment error   */
92 #define HAL_FLASH_ERROR_WRP          ((uint32_t)0x00000010U)    /*!< Write protection error        */
93 #define HAL_FLASH_ERROR_OPERATION    ((uint32_t)0x00000020U)    /*!< Operation Error               */
94 #define HAL_FLASH_ERROR_RD           ((uint32_t)0x00000040U)    /*!< Read Protection Error         */
95 /**
96   * @}
97   */
98 
99 /** @defgroup FLASH_Type_Program FLASH Type Program
100   * @{
101   */
102 #define FLASH_TYPEPROGRAM_BYTE        ((uint32_t)0x00U)  /*!< Program byte (8-bit) at a specified address           */
103 #define FLASH_TYPEPROGRAM_HALFWORD    ((uint32_t)0x01U)  /*!< Program a half-word (16-bit) at a specified address   */
104 #define FLASH_TYPEPROGRAM_WORD        ((uint32_t)0x02U)  /*!< Program a word (32-bit) at a specified address        */
105 #define FLASH_TYPEPROGRAM_DOUBLEWORD  ((uint32_t)0x03U)  /*!< 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_OPERR          /*!< 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_ERSERR              FLASH_SR_ERSERR         /*!< FLASH Erasing Sequence error flag         */
120 #define FLASH_FLAG_BSY                 FLASH_SR_BSY            /*!< FLASH Busy flag                           */
121 
122 #if defined (FLASH_OPTCR2_PCROP)
123 #define FLASH_FLAG_RDERR               FLASH_SR_RDERR          /*!< FLASH Read protection error flag          */
124 #define FLASH_FLAG_ALL_ERRORS     (FLASH_FLAG_OPERR   | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
125                                    FLASH_FLAG_PGPERR  | FLASH_FLAG_ERSERR | FLASH_FLAG_RDERR)
126 #else
127 #define FLASH_FLAG_ALL_ERRORS     (FLASH_FLAG_OPERR   | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
128                                    FLASH_FLAG_PGPERR  | FLASH_FLAG_ERSERR)
129 #endif /* FLASH_OPTCR2_PCROP */
130 /**
131   * @}
132   */
133 
134 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
135   * @brief FLASH Interrupt definition
136   * @{
137   */
138 #define FLASH_IT_EOP                   FLASH_CR_EOPIE          /*!< End of FLASH Operation Interrupt source */
139 #define FLASH_IT_ERR                   ((uint32_t)0x02000000U)  /*!< Error Interrupt source                  */
140 /**
141   * @}
142   */
143 
144 /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
145   * @{
146   */
147 #define FLASH_PSIZE_BYTE           ((uint32_t)0x00000000U)
148 #define FLASH_PSIZE_HALF_WORD      ((uint32_t)FLASH_CR_PSIZE_0)
149 #define FLASH_PSIZE_WORD           ((uint32_t)FLASH_CR_PSIZE_1)
150 #define FLASH_PSIZE_DOUBLE_WORD    ((uint32_t)FLASH_CR_PSIZE)
151 #define CR_PSIZE_MASK              ((uint32_t)0xFFFFFCFFU)
152 /**
153   * @}
154   */
155 
156 /** @defgroup FLASH_Keys FLASH Keys
157   * @{
158   */
159 #define FLASH_KEY1               ((uint32_t)0x45670123U)
160 #define FLASH_KEY2               ((uint32_t)0xCDEF89ABU)
161 #define FLASH_OPT_KEY1           ((uint32_t)0x08192A3BU)
162 #define FLASH_OPT_KEY2           ((uint32_t)0x4C5D6E7FU)
163 /**
164   * @}
165   */
166 
167 /** @defgroup FLASH_Sectors FLASH Sectors
168   * @{
169   */
170 #if (FLASH_SECTOR_TOTAL == 2)
171 #define FLASH_SECTOR_0           ((uint32_t)0U) /*!< Sector Number 0   */
172 #define FLASH_SECTOR_1           ((uint32_t)1U) /*!< Sector Number 1   */
173 #elif (FLASH_SECTOR_TOTAL == 4)
174 #define FLASH_SECTOR_0           ((uint32_t)0U) /*!< Sector Number 0   */
175 #define FLASH_SECTOR_1           ((uint32_t)1U) /*!< Sector Number 1   */
176 #define FLASH_SECTOR_2           ((uint32_t)2U) /*!< Sector Number 2   */
177 #define FLASH_SECTOR_3           ((uint32_t)3U) /*!< Sector Number 3   */
178 #else
179 #define FLASH_SECTOR_0           ((uint32_t)0U) /*!< Sector Number 0   */
180 #define FLASH_SECTOR_1           ((uint32_t)1U) /*!< Sector Number 1   */
181 #define FLASH_SECTOR_2           ((uint32_t)2U) /*!< Sector Number 2   */
182 #define FLASH_SECTOR_3           ((uint32_t)3U) /*!< Sector Number 3   */
183 #define FLASH_SECTOR_4           ((uint32_t)4U) /*!< Sector Number 4   */
184 #define FLASH_SECTOR_5           ((uint32_t)5U) /*!< Sector Number 5   */
185 #define FLASH_SECTOR_6           ((uint32_t)6U) /*!< Sector Number 6   */
186 #define FLASH_SECTOR_7           ((uint32_t)7U) /*!< Sector Number 7   */
187 #endif /* FLASH_SECTOR_TOTAL */
188 /**
189   * @}
190   */
191 
192 /**
193   * @}
194   */
195 
196 /* Exported macro ------------------------------------------------------------*/
197 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
198   * @{
199   */
200 /**
201   * @brief  Set the FLASH Latency.
202   * @param  __LATENCY__ FLASH Latency
203   *         The value of this parameter depend on device used within the same series
204   * @retval none
205   */
206 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) \
207                   MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, (uint32_t)(__LATENCY__))
208 
209 /**
210   * @brief  Get the FLASH Latency.
211   * @retval FLASH Latency
212   *          The value of this parameter depend on device used within the same series
213   */
214 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
215 
216 /**
217   * @brief  Enable the FLASH prefetch buffer.
218   * @retval none
219   */
220 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()  (FLASH->ACR |= FLASH_ACR_PRFTEN)
221 
222 /**
223   * @brief  Disable the FLASH prefetch buffer.
224   * @retval none
225   */
226 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
227 
228 /**
229   * @brief  Enable the FLASH Adaptive Real-Time memory accelerator.
230   * @note   The ART accelerator is available only for flash access on ITCM interface.
231   * @retval none
232   */
233 #define __HAL_FLASH_ART_ENABLE()  SET_BIT(FLASH->ACR, FLASH_ACR_ARTEN)
234 
235 /**
236   * @brief  Disable the FLASH Adaptive Real-Time memory accelerator.
237   * @retval none
238   */
239 #define __HAL_FLASH_ART_DISABLE()   CLEAR_BIT(FLASH->ACR, FLASH_ACR_ARTEN)
240 
241 /**
242   * @brief  Resets the FLASH Adaptive Real-Time memory accelerator.
243   * @note   This function must be used only when the Adaptive Real-Time memory accelerator
244   *         is disabled.
245   * @retval None
246   */
247 #define __HAL_FLASH_ART_RESET()  (FLASH->ACR |= FLASH_ACR_ARTRST)
248 
249 /**
250   * @brief  Enable the specified FLASH interrupt.
251   * @param  __INTERRUPT__  FLASH interrupt
252   *         This parameter can be any combination of the following values:
253   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
254   *     @arg FLASH_IT_ERR: Error Interrupt
255   * @retval none
256   */
257 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  (FLASH->CR |= (__INTERRUPT__))
258 
259 /**
260   * @brief  Disable the specified FLASH interrupt.
261   * @param  __INTERRUPT__  FLASH interrupt
262   *         This parameter can be any combination of the following values:
263   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
264   *     @arg FLASH_IT_ERR: Error Interrupt
265   * @retval none
266   */
267 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
268 
269 /**
270   * @brief  Get the specified FLASH flag status.
271   * @param  __FLAG__ specifies the FLASH flag to check.
272   *          This parameter can be one of the following values:
273   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag
274   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag
275   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
276   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
277   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
278   *            @arg FLASH_FLAG_ERSERR : FLASH Erasing Sequence error flag
279   *            @arg FLASH_FLAG_BSY   : FLASH Busy flag
280   * @retval The new state of __FLAG__ (SET or RESET).
281   */
282 #define __HAL_FLASH_GET_FLAG(__FLAG__)   ((FLASH->SR & (__FLAG__)))
283 
284 /**
285   * @brief  Clear the specified FLASH flag.
286   * @param  __FLAG__ specifies the FLASH flags to clear.
287   *          This parameter can be any combination of the following values:
288   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag
289   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag
290   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
291   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
292   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
293   *            @arg FLASH_FLAG_ERSERR : FLASH Erasing Sequence error flag
294   * @retval none
295   */
296 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   (FLASH->SR = (__FLAG__))
297 /**
298   * @}
299   */
300 
301 /* Include FLASH HAL Extension module */
302 #include "stm32f7xx_hal_flash_ex.h"
303 
304 /* Exported functions --------------------------------------------------------*/
305 /** @addtogroup FLASH_Exported_Functions
306   * @{
307   */
308 /** @addtogroup FLASH_Exported_Functions_Group1
309   * @{
310   */
311 /* Program operation functions  ***********************************************/
312 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
313 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
314 /* FLASH IRQ handler method */
315 void HAL_FLASH_IRQHandler(void);
316 /* Callbacks in non blocking modes */
317 void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
318 void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
319 /**
320   * @}
321   */
322 
323 /** @addtogroup FLASH_Exported_Functions_Group2
324   * @{
325   */
326 /* Peripheral Control functions  **********************************************/
327 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
328 HAL_StatusTypeDef HAL_FLASH_Lock(void);
329 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
330 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
331 /* Option bytes control */
332 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
333 /**
334   * @}
335   */
336 
337 /** @addtogroup FLASH_Exported_Functions_Group3
338   * @{
339   */
340 /* Peripheral State functions  ************************************************/
341 uint32_t HAL_FLASH_GetError(void);
342 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
343 /**
344   * @}
345   */
346 
347 /**
348   * @}
349   */
350 /* Private types -------------------------------------------------------------*/
351 /* Private variables ---------------------------------------------------------*/
352 /** @defgroup FLASH_Private_Variables FLASH Private Variables
353   * @{
354   */
355 
356 /**
357   * @}
358   */
359 /* Private constants ---------------------------------------------------------*/
360 /** @defgroup FLASH_Private_Constants FLASH Private Constants
361   * @{
362   */
363 
364 /**
365   * @brief   OPTCR register byte 1 (Bits[15:8]) base address
366   */
367 #define OPTCR_BYTE1_ADDRESS         ((uint32_t)0x40023C15)
368 
369 /**
370   * @}
371   */
372 
373 /* Private macros ------------------------------------------------------------*/
374 /** @defgroup FLASH_Private_Macros FLASH Private Macros
375   * @{
376   */
377 
378 /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
379   * @{
380   */
381 #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
382                                     ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
383                                     ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
384                                     ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
385 /**
386   * @}
387   */
388 
389 /**
390   * @}
391   */
392 
393 /* Private functions ---------------------------------------------------------*/
394 /** @defgroup FLASH_Private_Functions FLASH Private Functions
395   * @{
396   */
397 
398 /**
399   * @}
400   */
401 
402 /**
403   * @}
404   */
405 
406 /**
407   * @}
408   */
409 
410 #ifdef __cplusplus
411 }
412 #endif
413 
414 #endif /* __STM32F7xx_HAL_FLASH_H */
415 
416