STM32F479xx HAL User Manual
stm32f4xx_hal_flash.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_flash.c
00004   * @author  MCD Application Team
00005   * @brief   FLASH HAL module driver.
00006   *          This file provides firmware functions to manage the following 
00007   *          functionalities of the internal FLASH memory:
00008   *           + Program operations functions
00009   *           + Memory Control functions 
00010   *           + Peripheral Errors functions
00011   *         
00012   @verbatim
00013   ==============================================================================
00014                         ##### FLASH peripheral features #####
00015   ==============================================================================
00016            
00017   [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses 
00018        to the Flash memory. It implements the erase and program Flash memory operations 
00019        and the read and write protection mechanisms.
00020       
00021   [..] The Flash memory interface accelerates code execution with a system of instruction
00022        prefetch and cache lines. 
00023 
00024   [..] The FLASH main features are:
00025       (+) Flash memory read operations
00026       (+) Flash memory program/erase operations
00027       (+) Read / write protections
00028       (+) Prefetch on I-Code
00029       (+) 64 cache lines of 128 bits on I-Code
00030       (+) 8 cache lines of 128 bits on D-Code
00031       
00032       
00033                      ##### How to use this driver #####
00034   ==============================================================================
00035     [..]                             
00036       This driver provides functions and macros to configure and program the FLASH 
00037       memory of all STM32F4xx devices.
00038     
00039       (#) FLASH Memory IO Programming functions: 
00040            (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and 
00041                 HAL_FLASH_Lock() functions
00042            (++) Program functions: byte, half word, word and double word
00043            (++) There Two modes of programming :
00044             (+++) Polling mode using HAL_FLASH_Program() function
00045             (+++) Interrupt mode using HAL_FLASH_Program_IT() function
00046     
00047       (#) Interrupts and flags management functions : 
00048            (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
00049            (++) Wait for last FLASH operation according to its status
00050            (++) Get error flag status by calling HAL_SetErrorCode()          
00051 
00052     [..] 
00053       In addition to these functions, this driver includes a set of macros allowing
00054       to handle the following operations:
00055        (+) Set the latency
00056        (+) Enable/Disable the prefetch buffer
00057        (+) Enable/Disable the Instruction cache and the Data cache
00058        (+) Reset the Instruction cache and the Data cache
00059        (+) Enable/Disable the FLASH interrupts
00060        (+) Monitor the FLASH flags status
00061           
00062   @endverbatim
00063   ******************************************************************************
00064   * @attention
00065   *
00066   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
00067   * All rights reserved.</center></h2>
00068   *
00069   * This software component is licensed by ST under BSD 3-Clause license,
00070   * the "License"; You may not use this file except in compliance with the
00071   * License. You may obtain a copy of the License at:
00072   *                        opensource.org/licenses/BSD-3-Clause
00073   *
00074   ******************************************************************************
00075   */ 
00076 
00077 /* Includes ------------------------------------------------------------------*/
00078 #include "stm32f4xx_hal.h"
00079 
00080 /** @addtogroup STM32F4xx_HAL_Driver
00081   * @{
00082   */
00083 
00084 /** @defgroup FLASH FLASH
00085   * @brief FLASH HAL module driver
00086   * @{
00087   */
00088 
00089 #ifdef HAL_FLASH_MODULE_ENABLED
00090 
00091 /* Private typedef -----------------------------------------------------------*/
00092 /* Private define ------------------------------------------------------------*/
00093 /** @addtogroup FLASH_Private_Constants
00094   * @{
00095   */
00096 #define FLASH_TIMEOUT_VALUE       50000U /* 50 s */
00097 /**
00098   * @}
00099   */         
00100 /* Private macro -------------------------------------------------------------*/
00101 /* Private variables ---------------------------------------------------------*/
00102 /** @addtogroup FLASH_Private_Variables
00103   * @{
00104   */
00105 /* Variable used for Erase sectors under interruption */
00106 FLASH_ProcessTypeDef pFlash;
00107 /**
00108   * @}
00109   */
00110 
00111 /* Private function prototypes -----------------------------------------------*/
00112 /** @addtogroup FLASH_Private_Functions
00113   * @{
00114   */
00115 /* Program operations */
00116 static void   FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
00117 static void   FLASH_Program_Word(uint32_t Address, uint32_t Data);
00118 static void   FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
00119 static void   FLASH_Program_Byte(uint32_t Address, uint8_t Data);
00120 static void   FLASH_SetErrorCode(void);
00121 
00122 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
00123 /**
00124   * @}
00125   */
00126 
00127 /* Exported functions --------------------------------------------------------*/
00128 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
00129   * @{
00130   */
00131   
00132 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions 
00133  *  @brief   Programming operation functions 
00134  *
00135 @verbatim   
00136  ===============================================================================
00137                   ##### Programming operation functions #####
00138  ===============================================================================  
00139     [..]
00140     This subsection provides a set of functions allowing to manage the FLASH 
00141     program operations.
00142 
00143 @endverbatim
00144   * @{
00145   */
00146 
00147 /**
00148   * @brief  Program byte, halfword, word or double word at a specified address
00149   * @param  TypeProgram  Indicate the way to program at a specified address.
00150   *                           This parameter can be a value of @ref FLASH_Type_Program
00151   * @param  Address  specifies the address to be programmed.
00152   * @param  Data specifies the data to be programmed
00153   * 
00154   * @retval HAL_StatusTypeDef HAL Status
00155   */
00156 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
00157 {
00158   HAL_StatusTypeDef status = HAL_ERROR;
00159   
00160   /* Process Locked */
00161   __HAL_LOCK(&pFlash);
00162   
00163   /* Check the parameters */
00164   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
00165   
00166   /* Wait for last operation to be completed */
00167   status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
00168   
00169   if(status == HAL_OK)
00170   {
00171     if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
00172     {
00173       /*Program byte (8-bit) at a specified address.*/
00174       FLASH_Program_Byte(Address, (uint8_t) Data);
00175     }
00176     else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
00177     {
00178       /*Program halfword (16-bit) at a specified address.*/
00179       FLASH_Program_HalfWord(Address, (uint16_t) Data);
00180     }
00181     else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
00182     {
00183       /*Program word (32-bit) at a specified address.*/
00184       FLASH_Program_Word(Address, (uint32_t) Data);
00185     }
00186     else
00187     {
00188       /*Program double word (64-bit) at a specified address.*/
00189       FLASH_Program_DoubleWord(Address, Data);
00190     }
00191     
00192     /* Wait for last operation to be completed */
00193     status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
00194     
00195     /* If the program operation is completed, disable the PG Bit */
00196     FLASH->CR &= (~FLASH_CR_PG);  
00197   }
00198   
00199   /* Process Unlocked */
00200   __HAL_UNLOCK(&pFlash);
00201   
00202   return status;
00203 }
00204 
00205 /**
00206   * @brief   Program byte, halfword, word or double word at a specified address  with interrupt enabled.
00207   * @param  TypeProgram  Indicate the way to program at a specified address.
00208   *                           This parameter can be a value of @ref FLASH_Type_Program
00209   * @param  Address  specifies the address to be programmed.
00210   * @param  Data specifies the data to be programmed
00211   * 
00212   * @retval HAL Status
00213   */
00214 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
00215 {
00216   HAL_StatusTypeDef status = HAL_OK;
00217   
00218   /* Process Locked */
00219   __HAL_LOCK(&pFlash);
00220 
00221   /* Check the parameters */
00222   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
00223 
00224   /* Enable End of FLASH Operation interrupt */
00225   __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
00226   
00227   /* Enable Error source interrupt */
00228   __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);
00229 
00230   pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
00231   pFlash.Address = Address;
00232 
00233   if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
00234   {
00235     /*Program byte (8-bit) at a specified address.*/
00236       FLASH_Program_Byte(Address, (uint8_t) Data);
00237   }
00238   else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
00239   {
00240     /*Program halfword (16-bit) at a specified address.*/
00241     FLASH_Program_HalfWord(Address, (uint16_t) Data);
00242   }
00243   else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
00244   {
00245     /*Program word (32-bit) at a specified address.*/
00246     FLASH_Program_Word(Address, (uint32_t) Data);
00247   }
00248   else
00249   {
00250     /*Program double word (64-bit) at a specified address.*/
00251     FLASH_Program_DoubleWord(Address, Data);
00252   }
00253 
00254   return status;
00255 }
00256 
00257 /**
00258   * @brief This function handles FLASH interrupt request.
00259   * @retval None
00260   */
00261 void HAL_FLASH_IRQHandler(void)
00262 {
00263   uint32_t addresstmp = 0U;
00264   
00265   /* Check FLASH operation error flags */
00266 #if defined(FLASH_SR_RDERR) 
00267   if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
00268     FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
00269 #else
00270   if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
00271     FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
00272 #endif /* FLASH_SR_RDERR */
00273   {
00274     if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
00275     {
00276       /*return the faulty sector*/
00277       addresstmp = pFlash.Sector;
00278       pFlash.Sector = 0xFFFFFFFFU;
00279     }
00280     else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
00281     {
00282       /*return the faulty bank*/
00283       addresstmp = pFlash.Bank;
00284     }
00285     else
00286     {
00287       /*return the faulty address*/
00288       addresstmp = pFlash.Address;
00289     }
00290     
00291     /*Save the Error code*/
00292     FLASH_SetErrorCode();
00293     
00294     /* FLASH error interrupt user callback */
00295     HAL_FLASH_OperationErrorCallback(addresstmp);
00296     
00297     /*Stop the procedure ongoing*/
00298     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00299   }
00300   
00301   /* Check FLASH End of Operation flag  */
00302   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
00303   {
00304     /* Clear FLASH End of Operation pending bit */
00305     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00306     
00307     if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE)
00308     {
00309       /*Nb of sector to erased can be decreased*/
00310       pFlash.NbSectorsToErase--;
00311       
00312       /* Check if there are still sectors to erase*/
00313       if(pFlash.NbSectorsToErase != 0U)
00314       {
00315         addresstmp = pFlash.Sector;
00316         /*Indicate user which sector has been erased*/
00317         HAL_FLASH_EndOfOperationCallback(addresstmp);
00318         
00319         /*Increment sector number*/
00320         pFlash.Sector++;
00321         addresstmp = pFlash.Sector;
00322         FLASH_Erase_Sector(addresstmp, pFlash.VoltageForErase);
00323       }
00324       else
00325       {
00326         /*No more sectors to Erase, user callback can be called.*/
00327         /*Reset Sector and stop Erase sectors procedure*/
00328         pFlash.Sector = addresstmp = 0xFFFFFFFFU;
00329         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00330         
00331         /* Flush the caches to be sure of the data consistency */
00332         FLASH_FlushCaches() ;
00333                 
00334         /* FLASH EOP interrupt user callback */
00335         HAL_FLASH_EndOfOperationCallback(addresstmp);
00336       }
00337     }
00338     else 
00339     {
00340       if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) 
00341       {
00342         /* MassErase ended. Return the selected bank */
00343         /* Flush the caches to be sure of the data consistency */
00344         FLASH_FlushCaches() ;
00345 
00346         /* FLASH EOP interrupt user callback */
00347         HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
00348       }
00349       else
00350       {
00351         /*Program ended. Return the selected address*/
00352         /* FLASH EOP interrupt user callback */
00353         HAL_FLASH_EndOfOperationCallback(pFlash.Address);
00354       }
00355       pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00356     }
00357   }
00358   
00359   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
00360   {
00361     /* Operation is completed, disable the PG, SER, SNB and MER Bits */
00362     CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_SER | FLASH_CR_SNB | FLASH_MER_BIT));
00363 
00364     /* Disable End of FLASH Operation interrupt */
00365     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP);
00366     
00367     /* Disable Error source interrupt */
00368     __HAL_FLASH_DISABLE_IT(FLASH_IT_ERR);
00369     
00370     /* Process Unlocked */
00371     __HAL_UNLOCK(&pFlash);
00372   }
00373 }
00374 
00375 /**
00376   * @brief  FLASH end of operation interrupt callback
00377   * @param  ReturnValue The value saved in this parameter depends on the ongoing procedure
00378   *                  Mass Erase: Bank number which has been requested to erase
00379   *                  Sectors Erase: Sector which has been erased 
00380   *                    (if 0xFFFFFFFFU, it means that all the selected sectors have been erased)
00381   *                  Program: Address which was selected for data program
00382   * @retval None
00383   */
00384 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
00385 {
00386   /* Prevent unused argument(s) compilation warning */
00387   UNUSED(ReturnValue);
00388   /* NOTE : This function Should not be modified, when the callback is needed,
00389             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
00390    */ 
00391 }
00392 
00393 /**
00394   * @brief  FLASH operation error interrupt callback
00395   * @param  ReturnValue The value saved in this parameter depends on the ongoing procedure
00396   *                 Mass Erase: Bank number which has been requested to erase
00397   *                 Sectors Erase: Sector number which returned an error
00398   *                 Program: Address which was selected for data program
00399   * @retval None
00400   */
00401 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
00402 {
00403   /* Prevent unused argument(s) compilation warning */
00404   UNUSED(ReturnValue);
00405   /* NOTE : This function Should not be modified, when the callback is needed,
00406             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
00407    */ 
00408 }
00409 
00410 /**
00411   * @}
00412   */
00413 
00414 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions 
00415  *  @brief   management functions 
00416  *
00417 @verbatim   
00418  ===============================================================================
00419                       ##### Peripheral Control functions #####
00420  ===============================================================================  
00421     [..]
00422     This subsection provides a set of functions allowing to control the FLASH 
00423     memory operations.
00424 
00425 @endverbatim
00426   * @{
00427   */
00428 
00429 /**
00430   * @brief  Unlock the FLASH control register access
00431   * @retval HAL Status
00432   */
00433 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
00434 {
00435   HAL_StatusTypeDef status = HAL_OK;
00436 
00437   if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
00438   {
00439     /* Authorize the FLASH Registers access */
00440     WRITE_REG(FLASH->KEYR, FLASH_KEY1);
00441     WRITE_REG(FLASH->KEYR, FLASH_KEY2);
00442 
00443     /* Verify Flash is unlocked */
00444     if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
00445     {
00446       status = HAL_ERROR;
00447     }
00448   }
00449 
00450   return status;
00451 }
00452 
00453 /**
00454   * @brief  Locks the FLASH control register access
00455   * @retval HAL Status
00456   */
00457 HAL_StatusTypeDef HAL_FLASH_Lock(void)
00458 {
00459   /* Set the LOCK Bit to lock the FLASH Registers access */
00460   FLASH->CR |= FLASH_CR_LOCK;
00461   
00462   return HAL_OK;  
00463 }
00464 
00465 /**
00466   * @brief  Unlock the FLASH Option Control Registers access.
00467   * @retval HAL Status
00468   */
00469 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
00470 {
00471   if((FLASH->OPTCR & FLASH_OPTCR_OPTLOCK) != RESET)
00472   {
00473     /* Authorizes the Option Byte register programming */
00474     FLASH->OPTKEYR = FLASH_OPT_KEY1;
00475     FLASH->OPTKEYR = FLASH_OPT_KEY2;
00476   }
00477   else
00478   {
00479     return HAL_ERROR;
00480   }  
00481   
00482   return HAL_OK;  
00483 }
00484 
00485 /**
00486   * @brief  Lock the FLASH Option Control Registers access.
00487   * @retval HAL Status 
00488   */
00489 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
00490 {
00491   /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
00492   FLASH->OPTCR |= FLASH_OPTCR_OPTLOCK;
00493   
00494   return HAL_OK;  
00495 }
00496 
00497 /**
00498   * @brief  Launch the option byte loading.
00499   * @retval HAL Status
00500   */
00501 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
00502 {
00503   /* Set the OPTSTRT bit in OPTCR register */
00504   *(__IO uint8_t *)OPTCR_BYTE0_ADDRESS |= FLASH_OPTCR_OPTSTRT;
00505 
00506   /* Wait for last operation to be completed */
00507   return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)); 
00508 }
00509 
00510 /**
00511   * @}
00512   */
00513 
00514 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions 
00515  *  @brief   Peripheral Errors functions 
00516  *
00517 @verbatim   
00518  ===============================================================================
00519                 ##### Peripheral Errors functions #####
00520  ===============================================================================  
00521     [..]
00522     This subsection permits to get in run-time Errors of the FLASH peripheral.
00523 
00524 @endverbatim
00525   * @{
00526   */
00527 
00528 /**
00529   * @brief  Get the specific FLASH error flag.
00530   * @retval FLASH_ErrorCode: The returned value can be a combination of:
00531   *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
00532   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag 
00533   *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag  
00534   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
00535   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
00536   *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag 
00537   */
00538 uint32_t HAL_FLASH_GetError(void)
00539 { 
00540    return pFlash.ErrorCode;
00541 }  
00542   
00543 /**
00544   * @}
00545   */    
00546 
00547 /**
00548   * @brief  Wait for a FLASH operation to complete.
00549   * @param  Timeout maximum flash operationtimeout
00550   * @retval HAL Status
00551   */
00552 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
00553 { 
00554   uint32_t tickstart = 0U;
00555   
00556   /* Clear Error Code */
00557   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
00558   
00559   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
00560      Even if the FLASH operation fails, the BUSY flag will be reset and an error
00561      flag will be set */
00562   /* Get tick */
00563   tickstart = HAL_GetTick();
00564 
00565   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) 
00566   { 
00567     if(Timeout != HAL_MAX_DELAY)
00568     {
00569       if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
00570       {
00571         return HAL_TIMEOUT;
00572       }
00573     } 
00574   }
00575 
00576   /* Check FLASH End of Operation flag  */
00577   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)
00578   {
00579     /* Clear FLASH End of Operation pending bit */
00580     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00581   }
00582 #if defined(FLASH_SR_RDERR)  
00583   if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
00584                            FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)
00585 #else
00586   if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \
00587                            FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)
00588 #endif /* FLASH_SR_RDERR */
00589   {
00590     /*Save the error code*/
00591     FLASH_SetErrorCode();
00592     return HAL_ERROR;
00593   }
00594 
00595   /* If there is no error flag set */
00596   return HAL_OK;
00597   
00598 }  
00599 
00600 /**
00601   * @brief  Program a double word (64-bit) at a specified address.
00602   * @note   This function must be used when the device voltage range is from
00603   *         2.7V to 3.6V and Vpp in the range 7V to 9V.
00604   *
00605   * @note   If an erase and a program operations are requested simultaneously,    
00606   *         the erase operation is performed before the program one.
00607   *  
00608   * @param  Address specifies the address to be programmed.
00609   * @param  Data specifies the data to be programmed.
00610   * @retval None
00611   */
00612 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
00613 {
00614   /* Check the parameters */
00615   assert_param(IS_FLASH_ADDRESS(Address));
00616   
00617   /* If the previous operation is completed, proceed to program the new data */
00618   CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
00619   FLASH->CR |= FLASH_PSIZE_DOUBLE_WORD;
00620   FLASH->CR |= FLASH_CR_PG;
00621 
00622   /* Program first word */
00623   *(__IO uint32_t*)Address = (uint32_t)Data;
00624 
00625   /* Barrier to ensure programming is performed in 2 steps, in right order
00626     (independently of compiler optimization behavior) */
00627   __ISB();
00628 
00629   /* Program second word */
00630   *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
00631 }
00632 
00633 
00634 /**
00635   * @brief  Program word (32-bit) at a specified address.
00636   * @note   This function must be used when the device voltage range is from
00637   *         2.7V to 3.6V.
00638   *
00639   * @note   If an erase and a program operations are requested simultaneously,    
00640   *         the erase operation is performed before the program one.
00641   *  
00642   * @param  Address specifies the address to be programmed.
00643   * @param  Data specifies the data to be programmed.
00644   * @retval None
00645   */
00646 static void FLASH_Program_Word(uint32_t Address, uint32_t Data)
00647 {
00648   /* Check the parameters */
00649   assert_param(IS_FLASH_ADDRESS(Address));
00650   
00651   /* If the previous operation is completed, proceed to program the new data */
00652   CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
00653   FLASH->CR |= FLASH_PSIZE_WORD;
00654   FLASH->CR |= FLASH_CR_PG;
00655 
00656   *(__IO uint32_t*)Address = Data;
00657 }
00658 
00659 /**
00660   * @brief  Program a half-word (16-bit) at a specified address.
00661   * @note   This function must be used when the device voltage range is from
00662   *         2.1V to 3.6V.
00663   *
00664   * @note   If an erase and a program operations are requested simultaneously,    
00665   *         the erase operation is performed before the program one.
00666   *  
00667   * @param  Address specifies the address to be programmed.
00668   * @param  Data specifies the data to be programmed.
00669   * @retval None
00670   */
00671 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
00672 {
00673   /* Check the parameters */
00674   assert_param(IS_FLASH_ADDRESS(Address));
00675   
00676   /* If the previous operation is completed, proceed to program the new data */
00677   CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
00678   FLASH->CR |= FLASH_PSIZE_HALF_WORD;
00679   FLASH->CR |= FLASH_CR_PG;
00680 
00681   *(__IO uint16_t*)Address = Data;
00682 }
00683 
00684 /**
00685   * @brief  Program byte (8-bit) at a specified address.
00686   * @note   This function must be used when the device voltage range is from
00687   *         1.8V to 3.6V.
00688   *
00689   * @note   If an erase and a program operations are requested simultaneously,    
00690   *         the erase operation is performed before the program one.
00691   *  
00692   * @param  Address specifies the address to be programmed.
00693   * @param  Data specifies the data to be programmed.
00694   * @retval None
00695   */
00696 static void FLASH_Program_Byte(uint32_t Address, uint8_t Data)
00697 {
00698   /* Check the parameters */
00699   assert_param(IS_FLASH_ADDRESS(Address));
00700   
00701   /* If the previous operation is completed, proceed to program the new data */
00702   CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);
00703   FLASH->CR |= FLASH_PSIZE_BYTE;
00704   FLASH->CR |= FLASH_CR_PG;
00705 
00706   *(__IO uint8_t*)Address = Data;
00707 }
00708 
00709 /**
00710   * @brief  Set the specific FLASH error flag.
00711   * @retval None
00712   */
00713 static void FLASH_SetErrorCode(void)
00714 { 
00715   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) != RESET)
00716   {
00717    pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
00718    
00719    /* Clear FLASH write protection error pending bit */
00720    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR);
00721   }
00722   
00723   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR) != RESET)
00724   {
00725    pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
00726    
00727    /* Clear FLASH Programming alignment error pending bit */
00728    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGAERR);
00729   }
00730   
00731   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGPERR) != RESET)
00732   {
00733     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGP;
00734     
00735     /* Clear FLASH Programming parallelism error pending bit */
00736     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR);
00737   }
00738   
00739   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR) != RESET)
00740   {
00741     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
00742     
00743     /* Clear FLASH Programming sequence error pending bit */
00744     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGSERR);
00745   }
00746 #if defined(FLASH_SR_RDERR) 
00747   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR) != RESET)
00748   {
00749     pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
00750     
00751     /* Clear FLASH Proprietary readout protection error pending bit */
00752     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_RDERR);
00753   }
00754 #endif /* FLASH_SR_RDERR */  
00755   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR) != RESET)
00756   {
00757     pFlash.ErrorCode |= HAL_FLASH_ERROR_OPERATION;
00758     
00759     /* Clear FLASH Operation error pending bit */
00760     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR);
00761   }
00762 }
00763 
00764 /**
00765   * @}
00766   */
00767 
00768 #endif /* HAL_FLASH_MODULE_ENABLED */
00769 
00770 /**
00771   * @}
00772   */
00773 
00774 /**
00775   * @}
00776   */
00777 
00778 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/