Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c (34588B)
1 /** 2 ****************************************************************************** 3 * @file stm32f1xx_hal_flash_ex.c 4 * @author MCD Application Team 5 * @brief Extended FLASH HAL module driver. 6 * 7 * This file provides firmware functions to manage the following 8 * functionalities of the FLASH peripheral: 9 * + Extended Initialization/de-initialization functions 10 * + Extended I/O operation functions 11 * + Extended Peripheral Control functions 12 * 13 @verbatim 14 ============================================================================== 15 ##### Flash peripheral extended features ##### 16 ============================================================================== 17 18 ##### How to use this driver ##### 19 ============================================================================== 20 [..] This driver provides functions to configure and program the FLASH memory 21 of all STM32F1xxx devices. It includes 22 23 (++) Set/Reset the write protection 24 (++) Program the user Option Bytes 25 (++) Get the Read protection Level 26 27 @endverbatim 28 ****************************************************************************** 29 * @attention 30 * 31 * Copyright (c) 2016 STMicroelectronics. 32 * All rights reserved. 33 * 34 * This software is licensed under terms that can be found in the LICENSE file in 35 * the root directory of this software component. 36 * If no LICENSE file comes with this software, it is provided AS-IS. 37 ****************************************************************************** 38 */ 39 40 /* Includes ------------------------------------------------------------------*/ 41 #include "stm32f1xx_hal.h" 42 43 /** @addtogroup STM32F1xx_HAL_Driver 44 * @{ 45 */ 46 #ifdef HAL_FLASH_MODULE_ENABLED 47 48 /** @addtogroup FLASH 49 * @{ 50 */ 51 /** @addtogroup FLASH_Private_Variables 52 * @{ 53 */ 54 /* Variables used for Erase pages under interruption*/ 55 extern FLASH_ProcessTypeDef pFlash; 56 /** 57 * @} 58 */ 59 60 /** 61 * @} 62 */ 63 64 /** @defgroup FLASHEx FLASHEx 65 * @brief FLASH HAL Extension module driver 66 * @{ 67 */ 68 69 /* Private typedef -----------------------------------------------------------*/ 70 /* Private define ------------------------------------------------------------*/ 71 /** @defgroup FLASHEx_Private_Constants FLASHEx Private Constants 72 * @{ 73 */ 74 #define FLASH_POSITION_IWDGSW_BIT FLASH_OBR_IWDG_SW_Pos 75 #define FLASH_POSITION_OB_USERDATA0_BIT FLASH_OBR_DATA0_Pos 76 #define FLASH_POSITION_OB_USERDATA1_BIT FLASH_OBR_DATA1_Pos 77 /** 78 * @} 79 */ 80 81 /* Private macro -------------------------------------------------------------*/ 82 /** @defgroup FLASHEx_Private_Macros FLASHEx Private Macros 83 * @{ 84 */ 85 /** 86 * @} 87 */ 88 89 /* Private variables ---------------------------------------------------------*/ 90 /* Private function prototypes -----------------------------------------------*/ 91 /** @defgroup FLASHEx_Private_Functions FLASHEx Private Functions 92 * @{ 93 */ 94 /* Erase operations */ 95 static void FLASH_MassErase(uint32_t Banks); 96 void FLASH_PageErase(uint32_t PageAddress); 97 98 /* Option bytes control */ 99 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage); 100 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage); 101 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel); 102 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig); 103 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data); 104 static uint32_t FLASH_OB_GetWRP(void); 105 static uint32_t FLASH_OB_GetRDP(void); 106 static uint8_t FLASH_OB_GetUser(void); 107 108 /** 109 * @} 110 */ 111 112 /* Exported functions ---------------------------------------------------------*/ 113 /** @defgroup FLASHEx_Exported_Functions FLASHEx Exported Functions 114 * @{ 115 */ 116 117 /** @defgroup FLASHEx_Exported_Functions_Group1 FLASHEx Memory Erasing functions 118 * @brief FLASH Memory Erasing functions 119 * 120 @verbatim 121 ============================================================================== 122 ##### FLASH Erasing Programming functions ##### 123 ============================================================================== 124 125 [..] The FLASH Memory Erasing functions, includes the following functions: 126 (+) HAL_FLASHEx_Erase: return only when erase has been done 127 (+) HAL_FLASHEx_Erase_IT: end of erase is done when HAL_FLASH_EndOfOperationCallback 128 is called with parameter 0xFFFFFFFF 129 130 [..] Any operation of erase should follow these steps: 131 (#) Call the HAL_FLASH_Unlock() function to enable the flash control register and 132 program memory access. 133 (#) Call the desired function to erase page. 134 (#) Call the HAL_FLASH_Lock() to disable the flash program memory access 135 (recommended to protect the FLASH memory against possible unwanted operation). 136 137 @endverbatim 138 * @{ 139 */ 140 141 /** 142 * @brief Perform a mass erase or erase the specified FLASH memory pages 143 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function 144 * must be called before. 145 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access 146 * (recommended to protect the FLASH memory against possible unwanted operation) 147 * @param[in] pEraseInit pointer to an FLASH_EraseInitTypeDef structure that 148 * contains the configuration information for the erasing. 149 * 150 * @param[out] PageError pointer to variable that 151 * contains the configuration information on faulty page in case of error 152 * (0xFFFFFFFF means that all the pages have been correctly erased) 153 * 154 * @retval HAL_StatusTypeDef HAL Status 155 */ 156 HAL_StatusTypeDef HAL_FLASHEx_Erase(FLASH_EraseInitTypeDef *pEraseInit, 157 uint32_t *PageError) 158 { 159 HAL_StatusTypeDef status = HAL_ERROR; 160 uint32_t address = 0U; 161 162 /* Process Locked */ 163 __HAL_LOCK(&pFlash); 164 165 /* Check the parameters */ 166 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase)); 167 168 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE) { 169 #if defined(FLASH_BANK2_END) 170 if (pEraseInit->Banks == FLASH_BANK_BOTH) 171 { 172 /* Mass Erase requested for Bank1 and Bank2 */ 173 /* Wait for last operation to be completed */ 174 if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \ 175 (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)) 176 { 177 /*Mass erase to be done*/ 178 FLASH_MassErase(FLASH_BANK_BOTH); 179 180 /* Wait for last operation to be completed */ 181 if ((FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) && \ 182 (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK)) 183 { 184 status = HAL_OK; 185 } 186 187 /* If the erase operation is completed, disable the MER Bit */ 188 CLEAR_BIT(FLASH->CR, FLASH_CR_MER); 189 CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); 190 } 191 } 192 else if (pEraseInit->Banks == FLASH_BANK_2) 193 { 194 /* Mass Erase requested for Bank2 */ 195 /* Wait for last operation to be completed */ 196 if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) 197 { 198 /*Mass erase to be done*/ 199 FLASH_MassErase(FLASH_BANK_2); 200 201 /* Wait for last operation to be completed */ 202 status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE); 203 204 /* If the erase operation is completed, disable the MER Bit */ 205 CLEAR_BIT(FLASH->CR2, FLASH_CR2_MER); 206 } 207 } 208 else 209 #endif /* FLASH_BANK2_END */ 210 { 211 /* Mass Erase requested for Bank1 */ 212 /* Wait for last operation to be completed */ 213 if (FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE) 214 == HAL_OK) { 215 /*Mass erase to be done*/ 216 FLASH_MassErase(FLASH_BANK_1); 217 218 /* Wait for last operation to be completed */ 219 status = FLASH_WaitForLastOperation( 220 (uint32_t) FLASH_TIMEOUT_VALUE); 221 222 /* If the erase operation is completed, disable the MER Bit */ 223 CLEAR_BIT(FLASH->CR, FLASH_CR_MER); 224 } 225 } 226 } else { 227 /* Page Erase is requested */ 228 /* Check the parameters */ 229 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress)); 230 assert_param( 231 IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages)); 232 233 #if defined(FLASH_BANK2_END) 234 /* Page Erase requested on address located on bank2 */ 235 if(pEraseInit->PageAddress > FLASH_BANK1_END) 236 { 237 /* Wait for last operation to be completed */ 238 if (FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE) == HAL_OK) 239 { 240 /*Initialization of PageError variable*/ 241 *PageError = 0xFFFFFFFFU; 242 243 /* Erase by page by page to be done*/ 244 for(address = pEraseInit->PageAddress; 245 address < (pEraseInit->PageAddress + (pEraseInit->NbPages)*FLASH_PAGE_SIZE); 246 address += FLASH_PAGE_SIZE) 247 { 248 FLASH_PageErase(address); 249 250 /* Wait for last operation to be completed */ 251 status = FLASH_WaitForLastOperationBank2((uint32_t)FLASH_TIMEOUT_VALUE); 252 253 /* If the erase operation is completed, disable the PER Bit */ 254 CLEAR_BIT(FLASH->CR2, FLASH_CR2_PER); 255 256 if (status != HAL_OK) 257 { 258 /* In case of error, stop erase procedure and return the faulty address */ 259 *PageError = address; 260 break; 261 } 262 } 263 } 264 } 265 else 266 #endif /* FLASH_BANK2_END */ 267 { 268 /* Page Erase requested on address located on bank1 */ 269 /* Wait for last operation to be completed */ 270 if (FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE) 271 == HAL_OK) { 272 /*Initialization of PageError variable*/ 273 *PageError = 0xFFFFFFFFU; 274 275 /* Erase page by page to be done*/ 276 for (address = pEraseInit->PageAddress; 277 address 278 < ((pEraseInit->NbPages * FLASH_PAGE_SIZE) 279 + pEraseInit->PageAddress); address += 280 FLASH_PAGE_SIZE) 281 { 282 FLASH_PageErase(address); 283 284 /* Wait for last operation to be completed */ 285 status = FLASH_WaitForLastOperation( 286 (uint32_t) FLASH_TIMEOUT_VALUE); 287 288 /* If the erase operation is completed, disable the PER Bit */ 289 CLEAR_BIT(FLASH->CR, FLASH_CR_PER); 290 291 if (status != HAL_OK) { 292 /* In case of error, stop erase procedure and return the faulty address */ 293 *PageError = address; 294 break; 295 } 296 } 297 } 298 } 299 } 300 301 /* Process Unlocked */ 302 __HAL_UNLOCK(&pFlash); 303 304 return status; 305 } 306 307 /** 308 * @brief Perform a mass erase or erase the specified FLASH memory pages with interrupt enabled 309 * @note To correctly run this function, the @ref HAL_FLASH_Unlock() function 310 * must be called before. 311 * Call the @ref HAL_FLASH_Lock() to disable the flash memory access 312 * (recommended to protect the FLASH memory against possible unwanted operation) 313 * @param pEraseInit pointer to an FLASH_EraseInitTypeDef structure that 314 * contains the configuration information for the erasing. 315 * 316 * @retval HAL_StatusTypeDef HAL Status 317 */ 318 HAL_StatusTypeDef HAL_FLASHEx_Erase_IT(FLASH_EraseInitTypeDef *pEraseInit) 319 { 320 HAL_StatusTypeDef status = HAL_OK; 321 322 /* If procedure already ongoing, reject the next one */ 323 if (pFlash.ProcedureOnGoing != FLASH_PROC_NONE) { 324 return HAL_ERROR; 325 } 326 327 /* Check the parameters */ 328 assert_param(IS_FLASH_TYPEERASE(pEraseInit->TypeErase)); 329 330 /* Enable End of FLASH Operation and Error source interrupts */ 331 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP | FLASH_IT_ERR); 332 333 #if defined(FLASH_BANK2_END) 334 /* Enable End of FLASH Operation and Error source interrupts */ 335 __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP_BANK2 | FLASH_IT_ERR_BANK2); 336 337 #endif 338 if (pEraseInit->TypeErase == FLASH_TYPEERASE_MASSERASE) { 339 /*Mass erase to be done*/ 340 pFlash.ProcedureOnGoing = FLASH_PROC_MASSERASE; 341 FLASH_MassErase(pEraseInit->Banks); 342 } else { 343 /* Erase by page to be done*/ 344 345 /* Check the parameters */ 346 assert_param(IS_FLASH_PROGRAM_ADDRESS(pEraseInit->PageAddress)); 347 assert_param( 348 IS_FLASH_NB_PAGES(pEraseInit->PageAddress, pEraseInit->NbPages)); 349 350 pFlash.ProcedureOnGoing = FLASH_PROC_PAGEERASE; 351 pFlash.DataRemaining = pEraseInit->NbPages; 352 pFlash.Address = pEraseInit->PageAddress; 353 354 /*Erase 1st page and wait for IT*/ 355 FLASH_PageErase(pEraseInit->PageAddress); 356 } 357 358 return status; 359 } 360 361 /** 362 * @} 363 */ 364 365 /** @defgroup FLASHEx_Exported_Functions_Group2 Option Bytes Programming functions 366 * @brief Option Bytes Programming functions 367 * 368 @verbatim 369 ============================================================================== 370 ##### Option Bytes Programming functions ##### 371 ============================================================================== 372 [..] 373 This subsection provides a set of functions allowing to control the FLASH 374 option bytes operations. 375 376 @endverbatim 377 * @{ 378 */ 379 380 /** 381 * @brief Erases the FLASH option bytes. 382 * @note This functions erases all option bytes except the Read protection (RDP). 383 * The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface 384 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes 385 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes 386 * (system reset will occur) 387 * @retval HAL status 388 */ 389 390 HAL_StatusTypeDef HAL_FLASHEx_OBErase(void) 391 { 392 uint8_t rdptmp = OB_RDP_LEVEL_0; 393 HAL_StatusTypeDef status = HAL_ERROR; 394 395 /* Get the actual read protection Option Byte value */ 396 rdptmp = FLASH_OB_GetRDP(); 397 398 /* Wait for last operation to be completed */ 399 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 400 401 if (status == HAL_OK) { 402 /* Clean the error context */ 403 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 404 405 /* If the previous operation is completed, proceed to erase the option bytes */ 406 SET_BIT(FLASH->CR, FLASH_CR_OPTER); 407 SET_BIT(FLASH->CR, FLASH_CR_STRT); 408 409 /* Wait for last operation to be completed */ 410 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 411 412 /* If the erase operation is completed, disable the OPTER Bit */ 413 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER); 414 415 if (status == HAL_OK) { 416 /* Restore the last read protection Option Byte value */ 417 status = FLASH_OB_RDP_LevelConfig(rdptmp); 418 } 419 } 420 421 /* Return the erase status */ 422 return status; 423 } 424 425 /** 426 * @brief Program option bytes 427 * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface 428 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes 429 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes 430 * (system reset will occur) 431 * 432 * @param pOBInit pointer to an FLASH_OBInitStruct structure that 433 * contains the configuration information for the programming. 434 * 435 * @retval HAL_StatusTypeDef HAL Status 436 */ 437 HAL_StatusTypeDef HAL_FLASHEx_OBProgram(FLASH_OBProgramInitTypeDef *pOBInit) 438 { 439 HAL_StatusTypeDef status = HAL_ERROR; 440 441 /* Process Locked */ 442 __HAL_LOCK(&pFlash); 443 444 /* Check the parameters */ 445 assert_param(IS_OPTIONBYTE(pOBInit->OptionType)); 446 447 /* Write protection configuration */ 448 if ((pOBInit->OptionType & OPTIONBYTE_WRP) == OPTIONBYTE_WRP) { 449 assert_param(IS_WRPSTATE(pOBInit->WRPState)); 450 if (pOBInit->WRPState == OB_WRPSTATE_ENABLE) { 451 /* Enable of Write protection on the selected page */ 452 status = FLASH_OB_EnableWRP(pOBInit->WRPPage); 453 } else { 454 /* Disable of Write protection on the selected page */ 455 status = FLASH_OB_DisableWRP(pOBInit->WRPPage); 456 } 457 if (status != HAL_OK) { 458 /* Process Unlocked */ 459 __HAL_UNLOCK(&pFlash); 460 return status; 461 } 462 } 463 464 /* Read protection configuration */ 465 if ((pOBInit->OptionType & OPTIONBYTE_RDP) == OPTIONBYTE_RDP) { 466 status = FLASH_OB_RDP_LevelConfig(pOBInit->RDPLevel); 467 if (status != HAL_OK) { 468 /* Process Unlocked */ 469 __HAL_UNLOCK(&pFlash); 470 return status; 471 } 472 } 473 474 /* USER configuration */ 475 if ((pOBInit->OptionType & OPTIONBYTE_USER) == OPTIONBYTE_USER) { 476 status = FLASH_OB_UserConfig(pOBInit->USERConfig); 477 if (status != HAL_OK) { 478 /* Process Unlocked */ 479 __HAL_UNLOCK(&pFlash); 480 return status; 481 } 482 } 483 484 /* DATA configuration*/ 485 if ((pOBInit->OptionType & OPTIONBYTE_DATA) == OPTIONBYTE_DATA) { 486 status = FLASH_OB_ProgramData(pOBInit->DATAAddress, pOBInit->DATAData); 487 if (status != HAL_OK) { 488 /* Process Unlocked */ 489 __HAL_UNLOCK(&pFlash); 490 return status; 491 } 492 } 493 494 /* Process Unlocked */ 495 __HAL_UNLOCK(&pFlash); 496 497 return status; 498 } 499 500 /** 501 * @brief Get the Option byte configuration 502 * @param pOBInit pointer to an FLASH_OBInitStruct structure that 503 * contains the configuration information for the programming. 504 * 505 * @retval None 506 */ 507 void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit) 508 { 509 pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER; 510 511 /*Get WRP*/ 512 pOBInit->WRPPage = FLASH_OB_GetWRP(); 513 514 /*Get RDP Level*/ 515 pOBInit->RDPLevel = FLASH_OB_GetRDP(); 516 517 /*Get USER*/ 518 pOBInit->USERConfig = FLASH_OB_GetUser(); 519 } 520 521 /** 522 * @brief Get the Option byte user data 523 * @param DATAAdress Address of the option byte DATA 524 * This parameter can be one of the following values: 525 * @arg @ref OB_DATA_ADDRESS_DATA0 526 * @arg @ref OB_DATA_ADDRESS_DATA1 527 * @retval Value programmed in USER data 528 */ 529 uint32_t HAL_FLASHEx_OBGetUserData(uint32_t DATAAdress) 530 { 531 uint32_t value = 0; 532 533 if (DATAAdress == OB_DATA_ADDRESS_DATA0) { 534 /* Get value programmed in OB USER Data0 */ 535 value = READ_BIT(FLASH->OBR, 536 FLASH_OBR_DATA0) >> FLASH_POSITION_OB_USERDATA0_BIT; 537 } else { 538 /* Get value programmed in OB USER Data1 */ 539 value = READ_BIT(FLASH->OBR, 540 FLASH_OBR_DATA1) >> FLASH_POSITION_OB_USERDATA1_BIT; 541 } 542 543 return value; 544 } 545 546 /** 547 * @} 548 */ 549 550 /** 551 * @} 552 */ 553 554 /** @addtogroup FLASHEx_Private_Functions 555 * @{ 556 */ 557 558 /** 559 * @brief Full erase of FLASH memory Bank 560 * @param Banks Banks to be erased 561 * This parameter can be one of the following values: 562 * @arg @ref FLASH_BANK_1 Bank1 to be erased 563 @if STM32F101xG 564 * @arg @ref FLASH_BANK_2 Bank2 to be erased 565 * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased 566 @endif 567 @if STM32F103xG 568 * @arg @ref FLASH_BANK_2 Bank2 to be erased 569 * @arg @ref FLASH_BANK_BOTH Bank1 and Bank2 to be erased 570 @endif 571 * 572 * @retval None 573 */ 574 static void FLASH_MassErase(uint32_t Banks) 575 { 576 /* Check the parameters */ 577 assert_param(IS_FLASH_BANK(Banks)); 578 579 /* Clean the error context */ 580 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 581 582 #if defined(FLASH_BANK2_END) 583 if(Banks == FLASH_BANK_BOTH) 584 { 585 /* bank1 & bank2 will be erased*/ 586 SET_BIT(FLASH->CR, FLASH_CR_MER); 587 SET_BIT(FLASH->CR2, FLASH_CR2_MER); 588 SET_BIT(FLASH->CR, FLASH_CR_STRT); 589 SET_BIT(FLASH->CR2, FLASH_CR2_STRT); 590 } 591 else if(Banks == FLASH_BANK_2) 592 { 593 /*Only bank2 will be erased*/ 594 SET_BIT(FLASH->CR2, FLASH_CR2_MER); 595 SET_BIT(FLASH->CR2, FLASH_CR2_STRT); 596 } 597 else 598 { 599 #endif /* FLASH_BANK2_END */ 600 #if !defined(FLASH_BANK2_END) 601 /* Prevent unused argument(s) compilation warning */ 602 UNUSED(Banks); 603 #endif /* FLASH_BANK2_END */ 604 /* Only bank1 will be erased*/ 605 SET_BIT(FLASH->CR, FLASH_CR_MER); 606 SET_BIT(FLASH->CR, FLASH_CR_STRT); 607 #if defined(FLASH_BANK2_END) 608 } 609 #endif /* FLASH_BANK2_END */ 610 } 611 612 /** 613 * @brief Enable the write protection of the desired pages 614 * @note An option byte erase is done automatically in this function. 615 * @note When the memory read protection level is selected (RDP level = 1), 616 * it is not possible to program or erase the flash page i if 617 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1 618 * 619 * @param WriteProtectPage specifies the page(s) to be write protected. 620 * The value of this parameter depend on device used within the same series 621 * @retval HAL status 622 */ 623 static HAL_StatusTypeDef FLASH_OB_EnableWRP(uint32_t WriteProtectPage) 624 { 625 HAL_StatusTypeDef status = HAL_OK; 626 uint16_t WRP0_Data = 0xFFFF; 627 #if defined(FLASH_WRP1_WRP1) 628 uint16_t WRP1_Data = 0xFFFF; 629 #endif /* FLASH_WRP1_WRP1 */ 630 #if defined(FLASH_WRP2_WRP2) 631 uint16_t WRP2_Data = 0xFFFF; 632 #endif /* FLASH_WRP2_WRP2 */ 633 #if defined(FLASH_WRP3_WRP3) 634 uint16_t WRP3_Data = 0xFFFF; 635 #endif /* FLASH_WRP3_WRP3 */ 636 637 /* Check the parameters */ 638 assert_param(IS_OB_WRP(WriteProtectPage)); 639 640 /* Get current write protected pages and the new pages to be protected ******/ 641 WriteProtectPage = (uint32_t) (~((~FLASH_OB_GetWRP()) | WriteProtectPage)); 642 643 #if defined(OB_WRP_PAGES0TO15MASK) 644 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK); 645 #elif defined(OB_WRP_PAGES0TO31MASK) 646 WRP0_Data = (uint16_t) (WriteProtectPage & OB_WRP_PAGES0TO31MASK); 647 #endif /* OB_WRP_PAGES0TO31MASK */ 648 649 #if defined(OB_WRP_PAGES16TO31MASK) 650 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U); 651 #elif defined(OB_WRP_PAGES32TO63MASK) 652 WRP1_Data = (uint16_t) ((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U); 653 #endif /* OB_WRP_PAGES32TO63MASK */ 654 655 #if defined(OB_WRP_PAGES64TO95MASK) 656 WRP2_Data = (uint16_t) ((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U); 657 #endif /* OB_WRP_PAGES64TO95MASK */ 658 #if defined(OB_WRP_PAGES32TO47MASK) 659 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U); 660 #endif /* OB_WRP_PAGES32TO47MASK */ 661 662 #if defined(OB_WRP_PAGES96TO127MASK) 663 WRP3_Data = 664 (uint16_t) ((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U); 665 #elif defined(OB_WRP_PAGES48TO255MASK) 666 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U); 667 #elif defined(OB_WRP_PAGES48TO511MASK) 668 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U); 669 #elif defined(OB_WRP_PAGES48TO127MASK) 670 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U); 671 #endif /* OB_WRP_PAGES96TO127MASK */ 672 673 /* Wait for last operation to be completed */ 674 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 675 676 if (status == HAL_OK) { 677 /* Clean the error context */ 678 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 679 680 /* To be able to write again option byte, need to perform a option byte erase */ 681 status = HAL_FLASHEx_OBErase(); 682 if (status == HAL_OK) { 683 /* Enable write protection */ 684 SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 685 686 #if defined(FLASH_WRP0_WRP0) 687 if (WRP0_Data != 0xFFU) { 688 OB->WRP0 &= WRP0_Data; 689 690 /* Wait for last operation to be completed */ 691 status = FLASH_WaitForLastOperation( 692 (uint32_t) FLASH_TIMEOUT_VALUE); 693 } 694 #endif /* FLASH_WRP0_WRP0 */ 695 696 #if defined(FLASH_WRP1_WRP1) 697 if ((status == HAL_OK) && (WRP1_Data != 0xFFU)) { 698 OB->WRP1 &= WRP1_Data; 699 700 /* Wait for last operation to be completed */ 701 status = FLASH_WaitForLastOperation( 702 (uint32_t) FLASH_TIMEOUT_VALUE); 703 } 704 #endif /* FLASH_WRP1_WRP1 */ 705 706 #if defined(FLASH_WRP2_WRP2) 707 if ((status == HAL_OK) && (WRP2_Data != 0xFFU)) { 708 OB->WRP2 &= WRP2_Data; 709 710 /* Wait for last operation to be completed */ 711 status = FLASH_WaitForLastOperation( 712 (uint32_t) FLASH_TIMEOUT_VALUE); 713 } 714 #endif /* FLASH_WRP2_WRP2 */ 715 716 #if defined(FLASH_WRP3_WRP3) 717 if ((status == HAL_OK) && (WRP3_Data != 0xFFU)) { 718 OB->WRP3 &= WRP3_Data; 719 720 /* Wait for last operation to be completed */ 721 status = FLASH_WaitForLastOperation( 722 (uint32_t) FLASH_TIMEOUT_VALUE); 723 } 724 #endif /* FLASH_WRP3_WRP3 */ 725 726 /* if the program operation is completed, disable the OPTPG Bit */ 727 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); 728 } 729 } 730 731 return status; 732 } 733 734 /** 735 * @brief Disable the write protection of the desired pages 736 * @note An option byte erase is done automatically in this function. 737 * @note When the memory read protection level is selected (RDP level = 1), 738 * it is not possible to program or erase the flash page i if 739 * debug features are connected or boot code is executed in RAM, even if nWRPi = 1 740 * 741 * @param WriteProtectPage specifies the page(s) to be write unprotected. 742 * The value of this parameter depend on device used within the same series 743 * @retval HAL status 744 */ 745 static HAL_StatusTypeDef FLASH_OB_DisableWRP(uint32_t WriteProtectPage) 746 { 747 HAL_StatusTypeDef status = HAL_OK; 748 uint16_t WRP0_Data = 0xFFFF; 749 #if defined(FLASH_WRP1_WRP1) 750 uint16_t WRP1_Data = 0xFFFF; 751 #endif /* FLASH_WRP1_WRP1 */ 752 #if defined(FLASH_WRP2_WRP2) 753 uint16_t WRP2_Data = 0xFFFF; 754 #endif /* FLASH_WRP2_WRP2 */ 755 #if defined(FLASH_WRP3_WRP3) 756 uint16_t WRP3_Data = 0xFFFF; 757 #endif /* FLASH_WRP3_WRP3 */ 758 759 /* Check the parameters */ 760 assert_param(IS_OB_WRP(WriteProtectPage)); 761 762 /* Get current write protected pages and the new pages to be unprotected ******/ 763 WriteProtectPage = (FLASH_OB_GetWRP() | WriteProtectPage); 764 765 #if defined(OB_WRP_PAGES0TO15MASK) 766 WRP0_Data = (uint16_t)(WriteProtectPage & OB_WRP_PAGES0TO15MASK); 767 #elif defined(OB_WRP_PAGES0TO31MASK) 768 WRP0_Data = (uint16_t) (WriteProtectPage & OB_WRP_PAGES0TO31MASK); 769 #endif /* OB_WRP_PAGES0TO31MASK */ 770 771 #if defined(OB_WRP_PAGES16TO31MASK) 772 WRP1_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES16TO31MASK) >> 8U); 773 #elif defined(OB_WRP_PAGES32TO63MASK) 774 WRP1_Data = (uint16_t) ((WriteProtectPage & OB_WRP_PAGES32TO63MASK) >> 8U); 775 #endif /* OB_WRP_PAGES32TO63MASK */ 776 777 #if defined(OB_WRP_PAGES64TO95MASK) 778 WRP2_Data = (uint16_t) ((WriteProtectPage & OB_WRP_PAGES64TO95MASK) >> 16U); 779 #endif /* OB_WRP_PAGES64TO95MASK */ 780 #if defined(OB_WRP_PAGES32TO47MASK) 781 WRP2_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES32TO47MASK) >> 16U); 782 #endif /* OB_WRP_PAGES32TO47MASK */ 783 784 #if defined(OB_WRP_PAGES96TO127MASK) 785 WRP3_Data = 786 (uint16_t) ((WriteProtectPage & OB_WRP_PAGES96TO127MASK) >> 24U); 787 #elif defined(OB_WRP_PAGES48TO255MASK) 788 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO255MASK) >> 24U); 789 #elif defined(OB_WRP_PAGES48TO511MASK) 790 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO511MASK) >> 24U); 791 #elif defined(OB_WRP_PAGES48TO127MASK) 792 WRP3_Data = (uint16_t)((WriteProtectPage & OB_WRP_PAGES48TO127MASK) >> 24U); 793 #endif /* OB_WRP_PAGES96TO127MASK */ 794 795 /* Wait for last operation to be completed */ 796 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 797 798 if (status == HAL_OK) { 799 /* Clean the error context */ 800 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 801 802 /* To be able to write again option byte, need to perform a option byte erase */ 803 status = HAL_FLASHEx_OBErase(); 804 if (status == HAL_OK) { 805 SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 806 807 #if defined(FLASH_WRP0_WRP0) 808 if (WRP0_Data != 0xFFU) { 809 OB->WRP0 |= WRP0_Data; 810 811 /* Wait for last operation to be completed */ 812 status = FLASH_WaitForLastOperation( 813 (uint32_t) FLASH_TIMEOUT_VALUE); 814 } 815 #endif /* FLASH_WRP0_WRP0 */ 816 817 #if defined(FLASH_WRP1_WRP1) 818 if ((status == HAL_OK) && (WRP1_Data != 0xFFU)) { 819 OB->WRP1 |= WRP1_Data; 820 821 /* Wait for last operation to be completed */ 822 status = FLASH_WaitForLastOperation( 823 (uint32_t) FLASH_TIMEOUT_VALUE); 824 } 825 #endif /* FLASH_WRP1_WRP1 */ 826 827 #if defined(FLASH_WRP2_WRP2) 828 if ((status == HAL_OK) && (WRP2_Data != 0xFFU)) { 829 OB->WRP2 |= WRP2_Data; 830 831 /* Wait for last operation to be completed */ 832 status = FLASH_WaitForLastOperation( 833 (uint32_t) FLASH_TIMEOUT_VALUE); 834 } 835 #endif /* FLASH_WRP2_WRP2 */ 836 837 #if defined(FLASH_WRP3_WRP3) 838 if ((status == HAL_OK) && (WRP3_Data != 0xFFU)) { 839 OB->WRP3 |= WRP3_Data; 840 841 /* Wait for last operation to be completed */ 842 status = FLASH_WaitForLastOperation( 843 (uint32_t) FLASH_TIMEOUT_VALUE); 844 } 845 #endif /* FLASH_WRP3_WRP3 */ 846 847 /* if the program operation is completed, disable the OPTPG Bit */ 848 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); 849 } 850 } 851 return status; 852 } 853 854 /** 855 * @brief Set the read protection level. 856 * @param ReadProtectLevel specifies the read protection level. 857 * This parameter can be one of the following values: 858 * @arg @ref OB_RDP_LEVEL_0 No protection 859 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory 860 * @retval HAL status 861 */ 862 static HAL_StatusTypeDef FLASH_OB_RDP_LevelConfig(uint8_t ReadProtectLevel) 863 { 864 HAL_StatusTypeDef status = HAL_OK; 865 866 /* Check the parameters */ 867 assert_param(IS_OB_RDP_LEVEL(ReadProtectLevel)); 868 869 /* Wait for last operation to be completed */ 870 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 871 872 if (status == HAL_OK) { 873 /* Clean the error context */ 874 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 875 876 /* If the previous operation is completed, proceed to erase the option bytes */ 877 SET_BIT(FLASH->CR, FLASH_CR_OPTER); 878 SET_BIT(FLASH->CR, FLASH_CR_STRT); 879 880 /* Wait for last operation to be completed */ 881 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 882 883 /* If the erase operation is completed, disable the OPTER Bit */ 884 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTER); 885 886 if (status == HAL_OK) { 887 /* Enable the Option Bytes Programming operation */ 888 SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 889 890 WRITE_REG(OB->RDP, ReadProtectLevel); 891 892 /* Wait for last operation to be completed */ 893 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 894 895 /* if the program operation is completed, disable the OPTPG Bit */ 896 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); 897 } 898 } 899 900 return status; 901 } 902 903 /** 904 * @brief Program the FLASH User Option Byte. 905 * @note Programming of the OB should be performed only after an erase (otherwise PGERR occurs) 906 * @param UserConfig The FLASH User Option Bytes values FLASH_OBR_IWDG_SW(Bit2), 907 * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4). 908 * And BFBF2(Bit5) for STM32F101xG and STM32F103xG . 909 * @retval HAL status 910 */ 911 static HAL_StatusTypeDef FLASH_OB_UserConfig(uint8_t UserConfig) 912 { 913 HAL_StatusTypeDef status = HAL_OK; 914 915 /* Check the parameters */ 916 assert_param(IS_OB_IWDG_SOURCE((UserConfig&OB_IWDG_SW))); 917 assert_param(IS_OB_STOP_SOURCE((UserConfig&OB_STOP_NO_RST))); 918 assert_param(IS_OB_STDBY_SOURCE((UserConfig&OB_STDBY_NO_RST))); 919 #if defined(FLASH_BANK2_END) 920 assert_param(IS_OB_BOOT1((UserConfig&OB_BOOT1_SET))); 921 #endif /* FLASH_BANK2_END */ 922 923 /* Wait for last operation to be completed */ 924 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 925 926 if (status == HAL_OK) { 927 /* Clean the error context */ 928 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 929 930 /* Enable the Option Bytes Programming operation */ 931 SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 932 933 #if defined(FLASH_BANK2_END) 934 OB->USER = (UserConfig | 0xF0U); 935 #else 936 OB->USER = (UserConfig | 0x88U); 937 #endif /* FLASH_BANK2_END */ 938 939 /* Wait for last operation to be completed */ 940 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 941 942 /* if the program operation is completed, disable the OPTPG Bit */ 943 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); 944 } 945 946 return status; 947 } 948 949 /** 950 * @brief Programs a half word at a specified Option Byte Data address. 951 * @note The function @ref HAL_FLASH_Unlock() should be called before to unlock the FLASH interface 952 * The function @ref HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes 953 * The function @ref HAL_FLASH_OB_Launch() should be called after to force the reload of the options bytes 954 * (system reset will occur) 955 * Programming of the OB should be performed only after an erase (otherwise PGERR occurs) 956 * @param Address specifies the address to be programmed. 957 * This parameter can be 0x1FFFF804 or 0x1FFFF806. 958 * @param Data specifies the data to be programmed. 959 * @retval HAL status 960 */ 961 static HAL_StatusTypeDef FLASH_OB_ProgramData(uint32_t Address, uint8_t Data) 962 { 963 HAL_StatusTypeDef status = HAL_ERROR; 964 965 /* Check the parameters */ 966 assert_param(IS_OB_DATA_ADDRESS(Address)); 967 968 /* Wait for last operation to be completed */ 969 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 970 971 if (status == HAL_OK) { 972 /* Clean the error context */ 973 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 974 975 /* Enables the Option Bytes Programming operation */ 976 SET_BIT(FLASH->CR, FLASH_CR_OPTPG); 977 *(__IO uint16_t*) Address = Data; 978 979 /* Wait for last operation to be completed */ 980 status = FLASH_WaitForLastOperation((uint32_t) FLASH_TIMEOUT_VALUE); 981 982 /* If the program operation is completed, disable the OPTPG Bit */ 983 CLEAR_BIT(FLASH->CR, FLASH_CR_OPTPG); 984 } 985 /* Return the Option Byte Data Program Status */ 986 return status; 987 } 988 989 /** 990 * @brief Return the FLASH Write Protection Option Bytes value. 991 * @retval The FLASH Write Protection Option Bytes value 992 */ 993 static uint32_t FLASH_OB_GetWRP(void) 994 { 995 /* Return the FLASH write protection Register value */ 996 return (uint32_t) (READ_REG(FLASH->WRPR)); 997 } 998 999 /** 1000 * @brief Returns the FLASH Read Protection level. 1001 * @retval FLASH RDP level 1002 * This parameter can be one of the following values: 1003 * @arg @ref OB_RDP_LEVEL_0 No protection 1004 * @arg @ref OB_RDP_LEVEL_1 Read protection of the memory 1005 */ 1006 static uint32_t FLASH_OB_GetRDP(void) 1007 { 1008 uint32_t readstatus = OB_RDP_LEVEL_0; 1009 uint32_t tmp_reg = 0U; 1010 1011 /* Read RDP level bits */ 1012 tmp_reg = READ_BIT(FLASH->OBR, FLASH_OBR_RDPRT); 1013 1014 if (tmp_reg == FLASH_OBR_RDPRT) { 1015 readstatus = OB_RDP_LEVEL_1; 1016 } else { 1017 readstatus = OB_RDP_LEVEL_0; 1018 } 1019 1020 return readstatus; 1021 } 1022 1023 /** 1024 * @brief Return the FLASH User Option Byte value. 1025 * @retval The FLASH User Option Bytes values: FLASH_OBR_IWDG_SW(Bit2), 1026 * FLASH_OBR_nRST_STOP(Bit3),FLASH_OBR_nRST_STDBY(Bit4). 1027 * And FLASH_OBR_BFB2(Bit5) for STM32F101xG and STM32F103xG . 1028 */ 1029 static uint8_t FLASH_OB_GetUser(void) 1030 { 1031 /* Return the User Option Byte */ 1032 return (uint8_t) ((READ_REG(FLASH->OBR) & FLASH_OBR_USER) 1033 >> FLASH_POSITION_IWDGSW_BIT); 1034 } 1035 1036 /** 1037 * @} 1038 */ 1039 1040 /** 1041 * @} 1042 */ 1043 1044 /** @addtogroup FLASH 1045 * @{ 1046 */ 1047 1048 /** @addtogroup FLASH_Private_Functions 1049 * @{ 1050 */ 1051 1052 /** 1053 * @brief Erase the specified FLASH memory page 1054 * @param PageAddress FLASH page to erase 1055 * The value of this parameter depend on device used within the same series 1056 * 1057 * @retval None 1058 */ 1059 void FLASH_PageErase(uint32_t PageAddress) 1060 { 1061 /* Clean the error context */ 1062 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE; 1063 1064 #if defined(FLASH_BANK2_END) 1065 if(PageAddress > FLASH_BANK1_END) 1066 { 1067 /* Proceed to erase the page */ 1068 SET_BIT(FLASH->CR2, FLASH_CR2_PER); 1069 WRITE_REG(FLASH->AR2, PageAddress); 1070 SET_BIT(FLASH->CR2, FLASH_CR2_STRT); 1071 } 1072 else 1073 { 1074 #endif /* FLASH_BANK2_END */ 1075 /* Proceed to erase the page */ 1076 SET_BIT(FLASH->CR, FLASH_CR_PER); 1077 WRITE_REG(FLASH->AR, PageAddress); 1078 SET_BIT(FLASH->CR, FLASH_CR_STRT); 1079 #if defined(FLASH_BANK2_END) 1080 } 1081 #endif /* FLASH_BANK2_END */ 1082 } 1083 1084 /** 1085 * @} 1086 */ 1087 1088 /** 1089 * @} 1090 */ 1091 1092 #endif /* HAL_FLASH_MODULE_ENABLED */ 1093 /** 1094 * @} 1095 */ 1096
