2 ******************************************************************************
3 * @file stm32h7xx_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 Errors functions
13 ==============================================================================
14 ##### FLASH peripheral features #####
15 ==============================================================================
17 [..] The Flash memory interface manages CPU AXI I-Code and D-Code accesses
18 to the Flash memory. It implements the erase and program Flash memory operations
19 and the read and write protection mechanisms.
21 [..] The FLASH main features are:
22 (+) Flash memory read operations
23 (+) Flash memory program/erase operations
24 (+) Read / write protections
25 (+) Option bytes programming
26 (+) Error code correction (ECC) : Data in flash are 266-bits word
27 (10 bits added per flash word)
29 ##### How to use this driver #####
30 ==============================================================================
32 This driver provides functions and macros to configure and program the FLASH
33 memory of all STM32H7xx devices.
35 (#) FLASH Memory IO Programming functions:
36 (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
37 HAL_FLASH_Lock() functions
38 (++) Program functions: 256-bit word only
39 (++) There Two modes of programming :
40 (+++) Polling mode using HAL_FLASH_Program() function
41 (+++) Interrupt mode using HAL_FLASH_Program_IT() function
43 (#) Interrupts and flags management functions :
44 (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
45 (++) Callback functions are called when the flash operations are finished :
46 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
47 HAL_FLASH_OperationErrorCallback()
48 (++) Get error flag status by calling HAL_FLASH_GetError()
50 (#) Option bytes management functions :
51 (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
52 HAL_FLASH_OB_Lock() functions
53 (++) Launch the reload of the option bytes using HAL_FLASH_OB_Launch() function.
54 In this case, a reset is generated
56 In addition to these functions, this driver includes a set of macros allowing
57 to handle the following operations:
59 (+) Enable/Disable the FLASH interrupts
60 (+) Monitor the FLASH flags status
62 (@) For any Flash memory program operation (erase or program), the CPU clock frequency
63 (HCLK) must be at least 1MHz.
64 (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
65 a Flash memory operation.
66 (@) The application can simultaneously request a read and a write operation through each AXI
68 As the Flash memory is divided into two independent banks, the embedded Flash
69 memory interface can drive different operations at the same time on each bank. For
70 example a read, write or erase operation can be executed on bank 1 while another read,
71 write or erase operation is executed on bank 2.
74 ******************************************************************************
77 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics.
78 * All rights reserved.</center></h2>
80 * This software component is licensed by ST under BSD 3-Clause license,
81 * the "License"; You may not use this file except in compliance with the
82 * License. You may obtain a copy of the License at:
83 * opensource.org/licenses/BSD-3-Clause
85 ******************************************************************************
88 /* Includes ------------------------------------------------------------------*/
89 #include "stm32h7xx_hal.h"
91 /** @addtogroup STM32H7xx_HAL_Driver
95 /** @defgroup FLASH FLASH
96 * @brief FLASH HAL module driver
100 #ifdef HAL_FLASH_MODULE_ENABLED
102 /* Private typedef -----------------------------------------------------------*/
103 /* Private define ------------------------------------------------------------*/
104 /** @addtogroup FLASH_Private_Constants
107 #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
112 /* Private macro -------------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 FLASH_ProcessTypeDef pFlash
;
115 /* Private function prototypes -----------------------------------------------*/
116 /* Exported functions ---------------------------------------------------------*/
118 /** @defgroup FLASH_Exported_Functions FLASH Exported functions
122 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
123 * @brief Programming operation functions
126 ===============================================================================
127 ##### Programming operation functions #####
128 ===============================================================================
130 This subsection provides a set of functions allowing to manage the FLASH
138 * @brief Program flash word at a specified address
139 * @param TypeProgram Indicate the way to program at a specified address.
140 * This parameter can be a value of @ref FLASH_Type_Program
141 * @param FlashAddress specifies the address to be programmed.
142 * @param DataAddress specifies the address of data to be programmed
144 * @retval HAL_StatusTypeDef HAL Status
146 HAL_StatusTypeDef
HAL_FLASH_Program(uint32_t TypeProgram
, uint32_t FlashAddress
, uint32_t DataAddress
)
150 HAL_StatusTypeDef status
;
151 __IO
uint32_t *dest_addr
= (__IO
uint32_t *)FlashAddress
;
152 __IO
uint32_t *src_addr
= (__IO
uint32_t*)DataAddress
;
154 uint8_t row_index
= FLASH_NB_32BITWORD_IN_FLASHWORD
;
156 /* Check the parameters */
157 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram
));
158 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress
));
163 #if defined (FLASH_OPTCR_PG_OTP)
164 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress
)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress
)))
166 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress
))
167 #endif /* FLASH_OPTCR_PG_OTP */
176 /* Reset error code */
177 pFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
179 /* Wait for last operation to be completed */
180 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
, bank
);
184 if(bank
== FLASH_BANK_1
)
186 #if defined (FLASH_OPTCR_PG_OTP)
187 if (TypeProgram
== FLASH_TYPEPROGRAM_OTPWORD
)
190 SET_BIT(FLASH
->OPTCR
, FLASH_OPTCR_PG_OTP
);
193 #endif /* FLASH_OPTCR_PG_OTP */
196 SET_BIT(FLASH
->CR1
, FLASH_CR_PG
);
202 SET_BIT(FLASH
->CR2
, FLASH_CR_PG
);
208 #if defined (FLASH_OPTCR_PG_OTP)
209 if (TypeProgram
== FLASH_TYPEPROGRAM_OTPWORD
)
211 /* Program an OTP word (16 bits) */
212 *(__IO
uint16_t *)FlashAddress
= *(__IO
uint16_t*)DataAddress
;
215 #endif /* FLASH_OPTCR_PG_OTP */
217 /* Program the flash word */
220 *dest_addr
= *src_addr
;
224 } while (row_index
!= 0U);
230 /* Wait for last operation to be completed */
231 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
, bank
);
233 #if defined (FLASH_OPTCR_PG_OTP)
234 if (TypeProgram
== FLASH_TYPEPROGRAM_OTPWORD
)
236 /* If the program operation is completed, disable the OTP_PG */
237 CLEAR_BIT(FLASH
->OPTCR
, FLASH_OPTCR_PG_OTP
);
240 #endif /* FLASH_OPTCR_PG_OTP */
242 if(bank
== FLASH_BANK_1
)
244 /* If the program operation is completed, disable the PG */
245 CLEAR_BIT(FLASH
->CR1
, FLASH_CR_PG
);
249 /* If the program operation is completed, disable the PG */
250 CLEAR_BIT(FLASH
->CR2
, FLASH_CR_PG
);
255 /* Process Unlocked */
256 __HAL_UNLOCK(&pFlash
);
262 * @brief Program flash words of 256 bits at a specified address with interrupt enabled.
263 * @param TypeProgram Indicate the way to program at a specified address.
264 * This parameter can be a value of @ref FLASH_Type_Program
265 * @param FlashAddress specifies the address to be programmed.
266 * @param DataAddress specifies the address of data (256 bits) to be programmed
270 HAL_StatusTypeDef
HAL_FLASH_Program_IT(uint32_t TypeProgram
, uint32_t FlashAddress
, uint32_t DataAddress
)
274 HAL_StatusTypeDef status
;
275 __IO
uint32_t *dest_addr
= (__IO
uint32_t*)FlashAddress
;
276 __IO
uint32_t *src_addr
= (__IO
uint32_t*)DataAddress
;
278 uint8_t row_index
= FLASH_NB_32BITWORD_IN_FLASHWORD
;
280 /* Check the parameters */
281 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram
));
282 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress
));
287 /* Reset error code */
288 pFlash
.ErrorCode
= HAL_FLASH_ERROR_NONE
;
290 #if defined (FLASH_OPTCR_PG_OTP)
291 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress
)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress
)))
293 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress
))
294 #endif /* FLASH_OPTCR_PG_OTP */
303 /* Wait for last operation to be completed */
304 status
= FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
, bank
);
306 if (status
!= HAL_OK
)
308 /* Process Unlocked */
309 __HAL_UNLOCK(&pFlash
);
313 pFlash
.Address
= FlashAddress
;
315 if(bank
== FLASH_BANK_1
)
317 /* Set internal variables used by the IRQ handler */
318 pFlash
.ProcedureOnGoing
= FLASH_PROC_PROGRAM_BANK1
;
320 #if defined (FLASH_OPTCR_PG_OTP)
321 if (TypeProgram
== FLASH_TYPEPROGRAM_OTPWORD
)
324 SET_BIT(FLASH
->OPTCR
, FLASH_OPTCR_PG_OTP
);
327 #endif /* FLASH_OPTCR_PG_OTP */
330 SET_BIT(FLASH
->CR1
, FLASH_CR_PG
);
333 /* Enable End of Operation and Error interrupts for Bank 1 */
334 #if defined (FLASH_CR_OPERRIE)
335 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1
| FLASH_IT_WRPERR_BANK1
| FLASH_IT_PGSERR_BANK1
| \
336 FLASH_IT_STRBERR_BANK1
| FLASH_IT_INCERR_BANK1
| FLASH_IT_OPERR_BANK1
);
338 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1
| FLASH_IT_WRPERR_BANK1
| FLASH_IT_PGSERR_BANK1
| \
339 FLASH_IT_STRBERR_BANK1
| FLASH_IT_INCERR_BANK1
);
340 #endif /* FLASH_CR_OPERRIE */
344 /* Set internal variables used by the IRQ handler */
345 pFlash
.ProcedureOnGoing
= FLASH_PROC_PROGRAM_BANK2
;
348 SET_BIT(FLASH
->CR2
, FLASH_CR_PG
);
350 /* Enable End of Operation and Error interrupts for Bank2 */
351 #if defined (FLASH_CR_OPERRIE)
352 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2
| FLASH_IT_WRPERR_BANK2
| FLASH_IT_PGSERR_BANK2
| \
353 FLASH_IT_STRBERR_BANK2
| FLASH_IT_INCERR_BANK2
| FLASH_IT_OPERR_BANK2
);
355 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2
| FLASH_IT_WRPERR_BANK2
| FLASH_IT_PGSERR_BANK2
| \
356 FLASH_IT_STRBERR_BANK2
| FLASH_IT_INCERR_BANK2
);
357 #endif /* FLASH_CR_OPERRIE */
363 #if defined (FLASH_OPTCR_PG_OTP)
364 if (TypeProgram
== FLASH_TYPEPROGRAM_OTPWORD
)
366 /* Program an OTP word (16 bits) */
367 *(__IO
uint16_t *)FlashAddress
= *(__IO
uint16_t*)DataAddress
;
370 #endif /* FLASH_OPTCR_PG_OTP */
372 /* Program the flash word */
375 *dest_addr
= *src_addr
;
379 } while (row_index
!= 0U);
390 * @brief This function handles FLASH interrupt request.
393 void HAL_FLASH_IRQHandler(void)
397 FLASH_ProcedureTypeDef procedure
;
399 /* Check FLASH Bank1 End of Operation flag */
400 if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP
) != RESET
)
402 if(pFlash
.ProcedureOnGoing
== FLASH_PROC_SECTERASE_BANK1
)
404 /* Nb of sector to erased can be decreased */
405 pFlash
.NbSectorsToErase
--;
407 /* Check if there are still sectors to erase */
408 if(pFlash
.NbSectorsToErase
!= 0U)
410 /* Indicate user which sector has been erased */
411 HAL_FLASH_EndOfOperationCallback(pFlash
.Sector
);
413 /* Clear bank 1 End of Operation pending bit */
414 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1
);
416 /* Increment sector number */
418 temp
= pFlash
.Sector
;
419 FLASH_Erase_Sector(temp
, FLASH_BANK_1
, pFlash
.VoltageForErase
);
423 /* No more sectors to Erase, user callback can be called */
424 /* Reset Sector and stop Erase sectors procedure */
425 pFlash
.Sector
= 0xFFFFFFFFU
;
426 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
428 /* FLASH EOP interrupt user callback */
429 HAL_FLASH_EndOfOperationCallback(pFlash
.Sector
);
431 /* Clear FLASH End of Operation pending bit */
432 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1
);
437 procedure
= pFlash
.ProcedureOnGoing
;
439 if((procedure
== FLASH_PROC_MASSERASE_BANK1
) || (procedure
== FLASH_PROC_ALLBANK_MASSERASE
))
441 /* MassErase ended. Return the selected bank */
442 /* FLASH EOP interrupt user callback */
443 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1
);
445 else if(procedure
== FLASH_PROC_PROGRAM_BANK1
)
447 /* Program ended. Return the selected address */
448 /* FLASH EOP interrupt user callback */
449 HAL_FLASH_EndOfOperationCallback(pFlash
.Address
);
456 if((procedure
!= FLASH_PROC_SECTERASE_BANK2
) && \
457 (procedure
!= FLASH_PROC_MASSERASE_BANK2
) && \
458 (procedure
!= FLASH_PROC_PROGRAM_BANK2
))
460 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
461 /* Clear FLASH End of Operation pending bit */
462 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1
);
467 /* Check FLASH Bank2 End of Operation flag */
468 if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP
) != RESET
)
470 if(pFlash
.ProcedureOnGoing
== FLASH_PROC_SECTERASE_BANK2
)
472 /*Nb of sector to erased can be decreased*/
473 pFlash
.NbSectorsToErase
--;
475 /* Check if there are still sectors to erase*/
476 if(pFlash
.NbSectorsToErase
!= 0U)
478 /*Indicate user which sector has been erased*/
479 HAL_FLASH_EndOfOperationCallback(pFlash
.Sector
);
481 /* Clear bank 2 End of Operation pending bit */
482 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2
);
484 /*Increment sector number*/
486 temp
= pFlash
.Sector
;
487 FLASH_Erase_Sector(temp
, FLASH_BANK_2
, pFlash
.VoltageForErase
);
491 /* No more sectors to Erase, user callback can be called */
492 /* Reset Sector and stop Erase sectors procedure */
493 pFlash
.Sector
= 0xFFFFFFFFU
;
494 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
496 /* FLASH EOP interrupt user callback */
497 HAL_FLASH_EndOfOperationCallback(pFlash
.Sector
);
499 /* Clear FLASH End of Operation pending bit */
500 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2
);
505 procedure
= pFlash
.ProcedureOnGoing
;
507 if((procedure
== FLASH_PROC_MASSERASE_BANK2
) || (procedure
== FLASH_PROC_ALLBANK_MASSERASE
))
509 /*MassErase ended. Return the selected bank*/
510 /* FLASH EOP interrupt user callback */
511 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2
);
513 else if(procedure
== FLASH_PROC_PROGRAM_BANK2
)
515 /* Program ended. Return the selected address */
516 /* FLASH EOP interrupt user callback */
517 HAL_FLASH_EndOfOperationCallback(pFlash
.Address
);
524 if((procedure
!= FLASH_PROC_SECTERASE_BANK1
) && \
525 (procedure
!= FLASH_PROC_MASSERASE_BANK1
) && \
526 (procedure
!= FLASH_PROC_PROGRAM_BANK1
))
528 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
529 /* Clear FLASH End of Operation pending bit */
530 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2
);
535 /* Check FLASH Bank1 operation error flags */
536 #if defined (FLASH_SR_OPERR)
537 errorflag
= FLASH
->SR1
& (FLASH_FLAG_WRPERR_BANK1
| FLASH_FLAG_PGSERR_BANK1
| FLASH_FLAG_STRBERR_BANK1
| \
538 FLASH_FLAG_INCERR_BANK1
| FLASH_FLAG_OPERR_BANK1
);
540 errorflag
= FLASH
->SR1
& (FLASH_FLAG_WRPERR_BANK1
| FLASH_FLAG_PGSERR_BANK1
| FLASH_FLAG_STRBERR_BANK1
| \
541 FLASH_FLAG_INCERR_BANK1
);
542 #endif /* FLASH_SR_OPERR */
546 /* Save the error code */
547 pFlash
.ErrorCode
|= errorflag
;
549 /* Clear error programming flags */
550 __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag
);
552 procedure
= pFlash
.ProcedureOnGoing
;
554 if(procedure
== FLASH_PROC_SECTERASE_BANK1
)
556 /* Return the faulty sector */
557 temp
= pFlash
.Sector
;
558 pFlash
.Sector
= 0xFFFFFFFFU
;
560 else if((procedure
== FLASH_PROC_MASSERASE_BANK1
) || (procedure
== FLASH_PROC_ALLBANK_MASSERASE
))
562 /* Return the faulty bank */
567 /* Return the faulty address */
568 temp
= pFlash
.Address
;
571 /* Stop the procedure ongoing*/
572 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
574 /* FLASH error interrupt user callback */
575 HAL_FLASH_OperationErrorCallback(temp
);
578 /* Check FLASH Bank2 operation error flags */
579 #if defined (FLASH_SR_OPERR)
580 errorflag
= FLASH
->SR2
& ((FLASH_FLAG_WRPERR_BANK2
| FLASH_FLAG_PGSERR_BANK2
| FLASH_FLAG_STRBERR_BANK2
| \
581 FLASH_FLAG_INCERR_BANK2
| FLASH_FLAG_OPERR_BANK2
) & 0x7FFFFFFFU
);
583 errorflag
= FLASH
->SR2
& ((FLASH_FLAG_WRPERR_BANK2
| FLASH_FLAG_PGSERR_BANK2
| FLASH_FLAG_STRBERR_BANK2
| \
584 FLASH_FLAG_INCERR_BANK2
) & 0x7FFFFFFFU
);
585 #endif /* FLASH_SR_OPERR */
589 /* Save the error code */
590 pFlash
.ErrorCode
|= (errorflag
| 0x80000000U
);
592 /* Clear error programming flags */
593 __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag
);
595 procedure
= pFlash
.ProcedureOnGoing
;
597 if(procedure
== FLASH_PROC_SECTERASE_BANK2
)
599 /*return the faulty sector*/
600 temp
= pFlash
.Sector
;
601 pFlash
.Sector
= 0xFFFFFFFFU
;
603 else if((procedure
== FLASH_PROC_MASSERASE_BANK2
) || (procedure
== FLASH_PROC_ALLBANK_MASSERASE
))
605 /*return the faulty bank*/
610 /*return the faulty address*/
611 temp
= pFlash
.Address
;
614 /*Stop the procedure ongoing*/
615 pFlash
.ProcedureOnGoing
= FLASH_PROC_NONE
;
617 /* FLASH error interrupt user callback */
618 HAL_FLASH_OperationErrorCallback(temp
);
621 if(pFlash
.ProcedureOnGoing
== FLASH_PROC_NONE
)
623 #if defined (FLASH_CR_OPERRIE)
624 /* Disable Bank1 Operation and Error source interrupt */
625 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1
| FLASH_IT_WRPERR_BANK1
| FLASH_IT_PGSERR_BANK1
| \
626 FLASH_IT_STRBERR_BANK1
| FLASH_IT_INCERR_BANK1
| FLASH_IT_OPERR_BANK1
);
628 /* Disable Bank2 Operation and Error source interrupt */
629 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2
| FLASH_IT_WRPERR_BANK2
| FLASH_IT_PGSERR_BANK2
| \
630 FLASH_IT_STRBERR_BANK2
| FLASH_IT_INCERR_BANK2
| FLASH_IT_OPERR_BANK2
);
632 /* Disable Bank1 Operation and Error source interrupt */
633 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1
| FLASH_IT_WRPERR_BANK1
| FLASH_IT_PGSERR_BANK1
| \
634 FLASH_IT_STRBERR_BANK1
| FLASH_IT_INCERR_BANK1
);
636 /* Disable Bank2 Operation and Error source interrupt */
637 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2
| FLASH_IT_WRPERR_BANK2
| FLASH_IT_PGSERR_BANK2
| \
638 FLASH_IT_STRBERR_BANK2
| FLASH_IT_INCERR_BANK2
);
639 #endif /* FLASH_CR_OPERRIE */
641 /* Process Unlocked */
642 __HAL_UNLOCK(&pFlash
);
647 * @brief FLASH end of operation interrupt callback
648 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
649 * Mass Erase: Bank number which has been requested to erase
650 * Sectors Erase: Sector which has been erased
651 * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
652 * Program: Address which was selected for data program
655 __weak
void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue
)
657 /* Prevent unused argument(s) compilation warning */
660 /* NOTE : This function Should not be modified, when the callback is needed,
661 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
666 * @brief FLASH operation error interrupt callback
667 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
668 * Mass Erase: Bank number which has been requested to erase
669 * Sectors Erase: Sector number which returned an error
670 * Program: Address which was selected for data program
673 __weak
void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue
)
675 /* Prevent unused argument(s) compilation warning */
678 /* NOTE : This function Should not be modified, when the callback is needed,
679 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
687 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
688 * @brief Management functions
691 ===============================================================================
692 ##### Peripheral Control functions #####
693 ===============================================================================
695 This subsection provides a set of functions allowing to control the FLASH
703 * @brief Unlock the FLASH control registers access
706 HAL_StatusTypeDef
HAL_FLASH_Unlock(void)
708 if(READ_BIT(FLASH
->CR1
, FLASH_CR_LOCK
) != 0U)
710 /* Authorize the FLASH Bank1 Registers access */
711 WRITE_REG(FLASH
->KEYR1
, FLASH_KEY1
);
712 WRITE_REG(FLASH
->KEYR1
, FLASH_KEY2
);
714 /* Verify Flash Bank1 is unlocked */
715 if (READ_BIT(FLASH
->CR1
, FLASH_CR_LOCK
) != 0U)
721 if(READ_BIT(FLASH
->CR2
, FLASH_CR_LOCK
) != 0U)
723 /* Authorize the FLASH Bank2 Registers access */
724 WRITE_REG(FLASH
->KEYR2
, FLASH_KEY1
);
725 WRITE_REG(FLASH
->KEYR2
, FLASH_KEY2
);
727 /* Verify Flash Bank2 is unlocked */
728 if (READ_BIT(FLASH
->CR2
, FLASH_CR_LOCK
) != 0U)
738 * @brief Locks the FLASH control registers access
741 HAL_StatusTypeDef
HAL_FLASH_Lock(void)
743 /* Set the LOCK Bit to lock the FLASH Bank1 Control Register access */
744 SET_BIT(FLASH
->CR1
, FLASH_CR_LOCK
);
746 /* Verify Flash Bank1 is locked */
747 if (READ_BIT(FLASH
->CR1
, FLASH_CR_LOCK
) == 0U)
752 /* Set the LOCK Bit to lock the FLASH Bank2 Control Register access */
753 SET_BIT(FLASH
->CR2
, FLASH_CR_LOCK
);
755 /* Verify Flash Bank2 is locked */
756 if (READ_BIT(FLASH
->CR2
, FLASH_CR_LOCK
) == 0U)
765 * @brief Unlock the FLASH Option Control Registers access.
768 HAL_StatusTypeDef
HAL_FLASH_OB_Unlock(void)
770 if(READ_BIT(FLASH
->OPTCR
, FLASH_OPTCR_OPTLOCK
) != 0U)
772 /* Authorizes the Option Byte registers programming */
773 WRITE_REG(FLASH
->OPTKEYR
, FLASH_OPT_KEY1
);
774 WRITE_REG(FLASH
->OPTKEYR
, FLASH_OPT_KEY2
);
776 /* Verify that the Option Bytes are unlocked */
777 if (READ_BIT(FLASH
->OPTCR
, FLASH_OPTCR_OPTLOCK
) != 0U)
787 * @brief Lock the FLASH Option Control Registers access.
790 HAL_StatusTypeDef
HAL_FLASH_OB_Lock(void)
792 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
793 SET_BIT(FLASH
->OPTCR
, FLASH_OPTCR_OPTLOCK
);
795 /* Verify that the Option Bytes are locked */
796 if (READ_BIT(FLASH
->OPTCR
, FLASH_OPTCR_OPTLOCK
) == 0U)
805 * @brief Launch the option bytes loading.
808 HAL_StatusTypeDef
HAL_FLASH_OB_Launch(void)
810 HAL_StatusTypeDef status
;
812 /* Wait for CRC computation to be completed */
813 if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
, FLASH_BANK_1
) != HAL_OK
)
817 else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
, FLASH_BANK_2
) != HAL_OK
)
826 if (status
== HAL_OK
)
828 /* Set OPTSTRT Bit */
829 SET_BIT(FLASH
->OPTCR
, FLASH_OPTCR_OPTSTART
);
831 /* Wait for OB change operation to be completed */
832 status
= FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE
);
842 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
843 * @brief Peripheral Errors functions
846 ===============================================================================
847 ##### Peripheral Errors functions #####
848 ===============================================================================
850 This subsection permits to get in run-time Errors of the FLASH peripheral.
857 * @brief Get the specific FLASH error flag.
858 * @retval HAL_FLASH_ERRORCode The returned value can be:
859 * @arg HAL_FLASH_ERROR_NONE : No error set
861 * @arg HAL_FLASH_ERROR_WRP_BANK1 : Write Protection Error on Bank 1
862 * @arg HAL_FLASH_ERROR_PGS_BANK1 : Program Sequence Error on Bank 1
863 * @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
864 * @arg HAL_FLASH_ERROR_INC_BANK1 : Inconsistency Error on Bank 1
865 * @arg HAL_FLASH_ERROR_OPE_BANK1 : Operation Error on Bank 1
866 * @arg HAL_FLASH_ERROR_RDP_BANK1 : Read Protection Error on Bank 1
867 * @arg HAL_FLASH_ERROR_RDS_BANK1 : Read Secured Error on Bank 1
868 * @arg HAL_FLASH_ERROR_SNECC_BANK1: ECC Single Correction Error on Bank 1
869 * @arg HAL_FLASH_ERROR_DBECC_BANK1: ECC Double Detection Error on Bank 1
870 * @arg HAL_FLASH_ERROR_CRCRD_BANK1: CRC Read Error on Bank 1
872 * @arg HAL_FLASH_ERROR_WRP_BANK2 : Write Protection Error on Bank 2
873 * @arg HAL_FLASH_ERROR_PGS_BANK2 : Program Sequence Error on Bank 2
874 * @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
875 * @arg HAL_FLASH_ERROR_INC_BANK2 : Inconsistency Error on Bank 2
876 * @arg HAL_FLASH_ERROR_OPE_BANK2 : Operation Error on Bank 2
877 * @arg HAL_FLASH_ERROR_RDP_BANK2 : Read Protection Error on Bank 2
878 * @arg HAL_FLASH_ERROR_RDS_BANK2 : Read Secured Error on Bank 2
879 * @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
880 * @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
881 * @arg HAL_FLASH_ERROR_CRCRD_BANK2: CRC Read Error on Bank 2
884 uint32_t HAL_FLASH_GetError(void)
886 return pFlash
.ErrorCode
;
897 /* Private functions ---------------------------------------------------------*/
899 /** @addtogroup FLASH_Private_Functions
904 * @brief Wait for a FLASH operation to complete.
905 * @param Timeout maximum flash operation timeout
906 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
907 * @retval HAL_StatusTypeDef HAL Status
909 HAL_StatusTypeDef
FLASH_WaitForLastOperation(uint32_t Timeout
, uint32_t Bank
)
911 /* Wait for the FLASH operation to complete by polling on QW flag to be reset.
912 Even if the FLASH operation fails, the QW flag will be reset and an error
915 uint32_t bsyflag
, errorflag
;
916 uint32_t tickstart
= HAL_GetTick();
918 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank
));
920 /* Select bsyflag depending on Bank */
921 if(Bank
== FLASH_BANK_1
)
923 bsyflag
= FLASH_FLAG_QW_BANK1
;
927 bsyflag
= FLASH_FLAG_QW_BANK2
;
930 while(__HAL_FLASH_GET_FLAG(bsyflag
))
932 if(Timeout
!= HAL_MAX_DELAY
)
934 if(((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0U))
941 /* Get Error Flags */
942 if (Bank
== FLASH_BANK_1
)
944 errorflag
= FLASH
->SR1
& FLASH_FLAG_ALL_ERRORS_BANK1
;
948 errorflag
= (FLASH
->SR2
& FLASH_FLAG_ALL_ERRORS_BANK2
) | 0x80000000U
;
951 /* In case of error reported in Flash SR1 or SR2 registers (ECCC not managed as an error) */
952 if((errorflag
& 0x7DFFFFFFU
) != 0U)
954 /*Save the error code*/
955 pFlash
.ErrorCode
|= errorflag
;
957 /* Clear error programming flags */
958 __HAL_FLASH_CLEAR_FLAG(errorflag
);
963 /* Check FLASH End of Operation flag */
964 if(Bank
== FLASH_BANK_1
)
966 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1
))
968 /* Clear FLASH End of Operation pending bit */
969 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1
);
974 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2
))
976 /* Clear FLASH End of Operation pending bit */
977 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2
);
985 * @brief Wait for a FLASH Option Bytes change operation to complete.
986 * @param Timeout maximum flash operation timeout
987 * @retval HAL_StatusTypeDef HAL Status
989 HAL_StatusTypeDef
FLASH_OB_WaitForLastOperation(uint32_t Timeout
)
992 uint32_t tickstart
= HAL_GetTick();
994 /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset */
995 while(READ_BIT(FLASH
->OPTSR_CUR
, FLASH_OPTSR_OPT_BUSY
) != 0U)
997 if(Timeout
!= HAL_MAX_DELAY
)
999 if(((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0U))
1006 /* Check option byte change error */
1007 if(READ_BIT(FLASH
->OPTSR_CUR
, FLASH_OPTSR_OPTCHANGEERR
) != 0U)
1009 /* Save the error code */
1010 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_OB_CHANGE
;
1012 /* Clear the OB error flag */
1013 FLASH
->OPTCCR
|= FLASH_OPTCCR_CLR_OPTCHANGEERR
;
1018 /* If there is no error flag set */
1023 * @brief Wait for a FLASH CRC computation to complete.
1024 * @param Timeout maximum flash operation timeout
1025 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
1026 * @retval HAL_StatusTypeDef HAL Status
1028 HAL_StatusTypeDef
FLASH_CRC_WaitForLastOperation(uint32_t Timeout
, uint32_t Bank
)
1031 uint32_t tickstart
= HAL_GetTick();
1033 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank
));
1035 /* Select bsyflag depending on Bank */
1036 if(Bank
== FLASH_BANK_1
)
1038 bsyflag
= FLASH_FLAG_CRC_BUSY_BANK1
;
1042 bsyflag
= FLASH_FLAG_CRC_BUSY_BANK2
;
1045 /* Wait for the FLASH CRC computation to complete by polling on CRC_BUSY flag to be reset */
1046 while(__HAL_FLASH_GET_FLAG(bsyflag
))
1048 if(Timeout
!= HAL_MAX_DELAY
)
1050 if(((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0U))
1057 /* Check FLASH CRC read error flag */
1058 if(Bank
== FLASH_BANK_1
)
1060 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1
))
1062 /* Save the error code */
1063 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_CRCRD_BANK1
;
1065 /* Clear FLASH CRC read error pending bit */
1066 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1
);
1073 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2
))
1075 /* Save the error code */
1076 pFlash
.ErrorCode
|= HAL_FLASH_ERROR_CRCRD_BANK2
;
1078 /* Clear FLASH CRC read error pending bit */
1079 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2
);
1085 /* If there is no error flag set */
1093 #endif /* HAL_FLASH_MODULE_ENABLED */
1103 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/