Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c (29361B)
1 /** 2 ****************************************************************************** 3 * @file stm32f1xx_hal_flash.c 4 * @author MCD Application Team 5 * @brief FLASH HAL module driver. 6 * This file provides firmware functions to manage the following 7 * functionalities of the internal FLASH memory: 8 * + Program operations functions 9 * + Memory Control functions 10 * + Peripheral State functions 11 * 12 @verbatim 13 ============================================================================== 14 ##### FLASH peripheral features ##### 15 ============================================================================== 16 [..] The Flash memory interface manages CPU AHB I-Code and D-Code accesses 17 to the Flash memory. It implements the erase and program Flash memory operations 18 and the read and write protection mechanisms. 19 20 [..] The Flash memory interface accelerates code execution with a system of instruction 21 prefetch. 22 23 [..] The FLASH main features are: 24 (+) Flash memory read operations 25 (+) Flash memory program/erase operations 26 (+) Read / write protections 27 (+) Prefetch on I-Code 28 (+) Option Bytes programming 29 30 31 ##### How to use this driver ##### 32 ============================================================================== 33 [..] 34 This driver provides functions and macros to configure and program the FLASH 35 memory of all STM32F1xx devices. 36 37 (#) FLASH Memory I/O Programming functions: this group includes all needed 38 functions to erase and program the main memory: 39 (++) Lock and Unlock the FLASH interface 40 (++) Erase function: Erase page, erase all pages 41 (++) Program functions: half word, word and doubleword 42 (#) FLASH Option Bytes Programming functions: this group includes all needed 43 functions to manage the Option Bytes: 44 (++) Lock and Unlock the Option Bytes 45 (++) Set/Reset the write protection 46 (++) Set the Read protection Level 47 (++) Program the user Option Bytes 48 (++) Launch the Option Bytes loader 49 (++) Erase Option Bytes 50 (++) Program the data Option Bytes 51 (++) Get the Write protection. 52 (++) Get the user option bytes. 53 54 (#) Interrupts and flags management functions : this group 55 includes all needed functions to: 56 (++) Handle FLASH interrupts 57 (++) Wait for last FLASH operation according to its status 58 (++) Get error flag status 59 60 [..] In addition to these function, this driver includes a set of macros allowing 61 to handle the following operations: 62 63 (+) Set/Get the latency 64 (+) Enable/Disable the prefetch buffer 65 (+) Enable/Disable the half cycle access 66 (+) Enable/Disable the FLASH interrupts 67 (+) Monitor the FLASH flags status 68 69 @endverbatim 70 ****************************************************************************** 71 * @attention 72 * 73 * Copyright (c) 2016 STMicroelectronics. 74 * All rights reserved. 75 * 76 * This software is licensed under terms that can be found in the LICENSE file in 77 * the root directory of this software component. 78 * If no LICENSE file comes with this software, it is provided AS-IS. 79 ****************************************************************************** 80 */ 81 82 /* Includes ------------------------------------------------------------------*/ 83 #include "stm32f1xx_hal.h" 84 85 /** @addtogroup STM32F1xx_HAL_Driver 86 * @{ 87 */ 88 89 #ifdef HAL_FLASH_MODULE_ENABLED 90 91 /** @defgroup FLASH FLASH 92 * @brief FLASH HAL module driver 93 * @{ 94 */ 95 96 /* Private typedef -----------------------------------------------------------*/ 97 /* Private define ------------------------------------------------------------*/ 98 /** @defgroup FLASH_Private_Constants FLASH Private Constants 99 * @{ 100 */ 101 /** 102 * @} 103 */ 104 105 /* Private macro ---------------------------- ---------------------------------*/ 106 /** @defgroup FLASH_Private_Macros FLASH Private Macros 107 * @{ 108 */ 109 110 /** 111 * @} 112 */ 113 114 /* Private variables ---------------------------------------------------------*/ 115 /** @defgroup FLASH_Private_Variables FLASH Private Variables 116 * @{ 117 */ 118 /* Variables used for Erase pages under interruption*/ 119 FLASH_ProcessTypeDef pFlash; 120 /** 121 * @} 122 */ 123 124 /* Private function prototypes -----------------------------------------------*/ 125 /** @defgroup FLASH_Private_Functions FLASH Private Functions 126 * @{ 127 */ 128 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data); 129 static void FLASH_SetErrorCode(void); 130 extern void FLASH_PageErase(uint32_t PageAddress); 131 /** 132 * @} 133 */ 134 135 /* Exported functions ---------------------------------------------------------*/ 136 /** @defgroup FLASH_Exported_Functions FLASH Exported Functions 137 * @{ 138 */ 139 140 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions 141 * @brief Programming operation functions 142 * 143 @verbatim 144 @endverbatim 145 * @{ 146 */ 147 148 /** 149 * @brief Program halfword, word or double word at a specified address 150 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface 151 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface 152 * 153 * @note If an erase and a program operations are requested simultaneously, 154 * the erase operation is performed before the program one. 155 * 156 * @note FLASH should be previously erased before new programmation (only exception to this 157 * is when 0x0000 is programmed) 158 * 159 * @param TypeProgram: Indicate the way to program at a specified address. 160 * This parameter can be a value of @ref FLASH_Type_Program 161 * @param Address: Specifies the address to be programmed. 162 * @param Data: Specifies the data to be programmed 163 * 164 * @retval HAL_StatusTypeDef HAL Status 165 */ 166 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, 167 uint64_t Data) 168 { 169 HAL_StatusTypeDef status = HAL_ERROR; 170 uint8_t index = 0; 171 uint8_t nbiterations = 0; 172 173 /* Process Locked */ 174 __HAL_LOCK(&pFlash); 175 176 /* Check the parameters */ 177 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); 178 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); 179 180 #if defined(FLASH_BANK2_END) 181 if(Address <= FLASH_BANK1_END) 182 { 183 #endif /* FLASH_BANK2_END */ 184 /* Wait for last operation to be completed */ 185 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); 186 #if defined(FLASH_BANK2_END) 187 } 188 else 189 { 190 /* Wait for last operation to be completed */ 191 status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); 192 } 193 #endif /* FLASH_BANK2_END */ 194 195 if (status == HAL_OK) { 196 if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) { 197 /* Program halfword (16-bit) at a specified address. */ 198 nbiterations = 1U; 199 } else if (TypeProgram == FLASH_TYPEPROGRAM_WORD) { 200 /* Program word (32-bit = 2*16-bit) at a specified address. */ 201 nbiterations = 2U; 202 } else { 203 /* Program double word (64-bit = 4*16-bit) at a specified address. */ 204 nbiterations = 4U; 205 } 206 207 for (index = 0U; index < nbiterations; index++) { 208 FLASH_Program_HalfWord((Address + (2U * index)), 209 (uint16_t) (Data >> (16U * index))); 210 211 #if defined(FLASH_BANK2_END) 212 if(Address <= FLASH_BANK1_END) 213 { 214 #endif /* FLASH_BANK2_END */ 215 /* Wait for last operation to be completed */ 216 status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE); 217 218 /* If the program operation is completed, disable the PG Bit */ 219 CLEAR_BIT(FLASH->CR, FLASH_CR_PG); 220 #if defined(FLASH_BANK2_END) 221 } 222 else 223 { 224 /* Wait for last operation to be completed */ 225 status = FLASH_WaitForLastOperationBank2(FLASH_TIMEOUT_VALUE); 226 227 /* If the program operation is completed, disable the PG Bit */ 228 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); 229 } 230 #endif /* FLASH_BANK2_END */ 231 /* In case of error, stop programation procedure */ 232 if (status != HAL_OK) { 233 break; 234 } 235 } 236 } 237 238 /* Process Unlocked */ 239 __HAL_UNLOCK(&pFlash); 240 241 return status; 242 } 243 244 /** 245 * @brief Program halfword, word or double word at a specified address with interrupt enabled. 246 * @note The function HAL_FLASH_Unlock() should be called before to unlock the FLASH interface 247 * The function HAL_FLASH_Lock() should be called after to lock the FLASH interface 248 * 249 * @note If an erase and a program operations are requested simultaneously, 250 * the erase operation is performed before the program one. 251 * 252 * @param TypeProgram: Indicate the way to program at a specified address. 253 * This parameter can be a value of @ref FLASH_Type_Program 254 * @param Address: Specifies the address to be programmed. 255 * @param Data: Specifies the data to be programmed 256 * 257 * @retval HAL_StatusTypeDef HAL Status 258 */ 259 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, 260 uint64_t Data) 261 { 262 HAL_StatusTypeDef status = HAL_OK; 263 264 /* Check the parameters */ 265 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram)); 266 assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)); 267 268 #if defined(FLASH_BANK2_END) 269 /* If procedure already ongoing, reject the next one */ 270 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) 271 { 272 return HAL_ERROR; 273 } 274 275 if(Address <= FLASH_BANK1_END) 276 { 277 /* Enable End of FLASH Operation and Error source interrupts */ 278 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1); 279 280 }else 281 { 282 /* Enable End of FLASH Operation and Error source interrupts */ 283 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); 284 } 285 #else 286 /* Enable End of FLASH Operation and Error source interrupts */ 287 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); 288 #endif /* FLASH_BANK2_END */ 289 290 pFlash.Address = Address; 291 pFlash.Data = Data; 292 293 if (TypeProgram == FLASH_TYPEPROGRAM_HALFWORD) { 294 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMHALFWORD; 295 /* Program halfword (16-bit) at a specified address. */ 296 pFlash.DataRemaining = 1U; 297 } else if (TypeProgram == FLASH_TYPEPROGRAM_WORD) { 298 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMWORD; 299 /* Program word (32-bit : 2*16-bit) at a specified address. */ 300 pFlash.DataRemaining = 2U; 301 } else { 302 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAMDOUBLEWORD; 303 /* Program double word (64-bit : 4*16-bit) at a specified address. */ 304 pFlash.DataRemaining = 4U; 305 } 306 307 /* Program halfword (16-bit) at a specified address. */ 308 FLASH_Program_HalfWord(Address, (uint16_t) Data); 309 310 return status; 311 } 312 313 /** 314 * @brief This function handles FLASH interrupt request. 315 * @retval None 316 */ 317 void HAL_FLASH_IRQHandler(void) 318 { 319 uint32_t addresstmp = 0U; 320 321 /* Check FLASH operation error flags */ 322 #if defined(FLASH_BANK2_END) 323 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK1) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK1) || \ 324 (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2))) 325 #else 326 if (__HAL_FLASH_GET_FLAG( 327 FLASH_FLAG_WRPERR) ||__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) 328 #endif /* FLASH_BANK2_END */ 329 { 330 /* Return the faulty address */ 331 addresstmp = pFlash.Address; 332 /* Reset address */ 333 pFlash.Address = 0xFFFFFFFFU; 334 335 /* Save the Error code */ 336 FLASH_SetErrorCode(); 337 338 /* FLASH error interrupt user callback */ 339 HAL_FLASH_OperationErrorCallback(addresstmp); 340 341 /* Stop the procedure ongoing */ 342 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 343 } 344 345 /* Check FLASH End of Operation flag */ 346 #if defined(FLASH_BANK2_END) 347 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK1)) 348 { 349 /* Clear FLASH End of Operation pending bit */ 350 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK1); 351 #else 352 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) { 353 /* Clear FLASH End of Operation pending bit */ 354 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); 355 #endif /* FLASH_BANK2_END */ 356 357 /* Process can continue only if no error detected */ 358 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) { 359 if (pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) { 360 /* Nb of pages to erased can be decreased */ 361 pFlash.DataRemaining--; 362 363 /* Check if there are still pages to erase */ 364 if (pFlash.DataRemaining != 0U) { 365 addresstmp = pFlash.Address; 366 /*Indicate user which sector has been erased */ 367 HAL_FLASH_EndOfOperationCallback(addresstmp); 368 369 /*Increment sector number*/ 370 addresstmp = pFlash.Address + FLASH_PAGE_SIZE; 371 pFlash.Address = addresstmp; 372 373 /* If the erase operation is completed, disable the PER Bit */ 374 CLEAR_BIT(FLASH->CR, FLASH_CR_PER); 375 376 FLASH_PageErase(addresstmp); 377 } else { 378 /* No more pages to Erase, user callback can be called. */ 379 /* Reset Sector and stop Erase pages procedure */ 380 pFlash.Address = addresstmp = 0xFFFFFFFFU; 381 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 382 /* FLASH EOP interrupt user callback */ 383 HAL_FLASH_EndOfOperationCallback(addresstmp); 384 } 385 } else if (pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) { 386 /* Operation is completed, disable the MER Bit */ 387 CLEAR_BIT(FLASH->CR, FLASH_CR_MER); 388 389 #if defined(FLASH_BANK2_END) 390 /* Stop Mass Erase procedure if no pending mass erase on other bank */ 391 if (HAL_IS_BIT_CLR(FLASH->CR2, FLASH_CR2_MER)) 392 { 393 #endif /* FLASH_BANK2_END */ 394 /* MassErase ended. Return the selected bank */ 395 /* FLASH EOP interrupt user callback */ 396 HAL_FLASH_EndOfOperationCallback(0U); 397 398 /* Stop Mass Erase procedure*/ 399 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 400 } 401 #if defined(FLASH_BANK2_END) 402 } 403 #endif /* FLASH_BANK2_END */ 404 else { 405 /* Nb of 16-bit data to program can be decreased */ 406 pFlash.DataRemaining--; 407 408 /* Check if there are still 16-bit data to program */ 409 if (pFlash.DataRemaining != 0U) { 410 /* Increment address to 16-bit */ 411 pFlash.Address += 2U; 412 addresstmp = pFlash.Address; 413 414 /* Shift to have next 16-bit data */ 415 pFlash.Data = (pFlash.Data >> 16U); 416 417 /* Operation is completed, disable the PG Bit */ 418 CLEAR_BIT(FLASH->CR, FLASH_CR_PG); 419 420 /*Program halfword (16-bit) at a specified address.*/ 421 FLASH_Program_HalfWord(addresstmp, (uint16_t) pFlash.Data); 422 } else { 423 /* Program ended. Return the selected address */ 424 /* FLASH EOP interrupt user callback */ 425 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) { 426 HAL_FLASH_EndOfOperationCallback(pFlash.Address); 427 } else if (pFlash.ProcedureOnGoing 428 == FLASH_PROC_PROGRAMWORD) { 429 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 2U); 430 } else { 431 HAL_FLASH_EndOfOperationCallback(pFlash.Address - 6U); 432 } 433 434 /* Reset Address and stop Program procedure */ 435 pFlash.Address = 0xFFFFFFFFU; 436 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 437 } 438 } 439 } 440 } 441 442 #if defined(FLASH_BANK2_END) 443 /* Check FLASH End of Operation flag */ 444 if(__HAL_FLASH_GET_FLAG( FLASH_FLAG_EOP_BANK2)) 445 { 446 /* Clear FLASH End of Operation pending bit */ 447 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); 448 449 /* Process can continue only if no error detected */ 450 if(pFlash.ProcedureOnGoing != FLASH_PROC_NONE) 451 { 452 if(pFlash.ProcedureOnGoing == FLASH_PROC_PAGEERASE) 453 { 454 /* Nb of pages to erased can be decreased */ 455 pFlash.DataRemaining--; 456 457 /* Check if there are still pages to erase*/ 458 if(pFlash.DataRemaining != 0U) 459 { 460 /* Indicate user which page address has been erased*/ 461 HAL_FLASH_EndOfOperationCallback(pFlash.Address); 462 463 /* Increment page address to next page */ 464 pFlash.Address += FLASH_PAGE_SIZE; 465 addresstmp = pFlash.Address; 466 467 /* Operation is completed, disable the PER Bit */ 468 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER); 469 470 FLASH_PageErase(addresstmp); 471 } 472 else 473 { 474 /*No more pages to Erase*/ 475 476 /*Reset Address and stop Erase pages procedure*/ 477 pFlash.Address = 0xFFFFFFFFU; 478 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 479 480 /* FLASH EOP interrupt user callback */ 481 HAL_FLASH_EndOfOperationCallback(pFlash.Address); 482 } 483 } 484 else if(pFlash.ProcedureOnGoing == FLASH_PROC_MASSERASE) 485 { 486 /* Operation is completed, disable the MER Bit */ 487 CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); 488 489 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_MER)) 490 { 491 /* MassErase ended. Return the selected bank*/ 492 /* FLASH EOP interrupt user callback */ 493 HAL_FLASH_EndOfOperationCallback(0U); 494 495 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 496 } 497 } 498 else 499 { 500 /* Nb of 16-bit data to program can be decreased */ 501 pFlash.DataRemaining--; 502 503 /* Check if there are still 16-bit data to program */ 504 if(pFlash.DataRemaining != 0U) 505 { 506 /* Increment address to 16-bit */ 507 pFlash.Address += 2U; 508 addresstmp = pFlash.Address; 509 510 /* Shift to have next 16-bit data */ 511 pFlash.Data = (pFlash.Data >> 16U); 512 513 /* Operation is completed, disable the PG Bit */ 514 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PG); 515 516 /*Program halfword (16-bit) at a specified address.*/ 517 FLASH_Program_HalfWord(addresstmp, (uint16_t)pFlash.Data); 518 } 519 else 520 { 521 /*Program ended. Return the selected address*/ 522 /* FLASH EOP interrupt user callback */ 523 if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMHALFWORD) 524 { 525 HAL_FLASH_EndOfOperationCallback(pFlash.Address); 526 } 527 else if (pFlash.ProcedureOnGoing == FLASH_PROC_PROGRAMWORD) 528 { 529 HAL_FLASH_EndOfOperationCallback(pFlash.Address-2U); 530 } 531 else 532 { 533 HAL_FLASH_EndOfOperationCallback(pFlash.Address-6U); 534 } 535 536 /* Reset Address and stop Program procedure*/ 537 pFlash.Address = 0xFFFFFFFFU; 538 pFlash.ProcedureOnGoing = FLASH_PROC_NONE; 539 } 540 } 541 } 542 } 543 #endif 544 545 if (pFlash.ProcedureOnGoing == FLASH_PROC_NONE) { 546 #if defined(FLASH_BANK2_END) 547 /* Operation is completed, disable the PG, PER and MER Bits for both bank */ 548 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); 549 CLEAR_BIT(FLASH->CR2, (FLASH_CR2_PG | FLASH_CR2_PER | FLASH_CR2_MER)); 550 551 /* Disable End of FLASH Operation and Error source interrupts for both banks */ 552 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP_BANK1 | FLASH_IT_ERR_BANK1 | FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); 553 #else 554 /* Operation is completed, disable the PG, PER and MER Bits */ 555 CLEAR_BIT(FLASH->CR, (FLASH_CR_PG | FLASH_CR_PER | FLASH_CR_MER)); 556 557 /* Disable End of FLASH Operation and Error source interrupts */ 558 __HAL_FLASH_DISABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); 559 #endif /* FLASH_BANK2_END */ 560 561 } 562 } 563 564 /** 565 * @brief FLASH end of operation interrupt callback 566 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure 567 * - Mass Erase: No return value expected 568 * - Pages Erase: Address of the page which has been erased 569 * (if 0xFFFFFFFF, it means that all the selected pages have been erased) 570 * - Program: Address which was selected for data program 571 * @retval none 572 */ 573 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue) 574 { 575 /* Prevent unused argument(s) compilation warning */ 576 UNUSED(ReturnValue); 577 578 /* NOTE : This function Should not be modified, when the callback is needed, 579 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file 580 */ 581 } 582 583 /** 584 * @brief FLASH operation error interrupt callback 585 * @param ReturnValue: The value saved in this parameter depends on the ongoing procedure 586 * - Mass Erase: No return value expected 587 * - Pages Erase: Address of the page which returned an error 588 * - Program: Address which was selected for data program 589 * @retval none 590 */ 591 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue) 592 { 593 /* Prevent unused argument(s) compilation warning */ 594 UNUSED(ReturnValue); 595 596 /* NOTE : This function Should not be modified, when the callback is needed, 597 the HAL_FLASH_OperationErrorCallback could be implemented in the user file 598 */ 599 } 600 601 /** 602 * @} 603 */ 604 605 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions 606 * @brief management functions 607 * 608 @verbatim 609 =============================================================================== 610 ##### Peripheral Control functions ##### 611 =============================================================================== 612 [..] 613 This subsection provides a set of functions allowing to control the FLASH 614 memory operations. 615 616 @endverbatim 617 * @{ 618 */ 619 620 /** 621 * @brief Unlock the FLASH control register access 622 * @retval HAL Status 623 */ 624 HAL_StatusTypeDef HAL_FLASH_Unlock(void) 625 { 626 HAL_StatusTypeDef status = HAL_OK; 627 628 if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) { 629 /* Authorize the FLASH Registers access */ 630 WRITE_REG(FLASH->KEYR, FLASH_KEY1); 631 WRITE_REG(FLASH->KEYR, FLASH_KEY2); 632 633 /* Verify Flash is unlocked */ 634 if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) != RESET) { 635 status = HAL_ERROR; 636 } 637 } 638 #if defined(FLASH_BANK2_END) 639 if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) 640 { 641 /* Authorize the FLASH BANK2 Registers access */ 642 WRITE_REG(FLASH->KEYR2, FLASH_KEY1); 643 WRITE_REG(FLASH->KEYR2, FLASH_KEY2); 644 645 /* Verify Flash BANK2 is unlocked */ 646 if(READ_BIT(FLASH->CR2, FLASH_CR2_LOCK) != RESET) 647 { 648 status = HAL_ERROR; 649 } 650 } 651 #endif /* FLASH_BANK2_END */ 652 653 return status; 654 } 655 656 /** 657 * @brief Locks the FLASH control register access 658 * @retval HAL Status 659 */ 660 HAL_StatusTypeDef HAL_FLASH_Lock(void) 661 { 662 /* Set the LOCK Bit to lock the FLASH Registers access */ 663 SET_BIT(FLASH->CR, FLASH_CR_LOCK); 664 665 #if defined(FLASH_BANK2_END) 666 /* Set the LOCK Bit to lock the FLASH BANK2 Registers access */ 667 SET_BIT(FLASH->CR2, FLASH_CR2_LOCK); 668 669 #endif /* FLASH_BANK2_END */ 670 return HAL_OK; 671 } 672 673 /** 674 * @brief Unlock the FLASH Option Control Registers access. 675 * @retval HAL Status 676 */ 677 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void) 678 { 679 if (HAL_IS_BIT_CLR(FLASH->CR, FLASH_CR_OPTWRE)) { 680 /* Authorizes the Option Byte register programming */ 681 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1); 682 WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2); 683 } else { 684 return HAL_ERROR; 685 } 686 687 return HAL_OK; 688 } 689 690 /** 691 * @brief Lock the FLASH Option Control Registers access. 692 * @retval HAL Status 693 */ 694 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void) 695 { 696 /* Clear the OPTWRE Bit to lock the FLASH Option Byte Registers access */ 697 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTWRE); 698 699 return HAL_OK; 700 } 701 702 /** 703 * @brief Launch the option byte loading. 704 * @note This function will reset automatically the MCU. 705 * @retval None 706 */ 707 void HAL_FLASH_OB_Launch(void) 708 { 709 /* Initiates a system reset request to launch the option byte loading */ 710 HAL_NVIC_SystemReset(); 711 } 712 713 /** 714 * @} 715 */ 716 717 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral errors functions 718 * @brief Peripheral errors functions 719 * 720 @verbatim 721 =============================================================================== 722 ##### Peripheral Errors functions ##### 723 =============================================================================== 724 [..] 725 This subsection permit to get in run-time errors of the FLASH peripheral. 726 727 @endverbatim 728 * @{ 729 */ 730 731 /** 732 * @brief Get the specific FLASH error flag. 733 * @retval FLASH_ErrorCode The returned value can be: 734 * @ref FLASH_Error_Codes 735 */ 736 uint32_t HAL_FLASH_GetError(void) 737 { 738 return pFlash.ErrorCode; 739 } 740 741 /** 742 * @} 743 */ 744 745 /** 746 * @} 747 */ 748 749 /** @addtogroup FLASH_Private_Functions 750 * @{ 751 */ 752 753 /** 754 * @brief Program a half-word (16-bit) at a specified address. 755 * @param Address specify the address to be programmed. 756 * @param Data specify the data to be programmed. 757 * @retval None 758 */ 759 static void FLASH_Program_HalfWord(uint32_t Address, uint16_t Data) 760 { 761 /* Clean the error context */ 762 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 763 764 #if defined(FLASH_BANK2_END) 765 if(Address <= FLASH_BANK1_END) 766 { 767 #endif /* FLASH_BANK2_END */ 768 /* Proceed to program the new data */ 769 SET_BIT(FLASH->CR, FLASH_CR_PG); 770 #if defined(FLASH_BANK2_END) 771 } 772 else 773 { 774 /* Proceed to program the new data */ 775 SET_BIT(FLASH->CR2, FLASH_CR2_PG); 776 } 777 #endif /* FLASH_BANK2_END */ 778 779 /* Write data in the address */ 780 *(__IO uint16_t*) Address = Data; 781 } 782 783 /** 784 * @brief Wait for a FLASH operation to complete. 785 * @param Timeout maximum flash operation timeout 786 * @retval HAL Status 787 */ 788 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout) 789 { 790 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset. 791 Even if the FLASH operation fails, the BUSY flag will be reset and an error 792 flag will be set */ 793 794 uint32_t tickstart = HAL_GetTick(); 795 796 while (__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY)) { 797 if (Timeout != HAL_MAX_DELAY) { 798 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)) { 799 return HAL_TIMEOUT; 800 } 801 } 802 } 803 804 /* Check FLASH End of Operation flag */ 805 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP)) { 806 /* Clear FLASH End of Operation pending bit */ 807 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP); 808 } 809 810 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || 811 __HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR) || 812 __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) { 813 /*Save the error code*/ 814 FLASH_SetErrorCode(); 815 return HAL_ERROR; 816 } 817 818 /* There is no error flag set */ 819 return HAL_OK; 820 } 821 822 #if defined(FLASH_BANK2_END) 823 /** 824 * @brief Wait for a FLASH BANK2 operation to complete. 825 * @param Timeout maximum flash operation timeout 826 * @retval HAL_StatusTypeDef HAL Status 827 */ 828 HAL_StatusTypeDef FLASH_WaitForLastOperationBank2(uint32_t Timeout) 829 { 830 /* Wait for the FLASH BANK2 operation to complete by polling on BUSY flag to be reset. 831 Even if the FLASH BANK2 operation fails, the BUSY flag will be reset and an error 832 flag will be set */ 833 834 uint32_t tickstart = HAL_GetTick(); 835 836 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY_BANK2)) 837 { 838 if (Timeout != HAL_MAX_DELAY) 839 { 840 if((Timeout == 0U) || ((HAL_GetTick()-tickstart) > Timeout)) 841 { 842 return HAL_TIMEOUT; 843 } 844 } 845 } 846 847 /* Check FLASH End of Operation flag */ 848 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP_BANK2)) 849 { 850 /* Clear FLASH End of Operation pending bit */ 851 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP_BANK2); 852 } 853 854 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) 855 { 856 /*Save the error code*/ 857 FLASH_SetErrorCode(); 858 return HAL_ERROR; 859 } 860 861 /* If there is an error flag set */ 862 return HAL_OK; 863 864 } 865 #endif /* FLASH_BANK2_END */ 866 867 /** 868 * @brief Set the specific FLASH error flag. 869 * @retval None 870 */ 871 static void FLASH_SetErrorCode(void) 872 { 873 uint32_t flags = 0U; 874 875 #if defined(FLASH_BANK2_END) 876 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR_BANK2)) 877 #else 878 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_WRPERR)) 879 #endif /* FLASH_BANK2_END */ 880 { 881 pFlash.ErrorCode |= HAL_FLASH_ERROR_WRP; 882 #if defined(FLASH_BANK2_END) 883 flags |= FLASH_FLAG_WRPERR | FLASH_FLAG_WRPERR_BANK2; 884 #else 885 flags |= FLASH_FLAG_WRPERR; 886 #endif /* FLASH_BANK2_END */ 887 } 888 #if defined(FLASH_BANK2_END) 889 if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR) || __HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR_BANK2)) 890 #else 891 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_PGERR)) 892 #endif /* FLASH_BANK2_END */ 893 { 894 pFlash.ErrorCode |= HAL_FLASH_ERROR_PROG; 895 #if defined(FLASH_BANK2_END) 896 flags |= FLASH_FLAG_PGERR | FLASH_FLAG_PGERR_BANK2; 897 #else 898 flags |= FLASH_FLAG_PGERR; 899 #endif /* FLASH_BANK2_END */ 900 } 901 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)) { 902 pFlash.ErrorCode |= HAL_FLASH_ERROR_OPTV; 903 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); 904 } 905 906 /* Clear FLASH error pending bits */ 907 __HAL_FLASH_CLEAR_FLAG(flags); 908 } 909 /** 910 * @} 911 */ 912 913 /** 914 * @} 915 */ 916 917 #endif /* HAL_FLASH_MODULE_ENABLED */ 918 919 /** 920 * @} 921 */ 922
