Modified version of the mbed library for use with the Nucleo boards.

Dependents:   EEPROMWrite Full-Project

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Sep 28 10:45:10 2015 +0100
Revision:
630:825f75ca301e
Parent:
441:d2c15dda23c1
Synchronized with git revision 54fbe4144faf309c37205a5d39fa665daa919f10

Full URL: https://github.com/mbedmicro/mbed/commit/54fbe4144faf309c37205a5d39fa665daa919f10/

NUCLEO_F031K6 : Add new target

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 340:28d1f895c6fe 1 /**
mbed_official 340:28d1f895c6fe 2 ******************************************************************************
mbed_official 340:28d1f895c6fe 3 * @file stm32f0xx_hal_flash.c
mbed_official 340:28d1f895c6fe 4 * @author MCD Application Team
mbed_official 630:825f75ca301e 5 * @version V1.3.0
mbed_official 630:825f75ca301e 6 * @date 26-June-2015
mbed_official 340:28d1f895c6fe 7 * @brief FLASH HAL module driver.
mbed_official 340:28d1f895c6fe 8 * This file provides firmware functions to manage the following
mbed_official 340:28d1f895c6fe 9 * functionalities of the internal FLASH memory:
mbed_official 340:28d1f895c6fe 10 * + Program operations functions
mbed_official 340:28d1f895c6fe 11 * + Memory Control functions
mbed_official 340:28d1f895c6fe 12 * + Peripheral State functions
mbed_official 340:28d1f895c6fe 13 *
mbed_official 340:28d1f895c6fe 14 @verbatim
mbed_official 340:28d1f895c6fe 15 ==============================================================================
mbed_official 340:28d1f895c6fe 16 ##### FLASH peripheral features #####
mbed_official 630:825f75ca301e 17 ==============================================================================
mbed_official 340:28d1f895c6fe 18 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses
mbed_official 340:28d1f895c6fe 19 to the Flash memory. It implements the erase and program Flash memory operations
mbed_official 340:28d1f895c6fe 20 and the read and write protection mechanisms.
mbed_official 630:825f75ca301e 21
mbed_official 340:28d1f895c6fe 22 [..] The Flash memory interface accelerates code execution with a system of instruction
mbed_official 340:28d1f895c6fe 23 prefetch.
mbed_official 340:28d1f895c6fe 24
mbed_official 340:28d1f895c6fe 25 [..] The FLASH main features are:
mbed_official 340:28d1f895c6fe 26 (+) Flash memory read operations
mbed_official 340:28d1f895c6fe 27 (+) Flash memory program/erase operations
mbed_official 340:28d1f895c6fe 28 (+) Read / write protections
mbed_official 340:28d1f895c6fe 29 (+) Prefetch on I-Code
mbed_official 340:28d1f895c6fe 30 (+) Option Bytes programming
mbed_official 630:825f75ca301e 31
mbed_official 630:825f75ca301e 32
mbed_official 340:28d1f895c6fe 33 ##### How to use this driver #####
mbed_official 340:28d1f895c6fe 34 ==============================================================================
mbed_official 340:28d1f895c6fe 35 [..]
mbed_official 340:28d1f895c6fe 36 This driver provides functions and macros to configure and program the FLASH
mbed_official 630:825f75ca301e 37 memory of all STM32F0xx devices.
mbed_official 340:28d1f895c6fe 38
mbed_official 340:28d1f895c6fe 39 (#) FLASH Memory I/O Programming functions: this group includes all needed
mbed_official 340:28d1f895c6fe 40 functions to erase and program the main memory:
mbed_official 340:28d1f895c6fe 41 (++) Lock and Unlock the FLASH interface
mbed_official 340:28d1f895c6fe 42 (++) Erase function: Erase page, erase all pages
mbed_official 441:d2c15dda23c1 43 (++) Program functions: half word, word and doubleword
mbed_official 340:28d1f895c6fe 44
mbed_official 630:825f75ca301e 45 (#) FLASH Option Bytes Programming functions: this group includes all needed
mbed_official 340:28d1f895c6fe 46 functions to manage the Option Bytes:
mbed_official 340:28d1f895c6fe 47 (++) Lock and Unlock the Option Bytes
mbed_official 340:28d1f895c6fe 48 (++) Set/Reset the write protection
mbed_official 340:28d1f895c6fe 49 (++) Set the Read protection Level
mbed_official 340:28d1f895c6fe 50 (++) Program the user Option Bytes
mbed_official 630:825f75ca301e 51 (++) Launch the Option Bytes loader
mbed_official 630:825f75ca301e 52 (++) Erase Option Bytes
mbed_official 340:28d1f895c6fe 53 (++) Program the data Option Bytes
mbed_official 630:825f75ca301e 54 (++) Get the Write protection.
mbed_official 630:825f75ca301e 55 (++) Get the user option bytes.
mbed_official 340:28d1f895c6fe 56
mbed_official 340:28d1f895c6fe 57 (#) Interrupts and flags management functions : this group
mbed_official 340:28d1f895c6fe 58 includes all needed functions to:
mbed_official 340:28d1f895c6fe 59 (++) Handle FLASH interrupts
mbed_official 340:28d1f895c6fe 60 (++) Wait for last FLASH operation according to its status
mbed_official 340:28d1f895c6fe 61 (++) Get error flag status
mbed_official 340:28d1f895c6fe 62
mbed_official 340:28d1f895c6fe 63 [..] In addition to these function, this driver includes a set of macros allowing
mbed_official 340:28d1f895c6fe 64 to handle the following operations:
mbed_official 340:28d1f895c6fe 65
mbed_official 340:28d1f895c6fe 66 (+) Set the latency
mbed_official 340:28d1f895c6fe 67 (+) Enable/Disable the prefetch buffer
mbed_official 340:28d1f895c6fe 68 (+) Enable/Disable the FLASH interrupts
mbed_official 340:28d1f895c6fe 69 (+) Monitor the FLASH flags status
mbed_official 340:28d1f895c6fe 70
mbed_official 340:28d1f895c6fe 71 @endverbatim
mbed_official 340:28d1f895c6fe 72 ******************************************************************************
mbed_official 340:28d1f895c6fe 73 * @attention
mbed_official 340:28d1f895c6fe 74 *
mbed_official 630:825f75ca301e 75 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
mbed_official 340:28d1f895c6fe 76 *
mbed_official 340:28d1f895c6fe 77 * Redistribution and use in source and binary forms, with or without modification,
mbed_official 340:28d1f895c6fe 78 * are permitted provided that the following conditions are met:
mbed_official 340:28d1f895c6fe 79 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 340:28d1f895c6fe 80 * this list of conditions and the following disclaimer.
mbed_official 340:28d1f895c6fe 81 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 340:28d1f895c6fe 82 * this list of conditions and the following disclaimer in the documentation
mbed_official 340:28d1f895c6fe 83 * and/or other materials provided with the distribution.
mbed_official 340:28d1f895c6fe 84 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 340:28d1f895c6fe 85 * may be used to endorse or promote products derived from this software
mbed_official 340:28d1f895c6fe 86 * without specific prior written permission.
mbed_official 340:28d1f895c6fe 87 *
mbed_official 340:28d1f895c6fe 88 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 340:28d1f895c6fe 89 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 340:28d1f895c6fe 90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 340:28d1f895c6fe 91 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 340:28d1f895c6fe 92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 340:28d1f895c6fe 93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 340:28d1f895c6fe 94 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 340:28d1f895c6fe 95 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 340:28d1f895c6fe 96 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 340:28d1f895c6fe 97 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 340:28d1f895c6fe 98 *
mbed_official 340:28d1f895c6fe 99 ******************************************************************************
mbed_official 340:28d1f895c6fe 100 */
mbed_official 340:28d1f895c6fe 101
mbed_official 340:28d1f895c6fe 102 /* Includes ------------------------------------------------------------------*/
mbed_official 340:28d1f895c6fe 103 #include "stm32f0xx_hal.h"
mbed_official 340:28d1f895c6fe 104
mbed_official 340:28d1f895c6fe 105 /** @addtogroup STM32F0xx_HAL_Driver
mbed_official 340:28d1f895c6fe 106 * @{
mbed_official 340:28d1f895c6fe 107 */
mbed_official 340:28d1f895c6fe 108
mbed_official 630:825f75ca301e 109 #ifdef HAL_FLASH_MODULE_ENABLED
mbed_official 630:825f75ca301e 110
mbed_official 630:825f75ca301e 111 /** @defgroup FLASH FLASH
mbed_official 340:28d1f895c6fe 112 * @brief FLASH HAL module driver
mbed_official 340:28d1f895c6fe 113 * @{
mbed_official 340:28d1f895c6fe 114 */
mbed_official 340:28d1f895c6fe 115
mbed_official 340:28d1f895c6fe 116 /* Private typedef -----------------------------------------------------------*/
mbed_official 340:28d1f895c6fe 117 /* Private define ------------------------------------------------------------*/
mbed_official 630:825f75ca301e 118 /** @defgroup FLASH_Private_Constants FLASH Private Constants
mbed_official 441:d2c15dda23c1 119 * @{
mbed_official 441:d2c15dda23c1 120 */
mbed_official 441:d2c15dda23c1 121 /**
mbed_official 441:d2c15dda23c1 122 * @}
mbed_official 441:d2c15dda23c1 123 */
mbed_official 441:d2c15dda23c1 124
mbed_official 630:825f75ca301e 125 /* Private macro ---------------------------- ---------------------------------*/
mbed_official 441:d2c15dda23c1 126 /** @defgroup FLASH_Private_Macros FLASH Private Macros
mbed_official 441:d2c15dda23c1 127 * @{
mbed_official 441:d2c15dda23c1 128 */
mbed_official 441:d2c15dda23c1 129
mbed_official 441:d2c15dda23c1 130 /**
mbed_official 441:d2c15dda23c1 131 * @}
mbed_official 441:d2c15dda23c1 132 */
mbed_official 441:d2c15dda23c1 133
mbed_official 340:28d1f895c6fe 134 /* Private variables ---------------------------------------------------------*/
mbed_official 340:28d1f895c6fe 135 /** @defgroup FLASH_Private_Variables FLASH Private Variables
mbed_official 630:825f75ca301e 136 * @{
mbed_official 630:825f75ca301e 137 */
mbed_official 340:28d1f895c6fe 138 /* Variables used for Erase pages under interruption*/
mbed_official 340:28d1f895c6fe 139 FLASH_ProcessTypeDef pFlash;
mbed_official 340:28d1f895c6fe 140 /**
mbed_official 340:28d1f895c6fe 141 * @}
mbed_official 340:28d1f895c6fe 142 */
mbed_official 630:825f75ca301e 143
mbed_official 340:28d1f895c6fe 144 /* Private function prototypes -----------------------------------------------*/
mbed_official 340:28d1f895c6fe 145 /** @defgroup FLASH_Private_Functions FLASH Private Functions
mbed_official 630:825f75ca301e 146 * @{
mbed_official 630:825f75ca301e 147 */
mbed_official 630:825f75ca301e 148 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data);
mbed_official 630:825f75ca301e 149 static void FLASH_SetErrorCode(void);
mbed_official 340:28d1f895c6fe 150 /**
mbed_official 340:28d1f895c6fe 151 * @}
mbed_official 340:28d1f895c6fe 152 */
mbed_official 340:28d1f895c6fe 153
mbed_official 441:d2c15dda23c1 154 /* Exported functions ---------------------------------------------------------*/
mbed_official 340:28d1f895c6fe 155 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions
mbed_official 340:28d1f895c6fe 156 * @{
mbed_official 340:28d1f895c6fe 157 */
mbed_official 340:28d1f895c6fe 158
mbed_official 630:825f75ca301e 159 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
mbed_official 630:825f75ca301e 160 * @brief Programming operation functions
mbed_official 340:28d1f895c6fe 161 *
mbed_official 340:28d1f895c6fe 162 @verbatim
mbed_official 340:28d1f895c6fe 163 @endverbatim
mbed_official 340:28d1f895c6fe 164 * @{
mbed_official 340:28d1f895c6fe 165 */
mbed_official 340:28d1f895c6fe 166
mbed_official 340:28d1f895c6fe 167 /**
mbed_official 340:28d1f895c6fe 168 * @brief Program halfword, word or double word at a specified address
mbed_official 340:28d1f895c6fe 169 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 340:28d1f895c6fe 170 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
mbed_official 340:28d1f895c6fe 171 *
mbed_official 340:28d1f895c6fe 172 * @note If an erase and a program operations are requested simultaneously,
mbed_official 340:28d1f895c6fe 173 * the erase operation is performed before the program one.
mbed_official 340:28d1f895c6fe 174 *
mbed_official 441:d2c15dda23c1 175 * @note FLASH should be previously erased before new programmation (only exception to this
mbed_official 441:d2c15dda23c1 176 * is when 0x0000 is programmed)
mbed_official 441:d2c15dda23c1 177 *
mbed_official 340:28d1f895c6fe 178 * @param TypeProgram: Indicate the way to program at a specified address.
mbed_official 340:28d1f895c6fe 179 * This parameter can be a value of @ref FLASH_Type_Program
mbed_official 340:28d1f895c6fe 180 * @param Address: Specifies the address to be programmed.
mbed_official 340:28d1f895c6fe 181 * @param Data: Specifies the data to be programmed
mbed_official 340:28d1f895c6fe 182 *
mbed_official 340:28d1f895c6fe 183 * @retval HAL_StatusTypeDef HAL Status
mbed_official 340:28d1f895c6fe 184 */
mbed_official 340:28d1f895c6fe 185 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
mbed_official 340:28d1f895c6fe 186 {
mbed_official 340:28d1f895c6fe 187 HAL_StatusTypeDef status = HAL_ERROR;
mbed_official 340:28d1f895c6fe 188 uint8_t index = 0;
mbed_official 340:28d1f895c6fe 189 uint8_t nbiterations = 0;
mbed_official 340:28d1f895c6fe 190
mbed_official 340:28d1f895c6fe 191 /* Process Locked */
mbed_official 340:28d1f895c6fe 192 __HAL_LOCK(&pFlash);
mbed_official 340:28d1f895c6fe 193
mbed_official 340:28d1f895c6fe 194 /* Check the parameters */
mbed_official 630:825f75ca301e 195 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
mbed_official 340:28d1f895c6fe 196 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
mbed_official 340:28d1f895c6fe 197
mbed_official 630:825f75ca301e 198 /* Wait for last operation to be completed */
mbed_official 630:825f75ca301e 199 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
mbed_official 340:28d1f895c6fe 200
mbed_official 340:28d1f895c6fe 201 if(status == HAL_OK)
mbed_official 340:28d1f895c6fe 202 {
mbed_official 630:825f75ca301e 203 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
mbed_official 340:28d1f895c6fe 204 {
mbed_official 340:28d1f895c6fe 205 /* Program halfword (16-bit) at a specified address. */
mbed_official 340:28d1f895c6fe 206 nbiterations = 1;
mbed_official 340:28d1f895c6fe 207 }
mbed_official 630:825f75ca301e 208 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
mbed_official 340:28d1f895c6fe 209 {
mbed_official 340:28d1f895c6fe 210 /* Program word (32-bit = 2*16-bit) at a specified address. */
mbed_official 340:28d1f895c6fe 211 nbiterations = 2;
mbed_official 340:28d1f895c6fe 212 }
mbed_official 340:28d1f895c6fe 213 else
mbed_official 340:28d1f895c6fe 214 {
mbed_official 340:28d1f895c6fe 215 /* Program double word (64-bit = 4*16-bit) at a specified address. */
mbed_official 340:28d1f895c6fe 216 nbiterations = 4;
mbed_official 340:28d1f895c6fe 217 }
mbed_official 340:28d1f895c6fe 218
mbed_official 340:28d1f895c6fe 219 for (index = 0; index < nbiterations; index++)
mbed_official 340:28d1f895c6fe 220 {
mbed_official 340:28d1f895c6fe 221 FLASH_Program_HalfWord((Address + (2*index)), (uint16_t)(Data >> (16*index)));
mbed_official 340:28d1f895c6fe 222
mbed_official 630:825f75ca301e 223 /* Wait for last operation to be completed */
mbed_official 630:825f75ca301e 224 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
mbed_official 340:28d1f895c6fe 225
mbed_official 630:825f75ca301e 226 /* If the program operation is completed, disable the PG Bit */
mbed_official 630:825f75ca301e 227 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
mbed_official 441:d2c15dda23c1 228 /* In case of error, stop programation procedure */
mbed_official 441:d2c15dda23c1 229 if (status != HAL_OK)
mbed_official 441:d2c15dda23c1 230 {
mbed_official 441:d2c15dda23c1 231 break;
mbed_official 441:d2c15dda23c1 232 }
mbed_official 340:28d1f895c6fe 233 }
mbed_official 340:28d1f895c6fe 234 }
mbed_official 340:28d1f895c6fe 235
mbed_official 340:28d1f895c6fe 236 /* Process Unlocked */
mbed_official 340:28d1f895c6fe 237 __HAL_UNLOCK(&pFlash);
mbed_official 340:28d1f895c6fe 238
mbed_official 340:28d1f895c6fe 239 return status;
mbed_official 340:28d1f895c6fe 240 }
mbed_official 340:28d1f895c6fe 241
mbed_official 340:28d1f895c6fe 242 /**
mbed_official 340:28d1f895c6fe 243 * @brief Program halfword, word or double word at a specified address with interrupt enabled.
mbed_official 340:28d1f895c6fe 244 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface
mbed_official 340:28d1f895c6fe 245 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface
mbed_official 340:28d1f895c6fe 246 *
mbed_official 340:28d1f895c6fe 247 * @note If an erase and a program operations are requested simultaneously,
mbed_official 340:28d1f895c6fe 248 * the erase operation is performed before the program one.
mbed_official 340:28d1f895c6fe 249 *
mbed_official 340:28d1f895c6fe 250 * @param TypeProgram: Indicate the way to program at a specified address.
mbed_official 340:28d1f895c6fe 251 * This parameter can be a value of @ref FLASH_Type_Program
mbed_official 340:28d1f895c6fe 252 * @param Address: Specifies the address to be programmed.
mbed_official 340:28d1f895c6fe 253 * @param Data: Specifies the data to be programmed
mbed_official 340:28d1f895c6fe 254 *
mbed_official 340:28d1f895c6fe 255 * @retval HAL_StatusTypeDef HAL Status
mbed_official 340:28d1f895c6fe 256 */
mbed_official 340:28d1f895c6fe 257 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
mbed_official 340:28d1f895c6fe 258 {
mbed_official 340:28d1f895c6fe 259 HAL_StatusTypeDef status = HAL_OK;
mbed_official 340:28d1f895c6fe 260
mbed_official 340:28d1f895c6fe 261 /* Process Locked */
mbed_official 340:28d1f895c6fe 262 __HAL_LOCK(&pFlash);
mbed_official 340:28d1f895c6fe 263
mbed_official 340:28d1f895c6fe 264 /* Check the parameters */
mbed_official 630:825f75ca301e 265 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
mbed_official 340:28d1f895c6fe 266 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address));
mbed_official 340:28d1f895c6fe 267
mbed_official 340:28d1f895c6fe 268 /* Enable End of FLASH Operation and Error source interrupts */
mbed_official 630:825f75ca301e 269 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
mbed_official 340:28d1f895c6fe 270
mbed_official 340:28d1f895c6fe 271 pFlash.Address = Address;
mbed_official 340:28d1f895c6fe 272 pFlash.Data = Data;
mbed_official 340:28d1f895c6fe 273
mbed_official 630:825f75ca301e 274 if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
mbed_official 340:28d1f895c6fe 275 {
mbed_official 340:28d1f895c6fe 276 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD;
mbed_official 340:28d1f895c6fe 277 /*Program halfword (16-bit) at a specified address.*/
mbed_official 340:28d1f895c6fe 278 pFlash.DataRemaining = 1;
mbed_official 340:28d1f895c6fe 279 }
mbed_official 630:825f75ca301e 280 else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
mbed_official 340:28d1f895c6fe 281 {
mbed_official 340:28d1f895c6fe 282 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD;
mbed_official 340:28d1f895c6fe 283 /*Program word (32-bit : 2*16-bit) at a specified address.*/
mbed_official 340:28d1f895c6fe 284 pFlash.DataRemaining = 2;
mbed_official 340:28d1f895c6fe 285 }
mbed_official 340:28d1f895c6fe 286 else
mbed_official 340:28d1f895c6fe 287 {
mbed_official 340:28d1f895c6fe 288 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD;
mbed_official 340:28d1f895c6fe 289 /*Program double word (64-bit : 4*16-bit) at a specified address.*/
mbed_official 340:28d1f895c6fe 290 pFlash.DataRemaining = 4;
mbed_official 340:28d1f895c6fe 291 }
mbed_official 340:28d1f895c6fe 292
mbed_official 340:28d1f895c6fe 293 /*Program halfword (16-bit) at a specified address.*/
mbed_official 340:28d1f895c6fe 294 FLASH_Program_HalfWord(Address, (uint16_t)Data);
mbed_official 340:28d1f895c6fe 295
mbed_official 340:28d1f895c6fe 296 return status;
mbed_official 340:28d1f895c6fe 297 }
mbed_official 340:28d1f895c6fe 298
mbed_official 340:28d1f895c6fe 299 /**
mbed_official 340:28d1f895c6fe 300 * @brief This function handles FLASH interrupt request.
mbed_official 340:28d1f895c6fe 301 * @retval None
mbed_official 340:28d1f895c6fe 302 */
mbed_official 340:28d1f895c6fe 303 void HAL_FLASH_IRQHandler(void)
mbed_official 340:28d1f895c6fe 304 {
mbed_official 441:d2c15dda23c1 305 uint32_t addresstmp = 0;
mbed_official 630:825f75ca301e 306
mbed_official 441:d2c15dda23c1 307 /* Check FLASH operation error flags */
mbed_official 441:d2c15dda23c1 308 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
mbed_official 441:d2c15dda23c1 309 {
mbed_official 441:d2c15dda23c1 310 /*Save the Error code*/
mbed_official 441:d2c15dda23c1 311 FLASH_SetErrorCode();
mbed_official 441:d2c15dda23c1 312
mbed_official 441:d2c15dda23c1 313 /* FLASH error interrupt user callback */
mbed_official 441:d2c15dda23c1 314 HAL_FLASH_OperationErrorCallback(pFlash.Address);
mbed_official 441:d2c15dda23c1 315
mbed_official 441:d2c15dda23c1 316 /* Reset address and stop the procedure ongoing*/
mbed_official 441:d2c15dda23c1 317 pFlash.Address = 0xFFFFFFFF;
mbed_official 441:d2c15dda23c1 318 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
mbed_official 441:d2c15dda23c1 319 }
mbed_official 340:28d1f895c6fe 320
mbed_official 340:28d1f895c6fe 321 /* Check FLASH End of Operation flag */
mbed_official 340:28d1f895c6fe 322 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
mbed_official 340:28d1f895c6fe 323 {
mbed_official 340:28d1f895c6fe 324 /* Clear FLASH End of Operation pending bit */
mbed_official 340:28d1f895c6fe 325 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
mbed_official 340:28d1f895c6fe 326
mbed_official 441:d2c15dda23c1 327 /* Process can continue only if no error detected */
mbed_official 441:d2c15dda23c1 328 if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE)
mbed_official 441:d2c15dda23c1 329 {
mbed_official 630:825f75ca301e 330 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE)
mbed_official 630:825f75ca301e 331 {
mbed_official 630:825f75ca301e 332 /* Nb of pages to erased can be decreased */
mbed_official 630:825f75ca301e 333 pFlash.DataRemaining--;
mbed_official 340:28d1f895c6fe 334
mbed_official 630:825f75ca301e 335 /* Indicate user which page address has been erased*/
mbed_official 630:825f75ca301e 336 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
mbed_official 340:28d1f895c6fe 337
mbed_official 630:825f75ca301e 338 /* Check if there are still pages to erase*/
mbed_official 630:825f75ca301e 339 if(pFlash.DataRemaining != 0)
mbed_official 630:825f75ca301e 340 {
mbed_official 630:825f75ca301e 341 /* Increment page address to next page */
mbed_official 630:825f75ca301e 342 pFlash.Address += FLASH_PAGE_SIZE;
mbed_official 630:825f75ca301e 343 addresstmp = pFlash.Address;
mbed_official 441:d2c15dda23c1 344
mbed_official 441:d2c15dda23c1 345 /* Operation is completed, disable the PER Bit */
mbed_official 441:d2c15dda23c1 346 CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
mbed_official 441:d2c15dda23c1 347
mbed_official 630:825f75ca301e 348 FLASH_PageErase(addresstmp);
mbed_official 630:825f75ca301e 349 }
mbed_official 630:825f75ca301e 350 else
mbed_official 630:825f75ca301e 351 {
mbed_official 630:825f75ca301e 352 /*No more pages to Erase*/
mbed_official 340:28d1f895c6fe 353
mbed_official 630:825f75ca301e 354 /*Reset Address and stop Erase pages procedure*/
mbed_official 630:825f75ca301e 355 pFlash.Address = 0xFFFFFFFF;
mbed_official 630:825f75ca301e 356 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
mbed_official 630:825f75ca301e 357 }
mbed_official 340:28d1f895c6fe 358 }
mbed_official 630:825f75ca301e 359 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE)
mbed_official 630:825f75ca301e 360 {
mbed_official 441:d2c15dda23c1 361 /* Operation is completed, disable the MER Bit */
mbed_official 441:d2c15dda23c1 362 CLEAR_BIT(FLASH->CR, FLASH_CR_MER);
mbed_official 441:d2c15dda23c1 363
mbed_official 630:825f75ca301e 364 /* MassErase ended. Return the selected bank*/
mbed_official 630:825f75ca301e 365 /* FLASH EOP interrupt user callback */
mbed_official 630:825f75ca301e 366 HAL_FLASH_EndOfOperationCallback(0);
mbed_official 340:28d1f895c6fe 367
mbed_official 630:825f75ca301e 368 /* Stop Mass Erase procedure*/
mbed_official 630:825f75ca301e 369 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
mbed_official 630:825f75ca301e 370 }
mbed_official 630:825f75ca301e 371 else
mbed_official 340:28d1f895c6fe 372 {
mbed_official 630:825f75ca301e 373 /* Nb of 16-bit data to program can be decreased */
mbed_official 630:825f75ca301e 374 pFlash.DataRemaining--;
mbed_official 630:825f75ca301e 375
mbed_official 630:825f75ca301e 376 /* Check if there are still 16-bit data to program */
mbed_official 630:825f75ca301e 377 if(pFlash.DataRemaining != 0)
mbed_official 630:825f75ca301e 378 {
mbed_official 630:825f75ca301e 379 /* Increment address to 16-bit */
mbed_official 630:825f75ca301e 380 pFlash.Address += 2;
mbed_official 630:825f75ca301e 381 addresstmp = pFlash.Address;
mbed_official 630:825f75ca301e 382
mbed_official 630:825f75ca301e 383 /* Shift to have next 16-bit data */
mbed_official 630:825f75ca301e 384 pFlash.Data = (pFlash.Data >> 16);
mbed_official 630:825f75ca301e 385
mbed_official 441:d2c15dda23c1 386 /* Operation is completed, disable the PG Bit */
mbed_official 441:d2c15dda23c1 387 CLEAR_BIT(FLASH->CR, FLASH_CR_PG);
mbed_official 441:d2c15dda23c1 388
mbed_official 630:825f75ca301e 389 /*Program halfword (16-bit) at a specified address.*/
mbed_official 630:825f75ca301e 390 FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data);
mbed_official 340:28d1f895c6fe 391 }
mbed_official 630:825f75ca301e 392 else
mbed_official 340:28d1f895c6fe 393 {
mbed_official 630:825f75ca301e 394 /*Program ended. Return the selected address*/
mbed_official 630:825f75ca301e 395 /* FLASH EOP interrupt user callback */
mbed_official 630:825f75ca301e 396 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD)
mbed_official 630:825f75ca301e 397 {
mbed_official 630:825f75ca301e 398 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
mbed_official 630:825f75ca301e 399 }
mbed_official 630:825f75ca301e 400 else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD)
mbed_official 630:825f75ca301e 401 {
mbed_official 630:825f75ca301e 402 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2);
mbed_official 630:825f75ca301e 403 }
mbed_official 630:825f75ca301e 404 else
mbed_official 630:825f75ca301e 405 {
mbed_official 630:825f75ca301e 406 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6);
mbed_official 630:825f75ca301e 407 }
mbed_official 630:825f75ca301e 408
mbed_official 630:825f75ca301e 409 /* Reset Address and stop Program procedure*/
mbed_official 630:825f75ca301e 410 pFlash.Address = 0xFFFFFFFF;
mbed_official 630:825f75ca301e 411 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
mbed_official 340:28d1f895c6fe 412 }
mbed_official 340:28d1f895c6fe 413 }
mbed_official 340:28d1f895c6fe 414 }
mbed_official 340:28d1f895c6fe 415 }
mbed_official 630:825f75ca301e 416
mbed_official 340:28d1f895c6fe 417
mbed_official 340:28d1f895c6fe 418 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
mbed_official 340:28d1f895c6fe 419 {
mbed_official 441:d2c15dda23c1 420 /* Operation is completed, disable the PG, PER and MER Bits */
mbed_official 441:d2c15dda23c1 421 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER));
mbed_official 441:d2c15dda23c1 422
mbed_official 340:28d1f895c6fe 423 /* Disable End of FLASH Operation and Error source interrupts */
mbed_official 630:825f75ca301e 424 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR);
mbed_official 340:28d1f895c6fe 425
mbed_official 340:28d1f895c6fe 426 /* Process Unlocked */
mbed_official 340:28d1f895c6fe 427 __HAL_UNLOCK(&pFlash);
mbed_official 340:28d1f895c6fe 428 }
mbed_official 340:28d1f895c6fe 429 }
mbed_official 340:28d1f895c6fe 430
mbed_official 340:28d1f895c6fe 431
mbed_official 340:28d1f895c6fe 432 /**
mbed_official 340:28d1f895c6fe 433 * @brief FLASH end of operation interrupt callback
mbed_official 340:28d1f895c6fe 434 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
mbed_official 340:28d1f895c6fe 435 * - Mass Erase: No return value expected
mbed_official 340:28d1f895c6fe 436 * - Pages Erase: Address of the page which has been erased
mbed_official 340:28d1f895c6fe 437 * - Program: Address which was selected for data program
mbed_official 340:28d1f895c6fe 438 * @retval none
mbed_official 340:28d1f895c6fe 439 */
mbed_official 340:28d1f895c6fe 440 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
mbed_official 340:28d1f895c6fe 441 {
mbed_official 340:28d1f895c6fe 442 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 340:28d1f895c6fe 443 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
mbed_official 340:28d1f895c6fe 444 */
mbed_official 340:28d1f895c6fe 445 }
mbed_official 340:28d1f895c6fe 446
mbed_official 340:28d1f895c6fe 447 /**
mbed_official 340:28d1f895c6fe 448 * @brief FLASH operation error interrupt callback
mbed_official 340:28d1f895c6fe 449 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure
mbed_official 340:28d1f895c6fe 450 * - Mass Erase: No return value expected
mbed_official 340:28d1f895c6fe 451 * - Pages Erase: Address of the page which returned an error
mbed_official 340:28d1f895c6fe 452 * - Program: Address which was selected for data program
mbed_official 340:28d1f895c6fe 453 * @retval none
mbed_official 340:28d1f895c6fe 454 */
mbed_official 340:28d1f895c6fe 455 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
mbed_official 340:28d1f895c6fe 456 {
mbed_official 340:28d1f895c6fe 457 /* NOTE : This function Should not be modified, when the callback is needed,
mbed_official 340:28d1f895c6fe 458 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
mbed_official 340:28d1f895c6fe 459 */
mbed_official 340:28d1f895c6fe 460 }
mbed_official 340:28d1f895c6fe 461
mbed_official 340:28d1f895c6fe 462 /**
mbed_official 340:28d1f895c6fe 463 * @}
mbed_official 340:28d1f895c6fe 464 */
mbed_official 340:28d1f895c6fe 465
mbed_official 340:28d1f895c6fe 466 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
mbed_official 340:28d1f895c6fe 467 * @brief management functions
mbed_official 340:28d1f895c6fe 468 *
mbed_official 340:28d1f895c6fe 469 @verbatim
mbed_official 340:28d1f895c6fe 470 ===============================================================================
mbed_official 340:28d1f895c6fe 471 ##### Peripheral Control functions #####
mbed_official 340:28d1f895c6fe 472 ===============================================================================
mbed_official 340:28d1f895c6fe 473 [..]
mbed_official 340:28d1f895c6fe 474 This subsection provides a set of functions allowing to control the FLASH
mbed_official 340:28d1f895c6fe 475 memory operations.
mbed_official 340:28d1f895c6fe 476
mbed_official 340:28d1f895c6fe 477 @endverbatim
mbed_official 340:28d1f895c6fe 478 * @{
mbed_official 340:28d1f895c6fe 479 */
mbed_official 340:28d1f895c6fe 480
mbed_official 340:28d1f895c6fe 481 /**
mbed_official 340:28d1f895c6fe 482 * @brief Unlock the FLASH control register access
mbed_official 340:28d1f895c6fe 483 * @retval HAL Status
mbed_official 340:28d1f895c6fe 484 */
mbed_official 340:28d1f895c6fe 485 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
mbed_official 340:28d1f895c6fe 486 {
mbed_official 630:825f75ca301e 487 if (HAL_IS_BIT_SET(FLASH->CR, FLASH_CR_LOCK))
mbed_official 340:28d1f895c6fe 488 {
mbed_official 340:28d1f895c6fe 489 /* Authorize the FLASH Registers access */
mbed_official 630:825f75ca301e 490 WRITE_REG(FLASH->KEYR, FLASH_KEY1);
mbed_official 630:825f75ca301e 491 WRITE_REG(FLASH->KEYR, FLASH_KEY2);
mbed_official 340:28d1f895c6fe 492 }
mbed_official 340:28d1f895c6fe 493 else
mbed_official 340:28d1f895c6fe 494 {
mbed_official 340:28d1f895c6fe 495 return HAL_ERROR;
mbed_official 340:28d1f895c6fe 496 }
mbed_official 630:825f75ca301e 497
mbed_official 340:28d1f895c6fe 498 return HAL_OK;
mbed_official 340:28d1f895c6fe 499 }
mbed_official 340:28d1f895c6fe 500
mbed_official 340:28d1f895c6fe 501 /**
mbed_official 340:28d1f895c6fe 502 * @brief Locks the FLASH control register access
mbed_official 340:28d1f895c6fe 503 * @retval HAL Status
mbed_official 340:28d1f895c6fe 504 */
mbed_official 340:28d1f895c6fe 505 HAL_StatusTypeDef HAL_FLASH_Lock(void)
mbed_official 340:28d1f895c6fe 506 {
mbed_official 340:28d1f895c6fe 507 /* Set the LOCK Bit to lock the FLASH Registers access */
mbed_official 340:28d1f895c6fe 508 SET_BIT(FLASH->CR, FLASH_CR_LOCK);
mbed_official 340:28d1f895c6fe 509
mbed_official 630:825f75ca301e 510
mbed_official 340:28d1f895c6fe 511 return HAL_OK;
mbed_official 340:28d1f895c6fe 512 }
mbed_official 340:28d1f895c6fe 513
mbed_official 340:28d1f895c6fe 514
mbed_official 340:28d1f895c6fe 515 /**
mbed_official 340:28d1f895c6fe 516 * @brief Unlock the FLASH Option Control Registers access.
mbed_official 340:28d1f895c6fe 517 * @retval HAL Status
mbed_official 340:28d1f895c6fe 518 */
mbed_official 340:28d1f895c6fe 519 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
mbed_official 340:28d1f895c6fe 520 {
mbed_official 630:825f75ca301e 521 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE))
mbed_official 340:28d1f895c6fe 522 {
mbed_official 340:28d1f895c6fe 523 /* Authorizes the Option Byte register programming */
mbed_official 340:28d1f895c6fe 524 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);
mbed_official 340:28d1f895c6fe 525 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);
mbed_official 340:28d1f895c6fe 526 }
mbed_official 340:28d1f895c6fe 527 else
mbed_official 340:28d1f895c6fe 528 {
mbed_official 340:28d1f895c6fe 529 return HAL_ERROR;
mbed_official 340:28d1f895c6fe 530 }
mbed_official 340:28d1f895c6fe 531
mbed_official 340:28d1f895c6fe 532 return HAL_OK;
mbed_official 340:28d1f895c6fe 533 }
mbed_official 340:28d1f895c6fe 534
mbed_official 340:28d1f895c6fe 535 /**
mbed_official 340:28d1f895c6fe 536 * @brief Lock the FLASH Option Control Registers access.
mbed_official 340:28d1f895c6fe 537 * @retval HAL Status
mbed_official 340:28d1f895c6fe 538 */
mbed_official 340:28d1f895c6fe 539 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
mbed_official 340:28d1f895c6fe 540 {
mbed_official 340:28d1f895c6fe 541 /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */
mbed_official 340:28d1f895c6fe 542 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE);
mbed_official 340:28d1f895c6fe 543
mbed_official 340:28d1f895c6fe 544 return HAL_OK;
mbed_official 340:28d1f895c6fe 545 }
mbed_official 630:825f75ca301e 546
mbed_official 340:28d1f895c6fe 547 /**
mbed_official 340:28d1f895c6fe 548 * @brief Launch the option byte loading.
mbed_official 630:825f75ca301e 549 * @note This function will reset automatically the MCU.
mbed_official 630:825f75ca301e 550 * @retval HAL_StatusTypeDef HAL Status
mbed_official 340:28d1f895c6fe 551 */
mbed_official 340:28d1f895c6fe 552 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
mbed_official 340:28d1f895c6fe 553 {
mbed_official 630:825f75ca301e 554 /* Set the OBL_Launch bit to lauch the option byte loading */
mbed_official 630:825f75ca301e 555 SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);
mbed_official 630:825f75ca301e 556
mbed_official 340:28d1f895c6fe 557 /* Wait for last operation to be completed */
mbed_official 630:825f75ca301e 558 return(FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE));
mbed_official 340:28d1f895c6fe 559 }
mbed_official 340:28d1f895c6fe 560
mbed_official 340:28d1f895c6fe 561 /**
mbed_official 340:28d1f895c6fe 562 * @}
mbed_official 630:825f75ca301e 563 */
mbed_official 340:28d1f895c6fe 564
mbed_official 630:825f75ca301e 565 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State functions
mbed_official 630:825f75ca301e 566 * @brief Peripheral State functions
mbed_official 340:28d1f895c6fe 567 *
mbed_official 340:28d1f895c6fe 568 @verbatim
mbed_official 340:28d1f895c6fe 569 ===============================================================================
mbed_official 630:825f75ca301e 570 ##### Peripheral State functions #####
mbed_official 340:28d1f895c6fe 571 ===============================================================================
mbed_official 340:28d1f895c6fe 572 [..]
mbed_official 630:825f75ca301e 573 This subsection permit to get in run-time the status of the FLASH peripheral.
mbed_official 340:28d1f895c6fe 574
mbed_official 340:28d1f895c6fe 575 @endverbatim
mbed_official 340:28d1f895c6fe 576 * @{
mbed_official 340:28d1f895c6fe 577 */
mbed_official 340:28d1f895c6fe 578
mbed_official 340:28d1f895c6fe 579 /**
mbed_official 340:28d1f895c6fe 580 * @brief Get the specific FLASH error flag.
mbed_official 340:28d1f895c6fe 581 * @retval FLASH_ErrorCode: The returned value can be:
mbed_official 630:825f75ca301e 582 * @ref FLASH_Error_Codes
mbed_official 340:28d1f895c6fe 583 */
mbed_official 441:d2c15dda23c1 584 uint32_t HAL_FLASH_GetError(void)
mbed_official 340:28d1f895c6fe 585 {
mbed_official 340:28d1f895c6fe 586 return pFlash.ErrorCode;
mbed_official 630:825f75ca301e 587 }
mbed_official 340:28d1f895c6fe 588 /**
mbed_official 340:28d1f895c6fe 589 * @}
mbed_official 340:28d1f895c6fe 590 */
mbed_official 340:28d1f895c6fe 591
mbed_official 340:28d1f895c6fe 592 /**
mbed_official 340:28d1f895c6fe 593 * @}
mbed_official 630:825f75ca301e 594 */
mbed_official 340:28d1f895c6fe 595
mbed_official 340:28d1f895c6fe 596 /** @addtogroup FLASH_Private_Functions
mbed_official 630:825f75ca301e 597 * @{
mbed_official 630:825f75ca301e 598 */
mbed_official 340:28d1f895c6fe 599 /**
mbed_official 340:28d1f895c6fe 600 * @brief Program a half-word (16-bit) at a specified address.
mbed_official 340:28d1f895c6fe 601 * @param Address: specifies the address to be programmed.
mbed_official 340:28d1f895c6fe 602 * @param Data: specifies the data to be programmed.
mbed_official 340:28d1f895c6fe 603 * @retval None
mbed_official 340:28d1f895c6fe 604 */
mbed_official 340:28d1f895c6fe 605 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data)
mbed_official 340:28d1f895c6fe 606 {
mbed_official 441:d2c15dda23c1 607 /* Clean the error context */
mbed_official 630:825f75ca301e 608 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
mbed_official 630:825f75ca301e 609
mbed_official 630:825f75ca301e 610 /* Proceed to program the new data */
mbed_official 630:825f75ca301e 611 SET_BIT(FLASH->CR, FLASH_CR_PG);
mbed_official 441:d2c15dda23c1 612
mbed_official 630:825f75ca301e 613 /* Write data in the address */
mbed_official 340:28d1f895c6fe 614 *(__IO uint16_t*)Address = Data;
mbed_official 340:28d1f895c6fe 615 }
mbed_official 340:28d1f895c6fe 616
mbed_official 340:28d1f895c6fe 617 /**
mbed_official 340:28d1f895c6fe 618 * @brief Wait for a FLASH operation to complete.
mbed_official 340:28d1f895c6fe 619 * @param Timeout: maximum flash operationtimeout
mbed_official 340:28d1f895c6fe 620 * @retval HAL_StatusTypeDef HAL Status
mbed_official 340:28d1f895c6fe 621 */
mbed_official 340:28d1f895c6fe 622 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)
mbed_official 630:825f75ca301e 623 {
mbed_official 340:28d1f895c6fe 624 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
mbed_official 340:28d1f895c6fe 625 Even if the FLASH operation fails, the BUSY flag will be reset and an error
mbed_official 340:28d1f895c6fe 626 flag will be set */
mbed_official 340:28d1f895c6fe 627
mbed_official 340:28d1f895c6fe 628 uint32_t tickstart = HAL_GetTick();
mbed_official 340:28d1f895c6fe 629
mbed_official 340:28d1f895c6fe 630 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY))
mbed_official 340:28d1f895c6fe 631 {
mbed_official 630:825f75ca301e 632 if (Timeout != HAL_MAX_DELAY)
mbed_official 340:28d1f895c6fe 633 {
mbed_official 630:825f75ca301e 634 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
mbed_official 340:28d1f895c6fe 635 {
mbed_official 340:28d1f895c6fe 636 return HAL_TIMEOUT;
mbed_official 340:28d1f895c6fe 637 }
mbed_official 340:28d1f895c6fe 638 }
mbed_official 340:28d1f895c6fe 639 }
mbed_official 340:28d1f895c6fe 640
mbed_official 441:d2c15dda23c1 641 /* Check FLASH End of Operation flag */
mbed_official 441:d2c15dda23c1 642 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP))
mbed_official 441:d2c15dda23c1 643 {
mbed_official 441:d2c15dda23c1 644 /* Clear FLASH End of Operation pending bit */
mbed_official 441:d2c15dda23c1 645 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);
mbed_official 441:d2c15dda23c1 646 }
mbed_official 630:825f75ca301e 647
mbed_official 340:28d1f895c6fe 648 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
mbed_official 340:28d1f895c6fe 649 {
mbed_official 340:28d1f895c6fe 650 /*Save the error code*/
mbed_official 340:28d1f895c6fe 651 FLASH_SetErrorCode();
mbed_official 340:28d1f895c6fe 652 return HAL_ERROR;
mbed_official 340:28d1f895c6fe 653 }
mbed_official 340:28d1f895c6fe 654
mbed_official 441:d2c15dda23c1 655 /* If there is no error flag set */
mbed_official 340:28d1f895c6fe 656 return HAL_OK;
mbed_official 340:28d1f895c6fe 657
mbed_official 340:28d1f895c6fe 658 }
mbed_official 340:28d1f895c6fe 659
mbed_official 630:825f75ca301e 660
mbed_official 340:28d1f895c6fe 661 /**
mbed_official 340:28d1f895c6fe 662 * @brief Set the specific FLASH error flag.
mbed_official 340:28d1f895c6fe 663 * @retval None
mbed_official 340:28d1f895c6fe 664 */
mbed_official 340:28d1f895c6fe 665 static void FLASH_SetErrorCode(void)
mbed_official 630:825f75ca301e 666 {
mbed_official 340:28d1f895c6fe 667 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR))
mbed_official 340:28d1f895c6fe 668 {
mbed_official 630:825f75ca301e 669 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP;
mbed_official 340:28d1f895c6fe 670 }
mbed_official 340:28d1f895c6fe 671 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR))
mbed_official 340:28d1f895c6fe 672 {
mbed_official 630:825f75ca301e 673 pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG;
mbed_official 340:28d1f895c6fe 674 }
mbed_official 441:d2c15dda23c1 675
mbed_official 630:825f75ca301e 676 /* Clear FLASH error pending bits */
mbed_official 441:d2c15dda23c1 677 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);
mbed_official 630:825f75ca301e 678 }
mbed_official 630:825f75ca301e 679 /**
mbed_official 630:825f75ca301e 680 * @}
mbed_official 630:825f75ca301e 681 */
mbed_official 340:28d1f895c6fe 682
mbed_official 340:28d1f895c6fe 683 /**
mbed_official 340:28d1f895c6fe 684 * @}
mbed_official 630:825f75ca301e 685 */
mbed_official 340:28d1f895c6fe 686
mbed_official 340:28d1f895c6fe 687 #endif /* HAL_FLASH_MODULE_ENABLED */
mbed_official 340:28d1f895c6fe 688
mbed_official 340:28d1f895c6fe 689 /**
mbed_official 340:28d1f895c6fe 690 * @}
mbed_official 340:28d1f895c6fe 691 */
mbed_official 340:28d1f895c6fe 692
mbed_official 340:28d1f895c6fe 693 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/