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