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:
157:90e3acc479a2
test with CLOCK_SETUP = 0

Who changed what in which revision?

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