--- /dev/null
+/**\r
+ ******************************************************************************\r
+ * @file stm32f10x_flash.c\r
+ * @author MCD Application Team\r
+ * @version V3.3.0\r
+ * @date 04/16/2010\r
+ * @brief This file provides all the FLASH firmware functions.\r
+ ******************************************************************************\r
+ * @copy\r
+ *\r
+ * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
+ * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
+ * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
+ * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
+ * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
+ * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
+ *\r
+ * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>\r
+ */ \r
+\r
+/* Includes ------------------------------------------------------------------*/\r
+#include "stm32f10x_flash.h"\r
+\r
+/** @addtogroup STM32F10x_StdPeriph_Driver\r
+ * @{\r
+ */\r
+\r
+/** @defgroup FLASH \r
+ * @brief FLASH driver modules\r
+ * @{\r
+ */ \r
+\r
+/** @defgroup FLASH_Private_TypesDefinitions\r
+ * @{\r
+ */\r
+\r
+/**\r
+ * @}\r
+ */ \r
+\r
+/** @defgroup FLASH_Private_Defines\r
+ * @{\r
+ */ \r
+\r
+/* Flash Access Control Register bits */\r
+#define ACR_LATENCY_Mask ((uint32_t)0x00000038)\r
+#define ACR_HLFCYA_Mask ((uint32_t)0xFFFFFFF7)\r
+#define ACR_PRFTBE_Mask ((uint32_t)0xFFFFFFEF)\r
+\r
+/* Flash Access Control Register bits */\r
+#define ACR_PRFTBS_Mask ((uint32_t)0x00000020) \r
+\r
+/* Flash Control Register bits */\r
+#define CR_PG_Set ((uint32_t)0x00000001)\r
+#define CR_PG_Reset ((uint32_t)0x00001FFE) \r
+#define CR_PER_Set ((uint32_t)0x00000002)\r
+#define CR_PER_Reset ((uint32_t)0x00001FFD)\r
+#define CR_MER_Set ((uint32_t)0x00000004)\r
+#define CR_MER_Reset ((uint32_t)0x00001FFB)\r
+#define CR_OPTPG_Set ((uint32_t)0x00000010)\r
+#define CR_OPTPG_Reset ((uint32_t)0x00001FEF)\r
+#define CR_OPTER_Set ((uint32_t)0x00000020)\r
+#define CR_OPTER_Reset ((uint32_t)0x00001FDF)\r
+#define CR_STRT_Set ((uint32_t)0x00000040)\r
+#define CR_LOCK_Set ((uint32_t)0x00000080)\r
+\r
+/* FLASH Mask */\r
+#define RDPRT_Mask ((uint32_t)0x00000002)\r
+#define WRP0_Mask ((uint32_t)0x000000FF)\r
+#define WRP1_Mask ((uint32_t)0x0000FF00)\r
+#define WRP2_Mask ((uint32_t)0x00FF0000)\r
+#define WRP3_Mask ((uint32_t)0xFF000000)\r
+#define OB_USER_BFB2 ((uint16_t)0x0008)\r
+\r
+/* FLASH Keys */\r
+#define RDP_Key ((uint16_t)0x00A5)\r
+#define FLASH_KEY1 ((uint32_t)0x45670123)\r
+#define FLASH_KEY2 ((uint32_t)0xCDEF89AB)\r
+\r
+/* FLASH BANK address */\r
+#define FLASH_BANK1_END_ADDRESS ((uint32_t)0x807FFFF)\r
+\r
+/* Delay definition */ \r
+#define EraseTimeout ((uint32_t)0x000B0000)\r
+#define ProgramTimeout ((uint32_t)0x00002000)\r
+/**\r
+ * @}\r
+ */ \r
+\r
+/** @defgroup FLASH_Private_Macros\r
+ * @{\r
+ */\r
+\r
+/**\r
+ * @}\r
+ */ \r
+\r
+/** @defgroup FLASH_Private_Variables\r
+ * @{\r
+ */\r
+\r
+/**\r
+ * @}\r
+ */ \r
+\r
+/** @defgroup FLASH_Private_FunctionPrototypes\r
+ * @{\r
+ */\r
+ \r
+/**\r
+ * @}\r
+ */\r
+\r
+/** @defgroup FLASH_Private_Functions\r
+ * @{\r
+ */\r
+\r
+/**\r
+@code \r
+ \r
+ This driver provides functions to configure and program the Flash memory of all STM32F10x devices,\r
+ including the latest STM32F10x_XL density devices. \r
+\r
+ STM32F10x_XL devices feature up to 1 Mbyte with dual bank architecture for read-while-write (RWW) capability:\r
+ - bank1: fixed size of 512 Kbytes (256 pages of 2Kbytes each)\r
+ - bank2: up to 512 Kbytes (up to 256 pages of 2Kbytes each)\r
+ While other STM32F10x devices features only one bank with memory up to 512 Kbytes.\r
+\r
+ In version V3.3.0, some functions were updated and new ones were added to support\r
+ STM32F10x_XL devices. Thus some functions manages all devices, while other are \r
+ dedicated for XL devices only.\r
+ \r
+ The table below presents the list of available functions depending on the used STM32F10x devices. \r
+ \r
+ ***************************************************\r
+ * Legacy functions used for all STM32F10x devices *\r
+ ***************************************************\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+ | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |\r
+ | | devices | devices | |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_SetLatency | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_HalfCycleAccessCmd | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_PrefetchBufferCmd | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_Unlock | Yes | Yes | - For STM32F10X_XL devices: unlock Bank1 and Bank2. |\r
+ | | | | - For other devices: unlock Bank1 and it is equivalent |\r
+ | | | | to FLASH_UnlockBank1 function. |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_Lock | Yes | Yes | - For STM32F10X_XL devices: lock Bank1 and Bank2. |\r
+ | | | | - For other devices: lock Bank1 and it is equivalent |\r
+ | | | | to FLASH_LockBank1 function. |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ErasePage | Yes | Yes | - For STM32F10x_XL devices: erase a page in Bank1 and Bank2 |\r
+ | | | | - For other devices: erase a page in Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_EraseAllPages | Yes | Yes | - For STM32F10x_XL devices: erase all pages in Bank1 and Bank2 |\r
+ | | | | - For other devices: erase all pages in Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_EraseOptionBytes | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ProgramWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ProgramHalfWord | Yes | Yes | Updated to program up to 1MByte (depending on the used device) |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ProgramOptionByteData | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_EnableWriteProtection | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ReadOutProtection | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_UserOptionByteConfig | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetUserOptionByte | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetWriteProtectionOptionByte | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetReadOutProtectionStatus | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetPrefetchBufferStatus | Yes | Yes | No change |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ITConfig | Yes | Yes | - For STM32F10x_XL devices: enable Bank1 and Bank2's interrupts|\r
+ | | | | - For other devices: enable Bank1's interrupts |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetFlagStatus | Yes | Yes | - For STM32F10x_XL devices: return Bank1 and Bank2's flag status|\r
+ | | | | - For other devices: return Bank1's flag status |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_ClearFlag | Yes | Yes | - For STM32F10x_XL devices: clear Bank1 and Bank2's flag |\r
+ | | | | - For other devices: clear Bank1's flag |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_GetStatus | Yes | Yes | - Return the status of Bank1 (for all devices) |\r
+ | | | | equivalent to FLASH_GetBank1Status function |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_WaitForLastOperation | Yes | Yes | - Wait for Bank1 last operation (for all devices) |\r
+ | | | | equivalent to: FLASH_WaitForLastBank1Operation function |\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+\r
+ ************************************************************************************************************************\r
+ * New functions used for all STM32F10x devices to manage Bank1: *\r
+ * - These functions are mainly useful for STM32F10x_XL density devices, to have separate control for Bank1 and bank2 *\r
+ * - For other devices, these functions are optional (covered by functions listed above) *\r
+ ************************************************************************************************************************\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+ | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |\r
+ | | devices | devices | |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_UnlockBank1 | Yes | Yes | - Unlock Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_LockBank1 | Yes | Yes | - Lock Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_EraseAllBank1Pages | Yes | Yes | - Erase all pages in Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_GetBank1Status | Yes | Yes | - Return the status of Bank1 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_WaitForLastBank1Operation | Yes | Yes | - Wait for Bank1 last operation |\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+\r
+ *****************************************************************************\r
+ * New Functions used only with STM32F10x_XL density devices to manage Bank2 *\r
+ *****************************************************************************\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+ | Functions prototypes |STM32F10x_XL|Other STM32F10x| Comments |\r
+ | | devices | devices | |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_UnlockBank2 | Yes | No | - Unlock Bank2 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ |FLASH_LockBank2 | Yes | No | - Lock Bank2 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_EraseAllBank2Pages | Yes | No | - Erase all pages in Bank2 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_GetBank2Status | Yes | No | - Return the status of Bank2 |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_WaitForLastBank2Operation | Yes | No | - Wait for Bank2 last operation |\r
+ |----------------------------------------------------------------------------------------------------------------------------------|\r
+ | FLASH_BootConfig | Yes | No | - Configure to boot from Bank1 or Bank2 |\r
+ +----------------------------------------------------------------------------------------------------------------------------------+\r
+@endcode\r
+*/\r
+\r
+\r
+/**\r
+ * @brief Sets the code latency value.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param FLASH_Latency: specifies the FLASH Latency value.\r
+ * This parameter can be one of the following values:\r
+ * @arg FLASH_Latency_0: FLASH Zero Latency cycle\r
+ * @arg FLASH_Latency_1: FLASH One Latency cycle\r
+ * @arg FLASH_Latency_2: FLASH Two Latency cycles\r
+ * @retval None\r
+ */\r
+void FLASH_SetLatency(uint32_t FLASH_Latency)\r
+{\r
+ uint32_t tmpreg = 0;\r
+ \r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_LATENCY(FLASH_Latency));\r
+ \r
+ /* Read the ACR register */\r
+ tmpreg = FLASH->ACR; \r
+ \r
+ /* Sets the Latency value */\r
+ tmpreg &= ACR_LATENCY_Mask;\r
+ tmpreg |= FLASH_Latency;\r
+ \r
+ /* Write the ACR register */\r
+ FLASH->ACR = tmpreg;\r
+}\r
+\r
+/**\r
+ * @brief Enables or disables the Half cycle flash access.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param FLASH_HalfCycleAccess: specifies the FLASH Half cycle Access mode.\r
+ * This parameter can be one of the following values:\r
+ * @arg FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable\r
+ * @arg FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable\r
+ * @retval None\r
+ */\r
+void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess)\r
+{\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));\r
+ \r
+ /* Enable or disable the Half cycle access */\r
+ FLASH->ACR &= ACR_HLFCYA_Mask;\r
+ FLASH->ACR |= FLASH_HalfCycleAccess;\r
+}\r
+\r
+/**\r
+ * @brief Enables or disables the Prefetch Buffer.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param FLASH_PrefetchBuffer: specifies the Prefetch buffer status.\r
+ * This parameter can be one of the following values:\r
+ * @arg FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable\r
+ * @arg FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable\r
+ * @retval None\r
+ */\r
+void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer)\r
+{\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));\r
+ \r
+ /* Enable or disable the Prefetch Buffer */\r
+ FLASH->ACR &= ACR_PRFTBE_Mask;\r
+ FLASH->ACR |= FLASH_PrefetchBuffer;\r
+}\r
+\r
+/**\r
+ * @brief Unlocks the FLASH Program Erase Controller.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices this function unlocks Bank1 and Bank2.\r
+ * - For all other devices it unlocks Bank1 and it is equivalent \r
+ * to FLASH_UnlockBank1 function.. \r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_Unlock(void)\r
+{\r
+ /* Authorize the FPEC of Bank1 Access */\r
+ FLASH->KEYR = FLASH_KEY1;\r
+ FLASH->KEYR = FLASH_KEY2;\r
+\r
+#ifdef STM32F10X_XL\r
+ /* Authorize the FPEC of Bank2 Access */\r
+ FLASH->KEYR2 = FLASH_KEY1;\r
+ FLASH->KEYR2 = FLASH_KEY2;\r
+#endif /* STM32F10X_XL */\r
+}\r
+/**\r
+ * @brief Unlocks the FLASH Bank1 Program Erase Controller.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices this function unlocks Bank1.\r
+ * - For all other devices it unlocks Bank1 and it is \r
+ * equivalent to FLASH_Unlock function.\r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_UnlockBank1(void)\r
+{\r
+ /* Authorize the FPEC of Bank1 Access */\r
+ FLASH->KEYR = FLASH_KEY1;\r
+ FLASH->KEYR = FLASH_KEY2;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Unlocks the FLASH Bank2 Program Erase Controller.\r
+ * @note This function can be used only for STM32F10X_XL density devices.\r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_UnlockBank2(void)\r
+{\r
+ /* Authorize the FPEC of Bank2 Access */\r
+ FLASH->KEYR2 = FLASH_KEY1;\r
+ FLASH->KEYR2 = FLASH_KEY2;\r
+\r
+}\r
+#endif /* STM32F10X_XL */\r
+\r
+/**\r
+ * @brief Locks the FLASH Program Erase Controller.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices this function Locks Bank1 and Bank2.\r
+ * - For all other devices it Locks Bank1 and it is equivalent \r
+ * to FLASH_LockBank1 function.\r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_Lock(void)\r
+{\r
+ /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */\r
+ FLASH->CR |= CR_LOCK_Set;\r
+\r
+#ifdef STM32F10X_XL\r
+ /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */\r
+ FLASH->CR2 |= CR_LOCK_Set;\r
+#endif /* STM32F10X_XL */\r
+}\r
+\r
+/**\r
+ * @brief Locks the FLASH Bank1 Program Erase Controller.\r
+ * @note this function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices this function Locks Bank1.\r
+ * - For all other devices it Locks Bank1 and it is equivalent \r
+ * to FLASH_Lock function.\r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_LockBank1(void)\r
+{\r
+ /* Set the Lock Bit to lock the FPEC and the CR of Bank1 */\r
+ FLASH->CR |= CR_LOCK_Set;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Locks the FLASH Bank2 Program Erase Controller.\r
+ * @note This function can be used only for STM32F10X_XL density devices.\r
+ * @param None\r
+ * @retval None\r
+ */\r
+void FLASH_LockBank2(void)\r
+{\r
+ /* Set the Lock Bit to lock the FPEC and the CR of Bank2 */\r
+ FLASH->CR2 |= CR_LOCK_Set;\r
+}\r
+#endif /* STM32F10X_XL */\r
+\r
+/**\r
+ * @brief Erases a specified FLASH page.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param Page_Address: The page address to be erased.\r
+ * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_ErasePage(uint32_t Page_Address)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_ADDRESS(Page_Address));\r
+\r
+#ifdef STM32F10X_XL\r
+ if(Page_Address < FLASH_BANK1_END_ADDRESS) \r
+ {\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ { \r
+ /* if the previous operation is completed, proceed to erase the page */\r
+ FLASH->CR|= CR_PER_Set;\r
+ FLASH->AR = Page_Address; \r
+ FLASH->CR|= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the PER Bit */\r
+ FLASH->CR &= CR_PER_Reset;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ { \r
+ /* if the previous operation is completed, proceed to erase the page */\r
+ FLASH->CR2|= CR_PER_Set;\r
+ FLASH->AR2 = Page_Address; \r
+ FLASH->CR2|= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the PER Bit */\r
+ FLASH->CR2 &= CR_PER_Reset;\r
+ }\r
+ }\r
+ }\r
+#else\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ { \r
+ /* if the previous operation is completed, proceed to erase the page */\r
+ FLASH->CR|= CR_PER_Set;\r
+ FLASH->AR = Page_Address; \r
+ FLASH->CR|= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the PER Bit */\r
+ FLASH->CR &= CR_PER_Reset;\r
+ }\r
+ }\r
+#endif /* STM32F10X_XL */\r
+\r
+ /* Return the Erase Status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Erases all FLASH pages.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_EraseAllPages(void)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+\r
+#ifdef STM32F10X_XL\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to erase all pages */\r
+ FLASH->CR |= CR_MER_Set;\r
+ FLASH->CR |= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the MER Bit */\r
+ FLASH->CR &= CR_MER_Reset;\r
+ }\r
+ } \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to erase all pages */\r
+ FLASH->CR2 |= CR_MER_Set;\r
+ FLASH->CR2 |= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the MER Bit */\r
+ FLASH->CR2 &= CR_MER_Reset;\r
+ }\r
+ }\r
+#else\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to erase all pages */\r
+ FLASH->CR |= CR_MER_Set;\r
+ FLASH->CR |= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the MER Bit */\r
+ FLASH->CR &= CR_MER_Reset;\r
+ }\r
+ }\r
+#endif /* STM32F10X_XL */\r
+\r
+ /* Return the Erase Status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Erases all Bank1 FLASH pages.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices this function erases all Bank1 pages.\r
+ * - For all other devices it erases all Bank1 pages and it is equivalent \r
+ * to FLASH_EraseAllPages function.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_EraseAllBank1Pages(void)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to erase all pages */\r
+ FLASH->CR |= CR_MER_Set;\r
+ FLASH->CR |= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the MER Bit */\r
+ FLASH->CR &= CR_MER_Reset;\r
+ }\r
+ } \r
+ /* Return the Erase Status */\r
+ return status;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Erases all Bank2 FLASH pages.\r
+ * @note This function can be used only for STM32F10x_XL density devices.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_EraseAllBank2Pages(void)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to erase all pages */\r
+ FLASH->CR2 |= CR_MER_Set;\r
+ FLASH->CR2 |= CR_STRT_Set;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(EraseTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the erase operation is completed, disable the MER Bit */\r
+ FLASH->CR2 &= CR_MER_Reset;\r
+ }\r
+ } \r
+ /* Return the Erase Status */\r
+ return status;\r
+}\r
+#endif /* STM32F10X_XL */\r
+\r
+/**\r
+ * @brief Erases the FLASH option bytes.\r
+ * @note This functions erases all option bytes except the Read protection (RDP). \r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_EraseOptionBytes(void)\r
+{\r
+ uint16_t rdptmp = RDP_Key;\r
+\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+\r
+ /* Get the actual read protection Option Byte value */ \r
+ if(FLASH_GetReadOutProtectionStatus() != RESET)\r
+ {\r
+ rdptmp = 0x00; \r
+ }\r
+\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* Authorize the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ \r
+ /* if the previous operation is completed, proceed to erase the option bytes */\r
+ FLASH->CR |= CR_OPTER_Set;\r
+ FLASH->CR |= CR_STRT_Set;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the erase operation is completed, disable the OPTER Bit */\r
+ FLASH->CR &= CR_OPTER_Reset;\r
+ \r
+ /* Enable the Option Bytes Programming operation */\r
+ FLASH->CR |= CR_OPTPG_Set;\r
+ /* Restore the last read protection Option Byte value */\r
+ OB->RDP = (uint16_t)rdptmp; \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ } \r
+ }\r
+ /* Return the erase status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Programs a word at a specified address.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param Address: specifies the address to be programmed.\r
+ * @param Data: specifies the data to be programmed.\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
+ */\r
+FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ __IO uint32_t tmp = 0;\r
+\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_ADDRESS(Address));\r
+\r
+#ifdef STM32F10X_XL\r
+ if(Address < FLASH_BANK1_END_ADDRESS - 2)\r
+ { \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(ProgramTimeout); \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new first \r
+ half word */\r
+ FLASH->CR |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = (uint16_t)Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new second \r
+ half word */\r
+ tmp = Address + 2;\r
+\r
+ *(__IO uint16_t*) tmp = Data >> 16;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else if(Address == (FLASH_BANK1_END_ADDRESS - 1))\r
+ {\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
+\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new first \r
+ half word */\r
+ FLASH->CR |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = (uint16_t)Data;\r
+\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
+\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new second \r
+ half word */\r
+ FLASH->CR2 |= CR_PG_Set;\r
+ tmp = Address + 2;\r
+\r
+ *(__IO uint16_t*) tmp = Data >> 16;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR2 &= CR_PG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR2 &= CR_PG_Reset;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new first \r
+ half word */\r
+ FLASH->CR2 |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = (uint16_t)Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new second \r
+ half word */\r
+ tmp = Address + 2;\r
+\r
+ *(__IO uint16_t*) tmp = Data >> 16;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR2 &= CR_PG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR2 &= CR_PG_Reset;\r
+ }\r
+ }\r
+ }\r
+ }\r
+#else\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new first \r
+ half word */\r
+ FLASH->CR |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = (uint16_t)Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new second \r
+ half word */\r
+ tmp = Address + 2;\r
+\r
+ *(__IO uint16_t*) tmp = Data >> 16;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ } \r
+#endif /* STM32F10X_XL */\r
+ \r
+ /* Return the Program Status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Programs a half word at a specified address.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param Address: specifies the address to be programmed.\r
+ * @param Data: specifies the data to be programmed.\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
+ */\r
+FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_ADDRESS(Address));\r
+\r
+#ifdef STM32F10X_XL\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(Address < FLASH_BANK1_END_ADDRESS)\r
+ {\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new data */\r
+ FLASH->CR |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank1Operation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new data */\r
+ FLASH->CR2 |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastBank2Operation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the PG Bit */\r
+ FLASH->CR2 &= CR_PG_Reset;\r
+ }\r
+ }\r
+ }\r
+#else\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the previous operation is completed, proceed to program the new data */\r
+ FLASH->CR |= CR_PG_Set;\r
+ \r
+ *(__IO uint16_t*)Address = Data;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the PG Bit */\r
+ FLASH->CR &= CR_PG_Reset;\r
+ }\r
+ } \r
+#endif /* STM32F10X_XL */\r
+ \r
+ /* Return the Program Status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Programs a half word at a specified Option Byte Data address.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param Address: specifies the address to be programmed.\r
+ * This parameter can be 0x1FFFF804 or 0x1FFFF806. \r
+ * @param Data: specifies the data to be programmed.\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT. \r
+ */\r
+FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Check the parameters */\r
+ assert_param(IS_OB_DATA_ADDRESS(Address));\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* Authorize the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ /* Enables the Option Bytes Programming operation */\r
+ FLASH->CR |= CR_OPTPG_Set; \r
+ *(__IO uint16_t*)Address = Data;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ }\r
+ /* Return the Option Byte Data Program Status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Write protects the desired pages\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param FLASH_Pages: specifies the address of the pages to be write protected.\r
+ * This parameter can be:\r
+ * @arg For @b STM32_Low-density_devices: value between FLASH_WRProt_Pages0to3 and FLASH_WRProt_Pages28to31 \r
+ * @arg For @b STM32_Medium-density_devices: value between FLASH_WRProt_Pages0to3\r
+ * and FLASH_WRProt_Pages124to127\r
+ * @arg For @b STM32_High-density_devices: value between FLASH_WRProt_Pages0to1 and\r
+ * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255\r
+ * @arg For @b STM32_Connectivity_line_devices: value between FLASH_WRProt_Pages0to1 and\r
+ * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to127 \r
+ * @arg For @b STM32_XL-density_devices: value between FLASH_WRProt_Pages0to1 and\r
+ * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to511\r
+ * @arg FLASH_WRProt_AllPages\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_EnableWriteProtection(uint32_t FLASH_Pages)\r
+{\r
+ uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;\r
+ \r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ \r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));\r
+ \r
+ FLASH_Pages = (uint32_t)(~FLASH_Pages);\r
+ WRP0_Data = (uint16_t)(FLASH_Pages & WRP0_Mask);\r
+ WRP1_Data = (uint16_t)((FLASH_Pages & WRP1_Mask) >> 8);\r
+ WRP2_Data = (uint16_t)((FLASH_Pages & WRP2_Mask) >> 16);\r
+ WRP3_Data = (uint16_t)((FLASH_Pages & WRP3_Mask) >> 24);\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* Authorizes the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ FLASH->CR |= CR_OPTPG_Set;\r
+ if(WRP0_Data != 0xFF)\r
+ {\r
+ OB->WRP0 = WRP0_Data;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ }\r
+ if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))\r
+ {\r
+ OB->WRP1 = WRP1_Data;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ }\r
+ if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))\r
+ {\r
+ OB->WRP2 = WRP2_Data;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ }\r
+ \r
+ if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))\r
+ {\r
+ OB->WRP3 = WRP3_Data;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ }\r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ } \r
+ /* Return the write protection operation Status */\r
+ return status; \r
+}\r
+\r
+/**\r
+ * @brief Enables or disables the read out protection.\r
+ * @note If the user has already programmed the other option bytes before calling \r
+ * this function, he must re-program them since this function erases all option bytes.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param Newstate: new state of the ReadOut Protection.\r
+ * This parameter can be: ENABLE or DISABLE.\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ /* Check the parameters */\r
+ assert_param(IS_FUNCTIONAL_STATE(NewState));\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* Authorizes the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ FLASH->CR |= CR_OPTER_Set;\r
+ FLASH->CR |= CR_STRT_Set;\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout);\r
+ if(status == FLASH_COMPLETE)\r
+ {\r
+ /* if the erase operation is completed, disable the OPTER Bit */\r
+ FLASH->CR &= CR_OPTER_Reset;\r
+ /* Enable the Option Bytes Programming operation */\r
+ FLASH->CR |= CR_OPTPG_Set; \r
+ if(NewState != DISABLE)\r
+ {\r
+ OB->RDP = 0x00;\r
+ }\r
+ else\r
+ {\r
+ OB->RDP = RDP_Key; \r
+ }\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(EraseTimeout); \r
+ \r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ }\r
+ else \r
+ {\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* Disable the OPTER Bit */\r
+ FLASH->CR &= CR_OPTER_Reset;\r
+ }\r
+ }\r
+ }\r
+ /* Return the protection operation Status */\r
+ return status; \r
+}\r
+\r
+/**\r
+ * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param OB_IWDG: Selects the IWDG mode\r
+ * This parameter can be one of the following values:\r
+ * @arg OB_IWDG_SW: Software IWDG selected\r
+ * @arg OB_IWDG_HW: Hardware IWDG selected\r
+ * @param OB_STOP: Reset event when entering STOP mode.\r
+ * This parameter can be one of the following values:\r
+ * @arg OB_STOP_NoRST: No reset generated when entering in STOP\r
+ * @arg OB_STOP_RST: Reset generated when entering in STOP\r
+ * @param OB_STDBY: Reset event when entering Standby mode.\r
+ * This parameter can be one of the following values:\r
+ * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY\r
+ * @arg OB_STDBY_RST: Reset generated when entering in STANDBY\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, \r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_UserOptionByteConfig(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY)\r
+{\r
+ FLASH_Status status = FLASH_COMPLETE; \r
+\r
+ /* Check the parameters */\r
+ assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));\r
+ assert_param(IS_OB_STOP_SOURCE(OB_STOP));\r
+ assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));\r
+\r
+ /* Authorize the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ { \r
+ /* Enable the Option Bytes Programming operation */\r
+ FLASH->CR |= CR_OPTPG_Set; \r
+ \r
+ OB->USER = OB_IWDG | (uint16_t)(OB_STOP | (uint16_t)(OB_STDBY | ((uint16_t)0xF8))); \r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ } \r
+ /* Return the Option Byte program Status */\r
+ return status;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Configures to boot from Bank1 or Bank2. \r
+ * @note This function can be used only for STM32F10x_XL density devices.\r
+ * @param FLASH_BOOT: select the FLASH Bank to boot from.\r
+ * This parameter can be one of the following values:\r
+ * @arg FLASH_BOOT_Bank1: At startup, if boot pins are set in boot from user Flash\r
+ * position and this parameter is selected the device will boot from Bank1(Default).\r
+ * @arg FLASH_BOOT_Bank2: At startup, if boot pins are set in boot from user Flash\r
+ * position and this parameter is selected the device will boot from Bank2 or Bank1,\r
+ * depending on the activation of the bank. The active banks are checked in\r
+ * the following order: Bank2, followed by Bank1.\r
+ * The active bank is recognized by the value programmed at the base address\r
+ * of the respective bank (corresponding to the initial stack pointer value\r
+ * in the interrupt vector table).\r
+ * For more information, please refer to AN2606 from www.st.com. \r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG, \r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_BootConfig(uint16_t FLASH_BOOT)\r
+{ \r
+ FLASH_Status status = FLASH_COMPLETE; \r
+ assert_param(IS_FLASH_BOOT(FLASH_BOOT));\r
+ /* Authorize the small information block programming */\r
+ FLASH->OPTKEYR = FLASH_KEY1;\r
+ FLASH->OPTKEYR = FLASH_KEY2;\r
+ \r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ \r
+ if(status == FLASH_COMPLETE)\r
+ { \r
+ /* Enable the Option Bytes Programming operation */\r
+ FLASH->CR |= CR_OPTPG_Set; \r
+\r
+ if(FLASH_BOOT == FLASH_BOOT_Bank1)\r
+ {\r
+ OB->USER |= OB_USER_BFB2;\r
+ }\r
+ else\r
+ {\r
+ OB->USER &= (uint16_t)(~(uint16_t)(OB_USER_BFB2));\r
+ }\r
+ /* Wait for last operation to be completed */\r
+ status = FLASH_WaitForLastOperation(ProgramTimeout);\r
+ if(status != FLASH_TIMEOUT)\r
+ {\r
+ /* if the program operation is completed, disable the OPTPG Bit */\r
+ FLASH->CR &= CR_OPTPG_Reset;\r
+ }\r
+ } \r
+ /* Return the Option Byte program Status */\r
+ return status;\r
+}\r
+#endif /* STM32F10X_XL */\r
+\r
+/**\r
+ * @brief Returns the FLASH User Option Bytes values.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)\r
+ * and RST_STDBY(Bit2).\r
+ */\r
+uint32_t FLASH_GetUserOptionByte(void)\r
+{\r
+ /* Return the User Option Byte */\r
+ return (uint32_t)(FLASH->OBR >> 2);\r
+}\r
+\r
+/**\r
+ * @brief Returns the FLASH Write Protection Option Bytes Register value.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval The FLASH Write Protection Option Bytes Register value\r
+ */\r
+uint32_t FLASH_GetWriteProtectionOptionByte(void)\r
+{\r
+ /* Return the Falsh write protection Register value */\r
+ return (uint32_t)(FLASH->WRPR);\r
+}\r
+\r
+/**\r
+ * @brief Checks whether the FLASH Read Out Protection Status is set or not.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval FLASH ReadOut Protection Status(SET or RESET)\r
+ */\r
+FlagStatus FLASH_GetReadOutProtectionStatus(void)\r
+{\r
+ FlagStatus readoutstatus = RESET;\r
+ if ((FLASH->OBR & RDPRT_Mask) != (uint32_t)RESET)\r
+ {\r
+ readoutstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ readoutstatus = RESET;\r
+ }\r
+ return readoutstatus;\r
+}\r
+\r
+/**\r
+ * @brief Checks whether the FLASH Prefetch Buffer status is set or not.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * @param None\r
+ * @retval FLASH Prefetch Buffer Status (SET or RESET).\r
+ */\r
+FlagStatus FLASH_GetPrefetchBufferStatus(void)\r
+{\r
+ FlagStatus bitstatus = RESET;\r
+ \r
+ if ((FLASH->ACR & ACR_PRFTBS_Mask) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */\r
+ return bitstatus; \r
+}\r
+\r
+/**\r
+ * @brief Enables or disables the specified FLASH interrupts.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices, enables or disables the specified FLASH interrupts\r
+ for Bank1 and Bank2.\r
+ * - For other devices it enables or disables the specified FLASH interrupts for Bank1.\r
+ * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or disabled.\r
+ * This parameter can be any combination of the following values:\r
+ * @arg FLASH_IT_ERROR: FLASH Error Interrupt\r
+ * @arg FLASH_IT_EOP: FLASH end of operation Interrupt\r
+ * @param NewState: new state of the specified Flash interrupts.\r
+ * This parameter can be: ENABLE or DISABLE. \r
+ * @retval None \r
+ */\r
+void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)\r
+{\r
+#ifdef STM32F10X_XL\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_IT(FLASH_IT)); \r
+ assert_param(IS_FUNCTIONAL_STATE(NewState));\r
+\r
+ if((FLASH_IT & 0x80000000) != 0x0)\r
+ {\r
+ if(NewState != DISABLE)\r
+ {\r
+ /* Enable the interrupt sources */\r
+ FLASH->CR2 |= (FLASH_IT & 0x7FFFFFFF);\r
+ }\r
+ else\r
+ {\r
+ /* Disable the interrupt sources */\r
+ FLASH->CR2 &= ~(uint32_t)(FLASH_IT & 0x7FFFFFFF);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if(NewState != DISABLE)\r
+ {\r
+ /* Enable the interrupt sources */\r
+ FLASH->CR |= FLASH_IT;\r
+ }\r
+ else\r
+ {\r
+ /* Disable the interrupt sources */\r
+ FLASH->CR &= ~(uint32_t)FLASH_IT;\r
+ }\r
+ }\r
+#else\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_IT(FLASH_IT)); \r
+ assert_param(IS_FUNCTIONAL_STATE(NewState));\r
+\r
+ if(NewState != DISABLE)\r
+ {\r
+ /* Enable the interrupt sources */\r
+ FLASH->CR |= FLASH_IT;\r
+ }\r
+ else\r
+ {\r
+ /* Disable the interrupt sources */\r
+ FLASH->CR &= ~(uint32_t)FLASH_IT;\r
+ }\r
+#endif /* STM32F10X_XL */\r
+}\r
+\r
+/**\r
+ * @brief Checks whether the specified FLASH flag is set or not.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices, this function checks whether the specified \r
+ * Bank1 or Bank2 flag is set or not.\r
+ * - For other devices, it checks whether the specified Bank1 flag is \r
+ * set or not.\r
+ * @param FLASH_FLAG: specifies the FLASH flag to check.\r
+ * This parameter can be one of the following values:\r
+ * @arg FLASH_FLAG_BSY: FLASH Busy flag \r
+ * @arg FLASH_FLAG_PGERR: FLASH Program error flag \r
+ * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag \r
+ * @arg FLASH_FLAG_EOP: FLASH End of Operation flag \r
+ * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag \r
+ * @retval The new state of FLASH_FLAG (SET or RESET).\r
+ */\r
+FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)\r
+{\r
+ FlagStatus bitstatus = RESET;\r
+\r
+#ifdef STM32F10X_XL\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;\r
+ if(FLASH_FLAG == FLASH_FLAG_OPTERR) \r
+ {\r
+ if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if((FLASH_FLAG & 0x80000000) != 0x0)\r
+ {\r
+ if((FLASH->SR2 & FLASH_FLAG) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ }\r
+ }\r
+#else\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;\r
+ if(FLASH_FLAG == FLASH_FLAG_OPTERR) \r
+ {\r
+ if((FLASH->OBR & FLASH_FLAG_OPTERR) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)\r
+ {\r
+ bitstatus = SET;\r
+ }\r
+ else\r
+ {\r
+ bitstatus = RESET;\r
+ }\r
+ }\r
+#endif /* STM32F10X_XL */\r
+\r
+ /* Return the new state of FLASH_FLAG (SET or RESET) */\r
+ return bitstatus;\r
+}\r
+\r
+/**\r
+ * @brief Clears the FLASH\92s pending flags.\r
+ * @note This function can be used for all STM32F10x devices.\r
+ * - For STM32F10X_XL devices, this function clears Bank1 or Bank2\92s pending flags\r
+ * - For other devices, it clears Bank1\92s pending flags.\r
+ * @param FLASH_FLAG: specifies the FLASH flags to clear.\r
+ * This parameter can be any combination of the following values: \r
+ * @arg FLASH_FLAG_PGERR: FLASH Program error flag \r
+ * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag \r
+ * @arg FLASH_FLAG_EOP: FLASH End of Operation flag \r
+ * @retval None\r
+ */\r
+void FLASH_ClearFlag(uint32_t FLASH_FLAG)\r
+{\r
+#ifdef STM32F10X_XL\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;\r
+\r
+ if((FLASH_FLAG & 0x80000000) != 0x0)\r
+ {\r
+ /* Clear the flags */\r
+ FLASH->SR2 = FLASH_FLAG;\r
+ }\r
+ else\r
+ {\r
+ /* Clear the flags */\r
+ FLASH->SR = FLASH_FLAG;\r
+ } \r
+\r
+#else\r
+ /* Check the parameters */\r
+ assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;\r
+ \r
+ /* Clear the flags */\r
+ FLASH->SR = FLASH_FLAG;\r
+#endif /* STM32F10X_XL */\r
+}\r
+\r
+/**\r
+ * @brief Returns the FLASH Status.\r
+ * @note This function can be used for all STM32F10x devices, it is equivalent\r
+ * to FLASH_GetBank1Status function.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP or FLASH_COMPLETE\r
+ */\r
+FLASH_Status FLASH_GetStatus(void)\r
+{\r
+ FLASH_Status flashstatus = FLASH_COMPLETE;\r
+ \r
+ if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) \r
+ {\r
+ flashstatus = FLASH_BUSY;\r
+ }\r
+ else \r
+ { \r
+ if((FLASH->SR & FLASH_FLAG_PGERR) != 0)\r
+ { \r
+ flashstatus = FLASH_ERROR_PG;\r
+ }\r
+ else \r
+ {\r
+ if((FLASH->SR & FLASH_FLAG_WRPRTERR) != 0 )\r
+ {\r
+ flashstatus = FLASH_ERROR_WRP;\r
+ }\r
+ else\r
+ {\r
+ flashstatus = FLASH_COMPLETE;\r
+ }\r
+ }\r
+ }\r
+ /* Return the Flash Status */\r
+ return flashstatus;\r
+}\r
+\r
+/**\r
+ * @brief Returns the FLASH Bank1 Status.\r
+ * @note This function can be used for all STM32F10x devices, it is equivalent\r
+ * to FLASH_GetStatus function.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP or FLASH_COMPLETE\r
+ */\r
+FLASH_Status FLASH_GetBank1Status(void)\r
+{\r
+ FLASH_Status flashstatus = FLASH_COMPLETE;\r
+ \r
+ if((FLASH->SR & FLASH_FLAG_BANK1_BSY) == FLASH_FLAG_BSY) \r
+ {\r
+ flashstatus = FLASH_BUSY;\r
+ }\r
+ else \r
+ { \r
+ if((FLASH->SR & FLASH_FLAG_BANK1_PGERR) != 0)\r
+ { \r
+ flashstatus = FLASH_ERROR_PG;\r
+ }\r
+ else \r
+ {\r
+ if((FLASH->SR & FLASH_FLAG_BANK1_WRPRTERR) != 0 )\r
+ {\r
+ flashstatus = FLASH_ERROR_WRP;\r
+ }\r
+ else\r
+ {\r
+ flashstatus = FLASH_COMPLETE;\r
+ }\r
+ }\r
+ }\r
+ /* Return the Flash Status */\r
+ return flashstatus;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Returns the FLASH Bank2 Status.\r
+ * @note This function can be used for STM32F10x_XL density devices.\r
+ * @param None\r
+ * @retval FLASH Status: The returned value can be: FLASH_BUSY, FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP or FLASH_COMPLETE\r
+ */\r
+FLASH_Status FLASH_GetBank2Status(void)\r
+{\r
+ FLASH_Status flashstatus = FLASH_COMPLETE;\r
+ \r
+ if((FLASH->SR2 & (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) \r
+ {\r
+ flashstatus = FLASH_BUSY;\r
+ }\r
+ else \r
+ { \r
+ if((FLASH->SR2 & (FLASH_FLAG_BANK2_PGERR & 0x7FFFFFFF)) != 0)\r
+ { \r
+ flashstatus = FLASH_ERROR_PG;\r
+ }\r
+ else \r
+ {\r
+ if((FLASH->SR2 & (FLASH_FLAG_BANK2_WRPRTERR & 0x7FFFFFFF)) != 0 )\r
+ {\r
+ flashstatus = FLASH_ERROR_WRP;\r
+ }\r
+ else\r
+ {\r
+ flashstatus = FLASH_COMPLETE;\r
+ }\r
+ }\r
+ }\r
+ /* Return the Flash Status */\r
+ return flashstatus;\r
+}\r
+#endif /* STM32F10X_XL */\r
+/**\r
+ * @brief Waits for a Flash operation to complete or a TIMEOUT to occur.\r
+ * @note This function can be used for all STM32F10x devices, \r
+ * it is equivalent to FLASH_WaitForLastBank1Operation.\r
+ * - For STM32F10X_XL devices this function waits for a Bank1 Flash operation\r
+ * to complete or a TIMEOUT to occur.\r
+ * - For all other devices it waits for a Flash operation to complete \r
+ * or a TIMEOUT to occur.\r
+ * @param Timeout: FLASH progamming Timeout\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)\r
+{ \r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ \r
+ /* Check for the Flash Status */\r
+ status = FLASH_GetBank1Status();\r
+ /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
+ while((status == FLASH_BUSY) && (Timeout != 0x00))\r
+ {\r
+ status = FLASH_GetBank1Status();\r
+ Timeout--;\r
+ }\r
+ if(Timeout == 0x00 )\r
+ {\r
+ status = FLASH_TIMEOUT;\r
+ }\r
+ /* Return the operation status */\r
+ return status;\r
+}\r
+\r
+/**\r
+ * @brief Waits for a Flash operation on Bank1 to complete or a TIMEOUT to occur.\r
+ * @note This function can be used for all STM32F10x devices, \r
+ * it is equivalent to FLASH_WaitForLastOperation.\r
+ * @param Timeout: FLASH progamming Timeout\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_WaitForLastBank1Operation(uint32_t Timeout)\r
+{ \r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ \r
+ /* Check for the Flash Status */\r
+ status = FLASH_GetBank1Status();\r
+ /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
+ while((status == FLASH_FLAG_BANK1_BSY) && (Timeout != 0x00))\r
+ {\r
+ status = FLASH_GetBank1Status();\r
+ Timeout--;\r
+ }\r
+ if(Timeout == 0x00 )\r
+ {\r
+ status = FLASH_TIMEOUT;\r
+ }\r
+ /* Return the operation status */\r
+ return status;\r
+}\r
+\r
+#ifdef STM32F10X_XL\r
+/**\r
+ * @brief Waits for a Flash operation on Bank2 to complete or a TIMEOUT to occur.\r
+ * @note This function can be used only for STM32F10x_XL density devices.\r
+ * @param Timeout: FLASH progamming Timeout\r
+ * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,\r
+ * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.\r
+ */\r
+FLASH_Status FLASH_WaitForLastBank2Operation(uint32_t Timeout)\r
+{ \r
+ FLASH_Status status = FLASH_COMPLETE;\r
+ \r
+ /* Check for the Flash Status */\r
+ status = FLASH_GetBank2Status();\r
+ /* Wait for a Flash operation to complete or a TIMEOUT to occur */\r
+ while((status == (FLASH_FLAG_BANK2_BSY & 0x7FFFFFFF)) && (Timeout != 0x00))\r
+ {\r
+ status = FLASH_GetBank2Status();\r
+ Timeout--;\r
+ }\r
+ if(Timeout == 0x00 )\r
+ {\r
+ status = FLASH_TIMEOUT;\r
+ }\r
+ /* Return the operation status */\r
+ return status;\r
+}\r
+#endif /* STM32F10X_XL */\r
+\r
+/**\r
+ * @}\r
+ */\r
+\r
+/**\r
+ * @}\r
+ */\r
+\r
+/**\r
+ * @}\r
+ */\r
+\r
+/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/\r