stm32f10x_flash.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f10x_flash.c
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   This file provides all the FLASH firmware functions.
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x_flash.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Driver
00025   * @{
00026   */
00027 
00028 /** @defgroup FLASH 
00029   * @brief FLASH driver modules
00030   * @{
00031   */ 
00032 
00033 /** @defgroup FLASH_Private_TypesDefinitions
00034   * @{
00035   */
00036 
00037 /**
00038   * @}
00039   */ 
00040 
00041 /** @defgroup FLASH_Private_Defines
00042   * @{
00043   */ 
00044 
00045 /* Flash Access Control Register bits */
00046 #define ACR_LATENCY_Mask         ((uint32_t)0x00000038)
00047 #define ACR_HLFCYA_Mask          ((uint32_t)0xFFFFFFF7)
00048 #define ACR_PRFTBE_Mask          ((uint32_t)0xFFFFFFEF)
00049 
00050 /* Flash Access Control Register bits */
00051 #define ACR_PRFTBS_Mask          ((uint32_t)0x00000020) 
00052 
00053 /* Flash Control Register bits */
00054 #define CR_PG_Set                ((uint32_t)0x00000001)
00055 #define CR_PG_Reset              ((uint32_t)0x00001FFE) 
00056 #define CR_PER_Set               ((uint32_t)0x00000002)
00057 #define CR_PER_Reset             ((uint32_t)0x00001FFD)
00058 #define CR_MER_Set               ((uint32_t)0x00000004)
00059 #define CR_MER_Reset             ((uint32_t)0x00001FFB)
00060 #define CR_OPTPG_Set             ((uint32_t)0x00000010)
00061 #define CR_OPTPG_Reset           ((uint32_t)0x00001FEF)
00062 #define CR_OPTER_Set             ((uint32_t)0x00000020)
00063 #define CR_OPTER_Reset           ((uint32_t)0x00001FDF)
00064 #define CR_STRT_Set              ((uint32_t)0x00000040)
00065 #define CR_LOCK_Set              ((uint32_t)0x00000080)
00066 
00067 /* FLASH Mask */
00068 #define RDPRT_Mask               ((uint32_t)0x00000002)
00069 #define WRP0_Mask                ((uint32_t)0x000000FF)
00070 #define WRP1_Mask                ((uint32_t)0x0000FF00)
00071 #define WRP2_Mask                ((uint32_t)0x00FF0000)
00072 #define WRP3_Mask                ((uint32_t)0xFF000000)
00073 #define OB_USER_BFB2             ((uint16_t)0x0008)
00074 
00075 /* FLASH Keys */
00076 #define RDP_Key                  ((uint16_t)0x00A5)
00077 #define FLASH_KEY1               ((uint32_t)0x45670123)
00078 #define FLASH_KEY2               ((uint32_t)0xCDEF89AB)
00079 
00080 /* FLASH BANK address */
00081 #define FLASH_BANK1_END_ADDRESS   ((uint32_t)0x807FFFF)
00082 
00083 /* Delay definition */   
00084 #define EraseTimeout          ((uint32_t)0x000B0000)
00085 #define ProgramTimeout        ((uint32_t)0x00002000)
00086 /**
00087   * @}
00088   */ 
00089 
00090 /** @defgroup FLASH_Private_Macros
00091   * @{
00092   */
00093 
00094 /**
00095   * @}
00096   */ 
00097 
00098 /** @defgroup FLASH_Private_Variables
00099   * @{
00100   */
00101 
00102 /**
00103   * @}
00104   */ 
00105 
00106 /** @defgroup FLASH_Private_FunctionPrototypes
00107   * @{
00108   */
00109   
00110 /**
00111   * @}
00112   */
00113 
00114 /** @defgroup FLASH_Private_Functions
00115   * @{
00116   */
00117 
00118 /**
00119 @code  
00120  
00121  This driver provides functions to configure and program the Flash memory of all STM32F10x devices,
00122  including the latest STM32F10x_XL density devices. 
00123 
00124  STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:
00125     - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)
00126     - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)
00127  While other STM32F10x devices features only one bank with memory up to 512 Kbytes.
00128 
00129  In version V3.3.0, some functions were updated and new ones were added to support
00130  STM32F10x_XL devices. Thus some functions manages all devices, while other are 
00131  dedicated for XL devices only.
00132  
00133  The table below presents the list of available functions depending on the used STM32F10x devices.  
00134       
00135    ***************************************************
00136    * Legacy functions used for all STM32F10x devices *
00137    ***************************************************
00138    +----------------------------------------------------------------------------------------------------------------------------------+
00139    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |
00140    |                                    |   devices  |  devices      |                                                                |
00141    |----------------------------------------------------------------------------------------------------------------------------------|
00142    |FLASH_SetLatency                    |    Yes     |      Yes      | No change                                                      |
00143    |----------------------------------------------------------------------------------------------------------------------------------|
00144    |FLASH_HalfCycleAccessCmd            |    Yes     |      Yes      | No change                                                      |
00145    |----------------------------------------------------------------------------------------------------------------------------------|
00146    |FLASH_PrefetchBufferCmd             |    Yes     |      Yes      | No change                                                      |
00147    |----------------------------------------------------------------------------------------------------------------------------------|
00148    |FLASH_Unlock                        |    Yes     |      Yes      | - For STM32F10X_XL devices: unlock Bank1 and Bank2.            |
00149    |                                    |            |               | - For other devices: unlock Bank1 and it is equivalent         |
00150    |                                    |            |               |   to FLASH_UnlockBank1 function.                               |
00151    |----------------------------------------------------------------------------------------------------------------------------------|
00152    |FLASH_Lock                          |    Yes     |      Yes      | - For STM32F10X_XL devices: lock Bank1 and Bank2.              |
00153    |                                    |            |               | - For other devices: lock Bank1 and it is equivalent           |
00154    |                                    |            |               |   to FLASH_LockBank1 function.                                 |
00155    |----------------------------------------------------------------------------------------------------------------------------------|
00156    |FLASH_ErasePage                     |    Yes     |      Yes      | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2    |
00157    |                                    |            |               | - For other devices: erase a page in Bank1                     |
00158    |----------------------------------------------------------------------------------------------------------------------------------|
00159    |FLASH_EraseAllPages                 |    Yes     |      Yes      | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |
00160    |                                    |            |               | - For other devices: erase all pages in Bank1                  |
00161    |----------------------------------------------------------------------------------------------------------------------------------|
00162    |FLASH_EraseOptionBytes              |    Yes     |      Yes      | No change                                                      |
00163    |----------------------------------------------------------------------------------------------------------------------------------|
00164    |FLASH_ProgramWord                   |    Yes     |      Yes      | Updated to program up to 1MByte (depending on the used device) |
00165    |----------------------------------------------------------------------------------------------------------------------------------|
00166    |FLASH_ProgramHalfWord               |    Yes     |      Yes      | Updated to program up to 1MByte (depending on the used device) |
00167    |----------------------------------------------------------------------------------------------------------------------------------|
00168    |FLASH_ProgramOptionByteData         |    Yes     |      Yes      | No change                                                      |
00169    |----------------------------------------------------------------------------------------------------------------------------------|
00170    |FLASH_EnableWriteProtection         |    Yes     |      Yes      | No change                                                      |
00171    |----------------------------------------------------------------------------------------------------------------------------------|
00172    |FLASH_ReadOutProtection             |    Yes     |      Yes      | No change                                                      |
00173    |----------------------------------------------------------------------------------------------------------------------------------|
00174    |FLASH_UserOptionByteConfig          |    Yes     |      Yes      | No change                                                      |
00175    |----------------------------------------------------------------------------------------------------------------------------------|
00176    |FLASH_GetUserOptionByte             |    Yes     |      Yes      | No change                                                      |
00177    |----------------------------------------------------------------------------------------------------------------------------------|
00178    |FLASH_GetWriteProtectionOptionByte  |    Yes     |      Yes      | No change                                                      |
00179    |----------------------------------------------------------------------------------------------------------------------------------|
00180    |FLASH_GetReadOutProtectionStatus    |    Yes     |      Yes      | No change                                                      |
00181    |----------------------------------------------------------------------------------------------------------------------------------|
00182    |FLASH_GetPrefetchBufferStatus       |    Yes     |      Yes      | No change                                                      |
00183    |----------------------------------------------------------------------------------------------------------------------------------|
00184    |FLASH_ITConfig                      |    Yes     |      Yes      | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|
00185    |                                    |            |               | - For other devices: enable Bank1's interrupts                 |
00186    |----------------------------------------------------------------------------------------------------------------------------------|
00187    |FLASH_GetFlagStatus                 |    Yes     |      Yes      | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|
00188    |                                    |            |               | - For other devices: return Bank1's flag status                |
00189    |----------------------------------------------------------------------------------------------------------------------------------|
00190    |FLASH_ClearFlag                     |    Yes     |      Yes      | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag       |
00191    |                                    |            |               | - For other devices: clear Bank1's flag                        |
00192    |----------------------------------------------------------------------------------------------------------------------------------|
00193    |FLASH_GetStatus                     |    Yes     |      Yes      | - Return the status of Bank1 (for all devices)                 |
00194    |                                    |            |               |   equivalent to FLASH_GetBank1Status function                  |
00195    |----------------------------------------------------------------------------------------------------------------------------------|
00196    |FLASH_WaitForLastOperation          |    Yes     |      Yes      | - Wait for Bank1 last operation (for all devices)              |
00197    |                                    |            |               |   equivalent to: FLASH_WaitForLastBank1Operation function      |
00198    +----------------------------------------------------------------------------------------------------------------------------------+
00199 
00200    ************************************************************************************************************************
00201    * New functions used for all STM32F10x devices to manage Bank1:                                                        *
00202    *   - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *
00203    *   - For other devices, these functions are optional (covered by functions listed above)                              *
00204    ************************************************************************************************************************
00205    +----------------------------------------------------------------------------------------------------------------------------------+
00206    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |
00207    |                                    |   devices  |  devices      |                                                                |
00208    |----------------------------------------------------------------------------------------------------------------------------------|
00209    | FLASH_UnlockBank1                  |    Yes     |      Yes      | - Unlock Bank1                                                 |
00210    |----------------------------------------------------------------------------------------------------------------------------------|
00211    |FLASH_LockBank1                     |    Yes     |      Yes      | - Lock Bank1                                                   |
00212    |----------------------------------------------------------------------------------------------------------------------------------|
00213    | FLASH_EraseAllBank1Pages           |    Yes     |      Yes      | - Erase all pages in Bank1                                     |
00214    |----------------------------------------------------------------------------------------------------------------------------------|
00215    | FLASH_GetBank1Status               |    Yes     |      Yes      | - Return the status of Bank1                                   |
00216    |----------------------------------------------------------------------------------------------------------------------------------|
00217    | FLASH_WaitForLastBank1Operation    |    Yes     |      Yes      | - Wait for Bank1 last operation                                |
00218    +----------------------------------------------------------------------------------------------------------------------------------+
00219 
00220    *****************************************************************************
00221    * New Functions used only with STM32F10x_XL density devices to manage Bank2 *
00222    *****************************************************************************
00223    +----------------------------------------------------------------------------------------------------------------------------------+
00224    |       Functions prototypes         |STM32F10x_XL|Other STM32F10x|    Comments                                                    |
00225    |                                    |   devices  |  devices      |                                                                |
00226    |----------------------------------------------------------------------------------------------------------------------------------|
00227    | FLASH_UnlockBank2                  |    Yes     |      No       | - Unlock Bank2                                                 |
00228    |----------------------------------------------------------------------------------------------------------------------------------|
00229    |FLASH_LockBank2                     |    Yes     |      No       | - Lock Bank2                                                   |
00230    |----------------------------------------------------------------------------------------------------------------------------------|
00231    | FLASH_EraseAllBank2Pages           |    Yes     |      No       | - Erase all pages in Bank2                                     |
00232    |----------------------------------------------------------------------------------------------------------------------------------|
00233    | FLASH_GetBank2Status               |    Yes     |      No       | - Return the status of Bank2                                   |
00234    |----------------------------------------------------------------------------------------------------------------------------------|
00235    | FLASH_WaitForLastBank2Operation    |    Yes     |      No       | - Wait for Bank2 last operation                                |
00236    |----------------------------------------------------------------------------------------------------------------------------------|
00237    | FLASH_BootConfig                   |    Yes     |      No       | - Configure to boot from Bank1 or Bank2                        |
00238    +----------------------------------------------------------------------------------------------------------------------------------+
00239 @endcode
00240 */
00241 
00242 
00243 /**
00244   * @brief  Sets the code latency value.
00245   * @note   This function can be used for all STM32F10x devices.
00246   * @param  FLASH_Latency: specifies the FLASH Latency value.
00247   *   This parameter can be one of the following values:
00248   *     @arg FLASH_Latency_0: FLASH Zero Latency cycle
00249   *     @arg FLASH_Latency_1: FLASH One Latency cycle
00250   *     @arg FLASH_Latency_2: FLASH Two Latency cycles
00251   * @retval None
00252   */
00253 void FLASH_SetLatency(uint32_t FLASH_Latency)
00254 {
00255   uint32_t tmpreg = 0;
00256   
00257   /* Check the parameters */
00258   assert_param(IS_FLASH_LATENCY(FLASH_Latency));
00259   
00260   /* Read the ACR register */
00261   tmpreg = FLASH->ACR;  
00262   
00263   /* Sets the Latency value */
00264   tmpreg &= ACR_LATENCY_Mask;
00265   tmpreg |= FLASH_Latency;
00266   
00267   /* Write the ACR register */
00268   FLASH->ACR = tmpreg;
00269 }
00270 
00271 /**
00272   * @brief  Enables or disables the Half cycle flash access.
00273   * @note   This function can be used for all STM32F10x devices.
00274   * @param  FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.
00275   *   This parameter can be one of the following values:
00276   *     @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
00277   *     @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
00278   * @retval None
00279   */
00280 void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)
00281 {
00282   /* Check the parameters */
00283   assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
00284   
00285   /* Enable or disable the Half cycle access */
00286   FLASH->ACR &= ACR_HLFCYA_Mask;
00287   FLASH->ACR |= FLASH_HalfCycleAccess;
00288 }
00289 
00290 /**
00291   * @brief  Enables or disables the Prefetch Buffer.
00292   * @note   This function can be used for all STM32F10x devices.
00293   * @param  FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
00294   *   This parameter can be one of the following values:
00295   *     @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
00296   *     @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
00297   * @retval None
00298   */
00299 void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)
00300 {
00301   /* Check the parameters */
00302   assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
00303   
00304   /* Enable or disable the Prefetch Buffer */
00305   FLASH->ACR &= ACR_PRFTBE_Mask;
00306   FLASH->ACR |= FLASH_PrefetchBuffer;
00307 }
00308 
00309 /**
00310   * @brief  Unlocks the FLASH Program Erase Controller.
00311   * @note   This function can be used for all STM32F10x devices.
00312   *         - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.
00313   *         - For all other devices it unlocks Bank1 and it is equivalent 
00314   *           to FLASH_UnlockBank1 function.. 
00315   * @param  None
00316   * @retval None
00317   */
00318 void FLASH_Unlock(void)
00319 {
00320   /* Authorize the FPEC of Bank1 Access */
00321   FLASH->KEYR = FLASH_KEY1;
00322   FLASH->KEYR = FLASH_KEY2;
00323 
00324 #ifdef STM32F10X_XL
00325   /* Authorize the FPEC of Bank2 Access */
00326   FLASH->KEYR2 = FLASH_KEY1;
00327   FLASH->KEYR2 = FLASH_KEY2;
00328 #endif /* STM32F10X_XL */
00329 }
00330 /**
00331   * @brief  Unlocks the FLASH Bank1 Program Erase Controller.
00332   * @note   This function can be used for all STM32F10x devices.
00333   *         - For STM32F10X_XL devices this function unlocks Bank1.
00334   *         - For all other devices it unlocks Bank1 and it is 
00335   *           equivalent to FLASH_Unlock function.
00336   * @param  None
00337   * @retval None
00338   */
00339 void FLASH_UnlockBank1(void)
00340 {
00341   /* Authorize the FPEC of Bank1 Access */
00342   FLASH->KEYR = FLASH_KEY1;
00343   FLASH->KEYR = FLASH_KEY2;
00344 }
00345 
00346 #ifdef STM32F10X_XL
00347 /**
00348   * @brief  Unlocks the FLASH Bank2 Program Erase Controller.
00349   * @note   This function can be used only for STM32F10X_XL density devices.
00350   * @param  None
00351   * @retval None
00352   */
00353 void FLASH_UnlockBank2(void)
00354 {
00355   /* Authorize the FPEC of Bank2 Access */
00356   FLASH->KEYR2 = FLASH_KEY1;
00357   FLASH->KEYR2 = FLASH_KEY2;
00358 
00359 }
00360 #endif /* STM32F10X_XL */
00361 
00362 /**
00363   * @brief  Locks the FLASH Program Erase Controller.
00364   * @note   This function can be used for all STM32F10x devices.
00365   *         - For STM32F10X_XL devices this function Locks Bank1 and Bank2.
00366   *         - For all other devices it Locks Bank1 and it is equivalent 
00367   *           to FLASH_LockBank1 function.
00368   * @param  None
00369   * @retval None
00370   */
00371 void FLASH_Lock(void)
00372 {
00373   /* Set the Lock Bit to lock the FPEC and the CR of  Bank1 */
00374   FLASH->CR |= CR_LOCK_Set;
00375 
00376 #ifdef STM32F10X_XL
00377   /* Set the Lock Bit to lock the FPEC and the CR of  Bank2 */
00378   FLASH->CR2 |= CR_LOCK_Set;
00379 #endif /* STM32F10X_XL */
00380 }
00381 
00382 /**
00383   * @brief  Locks the FLASH Bank1 Program Erase Controller.
00384   * @note   this function can be used for all STM32F10x devices.
00385   *         - For STM32F10X_XL devices this function Locks Bank1.
00386   *         - For all other devices it Locks Bank1 and it is equivalent 
00387   *           to FLASH_Lock function.
00388   * @param  None
00389   * @retval None
00390   */
00391 void FLASH_LockBank1(void)
00392 {
00393   /* Set the Lock Bit to lock the FPEC and the CR of  Bank1 */
00394   FLASH->CR |= CR_LOCK_Set;
00395 }
00396 
00397 #ifdef STM32F10X_XL
00398 /**
00399   * @brief  Locks the FLASH Bank2 Program Erase Controller.
00400   * @note   This function can be used only for STM32F10X_XL density devices.
00401   * @param  None
00402   * @retval None
00403   */
00404 void FLASH_LockBank2(void)
00405 {
00406   /* Set the Lock Bit to lock the FPEC and the CR of  Bank2 */
00407   FLASH->CR2 |= CR_LOCK_Set;
00408 }
00409 #endif /* STM32F10X_XL */
00410 
00411 /**
00412   * @brief  Erases a specified FLASH page.
00413   * @note   This function can be used for all STM32F10x devices.
00414   * @param  Page_Address: The page address to be erased.
00415   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
00416   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00417   */
00418 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
00419 {
00420   FLASH_Status status = FLASH_COMPLETE;
00421   /* Check the parameters */
00422   assert_param(IS_FLASH_ADDRESS(Page_Address));
00423 
00424 #ifdef STM32F10X_XL
00425   if(Page_Address < FLASH_BANK1_END_ADDRESS)  
00426   {
00427     /* Wait for last operation to be completed */
00428     status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00429     if(status == FLASH_COMPLETE)
00430     { 
00431       /* if the previous operation is completed, proceed to erase the page */
00432       FLASH->CR|= CR_PER_Set;
00433       FLASH->AR = Page_Address; 
00434       FLASH->CR|= CR_STRT_Set;
00435     
00436       /* Wait for last operation to be completed */
00437       status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00438 
00439       /* Disable the PER Bit */
00440       FLASH->CR &= CR_PER_Reset;
00441     }
00442   }
00443   else
00444   {
00445     /* Wait for last operation to be completed */
00446     status = FLASH_WaitForLastBank2Operation(EraseTimeout);
00447     if(status == FLASH_COMPLETE)
00448     { 
00449       /* if the previous operation is completed, proceed to erase the page */
00450       FLASH->CR2|= CR_PER_Set;
00451       FLASH->AR2 = Page_Address; 
00452       FLASH->CR2|= CR_STRT_Set;
00453     
00454       /* Wait for last operation to be completed */
00455       status = FLASH_WaitForLastBank2Operation(EraseTimeout);
00456       
00457       /* Disable the PER Bit */
00458       FLASH->CR2 &= CR_PER_Reset;
00459     }
00460   }
00461 #else
00462   /* Wait for last operation to be completed */
00463   status = FLASH_WaitForLastOperation(EraseTimeout);
00464   
00465   if(status == FLASH_COMPLETE)
00466   { 
00467     /* if the previous operation is completed, proceed to erase the page */
00468     FLASH->CR|= CR_PER_Set;
00469     FLASH->AR = Page_Address; 
00470     FLASH->CR|= CR_STRT_Set;
00471     
00472     /* Wait for last operation to be completed */
00473     status = FLASH_WaitForLastOperation(EraseTimeout);
00474     
00475     /* Disable the PER Bit */
00476     FLASH->CR &= CR_PER_Reset;
00477   }
00478 #endif /* STM32F10X_XL */
00479 
00480   /* Return the Erase Status */
00481   return status;
00482 }
00483 
00484 /**
00485   * @brief  Erases all FLASH pages.
00486   * @note   This function can be used for all STM32F10x devices.
00487   * @param  None
00488   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00489   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00490   */
00491 FLASH_Status FLASH_EraseAllPages(void)
00492 {
00493   FLASH_Status status = FLASH_COMPLETE;
00494 
00495 #ifdef STM32F10X_XL
00496   /* Wait for last operation to be completed */
00497   status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00498   
00499   if(status == FLASH_COMPLETE)
00500   {
00501     /* if the previous operation is completed, proceed to erase all pages */
00502      FLASH->CR |= CR_MER_Set;
00503      FLASH->CR |= CR_STRT_Set;
00504     
00505     /* Wait for last operation to be completed */
00506     status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00507     
00508     /* Disable the MER Bit */
00509     FLASH->CR &= CR_MER_Reset;
00510   }    
00511   if(status == FLASH_COMPLETE)
00512   {
00513     /* if the previous operation is completed, proceed to erase all pages */
00514      FLASH->CR2 |= CR_MER_Set;
00515      FLASH->CR2 |= CR_STRT_Set;
00516     
00517     /* Wait for last operation to be completed */
00518     status = FLASH_WaitForLastBank2Operation(EraseTimeout);
00519     
00520     /* Disable the MER Bit */
00521     FLASH->CR2 &= CR_MER_Reset;
00522   }
00523 #else
00524   /* Wait for last operation to be completed */
00525   status = FLASH_WaitForLastOperation(EraseTimeout);
00526   if(status == FLASH_COMPLETE)
00527   {
00528     /* if the previous operation is completed, proceed to erase all pages */
00529      FLASH->CR |= CR_MER_Set;
00530      FLASH->CR |= CR_STRT_Set;
00531     
00532     /* Wait for last operation to be completed */
00533     status = FLASH_WaitForLastOperation(EraseTimeout);
00534 
00535     /* Disable the MER Bit */
00536     FLASH->CR &= CR_MER_Reset;
00537   }
00538 #endif /* STM32F10X_XL */
00539 
00540   /* Return the Erase Status */
00541   return status;
00542 }
00543 
00544 /**
00545   * @brief  Erases all Bank1 FLASH pages.
00546   * @note   This function can be used for all STM32F10x devices.
00547   *         - For STM32F10X_XL devices this function erases all Bank1 pages.
00548   *         - For all other devices it erases all Bank1 pages and it is equivalent 
00549   *           to FLASH_EraseAllPages function.
00550   * @param  None
00551   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00552   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00553   */
00554 FLASH_Status FLASH_EraseAllBank1Pages(void)
00555 {
00556   FLASH_Status status = FLASH_COMPLETE;
00557   /* Wait for last operation to be completed */
00558   status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00559   
00560   if(status == FLASH_COMPLETE)
00561   {
00562     /* if the previous operation is completed, proceed to erase all pages */
00563      FLASH->CR |= CR_MER_Set;
00564      FLASH->CR |= CR_STRT_Set;
00565     
00566     /* Wait for last operation to be completed */
00567     status = FLASH_WaitForLastBank1Operation(EraseTimeout);
00568     
00569     /* Disable the MER Bit */
00570     FLASH->CR &= CR_MER_Reset;
00571   }    
00572   /* Return the Erase Status */
00573   return status;
00574 }
00575 
00576 #ifdef STM32F10X_XL
00577 /**
00578   * @brief  Erases all Bank2 FLASH pages.
00579   * @note   This function can be used only for STM32F10x_XL density devices.
00580   * @param  None
00581   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00582   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00583   */
00584 FLASH_Status FLASH_EraseAllBank2Pages(void)
00585 {
00586   FLASH_Status status = FLASH_COMPLETE;
00587   /* Wait for last operation to be completed */
00588   status = FLASH_WaitForLastBank2Operation(EraseTimeout);
00589   
00590   if(status == FLASH_COMPLETE)
00591   {
00592     /* if the previous operation is completed, proceed to erase all pages */
00593      FLASH->CR2 |= CR_MER_Set;
00594      FLASH->CR2 |= CR_STRT_Set;
00595     
00596     /* Wait for last operation to be completed */
00597     status = FLASH_WaitForLastBank2Operation(EraseTimeout);
00598 
00599     /* Disable the MER Bit */
00600     FLASH->CR2 &= CR_MER_Reset;
00601   }    
00602   /* Return the Erase Status */
00603   return status;
00604 }
00605 #endif /* STM32F10X_XL */
00606 
00607 /**
00608   * @brief  Erases the FLASH option bytes.
00609   * @note   This functions erases all option bytes except the Read protection (RDP). 
00610   * @note   This function can be used for all STM32F10x devices.
00611   * @param  None
00612   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00613   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00614   */
00615 FLASH_Status FLASH_EraseOptionBytes(void)
00616 {
00617   uint16_t rdptmp = RDP_Key;
00618 
00619   FLASH_Status status = FLASH_COMPLETE;
00620 
00621   /* Get the actual read protection Option Byte value */ 
00622   if(FLASH_GetReadOutProtectionStatus() != RESET)
00623   {
00624     rdptmp = 0x00;  
00625   }
00626 
00627   /* Wait for last operation to be completed */
00628   status = FLASH_WaitForLastOperation(EraseTimeout);
00629   if(status == FLASH_COMPLETE)
00630   {
00631     /* Authorize the small information block programming */
00632     FLASH->OPTKEYR = FLASH_KEY1;
00633     FLASH->OPTKEYR = FLASH_KEY2;
00634     
00635     /* if the previous operation is completed, proceed to erase the option bytes */
00636     FLASH->CR |= CR_OPTER_Set;
00637     FLASH->CR |= CR_STRT_Set;
00638     /* Wait for last operation to be completed */
00639     status = FLASH_WaitForLastOperation(EraseTimeout);
00640     
00641     if(status == FLASH_COMPLETE)
00642     {
00643       /* if the erase operation is completed, disable the OPTER Bit */
00644       FLASH->CR &= CR_OPTER_Reset;
00645        
00646       /* Enable the Option Bytes Programming operation */
00647       FLASH->CR |= CR_OPTPG_Set;
00648       /* Restore the last read protection Option Byte value */
00649       OB->RDP = (uint16_t)rdptmp; 
00650       /* Wait for last operation to be completed */
00651       status = FLASH_WaitForLastOperation(ProgramTimeout);
00652  
00653       if(status != FLASH_TIMEOUT)
00654       {
00655         /* if the program operation is completed, disable the OPTPG Bit */
00656         FLASH->CR &= CR_OPTPG_Reset;
00657       }
00658     }
00659     else
00660     {
00661       if (status != FLASH_TIMEOUT)
00662       {
00663         /* Disable the OPTPG Bit */
00664         FLASH->CR &= CR_OPTPG_Reset;
00665       }
00666     }  
00667   }
00668   /* Return the erase status */
00669   return status;
00670 }
00671 
00672 /**
00673   * @brief  Programs a word at a specified address.
00674   * @note   This function can be used for all STM32F10x devices.
00675   * @param  Address: specifies the address to be programmed.
00676   * @param  Data: specifies the data to be programmed.
00677   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00678   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
00679   */
00680 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
00681 {
00682   FLASH_Status status = FLASH_COMPLETE;
00683   __IO uint32_t tmp = 0;
00684 
00685   /* Check the parameters */
00686   assert_param(IS_FLASH_ADDRESS(Address));
00687 
00688 #ifdef STM32F10X_XL
00689   if(Address < FLASH_BANK1_END_ADDRESS - 2)
00690   { 
00691     /* Wait for last operation to be completed */
00692     status = FLASH_WaitForLastBank1Operation(ProgramTimeout); 
00693     if(status == FLASH_COMPLETE)
00694     {
00695       /* if the previous operation is completed, proceed to program the new first 
00696         half word */
00697       FLASH->CR |= CR_PG_Set;
00698   
00699       *(__IO uint16_t*)Address = (uint16_t)Data;
00700       /* Wait for last operation to be completed */
00701       status = FLASH_WaitForLastOperation(ProgramTimeout);
00702  
00703       if(status == FLASH_COMPLETE)
00704       {
00705         /* if the previous operation is completed, proceed to program the new second 
00706         half word */
00707         tmp = Address + 2;
00708 
00709         *(__IO uint16_t*) tmp = Data >> 16;
00710     
00711         /* Wait for last operation to be completed */
00712         status = FLASH_WaitForLastOperation(ProgramTimeout);
00713         
00714         /* Disable the PG Bit */
00715         FLASH->CR &= CR_PG_Reset;
00716       }
00717       else
00718       {
00719         /* Disable the PG Bit */
00720         FLASH->CR &= CR_PG_Reset;
00721        }
00722     }
00723   }
00724   else if(Address == (FLASH_BANK1_END_ADDRESS - 1))
00725   {
00726     /* Wait for last operation to be completed */
00727     status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
00728 
00729     if(status == FLASH_COMPLETE)
00730     {
00731       /* if the previous operation is completed, proceed to program the new first 
00732         half word */
00733       FLASH->CR |= CR_PG_Set;
00734   
00735       *(__IO uint16_t*)Address = (uint16_t)Data;
00736 
00737       /* Wait for last operation to be completed */
00738       status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
00739       
00740           /* Disable the PG Bit */
00741       FLASH->CR &= CR_PG_Reset;
00742     }
00743     else
00744     {
00745       /* Disable the PG Bit */
00746       FLASH->CR &= CR_PG_Reset;
00747     }
00748 
00749     /* Wait for last operation to be completed */
00750     status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00751 
00752     if(status == FLASH_COMPLETE)
00753     {
00754       /* if the previous operation is completed, proceed to program the new second 
00755       half word */
00756       FLASH->CR2 |= CR_PG_Set;
00757       tmp = Address + 2;
00758 
00759       *(__IO uint16_t*) tmp = Data >> 16;
00760     
00761       /* Wait for last operation to be completed */
00762       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00763         
00764       /* Disable the PG Bit */
00765       FLASH->CR2 &= CR_PG_Reset;
00766     }
00767     else
00768     {
00769       /* Disable the PG Bit */
00770       FLASH->CR2 &= CR_PG_Reset;
00771     }
00772   }
00773   else
00774   {
00775     /* Wait for last operation to be completed */
00776     status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00777 
00778     if(status == FLASH_COMPLETE)
00779     {
00780       /* if the previous operation is completed, proceed to program the new first 
00781         half word */
00782       FLASH->CR2 |= CR_PG_Set;
00783   
00784       *(__IO uint16_t*)Address = (uint16_t)Data;
00785       /* Wait for last operation to be completed */
00786       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00787  
00788       if(status == FLASH_COMPLETE)
00789       {
00790         /* if the previous operation is completed, proceed to program the new second 
00791         half word */
00792         tmp = Address + 2;
00793 
00794         *(__IO uint16_t*) tmp = Data >> 16;
00795     
00796         /* Wait for last operation to be completed */
00797         status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00798         
00799         /* Disable the PG Bit */
00800         FLASH->CR2 &= CR_PG_Reset;
00801       }
00802       else
00803       {
00804         /* Disable the PG Bit */
00805         FLASH->CR2 &= CR_PG_Reset;
00806       }
00807     }
00808   }
00809 #else
00810   /* Wait for last operation to be completed */
00811   status = FLASH_WaitForLastOperation(ProgramTimeout);
00812   
00813   if(status == FLASH_COMPLETE)
00814   {
00815     /* if the previous operation is completed, proceed to program the new first 
00816     half word */
00817     FLASH->CR |= CR_PG_Set;
00818   
00819     *(__IO uint16_t*)Address = (uint16_t)Data;
00820     /* Wait for last operation to be completed */
00821     status = FLASH_WaitForLastOperation(ProgramTimeout);
00822  
00823     if(status == FLASH_COMPLETE)
00824     {
00825       /* if the previous operation is completed, proceed to program the new second 
00826       half word */
00827       tmp = Address + 2;
00828 
00829       *(__IO uint16_t*) tmp = Data >> 16;
00830     
00831       /* Wait for last operation to be completed */
00832       status = FLASH_WaitForLastOperation(ProgramTimeout);
00833         
00834       /* Disable the PG Bit */
00835       FLASH->CR &= CR_PG_Reset;
00836     }
00837     else
00838     {
00839       /* Disable the PG Bit */
00840       FLASH->CR &= CR_PG_Reset;
00841     }
00842   }         
00843 #endif /* STM32F10X_XL */
00844    
00845   /* Return the Program Status */
00846   return status;
00847 }
00848 
00849 /**
00850   * @brief  Programs a half word at a specified address.
00851   * @note   This function can be used for all STM32F10x devices.
00852   * @param  Address: specifies the address to be programmed.
00853   * @param  Data: specifies the data to be programmed.
00854   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00855   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
00856   */
00857 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
00858 {
00859   FLASH_Status status = FLASH_COMPLETE;
00860   /* Check the parameters */
00861   assert_param(IS_FLASH_ADDRESS(Address));
00862 
00863 #ifdef STM32F10X_XL
00864   /* Wait for last operation to be completed */
00865   status = FLASH_WaitForLastOperation(ProgramTimeout);
00866   
00867   if(Address < FLASH_BANK1_END_ADDRESS)
00868   {
00869     if(status == FLASH_COMPLETE)
00870     {
00871       /* if the previous operation is completed, proceed to program the new data */
00872       FLASH->CR |= CR_PG_Set;
00873   
00874       *(__IO uint16_t*)Address = Data;
00875       /* Wait for last operation to be completed */
00876       status = FLASH_WaitForLastBank1Operation(ProgramTimeout);
00877 
00878       /* Disable the PG Bit */
00879       FLASH->CR &= CR_PG_Reset;
00880     }
00881   }
00882   else
00883   {
00884     if(status == FLASH_COMPLETE)
00885     {
00886       /* if the previous operation is completed, proceed to program the new data */
00887       FLASH->CR2 |= CR_PG_Set;
00888   
00889       *(__IO uint16_t*)Address = Data;
00890       /* Wait for last operation to be completed */
00891       status = FLASH_WaitForLastBank2Operation(ProgramTimeout);
00892 
00893       /* Disable the PG Bit */
00894       FLASH->CR2 &= CR_PG_Reset;
00895     }
00896   }
00897 #else
00898   /* Wait for last operation to be completed */
00899   status = FLASH_WaitForLastOperation(ProgramTimeout);
00900   
00901   if(status == FLASH_COMPLETE)
00902   {
00903     /* if the previous operation is completed, proceed to program the new data */
00904     FLASH->CR |= CR_PG_Set;
00905   
00906     *(__IO uint16_t*)Address = Data;
00907     /* Wait for last operation to be completed */
00908     status = FLASH_WaitForLastOperation(ProgramTimeout);
00909     
00910     /* Disable the PG Bit */
00911     FLASH->CR &= CR_PG_Reset;
00912   } 
00913 #endif  /* STM32F10X_XL */
00914   
00915   /* Return the Program Status */
00916   return status;
00917 }
00918 
00919 /**
00920   * @brief  Programs a half word at a specified Option Byte Data address.
00921   * @note   This function can be used for all STM32F10x devices.
00922   * @param  Address: specifies the address to be programmed.
00923   *   This parameter can be 0x1FFFF804 or 0x1FFFF806. 
00924   * @param  Data: specifies the data to be programmed.
00925   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00926   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. 
00927   */
00928 FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)
00929 {
00930   FLASH_Status status = FLASH_COMPLETE;
00931   /* Check the parameters */
00932   assert_param(IS_OB_DATA_ADDRESS(Address));
00933   status = FLASH_WaitForLastOperation(ProgramTimeout);
00934 
00935   if(status == FLASH_COMPLETE)
00936   {
00937     /* Authorize the small information block programming */
00938     FLASH->OPTKEYR = FLASH_KEY1;
00939     FLASH->OPTKEYR = FLASH_KEY2;
00940     /* Enables the Option Bytes Programming operation */
00941     FLASH->CR |= CR_OPTPG_Set; 
00942     *(__IO uint16_t*)Address = Data;
00943     
00944     /* Wait for last operation to be completed */
00945     status = FLASH_WaitForLastOperation(ProgramTimeout);
00946     if(status != FLASH_TIMEOUT)
00947     {
00948       /* if the program operation is completed, disable the OPTPG Bit */
00949       FLASH->CR &= CR_OPTPG_Reset;
00950     }
00951   }
00952   /* Return the Option Byte Data Program Status */
00953   return status;
00954 }
00955 
00956 /**
00957   * @brief  Write protects the desired pages
00958   * @note   This function can be used for all STM32F10x devices.
00959   * @param  FLASH_Pages: specifies the address of the pages to be write protected.
00960   *   This parameter can be:
00961   *     @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31  
00962   *     @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3
00963   *       and FLASH_WRProt_Pages124to127
00964   *     @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and
00965   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
00966   *     @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and
00967   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127    
00968   *     @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and
00969   *       FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511
00970   *     @arg FLASH_WRProt_AllPages
00971   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
00972   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
00973   */
00974 FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)
00975 {
00976   uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
00977   
00978   FLASH_Status status = FLASH_COMPLETE;
00979   
00980   /* Check the parameters */
00981   assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
00982   
00983   FLASH_Pages = (uint32_t)(~FLASH_Pages);
00984   WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);
00985   WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);
00986   WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);
00987   WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);
00988   
00989   /* Wait for last operation to be completed */
00990   status = FLASH_WaitForLastOperation(ProgramTimeout);
00991   
00992   if(status == FLASH_COMPLETE)
00993   {
00994     /* Authorizes the small information block programming */
00995     FLASH->OPTKEYR = FLASH_KEY1;
00996     FLASH->OPTKEYR = FLASH_KEY2;
00997     FLASH->CR |= CR_OPTPG_Set;
00998     if(WRP0_Data != 0xFF)
00999     {
01000       OB->WRP0 = WRP0_Data;
01001       
01002       /* Wait for last operation to be completed */
01003       status = FLASH_WaitForLastOperation(ProgramTimeout);
01004     }
01005     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
01006     {
01007       OB->WRP1 = WRP1_Data;
01008       
01009       /* Wait for last operation to be completed */
01010       status = FLASH_WaitForLastOperation(ProgramTimeout);
01011     }
01012     if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
01013     {
01014       OB->WRP2 = WRP2_Data;
01015       
01016       /* Wait for last operation to be completed */
01017       status = FLASH_WaitForLastOperation(ProgramTimeout);
01018     }
01019     
01020     if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
01021     {
01022       OB->WRP3 = WRP3_Data;
01023      
01024       /* Wait for last operation to be completed */
01025       status = FLASH_WaitForLastOperation(ProgramTimeout);
01026     }
01027           
01028     if(status != FLASH_TIMEOUT)
01029     {
01030       /* if the program operation is completed, disable the OPTPG Bit */
01031       FLASH->CR &= CR_OPTPG_Reset;
01032     }
01033   } 
01034   /* Return the write protection operation Status */
01035   return status;       
01036 }
01037 
01038 /**
01039   * @brief  Enables or disables the read out protection.
01040   * @note   If the user has already programmed the other option bytes before calling 
01041   *   this function, he must re-program them since this function erases all option bytes.
01042   * @note   This function can be used for all STM32F10x devices.
01043   * @param  Newstate: new state of the ReadOut Protection.
01044   *   This parameter can be: ENABLE or DISABLE.
01045   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
01046   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01047   */
01048 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
01049 {
01050   FLASH_Status status = FLASH_COMPLETE;
01051   /* Check the parameters */
01052   assert_param(IS_FUNCTIONAL_STATE(NewState));
01053   status = FLASH_WaitForLastOperation(EraseTimeout);
01054   if(status == FLASH_COMPLETE)
01055   {
01056     /* Authorizes the small information block programming */
01057     FLASH->OPTKEYR = FLASH_KEY1;
01058     FLASH->OPTKEYR = FLASH_KEY2;
01059     FLASH->CR |= CR_OPTER_Set;
01060     FLASH->CR |= CR_STRT_Set;
01061     /* Wait for last operation to be completed */
01062     status = FLASH_WaitForLastOperation(EraseTimeout);
01063     if(status == FLASH_COMPLETE)
01064     {
01065       /* if the erase operation is completed, disable the OPTER Bit */
01066       FLASH->CR &= CR_OPTER_Reset;
01067       /* Enable the Option Bytes Programming operation */
01068       FLASH->CR |= CR_OPTPG_Set; 
01069       if(NewState != DISABLE)
01070       {
01071         OB->RDP = 0x00;
01072       }
01073       else
01074       {
01075         OB->RDP = RDP_Key;  
01076       }
01077       /* Wait for last operation to be completed */
01078       status = FLASH_WaitForLastOperation(EraseTimeout); 
01079     
01080       if(status != FLASH_TIMEOUT)
01081       {
01082         /* if the program operation is completed, disable the OPTPG Bit */
01083         FLASH->CR &= CR_OPTPG_Reset;
01084       }
01085     }
01086     else 
01087     {
01088       if(status != FLASH_TIMEOUT)
01089       {
01090         /* Disable the OPTER Bit */
01091         FLASH->CR &= CR_OPTER_Reset;
01092       }
01093     }
01094   }
01095   /* Return the protection operation Status */
01096   return status;       
01097 }
01098 
01099 /**
01100   * @brief  Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
01101   * @note   This function can be used for all STM32F10x devices.
01102   * @param  OB_IWDG: Selects the IWDG mode
01103   *   This parameter can be one of the following values:
01104   *     @arg OB_IWDG_SW: Software IWDG selected
01105   *     @arg OB_IWDG_HW: Hardware IWDG selected
01106   * @param  OB_STOP: Reset event when entering STOP mode.
01107   *   This parameter can be one of the following values:
01108   *     @arg OB_STOP_NoRST: No reset generated when entering in STOP
01109   *     @arg OB_STOP_RST: Reset generated when entering in STOP
01110   * @param  OB_STDBY: Reset event when entering Standby mode.
01111   *   This parameter can be one of the following values:
01112   *     @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
01113   *     @arg OB_STDBY_RST: Reset generated when entering in STANDBY
01114   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, 
01115   * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01116   */
01117 FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)
01118 {
01119   FLASH_Status status = FLASH_COMPLETE; 
01120 
01121   /* Check the parameters */
01122   assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
01123   assert_param(IS_OB_STOP_SOURCE(OB_STOP));
01124   assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
01125 
01126   /* Authorize the small information block programming */
01127   FLASH->OPTKEYR = FLASH_KEY1;
01128   FLASH->OPTKEYR = FLASH_KEY2;
01129   
01130   /* Wait for last operation to be completed */
01131   status = FLASH_WaitForLastOperation(ProgramTimeout);
01132   
01133   if(status == FLASH_COMPLETE)
01134   {  
01135     /* Enable the Option Bytes Programming operation */
01136     FLASH->CR |= CR_OPTPG_Set; 
01137            
01138     OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8))); 
01139   
01140     /* Wait for last operation to be completed */
01141     status = FLASH_WaitForLastOperation(ProgramTimeout);
01142     if(status != FLASH_TIMEOUT)
01143     {
01144       /* if the program operation is completed, disable the OPTPG Bit */
01145       FLASH->CR &= CR_OPTPG_Reset;
01146     }
01147   }    
01148   /* Return the Option Byte program Status */
01149   return status;
01150 }
01151 
01152 #ifdef STM32F10X_XL
01153 /**
01154   * @brief  Configures to boot from Bank1 or Bank2.  
01155   * @note   This function can be used only for STM32F10x_XL density devices.
01156   * @param  FLASH_BOOT: select the FLASH Bank to boot from.
01157   *   This parameter can be one of the following values:
01158   *     @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash
01159   *        position and this parameter is selected the device will boot from Bank1(Default).
01160   *     @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash
01161   *        position and this parameter is selected the device will boot from Bank2 or Bank1,
01162   *        depending on the activation of the bank. The active banks are checked in
01163   *        the following order: Bank2, followed by Bank1.
01164   *        The active bank is recognized by the value programmed at the base address
01165   *        of the respective bank (corresponding to the initial stack pointer value
01166   *        in the interrupt vector table).
01167   *        For more information, please refer to AN2606 from www.st.com.    
01168   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, 
01169   * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01170   */
01171 FLASH_Status FLASH_BootConfig(uint16_t FLASH_BOOT)
01172 { 
01173   FLASH_Status status = FLASH_COMPLETE; 
01174   assert_param(IS_FLASH_BOOT(FLASH_BOOT));
01175   /* Authorize the small information block programming */
01176   FLASH->OPTKEYR = FLASH_KEY1;
01177   FLASH->OPTKEYR = FLASH_KEY2;
01178   
01179   /* Wait for last operation to be completed */
01180   status = FLASH_WaitForLastOperation(ProgramTimeout);
01181   
01182   if(status == FLASH_COMPLETE)
01183   {  
01184     /* Enable the Option Bytes Programming operation */
01185     FLASH->CR |= CR_OPTPG_Set; 
01186 
01187     if(FLASH_BOOT == FLASH_BOOT_Bank1)
01188     {
01189       OB->USER |= OB_USER_BFB2;
01190     }
01191     else
01192     {
01193       OB->USER &= (uint16_t)(~(uint16_t)(OB_USER_BFB2));
01194     }
01195     /* Wait for last operation to be completed */
01196     status = FLASH_WaitForLastOperation(ProgramTimeout);
01197     if(status != FLASH_TIMEOUT)
01198     {
01199       /* if the program operation is completed, disable the OPTPG Bit */
01200       FLASH->CR &= CR_OPTPG_Reset;
01201     }
01202   }    
01203   /* Return the Option Byte program Status */
01204   return status;
01205 }
01206 #endif /* STM32F10X_XL */
01207 
01208 /**
01209   * @brief  Returns the FLASH User Option Bytes values.
01210   * @note   This function can be used for all STM32F10x devices.
01211   * @param  None
01212   * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
01213   *   and RST_STDBY(Bit2).
01214   */
01215 uint32_t FLASH_GetUserOptionByte(void)
01216 {
01217   /* Return the User Option Byte */
01218   return (uint32_t)(FLASH->OBR >> 2);
01219 }
01220 
01221 /**
01222   * @brief  Returns the FLASH Write Protection Option Bytes Register value.
01223   * @note   This function can be used for all STM32F10x devices.
01224   * @param  None
01225   * @retval The FLASH Write Protection  Option Bytes Register value
01226   */
01227 uint32_t FLASH_GetWriteProtectionOptionByte(void)
01228 {
01229   /* Return the Falsh write protection Register value */
01230   return (uint32_t)(FLASH->WRPR);
01231 }
01232 
01233 /**
01234   * @brief  Checks whether the FLASH Read Out Protection Status is set or not.
01235   * @note   This function can be used for all STM32F10x devices.
01236   * @param  None
01237   * @retval FLASH ReadOut Protection Status(SET or RESET)
01238   */
01239 FlagStatus FLASH_GetReadOutProtectionStatus(void)
01240 {
01241   FlagStatus readoutstatus = RESET;
01242   if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)
01243   {
01244     readoutstatus = SET;
01245   }
01246   else
01247   {
01248     readoutstatus = RESET;
01249   }
01250   return readoutstatus;
01251 }
01252 
01253 /**
01254   * @brief  Checks whether the FLASH Prefetch Buffer status is set or not.
01255   * @note   This function can be used for all STM32F10x devices.
01256   * @param  None
01257   * @retval FLASH Prefetch Buffer Status (SET or RESET).
01258   */
01259 FlagStatus FLASH_GetPrefetchBufferStatus(void)
01260 {
01261   FlagStatus bitstatus = RESET;
01262   
01263   if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)
01264   {
01265     bitstatus = SET;
01266   }
01267   else
01268   {
01269     bitstatus = RESET;
01270   }
01271   /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
01272   return bitstatus; 
01273 }
01274 
01275 /**
01276   * @brief  Enables or disables the specified FLASH interrupts.
01277   * @note   This function can be used for all STM32F10x devices.
01278   *         - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts
01279               for Bank1 and Bank2.
01280   *         - For other devices it enables or disables the specified FLASH interrupts for Bank1.
01281   * @param  FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.
01282   *   This parameter can be any combination of the following values:
01283   *     @arg FLASH_IT_ERROR: FLASH Error Interrupt
01284   *     @arg FLASH_IT_EOP: FLASH end of operation Interrupt
01285   * @param  NewState: new state of the specified Flash interrupts.
01286   *   This parameter can be: ENABLE or DISABLE.      
01287   * @retval None 
01288   */
01289 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
01290 {
01291 #ifdef STM32F10X_XL
01292   /* Check the parameters */
01293   assert_param(IS_FLASH_IT(FLASH_IT)); 
01294   assert_param(IS_FUNCTIONAL_STATE(NewState));
01295 
01296   if((FLASH_IT & 0x80000000) != 0x0)
01297   {
01298     if(NewState != DISABLE)
01299     {
01300       /* Enable the interrupt sources */
01301       FLASH->CR2 |= (FLASH_IT & 0x7FFFFFFF);
01302     }
01303     else
01304     {
01305       /* Disable the interrupt sources */
01306       FLASH->CR2 &= ~(uint32_t)(FLASH_IT & 0x7FFFFFFF);
01307     }
01308   }
01309   else
01310   {
01311     if(NewState != DISABLE)
01312     {
01313       /* Enable the interrupt sources */
01314       FLASH->CR |= FLASH_IT;
01315     }
01316     else
01317     {
01318       /* Disable the interrupt sources */
01319       FLASH->CR &= ~(uint32_t)FLASH_IT;
01320     }
01321   }
01322 #else
01323   /* Check the parameters */
01324   assert_param(IS_FLASH_IT(FLASH_IT)); 
01325   assert_param(IS_FUNCTIONAL_STATE(NewState));
01326 
01327   if(NewState != DISABLE)
01328   {
01329     /* Enable the interrupt sources */
01330     FLASH->CR |= FLASH_IT;
01331   }
01332   else
01333   {
01334     /* Disable the interrupt sources */
01335     FLASH->CR &= ~(uint32_t)FLASH_IT;
01336   }
01337 #endif /* STM32F10X_XL */
01338 }
01339 
01340 /**
01341   * @brief  Checks whether the specified FLASH flag is set or not.
01342   * @note   This function can be used for all STM32F10x devices.
01343   *         - For STM32F10X_XL devices, this function checks whether the specified 
01344   *           Bank1 or Bank2 flag is set or not.
01345   *         - For other devices, it checks whether the specified Bank1 flag is 
01346   *           set or not.
01347   * @param  FLASH_FLAG: specifies the FLASH flag to check.
01348   *   This parameter can be one of the following values:
01349   *     @arg FLASH_FLAG_BSY: FLASH Busy flag           
01350   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       
01351   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      
01352   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           
01353   *     @arg FLASH_FLAG_OPTERR:  FLASH Option Byte error flag     
01354   * @retval The new state of FLASH_FLAG (SET or RESET).
01355   */
01356 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
01357 {
01358   FlagStatus bitstatus = RESET;
01359 
01360 #ifdef STM32F10X_XL
01361   /* Check the parameters */
01362   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
01363   if(FLASH_FLAG == FLASH_FLAG_OPTERR) 
01364   {
01365     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
01366     {
01367       bitstatus = SET;
01368     }
01369     else
01370     {
01371       bitstatus = RESET;
01372     }
01373   }
01374   else
01375   {
01376     if((FLASH_FLAG & 0x80000000) != 0x0)
01377     {
01378       if((FLASH->SR2 & FLASH_FLAG) != (uint32_t)RESET)
01379       {
01380         bitstatus = SET;
01381       }
01382       else
01383       {
01384         bitstatus = RESET;
01385       }
01386     }
01387     else
01388     {
01389       if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
01390       {
01391         bitstatus = SET;
01392       }
01393       else
01394       {
01395         bitstatus = RESET;
01396       }
01397     }
01398   }
01399 #else
01400   /* Check the parameters */
01401   assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
01402   if(FLASH_FLAG == FLASH_FLAG_OPTERR) 
01403   {
01404     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)
01405     {
01406       bitstatus = SET;
01407     }
01408     else
01409     {
01410       bitstatus = RESET;
01411     }
01412   }
01413   else
01414   {
01415    if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
01416     {
01417       bitstatus = SET;
01418     }
01419     else
01420     {
01421       bitstatus = RESET;
01422     }
01423   }
01424 #endif /* STM32F10X_XL */
01425 
01426   /* Return the new state of FLASH_FLAG (SET or RESET) */
01427   return bitstatus;
01428 }
01429 
01430 /**
01431   * @brief  Clears the FLASH’s pending flags.
01432   * @note   This function can be used for all STM32F10x devices.
01433   *         - For STM32F10X_XL devices, this function clears Bank1 or Bank2’s pending flags
01434   *         - For other devices, it clears Bank1’s pending flags.
01435   * @param  FLASH_FLAG: specifies the FLASH flags to clear.
01436   *   This parameter can be any combination of the following values:         
01437   *     @arg FLASH_FLAG_PGERR: FLASH Program error flag       
01438   *     @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      
01439   *     @arg FLASH_FLAG_EOP: FLASH End of Operation flag           
01440   * @retval None
01441   */
01442 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
01443 {
01444 #ifdef STM32F10X_XL
01445   /* Check the parameters */
01446   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
01447 
01448   if((FLASH_FLAG & 0x80000000) != 0x0)
01449   {
01450     /* Clear the flags */
01451     FLASH->SR2 = FLASH_FLAG;
01452   }
01453   else
01454   {
01455     /* Clear the flags */
01456     FLASH->SR = FLASH_FLAG;
01457   }  
01458 
01459 #else
01460   /* Check the parameters */
01461   assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
01462   
01463   /* Clear the flags */
01464   FLASH->SR = FLASH_FLAG;
01465 #endif /* STM32F10X_XL */
01466 }
01467 
01468 /**
01469   * @brief  Returns the FLASH Status.
01470   * @note   This function can be used for all STM32F10x devices, it is equivalent
01471   *    to FLASH_GetBank1Status function.
01472   * @param  None
01473   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
01474   *   FLASH_ERROR_WRP or FLASH_COMPLETE
01475   */
01476 FLASH_Status FLASH_GetStatus(void)
01477 {
01478   FLASH_Status flashstatus = FLASH_COMPLETE;
01479   
01480   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) 
01481   {
01482     flashstatus = FLASH_BUSY;
01483   }
01484   else 
01485   {  
01486     if((FLASH->SR & FLASH_FLAG_PGERR) != 0)
01487     { 
01488       flashstatus = FLASH_ERROR_PG;
01489     }
01490     else 
01491     {
01492       if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )
01493       {
01494         flashstatus = FLASH_ERROR_WRP;
01495       }
01496       else
01497       {
01498         flashstatus = FLASH_COMPLETE;
01499       }
01500     }
01501   }
01502   /* Return the Flash Status */
01503   return flashstatus;
01504 }
01505 
01506 /**
01507   * @brief  Returns the FLASH Bank1 Status.
01508   * @note   This function can be used for all STM32F10x devices, it is equivalent
01509   *   to FLASH_GetStatus function.
01510   * @param  None
01511   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
01512   *   FLASH_ERROR_WRP or FLASH_COMPLETE
01513   */
01514 FLASH_Status FLASH_GetBank1Status(void)
01515 {
01516   FLASH_Status flashstatus = FLASH_COMPLETE;
01517   
01518   if((FLASH->SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY) 
01519   {
01520     flashstatus = FLASH_BUSY;
01521   }
01522   else 
01523   {  
01524     if((FLASH->SR & FLASH_FLAG_BANK1_PGERR) != 0)
01525     { 
01526       flashstatus = FLASH_ERROR_PG;
01527     }
01528     else 
01529     {
01530       if((FLASH->SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )
01531       {
01532         flashstatus = FLASH_ERROR_WRP;
01533       }
01534       else
01535       {
01536         flashstatus = FLASH_COMPLETE;
01537       }
01538     }
01539   }
01540   /* Return the Flash Status */
01541   return flashstatus;
01542 }
01543 
01544 #ifdef STM32F10X_XL
01545 /**
01546   * @brief  Returns the FLASH Bank2 Status.
01547   * @note   This function can be used for STM32F10x_XL density devices.
01548   * @param  None
01549   * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,
01550   *   FLASH_ERROR_WRP or FLASH_COMPLETE
01551   */
01552 FLASH_Status FLASH_GetBank2Status(void)
01553 {
01554   FLASH_Status flashstatus = FLASH_COMPLETE;
01555   
01556   if((FLASH->SR2 & (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) 
01557   {
01558     flashstatus = FLASH_BUSY;
01559   }
01560   else 
01561   {  
01562     if((FLASH->SR2 & (FLASH_FLAG_BANK2_PGERR & 0x7FFFFFFF)) != 0)
01563     { 
01564       flashstatus = FLASH_ERROR_PG;
01565     }
01566     else 
01567     {
01568       if((FLASH->SR2 & (FLASH_FLAG_BANK2_WRPRTERR & 0x7FFFFFFF)) != 0 )
01569       {
01570         flashstatus = FLASH_ERROR_WRP;
01571       }
01572       else
01573       {
01574         flashstatus = FLASH_COMPLETE;
01575       }
01576     }
01577   }
01578   /* Return the Flash Status */
01579   return flashstatus;
01580 }
01581 #endif /* STM32F10X_XL */
01582 /**
01583   * @brief  Waits for a Flash operation to complete or a TIMEOUT to occur.
01584   * @note   This function can be used for all STM32F10x devices, 
01585   *         it is equivalent to FLASH_WaitForLastBank1Operation.
01586   *         - For STM32F10X_XL devices this function waits for a Bank1 Flash operation
01587   *           to complete or a TIMEOUT to occur.
01588   *         - For all other devices it waits for a Flash operation to complete 
01589   *           or a TIMEOUT to occur.
01590   * @param  Timeout: FLASH progamming Timeout
01591   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
01592   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01593   */
01594 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
01595 { 
01596   FLASH_Status status = FLASH_COMPLETE;
01597    
01598   /* Check for the Flash Status */
01599   status = FLASH_GetBank1Status();
01600   /* Wait for a Flash operation to complete or a TIMEOUT to occur */
01601   while((status == FLASH_BUSY) && (Timeout != 0x00))
01602   {
01603     status = FLASH_GetBank1Status();
01604     Timeout--;
01605   }
01606   if(Timeout == 0x00 )
01607   {
01608     status = FLASH_TIMEOUT;
01609   }
01610   /* Return the operation status */
01611   return status;
01612 }
01613 
01614 /**
01615   * @brief  Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.
01616   * @note   This function can be used for all STM32F10x devices, 
01617   *         it is equivalent to FLASH_WaitForLastOperation.
01618   * @param  Timeout: FLASH progamming Timeout
01619   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
01620   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01621   */
01622 FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout)
01623 { 
01624   FLASH_Status status = FLASH_COMPLETE;
01625    
01626   /* Check for the Flash Status */
01627   status = FLASH_GetBank1Status();
01628   /* Wait for a Flash operation to complete or a TIMEOUT to occur */
01629   while((status == FLASH_FLAG_BANK1_BSY) && (Timeout != 0x00))
01630   {
01631     status = FLASH_GetBank1Status();
01632     Timeout--;
01633   }
01634   if(Timeout == 0x00 )
01635   {
01636     status = FLASH_TIMEOUT;
01637   }
01638   /* Return the operation status */
01639   return status;
01640 }
01641 
01642 #ifdef STM32F10X_XL
01643 /**
01644   * @brief  Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.
01645   * @note   This function can be used only for STM32F10x_XL density devices.
01646   * @param  Timeout: FLASH progamming Timeout
01647   * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
01648   *   FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
01649   */
01650 FLASH_Status FLASH_WaitForLastBank2Operation(uint32_t Timeout)
01651 { 
01652   FLASH_Status status = FLASH_COMPLETE;
01653    
01654   /* Check for the Flash Status */
01655   status = FLASH_GetBank2Status();
01656   /* Wait for a Flash operation to complete or a TIMEOUT to occur */
01657   while((status == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) && (Timeout != 0x00))
01658   {
01659     status = FLASH_GetBank2Status();
01660     Timeout--;
01661   }
01662   if(Timeout == 0x00 )
01663   {
01664     status = FLASH_TIMEOUT;
01665   }
01666   /* Return the operation status */
01667   return status;
01668 }
01669 #endif /* STM32F10X_XL */
01670 
01671 /**
01672   * @}
01673   */
01674 
01675 /**
01676   * @}
01677   */
01678 
01679 /**
01680   * @}
01681   */
01682 
01683 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32