STM32L486xx HAL User Manual
stm32l4xx_hal_flash.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_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       (+) Option bytes programming
00029       (+) Prefetch on I-Code
00030       (+) 32 cache lines of 4*64 bits on I-Code
00031       (+) 8 cache lines of 4*64 bits on D-Code
00032       (+) Error code correction (ECC) : Data in flash are 72-bits word
00033           (8 bits added per double word)
00034       
00035       
00036                         ##### How to use this driver #####
00037  ==============================================================================
00038     [..]                             
00039       This driver provides functions and macros to configure and program the FLASH  
00040       memory of all STM32L4xx devices. 
00041    
00042       (#) Flash Memory IO Programming functions: 
00043            (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and 
00044                 HAL_FLASH_Lock() functions
00045            (++) Program functions: double word and fast program (full row programming)
00046            (++) There Two modes of programming :
00047             (+++) Polling mode using HAL_FLASH_Program() function
00048             (+++) Interrupt mode using HAL_FLASH_Program_IT() function
00049     
00050       (#) Interrupts and flags management functions : 
00051            (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
00052            (++) Callback functions are called when the flash operations are finished :
00053                 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
00054                 HAL_FLASH_OperationErrorCallback()
00055            (++) Get error flag status by calling HAL_GetError()    
00056            
00057       (#) Option bytes management functions :
00058            (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
00059                 HAL_FLASH_OB_Lock() functions
00060            (++) Launch the reload of the option bytes using HAL_FLASH_Launch() function.
00061                 In this case, a reset is generated
00062                          
00063     [..] 
00064       In addition to these functions, this driver includes a set of macros allowing
00065       to handle the following operations:
00066        (+) Set the latency
00067        (+) Enable/Disable the prefetch buffer
00068        (+) Enable/Disable the Instruction cache and the Data cache
00069        (+) Reset the Instruction cache and the Data cache
00070        (+) Enable/Disable the Flash power-down during low-power run and sleep modes
00071        (+) Enable/Disable the Flash interrupts
00072        (+) Monitor the Flash flags status
00073     
00074  @endverbatim                      
00075   ******************************************************************************
00076   * @attention
00077   *
00078   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00079   *
00080   * Redistribution and use in source and binary forms, with or without modification,
00081   * are permitted provided that the following conditions are met:
00082   *   1. Redistributions of source code must retain the above copyright notice,
00083   *      this list of conditions and the following disclaimer.
00084   *   2. Redistributions in binary form must reproduce the above copyright notice,
00085   *      this list of conditions and the following disclaimer in the documentation
00086   *      and/or other materials provided with the distribution.
00087   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00088   *      may be used to endorse or promote products derived from this software
00089   *      without specific prior written permission.
00090   *
00091   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00092   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00093   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00094   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00095   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00096   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00097   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00098   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00099   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00100   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00101   *
00102   ******************************************************************************
00103   */
00104 
00105 /* Includes ------------------------------------------------------------------*/
00106 #include "stm32l4xx_hal.h"
00107 
00108 /** @addtogroup STM32L4xx_HAL_Driver
00109   * @{
00110   */
00111 
00112 /** @defgroup FLASH FLASH
00113   * @brief FLASH HAL module driver
00114   * @{
00115   */
00116 
00117 #ifdef HAL_FLASH_MODULE_ENABLED
00118 
00119 /* Private typedef -----------------------------------------------------------*/
00120 /* Private defines -----------------------------------------------------------*/
00121 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
00122 #define FLASH_NB_DOUBLE_WORDS_IN_ROW  64 
00123 #else
00124 #define FLASH_NB_DOUBLE_WORDS_IN_ROW  32 
00125 #endif
00126 /* Private macros ------------------------------------------------------------*/
00127 /* Private variables ---------------------------------------------------------*/
00128 /** @defgroup FLASH_Private_Variables FLASH Private Variables
00129  * @{
00130  */
00131 /**
00132   * @brief  Variable used for Program/Erase sectors under interruption 
00133   */
00134 extern FLASH_ProcessTypeDef pFlash;
00135 FLASH_ProcessTypeDef pFlash = {NULL};
00136 /**
00137   * @}
00138   */
00139 
00140 /* Private function prototypes -----------------------------------------------*/
00141 /** @defgroup FLASH_Private_Functions FLASH Private Functions
00142  * @{
00143  */
00144 HAL_StatusTypeDef    FLASH_WaitForLastOperation(uint32_t Timeout);
00145 extern void          FLASH_PageErase(uint32_t Page, uint32_t Banks);
00146 extern void          FLASH_FlushCaches(void);
00147 static void          FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data);
00148 static void          FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress);
00149 /**
00150   * @}
00151   */
00152 
00153 /* Exported functions --------------------------------------------------------*/
00154 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
00155   * @{
00156   */
00157 
00158 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions 
00159  *  @brief   Programming operation functions 
00160  *
00161 @verbatim   
00162  ===============================================================================
00163                   ##### Programming operation functions #####
00164  ===============================================================================
00165     [..]
00166     This subsection provides a set of functions allowing to manage the FLASH 
00167     program operations.
00168  
00169 @endverbatim
00170   * @{
00171   */
00172  
00173 /**
00174   * @brief  Program double word or fast program of a row at a specified address.
00175   * @param  TypeProgram:  Indicate the way to program at a specified address.
00176   *                           This parameter can be a value of @ref FLASH_Type_Program
00177   * @param  Address:  specifies the address to be programmed.
00178   * @param  Data: specifies the data to be programmed
00179   *                This parameter is the data for the double word program and the address where 
00180   *                are stored the data for the row fast program
00181   * 
00182   * @retval HAL_StatusTypeDef HAL Status
00183   */
00184 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
00185 {
00186   HAL_StatusTypeDef status;
00187   uint32_t prog_bit = 0;
00188   
00189   /* Process Locked */
00190   __HAL_LOCK(&pFlash);
00191 
00192   /* Check the parameters */
00193   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
00194 
00195   /* Wait for last operation to be completed */
00196   status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
00197   
00198   if(status == HAL_OK)
00199   {
00200     pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
00201 
00202     /* Deactivate the data cache if they are activated to avoid data misbehavior */
00203     if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
00204     {
00205       /* Disable data cache  */
00206       __HAL_FLASH_DATA_CACHE_DISABLE();
00207       pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
00208     }
00209     else
00210     {
00211       pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
00212     }
00213 
00214     if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
00215     {
00216       /* Program double-word (64-bit) at a specified address */
00217       FLASH_Program_DoubleWord(Address, Data);
00218       prog_bit = FLASH_CR_PG;
00219     }
00220     else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
00221     {
00222       /* Fast program a 32 row double-word (64-bit) at a specified address */
00223       FLASH_Program_Fast(Address, (uint32_t)Data);
00224       
00225       /* If it is the last row, the bit will be cleared at the end of the operation */
00226       if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
00227       {
00228         prog_bit = FLASH_CR_FSTPG;
00229       }
00230     }
00231     else
00232     {
00233       /* Nothing to do */
00234     }
00235 
00236     /* Wait for last operation to be completed */
00237     status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
00238     
00239     /* If the program operation is completed, disable the PG or FSTPG Bit */
00240     if (prog_bit != 0U)
00241     {
00242       CLEAR_BIT(FLASH->CR, prog_bit);
00243     }
00244 
00245     /* Flush the caches to be sure of the data consistency */
00246     FLASH_FlushCaches();
00247   }
00248 
00249   /* Process Unlocked */
00250   __HAL_UNLOCK(&pFlash);
00251 
00252   return status;  
00253 }
00254 
00255 /**
00256   * @brief  Program double word or fast program of a row at a specified address with interrupt enabled.
00257   * @param  TypeProgram:  Indicate the way to program at a specified address.
00258   *                           This parameter can be a value of @ref FLASH_Type_Program
00259   * @param  Address:  specifies the address to be programmed.
00260   * @param  Data: specifies the data to be programmed
00261   *                This parameter is the data for the double word program and the address where 
00262   *                are stored the data for the row fast program
00263   * 
00264   * @retval HAL Status
00265   */
00266 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
00267 {
00268   HAL_StatusTypeDef status = HAL_OK;
00269 
00270   /* Check the parameters */
00271   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
00272 
00273   /* Process Locked */
00274   __HAL_LOCK(&pFlash);
00275 
00276   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
00277 
00278   /* Deactivate the data cache if they are activated to avoid data misbehavior */
00279   if(READ_BIT(FLASH->ACR, FLASH_ACR_DCEN) != 0U)
00280   {
00281     /* Disable data cache  */
00282     __HAL_FLASH_DATA_CACHE_DISABLE();
00283     pFlash.CacheToReactivate = FLASH_CACHE_DCACHE_ENABLED;
00284   }
00285   else
00286   {
00287     pFlash.CacheToReactivate = FLASH_CACHE_DISABLED;
00288   }
00289 
00290   /* Set internal variables used by the IRQ handler */
00291   if(TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST)
00292   {
00293     pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_LAST;
00294   }
00295   else
00296   {
00297     pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
00298   }
00299   pFlash.Address = Address;
00300     
00301   /* Enable End of Operation and Error interrupts */
00302   __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
00303 
00304   if(TypeProgram == FLASH_TYPEPROGRAM_DOUBLEWORD)
00305   {
00306     /* Program double-word (64-bit) at a specified address */
00307     FLASH_Program_DoubleWord(Address, Data);
00308   }
00309   else if((TypeProgram == FLASH_TYPEPROGRAM_FAST) || (TypeProgram == FLASH_TYPEPROGRAM_FAST_AND_LAST))
00310   {
00311     /* Fast program a 32 row double-word (64-bit) at a specified address */
00312     FLASH_Program_Fast(Address, (uint32_t)Data);
00313   }
00314   else
00315   {
00316     /* Nothing to do */
00317   }
00318 
00319   return status;  
00320 }
00321 
00322 /**
00323   * @brief Handle FLASH interrupt request.
00324   * @retval None
00325   */
00326 void HAL_FLASH_IRQHandler(void)
00327 {
00328   uint32_t tmp_page;
00329   uint32_t error;
00330   FLASH_ProcedureTypeDef procedure;
00331 
00332   /* If the operation is completed, disable the PG, PNB, MER1, MER2 and PER Bit */
00333   CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_PER | FLASH_CR_PNB));
00334 #if defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || \
00335     defined (STM32L496xx) || defined (STM32L4A6xx) || \
00336     defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined (STM32L4S9xx)
00337   CLEAR_BIT(FLASH->CR, FLASH_CR_MER2);
00338 #endif
00339   
00340   /* Disable the FSTPG Bit only if it is the last row programmed */
00341   if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
00342   {
00343     CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
00344   }
00345 
00346   /* Check FLASH operation error flags */
00347   error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
00348   error |= (FLASH->ECCR & FLASH_FLAG_ECCC);
00349 
00350   if (error !=0U) 
00351   {
00352     /*Save the error code*/
00353     pFlash.ErrorCode |= error;
00354     
00355     /* Clear error programming flags */
00356     __HAL_FLASH_CLEAR_FLAG(error);
00357 
00358     /* Flush the caches to be sure of the data consistency */
00359     FLASH_FlushCaches() ;
00360 
00361     /* FLASH error interrupt user callback */
00362     procedure = pFlash.ProcedureOnGoing;
00363     if(procedure == FLASH_PROC_PAGE_ERASE)
00364     {
00365        HAL_FLASH_OperationErrorCallback(pFlash.Page);
00366     }
00367     else if(procedure == FLASH_PROC_MASS_ERASE)
00368     {
00369         HAL_FLASH_OperationErrorCallback(pFlash.Bank);
00370     }
00371     else if((procedure == FLASH_PROC_PROGRAM) || 
00372             (procedure == FLASH_PROC_PROGRAM_LAST))
00373     {
00374        HAL_FLASH_OperationErrorCallback(pFlash.Address);
00375     }
00376     else
00377     {
00378       /* Nothing to do */
00379     }
00380 
00381     /*Stop the procedure ongoing*/
00382     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00383   }
00384 
00385   /* Check FLASH End of Operation flag  */
00386   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != 0U)
00387   {
00388     /* Clear FLASH End of Operation pending bit */
00389     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00390 
00391     if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
00392     {
00393       /* Nb of pages to erased can be decreased */
00394       pFlash.NbPagesToErase--;
00395 
00396       /* Check if there are still pages to erase*/
00397       if(pFlash.NbPagesToErase != 0U)
00398       {
00399         /* Indicate user which page has been erased*/
00400         HAL_FLASH_EndOfOperationCallback(pFlash.Page);
00401 
00402         /* Increment page number */
00403         pFlash.Page++;
00404         tmp_page = pFlash.Page;
00405         FLASH_PageErase(tmp_page, pFlash.Bank);
00406       }
00407       else
00408       {
00409         /* No more pages to Erase */
00410         /* Reset Address and stop Erase pages procedure */
00411         pFlash.Page = 0xFFFFFFFFU;
00412         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00413 
00414         /* Flush the caches to be sure of the data consistency */
00415         FLASH_FlushCaches() ;
00416         
00417         /* FLASH EOP interrupt user callback */
00418         HAL_FLASH_EndOfOperationCallback(pFlash.Page);
00419       }
00420     }
00421     else
00422     {
00423       /* Flush the caches to be sure of the data consistency */
00424       FLASH_FlushCaches() ;
00425 
00426       procedure = pFlash.ProcedureOnGoing;
00427       if(procedure == FLASH_PROC_MASS_ERASE)
00428       {
00429         /* MassErase ended. Return the selected bank */
00430         /* FLASH EOP interrupt user callback */
00431         HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
00432       }
00433       else if((procedure == FLASH_PROC_PROGRAM) || 
00434               (procedure == FLASH_PROC_PROGRAM_LAST))
00435       {
00436         /* Program ended. Return the selected address */
00437         /* FLASH EOP interrupt user callback */
00438         HAL_FLASH_EndOfOperationCallback(pFlash.Address);
00439       }
00440       else
00441       {
00442         /* Nothing to do */
00443       }
00444 
00445       /*Clear the procedure ongoing*/
00446       pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00447     }
00448   }
00449 
00450   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
00451   {
00452     /* Disable End of Operation and Error interrupts */
00453     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
00454 
00455     /* Process Unlocked */
00456     __HAL_UNLOCK(&pFlash);
00457   }
00458 }
00459 
00460 /**
00461   * @brief  FLASH end of operation interrupt callback.
00462   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
00463   *                  Mass Erase: Bank number which has been requested to erase
00464   *                  Page Erase: Page which has been erased 
00465   *                    (if 0xFFFFFFFF, it means that all the selected pages have been erased)
00466   *                  Program: Address which was selected for data program
00467   * @retval None
00468   */
00469 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
00470 {
00471   /* Prevent unused argument(s) compilation warning */
00472   UNUSED(ReturnValue);
00473 
00474   /* NOTE : This function should not be modified, when the callback is needed,
00475             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
00476    */ 
00477 }
00478 
00479 /**
00480   * @brief  FLASH operation error interrupt callback.
00481   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
00482   *                 Mass Erase: Bank number which has been requested to erase
00483   *                 Page Erase: Page number which returned an error
00484   *                 Program: Address which was selected for data program
00485   * @retval None
00486   */
00487 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
00488 {
00489   /* Prevent unused argument(s) compilation warning */
00490   UNUSED(ReturnValue);
00491 
00492   /* NOTE : This function should not be modified, when the callback is needed,
00493             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
00494    */ 
00495 }
00496 
00497 /**
00498   * @}
00499   */ 
00500 
00501 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions 
00502  *  @brief   Management functions 
00503  *
00504 @verbatim   
00505  ===============================================================================
00506                       ##### Peripheral Control functions #####
00507  ===============================================================================  
00508     [..]
00509     This subsection provides a set of functions allowing to control the FLASH 
00510     memory operations.
00511 
00512 @endverbatim
00513   * @{
00514   */
00515 
00516 /**
00517   * @brief  Unlock the FLASH control register access.
00518   * @retval HAL Status
00519   */
00520 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
00521 {
00522   HAL_StatusTypeDef status = HAL_OK;
00523 
00524   if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
00525   {
00526     /* Authorize the FLASH Registers access */
00527     WRITE_REG(FLASH->KEYR, FLASH_KEY1);
00528     WRITE_REG(FLASH->KEYR, FLASH_KEY2);
00529 
00530     /* Verify Flash is unlocked */
00531     if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != 0U)
00532     {
00533       status = HAL_ERROR;
00534     }
00535   }
00536   
00537   return status;
00538 }
00539 
00540 /**
00541   * @brief  Lock the FLASH control register access.
00542   * @retval HAL Status
00543   */
00544 HAL_StatusTypeDef HAL_FLASH_Lock(void)
00545 {
00546   /* Set the LOCK Bit to lock the FLASH Registers access */
00547   SET_BIT(FLASH->CR, FLASH_CR_LOCK);
00548   
00549   return HAL_OK;  
00550 }
00551 
00552 /**
00553   * @brief  Unlock the FLASH Option Bytes Registers access.
00554   * @retval HAL Status
00555   */
00556 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
00557 {
00558   if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != 0U)
00559   {
00560     /* Authorizes the Option Byte register programming */
00561     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
00562     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
00563   }
00564   else
00565   {
00566     return HAL_ERROR;
00567   }  
00568   
00569   return HAL_OK;  
00570 }
00571 
00572 /**
00573   * @brief  Lock the FLASH Option Bytes Registers access.
00574   * @retval HAL Status 
00575   */
00576 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
00577 {
00578   /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
00579   SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
00580   
00581   return HAL_OK;  
00582 }
00583 
00584 /**
00585   * @brief  Launch the option byte loading.
00586   * @retval HAL Status
00587   */
00588 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
00589 {
00590   /* Set the bit to force the option byte reloading */
00591   SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH); 
00592 
00593   /* Wait for last operation to be completed */
00594   return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)); 
00595 }
00596 
00597 /**
00598   * @}
00599   */
00600 
00601 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions 
00602  *  @brief   Peripheral Errors functions 
00603  *
00604 @verbatim   
00605  ===============================================================================
00606                 ##### Peripheral Errors functions #####
00607  ===============================================================================  
00608     [..]
00609     This subsection permits to get in run-time Errors of the FLASH peripheral.
00610 
00611 @endverbatim
00612   * @{
00613   */
00614 
00615 /**
00616   * @brief  Get the specific FLASH error flag.
00617   * @retval FLASH_ErrorCode: The returned value can be:
00618   *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
00619   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag 
00620   *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag  
00621   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
00622   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
00623   *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag 
00624   *            @arg HAL_FLASH_ERROR_NONE: No error set 
00625   *            @arg HAL_FLASH_ERROR_OP: FLASH Operation error
00626   *            @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
00627   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
00628   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
00629   *            @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
00630   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
00631   *            @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
00632   *            @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
00633   *            @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
00634   *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
00635   *            @arg FLASH_FLAG_PEMPTY : FLASH Boot from not programmed flash (apply only for STM32L43x/STM32L44x devices)
00636   *            @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
00637   */
00638 uint32_t HAL_FLASH_GetError(void)
00639 { 
00640    return pFlash.ErrorCode;
00641 }
00642 
00643 /**
00644   * @}
00645   */ 
00646 
00647 /**
00648   * @}
00649   */
00650 
00651 /* Private functions ---------------------------------------------------------*/
00652 
00653 /** @addtogroup FLASH_Private_Functions
00654   * @{
00655   */
00656 
00657 /**
00658   * @brief  Wait for a FLASH operation to complete.
00659   * @param  Timeout: maximum flash operation timeout
00660   * @retval HAL_StatusTypeDef HAL Status
00661   */
00662 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
00663 {
00664   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
00665      Even if the FLASH operation fails, the BUSY flag will be reset and an error
00666      flag will be set */
00667     
00668   uint32_t tickstart = HAL_GetTick();
00669   uint32_t error;
00670 
00671   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) 
00672   { 
00673     if(Timeout != HAL_MAX_DELAY)
00674     {
00675       if((HAL_GetTick() - tickstart) >= Timeout)
00676       {
00677         return HAL_TIMEOUT;
00678       }
00679     } 
00680   }
00681   
00682   error = (FLASH->SR & FLASH_FLAG_SR_ERRORS);
00683   error |= (FLASH->ECCR & FLASH_FLAG_ECCD);
00684 
00685   if(error != 0u)
00686   {
00687     /*Save the error code*/
00688     pFlash.ErrorCode |= error;
00689 
00690     /* Clear error programming flags */
00691     __HAL_FLASH_CLEAR_FLAG(error);
00692     
00693     return HAL_ERROR;
00694   }
00695 
00696   /* Check FLASH End of Operation flag  */
00697   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
00698   {
00699     /* Clear FLASH End of Operation pending bit */
00700     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00701   }
00702   
00703   /* If there is an error flag set */
00704   return HAL_OK;  
00705 }
00706 
00707 /**
00708   * @brief  Program double-word (64-bit) at a specified address.
00709   * @param  Address: specifies the address to be programmed.
00710   * @param  Data: specifies the data to be programmed.
00711   * @retval None
00712   */
00713 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
00714 {
00715   /* Check the parameters */
00716   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
00717 
00718   /* Set PG bit */
00719   SET_BIT(FLASH->CR, FLASH_CR_PG);
00720   
00721   /* Program the double word */
00722   *(__IO uint32_t*)Address = (uint32_t)Data;
00723   *(__IO uint32_t*)(Address+4U) = (uint32_t)(Data >> 32);
00724 }
00725 
00726 /**
00727   * @brief  Fast program a row double-word (64-bit) at a specified address.
00728   * @param  Address: specifies the address to be programmed.
00729   * @param  DataAddress: specifies the address where the data are stored.
00730   * @retval None
00731   */
00732 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
00733 {
00734   uint8_t row_index = (2*FLASH_NB_DOUBLE_WORDS_IN_ROW);
00735   __IO uint32_t *dest_addr = (__IO uint32_t*)Address;
00736   __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
00737 
00738   /* Check the parameters */
00739   assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
00740 
00741   /* Set FSTPG bit */
00742   SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
00743   
00744   /* Disable interrupts to avoid any interruption during the loop */
00745   __disable_irq();
00746   
00747   /* Program the double word of the row */
00748   do
00749   {
00750     *dest_addr = *src_addr;
00751     dest_addr++;
00752     src_addr++;
00753     row_index--;
00754   } while (row_index != 0U);
00755 
00756   /* Re-enable the interrupts */
00757   __enable_irq();
00758 }
00759 
00760 /**
00761   * @}
00762   */
00763 
00764 #endif /* HAL_FLASH_MODULE_ENABLED */
00765 
00766 /**
00767   * @}
00768   */
00769 
00770 /**
00771   * @}
00772   */
00773 
00774 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/