mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
130:1dec54e4aec3
test with CLOCK_SETUP = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 130:1dec54e4aec3 1 /**
mbed_official 130:1dec54e4aec3 2 ******************************************************************************
mbed_official 130:1dec54e4aec3 3 * @file stm32f0xx_flash.c
mbed_official 130:1dec54e4aec3 4 * @author MCD Application Team
mbed_official 130:1dec54e4aec3 5 * @version V1.3.0
mbed_official 130:1dec54e4aec3 6 * @date 16-January-2014
mbed_official 130:1dec54e4aec3 7 * @brief This file provides firmware functions to manage the following
mbed_official 130:1dec54e4aec3 8 * functionalities of the FLASH peripheral:
mbed_official 130:1dec54e4aec3 9 * - FLASH Interface configuration
mbed_official 130:1dec54e4aec3 10 * - FLASH Memory Programming
mbed_official 130:1dec54e4aec3 11 * - Option Bytes Programming
mbed_official 130:1dec54e4aec3 12 * - Interrupts and flags management
mbed_official 130:1dec54e4aec3 13 *
mbed_official 130:1dec54e4aec3 14 * @verbatim
mbed_official 130:1dec54e4aec3 15 ===============================================================================
mbed_official 130:1dec54e4aec3 16 ##### How to use this driver #####
mbed_official 130:1dec54e4aec3 17 ===============================================================================
mbed_official 130:1dec54e4aec3 18 [..] This driver provides functions to configure and program the Flash
mbed_official 130:1dec54e4aec3 19 memory of all STM32F0xx devices. These functions are split in 4 groups
mbed_official 130:1dec54e4aec3 20 (#) FLASH Interface configuration functions: this group includes the
mbed_official 130:1dec54e4aec3 21 management of following features:
mbed_official 130:1dec54e4aec3 22 (++) Set the latency
mbed_official 130:1dec54e4aec3 23 (++) Enable/Disable the prefetch buffer
mbed_official 130:1dec54e4aec3 24
mbed_official 130:1dec54e4aec3 25 (#) FLASH Memory Programming functions: this group includes all needed
mbed_official 130:1dec54e4aec3 26 functions to erase and program the main memory:
mbed_official 130:1dec54e4aec3 27 (++) Lock and Unlock the Flash interface.
mbed_official 130:1dec54e4aec3 28 (++) Erase function: Erase Page, erase all pages.
mbed_official 130:1dec54e4aec3 29 (++) Program functions: Half Word and Word write.
mbed_official 130:1dec54e4aec3 30
mbed_official 130:1dec54e4aec3 31 (#) FLASH Option Bytes Programming functions: this group includes all
mbed_official 130:1dec54e4aec3 32 needed functions to:
mbed_official 130:1dec54e4aec3 33 (++) Lock and Unlock the Flash Option bytes.
mbed_official 130:1dec54e4aec3 34 (++) Launch the Option Bytes loader
mbed_official 130:1dec54e4aec3 35 (++) Erase the Option Bytes
mbed_official 130:1dec54e4aec3 36 (++)Set/Reset the write protection
mbed_official 130:1dec54e4aec3 37 (++) Set the Read protection Level
mbed_official 130:1dec54e4aec3 38 (++) Program the user option Bytes
mbed_official 130:1dec54e4aec3 39 (++) Set/Reset the BOOT1 bit
mbed_official 130:1dec54e4aec3 40 (++) Enable/Disable the VDDA Analog Monitoring
mbed_official 130:1dec54e4aec3 41 (++) Get the user option bytes
mbed_official 130:1dec54e4aec3 42 (++) Get the Write protection
mbed_official 130:1dec54e4aec3 43 (++) Get the read protection status
mbed_official 130:1dec54e4aec3 44
mbed_official 130:1dec54e4aec3 45 (#) FLASH Interrupts and flag management functions: this group includes
mbed_official 130:1dec54e4aec3 46 all needed functions to:
mbed_official 130:1dec54e4aec3 47 (++) Enable/Disable the flash interrupt sources
mbed_official 130:1dec54e4aec3 48 (++) Get flags status
mbed_official 130:1dec54e4aec3 49 (++) Clear flags
mbed_official 130:1dec54e4aec3 50 (++) Get Flash operation status
mbed_official 130:1dec54e4aec3 51 (++) Wait for last flash operation
mbed_official 130:1dec54e4aec3 52
mbed_official 130:1dec54e4aec3 53 @endverbatim
mbed_official 130:1dec54e4aec3 54
mbed_official 130:1dec54e4aec3 55 ******************************************************************************
mbed_official 130:1dec54e4aec3 56 * @attention
mbed_official 130:1dec54e4aec3 57 *
mbed_official 130:1dec54e4aec3 58 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
mbed_official 130:1dec54e4aec3 59 *
mbed_official 130:1dec54e4aec3 60 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 130:1dec54e4aec3 61 * are permitted provided that the following conditions are met:
mbed_official 130:1dec54e4aec3 62 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 130:1dec54e4aec3 63 * this list of conditions and the following disclaimer.
mbed_official 130:1dec54e4aec3 64 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 130:1dec54e4aec3 65 * this list of conditions and the following disclaimer in the documentation
mbed_official 130:1dec54e4aec3 66 * and/or other materials provided with the distribution.
mbed_official 130:1dec54e4aec3 67 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 130:1dec54e4aec3 68 * may be used to endorse or promote products derived from this software
mbed_official 130:1dec54e4aec3 69 * without specific prior written permission.
mbed_official 130:1dec54e4aec3 70 *
mbed_official 130:1dec54e4aec3 71 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 130:1dec54e4aec3 72 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 130:1dec54e4aec3 73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 130:1dec54e4aec3 74 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 130:1dec54e4aec3 75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 130:1dec54e4aec3 76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 130:1dec54e4aec3 77 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 130:1dec54e4aec3 78 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 130:1dec54e4aec3 79 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 130:1dec54e4aec3 80 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 130:1dec54e4aec3 81 *
mbed_official 130:1dec54e4aec3 82 ******************************************************************************
mbed_official 130:1dec54e4aec3 83 */
mbed_official 130:1dec54e4aec3 84
mbed_official 130:1dec54e4aec3 85 /* Includes ------------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 86 #include "stm32f0xx_flash.h"
mbed_official 130:1dec54e4aec3 87
mbed_official 130:1dec54e4aec3 88 /** @addtogroup STM32F0xx_StdPeriph_Driver
mbed_official 130:1dec54e4aec3 89 * @{
mbed_official 130:1dec54e4aec3 90 */
mbed_official 130:1dec54e4aec3 91
mbed_official 130:1dec54e4aec3 92 /** @defgroup FLASH
mbed_official 130:1dec54e4aec3 93 * @brief FLASH driver modules
mbed_official 130:1dec54e4aec3 94 * @{
mbed_official 130:1dec54e4aec3 95 */
mbed_official 130:1dec54e4aec3 96
mbed_official 130:1dec54e4aec3 97 /* Private typedef -----------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 98 /* Private define ------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 99 /* Private macro -------------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 100 /* Private variables ---------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 101 /* Private function prototypes -----------------------------------------------*/
mbed_official 130:1dec54e4aec3 102 /* Private functions ---------------------------------------------------------*/
mbed_official 130:1dec54e4aec3 103
mbed_official 130:1dec54e4aec3 104 /** @defgroup FLASH_Private_Functions
mbed_official 130:1dec54e4aec3 105 * @{
mbed_official 130:1dec54e4aec3 106 */
mbed_official 130:1dec54e4aec3 107
mbed_official 130:1dec54e4aec3 108 /** @defgroup FLASH_Group1 FLASH Interface configuration functions
mbed_official 130:1dec54e4aec3 109 * @brief FLASH Interface configuration functions
mbed_official 130:1dec54e4aec3 110 *
mbed_official 130:1dec54e4aec3 111 @verbatim
mbed_official 130:1dec54e4aec3 112 ===============================================================================
mbed_official 130:1dec54e4aec3 113 ##### FLASH Interface configuration functions #####
mbed_official 130:1dec54e4aec3 114 ===============================================================================
mbed_official 130:1dec54e4aec3 115
mbed_official 130:1dec54e4aec3 116 [..] FLASH_Interface configuration_Functions, includes the following functions:
mbed_official 130:1dec54e4aec3 117 (+) void FLASH_SetLatency(uint32_t FLASH_Latency):
mbed_official 130:1dec54e4aec3 118 [..] To correctly read data from Flash memory, the number of wait states (LATENCY)
mbed_official 130:1dec54e4aec3 119 must be correctly programmed according to the frequency of the CPU clock (HCLK)
mbed_official 130:1dec54e4aec3 120 [..]
mbed_official 130:1dec54e4aec3 121 +--------------------------------------------- +
mbed_official 130:1dec54e4aec3 122 | Wait states | HCLK clock frequency (MHz) |
mbed_official 130:1dec54e4aec3 123 |---------------|------------------------------|
mbed_official 130:1dec54e4aec3 124 |0WS(1CPU cycle)| 0 < HCLK <= 24 |
mbed_official 130:1dec54e4aec3 125 |---------------|------------------------------|
mbed_official 130:1dec54e4aec3 126 |1WS(2CPU cycle)| 24 < HCLK <= 48 |
mbed_official 130:1dec54e4aec3 127 +----------------------------------------------+
mbed_official 130:1dec54e4aec3 128 [..]
mbed_official 130:1dec54e4aec3 129 (+) void FLASH_PrefetchBufferCmd(FunctionalState NewState);
mbed_official 130:1dec54e4aec3 130 [..]
mbed_official 130:1dec54e4aec3 131 All these functions don't need the unlock sequence.
mbed_official 130:1dec54e4aec3 132
mbed_official 130:1dec54e4aec3 133 @endverbatim
mbed_official 130:1dec54e4aec3 134 * @{
mbed_official 130:1dec54e4aec3 135 */
mbed_official 130:1dec54e4aec3 136
mbed_official 130:1dec54e4aec3 137 /**
mbed_official 130:1dec54e4aec3 138 * @brief Sets the code latency value.
mbed_official 130:1dec54e4aec3 139 * @param FLASH_Latency: specifies the FLASH Latency value.
mbed_official 130:1dec54e4aec3 140 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 141 * @arg FLASH_Latency_0: FLASH Zero Latency cycle
mbed_official 130:1dec54e4aec3 142 * @arg FLASH_Latency_1: FLASH One Latency cycle
mbed_official 130:1dec54e4aec3 143 * @retval None
mbed_official 130:1dec54e4aec3 144 */
mbed_official 130:1dec54e4aec3 145 void FLASH_SetLatency(uint32_t FLASH_Latency)
mbed_official 130:1dec54e4aec3 146 {
mbed_official 130:1dec54e4aec3 147 uint32_t tmpreg = 0;
mbed_official 130:1dec54e4aec3 148
mbed_official 130:1dec54e4aec3 149 /* Check the parameters */
mbed_official 130:1dec54e4aec3 150 assert_param(IS_FLASH_LATENCY(FLASH_Latency));
mbed_official 130:1dec54e4aec3 151
mbed_official 130:1dec54e4aec3 152 /* Read the ACR register */
mbed_official 130:1dec54e4aec3 153 tmpreg = FLASH->ACR;
mbed_official 130:1dec54e4aec3 154
mbed_official 130:1dec54e4aec3 155 /* Sets the Latency value */
mbed_official 130:1dec54e4aec3 156 tmpreg &= (uint32_t) (~((uint32_t)FLASH_ACR_LATENCY));
mbed_official 130:1dec54e4aec3 157 tmpreg |= FLASH_Latency;
mbed_official 130:1dec54e4aec3 158
mbed_official 130:1dec54e4aec3 159 /* Write the ACR register */
mbed_official 130:1dec54e4aec3 160 FLASH->ACR = tmpreg;
mbed_official 130:1dec54e4aec3 161 }
mbed_official 130:1dec54e4aec3 162
mbed_official 130:1dec54e4aec3 163 /**
mbed_official 130:1dec54e4aec3 164 * @brief Enables or disables the Prefetch Buffer.
mbed_official 130:1dec54e4aec3 165 * @param NewState: new state of the FLASH prefetch buffer.
mbed_official 130:1dec54e4aec3 166 * This parameter can be: ENABLE or DISABLE.
mbed_official 130:1dec54e4aec3 167 * @retval None
mbed_official 130:1dec54e4aec3 168 */
mbed_official 130:1dec54e4aec3 169 void FLASH_PrefetchBufferCmd(FunctionalState NewState)
mbed_official 130:1dec54e4aec3 170 {
mbed_official 130:1dec54e4aec3 171 /* Check the parameters */
mbed_official 130:1dec54e4aec3 172 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 173
mbed_official 130:1dec54e4aec3 174 if(NewState != DISABLE)
mbed_official 130:1dec54e4aec3 175 {
mbed_official 130:1dec54e4aec3 176 FLASH->ACR |= FLASH_ACR_PRFTBE;
mbed_official 130:1dec54e4aec3 177 }
mbed_official 130:1dec54e4aec3 178 else
mbed_official 130:1dec54e4aec3 179 {
mbed_official 130:1dec54e4aec3 180 FLASH->ACR &= (uint32_t)(~((uint32_t)FLASH_ACR_PRFTBE));
mbed_official 130:1dec54e4aec3 181 }
mbed_official 130:1dec54e4aec3 182 }
mbed_official 130:1dec54e4aec3 183
mbed_official 130:1dec54e4aec3 184 /**
mbed_official 130:1dec54e4aec3 185 * @brief Checks whether the FLASH Prefetch Buffer status is set or not.
mbed_official 130:1dec54e4aec3 186 * @param None
mbed_official 130:1dec54e4aec3 187 * @retval FLASH Prefetch Buffer Status (SET or RESET).
mbed_official 130:1dec54e4aec3 188 */
mbed_official 130:1dec54e4aec3 189 FlagStatus FLASH_GetPrefetchBufferStatus(void)
mbed_official 130:1dec54e4aec3 190 {
mbed_official 130:1dec54e4aec3 191 FlagStatus bitstatus = RESET;
mbed_official 130:1dec54e4aec3 192
mbed_official 130:1dec54e4aec3 193 if ((FLASH->ACR & FLASH_ACR_PRFTBS) != (uint32_t)RESET)
mbed_official 130:1dec54e4aec3 194 {
mbed_official 130:1dec54e4aec3 195 bitstatus = SET;
mbed_official 130:1dec54e4aec3 196 }
mbed_official 130:1dec54e4aec3 197 else
mbed_official 130:1dec54e4aec3 198 {
mbed_official 130:1dec54e4aec3 199 bitstatus = RESET;
mbed_official 130:1dec54e4aec3 200 }
mbed_official 130:1dec54e4aec3 201 /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
mbed_official 130:1dec54e4aec3 202 return bitstatus;
mbed_official 130:1dec54e4aec3 203 }
mbed_official 130:1dec54e4aec3 204
mbed_official 130:1dec54e4aec3 205 /**
mbed_official 130:1dec54e4aec3 206 * @}
mbed_official 130:1dec54e4aec3 207 */
mbed_official 130:1dec54e4aec3 208
mbed_official 130:1dec54e4aec3 209 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
mbed_official 130:1dec54e4aec3 210 * @brief FLASH Memory Programming functions
mbed_official 130:1dec54e4aec3 211 *
mbed_official 130:1dec54e4aec3 212 @verbatim
mbed_official 130:1dec54e4aec3 213 ===============================================================================
mbed_official 130:1dec54e4aec3 214 ##### FLASH Memory Programming functions #####
mbed_official 130:1dec54e4aec3 215 ===============================================================================
mbed_official 130:1dec54e4aec3 216
mbed_official 130:1dec54e4aec3 217 [..] The FLASH Memory Programming functions, includes the following functions:
mbed_official 130:1dec54e4aec3 218 (+) void FLASH_Unlock(void);
mbed_official 130:1dec54e4aec3 219 (+) void FLASH_Lock(void);
mbed_official 130:1dec54e4aec3 220 (+) FLASH_Status FLASH_ErasePage(uint32_t Page_Address);
mbed_official 130:1dec54e4aec3 221 (+) FLASH_Status FLASH_EraseAllPages(void);
mbed_official 130:1dec54e4aec3 222 (+) FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
mbed_official 130:1dec54e4aec3 223 (+) FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
mbed_official 130:1dec54e4aec3 224
mbed_official 130:1dec54e4aec3 225 [..] Any operation of erase or program should follow these steps:
mbed_official 130:1dec54e4aec3 226
mbed_official 130:1dec54e4aec3 227 (#) Call the FLASH_Unlock() function to enable the flash control register and
mbed_official 130:1dec54e4aec3 228 program memory access
mbed_official 130:1dec54e4aec3 229 (#) Call the desired function to erase page or program data
mbed_official 130:1dec54e4aec3 230 (#) Call the FLASH_Lock() to disable the flash program memory access
mbed_official 130:1dec54e4aec3 231 (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 232
mbed_official 130:1dec54e4aec3 233 @endverbatim
mbed_official 130:1dec54e4aec3 234 * @{
mbed_official 130:1dec54e4aec3 235 */
mbed_official 130:1dec54e4aec3 236
mbed_official 130:1dec54e4aec3 237 /**
mbed_official 130:1dec54e4aec3 238 * @brief Unlocks the FLASH control register and program memory access.
mbed_official 130:1dec54e4aec3 239 * @param None
mbed_official 130:1dec54e4aec3 240 * @retval None
mbed_official 130:1dec54e4aec3 241 */
mbed_official 130:1dec54e4aec3 242 void FLASH_Unlock(void)
mbed_official 130:1dec54e4aec3 243 {
mbed_official 130:1dec54e4aec3 244 if((FLASH->CR & FLASH_CR_LOCK) != RESET)
mbed_official 130:1dec54e4aec3 245 {
mbed_official 130:1dec54e4aec3 246 /* Unlocking the program memory access */
mbed_official 130:1dec54e4aec3 247 FLASH->KEYR = FLASH_FKEY1;
mbed_official 130:1dec54e4aec3 248 FLASH->KEYR = FLASH_FKEY2;
mbed_official 130:1dec54e4aec3 249 }
mbed_official 130:1dec54e4aec3 250 }
mbed_official 130:1dec54e4aec3 251
mbed_official 130:1dec54e4aec3 252 /**
mbed_official 130:1dec54e4aec3 253 * @brief Locks the Program memory access.
mbed_official 130:1dec54e4aec3 254 * @param None
mbed_official 130:1dec54e4aec3 255 * @retval None
mbed_official 130:1dec54e4aec3 256 */
mbed_official 130:1dec54e4aec3 257 void FLASH_Lock(void)
mbed_official 130:1dec54e4aec3 258 {
mbed_official 130:1dec54e4aec3 259 /* Set the LOCK Bit to lock the FLASH control register and program memory access */
mbed_official 130:1dec54e4aec3 260 FLASH->CR |= FLASH_CR_LOCK;
mbed_official 130:1dec54e4aec3 261 }
mbed_official 130:1dec54e4aec3 262
mbed_official 130:1dec54e4aec3 263 /**
mbed_official 130:1dec54e4aec3 264 * @brief Erases a specified page in program memory.
mbed_official 130:1dec54e4aec3 265 * @note To correctly run this function, the FLASH_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 266 * @note Call the FLASH_Lock() to disable the flash memory access (recommended
mbed_official 130:1dec54e4aec3 267 * to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 268 * @param Page_Address: The page address in program memory to be erased.
mbed_official 130:1dec54e4aec3 269 * @note A Page is erased in the Program memory only if the address to load
mbed_official 130:1dec54e4aec3 270 * is the start address of a page (multiple of 1024 bytes).
mbed_official 130:1dec54e4aec3 271 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 272 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 273 */
mbed_official 130:1dec54e4aec3 274 FLASH_Status FLASH_ErasePage(uint32_t Page_Address)
mbed_official 130:1dec54e4aec3 275 {
mbed_official 130:1dec54e4aec3 276 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 277
mbed_official 130:1dec54e4aec3 278 /* Check the parameters */
mbed_official 130:1dec54e4aec3 279 assert_param(IS_FLASH_PROGRAM_ADDRESS(Page_Address));
mbed_official 130:1dec54e4aec3 280
mbed_official 130:1dec54e4aec3 281 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 282 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 283
mbed_official 130:1dec54e4aec3 284 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 285 {
mbed_official 130:1dec54e4aec3 286 /* If the previous operation is completed, proceed to erase the page */
mbed_official 130:1dec54e4aec3 287 FLASH->CR |= FLASH_CR_PER;
mbed_official 130:1dec54e4aec3 288 FLASH->AR = Page_Address;
mbed_official 130:1dec54e4aec3 289 FLASH->CR |= FLASH_CR_STRT;
mbed_official 130:1dec54e4aec3 290
mbed_official 130:1dec54e4aec3 291 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 292 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 293
mbed_official 130:1dec54e4aec3 294 /* Disable the PER Bit */
mbed_official 130:1dec54e4aec3 295 FLASH->CR &= ~FLASH_CR_PER;
mbed_official 130:1dec54e4aec3 296 }
mbed_official 130:1dec54e4aec3 297
mbed_official 130:1dec54e4aec3 298 /* Return the Erase Status */
mbed_official 130:1dec54e4aec3 299 return status;
mbed_official 130:1dec54e4aec3 300 }
mbed_official 130:1dec54e4aec3 301
mbed_official 130:1dec54e4aec3 302 /**
mbed_official 130:1dec54e4aec3 303 * @brief Erases all FLASH pages.
mbed_official 130:1dec54e4aec3 304 * @note To correctly run this function, the FLASH_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 305 * @note Call the FLASH_Lock() to disable the flash memory access (recommended
mbed_official 130:1dec54e4aec3 306 * to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 307 * @param None
mbed_official 130:1dec54e4aec3 308 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
mbed_official 130:1dec54e4aec3 309 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 310 */
mbed_official 130:1dec54e4aec3 311 FLASH_Status FLASH_EraseAllPages(void)
mbed_official 130:1dec54e4aec3 312 {
mbed_official 130:1dec54e4aec3 313 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 314
mbed_official 130:1dec54e4aec3 315 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 316 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 317
mbed_official 130:1dec54e4aec3 318 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 319 {
mbed_official 130:1dec54e4aec3 320 /* if the previous operation is completed, proceed to erase all pages */
mbed_official 130:1dec54e4aec3 321 FLASH->CR |= FLASH_CR_MER;
mbed_official 130:1dec54e4aec3 322 FLASH->CR |= FLASH_CR_STRT;
mbed_official 130:1dec54e4aec3 323
mbed_official 130:1dec54e4aec3 324 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 325 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 326
mbed_official 130:1dec54e4aec3 327 /* Disable the MER Bit */
mbed_official 130:1dec54e4aec3 328 FLASH->CR &= ~FLASH_CR_MER;
mbed_official 130:1dec54e4aec3 329 }
mbed_official 130:1dec54e4aec3 330
mbed_official 130:1dec54e4aec3 331 /* Return the Erase Status */
mbed_official 130:1dec54e4aec3 332 return status;
mbed_official 130:1dec54e4aec3 333 }
mbed_official 130:1dec54e4aec3 334
mbed_official 130:1dec54e4aec3 335 /**
mbed_official 130:1dec54e4aec3 336 * @brief Programs a word at a specified address.
mbed_official 130:1dec54e4aec3 337 * @note To correctly run this function, the FLASH_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 338 * @note Call the FLASH_Lock() to disable the flash memory access (recommended
mbed_official 130:1dec54e4aec3 339 * to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 340 * @param Address: specifies the address to be programmed.
mbed_official 130:1dec54e4aec3 341 * @param Data: specifies the data to be programmed.
mbed_official 130:1dec54e4aec3 342 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
mbed_official 130:1dec54e4aec3 343 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 344 */
mbed_official 130:1dec54e4aec3 345 FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data)
mbed_official 130:1dec54e4aec3 346 {
mbed_official 130:1dec54e4aec3 347 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 348 __IO uint32_t tmp = 0;
mbed_official 130:1dec54e4aec3 349
mbed_official 130:1dec54e4aec3 350 /* Check the parameters */
mbed_official 130:1dec54e4aec3 351 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
mbed_official 130:1dec54e4aec3 352
mbed_official 130:1dec54e4aec3 353 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 354 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 355
mbed_official 130:1dec54e4aec3 356 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 357 {
mbed_official 130:1dec54e4aec3 358 /* If the previous operation is completed, proceed to program the new first
mbed_official 130:1dec54e4aec3 359 half word */
mbed_official 130:1dec54e4aec3 360 FLASH->CR |= FLASH_CR_PG;
mbed_official 130:1dec54e4aec3 361
mbed_official 130:1dec54e4aec3 362 *(__IO uint16_t*)Address = (uint16_t)Data;
mbed_official 130:1dec54e4aec3 363
mbed_official 130:1dec54e4aec3 364 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 365 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 366
mbed_official 130:1dec54e4aec3 367 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 368 {
mbed_official 130:1dec54e4aec3 369 /* If the previous operation is completed, proceed to program the new second
mbed_official 130:1dec54e4aec3 370 half word */
mbed_official 130:1dec54e4aec3 371 tmp = Address + 2;
mbed_official 130:1dec54e4aec3 372
mbed_official 130:1dec54e4aec3 373 *(__IO uint16_t*) tmp = Data >> 16;
mbed_official 130:1dec54e4aec3 374
mbed_official 130:1dec54e4aec3 375 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 376 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 377
mbed_official 130:1dec54e4aec3 378 /* Disable the PG Bit */
mbed_official 130:1dec54e4aec3 379 FLASH->CR &= ~FLASH_CR_PG;
mbed_official 130:1dec54e4aec3 380 }
mbed_official 130:1dec54e4aec3 381 else
mbed_official 130:1dec54e4aec3 382 {
mbed_official 130:1dec54e4aec3 383 /* Disable the PG Bit */
mbed_official 130:1dec54e4aec3 384 FLASH->CR &= ~FLASH_CR_PG;
mbed_official 130:1dec54e4aec3 385 }
mbed_official 130:1dec54e4aec3 386 }
mbed_official 130:1dec54e4aec3 387
mbed_official 130:1dec54e4aec3 388 /* Return the Program Status */
mbed_official 130:1dec54e4aec3 389 return status;
mbed_official 130:1dec54e4aec3 390 }
mbed_official 130:1dec54e4aec3 391
mbed_official 130:1dec54e4aec3 392 /**
mbed_official 130:1dec54e4aec3 393 * @brief Programs a half word at a specified address.
mbed_official 130:1dec54e4aec3 394 * @note To correctly run this function, the FLASH_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 395 * @note Call the FLASH_Lock() to disable the flash memory access (recommended
mbed_official 130:1dec54e4aec3 396 * to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 397 * @param Address: specifies the address to be programmed.
mbed_official 130:1dec54e4aec3 398 * @param Data: specifies the data to be programmed.
mbed_official 130:1dec54e4aec3 399 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
mbed_official 130:1dec54e4aec3 400 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 401 */
mbed_official 130:1dec54e4aec3 402 FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data)
mbed_official 130:1dec54e4aec3 403 {
mbed_official 130:1dec54e4aec3 404 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 405
mbed_official 130:1dec54e4aec3 406 /* Check the parameters */
mbed_official 130:1dec54e4aec3 407 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
mbed_official 130:1dec54e4aec3 408
mbed_official 130:1dec54e4aec3 409 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 410 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 411
mbed_official 130:1dec54e4aec3 412 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 413 {
mbed_official 130:1dec54e4aec3 414 /* If the previous operation is completed, proceed to program the new data */
mbed_official 130:1dec54e4aec3 415 FLASH->CR |= FLASH_CR_PG;
mbed_official 130:1dec54e4aec3 416
mbed_official 130:1dec54e4aec3 417 *(__IO uint16_t*)Address = Data;
mbed_official 130:1dec54e4aec3 418
mbed_official 130:1dec54e4aec3 419 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 420 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 421
mbed_official 130:1dec54e4aec3 422 /* Disable the PG Bit */
mbed_official 130:1dec54e4aec3 423 FLASH->CR &= ~FLASH_CR_PG;
mbed_official 130:1dec54e4aec3 424 }
mbed_official 130:1dec54e4aec3 425
mbed_official 130:1dec54e4aec3 426 /* Return the Program Status */
mbed_official 130:1dec54e4aec3 427 return status;
mbed_official 130:1dec54e4aec3 428 }
mbed_official 130:1dec54e4aec3 429
mbed_official 130:1dec54e4aec3 430 /**
mbed_official 130:1dec54e4aec3 431 * @}
mbed_official 130:1dec54e4aec3 432 */
mbed_official 130:1dec54e4aec3 433
mbed_official 130:1dec54e4aec3 434 /** @defgroup FLASH_Group3 Option Bytes Programming functions
mbed_official 130:1dec54e4aec3 435 * @brief Option Bytes Programming functions
mbed_official 130:1dec54e4aec3 436 *
mbed_official 130:1dec54e4aec3 437 @verbatim
mbed_official 130:1dec54e4aec3 438 ===============================================================================
mbed_official 130:1dec54e4aec3 439 ##### Option Bytes Programming functions #####
mbed_official 130:1dec54e4aec3 440 ===============================================================================
mbed_official 130:1dec54e4aec3 441
mbed_official 130:1dec54e4aec3 442 [..] The FLASH_Option Bytes Programming_functions, includes the following functions:
mbed_official 130:1dec54e4aec3 443 (+) void FLASH_OB_Unlock(void);
mbed_official 130:1dec54e4aec3 444 (+) void FLASH_OB_Lock(void);
mbed_official 130:1dec54e4aec3 445 (+) void FLASH_OB_Launch(void);
mbed_official 130:1dec54e4aec3 446 (+) FLASH_Status FLASH_OB_Erase(void);
mbed_official 130:1dec54e4aec3 447 (+) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState);
mbed_official 130:1dec54e4aec3 448 (+) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP);
mbed_official 130:1dec54e4aec3 449 (+) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY);
mbed_official 130:1dec54e4aec3 450 (+) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1);
mbed_official 130:1dec54e4aec3 451 (+) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG);
mbed_official 130:1dec54e4aec3 452 (+) FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER);
mbed_official 130:1dec54e4aec3 453 (+) FLASH_OB_ProgramData(uint32_t Address, uint8_t Data);
mbed_official 130:1dec54e4aec3 454 (+) uint8_t FLASH_OB_GetUser(void);
mbed_official 130:1dec54e4aec3 455 (+) uint32_t FLASH_OB_GetWRP(void);
mbed_official 130:1dec54e4aec3 456 (+) FlagStatus FLASH_OB_GetRDP(void);
mbed_official 130:1dec54e4aec3 457
mbed_official 130:1dec54e4aec3 458 [..] Any operation of erase or program should follow these steps:
mbed_official 130:1dec54e4aec3 459
mbed_official 130:1dec54e4aec3 460 (#) Call the FLASH_OB_Unlock() function to enable the Option Bytes registers access
mbed_official 130:1dec54e4aec3 461
mbed_official 130:1dec54e4aec3 462 (#) Call one or several functions to program the desired option bytes
mbed_official 130:1dec54e4aec3 463 (++) FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP) => to set the desired read Protection Level
mbed_official 130:1dec54e4aec3 464 (++) FLASH_Status FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 465 => to Enable/Disable the desired sector write protection
mbed_official 130:1dec54e4aec3 466 (++) FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
mbed_official 130:1dec54e4aec3 467 => to configure the user option Bytes: IWDG, STOP and the Standby.
mbed_official 130:1dec54e4aec3 468 (++) FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
mbed_official 130:1dec54e4aec3 469 => to set or reset BOOT1
mbed_official 130:1dec54e4aec3 470 (++) FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
mbed_official 130:1dec54e4aec3 471 => to enable or disable the VDDA Analog Monitoring
mbed_official 130:1dec54e4aec3 472 (++) You can write all User Options bytes at once using a single function
mbed_official 130:1dec54e4aec3 473 by calling FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
mbed_official 130:1dec54e4aec3 474 (++) FLASH_OB_ProgramData(uint32_t Address, uint8_t Data) to program the
mbed_official 130:1dec54e4aec3 475 two half word in the option bytes
mbed_official 130:1dec54e4aec3 476
mbed_official 130:1dec54e4aec3 477 (#) Once all needed option bytes to be programmed are correctly written, call the
mbed_official 130:1dec54e4aec3 478 FLASH_OB_Launch(void) function to launch the Option Bytes programming process.
mbed_official 130:1dec54e4aec3 479
mbed_official 130:1dec54e4aec3 480 (#) Call the FLASH_OB_Lock() to disable the Option Bytes registers access (recommended
mbed_official 130:1dec54e4aec3 481 to protect the option Bytes against possible unwanted operations)
mbed_official 130:1dec54e4aec3 482
mbed_official 130:1dec54e4aec3 483 @endverbatim
mbed_official 130:1dec54e4aec3 484 * @{
mbed_official 130:1dec54e4aec3 485 */
mbed_official 130:1dec54e4aec3 486
mbed_official 130:1dec54e4aec3 487 /**
mbed_official 130:1dec54e4aec3 488 * @brief Unlocks the option bytes block access.
mbed_official 130:1dec54e4aec3 489 * @param None
mbed_official 130:1dec54e4aec3 490 * @retval None
mbed_official 130:1dec54e4aec3 491 */
mbed_official 130:1dec54e4aec3 492 void FLASH_OB_Unlock(void)
mbed_official 130:1dec54e4aec3 493 {
mbed_official 130:1dec54e4aec3 494 if((FLASH->CR & FLASH_CR_OPTWRE) == RESET)
mbed_official 130:1dec54e4aec3 495 {
mbed_official 130:1dec54e4aec3 496 /* Unlocking the option bytes block access */
mbed_official 130:1dec54e4aec3 497 FLASH->OPTKEYR = FLASH_OPTKEY1;
mbed_official 130:1dec54e4aec3 498 FLASH->OPTKEYR = FLASH_OPTKEY2;
mbed_official 130:1dec54e4aec3 499 }
mbed_official 130:1dec54e4aec3 500 }
mbed_official 130:1dec54e4aec3 501
mbed_official 130:1dec54e4aec3 502 /**
mbed_official 130:1dec54e4aec3 503 * @brief Locks the option bytes block access.
mbed_official 130:1dec54e4aec3 504 * @param None
mbed_official 130:1dec54e4aec3 505 * @retval None
mbed_official 130:1dec54e4aec3 506 */
mbed_official 130:1dec54e4aec3 507 void FLASH_OB_Lock(void)
mbed_official 130:1dec54e4aec3 508 {
mbed_official 130:1dec54e4aec3 509 /* Set the OPTWREN Bit to lock the option bytes block access */
mbed_official 130:1dec54e4aec3 510 FLASH->CR &= ~FLASH_CR_OPTWRE;
mbed_official 130:1dec54e4aec3 511 }
mbed_official 130:1dec54e4aec3 512
mbed_official 130:1dec54e4aec3 513 /**
mbed_official 130:1dec54e4aec3 514 * @brief Launch the option byte loading.
mbed_official 130:1dec54e4aec3 515 * @param None
mbed_official 130:1dec54e4aec3 516 * @retval None
mbed_official 130:1dec54e4aec3 517 */
mbed_official 130:1dec54e4aec3 518 void FLASH_OB_Launch(void)
mbed_official 130:1dec54e4aec3 519 {
mbed_official 130:1dec54e4aec3 520 /* Set the OBL_Launch bit to launch the option byte loading */
mbed_official 130:1dec54e4aec3 521 FLASH->CR |= FLASH_CR_OBL_LAUNCH;
mbed_official 130:1dec54e4aec3 522 }
mbed_official 130:1dec54e4aec3 523
mbed_official 130:1dec54e4aec3 524 /**
mbed_official 130:1dec54e4aec3 525 * @brief Erases the FLASH option bytes.
mbed_official 130:1dec54e4aec3 526 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 527 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 528 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 529 * @note This functions erases all option bytes except the Read protection (RDP).
mbed_official 130:1dec54e4aec3 530 * @param None
mbed_official 130:1dec54e4aec3 531 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
mbed_official 130:1dec54e4aec3 532 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 533 */
mbed_official 130:1dec54e4aec3 534 FLASH_Status FLASH_OB_Erase(void)
mbed_official 130:1dec54e4aec3 535 {
mbed_official 130:1dec54e4aec3 536 uint16_t rdptmp = OB_RDP_Level_0;
mbed_official 130:1dec54e4aec3 537
mbed_official 130:1dec54e4aec3 538 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 539
mbed_official 130:1dec54e4aec3 540 /* Get the actual read protection Option Byte value */
mbed_official 130:1dec54e4aec3 541 if(FLASH_OB_GetRDP() != RESET)
mbed_official 130:1dec54e4aec3 542 {
mbed_official 130:1dec54e4aec3 543 rdptmp = 0x00;
mbed_official 130:1dec54e4aec3 544 }
mbed_official 130:1dec54e4aec3 545
mbed_official 130:1dec54e4aec3 546 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 547 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 548
mbed_official 130:1dec54e4aec3 549 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 550 {
mbed_official 130:1dec54e4aec3 551 /* If the previous operation is completed, proceed to erase the option bytes */
mbed_official 130:1dec54e4aec3 552 FLASH->CR |= FLASH_CR_OPTER;
mbed_official 130:1dec54e4aec3 553 FLASH->CR |= FLASH_CR_STRT;
mbed_official 130:1dec54e4aec3 554
mbed_official 130:1dec54e4aec3 555 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 556 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 557
mbed_official 130:1dec54e4aec3 558 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 559 {
mbed_official 130:1dec54e4aec3 560 /* If the erase operation is completed, disable the OPTER Bit */
mbed_official 130:1dec54e4aec3 561 FLASH->CR &= ~FLASH_CR_OPTER;
mbed_official 130:1dec54e4aec3 562
mbed_official 130:1dec54e4aec3 563 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 564 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 565
mbed_official 130:1dec54e4aec3 566 /* Restore the last read protection Option Byte value */
mbed_official 130:1dec54e4aec3 567 OB->RDP = (uint16_t)rdptmp;
mbed_official 130:1dec54e4aec3 568
mbed_official 130:1dec54e4aec3 569 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 570 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 571
mbed_official 130:1dec54e4aec3 572 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 573 {
mbed_official 130:1dec54e4aec3 574 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 575 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 576 }
mbed_official 130:1dec54e4aec3 577 }
mbed_official 130:1dec54e4aec3 578 else
mbed_official 130:1dec54e4aec3 579 {
mbed_official 130:1dec54e4aec3 580 if (status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 581 {
mbed_official 130:1dec54e4aec3 582 /* Disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 583 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 584 }
mbed_official 130:1dec54e4aec3 585 }
mbed_official 130:1dec54e4aec3 586 }
mbed_official 130:1dec54e4aec3 587 /* Return the erase status */
mbed_official 130:1dec54e4aec3 588 return status;
mbed_official 130:1dec54e4aec3 589 }
mbed_official 130:1dec54e4aec3 590
mbed_official 130:1dec54e4aec3 591 /**
mbed_official 130:1dec54e4aec3 592 * @brief Write protects the desired pages
mbed_official 130:1dec54e4aec3 593 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 594 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 595 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 596 * @param OB_WRP: specifies the address of the pages to be write protected.
mbed_official 130:1dec54e4aec3 597 * This parameter can be:
mbed_official 130:1dec54e4aec3 598 * @arg OB_WRP_Pages0to3..OB_WRP_Pages60to63
mbed_official 130:1dec54e4aec3 599 * @arg OB_WRP_AllPages
mbed_official 130:1dec54e4aec3 600 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 601 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 602 */
mbed_official 130:1dec54e4aec3 603 FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP)
mbed_official 130:1dec54e4aec3 604 {
mbed_official 130:1dec54e4aec3 605 uint16_t WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
mbed_official 130:1dec54e4aec3 606
mbed_official 130:1dec54e4aec3 607 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 608
mbed_official 130:1dec54e4aec3 609 /* Check the parameters */
mbed_official 130:1dec54e4aec3 610 assert_param(IS_OB_WRP(OB_WRP));
mbed_official 130:1dec54e4aec3 611
mbed_official 130:1dec54e4aec3 612 OB_WRP = (uint32_t)(~OB_WRP);
mbed_official 130:1dec54e4aec3 613 WRP0_Data = (uint16_t)(OB_WRP & OB_WRP0_WRP0);
mbed_official 130:1dec54e4aec3 614 WRP1_Data = (uint16_t)((OB_WRP >> 8) & OB_WRP0_WRP0);
mbed_official 130:1dec54e4aec3 615 WRP2_Data = (uint16_t)((OB_WRP >> 16) & OB_WRP0_WRP0) ;
mbed_official 130:1dec54e4aec3 616 WRP3_Data = (uint16_t)((OB_WRP >> 24) & OB_WRP0_WRP0) ;
mbed_official 130:1dec54e4aec3 617
mbed_official 130:1dec54e4aec3 618 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 619 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 620
mbed_official 130:1dec54e4aec3 621 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 622 {
mbed_official 130:1dec54e4aec3 623 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 624
mbed_official 130:1dec54e4aec3 625 if(WRP0_Data != 0xFF)
mbed_official 130:1dec54e4aec3 626 {
mbed_official 130:1dec54e4aec3 627 OB->WRP0 = WRP0_Data;
mbed_official 130:1dec54e4aec3 628
mbed_official 130:1dec54e4aec3 629 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 630 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 631 }
mbed_official 130:1dec54e4aec3 632 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
mbed_official 130:1dec54e4aec3 633 {
mbed_official 130:1dec54e4aec3 634 OB->WRP1 = WRP1_Data;
mbed_official 130:1dec54e4aec3 635
mbed_official 130:1dec54e4aec3 636 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 637 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 638 }
mbed_official 130:1dec54e4aec3 639 if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
mbed_official 130:1dec54e4aec3 640 {
mbed_official 130:1dec54e4aec3 641 OB->WRP2 = WRP2_Data;
mbed_official 130:1dec54e4aec3 642
mbed_official 130:1dec54e4aec3 643 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 644 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 645 }
mbed_official 130:1dec54e4aec3 646 if((status == FLASH_COMPLETE) && (WRP3_Data != 0xFF))
mbed_official 130:1dec54e4aec3 647 {
mbed_official 130:1dec54e4aec3 648 OB->WRP3 = WRP3_Data;
mbed_official 130:1dec54e4aec3 649
mbed_official 130:1dec54e4aec3 650 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 651 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 652 }
mbed_official 130:1dec54e4aec3 653 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 654 {
mbed_official 130:1dec54e4aec3 655 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 656 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 657 }
mbed_official 130:1dec54e4aec3 658 }
mbed_official 130:1dec54e4aec3 659 /* Return the write protection operation Status */
mbed_official 130:1dec54e4aec3 660 return status;
mbed_official 130:1dec54e4aec3 661 }
mbed_official 130:1dec54e4aec3 662
mbed_official 130:1dec54e4aec3 663 /**
mbed_official 130:1dec54e4aec3 664 * @brief Enables or disables the read out protection.
mbed_official 130:1dec54e4aec3 665 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 666 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 667 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 668 * @param FLASH_ReadProtection_Level: specifies the read protection level.
mbed_official 130:1dec54e4aec3 669 * This parameter can be:
mbed_official 130:1dec54e4aec3 670 * @arg OB_RDP_Level_0: No protection
mbed_official 130:1dec54e4aec3 671 * @arg OB_RDP_Level_1: Read protection of the memory
mbed_official 130:1dec54e4aec3 672 * @arg OB_RDP_Level_2: Chip protection
mbed_official 130:1dec54e4aec3 673 * @note When enabling OB_RDP level 2 it's no more possible to go back to level 1 or 0
mbed_official 130:1dec54e4aec3 674 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 675 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 676 */
mbed_official 130:1dec54e4aec3 677 FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP)
mbed_official 130:1dec54e4aec3 678 {
mbed_official 130:1dec54e4aec3 679 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 680
mbed_official 130:1dec54e4aec3 681 /* Check the parameters */
mbed_official 130:1dec54e4aec3 682 assert_param(IS_OB_RDP(OB_RDP));
mbed_official 130:1dec54e4aec3 683 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 684
mbed_official 130:1dec54e4aec3 685 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 686 {
mbed_official 130:1dec54e4aec3 687 FLASH->CR |= FLASH_CR_OPTER;
mbed_official 130:1dec54e4aec3 688 FLASH->CR |= FLASH_CR_STRT;
mbed_official 130:1dec54e4aec3 689
mbed_official 130:1dec54e4aec3 690 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 691 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 692
mbed_official 130:1dec54e4aec3 693 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 694 {
mbed_official 130:1dec54e4aec3 695 /* If the erase operation is completed, disable the OPTER Bit */
mbed_official 130:1dec54e4aec3 696 FLASH->CR &= ~FLASH_CR_OPTER;
mbed_official 130:1dec54e4aec3 697
mbed_official 130:1dec54e4aec3 698 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 699 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 700
mbed_official 130:1dec54e4aec3 701 OB->RDP = OB_RDP;
mbed_official 130:1dec54e4aec3 702
mbed_official 130:1dec54e4aec3 703 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 704 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 705
mbed_official 130:1dec54e4aec3 706 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 707 {
mbed_official 130:1dec54e4aec3 708 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 709 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 710 }
mbed_official 130:1dec54e4aec3 711 }
mbed_official 130:1dec54e4aec3 712 else
mbed_official 130:1dec54e4aec3 713 {
mbed_official 130:1dec54e4aec3 714 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 715 {
mbed_official 130:1dec54e4aec3 716 /* Disable the OPTER Bit */
mbed_official 130:1dec54e4aec3 717 FLASH->CR &= ~FLASH_CR_OPTER;
mbed_official 130:1dec54e4aec3 718 }
mbed_official 130:1dec54e4aec3 719 }
mbed_official 130:1dec54e4aec3 720 }
mbed_official 130:1dec54e4aec3 721 /* Return the protection operation Status */
mbed_official 130:1dec54e4aec3 722 return status;
mbed_official 130:1dec54e4aec3 723 }
mbed_official 130:1dec54e4aec3 724
mbed_official 130:1dec54e4aec3 725 /**
mbed_official 130:1dec54e4aec3 726 * @brief Programs the FLASH User Option Byte: IWDG_SW / RST_STOP / RST_STDBY.
mbed_official 130:1dec54e4aec3 727 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 728 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 729 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 730 * @param OB_IWDG: Selects the WDG mode
mbed_official 130:1dec54e4aec3 731 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 732 * @arg OB_IWDG_SW: Software WDG selected
mbed_official 130:1dec54e4aec3 733 * @arg OB_IWDG_HW: Hardware WDG selected
mbed_official 130:1dec54e4aec3 734 * @param OB_STOP: Reset event when entering STOP mode.
mbed_official 130:1dec54e4aec3 735 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 736 * @arg OB_STOP_NoRST: No reset generated when entering in STOP
mbed_official 130:1dec54e4aec3 737 * @arg OB_STOP_RST: Reset generated when entering in STOP
mbed_official 130:1dec54e4aec3 738 * @param OB_STDBY: Reset event when entering Standby mode.
mbed_official 130:1dec54e4aec3 739 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 740 * @arg OB_STDBY_NoRST: No reset generated when entering in STANDBY
mbed_official 130:1dec54e4aec3 741 * @arg OB_STDBY_RST: Reset generated when entering in STANDBY
mbed_official 130:1dec54e4aec3 742 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 743 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 744 */
mbed_official 130:1dec54e4aec3 745 FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY)
mbed_official 130:1dec54e4aec3 746 {
mbed_official 130:1dec54e4aec3 747 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 748
mbed_official 130:1dec54e4aec3 749 /* Check the parameters */
mbed_official 130:1dec54e4aec3 750 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
mbed_official 130:1dec54e4aec3 751 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
mbed_official 130:1dec54e4aec3 752 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
mbed_official 130:1dec54e4aec3 753
mbed_official 130:1dec54e4aec3 754 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 755 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 756
mbed_official 130:1dec54e4aec3 757 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 758 {
mbed_official 130:1dec54e4aec3 759 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 760 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 761
mbed_official 130:1dec54e4aec3 762 OB->USER = (uint16_t)((uint16_t)(OB_IWDG | OB_STOP) | (uint16_t)(OB_STDBY | 0xF8));
mbed_official 130:1dec54e4aec3 763
mbed_official 130:1dec54e4aec3 764 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 765 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 766
mbed_official 130:1dec54e4aec3 767 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 768 {
mbed_official 130:1dec54e4aec3 769 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 770 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 771 }
mbed_official 130:1dec54e4aec3 772 }
mbed_official 130:1dec54e4aec3 773 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 774 return status;
mbed_official 130:1dec54e4aec3 775 }
mbed_official 130:1dec54e4aec3 776
mbed_official 130:1dec54e4aec3 777 /**
mbed_official 130:1dec54e4aec3 778 * @brief Sets or resets the BOOT1 option bit.
mbed_official 130:1dec54e4aec3 779 * @param OB_BOOT1: Set or Reset the BOOT1 option bit.
mbed_official 130:1dec54e4aec3 780 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 781 * @arg OB_BOOT1_RESET: BOOT1 option bit reset
mbed_official 130:1dec54e4aec3 782 * @arg OB_BOOT1_SET: BOOT1 option bit set
mbed_official 130:1dec54e4aec3 783 * @retval None
mbed_official 130:1dec54e4aec3 784 */
mbed_official 130:1dec54e4aec3 785 FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT1)
mbed_official 130:1dec54e4aec3 786 {
mbed_official 130:1dec54e4aec3 787 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 788
mbed_official 130:1dec54e4aec3 789 /* Check the parameters */
mbed_official 130:1dec54e4aec3 790 assert_param(IS_OB_BOOT1(OB_BOOT1));
mbed_official 130:1dec54e4aec3 791
mbed_official 130:1dec54e4aec3 792 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 793 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 794
mbed_official 130:1dec54e4aec3 795 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 796 {
mbed_official 130:1dec54e4aec3 797 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 798 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 799
mbed_official 130:1dec54e4aec3 800 OB->USER = OB_BOOT1 | 0xEF;
mbed_official 130:1dec54e4aec3 801
mbed_official 130:1dec54e4aec3 802 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 803 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 804
mbed_official 130:1dec54e4aec3 805 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 806 {
mbed_official 130:1dec54e4aec3 807 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 808 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 809 }
mbed_official 130:1dec54e4aec3 810 }
mbed_official 130:1dec54e4aec3 811 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 812 return status;
mbed_official 130:1dec54e4aec3 813 }
mbed_official 130:1dec54e4aec3 814
mbed_official 130:1dec54e4aec3 815 /**
mbed_official 130:1dec54e4aec3 816 * @brief Sets or resets the BOOT0 option bit.
mbed_official 130:1dec54e4aec3 817 * @note This function is applicable only for the STM32F042 devices.
mbed_official 130:1dec54e4aec3 818 * @param OB_BOOT0: Set or Reset the BOOT0 option bit.
mbed_official 130:1dec54e4aec3 819 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 820 * @arg OB_BOOT0_RESET: BOOT0 option bit reset
mbed_official 130:1dec54e4aec3 821 * @arg OB_BOOT0_SET: BOOT0 option bit set
mbed_official 130:1dec54e4aec3 822 * @retval None
mbed_official 130:1dec54e4aec3 823 */
mbed_official 130:1dec54e4aec3 824 FLASH_Status FLASH_OB_BOOT0Config(uint8_t OB_BOOT0)
mbed_official 130:1dec54e4aec3 825 {
mbed_official 130:1dec54e4aec3 826 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 827
mbed_official 130:1dec54e4aec3 828 /* Check the parameters */
mbed_official 130:1dec54e4aec3 829 assert_param(IS_OB_BOOT0(OB_BOOT0));
mbed_official 130:1dec54e4aec3 830
mbed_official 130:1dec54e4aec3 831 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 832 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 833
mbed_official 130:1dec54e4aec3 834 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 835 {
mbed_official 130:1dec54e4aec3 836 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 837 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 838
mbed_official 130:1dec54e4aec3 839 OB->USER = OB_BOOT0 | 0xF7;
mbed_official 130:1dec54e4aec3 840
mbed_official 130:1dec54e4aec3 841 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 842 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 843
mbed_official 130:1dec54e4aec3 844 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 845 {
mbed_official 130:1dec54e4aec3 846 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 847 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 848 }
mbed_official 130:1dec54e4aec3 849 }
mbed_official 130:1dec54e4aec3 850 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 851 return status;
mbed_official 130:1dec54e4aec3 852 }
mbed_official 130:1dec54e4aec3 853
mbed_official 130:1dec54e4aec3 854 /**
mbed_official 130:1dec54e4aec3 855 * @brief Sets or resets the BOOT0SW option bit.
mbed_official 130:1dec54e4aec3 856 * @note This function is applicable only for the STM32F042 devices.
mbed_official 130:1dec54e4aec3 857 * @param OB_BOOT0SW: Set or Reset the BOOT0_SW option bit.
mbed_official 130:1dec54e4aec3 858 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 859 * @arg OB_BOOT0_SW: BOOT0_SW option bit reset
mbed_official 130:1dec54e4aec3 860 * @arg OB_BOOT0_HW: BOOT0_SW option bit set
mbed_official 130:1dec54e4aec3 861 * @retval None
mbed_official 130:1dec54e4aec3 862 */
mbed_official 130:1dec54e4aec3 863 FLASH_Status FLASH_OB_BOOT0SWConfig(uint8_t OB_BOOT0SW)
mbed_official 130:1dec54e4aec3 864 {
mbed_official 130:1dec54e4aec3 865 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 866
mbed_official 130:1dec54e4aec3 867 /* Check the parameters */
mbed_official 130:1dec54e4aec3 868 assert_param(IS_OB_BOOT0SW(OB_BOOT0SW));
mbed_official 130:1dec54e4aec3 869
mbed_official 130:1dec54e4aec3 870 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 871 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 872
mbed_official 130:1dec54e4aec3 873 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 874 {
mbed_official 130:1dec54e4aec3 875 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 876 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 877
mbed_official 130:1dec54e4aec3 878 OB->USER = OB_BOOT0SW | 0x7F;
mbed_official 130:1dec54e4aec3 879
mbed_official 130:1dec54e4aec3 880 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 881 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 882
mbed_official 130:1dec54e4aec3 883 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 884 {
mbed_official 130:1dec54e4aec3 885 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 886 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 887 }
mbed_official 130:1dec54e4aec3 888 }
mbed_official 130:1dec54e4aec3 889 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 890 return status;
mbed_official 130:1dec54e4aec3 891 }
mbed_official 130:1dec54e4aec3 892
mbed_official 130:1dec54e4aec3 893 /**
mbed_official 130:1dec54e4aec3 894 * @brief Sets or resets the analogue monitoring on VDDA Power source.
mbed_official 130:1dec54e4aec3 895 * @param OB_VDDA_ANALOG: Selects the analog monitoring on VDDA Power source.
mbed_official 130:1dec54e4aec3 896 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 897 * @arg OB_VDDA_ANALOG_ON: Analog monitoring on VDDA Power source ON
mbed_official 130:1dec54e4aec3 898 * @arg OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source OFF
mbed_official 130:1dec54e4aec3 899 * @retval None
mbed_official 130:1dec54e4aec3 900 */
mbed_official 130:1dec54e4aec3 901 FLASH_Status FLASH_OB_VDDAConfig(uint8_t OB_VDDA_ANALOG)
mbed_official 130:1dec54e4aec3 902 {
mbed_official 130:1dec54e4aec3 903 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 904
mbed_official 130:1dec54e4aec3 905 /* Check the parameters */
mbed_official 130:1dec54e4aec3 906 assert_param(IS_OB_VDDA_ANALOG(OB_VDDA_ANALOG));
mbed_official 130:1dec54e4aec3 907
mbed_official 130:1dec54e4aec3 908 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 909 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 910
mbed_official 130:1dec54e4aec3 911 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 912 {
mbed_official 130:1dec54e4aec3 913 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 914 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 915
mbed_official 130:1dec54e4aec3 916 OB->USER = OB_VDDA_ANALOG | 0xDF;
mbed_official 130:1dec54e4aec3 917
mbed_official 130:1dec54e4aec3 918 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 919 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 920
mbed_official 130:1dec54e4aec3 921 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 922 {
mbed_official 130:1dec54e4aec3 923 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 924 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 925 }
mbed_official 130:1dec54e4aec3 926 }
mbed_official 130:1dec54e4aec3 927 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 928 return status;
mbed_official 130:1dec54e4aec3 929 }
mbed_official 130:1dec54e4aec3 930
mbed_official 130:1dec54e4aec3 931 /**
mbed_official 130:1dec54e4aec3 932 * @brief Sets or resets the SRAM parity.
mbed_official 130:1dec54e4aec3 933 * @param OB_SRAM_Parity: Set or Reset the SRAM parity enable bit.
mbed_official 130:1dec54e4aec3 934 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 935 * @arg OB_SRAM_PARITY_SET: Set SRAM parity.
mbed_official 130:1dec54e4aec3 936 * @arg OB_SRAM_PARITY_RESET: Reset SRAM parity.
mbed_official 130:1dec54e4aec3 937 * @retval None
mbed_official 130:1dec54e4aec3 938 */
mbed_official 130:1dec54e4aec3 939 FLASH_Status FLASH_OB_SRAMParityConfig(uint8_t OB_SRAM_Parity)
mbed_official 130:1dec54e4aec3 940 {
mbed_official 130:1dec54e4aec3 941 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 942
mbed_official 130:1dec54e4aec3 943 /* Check the parameters */
mbed_official 130:1dec54e4aec3 944 assert_param(IS_OB_SRAM_PARITY(OB_SRAM_Parity));
mbed_official 130:1dec54e4aec3 945
mbed_official 130:1dec54e4aec3 946 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 947 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 948
mbed_official 130:1dec54e4aec3 949 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 950 {
mbed_official 130:1dec54e4aec3 951 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 952 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 953
mbed_official 130:1dec54e4aec3 954 OB->USER = OB_SRAM_Parity | 0xBF;
mbed_official 130:1dec54e4aec3 955
mbed_official 130:1dec54e4aec3 956 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 957 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 958
mbed_official 130:1dec54e4aec3 959 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 960 {
mbed_official 130:1dec54e4aec3 961 /* if the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 962 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 963 }
mbed_official 130:1dec54e4aec3 964 }
mbed_official 130:1dec54e4aec3 965 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 966 return status;
mbed_official 130:1dec54e4aec3 967 }
mbed_official 130:1dec54e4aec3 968
mbed_official 130:1dec54e4aec3 969 /**
mbed_official 130:1dec54e4aec3 970 * @brief Programs the FLASH User Option Byte: IWDG_SW, RST_STOP, RST_STDBY,
mbed_official 130:1dec54e4aec3 971 * BOOT1 and VDDA ANALOG monitoring.
mbed_official 130:1dec54e4aec3 972 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 973 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 974 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 975 * @param OB_USER: Selects all user option bytes
mbed_official 130:1dec54e4aec3 976 * This parameter is a combination of the following values:
mbed_official 130:1dec54e4aec3 977 * @arg OB_IWDG_SW / OB_IWDG_HW: Software / Hardware WDG selected
mbed_official 130:1dec54e4aec3 978 * @arg OB_STOP_NoRST / OB_STOP_RST: No reset / Reset generated when entering in STOP
mbed_official 130:1dec54e4aec3 979 * @arg OB_STDBY_NoRST / OB_STDBY_RST: No reset / Reset generated when entering in STANDBY
mbed_official 130:1dec54e4aec3 980 * @arg OB_BOOT1_RESET / OB_BOOT1_SET: BOOT1 Reset / Set
mbed_official 130:1dec54e4aec3 981 * @arg OB_VDDA_ANALOG_ON / OB_VDDA_ANALOG_OFF: Analog monitoring on VDDA Power source ON / OFF
mbed_official 130:1dec54e4aec3 982 * @arg OB_SRAM_PARITY_SET / OB_SRAM_PARITY_RESET: SRAM Parity SET / RESET
mbed_official 130:1dec54e4aec3 983 * @arg OB_BOOT0_RESET / OB_BOOT0_SET: BOOT0 Reset / Set
mbed_official 130:1dec54e4aec3 984 * @arg OB_BOOT0_SW / OB_BOOT0_SW: BOOT0 pin disabled / BOOT0 pin bonded with GPIO
mbed_official 130:1dec54e4aec3 985 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 986 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 987 */
mbed_official 130:1dec54e4aec3 988 FLASH_Status FLASH_OB_WriteUser(uint8_t OB_USER)
mbed_official 130:1dec54e4aec3 989 {
mbed_official 130:1dec54e4aec3 990 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 991
mbed_official 130:1dec54e4aec3 992 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 993 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 994
mbed_official 130:1dec54e4aec3 995 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 996 {
mbed_official 130:1dec54e4aec3 997 /* Enable the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 998 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 999
mbed_official 130:1dec54e4aec3 1000 OB->USER = OB_USER;
mbed_official 130:1dec54e4aec3 1001
mbed_official 130:1dec54e4aec3 1002 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 1003 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 1004
mbed_official 130:1dec54e4aec3 1005 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 1006 {
mbed_official 130:1dec54e4aec3 1007 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 1008 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 1009 }
mbed_official 130:1dec54e4aec3 1010 }
mbed_official 130:1dec54e4aec3 1011 /* Return the Option Byte program Status */
mbed_official 130:1dec54e4aec3 1012 return status;
mbed_official 130:1dec54e4aec3 1013
mbed_official 130:1dec54e4aec3 1014 }
mbed_official 130:1dec54e4aec3 1015
mbed_official 130:1dec54e4aec3 1016 /**
mbed_official 130:1dec54e4aec3 1017 * @brief Programs a half word at a specified Option Byte Data address.
mbed_official 130:1dec54e4aec3 1018 * @note To correctly run this function, the FLASH_OB_Unlock() function must be called before.
mbed_official 130:1dec54e4aec3 1019 * @note Call the FLASH_OB_Lock() to disable the flash control register access and the option
mbed_official 130:1dec54e4aec3 1020 * bytes (recommended to protect the FLASH memory against possible unwanted operation)
mbed_official 130:1dec54e4aec3 1021 * @param Address: specifies the address to be programmed.
mbed_official 130:1dec54e4aec3 1022 * This parameter can be 0x1FFFF804 or 0x1FFFF806.
mbed_official 130:1dec54e4aec3 1023 * @param Data: specifies the data to be programmed.
mbed_official 130:1dec54e4aec3 1024 * @retval FLASH Status: The returned value can be: FLASH_ERROR_PG,
mbed_official 130:1dec54e4aec3 1025 * FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 1026 */
mbed_official 130:1dec54e4aec3 1027 FLASH_Status FLASH_OB_ProgramData(uint32_t Address, uint8_t Data)
mbed_official 130:1dec54e4aec3 1028 {
mbed_official 130:1dec54e4aec3 1029 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 1030 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1031 assert_param(IS_OB_DATA_ADDRESS(Address));
mbed_official 130:1dec54e4aec3 1032 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 1033
mbed_official 130:1dec54e4aec3 1034 if(status == FLASH_COMPLETE)
mbed_official 130:1dec54e4aec3 1035 {
mbed_official 130:1dec54e4aec3 1036 /* Enables the Option Bytes Programming operation */
mbed_official 130:1dec54e4aec3 1037 FLASH->CR |= FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 1038 *(__IO uint16_t*)Address = Data;
mbed_official 130:1dec54e4aec3 1039
mbed_official 130:1dec54e4aec3 1040 /* Wait for last operation to be completed */
mbed_official 130:1dec54e4aec3 1041 status = FLASH_WaitForLastOperation(FLASH_ER_PRG_TIMEOUT);
mbed_official 130:1dec54e4aec3 1042
mbed_official 130:1dec54e4aec3 1043 if(status != FLASH_TIMEOUT)
mbed_official 130:1dec54e4aec3 1044 {
mbed_official 130:1dec54e4aec3 1045 /* If the program operation is completed, disable the OPTPG Bit */
mbed_official 130:1dec54e4aec3 1046 FLASH->CR &= ~FLASH_CR_OPTPG;
mbed_official 130:1dec54e4aec3 1047 }
mbed_official 130:1dec54e4aec3 1048 }
mbed_official 130:1dec54e4aec3 1049 /* Return the Option Byte Data Program Status */
mbed_official 130:1dec54e4aec3 1050 return status;
mbed_official 130:1dec54e4aec3 1051 }
mbed_official 130:1dec54e4aec3 1052
mbed_official 130:1dec54e4aec3 1053 /**
mbed_official 130:1dec54e4aec3 1054 * @brief Returns the FLASH User Option Bytes values.
mbed_official 130:1dec54e4aec3 1055 * @param None
mbed_official 130:1dec54e4aec3 1056 * @retval The FLASH User Option Bytes .
mbed_official 130:1dec54e4aec3 1057 */
mbed_official 130:1dec54e4aec3 1058 uint8_t FLASH_OB_GetUser(void)
mbed_official 130:1dec54e4aec3 1059 {
mbed_official 130:1dec54e4aec3 1060 /* Return the User Option Byte */
mbed_official 130:1dec54e4aec3 1061 return (uint8_t)(FLASH->OBR >> 8);
mbed_official 130:1dec54e4aec3 1062 }
mbed_official 130:1dec54e4aec3 1063
mbed_official 130:1dec54e4aec3 1064 /**
mbed_official 130:1dec54e4aec3 1065 * @brief Returns the FLASH Write Protection Option Bytes value.
mbed_official 130:1dec54e4aec3 1066 * @param None
mbed_official 130:1dec54e4aec3 1067 * @retval The FLASH Write Protection Option Bytes value
mbed_official 130:1dec54e4aec3 1068 */
mbed_official 130:1dec54e4aec3 1069 uint32_t FLASH_OB_GetWRP(void)
mbed_official 130:1dec54e4aec3 1070 {
mbed_official 130:1dec54e4aec3 1071 /* Return the FLASH write protection Register value */
mbed_official 130:1dec54e4aec3 1072 return (uint32_t)(FLASH->WRPR);
mbed_official 130:1dec54e4aec3 1073 }
mbed_official 130:1dec54e4aec3 1074
mbed_official 130:1dec54e4aec3 1075 /**
mbed_official 130:1dec54e4aec3 1076 * @brief Checks whether the FLASH Read out Protection Status is set or not.
mbed_official 130:1dec54e4aec3 1077 * @param None
mbed_official 130:1dec54e4aec3 1078 * @retval FLASH ReadOut Protection Status(SET or RESET)
mbed_official 130:1dec54e4aec3 1079 */
mbed_official 130:1dec54e4aec3 1080 FlagStatus FLASH_OB_GetRDP(void)
mbed_official 130:1dec54e4aec3 1081 {
mbed_official 130:1dec54e4aec3 1082 FlagStatus readstatus = RESET;
mbed_official 130:1dec54e4aec3 1083
mbed_official 130:1dec54e4aec3 1084 if ((uint8_t)(FLASH->OBR & (FLASH_OBR_RDPRT1 | FLASH_OBR_RDPRT2)) != RESET)
mbed_official 130:1dec54e4aec3 1085 {
mbed_official 130:1dec54e4aec3 1086 readstatus = SET;
mbed_official 130:1dec54e4aec3 1087 }
mbed_official 130:1dec54e4aec3 1088 else
mbed_official 130:1dec54e4aec3 1089 {
mbed_official 130:1dec54e4aec3 1090 readstatus = RESET;
mbed_official 130:1dec54e4aec3 1091 }
mbed_official 130:1dec54e4aec3 1092 return readstatus;
mbed_official 130:1dec54e4aec3 1093 }
mbed_official 130:1dec54e4aec3 1094
mbed_official 130:1dec54e4aec3 1095 /**
mbed_official 130:1dec54e4aec3 1096 * @}
mbed_official 130:1dec54e4aec3 1097 */
mbed_official 130:1dec54e4aec3 1098
mbed_official 130:1dec54e4aec3 1099 /** @defgroup FLASH_Group4 Interrupts and flags management functions
mbed_official 130:1dec54e4aec3 1100 * @brief Interrupts and flags management functions
mbed_official 130:1dec54e4aec3 1101 *
mbed_official 130:1dec54e4aec3 1102 @verbatim
mbed_official 130:1dec54e4aec3 1103 ===============================================================================
mbed_official 130:1dec54e4aec3 1104 ##### Interrupts and flags management functions #####
mbed_official 130:1dec54e4aec3 1105 ===============================================================================
mbed_official 130:1dec54e4aec3 1106
mbed_official 130:1dec54e4aec3 1107 @endverbatim
mbed_official 130:1dec54e4aec3 1108 * @{
mbed_official 130:1dec54e4aec3 1109 */
mbed_official 130:1dec54e4aec3 1110
mbed_official 130:1dec54e4aec3 1111 /**
mbed_official 130:1dec54e4aec3 1112 * @brief Enables or disables the specified FLASH interrupts.
mbed_official 130:1dec54e4aec3 1113 * @param FLASH_IT: specifies the FLASH interrupt sources to be enabled or
mbed_official 130:1dec54e4aec3 1114 * disabled.
mbed_official 130:1dec54e4aec3 1115 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 1116 * @arg FLASH_IT_EOP: FLASH end of programming Interrupt
mbed_official 130:1dec54e4aec3 1117 * @arg FLASH_IT_ERR: FLASH Error Interrupt
mbed_official 130:1dec54e4aec3 1118 * @retval None
mbed_official 130:1dec54e4aec3 1119 */
mbed_official 130:1dec54e4aec3 1120 void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState)
mbed_official 130:1dec54e4aec3 1121 {
mbed_official 130:1dec54e4aec3 1122 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1123 assert_param(IS_FLASH_IT(FLASH_IT));
mbed_official 130:1dec54e4aec3 1124 assert_param(IS_FUNCTIONAL_STATE(NewState));
mbed_official 130:1dec54e4aec3 1125
mbed_official 130:1dec54e4aec3 1126 if(NewState != DISABLE)
mbed_official 130:1dec54e4aec3 1127 {
mbed_official 130:1dec54e4aec3 1128 /* Enable the interrupt sources */
mbed_official 130:1dec54e4aec3 1129 FLASH->CR |= FLASH_IT;
mbed_official 130:1dec54e4aec3 1130 }
mbed_official 130:1dec54e4aec3 1131 else
mbed_official 130:1dec54e4aec3 1132 {
mbed_official 130:1dec54e4aec3 1133 /* Disable the interrupt sources */
mbed_official 130:1dec54e4aec3 1134 FLASH->CR &= ~(uint32_t)FLASH_IT;
mbed_official 130:1dec54e4aec3 1135 }
mbed_official 130:1dec54e4aec3 1136 }
mbed_official 130:1dec54e4aec3 1137
mbed_official 130:1dec54e4aec3 1138 /**
mbed_official 130:1dec54e4aec3 1139 * @brief Checks whether the specified FLASH flag is set or not.
mbed_official 130:1dec54e4aec3 1140 * @param FLASH_FLAG: specifies the FLASH flag to check.
mbed_official 130:1dec54e4aec3 1141 * This parameter can be one of the following values:
mbed_official 130:1dec54e4aec3 1142 * @arg FLASH_FLAG_BSY: FLASH write/erase operations in progress flag
mbed_official 130:1dec54e4aec3 1143 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
mbed_official 130:1dec54e4aec3 1144 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
mbed_official 130:1dec54e4aec3 1145 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
mbed_official 130:1dec54e4aec3 1146 * @retval The new state of FLASH_FLAG (SET or RESET).
mbed_official 130:1dec54e4aec3 1147 */
mbed_official 130:1dec54e4aec3 1148 FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
mbed_official 130:1dec54e4aec3 1149 {
mbed_official 130:1dec54e4aec3 1150 FlagStatus bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1151
mbed_official 130:1dec54e4aec3 1152 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1153 assert_param(IS_FLASH_GET_FLAG(FLASH_FLAG));
mbed_official 130:1dec54e4aec3 1154
mbed_official 130:1dec54e4aec3 1155 if((FLASH->SR & FLASH_FLAG) != (uint32_t)RESET)
mbed_official 130:1dec54e4aec3 1156 {
mbed_official 130:1dec54e4aec3 1157 bitstatus = SET;
mbed_official 130:1dec54e4aec3 1158 }
mbed_official 130:1dec54e4aec3 1159 else
mbed_official 130:1dec54e4aec3 1160 {
mbed_official 130:1dec54e4aec3 1161 bitstatus = RESET;
mbed_official 130:1dec54e4aec3 1162 }
mbed_official 130:1dec54e4aec3 1163 /* Return the new state of FLASH_FLAG (SET or RESET) */
mbed_official 130:1dec54e4aec3 1164 return bitstatus;
mbed_official 130:1dec54e4aec3 1165 }
mbed_official 130:1dec54e4aec3 1166
mbed_official 130:1dec54e4aec3 1167 /**
mbed_official 130:1dec54e4aec3 1168 * @brief Clears the FLASH's pending flags.
mbed_official 130:1dec54e4aec3 1169 * @param FLASH_FLAG: specifies the FLASH flags to clear.
mbed_official 130:1dec54e4aec3 1170 * This parameter can be any combination of the following values:
mbed_official 130:1dec54e4aec3 1171 * @arg FLASH_FLAG_PGERR: FLASH Programming error flag flag
mbed_official 130:1dec54e4aec3 1172 * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
mbed_official 130:1dec54e4aec3 1173 * @arg FLASH_FLAG_EOP: FLASH End of Programming flag
mbed_official 130:1dec54e4aec3 1174 * @retval None
mbed_official 130:1dec54e4aec3 1175 */
mbed_official 130:1dec54e4aec3 1176 void FLASH_ClearFlag(uint32_t FLASH_FLAG)
mbed_official 130:1dec54e4aec3 1177 {
mbed_official 130:1dec54e4aec3 1178 /* Check the parameters */
mbed_official 130:1dec54e4aec3 1179 assert_param(IS_FLASH_CLEAR_FLAG(FLASH_FLAG));
mbed_official 130:1dec54e4aec3 1180
mbed_official 130:1dec54e4aec3 1181 /* Clear the flags */
mbed_official 130:1dec54e4aec3 1182 FLASH->SR = FLASH_FLAG;
mbed_official 130:1dec54e4aec3 1183 }
mbed_official 130:1dec54e4aec3 1184
mbed_official 130:1dec54e4aec3 1185 /**
mbed_official 130:1dec54e4aec3 1186 * @brief Returns the FLASH Status.
mbed_official 130:1dec54e4aec3 1187 * @param None
mbed_official 130:1dec54e4aec3 1188 * @retval FLASH Status: The returned value can be:
mbed_official 130:1dec54e4aec3 1189 * FLASH_BUSY, FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP or FLASH_COMPLETE.
mbed_official 130:1dec54e4aec3 1190 */
mbed_official 130:1dec54e4aec3 1191 FLASH_Status FLASH_GetStatus(void)
mbed_official 130:1dec54e4aec3 1192 {
mbed_official 130:1dec54e4aec3 1193 FLASH_Status FLASHstatus = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 1194
mbed_official 130:1dec54e4aec3 1195 if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY)
mbed_official 130:1dec54e4aec3 1196 {
mbed_official 130:1dec54e4aec3 1197 FLASHstatus = FLASH_BUSY;
mbed_official 130:1dec54e4aec3 1198 }
mbed_official 130:1dec54e4aec3 1199 else
mbed_official 130:1dec54e4aec3 1200 {
mbed_official 130:1dec54e4aec3 1201 if((FLASH->SR & (uint32_t)FLASH_FLAG_WRPERR)!= (uint32_t)0x00)
mbed_official 130:1dec54e4aec3 1202 {
mbed_official 130:1dec54e4aec3 1203 FLASHstatus = FLASH_ERROR_WRP;
mbed_official 130:1dec54e4aec3 1204 }
mbed_official 130:1dec54e4aec3 1205 else
mbed_official 130:1dec54e4aec3 1206 {
mbed_official 130:1dec54e4aec3 1207 if((FLASH->SR & (uint32_t)(FLASH_SR_PGERR)) != (uint32_t)0x00)
mbed_official 130:1dec54e4aec3 1208 {
mbed_official 130:1dec54e4aec3 1209 FLASHstatus = FLASH_ERROR_PROGRAM;
mbed_official 130:1dec54e4aec3 1210 }
mbed_official 130:1dec54e4aec3 1211 else
mbed_official 130:1dec54e4aec3 1212 {
mbed_official 130:1dec54e4aec3 1213 FLASHstatus = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 1214 }
mbed_official 130:1dec54e4aec3 1215 }
mbed_official 130:1dec54e4aec3 1216 }
mbed_official 130:1dec54e4aec3 1217 /* Return the FLASH Status */
mbed_official 130:1dec54e4aec3 1218 return FLASHstatus;
mbed_official 130:1dec54e4aec3 1219 }
mbed_official 130:1dec54e4aec3 1220
mbed_official 130:1dec54e4aec3 1221
mbed_official 130:1dec54e4aec3 1222 /**
mbed_official 130:1dec54e4aec3 1223 * @brief Waits for a FLASH operation to complete or a TIMEOUT to occur.
mbed_official 130:1dec54e4aec3 1224 * @param Timeout: FLASH programming Timeout
mbed_official 130:1dec54e4aec3 1225 * @retval FLASH Status: The returned value can be: FLASH_BUSY,
mbed_official 130:1dec54e4aec3 1226 * FLASH_ERROR_PROGRAM, FLASH_ERROR_WRP, FLASH_COMPLETE or FLASH_TIMEOUT.
mbed_official 130:1dec54e4aec3 1227 */
mbed_official 130:1dec54e4aec3 1228 FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout)
mbed_official 130:1dec54e4aec3 1229 {
mbed_official 130:1dec54e4aec3 1230 FLASH_Status status = FLASH_COMPLETE;
mbed_official 130:1dec54e4aec3 1231
mbed_official 130:1dec54e4aec3 1232 /* Check for the FLASH Status */
mbed_official 130:1dec54e4aec3 1233 status = FLASH_GetStatus();
mbed_official 130:1dec54e4aec3 1234
mbed_official 130:1dec54e4aec3 1235 /* Wait for a FLASH operation to complete or a TIMEOUT to occur */
mbed_official 130:1dec54e4aec3 1236 while((status == FLASH_BUSY) && (Timeout != 0x00))
mbed_official 130:1dec54e4aec3 1237 {
mbed_official 130:1dec54e4aec3 1238 status = FLASH_GetStatus();
mbed_official 130:1dec54e4aec3 1239 Timeout--;
mbed_official 130:1dec54e4aec3 1240 }
mbed_official 130:1dec54e4aec3 1241
mbed_official 130:1dec54e4aec3 1242 if(Timeout == 0x00 )
mbed_official 130:1dec54e4aec3 1243 {
mbed_official 130:1dec54e4aec3 1244 status = FLASH_TIMEOUT;
mbed_official 130:1dec54e4aec3 1245 }
mbed_official 130:1dec54e4aec3 1246 /* Return the operation status */
mbed_official 130:1dec54e4aec3 1247 return status;
mbed_official 130:1dec54e4aec3 1248 }
mbed_official 130:1dec54e4aec3 1249
mbed_official 130:1dec54e4aec3 1250 /**
mbed_official 130:1dec54e4aec3 1251 * @}
mbed_official 130:1dec54e4aec3 1252 */
mbed_official 130:1dec54e4aec3 1253
mbed_official 130:1dec54e4aec3 1254 /**
mbed_official 130:1dec54e4aec3 1255 * @}
mbed_official 130:1dec54e4aec3 1256 */
mbed_official 130:1dec54e4aec3 1257
mbed_official 130:1dec54e4aec3 1258 /**
mbed_official 130:1dec54e4aec3 1259 * @}
mbed_official 130:1dec54e4aec3 1260 */
mbed_official 130:1dec54e4aec3 1261
mbed_official 130:1dec54e4aec3 1262 /**
mbed_official 130:1dec54e4aec3 1263 * @}
mbed_official 130:1dec54e4aec3 1264 */
mbed_official 130:1dec54e4aec3 1265
mbed_official 130:1dec54e4aec3 1266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/