Martin Johnson / STM32F3-Discovery

Dependents:   Space_Invaders_Demo neopixels gpio_test_stm32f3_discovery gpio_test_systimer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f30x_flash.c Source File

stm32f30x_flash.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f30x_flash.c
00004   * @author  MCD Application Team
00005   * @version V1.2.3
00006   * @date    10-July-2015
00007   * @brief   This file provides firmware functions to manage the following 
00008   *          functionalities of the FLASH peripheral:
00009   *            + FLASH Interface configuration
00010   *            + FLASH Memory Programming
00011   *            + Option Bytes Programming
00012   *            + Interrupts and flags management
00013   *  
00014   @verbatim
00015   
00016  ===============================================================================
00017                       ##### How to use this driver #####
00018  ===============================================================================
00019     [..] This driver provides functions to configure and program the FLASH 
00020          memory of all STM32F30x devices. These functions are split in 4 groups:
00021          (#) FLASH Interface configuration functions: this group includes the
00022              management of following features:
00023              (++) Set the latency.
00024              (++) Enable/Disable the Half Cycle Access.
00025              (++) Enable/Disable the prefetch buffer.
00026          (#) FLASH Memory Programming functions: this group includes all needed
00027              functions to erase and program the main memory:
00028              (++) Lock and Unlock the FLASH interface.
00029              (++) Erase function: Erase page, erase all pages.
00030              (++) Program functions: Half Word and Word write.
00031          (#) FLASH Option Bytes Programming functions: this group includes all 
00032              needed functions to manage the Option Bytes:
00033              (++) Lock and Unlock the Flash Option bytes.
00034              (++) Launch the Option Bytes loader
00035              (++) Erase the Option Bytes
00036              (++) Set/Reset the write protection
00037              (++) Set the Read protection Level
00038              (++) Program the user option Bytes
00039              (++) Set/Reset the BOOT1 bit
00040              (++) Enable/Disable the VDDA Analog Monitoring
00041              (++) Enable/Disable the SRAM parity
00042              (++) Get the user option bytes
00043              (++) Get the Write protection
00044              (++) Get the read protection status
00045          (#) FLASH Interrupts and flags management functions: this group includes 
00046              all needed functions to:
00047              (++) Enable/Disable the FLASH interrupt sources.
00048              (++) Get flags status.
00049              (++) Clear flags.
00050              (++) Get FLASH operation status.
00051              (++) Wait for last FLASH operation.
00052  
00053   @endverbatim
00054                       
00055   ******************************************************************************
00056   * @attention
00057   *
00058   * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
00059   *
00060   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00061   * You may not use this file except in compliance with the License.
00062   * You may obtain a copy of the License at:
00063   *
00064   *        http://www.st.com/software_license_agreement_liberty_v2
00065   *
00066   * Unless required by applicable law or agreed to in writing, software 
00067   * distributed under the License is distributed on an "AS IS" BASIS, 
00068   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00069   * See the License for the specific language governing permissions and
00070   * limitations under the License.
00071   *
00072   ******************************************************************************
00073   */
00074 
00075 /* Includes ------------------------------------------------------------------*/
00076 #include "stm32f30x_flash.h"
00077 
00078 /** @addtogroup STM32F30x_StdPeriph_Driver
00079   * @{
00080   */
00081 
00082 /** @defgroup FLASH 
00083   * @brief FLASH driver modules
00084   * @{
00085   */ 
00086 
00087 /* Private typedef -----------------------------------------------------------*/
00088 /* Private define ------------------------------------------------------------*/
00089 
00090 /* FLASH Mask */
00091 #define RDPRT_MASK                 ((uint32_t)0x00000002)
00092 #define WRP01_MASK                 ((uint32_t)0x0000FFFF)
00093 #define WRP23_MASK                 ((uint32_t)0xFFFF0000)
00094 /* Private macro -------------------------------------------------------------*/
00095 /* Private variables ---------------------------------------------------------*/
00096 /* Private function prototypes -----------------------------------------------*/
00097 /* Private functions ---------------------------------------------------------*/
00098 
00099 /** @defgroup FLASH_Private_Functions
00100   * @{
00101   */ 
00102 
00103 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
00104   *  @brief   FLASH Interface configuration functions 
00105  *
00106 
00107 @verbatim   
00108  ===============================================================================
00109             ##### FLASH Interface configuration functions #####
00110  ===============================================================================
00111     [..] This group includes the following functions:
00112          (+) void FLASH_SetLatency(uint32_t FLASH_Latency); 
00113          (+) void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);     
00114          (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
00115     [..] The unlock sequence is not needed for these functions.
00116  
00117 @endverbatim
00118   * @{
00119   */
00120  
00121 /**
00122   * @brief  Sets the code latency value.
00123   * @param  FLASH_Latency: specifies the FLASH Latency value.
00124   *          This parameter can be one of the following values:
00125   *            @arg FLASH_Latency_0: FLASH Zero Latency cycle
00126   *            @arg FLASH_Latency_1: FLASH One Latency cycle
00127   *            @arg FLASH_Latency_2: FLASH Two Latency cycles      
00128   * @retval None
00129   */
00130 void FLASH_SetLatency(uint32_t FLASH_Latency)
00131 {
00132    uint32_t tmpreg = 0;
00133   
00134   /* Check the parameters */
00135   assert_param(IS_FLASH_LATENCY(FLASH_Latency));
00136   
00137   /* Read the ACR register */
00138   tmpreg = FLASH->ACR;  
00139   
00140   /* Sets the Latency value */
00141   tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
00142   tmpreg |= FLASH_Latency;
00143   
00144   /* Write the ACR register */
00145   FLASH->ACR = tmpreg;
00146 }
00147 
00148 /**
00149   * @brief  Enables or disables the Half cycle flash access.
00150   * @param  FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
00151   *          This parameter can be one of the following values:
00152   *            @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
00153   *            @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
00154   * @retval None
00155   */
00156 void FLASH_HalfCycleAccessCmd(FunctionalState NewState)
00157 {
00158   /* Check the parameters */
00159   assert_param(IS_FUNCTIONAL_STATE(NewState));
00160    
00161   if(NewState != DISABLE)
00162   {
00163     FLASH->ACR |= FLASH_ACR_HLFCYA;
00164   }
00165   else
00166   {
00167     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_HLFCYA));
00168   }
00169 }
00170 
00171 /**
00172   * @brief  Enables or disables the Prefetch Buffer.
00173   * @param  NewState: new state of the Prefetch Buffer.
00174   *          This parameter  can be: ENABLE or DISABLE.
00175   * @retval None
00176   */
00177 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
00178 {
00179   /* Check the parameters */
00180   assert_param(IS_FUNCTIONAL_STATE(NewState));
00181    
00182   if(NewState != DISABLE)
00183   {
00184     FLASH->ACR |= FLASH_ACR_PRFTBE;
00185   }
00186   else
00187   {
00188     FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
00189   }
00190 }
00191 
00192 /**
00193   * @}
00194   */
00195 
00196 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
00197  *  @brief   FLASH Memory Programming functions
00198  *
00199 @verbatim   
00200  ===============================================================================
00201               ##### FLASH Memory Programming functions #####
00202  ===============================================================================   
00203     [..] This group includes the following functions:
00204          (+) void FLASH_Unlock(void);
00205          (+) void FLASH_Lock(void);
00206          (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
00207          (+) FLASH_Status FLASH_EraseAllPages(void);
00208          (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
00209          (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
00210     [..] Any operation of erase or program should follow these steps:
00211          (#) Call the FLASH_Unlock() function to enable the FLASH control register 
00212              program memory access.
00213          (#) Call the desired function to erase page or program data.
00214          (#) Call the FLASH_Lock() function to disable the FLASH control register 
00215              access (recommended to protect the FLASH memory against possible 
00216              unwanted operation).
00217     
00218 @endverbatim
00219   * @{
00220   */
00221 
00222 /**
00223   * @brief  Unlocks the FLASH control register access
00224   * @param  None
00225   * @retval None
00226   */
00227 void FLASH_Unlock(void)
00228 {
00229   if((FLASH->CR & FLASH_CR_LOCK) != RESET)
00230   {
00231     /* Authorize the FLASH Registers access */
00232     FLASH->KEYR = FLASH_KEY1;
00233     FLASH->KEYR = FLASH_KEY2;
00234   }  
00235 }
00236 
00237 /**
00238   * @brief  Locks the FLASH control register access
00239   * @param  None
00240   * @retval None
00241   */
00242 void FLASH_Lock(void)
00243 {
00244   /* Set the LOCK Bit to lock the FLASH Registers access */
00245   FLASH->CR |= FLASH_CR_LOCK;
00246 }
00247 
00248 /**
00249   * @brief  Erases a specified page in program memory.
00250   * @note   To correctly run this function, the FLASH_Unlock() function
00251   *         must be called before.
00252   * @note   Call the FLASH_Lock() to disable the flash memory access 
00253   *         (recommended to protect the FLASH memory against possible unwanted operation)  
00254   * @param  Page_Address: The page address in program memory to be erased.
00255   * @note   A Page is erased in the Program memory only if the address to load 
00256   *         is the start address of a page (multiple of 1024 bytes).  
00257   * @retval FLASH Status: The returned value can be: 
00258   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00259   */
00260 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
00261 {
00262   FLASH_Status status = FLASH_COMPLETE;
00263 
00264   /* Check the parameters */
00265   assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
00266  
00267   /* Wait for last operation to be completed */
00268   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00269   
00270   if(status == FLASH_COMPLETE)
00271   { 
00272     /* If the previous operation is completed, proceed to erase the page */
00273     FLASH->CR |= FLASH_CR_PER;
00274     FLASH->AR  = Page_Address; 
00275     FLASH->CR |= FLASH_CR_STRT;
00276     
00277     /* Wait for last operation to be completed */
00278     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00279     
00280     /* Disable the PER Bit */
00281     FLASH->CR &= ~FLASH_CR_PER;
00282   }
00283     
00284   /* Return the Erase Status */
00285   return status;
00286 }
00287 
00288 /**
00289   * @brief  Erases all FLASH pages.
00290   * @note   To correctly run this function, the FLASH_Unlock() function
00291   *         must be called before.
00292   *         all the FLASH_Lock() to disable the flash memory access 
00293   *         (recommended to protect the FLASH memory against possible unwanted operation)
00294   * @param  None
00295   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00296   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00297   */
00298 FLASH_Status FLASH_EraseAllPages(void)
00299 {
00300   FLASH_Status status = FLASH_COMPLETE;
00301 
00302   /* Wait for last operation to be completed */
00303   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00304   
00305   if(status == FLASH_COMPLETE)
00306   {
00307     /* if the previous operation is completed, proceed to erase all pages */
00308      FLASH->CR |= FLASH_CR_MER;
00309      FLASH->CR |= FLASH_CR_STRT;
00310     
00311     /* Wait for last operation to be completed */
00312     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00313 
00314     /* Disable the MER Bit */
00315     FLASH->CR &= ~FLASH_CR_MER;
00316   }
00317 
00318   /* Return the Erase Status */
00319   return status;
00320 }
00321 
00322 /**
00323   * @brief  Programs a word at a specified address.
00324   * @note   To correctly run this function, the FLASH_Unlock() function
00325   *         must be called before.
00326   *         Call the FLASH_Lock() to disable the flash memory access 
00327   *         (recommended to protect the FLASH memory against possible unwanted operation)  
00328   * @param  Address: specifies the address to be programmed.
00329   * @param  Data: specifies the data to be programmed.
00330   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00331   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
00332   */
00333 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
00334 {
00335   FLASH_Status status = FLASH_COMPLETE;
00336   __IO uint32_t tmp = 0;
00337 
00338   /* Check the parameters */
00339   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
00340 
00341   /* Wait for last operation to be completed */
00342   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00343   
00344   if(status == FLASH_COMPLETE)
00345   {
00346     /* If the previous operation is completed, proceed to program the new first 
00347     half word */
00348     FLASH->CR |= FLASH_CR_PG;
00349   
00350     *(__IO uint16_t*)Address = (uint16_t)Data;
00351     
00352     /* Wait for last operation to be completed */
00353     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00354  
00355     if(status == FLASH_COMPLETE)
00356     {
00357       /* If the previous operation is completed, proceed to program the new second 
00358       half word */
00359       tmp = Address + 2;
00360 
00361       *(__IO uint16_t*) tmp = Data >> 16;
00362     
00363       /* Wait for last operation to be completed */
00364       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00365         
00366       /* Disable the PG Bit */
00367       FLASH->CR &= ~FLASH_CR_PG;
00368     }
00369     else
00370     {
00371       /* Disable the PG Bit */
00372       FLASH->CR &= ~FLASH_CR_PG;
00373     }
00374   }
00375    
00376   /* Return the Program Status */
00377   return status;
00378 }
00379 
00380 /**
00381   * @brief  Programs a half word at a specified address.
00382   * @note   To correctly run this function, the FLASH_Unlock() function
00383   *         must be called before.
00384   *         Call the FLASH_Lock() to disable the flash memory access 
00385   *         (recommended to protect the FLASH memory against possible unwanted operation) 
00386   * @param  Address: specifies the address to be programmed.
00387   * @param  Data: specifies the data to be programmed.
00388   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00389   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
00390   */
00391 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
00392 {
00393   FLASH_Status status = FLASH_COMPLETE;
00394 
00395   /* Check the parameters */
00396   assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
00397 
00398   /* Wait for last operation to be completed */
00399   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00400   
00401   if(status == FLASH_COMPLETE)
00402   {
00403     /* If the previous operation is completed, proceed to program the new data */
00404     FLASH->CR |= FLASH_CR_PG;
00405   
00406     *(__IO uint16_t*)Address = Data;
00407 
00408     /* Wait for last operation to be completed */
00409     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00410     
00411     /* Disable the PG Bit */
00412     FLASH->CR &= ~FLASH_CR_PG;
00413   } 
00414   
00415   /* Return the Program Status */
00416   return status;
00417 }
00418 
00419 /**
00420   * @}
00421   */
00422   
00423 /** @defgroup FLASH_Group3 Option Bytes Programming functions
00424  *  @brief   Option Bytes Programming functions 
00425  *
00426 @verbatim   
00427  ===============================================================================
00428                 ##### Option Bytes Programming functions #####
00429  ===============================================================================  
00430     [..] This group includes the following functions:
00431          (+) void FLASH_OB_Unlock(void);
00432          (+) void FLASH_OB_Lock(void);
00433          (+) void FLASH_OB_Erase(void);
00434          (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
00435          (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
00436          (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
00437          (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
00438          (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
00439          (+) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity);
00440          (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);                  
00441          (+) FLASH_Status FLASH_OB_Launch(void);
00442          (+) uint32_t FLASH_OB_GetUser(void);                       
00443          (+) uint8_t FLASH_OB_GetWRP(void);                     
00444          (+) uint8_t FLASH_OB_GetRDP(void);                         
00445     [..] Any operation of erase or program should follow these steps:
00446          (#) Call the FLASH_OB_Unlock() function to enable the FLASH option control 
00447              register access.
00448          (#) Call one or several functions to program the desired Option Bytes:
00449              (++) void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState); 
00450                   => to Enable/Disable the desired sector write protection.
00451              (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the 
00452                   desired read Protection Level.
00453              (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY); 
00454                   => to configure the user Option Bytes.
00455              (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1); 
00456                   => to set the boot1 mode
00457              (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG); 
00458                   => to Enable/Disable the VDDA monitoring.
00459              (++) FLASH_Status FLASH_OB_SRMParityConfig(uint8_t OB_SRAM_Parity); 
00460                   => to Enable/Disable the SRAM Parity check.        
00461              (++) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER); 
00462                   => to write all user option bytes: OB_IWDG, OB_STOP, OB_STDBY, 
00463                      OB_BOOT1, OB_VDDA_ANALOG and OB_VDD_SD12.  
00464          (#) Once all needed Option Bytes to be programmed are correctly written, 
00465              call the FLASH_OB_Launch() function to launch the Option Bytes 
00466              programming process.
00467          (#@) When changing the IWDG mode from HW to SW or from SW to HW, a system 
00468               reset is needed to make the change effective.  
00469          (#) Call the FLASH_OB_Lock() function to disable the FLASH option control 
00470              register access (recommended to protect the Option Bytes against 
00471              possible unwanted operations).
00472     
00473 @endverbatim
00474   * @{
00475   */
00476 
00477 /**
00478   * @brief  Unlocks the option bytes block access.
00479   * @param  None
00480   * @retval None
00481   */
00482 void FLASH_OB_Unlock(void)
00483 {
00484   if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
00485   { 
00486     /* Unlocking the option bytes block access */
00487     FLASH->OPTKEYR = FLASH_OPTKEY1;
00488     FLASH->OPTKEYR = FLASH_OPTKEY2;
00489   }
00490 }
00491 
00492 /**
00493   * @brief  Locks the option bytes block access.
00494   * @param  None
00495   * @retval None
00496   */
00497 void FLASH_OB_Lock(void)
00498 {
00499   /* Set the OPTWREN Bit to lock the option bytes block access */
00500   FLASH->CR &= ~FLASH_CR_OPTWRE;
00501 }
00502 
00503 /**
00504   * @brief  Launch the option byte loading.
00505   * @param  None
00506   * @retval None
00507   */
00508 void FLASH_OB_Launch(void)
00509 {
00510   /* Set the OBL_Launch bit to launch the option byte loading */
00511   FLASH->CR |= FLASH_CR_OBL_LAUNCH; 
00512 }
00513 
00514 /**
00515   * @brief  Erases the FLASH option bytes.
00516   * @note   This functions erases all option bytes except the Read protection (RDP). 
00517   * @param  None
00518   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00519   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00520   */
00521 FLASH_Status FLASH_OB_Erase(void)
00522 {
00523   uint16_t rdptmp = OB_RDP_Level_0;
00524 
00525   FLASH_Status status = FLASH_COMPLETE;
00526 
00527   /* Get the actual read protection Option Byte value */ 
00528   if(FLASH_OB_GetRDP() != RESET)
00529   {
00530     rdptmp = 0x00;  
00531   }
00532 
00533   /* Wait for last operation to be completed */
00534   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00535 
00536   if(status == FLASH_COMPLETE)
00537   {   
00538     /* If the previous operation is completed, proceed to erase the option bytes */
00539     FLASH->CR |= FLASH_CR_OPTER;
00540     FLASH->CR |= FLASH_CR_STRT;
00541 
00542     /* Wait for last operation to be completed */
00543     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00544     
00545     if(status == FLASH_COMPLETE)
00546     {
00547       /* If the erase operation is completed, disable the OPTER Bit */
00548       FLASH->CR &= ~FLASH_CR_OPTER;
00549        
00550       /* Enable the Option Bytes Programming operation */
00551       FLASH->CR |= FLASH_CR_OPTPG;
00552 
00553       /* Restore the last read protection Option Byte value */
00554       OB->RDP = (uint16_t)rdptmp; 
00555 
00556       /* Wait for last operation to be completed */
00557       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00558  
00559       if(status != FLASH_TIMEOUT)
00560       {
00561         /* if the program operation is completed, disable the OPTPG Bit */
00562         FLASH->CR &= ~FLASH_CR_OPTPG;
00563       }
00564     }
00565     else
00566     {
00567       if (status != FLASH_TIMEOUT)
00568       {
00569         /* Disable the OPTPG Bit */
00570         FLASH->CR &= ~FLASH_CR_OPTPG;
00571       }
00572     }  
00573   }
00574   /* Return the erase status */
00575   return status;
00576 }
00577 
00578 /**
00579   * @brief  Write protects the desired pages
00580   * @note   To correctly run this function, the FLASH_OB_Unlock() function
00581   *         must be called before.
00582   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
00583   *         (recommended to protect the FLASH memory against possible unwanted operation)    
00584   * @param  OB_WRP: specifies the address of the pages to be write protected.
00585   *   This parameter can be:
00586   *     @arg  value between OB_WRP_Pages0to35 and OB_WRP_Pages60to63
00587   *     @arg OB_WRP_AllPages
00588   * @retval FLASH Status: The returned value can be: 
00589   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00590   */
00591 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
00592 {
00593   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
00594 
00595   FLASH_Status status = FLASH_COMPLETE;
00596 
00597   /* Check the parameters */
00598   assert_param(IS_OB_WRP(OB_WRP));
00599 
00600   OB_WRP = (uint32_t)(~OB_WRP);
00601   WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
00602   WRP1_Data = (uint16_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
00603   WRP2_Data = (uint16_t)((OB_WRP >> 16) & OB_WRP0_WRP0) ;
00604   WRP3_Data = (uint16_t)((OB_WRP >> 24) & OB_WRP0_WRP0) ;
00605     
00606   /* Wait for last operation to be completed */
00607   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00608 
00609   if(status == FLASH_COMPLETE)
00610   {
00611     FLASH->CR |= FLASH_CR_OPTPG;
00612 
00613     if(WRP0_Data != 0xFF)
00614     {
00615       OB->WRP0 = WRP0_Data;
00616       
00617       /* Wait for last operation to be completed */
00618       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00619     }
00620     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
00621     {
00622       OB->WRP1 = WRP1_Data;
00623       
00624       /* Wait for last operation to be completed */
00625       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00626     }
00627     if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
00628     {
00629       OB->WRP2 = WRP2_Data;
00630       
00631       /* Wait for last operation to be completed */
00632       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00633     }    
00634     if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
00635     {
00636       OB->WRP3 = WRP3_Data;
00637       
00638       /* Wait for last operation to be completed */
00639       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00640     }  
00641     if(status != FLASH_TIMEOUT)
00642     {
00643       /* if the program operation is completed, disable the OPTPG Bit */
00644       FLASH->CR &= ~FLASH_CR_OPTPG;
00645     }
00646   } 
00647   /* Return the write protection operation Status */
00648   return status; 
00649 }
00650 
00651 /**
00652   * @brief  Enables or disables the read out protection.
00653   * @note   To correctly run this function, the FLASH_OB_Unlock() function
00654   *         must be called before.
00655   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
00656   *         (recommended to protect the FLASH memory against possible unwanted operation)   
00657   * @param  FLASH_ReadProtection_Level: specifies the read protection level. 
00658   *   This parameter can be:
00659   *     @arg OB_RDP_Level_0: No protection
00660   *     @arg OB_RDP_Level_1: Read protection of the memory                     
00661   *     @arg OB_RDP_Level_2: Chip protection
00662   *     @retval FLASH Status: The returned value can be: 
00663   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00664   */
00665 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
00666 {
00667   FLASH_Status status = FLASH_COMPLETE;
00668   
00669   /* Check the parameters */
00670   assert_param(IS_OB_RDP(OB_RDP));
00671   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00672   
00673   if(status == FLASH_COMPLETE)
00674   {
00675     FLASH->CR |= FLASH_CR_OPTER;
00676     FLASH->CR |= FLASH_CR_STRT;
00677     
00678     /* Wait for last operation to be completed */
00679     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00680     
00681     if(status == FLASH_COMPLETE)
00682     {
00683       /* If the erase operation is completed, disable the OPTER Bit */
00684       FLASH->CR &= ~FLASH_CR_OPTER;
00685       
00686       /* Enable the Option Bytes Programming operation */
00687       FLASH->CR |= FLASH_CR_OPTPG;
00688        
00689       OB->RDP = OB_RDP;
00690 
00691       /* Wait for last operation to be completed */
00692       status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT); 
00693     
00694       if(status != FLASH_TIMEOUT)
00695       {
00696         /* if the program operation is completed, disable the OPTPG Bit */
00697         FLASH->CR &= ~FLASH_CR_OPTPG;
00698       }
00699     }
00700     else 
00701     {
00702       if(status != FLASH_TIMEOUT)
00703       {
00704         /* Disable the OPTER Bit */
00705         FLASH->CR &= ~FLASH_CR_OPTER;
00706       }
00707     }
00708   }
00709   /* Return the protection operation Status */
00710   return status;             
00711 }
00712 
00713 /**
00714   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
00715   * @param  OB_IWDG: Selects the IWDG mode
00716   *   This parameter can be one of the following values:
00717   *     @arg OB_IWDG_SW: Software IWDG selected
00718   *     @arg OB_IWDG_HW: Hardware IWDG selected
00719   * @param  OB_STOP: Reset event when entering STOP mode.
00720   *   This parameter can be one of the following values:
00721   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP
00722   *     @arg OB_STOP_RST: Reset generated when entering in STOP
00723   * @param  OB_STDBY: Reset event when entering Standby mode.
00724   *   This parameter can be one of the following values:
00725   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
00726   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY
00727   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, 
00728   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00729   */
00730 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
00731 {
00732   FLASH_Status status = FLASH_COMPLETE; 
00733 
00734   /* Check the parameters */
00735   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
00736   assert_param(IS_OB_STOP_SOURCE(OB_STOP));
00737   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
00738 
00739   /* Authorize the small information block programming */
00740   FLASH->OPTKEYR = FLASH_KEY1;
00741   FLASH->OPTKEYR = FLASH_KEY2;
00742   
00743   /* Wait for last operation to be completed */
00744   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00745   
00746   if(status == FLASH_COMPLETE)
00747   {  
00748     /* Enable the Option Bytes Programming operation */
00749     FLASH->CR |= FLASH_CR_OPTPG; 
00750            
00751     OB->USER = (uint8_t)((uint8_t)(OB_IWDG | OB_STOP) | (uint8_t)(OB_STDBY |0xF8));
00752   
00753     /* Wait for last operation to be completed */
00754     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00755 
00756     if(status != FLASH_TIMEOUT)
00757     {
00758       /* if the program operation is completed, disable the OPTPG Bit */
00759       FLASH->CR &= ~FLASH_CR_OPTPG;
00760     }
00761   }    
00762   /* Return the Option Byte program Status */
00763   return status;
00764 }
00765 
00766 /**
00767   * @brief  Sets or resets the BOOT1. 
00768   * @param  OB_BOOT1: Set or Reset the BOOT1.
00769   *   This parameter can be one of the following values:
00770   *     @arg OB_BOOT1_RESET: BOOT1 Reset
00771   *     @arg OB_BOOT1_SET: BOOT1 Set
00772   * @retval None
00773   */
00774 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
00775 {
00776   FLASH_Status status = FLASH_COMPLETE; 
00777 
00778   /* Check the parameters */
00779   assert_param(IS_OB_BOOT1(OB_BOOT1));
00780 
00781   /* Authorize the small information block programming */
00782   FLASH->OPTKEYR = FLASH_KEY1;
00783   FLASH->OPTKEYR = FLASH_KEY2;
00784   
00785   /* Wait for last operation to be completed */
00786   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00787   
00788   if(status == FLASH_COMPLETE)
00789   {  
00790     /* Enable the Option Bytes Programming operation */
00791     FLASH->CR |= FLASH_CR_OPTPG; 
00792            
00793     OB->USER = OB_BOOT1|0xEF;
00794   
00795     /* Wait for last operation to be completed */
00796     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00797 
00798     if(status != FLASH_TIMEOUT)
00799     {
00800       /* if the program operation is completed, disable the OPTPG Bit */
00801       FLASH->CR &= ~FLASH_CR_OPTPG;
00802     }
00803   }    
00804   /* Return the Option Byte program Status */
00805   return status;
00806 }
00807 
00808 /**
00809   * @brief  Sets or resets the analogue monitoring on VDDA Power source. 
00810   * @param  OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
00811   *   This parameter can be one of the following values:
00812   *     @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
00813   *     @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
00814   * @retval None
00815   */
00816 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
00817 {
00818   FLASH_Status status = FLASH_COMPLETE; 
00819 
00820   /* Check the parameters */
00821   assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
00822 
00823   /* Authorize the small information block programming */
00824   FLASH->OPTKEYR = FLASH_KEY1;
00825   FLASH->OPTKEYR = FLASH_KEY2;
00826   
00827   /* Wait for last operation to be completed */
00828   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00829   
00830   if(status == FLASH_COMPLETE)
00831   {  
00832     /* Enable the Option Bytes Programming operation */
00833     FLASH->CR |= FLASH_CR_OPTPG; 
00834            
00835     OB->USER = OB_VDDA_ANALOG |0xDF;
00836   
00837     /* Wait for last operation to be completed */
00838     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00839 
00840     if(status != FLASH_TIMEOUT)
00841     {
00842       /* if the program operation is completed, disable the OPTPG Bit */
00843       FLASH->CR &= ~FLASH_CR_OPTPG;
00844     }
00845   }    
00846   /* Return the Option Byte program Status */
00847   return status;
00848 }
00849 
00850 /**
00851   * @brief  Sets or resets the SRAM parity.
00852   * @param  OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
00853   *         This parameter can be one of the following values:
00854   *             @arg OB_SRAM_PARITY_SET: Set SRAM parity.
00855   *             @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
00856   * @retval None
00857   */
00858 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
00859 {
00860   FLASH_Status status = FLASH_COMPLETE; 
00861 
00862   /* Check the parameters */
00863   assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
00864 
00865   /* Wait for last operation to be completed */
00866   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00867   
00868   if(status == FLASH_COMPLETE)
00869   {  
00870     /* Enable the Option Bytes Programming operation */
00871     FLASH->CR |= FLASH_CR_OPTPG; 
00872 
00873     OB->USER = OB_SRAM_Parity | 0xBF;
00874   
00875     /* Wait for last operation to be completed */
00876     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00877 
00878     if(status != FLASH_TIMEOUT)
00879     {
00880       /* if the program operation is completed, disable the OPTPG Bit */
00881       FLASH->CR &= ~FLASH_CR_OPTPG;
00882     }
00883   }
00884   /* Return the Option Byte program Status */
00885   return status;
00886 }
00887 
00888 /**
00889   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY/ BOOT1 and OB_VDDA_ANALOG.
00890   * @note   To correctly run this function, the FLASH_OB_Unlock() function
00891   *         must be called before.
00892   * @note   Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
00893   *         (recommended to protect the FLASH memory against possible unwanted operation)   
00894   * @param  OB_USER: Selects all user option bytes
00895   *   This parameter is a combination of the following values:
00896   *     @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
00897   *     @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
00898   *     @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
00899   *     @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
00900   *     @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
00901   * @retval FLASH Status: The returned value can be: 
00902   * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00903   */
00904 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
00905 {
00906   FLASH_Status status = FLASH_COMPLETE; 
00907 
00908   /* Authorize the small information block programming */
00909   FLASH->OPTKEYR = FLASH_KEY1;
00910   FLASH->OPTKEYR = FLASH_KEY2;
00911   
00912   /* Wait for last operation to be completed */
00913   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00914   
00915   if(status == FLASH_COMPLETE)
00916   {  
00917     /* Enable the Option Bytes Programming operation */
00918     FLASH->CR |= FLASH_CR_OPTPG; 
00919            
00920       OB->USER = OB_USER | 0x88;
00921   
00922     /* Wait for last operation to be completed */
00923     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00924 
00925     if(status != FLASH_TIMEOUT)
00926     {
00927       /* if the program operation is completed, disable the OPTPG Bit */
00928       FLASH->CR &= ~FLASH_CR_OPTPG;
00929     }
00930   }    
00931   /* Return the Option Byte program Status */
00932   return status;
00933 
00934 }
00935 
00936 /**
00937   * @brief  Programs a half word at a specified Option Byte Data address.
00938   * @note    To correctly run this function, the FLASH_OB_Unlock() function
00939   *           must be called before.
00940   *          Call the FLASH_OB_Lock() to disable the flash control register access and the option bytes 
00941   *          (recommended to protect the FLASH memory against possible unwanted operation)
00942   * @param  Address: specifies the address to be programmed.
00943   *   This parameter can be 0x1FFFF804 or 0x1FFFF806. 
00944   * @param  Data: specifies the data to be programmed.
00945   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00946   *         FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00947   */
00948 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
00949 {
00950   FLASH_Status status = FLASH_COMPLETE;
00951   /* Check the parameters */
00952   assert_param(IS_OB_DATA_ADDRESS(Address));
00953   status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00954 
00955   if(status == FLASH_COMPLETE)
00956   {
00957     /* Enables the Option Bytes Programming operation */
00958     FLASH->CR |= FLASH_CR_OPTPG; 
00959     *(__IO uint16_t*)Address = Data;
00960     
00961     /* Wait for last operation to be completed */
00962     status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
00963     
00964     if(status != FLASH_TIMEOUT)
00965     {
00966       /* If the program operation is completed, disable the OPTPG Bit */
00967       FLASH->CR &= ~FLASH_CR_OPTPG;
00968     }
00969   }
00970   /* Return the Option Byte Data Program Status */
00971   return status;
00972 }
00973 
00974 /**
00975   * @brief  Returns the FLASH User Option Bytes values.
00976   * @param  None
00977   * @retval The FLASH User Option Bytes .
00978   */
00979 uint8_t FLASH_OB_GetUser(void)
00980 {
00981   /* Return the User Option Byte */
00982   return (uint8_t)(FLASH->OBR >> 8);
00983 }
00984 
00985 /**
00986   * @brief  Returns the FLASH Write Protection Option Bytes value.
00987   * @param  None
00988   * @retval The FLASH Write Protection Option Bytes value
00989   */
00990 uint32_t FLASH_OB_GetWRP(void)
00991 {
00992   /* Return the FLASH write protection Register value */
00993   return (uint32_t)(FLASH->WRPR);
00994 }
00995 
00996 /**
00997   * @brief  Checks whether the FLASH Read out Protection Status is set or not.
00998   * @param  None
00999   * @retval FLASH ReadOut Protection Status(SET or RESET)
01000   */
01001 FlagStatus FLASH_OB_GetRDP(void)
01002 {
01003   FlagStatus readstatus = RESET;
01004   
01005   if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
01006   {
01007     readstatus = SET;
01008   }
01009   else
01010   {
01011     readstatus = RESET;
01012   }
01013   return readstatus;
01014 }
01015 
01016 /**
01017   * @}
01018   */
01019 
01020 /** @defgroup FLASH_Group4 Interrupts and flags management functions
01021  *  @brief   Interrupts and flags management functions
01022  *
01023 @verbatim   
01024  ===============================================================================
01025              ##### Interrupts and flags management functions #####
01026  ===============================================================================  
01027 
01028 @endverbatim
01029   * @{
01030   */
01031 
01032 /**
01033   * @brief  Enables or disables the specified FLASH interrupts.
01034   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or 
01035   *         disabled.
01036   *   This parameter can be any combination of the following values:     
01037   *     @arg FLASH_IT_EOP: FLASH end of programming Interrupt
01038   *     @arg FLASH_IT_ERR: FLASH Error Interrupt 
01039   * @retval None 
01040   */
01041 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
01042 {
01043   /* Check the parameters */
01044   assert_param(IS_FLASH_IT(FLASH_IT)); 
01045   assert_param(IS_FUNCTIONAL_STATE(NewState));
01046   
01047   if(NewState != DISABLE)
01048   {
01049     /* Enable the interrupt sources */
01050     FLASH->CR |= FLASH_IT;
01051   }
01052   else
01053   {
01054     /* Disable the interrupt sources */
01055     FLASH->CR &= ~(uint32_t)FLASH_IT;
01056   }
01057 }
01058 
01059 /**
01060   * @brief  Checks whether the specified FLASH flag is set or not.
01061   * @param  FLASH_FLAG: specifies the FLASH flag to check.
01062   *   This parameter can be one of the following values:
01063   *     @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag 
01064   *     @arg FLASH_FLAG_PGERR: FLASH Programming error flag 
01065   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
01066   *     @arg FLASH_FLAG_EOP: FLASH End of Programming flag        
01067   * @retval The new state of FLASH_FLAG (SET or RESET).
01068   */
01069 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
01070 {
01071   FlagStatus bitstatus = RESET;
01072 
01073   /* Check the parameters */
01074   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
01075 
01076   if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
01077   {
01078     bitstatus = SET;
01079   }
01080   else
01081   {
01082     bitstatus = RESET;
01083   }
01084   /* Return the new state of FLASH_FLAG (SET or RESET) */
01085   return bitstatus; 
01086 }
01087 
01088 /**
01089   * @brief  Clears the FLASH's pending flags.
01090   * @param  FLASH_FLAG: specifies the FLASH flags to clear.
01091   *   This parameter can be any combination of the following values:
01092   *     @arg FLASH_FLAG_PGERR: FLASH Programming error flag 
01093   *     @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
01094   *     @arg FLASH_FLAG_EOP: FLASH End of Programming flag                
01095   * @retval None
01096   */
01097 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
01098 {
01099   /* Check the parameters */
01100   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
01101   
01102   /* Clear the flags */
01103   FLASH->SR = FLASH_FLAG;
01104 }
01105 
01106 /**
01107   * @brief  Returns the FLASH Status.
01108   * @param  None
01109   * @retval FLASH Status: The returned value can be: 
01110   *         FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
01111   */
01112 FLASH_Status FLASH_GetStatus(void)
01113 {
01114   FLASH_Status FLASHstatus = FLASH_COMPLETE;
01115   
01116   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) 
01117   {
01118     FLASHstatus = FLASH_BUSY;
01119   }
01120   else 
01121   {  
01122     if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
01123     { 
01124       FLASHstatus = FLASH_ERROR_WRP;
01125     }
01126     else 
01127     {
01128       if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
01129       {
01130         FLASHstatus = FLASH_ERROR_PROGRAM; 
01131       }
01132       else
01133       {
01134         FLASHstatus = FLASH_COMPLETE;
01135       }
01136     }
01137   }
01138   /* Return the FLASH Status */
01139   return FLASHstatus;
01140 }
01141 
01142 /**
01143   * @brief  Waits for a FLASH operation to complete or a TIMEOUT to occur.
01144   * @param  Timeout: FLASH programming Timeout
01145   * @retval FLASH Status: The returned value can be: FLASH_BUSY, 
01146   *         FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01147   */
01148 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
01149 { 
01150   FLASH_Status status = FLASH_COMPLETE;
01151    
01152   /* Check for the FLASH Status */
01153   status = FLASH_GetStatus();
01154   
01155   /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
01156   while((status == FLASH_BUSY) && (Timeout != 0x00))
01157   {
01158     status = FLASH_GetStatus();
01159     Timeout--;
01160   }
01161   
01162   if(Timeout == 0x00 )
01163   {
01164     status = FLASH_TIMEOUT;
01165   }
01166   /* Return the operation status */
01167   return status;
01168 }
01169 
01170 /**
01171   * @}
01172   */ 
01173 
01174 /**
01175   * @}
01176   */ 
01177 
01178 /**
01179   * @}
01180   */ 
01181 
01182 /**
01183   * @}
01184   */
01185 
01186 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/