STM32F439xx HAL User Manual
stm32f4xx_hal_flash.h
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_flash.h
00004   * @author  MCD Application Team
00005   * @brief   Header file of FLASH HAL module.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */ 
00035 
00036 /* Define to prevent recursive inclusion -------------------------------------*/
00037 #ifndef __STM32F4xx_HAL_FLASH_H
00038 #define __STM32F4xx_HAL_FLASH_H
00039 
00040 #ifdef __cplusplus
00041  extern "C" {
00042 #endif
00043 
00044 /* Includes ------------------------------------------------------------------*/
00045 #include "stm32f4xx_hal_def.h"
00046 
00047 /** @addtogroup STM32F4xx_HAL_Driver
00048   * @{
00049   */
00050 
00051 /** @addtogroup FLASH
00052   * @{
00053   */ 
00054 
00055 /* Exported types ------------------------------------------------------------*/
00056 /** @defgroup FLASH_Exported_Types FLASH Exported Types
00057   * @{
00058   */
00059  
00060 /**
00061   * @brief  FLASH Procedure structure definition
00062   */
00063 typedef enum 
00064 {
00065   FLASH_PROC_NONE = 0U, 
00066   FLASH_PROC_SECTERASE,
00067   FLASH_PROC_MASSERASE,
00068   FLASH_PROC_PROGRAM
00069 } FLASH_ProcedureTypeDef;
00070 
00071 /** 
00072   * @brief  FLASH handle Structure definition  
00073   */
00074 typedef struct
00075 {
00076   __IO FLASH_ProcedureTypeDef ProcedureOnGoing;   /*Internal variable to indicate which procedure is ongoing or not in IT context*/
00077   
00078   __IO uint32_t               NbSectorsToErase;   /*Internal variable to save the remaining sectors to erase in IT context*/
00079   
00080   __IO uint8_t                VoltageForErase;    /*Internal variable to provide voltage range selected by user in IT context*/
00081   
00082   __IO uint32_t               Sector;             /*Internal variable to define the current sector which is erasing*/
00083   
00084   __IO uint32_t               Bank;               /*Internal variable to save current bank selected during mass erase*/
00085   
00086   __IO uint32_t               Address;            /*Internal variable to save address selected for program*/
00087   
00088   HAL_LockTypeDef             Lock;               /* FLASH locking object                */
00089 
00090   __IO uint32_t               ErrorCode;          /* FLASH error code                    */
00091 
00092 }FLASH_ProcessTypeDef;
00093 
00094 /**
00095   * @}
00096   */
00097 
00098 /* Exported constants --------------------------------------------------------*/
00099 /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
00100   * @{
00101   */  
00102 /** @defgroup FLASH_Error_Code FLASH Error Code
00103   * @brief    FLASH Error Code 
00104   * @{
00105   */ 
00106 #define HAL_FLASH_ERROR_NONE         0x00000000U    /*!< No error                      */
00107 #define HAL_FLASH_ERROR_RD           0x00000001U    /*!< Read Protection error         */
00108 #define HAL_FLASH_ERROR_PGS          0x00000002U    /*!< Programming Sequence error    */
00109 #define HAL_FLASH_ERROR_PGP          0x00000004U    /*!< Programming Parallelism error */
00110 #define HAL_FLASH_ERROR_PGA          0x00000008U    /*!< Programming Alignment error   */
00111 #define HAL_FLASH_ERROR_WRP          0x00000010U    /*!< Write protection error        */
00112 #define HAL_FLASH_ERROR_OPERATION    0x00000020U    /*!< Operation Error               */
00113 /**
00114   * @}
00115   */
00116   
00117 /** @defgroup FLASH_Type_Program FLASH Type Program
00118   * @{
00119   */ 
00120 #define FLASH_TYPEPROGRAM_BYTE        0x00000000U  /*!< Program byte (8-bit) at a specified address           */
00121 #define FLASH_TYPEPROGRAM_HALFWORD    0x00000001U  /*!< Program a half-word (16-bit) at a specified address   */
00122 #define FLASH_TYPEPROGRAM_WORD        0x00000002U  /*!< Program a word (32-bit) at a specified address        */
00123 #define FLASH_TYPEPROGRAM_DOUBLEWORD  0x00000003U  /*!< Program a double word (64-bit) at a specified address */
00124 /**
00125   * @}
00126   */
00127 
00128 /** @defgroup FLASH_Flag_definition FLASH Flag definition
00129   * @brief Flag definition
00130   * @{
00131   */ 
00132 #define FLASH_FLAG_EOP                 FLASH_SR_EOP            /*!< FLASH End of Operation flag               */
00133 #define FLASH_FLAG_OPERR               FLASH_SR_SOP            /*!< FLASH operation Error flag                */
00134 #define FLASH_FLAG_WRPERR              FLASH_SR_WRPERR         /*!< FLASH Write protected error flag          */
00135 #define FLASH_FLAG_PGAERR              FLASH_SR_PGAERR         /*!< FLASH Programming Alignment error flag    */
00136 #define FLASH_FLAG_PGPERR              FLASH_SR_PGPERR         /*!< FLASH Programming Parallelism error flag  */
00137 #define FLASH_FLAG_PGSERR              FLASH_SR_PGSERR         /*!< FLASH Programming Sequence error flag     */
00138 #if defined(FLASH_SR_RDERR)
00139 #define FLASH_FLAG_RDERR               FLASH_SR_RDERR          /*!< Read Protection error flag (PCROP)        */
00140 #endif /* FLASH_SR_RDERR */
00141 #define FLASH_FLAG_BSY                 FLASH_SR_BSY            /*!< FLASH Busy flag                           */ 
00142 /**
00143   * @}
00144   */
00145   
00146 /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
00147   * @brief FLASH Interrupt definition
00148   * @{
00149   */ 
00150 #define FLASH_IT_EOP                   FLASH_CR_EOPIE          /*!< End of FLASH Operation Interrupt source */
00151 #define FLASH_IT_ERR                   0x02000000U             /*!< Error Interrupt source                  */
00152 /**
00153   * @}
00154   */  
00155 
00156 /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
00157   * @{
00158   */
00159 #define FLASH_PSIZE_BYTE           0x00000000U
00160 #define FLASH_PSIZE_HALF_WORD      0x00000100U
00161 #define FLASH_PSIZE_WORD           0x00000200U
00162 #define FLASH_PSIZE_DOUBLE_WORD    0x00000300U
00163 #define CR_PSIZE_MASK              0xFFFFFCFFU
00164 /**
00165   * @}
00166   */ 
00167 
00168 /** @defgroup FLASH_Keys FLASH Keys
00169   * @{
00170   */ 
00171 #define RDP_KEY                  ((uint16_t)0x00A5)
00172 #define FLASH_KEY1               0x45670123U
00173 #define FLASH_KEY2               0xCDEF89ABU
00174 #define FLASH_OPT_KEY1           0x08192A3BU
00175 #define FLASH_OPT_KEY2           0x4C5D6E7FU
00176 /**
00177   * @}
00178   */ 
00179 
00180 /**
00181   * @}
00182   */ 
00183   
00184 /* Exported macro ------------------------------------------------------------*/
00185 /** @defgroup FLASH_Exported_Macros FLASH Exported Macros
00186   * @{
00187   */
00188 /**
00189   * @brief  Set the FLASH Latency.
00190   * @param  __LATENCY__ FLASH Latency
00191   *         The value of this parameter depend on device used within the same series
00192   * @retval none
00193   */ 
00194 #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
00195 
00196 /**
00197   * @brief  Get the FLASH Latency.
00198   * @retval FLASH Latency
00199   *          The value of this parameter depend on device used within the same series
00200   */ 
00201 #define __HAL_FLASH_GET_LATENCY()     (READ_BIT((FLASH->ACR), FLASH_ACR_LATENCY))
00202 
00203 /**
00204   * @brief  Enable the FLASH prefetch buffer.
00205   * @retval none
00206   */ 
00207 #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE()  (FLASH->ACR |= FLASH_ACR_PRFTEN)
00208 
00209 /**
00210   * @brief  Disable the FLASH prefetch buffer.
00211   * @retval none
00212   */ 
00213 #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
00214 
00215 /**
00216   * @brief  Enable the FLASH instruction cache.
00217   * @retval none
00218   */ 
00219 #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_ICEN)
00220 
00221 /**
00222   * @brief  Disable the FLASH instruction cache.
00223   * @retval none
00224   */ 
00225 #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_ICEN))
00226 
00227 /**
00228   * @brief  Enable the FLASH data cache.
00229   * @retval none
00230   */ 
00231 #define __HAL_FLASH_DATA_CACHE_ENABLE()  (FLASH->ACR |= FLASH_ACR_DCEN)
00232 
00233 /**
00234   * @brief  Disable the FLASH data cache.
00235   * @retval none
00236   */ 
00237 #define __HAL_FLASH_DATA_CACHE_DISABLE()   (FLASH->ACR &= (~FLASH_ACR_DCEN))
00238 
00239 /**
00240   * @brief  Resets the FLASH instruction Cache.
00241   * @note   This function must be used only when the Instruction Cache is disabled.  
00242   * @retval None
00243   */
00244 #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_ICRST;  \
00245                                                   FLASH->ACR &= ~FLASH_ACR_ICRST; \
00246                                                  }while(0U)
00247 
00248 /**
00249   * @brief  Resets the FLASH data Cache.
00250   * @note   This function must be used only when the data Cache is disabled.  
00251   * @retval None
00252   */
00253 #define __HAL_FLASH_DATA_CACHE_RESET() do {FLASH->ACR |= FLASH_ACR_DCRST;  \
00254                                            FLASH->ACR &= ~FLASH_ACR_DCRST; \
00255                                           }while(0U)
00256 /**
00257   * @brief  Enable the specified FLASH interrupt.
00258   * @param  __INTERRUPT__  FLASH interrupt 
00259   *         This parameter can be any combination of the following values:
00260   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
00261   *     @arg FLASH_IT_ERR: Error Interrupt    
00262   * @retval none
00263   */  
00264 #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__)  (FLASH->CR |= (__INTERRUPT__))
00265 
00266 /**
00267   * @brief  Disable the specified FLASH interrupt.
00268   * @param  __INTERRUPT__  FLASH interrupt 
00269   *         This parameter can be any combination of the following values:
00270   *     @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
00271   *     @arg FLASH_IT_ERR: Error Interrupt    
00272   * @retval none
00273   */  
00274 #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__)  (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
00275 
00276 /**
00277   * @brief  Get the specified FLASH flag status. 
00278   * @param  __FLAG__ specifies the FLASH flags to check.
00279   *          This parameter can be any combination of the following values:
00280   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag 
00281   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag 
00282   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag 
00283   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
00284   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
00285   *            @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
00286   *            @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
00287   *            @arg FLASH_FLAG_BSY   : FLASH Busy flag
00288   *           (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices                             
00289   * @retval The new state of __FLAG__ (SET or RESET).
00290   */
00291 #define __HAL_FLASH_GET_FLAG(__FLAG__)   ((FLASH->SR & (__FLAG__)))
00292 
00293 /**
00294   * @brief  Clear the specified FLASH flags.
00295   * @param  __FLAG__ specifies the FLASH flags to clear.
00296   *          This parameter can be any combination of the following values:
00297   *            @arg FLASH_FLAG_EOP   : FLASH End of Operation flag 
00298   *            @arg FLASH_FLAG_OPERR : FLASH operation Error flag 
00299   *            @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag 
00300   *            @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag 
00301   *            @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
00302   *            @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
00303   *            @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP) (*)
00304   *           (*) FLASH_FLAG_RDERR is not available for STM32F405xx/407xx/415xx/417xx devices   
00305   * @retval none
00306   */
00307 #define __HAL_FLASH_CLEAR_FLAG(__FLAG__)   (FLASH->SR = (__FLAG__))
00308 /**
00309   * @}
00310   */
00311 
00312 /* Include FLASH HAL Extension module */
00313 #include "stm32f4xx_hal_flash_ex.h"
00314 #include "stm32f4xx_hal_flash_ramfunc.h"
00315 
00316 /* Exported functions --------------------------------------------------------*/
00317 /** @addtogroup FLASH_Exported_Functions
00318   * @{
00319   */
00320 /** @addtogroup FLASH_Exported_Functions_Group1
00321   * @{
00322   */
00323 /* Program operation functions  ***********************************************/
00324 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
00325 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
00326 /* FLASH IRQ handler method */
00327 void HAL_FLASH_IRQHandler(void);
00328 /* Callbacks in non blocking modes */ 
00329 void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
00330 void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
00331 /**
00332   * @}
00333   */
00334 
00335 /** @addtogroup FLASH_Exported_Functions_Group2
00336   * @{
00337   */
00338 /* Peripheral Control functions  **********************************************/
00339 HAL_StatusTypeDef HAL_FLASH_Unlock(void);
00340 HAL_StatusTypeDef HAL_FLASH_Lock(void);
00341 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
00342 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
00343 /* Option bytes control */
00344 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
00345 /**
00346   * @}
00347   */
00348 
00349 /** @addtogroup FLASH_Exported_Functions_Group3
00350   * @{
00351   */
00352 /* Peripheral State functions  ************************************************/
00353 uint32_t HAL_FLASH_GetError(void);
00354 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
00355 /**
00356   * @}
00357   */
00358 
00359 /**
00360   * @}
00361   */ 
00362 /* Private types -------------------------------------------------------------*/
00363 /* Private variables ---------------------------------------------------------*/
00364 /** @defgroup FLASH_Private_Variables FLASH Private Variables
00365   * @{
00366   */
00367 
00368 /**
00369   * @}
00370   */
00371 /* Private constants ---------------------------------------------------------*/
00372 /** @defgroup FLASH_Private_Constants FLASH Private Constants
00373   * @{
00374   */
00375 
00376 /** 
00377   * @brief   ACR register byte 0 (Bits[7:0]) base address  
00378   */ 
00379 #define ACR_BYTE0_ADDRESS           0x40023C00U 
00380 /** 
00381   * @brief   OPTCR register byte 0 (Bits[7:0]) base address  
00382   */ 
00383 #define OPTCR_BYTE0_ADDRESS         0x40023C14U
00384 /** 
00385   * @brief   OPTCR register byte 1 (Bits[15:8]) base address  
00386   */ 
00387 #define OPTCR_BYTE1_ADDRESS         0x40023C15U
00388 /** 
00389   * @brief   OPTCR register byte 2 (Bits[23:16]) base address  
00390   */ 
00391 #define OPTCR_BYTE2_ADDRESS         0x40023C16U
00392 /** 
00393   * @brief   OPTCR register byte 3 (Bits[31:24]) base address  
00394   */ 
00395 #define OPTCR_BYTE3_ADDRESS         0x40023C17U
00396 
00397 /**
00398   * @}
00399   */
00400 
00401 /* Private macros ------------------------------------------------------------*/
00402 /** @defgroup FLASH_Private_Macros FLASH Private Macros
00403   * @{
00404   */
00405 
00406 /** @defgroup FLASH_IS_FLASH_Definitions FLASH Private macros to check input parameters
00407   * @{
00408   */
00409 #define IS_FLASH_TYPEPROGRAM(VALUE)(((VALUE) == FLASH_TYPEPROGRAM_BYTE) || \
00410                                     ((VALUE) == FLASH_TYPEPROGRAM_HALFWORD) || \
00411                                     ((VALUE) == FLASH_TYPEPROGRAM_WORD) || \
00412                                     ((VALUE) == FLASH_TYPEPROGRAM_DOUBLEWORD))  
00413 /**
00414   * @}
00415   */
00416 
00417 /**
00418   * @}
00419   */
00420 
00421 /* Private functions ---------------------------------------------------------*/
00422 /** @defgroup FLASH_Private_Functions FLASH Private Functions
00423   * @{
00424   */
00425 
00426 /**
00427   * @}
00428   */
00429 
00430 /**
00431   * @}
00432   */ 
00433 
00434 /**
00435   * @}
00436   */
00437 
00438 #ifdef __cplusplus
00439 }
00440 #endif
00441 
00442 #endif /* __STM32F4xx_HAL_FLASH_H */
00443 
00444 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/