1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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 __STM32F2xx_HAL_FLASH_H
20 #define __STM32F2xx_HAL_FLASH_H
21 
22 #ifdef __cplusplus
23  extern "C" {
24 #endif
25 
26 /* Includes ------------------------------------------------------------------*/
27 #include "stm32f2xx_hal_def.h"
28 
29 /** @addtogroup STM32F2xx_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_PGS          0x00000001U    /*!< Programming Sequence error    */
90 #define HAL_FLASH_ERROR_PGP          0x00000002U    /*!< Programming Parallelism error */
91 #define HAL_FLASH_ERROR_PGA          0x00000004U    /*!< Programming Alignment error   */
92 #define HAL_FLASH_ERROR_WRP          0x00000008U    /*!< Write protection error        */
93 #define HAL_FLASH_ERROR_OPERATION    0x00000010U    /*!< Operation Error               */
94 /**
95   * @}
96   */
97 
98 /** @defgroup FLASH_Type_Program FLASH Type Program
99   * @{
100   */
101 #define FLASH_TYPEPROGRAM_BYTE        0x00U  /*!< Program byte (8-bit) at a specified address           */
102 #define FLASH_TYPEPROGRAM_HALFWORD    0x01U  /*!< Program a half-word (16-bit) at a specified address   */
103 #define FLASH_TYPEPROGRAM_WORD        0x02U  /*!< Program a word (32-bit) at a specified address        */
104 #define FLASH_TYPEPROGRAM_DOUBLEWORD  0x03U  /*!< Program a double word (64-bit) at a specified address */
105 /**
106   * @}
107   */
108 
109 /** @defgroup FLASH_Flag_definition FLASH Flag definition
110   * @brief Flag definition
111   * @{
112   */
113 #define FLASH_FLAG_EOP                 FLASH_SR_EOP            /*!< FLASH End of Operation flag               */
114 #define FLASH_FLAG_OPERR               FLASH_SR_SOP            /*!< FLASH operation Error flag                */
115 #define FLASH_FLAG_WRPERR              FLASH_SR_WRPERR         /*!< FLASH Write protected error flag          */
116 #define FLASH_FLAG_PGAERR              FLASH_SR_PGAERR         /*!< FLASH Programming Alignment error flag    */
117 #define FLASH_FLAG_PGPERR              FLASH_SR_PGPERR         /*!< FLASH Programming Parallelism error flag  */
118 #define FLASH_FLAG_PGSERR              FLASH_SR_PGSERR         /*!< FLASH Programming Sequence error flag     */
119 #define FLASH_FLAG_BSY                 FLASH_SR_BSY            /*!< FLASH Busy flag                           */
120 /**
121   * @}
122   */
123 
124 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
125   * @brief FLASH Interrupt definition
126   * @{
127   */
128 #define FLASH_IT_EOP                   FLASH_CR_EOPIE           /*!< End of FLASH Operation Interrupt source */
129 #define FLASH_IT_ERR                   0x02000000U  /*!< Error Interrupt source                  */
130 /**
131   * @}
132   */
133 
134 /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
135   * @{
136   */
137 #define FLASH_PSIZE_BYTE           0x00000000U
138 #define FLASH_PSIZE_HALF_WORD      0x00000100U
139 #define FLASH_PSIZE_WORD           0x00000200U
140 #define FLASH_PSIZE_DOUBLE_WORD    0x00000300U
141 #define CR_PSIZE_MASK              0xFFFFFCFFU
142 /**
143   * @}
144   */
145 
146 /** @defgroup FLASH_Keys FLASH Keys
147   * @{
148   */
149 #define RDP_KEY                  ((uint16_t)0x00A5)
150 #define FLASH_KEY1               0x45670123U
151 #define FLASH_KEY2               0xCDEF89ABU
152 #define FLASH_OPT_KEY1           0x08192A3BU
153 #define FLASH_OPT_KEY2           0x4C5D6E7FU
154 /**
155   * @}
156   */
157 
158 /**
159   * @}
160   */
161 
162 /* Exported macro ------------------------------------------------------------*/
163 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
164   * @{
165   */
166 /**
167   * @brief  Set the FLASH Latency.
168   * @param  __LATENCY__ FLASH Latency
169   *         The value of this parameter depend on device used within the same series
170   * @retval none
171   */
172 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
173 
174 /**
175   * @brief  Get the FLASH Latency.
176   * @retval FLASH Latency
177   *          The value of this parameter depend on device used within the same series
178   */
179 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
180 
181 /**
182   * @brief  Enable the FLASH prefetch buffer.
183   * @retval none
184   */
185 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()  (FLASH->ACR |= FLASH_ACR_PRFTEN)
186 
187 /**
188   * @brief  Disable the FLASH prefetch buffer.
189   * @retval none
190   */
191 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
192 
193 /**
194   * @brief  Enable the FLASH instruction cache.
195   * @retval none
196   */
197 #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_ICEN)
198 
199 /**
200   * @brief  Disable the FLASH instruction cache.
201   * @retval none
202   */
203 #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_ICEN))
204 
205 /**
206   * @brief  Enable the FLASH data cache.
207   * @retval none
208   */
209 #define __HAL_FLASH_DATA_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_DCEN)
210 
211 /**
212   * @brief  Disable the FLASH data cache.
213   * @retval none
214   */
215 #define __HAL_FLASH_DATA_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_DCEN))
216 
217 /**
218   * @brief  Resets the FLASH instruction Cache.
219   * @note   This function must be used only when the Instruction Cache is disabled.
220   * @retval None
221   */
222 #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_ICRST;  \
223                                                   FLASH->ACR &= ~FLASH_ACR_ICRST; \
224                                                  }while(0U)
225 
226 /**
227   * @brief  Resets the FLASH data Cache.
228   * @note   This function must be used only when the data Cache is disabled.
229   * @retval None
230   */
231 #define __HAL_FLASH_DATA_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_DCRST;  \
232                                            FLASH->ACR &= ~FLASH_ACR_DCRST; \
233                                           }while(0U)
234 /**
235   * @brief  Enable the specified FLASH interrupt.
236   * @param  __INTERRUPT__ FLASH interrupt
237   *         This parameter can be any combination of the following values:
238   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
239   *     @arg FLASH_IT_ERR: Error Interrupt
240   * @retval none
241   */
242 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  (FLASH->CR |= (__INTERRUPT__))
243 
244 /**
245   * @brief  Disable the specified FLASH interrupt.
246   * @param  __INTERRUPT__ FLASH interrupt
247   *         This parameter can be any combination of the following values:
248   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
249   *     @arg FLASH_IT_ERR: Error Interrupt
250   * @retval none
251   */
252 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
253 
254 /**
255   * @brief  Get the specified FLASH flag status.
256   * @param  __FLAG__ specifies the FLASH flags to check.
257   *          This parameter can be any combination of the following values:
258   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag
259   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag
260   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
261   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
262   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
263   *            @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
264   *            @arg FLASH_FLAG_BSY   : FLASH Busy flag
265   * @retval The new state of __FLAG__ (SET or RESET).
266   */
267 #define __HAL_FLASH_GET_FLAG(__FLAG__)   ((FLASH->SR & (__FLAG__)))
268 
269 /**
270   * @brief  Clear the specified FLASH flags.
271   * @param  __FLAG__ specifies the FLASH flags to clear.
272   *          This parameter can be any combination 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_PGSERR: FLASH Programming Sequence error flag
279   * @retval none
280   */
281 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   (FLASH->SR = (__FLAG__))
282 /**
283   * @}
284   */
285 
286 /* Include FLASH HAL Extension module */
287 #include "stm32f2xx_hal_flash_ex.h"
288 
289 /* Exported functions --------------------------------------------------------*/
290 /** @addtogroup FLASH_Exported_Functions
291   * @{
292   */
293 /** @addtogroup FLASH_Exported_Functions_Group1
294   * @{
295   */
296 /* Program operation functions  ***********************************************/
297 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
298 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
299 /* FLASH IRQ handler method */
300 void HAL_FLASH_IRQHandler(void);
301 /* Callbacks in non blocking modes */
302 void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
303 void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
304 /**
305   * @}
306   */
307 
308 /** @addtogroup FLASH_Exported_Functions_Group2
309   * @{
310   */
311 /* Peripheral Control functions  **********************************************/
312 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
313 HAL_StatusTypeDef HAL_FLASH_Lock(void);
314 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
315 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
316 /* Option bytes control */
317 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
318 /**
319   * @}
320   */
321 
322 /** @addtogroup FLASH_Exported_Functions_Group3
323   * @{
324   */
325 /* Peripheral State functions  ************************************************/
326 uint32_t HAL_FLASH_GetError(void);
327 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
328 /**
329   * @}
330   */
331 
332 /**
333   * @}
334   */
335 /* Private types -------------------------------------------------------------*/
336 /* Private variables ---------------------------------------------------------*/
337 /** @defgroup FLASH_Private_Variables FLASH Private Variables
338   * @{
339   */
340 
341 /**
342   * @}
343   */
344 /* Private constants ---------------------------------------------------------*/
345 /** @defgroup FLASH_Private_Constants FLASH Private Constants
346   * @{
347   */
348 
349 /**
350   * @brief   ACR register byte 0 (Bits[7:0]) base address
351   */
352 #define ACR_BYTE0_ADDRESS           0x40023C00U
353 /**
354   * @brief   OPTCR register byte 0 (Bits[7:0]) base address
355   */
356 #define OPTCR_BYTE0_ADDRESS         0x40023C14U
357 /**
358   * @brief   OPTCR register byte 1 (Bits[15:8]) base address
359   */
360 #define OPTCR_BYTE1_ADDRESS         0x40023C15U
361 /**
362   * @brief   OPTCR register byte 2 (Bits[23:16]) base address
363   */
364 #define OPTCR_BYTE2_ADDRESS         0x40023C16U
365 /**
366   * @brief   OPTCR register byte 3 (Bits[31:24]) base address
367   */
368 #define OPTCR_BYTE3_ADDRESS         0x40023C17U
369 
370 /**
371   * @}
372   */
373 
374 /* Private macros ------------------------------------------------------------*/
375 /** @defgroup FLASH_Private_Macros FLASH Private Macros
376   * @{
377   */
378 
379 /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
380   * @{
381   */
382 #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
383                                     ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
384                                     ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
385                                     ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))
386 /**
387   * @}
388   */
389 
390 /**
391   * @}
392   */
393 
394 /* Private functions ---------------------------------------------------------*/
395 /** @defgroup FLASH_Private_Functions FLASH Private Functions
396   * @{
397   */
398 
399 /**
400   * @}
401   */
402 
403 /**
404   * @}
405   */
406 
407 /**
408   * @}
409   */
410 
411 #ifdef __cplusplus
412 }
413 #endif
414 
415 #endif /* __STM32F2xx_HAL_FLASH_H */
416 
417