Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

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.1.0 
00006   * @date    16-September-2015
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) 2015 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_MER2 | FLASH_CR_PER | FLASH_CR_PNB));
00294   
00295   /* Disable the FSTPG Bit only if it is the last row programmed */
00296   if(pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST)
00297   {
00298     CLEAR_BIT(FLASH->CR, FLASH_CR_FSTPG);
00299   }
00300 
00301   /* Check FLASH operation error flags */
00302   if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) || 
00303      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))  || 
00304      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))  ||
00305      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) || 
00306      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
00307      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
00308   {
00309     /*Save the error code*/
00310     FLASH_SetErrorCode();
00311     
00312     /* FLASH error interrupt user callback */
00313     if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
00314     {
00315        HAL_FLASH_EndOfOperationCallback(pFlash.Page);
00316     }
00317     else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
00318     {
00319         HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
00320     }
00321     else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) || 
00322             (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
00323     {
00324        HAL_FLASH_OperationErrorCallback(pFlash.Address);
00325     }
00326 
00327     HAL_FLASH_OperationErrorCallback(pFlash.Address);
00328 
00329     /*Stop the procedure ongoing*/
00330     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00331   }
00332 
00333   /* Check FLASH End of Operation flag  */
00334   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
00335   {
00336     /* Clear FLASH End of Operation pending bit */
00337     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00338 
00339     if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGE_ERASE)
00340     {
00341       /* Nb of pages to erased can be decreased */
00342       pFlash.NbPagesToErase--;
00343 
00344       /* Check if there are still pages to erase*/
00345       if(pFlash.NbPagesToErase != 0)
00346       {
00347         /* Indicate user which page has been erased*/
00348         HAL_FLASH_EndOfOperationCallback(pFlash.Page);
00349 
00350         /* Increment page number */
00351         pFlash.Page++;
00352         tmp_page = pFlash.Page;
00353         FLASH_PageErase(tmp_page, pFlash.Bank);
00354       }
00355       else
00356       {
00357         /* No more pages to Erase */
00358         /* Reset Address and stop Erase pages procedure */
00359         pFlash.Page = 0xFFFFFFFF;
00360         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00361 
00362         /* Flush the caches to be sure of the data consistency */
00363         FLASH_FlushCaches() ;
00364         
00365         /* FLASH EOP interrupt user callback */
00366         HAL_FLASH_EndOfOperationCallback(pFlash.Page);
00367       }
00368     }
00369     else
00370     {
00371       if(pFlash.ProcedureOnGoing == FLASH_PROC_MASS_ERASE)
00372       {
00373         /* MassErase ended. Return the selected bank */
00374         /* Flush the caches to be sure of the data consistency */
00375         FLASH_FlushCaches() ;
00376 
00377         /* FLASH EOP interrupt user callback */
00378         HAL_FLASH_EndOfOperationCallback(pFlash.Bank);
00379       }
00380       else if((pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM) || 
00381               (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAM_LAST))
00382       {
00383         /* Program ended. Return the selected address */
00384         /* FLASH EOP interrupt user callback */
00385         HAL_FLASH_EndOfOperationCallback(pFlash.Address);
00386       }
00387 
00388       /*Clear the procedure ongoing*/
00389       pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
00390     }
00391   }
00392 
00393   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
00394   {
00395     /* Disable End of Operation and Error interrupts */
00396     __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_OPERR);
00397 
00398     /* Process Unlocked */
00399     __HAL_UNLOCK(&pFlash);
00400   }
00401 }
00402 
00403 /**
00404   * @brief  FLASH end of operation interrupt callback.
00405   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
00406   *                  Mass Erase: Bank number which has been requested to erase
00407   *                  Page Erase: Page which has been erased 
00408   *                    (if 0xFFFFFFFF, it means that all the selected pages have been erased)
00409   *                  Program: Address which was selected for data program
00410   * @retval None
00411   */
00412 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
00413 {
00414   /* NOTE : This function should not be modified, when the callback is needed,
00415             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
00416    */ 
00417 }
00418 
00419 /**
00420   * @brief  FLASH operation error interrupt callback.
00421   * @param  ReturnValue: The value saved in this parameter depends on the ongoing procedure
00422   *                 Mass Erase: Bank number which has been requested to erase
00423   *                 Page Erase: Page number which returned an error
00424   *                 Program: Address which was selected for data program
00425   * @retval None
00426   */
00427 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
00428 {
00429   /* NOTE : This function should not be modified, when the callback is needed,
00430             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
00431    */ 
00432 }
00433 
00434 /**
00435   * @}
00436   */ 
00437 
00438 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions 
00439  *  @brief   Management functions 
00440  *
00441 @verbatim   
00442  ===============================================================================
00443                       ##### Peripheral Control functions #####
00444  ===============================================================================  
00445     [..]
00446     This subsection provides a set of functions allowing to control the FLASH 
00447     memory operations.
00448 
00449 @endverbatim
00450   * @{
00451   */
00452 
00453 /**
00454   * @brief  Unlock the FLASH control register access.
00455   * @retval HAL Status
00456   */
00457 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
00458 {
00459   if(READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET)
00460   {
00461     /* Authorize the FLASH Registers access */
00462     WRITE_REG(FLASH->KEYR, FLASH_KEY1);
00463     WRITE_REG(FLASH->KEYR, FLASH_KEY2);
00464   }
00465   else
00466   {
00467     return HAL_ERROR;
00468   }
00469   
00470   return HAL_OK;
00471 }
00472 
00473 /**
00474   * @brief  Lock the FLASH control register access.
00475   * @retval HAL Status
00476   */
00477 HAL_StatusTypeDef HAL_FLASH_Lock(void)
00478 {
00479   /* Set the LOCK Bit to lock the FLASH Registers access */
00480   SET_BIT(FLASH->CR, FLASH_CR_LOCK);
00481   
00482   return HAL_OK;  
00483 }
00484 
00485 /**
00486   * @brief  Unlock the FLASH Option Bytes Registers access.
00487   * @retval HAL Status
00488   */
00489 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
00490 {
00491   if(READ_BIT(FLASH->CR, FLASH_CR_OPTLOCK) != RESET)
00492   {
00493     /* Authorizes the Option Byte register programming */
00494     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
00495     WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
00496   }
00497   else
00498   {
00499     return HAL_ERROR;
00500   }  
00501   
00502   return HAL_OK;  
00503 }
00504 
00505 /**
00506   * @brief  Lock the FLASH Option Bytes Registers access.
00507   * @retval HAL Status 
00508   */
00509 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
00510 {
00511   /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
00512   SET_BIT(FLASH->CR, FLASH_CR_OPTLOCK);
00513   
00514   return HAL_OK;  
00515 }
00516 
00517 /**
00518   * @brief  Launch the option byte loading.
00519   * @retval HAL Status
00520   */
00521 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
00522 {
00523   /* Set the bit to force the option byte reloading */
00524   SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH); 
00525 
00526   /* Wait for last operation to be completed */
00527   return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)); 
00528 }
00529 
00530 /**
00531   * @}
00532   */
00533 
00534 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions 
00535  *  @brief   Peripheral Errors functions 
00536  *
00537 @verbatim   
00538  ===============================================================================
00539                 ##### Peripheral Errors functions #####
00540  ===============================================================================  
00541     [..]
00542     This subsection permits to get in run-time Errors of the FLASH peripheral.
00543 
00544 @endverbatim
00545   * @{
00546   */
00547 
00548 /**
00549   * @brief  Get the specific FLASH error flag.
00550   * @retval FLASH_ErrorCode: The returned value can be:
00551   *            @arg HAL_FLASH_ERROR_RD: FLASH Read Protection error flag (PCROP)
00552   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming Sequence error flag 
00553   *            @arg HAL_FLASH_ERROR_PGP: FLASH Programming Parallelism error flag  
00554   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming Alignment error flag
00555   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protected error flag
00556   *            @arg HAL_FLASH_ERROR_OPERATION: FLASH operation Error flag 
00557   *            @arg HAL_FLASH_ERROR_NONE: No error set 
00558   *            @arg HAL_FLASH_ERROR_OP: FLASH Operation error
00559   *            @arg HAL_FLASH_ERROR_PROG: FLASH Programming error
00560   *            @arg HAL_FLASH_ERROR_WRP: FLASH Write protection error
00561   *            @arg HAL_FLASH_ERROR_PGA: FLASH Programming alignment error
00562   *            @arg HAL_FLASH_ERROR_SIZ: FLASH Size error
00563   *            @arg HAL_FLASH_ERROR_PGS: FLASH Programming sequence error
00564   *            @arg HAL_FLASH_ERROR_MIS: FLASH Fast programming data miss error
00565   *            @arg HAL_FLASH_ERROR_FAST: FLASH Fast programming error
00566   *            @arg HAL_FLASH_ERROR_RD: FLASH PCROP read error
00567   *            @arg HAL_FLASH_ERROR_OPTV: FLASH Option validity error
00568   *            @arg HAL_FLASH_ERROR_ECCD: FLASH two ECC errors have been detected
00569   */
00570 uint32_t HAL_FLASH_GetError(void)
00571 { 
00572    return pFlash.ErrorCode;
00573 }
00574 
00575 /**
00576   * @}
00577   */ 
00578 
00579 /**
00580   * @}
00581   */
00582 
00583 /* Private functions ---------------------------------------------------------*/
00584 
00585 /** @addtogroup FLASH_Private_Functions
00586   * @{
00587   */
00588 
00589 /**
00590   * @brief  Wait for a FLASH operation to complete.
00591   * @param  Timeout: maximum flash operation timeout
00592   * @retval HAL_StatusTypeDef HAL Status
00593   */
00594 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
00595 {
00596   /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
00597      Even if the FLASH operation fails, the BUSY flag will be reset and an error
00598      flag will be set */
00599     
00600   uint32_t timeout = HAL_GetTick() + Timeout;
00601      
00602   while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) 
00603   { 
00604     if(Timeout != HAL_MAX_DELAY)
00605     {
00606       if(HAL_GetTick() >= timeout)
00607       {
00608         return HAL_TIMEOUT;
00609       }
00610     } 
00611   }
00612   
00613   if((__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR)) || 
00614      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))  || 
00615      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))  ||
00616      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR)) || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR)) || 
00617      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))  || (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) ||
00618      (__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD)))
00619   {
00620     /*Save the error code*/
00621     FLASH_SetErrorCode();
00622     
00623     return HAL_ERROR;
00624   }
00625 
00626   /* Check FLASH End of Operation flag  */
00627   if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
00628   {
00629     /* Clear FLASH End of Operation pending bit */
00630     __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
00631   }
00632   
00633   /* If there is an error flag set */
00634   return HAL_OK;  
00635 }
00636 
00637 /**
00638   * @brief  Set the specific FLASH error flag.
00639   * @retval None
00640   */
00641 static void FLASH_SetErrorCode(void)
00642 {  
00643   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPERR))
00644   {
00645     pFlash.ErrorCode |= HAL_FLASH_ERROR_OP;
00646   }
00647 
00648   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PROGERR))
00649   { 
00650     pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
00651   }
00652 
00653   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
00654   { 
00655     pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
00656   }
00657 
00658   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGAERR))
00659   { 
00660     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGA;
00661   }
00662 
00663   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_SIZERR))
00664   { 
00665     pFlash.ErrorCode |= HAL_FLASH_ERROR_SIZ;
00666   }
00667 
00668   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGSERR))
00669   { 
00670     pFlash.ErrorCode |= HAL_FLASH_ERROR_PGS;
00671   }
00672 
00673   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_MISERR))
00674   { 
00675    pFlash.ErrorCode |= HAL_FLASH_ERROR_MIS;
00676   }
00677 
00678   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_FASTERR))
00679   { 
00680    pFlash.ErrorCode |= HAL_FLASH_ERROR_FAST;
00681   }
00682 
00683   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_RDERR))
00684   { 
00685    pFlash.ErrorCode |= HAL_FLASH_ERROR_RD;
00686   }
00687 
00688   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR))
00689   { 
00690    pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV;
00691   }
00692 
00693   if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_ECCD))
00694   { 
00695    pFlash.ErrorCode |= HAL_FLASH_ERROR_ECCD;
00696   }
00697 
00698   /* Clear error programming flags */
00699   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
00700 } 
00701 
00702 /**
00703   * @brief  Program double-word (64-bit) at a specified address.
00704   * @param  Address: specifies the address to be programmed.
00705   * @param  Data: specifies the data to be programmed.
00706   * @retval None
00707   */
00708 static void FLASH_Program_DoubleWord(uint32_t Address, uint64_t Data)
00709 {
00710   /* Check the parameters */
00711   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
00712 
00713   /* Set PG bit */
00714   SET_BIT(FLASH->CR, FLASH_CR_PG);
00715   
00716   /* Program the double word */
00717   *(__IO uint32_t*)Address = (uint32_t)Data;
00718   *(__IO uint32_t*)(Address+4) = (uint32_t)(Data >> 32);
00719 }
00720 
00721 /**
00722   * @brief  Fast program a 32 row double-word (64-bit) at a specified address.
00723   * @param  Address: specifies the address to be programmed.
00724   * @param  DataAddress: specifies the address where the data are stored.
00725   * @retval None
00726   */
00727 static void FLASH_Program_Fast(uint32_t Address, uint32_t DataAddress)
00728 {
00729   uint8_t row_index = 32;
00730   __IO uint64_t *dest_addr = (__IO uint64_t*)Address;
00731   __IO uint64_t *src_addr = (__IO uint64_t*)DataAddress;
00732 
00733   /* Check the parameters */
00734   assert_param(IS_FLASH_MAIN_MEM_ADDRESS(Address));
00735 
00736   /* Set FSTPG bit */
00737   SET_BIT(FLASH->CR, FLASH_CR_FSTPG);
00738   
00739   /* Disable interrupts to avoid any interruption during the loop */
00740   __disable_irq();
00741   
00742   /* Program the 32 double word */
00743   do
00744   {
00745     *dest_addr++ = *src_addr++;
00746   } while (--row_index != 0);
00747 
00748   /* Re-enable the interrupts */
00749   __enable_irq();
00750 }
00751 
00752 /**
00753   * @}
00754   */
00755 
00756 #endif /* HAL_FLASH_MODULE_ENABLED */
00757 
00758 /**
00759   * @}
00760   */
00761 
00762 /**
00763   * @}
00764   */
00765 
00766 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00767