2 ******************************************************************************
3 * @file stm32f3xx_hal_flash.h
4 * @author MCD Application Team
7 * @brief Header file of Flash HAL module.
8 ******************************************************************************
11 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ******************************************************************************
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F3xx_HAL_FLASH_H
40 #define __STM32F3xx_HAL_FLASH_H
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f3xx_hal_def.h"
49 /** @addtogroup STM32F3xx_HAL_Driver
53 /** @addtogroup FLASH FLASH HAL module driver
57 /* Exported types ------------------------------------------------------------*/
58 /** @defgroup FLASH_Exported_Types FLASH Exported Types
63 * @brief FLASH Error source
67 FLASH_ERROR_PG = 0x01,
68 FLASH_ERROR_WRP = 0x02
72 * @brief FLASH Erase structure definition
76 uint32_t TypeErase; /*!< TypeErase: Mass erase or page erase.
77 This parameter can be a value of @ref FLASH_Type_Erase */
79 uint32_t PageAddress; /*!< PageAdress: Initial FLASH page address to erase when mass erase is disabled
80 This parameter must be a value of @ref FLASHEx_Address */
82 uint32_t NbPages; /*!< NbPages: Number of pagess to be erased.
83 This parameter must be a value between 1 and (max number of pages - value of initial page)*/
85 } FLASH_EraseInitTypeDef;
88 * @brief FLASH Options bytes program structure definition
92 uint32_t OptionType; /*!< OptionType: Option byte to be configured.
93 This parameter can be a value of @ref FLASH_OB_Type */
95 uint32_t WRPState; /*!< WRPState: Write protection activation or deactivation.
96 This parameter can be a value of @ref FLASH_OB_WRP_State */
98 uint32_t WRPPage; /*!< WRPSector: specifies the page(s) to be write protected
99 This parameter can be a value of @ref FLASHEx_OB_Write_Protection */
101 uint8_t RDPLevel; /*!< RDPLevel: Set the read protection level..
102 This parameter can be a value of @ref FLASH_OB_Read_Protection */
104 uint8_t USERConfig; /*!< USERConfig: Program the FLASH User Option Byte:
105 IWDG / STOP / STDBY / BOOT1 / VDDA_ANALOG / SRAM_PARITY / SDADC12_VDD_MONITOR
106 This parameter can be a combination of @ref FLASH_OB_IWatchdog, @ref FLASH_OB_nRST_STOP,
107 @ref FLASH_OB_nRST_STDBY, @ref FLASH_OB_BOOT1, @ref FLASH_OB_VDDA_Analog_Monitoring,
108 @ref FLASH_OB_SRAM_Parity_Enable and @ref FLASH_OB_SDADC12_VDD_MONITOR */
110 uint32_t DATAAddress; /*!< DATAAddress: Address of the option byte DATA to be prgrammed
111 This parameter can be a value of @ref FLASH_OB_Data_Address */
113 uint8_t DATAData; /*!< DATAData: Data to be stored in the option byte DATA
114 This parameter can have any value */
116 } FLASH_OBProgramInitTypeDef;
119 * @brief FLASH Procedure structure definition
124 FLASH_PROC_PAGEERASE = 1,
125 FLASH_PROC_MASSERASE = 2,
126 FLASH_PROC_PROGRAMHALFWORD = 3,
127 FLASH_PROC_PROGRAMWORD = 4,
128 FLASH_PROC_PROGRAMDOUBLEWORD = 5
129 } FLASH_ProcedureTypeDef;
132 * @brief FLASH handle Structure definition
136 __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /* Internal variable to indicate which procedure is ongoing or not in IT context */
138 __IO uint32_t DataRemaining; /* Internal variable to save the remaining pages to erase or half-word to program in IT context */
140 __IO uint32_t Address; /* Internal variable to save address selected for program or erase */
142 __IO uint64_t Data; /* Internal variable to save data to be programmed */
144 HAL_LockTypeDef Lock; /* FLASH locking object */
146 __IO FLASH_ErrorTypeDef ErrorCode; /* FLASH error code */
148 } FLASH_ProcessTypeDef;
154 /* Exported constants --------------------------------------------------------*/
155 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
159 /** @defgroup FLASH_Type_Erase FLASH Type Erase
162 #define TYPEERASE_PAGES ((uint32_t)0x00) /*!<Pages erase only*/
163 #define TYPEERASE_MASSERASE ((uint32_t)0x01) /*!<Flash mass erase activation*/
165 #define IS_TYPEERASE(VALUE) (((VALUE) == TYPEERASE_PAGES) || \
166 ((VALUE) == TYPEERASE_MASSERASE))
171 /** @defgroup FLASH_Type_Program FLASH Type Program
174 #define TYPEPROGRAM_HALFWORD ((uint32_t)0x01) /*!<Program a half-word (16-bit) at a specified address.*/
175 #define TYPEPROGRAM_WORD ((uint32_t)0x02) /*!<Program a word (32-bit) at a specified address.*/
176 #define TYPEPROGRAM_DOUBLEWORD ((uint32_t)0x03) /*!<Program a double word (64-bit) at a specified address*/
178 #define IS_TYPEPROGRAM(VALUE) (((VALUE) == TYPEPROGRAM_HALFWORD) || \
179 ((VALUE) == TYPEPROGRAM_WORD) || \
180 ((VALUE) == TYPEPROGRAM_DOUBLEWORD))
185 /** @defgroup FLASH_OB_WRP_State FLASH WRP State
188 #define WRPSTATE_DISABLE ((uint32_t)0x00) /*!<Disable the write protection of the desired pages*/
189 #define WRPSTATE_ENABLE ((uint32_t)0x01) /*!<Enable the write protection of the desired pagess*/
191 #define IS_WRPSTATE(VALUE) (((VALUE) == WRPSTATE_DISABLE) || \
192 ((VALUE) == WRPSTATE_ENABLE))
197 /** @defgroup FLASH_OB_Type FLASH Option Bytes Type
200 #define OPTIONBYTE_WRP ((uint32_t)0x01) /*!<WRP option byte configuration*/
201 #define OPTIONBYTE_RDP ((uint32_t)0x02) /*!<RDP option byte configuration*/
202 #define OPTIONBYTE_USER ((uint32_t)0x04) /*!<USER option byte configuration*/
203 #define OPTIONBYTE_DATA ((uint32_t)0x08) /*!<DATA option byte configuration*/
205 #define IS_OPTIONBYTE(VALUE) (((VALUE) < (OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER | OPTIONBYTE_DATA)))
210 /** @defgroup FLASH_Latency FLASH Latency
213 #define FLASH_LATENCY_0 ((uint8_t)0x0000) /*!< FLASH Zero Latency cycle */
214 #define FLASH_LATENCY_1 FLASH_ACR_LATENCY_0 /*!< FLASH One Latency cycle */
215 #define FLASH_LATENCY_2 FLASH_ACR_LATENCY_1 /*!< FLASH Two Latency cycles */
217 #define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_LATENCY_0) || \
218 ((LATENCY) == FLASH_LATENCY_1) || \
219 ((LATENCY) == FLASH_LATENCY_2))
224 /** @defgroup FLASH_OB_Data_Address FLASH Option Byte Data Address
227 #define IS_OB_DATA_ADDRESS(ADDRESS) (((ADDRESS) == 0x1FFFF804) || ((ADDRESS) == 0x1FFFF806))
232 /** @defgroup FLASH_OB_Read_Protection FLASH Option Byte Read Protection
235 #define OB_RDP_LEVEL_0 ((uint8_t)0xAA)
236 #define OB_RDP_LEVEL_1 ((uint8_t)0xBB)
237 #define OB_RDP_LEVEL_2 ((uint8_t)0xCC) /*!< Warning: When enabling read protection level 2
238 it's no more possible to go back to level 1 or 0 */
239 #define IS_OB_RDP_LEVEL(LEVEL) (((LEVEL) == OB_RDP_LEVEL_0) ||\
240 ((LEVEL) == OB_RDP_LEVEL_1))/*||\
241 ((LEVEL) == OB_RDP_LEVEL_2))*/
246 /** @defgroup FLASH_OB_IWatchdog FLASH Option Byte IWatchdog
249 #define OB_IWDG_SW ((uint8_t)0x01) /*!< Software IWDG selected */
250 #define OB_IWDG_HW ((uint8_t)0x00) /*!< Hardware IWDG selected */
251 #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
256 /** @defgroup FLASH_OB_nRST_STOP FLASH Option Byte nRST STOP
259 #define OB_STOP_NO_RST ((uint8_t)0x02) /*!< No reset generated when entering in STOP */
260 #define OB_STOP_RST ((uint8_t)0x00) /*!< Reset generated when entering in STOP */
261 #define IS_OB_STOP_SOURCE(SOURCE) (((SOURCE) == OB_STOP_NO_RST) || ((SOURCE) == OB_STOP_RST))
266 /** @defgroup FLASH_OB_nRST_STDBY FLASH Option Byte nRST STDBY
269 #define OB_STDBY_NO_RST ((uint8_t)0x04) /*!< No reset generated when entering in STANDBY */
270 #define OB_STDBY_RST ((uint8_t)0x00) /*!< Reset generated when entering in STANDBY */
271 #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NO_RST) || ((SOURCE) == OB_STDBY_RST))
276 /** @defgroup FLASH_OB_BOOT1 FLASH Option Byte BOOT1
279 #define OB_BOOT1_RESET ((uint8_t)0x00) /*!< BOOT1 Reset */
280 #define OB_BOOT1_SET ((uint8_t)0x10) /*!< BOOT1 Set */
281 #define IS_OB_BOOT1(BOOT1) (((BOOT1) == OB_BOOT1_RESET) || ((BOOT1) == OB_BOOT1_SET))
286 /** @defgroup FLASH_OB_VDDA_Analog_Monitoring FLASH Option Byte VDDA Analog Monitoring
289 #define OB_VDDA_ANALOG_ON ((uint8_t)0x20) /*!< Analog monitoring on VDDA Power source ON */
290 #define OB_VDDA_ANALOG_OFF ((uint8_t)0x00) /*!< Analog monitoring on VDDA Power source OFF */
291 #define IS_OB_VDDA_ANALOG(ANALOG) (((ANALOG) == OB_VDDA_ANALOG_ON) || ((ANALOG) == OB_VDDA_ANALOG_OFF))
296 /** @defgroup FLASH_OB_SRAM_Parity_Enable FLASH Option Byte SRAM Parity Enable
299 #define OB_SRAM_PARITY_SET ((uint8_t)0x00) /*!< SRAM parity enable set */
300 #define OB_SRAM_PARITY_RESET ((uint8_t)0x40) /*!< SRAM parity enable reset */
301 #define IS_OB_SRAM_PARITY(PARITY) (((PARITY) == OB_SRAM_PARITY_SET) || ((PARITY) == OB_SRAM_PARITY_RESET))
306 /** @defgroup FLASH_OB_SDADC12_VDD_MONITOR FLASH Option Byte SDADC12 VDD MONITOR
309 #define OB_SDADC12_VDD_MONITOR_SET ((uint8_t)0x80) /*!< SDADC12_VDD power supply supervisor set */
310 #define OB_SDADC12_VDD_MONITOR_RESET ((uint8_t)0x00) /*!< SDADC12_VDD power supply supervisor reset */
311 #define IS_OB_SDADC12_VDD_MONITOR(MONITOR) (((MONITOR) == OB_SDADC12_VDD_MONITOR_SET) || ((MONITOR) == OB_SDADC12_VDD_MONITOR_RESET))
316 /** @defgroup FLASH_Flag_definition FLASH Flag definition
317 * @brief Flag definition
320 #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
321 #define FLASH_FLAG_PGERR FLASH_SR_PGERR /*!< FLASH Programming error flag */
322 #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
323 #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Operation flag */
325 #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFFFFC3) == 0x00000000) && ((FLAG) != 0x00000000))
326 #define IS_FLASH_GET_FLAG(FLAG) (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_PGERR) || \
327 ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP))
332 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
333 * @brief FLASH Interrupt definition
336 #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
337 #define FLASH_IT_ERR FLASH_CR_ERRIE /*!< Error Interrupt source */
338 #define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFEBFF) == 0x00000000) && ((IT) != 0x00000000))
343 /** @defgroup FLASH_Timeout_definition FLASH Timeout definition
344 * @brief FLASH Timeout definition
347 #define HAL_FLASH_TIMEOUT_VALUE ((uint32_t)50000)/* 50 s */
356 /* Exported macro ------------------------------------------------------------*/
358 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
359 * @brief macros to control FLASH features
364 * @brief Set the FLASH Latency.
365 * @param __LATENCY__: FLASH Latency
366 * The value of this parameter depend on device used within the same series
369 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (FLASH->ACR = (FLASH->ACR&(~FLASH_ACR_LATENCY)) | (__LATENCY__))
372 * @brief Enable the FLASH prefetch buffer.
375 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTBE)
378 * @brief Disable the FLASH prefetch buffer.
381 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTBE))
384 * @brief Enable the FLASH half cycle access.
387 #define __HAL_FLASH_HALF_CYCLE_ACCESS_ENABLE() (FLASH->ACR |= FLASH_ACR_HLFCYA)
390 * @brief Disable the FLASH half cycle access.
393 #define __HAL_FLASH_HALF_CYCLE_ACCESS_DISABLE() (FLASH->ACR &= (~FLASH_ACR_HLFCYA))
395 /** @defgroup FLASH_Interrupt FLASH Interrupt
396 * @brief macros to handle FLASH interrupts
401 * @brief Enable the specified FLASH interrupt.
402 * @param __INTERRUPT__ : FLASH interrupt
403 * This parameter can be any combination of the following values:
404 * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
405 * @arg FLASH_IT_ERR: Error Interrupt
408 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) (FLASH->CR |= (__INTERRUPT__))
411 * @brief Disable the specified FLASH interrupt.
412 * @param __INTERRUPT__ : FLASH interrupt
413 * This parameter can be any combination of the following values:
414 * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
415 * @arg FLASH_IT_ERR: Error Interrupt
418 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
421 * @brief Get the specified FLASH flag status.
422 * @param __FLAG__: specifies the FLASH flag to check.
423 * This parameter can be one of the following values:
424 * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
425 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
426 * @arg FLASH_FLAG_PGERR : FLASH Programming error flag
427 * @arg FLASH_FLAG_BSY : FLASH Busy flag
428 * @retval The new state of __FLAG__ (SET or RESET).
430 #define __HAL_FLASH_GET_FLAG(__FLAG__) ((FLASH->SR & (__FLAG__)) == (__FLAG__))
433 * @brief Clear the specified FLASH flag.
434 * @param __FLAG__: specifies the FLASH flags to clear.
435 * This parameter can be any combination of the following values:
436 * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
437 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
438 * @arg FLASH_FLAG_PGERR : FLASH Programming error flag
441 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) (FLASH->SR = (__FLAG__))
451 /* Include FLASH HAL Extended module */
452 #include "stm32f3xx_hal_flash_ex.h"
454 /* Exported functions --------------------------------------------------------*/
455 /** @addtogroup FLASH_Exported_Functions FLASH Exported Functions
459 /** @addtogroup FLASH_Exported_Functions_Group1 Input and Output operation functions
462 /* IO operation functions *****************************************************/
463 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
464 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
466 /* FLASH IRQ handler method */
467 void HAL_FLASH_IRQHandler(void);
468 /* Callbacks in non blocking modes */
469 void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
470 void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
476 /** @addtogroup FLASH_Exported_Functions_Group2 Peripheral Control functions
479 /* Peripheral Control functions ***********************************************/
480 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
481 HAL_StatusTypeDef HAL_FLASH_Lock(void);
482 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
483 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
484 /* Option bytes control */
485 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
491 /** @addtogroup FLASH_Exported_Functions_Group3 Peripheral State functions
494 /* Peripheral State and Error functions ***************************************/
495 FLASH_ErrorTypeDef HAL_FLASH_GetError(void);
505 /* Exported Private function -------------------------------------------------*/
506 /** @addtogroup FLASH_Exported_Private_Functions FLASH Exported Private Functions
509 /* Erase operations */
510 void FLASH_PageErase(uint32_t PageAddress);
511 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
513 /* Program operations */
530 #endif /* __STM32F3xx_HAL_FLASH_H */
532 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/