mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
184:08ed48f1de7f
mbed library release version 165

Who changed what in which revision?

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