TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_flash.c Source File

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