Merge maintenance-8.x.x fixes into master
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_flash.c
blob3706941f6b63a392aaf0884d3e1126998b4ff879
1 /**
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
12 @verbatim
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 ==============================================================================
31 [..]
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
55 [..]
56 In addition to these functions, this driver includes a set of macros allowing
57 to handle the following operations:
58 (+) Set the latency
59 (+) Enable/Disable the FLASH interrupts
60 (+) Monitor the FLASH flags status
61 [..]
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
67 interface.
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.
73 @endverbatim
74 ******************************************************************************
75 * @attention
77 * Copyright (c) 2017 STMicroelectronics.
78 * All rights reserved.
80 * This software is licensed under terms that can be found in the LICENSE file in
81 * the root directory of this software component.
82 * If no LICENSE file comes with this software, it is provided AS-IS.
83 ******************************************************************************
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32h7xx_hal.h"
89 /** @addtogroup STM32H7xx_HAL_Driver
90 * @{
93 /** @defgroup FLASH FLASH
94 * @brief FLASH HAL module driver
95 * @{
98 #ifdef HAL_FLASH_MODULE_ENABLED
100 /* Private typedef -----------------------------------------------------------*/
101 /* Private define ------------------------------------------------------------*/
102 /** @addtogroup FLASH_Private_Constants
103 * @{
105 #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
107 * @}
109 /* Private macro -------------------------------------------------------------*/
110 /* Private variables ---------------------------------------------------------*/
111 /** @addtogroup FLASH_Private_Variables
112 * @{
114 FLASH_ProcessTypeDef pFlash;
116 * @}
118 /* Private function prototypes -----------------------------------------------*/
119 /* Exported functions ---------------------------------------------------------*/
121 /** @defgroup FLASH_Exported_Functions FLASH Exported functions
122 * @{
125 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
126 * @brief Programming operation functions
128 @verbatim
129 ===============================================================================
130 ##### Programming operation functions #####
131 ===============================================================================
132 [..]
133 This subsection provides a set of functions allowing to manage the FLASH
134 program operations.
136 @endverbatim
137 * @{
141 * @brief Program a flash word at a specified address
142 * @param TypeProgram Indicate the way to program at a specified address.
143 * This parameter can be a value of @ref FLASH_Type_Program
144 * @param FlashAddress specifies the address to be programmed.
145 * This parameter shall be aligned to the Flash word:
146 * - 256 bits for STM32H74x/5X devices (8x 32bits words)
147 * - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
148 * - 256 bits for STM32H72x/3X devices (8x 32bits words)
149 * @param DataAddress specifies the address of data to be programmed.
150 * This parameter shall be 32-bit aligned
152 * @retval HAL_StatusTypeDef HAL Status
154 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
156 HAL_StatusTypeDef status;
157 __IO uint32_t *dest_addr = (__IO uint32_t *)FlashAddress;
158 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
159 uint32_t bank;
160 uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
162 /* Check the parameters */
163 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
164 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
166 /* Process Locked */
167 __HAL_LOCK(&pFlash);
169 #if defined (FLASH_OPTCR_PG_OTP)
170 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
171 #else
172 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
173 #endif /* FLASH_OPTCR_PG_OTP */
175 bank = FLASH_BANK_1;
176 /* Prevent unused argument(s) compilation warning */
177 UNUSED(TypeProgram);
179 #if defined (DUAL_BANK)
180 else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
182 bank = FLASH_BANK_2;
184 #endif /* DUAL_BANK */
185 else
187 return HAL_ERROR;
190 /* Reset error code */
191 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
193 /* Wait for last operation to be completed */
194 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
196 if(status == HAL_OK)
198 #if defined (DUAL_BANK)
199 if(bank == FLASH_BANK_1)
201 #if defined (FLASH_OPTCR_PG_OTP)
202 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
204 /* Set OTP_PG bit */
205 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
207 else
208 #endif /* FLASH_OPTCR_PG_OTP */
210 /* Set PG bit */
211 SET_BIT(FLASH->CR1, FLASH_CR_PG);
214 else
216 /* Set PG bit */
217 SET_BIT(FLASH->CR2, FLASH_CR_PG);
219 #else /* Single Bank */
220 #if defined (FLASH_OPTCR_PG_OTP)
221 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
223 /* Set OTP_PG bit */
224 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
226 else
227 #endif /* FLASH_OPTCR_PG_OTP */
229 /* Set PG bit */
230 SET_BIT(FLASH->CR1, FLASH_CR_PG);
232 #endif /* DUAL_BANK */
234 __ISB();
235 __DSB();
237 #if defined (FLASH_OPTCR_PG_OTP)
238 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
240 /* Program an OTP word (16 bits) */
241 *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
243 else
244 #endif /* FLASH_OPTCR_PG_OTP */
246 /* Program the flash word */
249 *dest_addr = *src_addr;
250 dest_addr++;
251 src_addr++;
252 row_index--;
253 } while (row_index != 0U);
256 __ISB();
257 __DSB();
259 /* Wait for last operation to be completed */
260 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
262 #if defined (DUAL_BANK)
263 #if defined (FLASH_OPTCR_PG_OTP)
264 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
266 /* If the program operation is completed, disable the OTP_PG */
267 CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
269 else
270 #endif /* FLASH_OPTCR_PG_OTP */
272 if(bank == FLASH_BANK_1)
274 /* If the program operation is completed, disable the PG */
275 CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
277 else
279 /* If the program operation is completed, disable the PG */
280 CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
283 #else /* Single Bank */
284 #if defined (FLASH_OPTCR_PG_OTP)
285 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
287 /* If the program operation is completed, disable the OTP_PG */
288 CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
290 else
291 #endif /* FLASH_OPTCR_PG_OTP */
293 /* If the program operation is completed, disable the PG */
294 CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
296 #endif /* DUAL_BANK */
299 /* Process Unlocked */
300 __HAL_UNLOCK(&pFlash);
302 return status;
306 * @brief Program a flash word at a specified address with interrupt enabled.
307 * @param TypeProgram Indicate the way to program at a specified address.
308 * This parameter can be a value of @ref FLASH_Type_Program
309 * @param FlashAddress specifies the address to be programmed.
310 * This parameter shall be aligned to the Flash word:
311 * - 256 bits for STM32H74x/5X devices (8x 32bits words)
312 * - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
313 * - 256 bits for STM32H72x/3X devices (8x 32bits words)
314 * @param DataAddress specifies the address of data to be programmed.
315 * This parameter shall be 32-bit aligned
317 * @retval HAL Status
319 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
321 HAL_StatusTypeDef status;
322 __IO uint32_t *dest_addr = (__IO uint32_t*)FlashAddress;
323 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
324 uint32_t bank;
325 uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
327 /* Check the parameters */
328 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
329 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
331 /* Process Locked */
332 __HAL_LOCK(&pFlash);
334 /* Reset error code */
335 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
337 #if defined (FLASH_OPTCR_PG_OTP)
338 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
339 #else
340 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
341 #endif /* FLASH_OPTCR_PG_OTP */
343 bank = FLASH_BANK_1;
344 /* Prevent unused argument(s) compilation warning */
345 UNUSED(TypeProgram);
347 #if defined (DUAL_BANK)
348 else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
350 bank = FLASH_BANK_2;
352 #endif /* DUAL_BANK */
353 else
355 return HAL_ERROR;
358 /* Wait for last operation to be completed */
359 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
361 if (status != HAL_OK)
363 /* Process Unlocked */
364 __HAL_UNLOCK(&pFlash);
366 else
368 pFlash.Address = FlashAddress;
370 #if defined (DUAL_BANK)
371 if(bank == FLASH_BANK_1)
373 /* Set internal variables used by the IRQ handler */
374 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
376 #if defined (FLASH_OPTCR_PG_OTP)
377 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
379 /* Set OTP_PG bit */
380 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
382 else
383 #endif /* FLASH_OPTCR_PG_OTP */
385 /* Set PG bit */
386 SET_BIT(FLASH->CR1, FLASH_CR_PG);
389 /* Enable End of Operation and Error interrupts for Bank 1 */
390 #if defined (FLASH_CR_OPERRIE)
391 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
392 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
393 #else
394 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
395 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
396 #endif /* FLASH_CR_OPERRIE */
398 else
400 /* Set internal variables used by the IRQ handler */
401 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
403 /* Set PG bit */
404 SET_BIT(FLASH->CR2, FLASH_CR_PG);
406 /* Enable End of Operation and Error interrupts for Bank2 */
407 #if defined (FLASH_CR_OPERRIE)
408 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
409 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
410 #else
411 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
412 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
413 #endif /* FLASH_CR_OPERRIE */
415 #else /* Single Bank */
416 /* Set internal variables used by the IRQ handler */
417 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
419 #if defined (FLASH_OPTCR_PG_OTP)
420 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
422 /* Set OTP_PG bit */
423 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
425 else
426 #endif /* FLASH_OPTCR_PG_OTP */
428 /* Set PG bit */
429 SET_BIT(FLASH->CR1, FLASH_CR_PG);
432 /* Enable End of Operation and Error interrupts for Bank 1 */
433 #if defined (FLASH_CR_OPERRIE)
434 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
435 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
436 #else
437 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
438 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
439 #endif /* FLASH_CR_OPERRIE */
440 #endif /* DUAL_BANK */
442 __ISB();
443 __DSB();
445 #if defined (FLASH_OPTCR_PG_OTP)
446 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
448 /* Program an OTP word (16 bits) */
449 *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
451 else
452 #endif /* FLASH_OPTCR_PG_OTP */
454 /* Program the flash word */
457 *dest_addr = *src_addr;
458 dest_addr++;
459 src_addr++;
460 row_index--;
461 } while (row_index != 0U);
464 __ISB();
465 __DSB();
468 return status;
472 * @brief This function handles FLASH interrupt request.
473 * @retval None
475 void HAL_FLASH_IRQHandler(void)
477 uint32_t temp;
478 uint32_t errorflag;
479 FLASH_ProcedureTypeDef procedure;
481 /* Check FLASH Bank1 End of Operation flag */
482 if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP) != RESET)
484 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
486 /* Nb of sector to erased can be decreased */
487 pFlash.NbSectorsToErase--;
489 /* Check if there are still sectors to erase */
490 if(pFlash.NbSectorsToErase != 0U)
492 /* Indicate user which sector has been erased */
493 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
495 /* Clear bank 1 End of Operation pending bit */
496 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
498 /* Increment sector number */
499 pFlash.Sector++;
500 temp = pFlash.Sector;
501 FLASH_Erase_Sector(temp, FLASH_BANK_1, pFlash.VoltageForErase);
503 else
505 /* No more sectors to Erase, user callback can be called */
506 /* Reset Sector and stop Erase sectors procedure */
507 pFlash.Sector = 0xFFFFFFFFU;
508 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
510 /* FLASH EOP interrupt user callback */
511 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
513 /* Clear FLASH End of Operation pending bit */
514 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
517 else
519 procedure = pFlash.ProcedureOnGoing;
521 if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
523 /* MassErase ended. Return the selected bank */
524 /* FLASH EOP interrupt user callback */
525 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1);
527 else if(procedure == FLASH_PROC_PROGRAM_BANK1)
529 /* Program ended. Return the selected address */
530 /* FLASH EOP interrupt user callback */
531 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
533 else
535 /* Nothing to do */
538 if((procedure != FLASH_PROC_SECTERASE_BANK2) && \
539 (procedure != FLASH_PROC_MASSERASE_BANK2) && \
540 (procedure != FLASH_PROC_PROGRAM_BANK2))
542 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
543 /* Clear FLASH End of Operation pending bit */
544 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
549 #if defined (DUAL_BANK)
550 /* Check FLASH Bank2 End of Operation flag */
551 if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP) != RESET)
553 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
555 /*Nb of sector to erased can be decreased*/
556 pFlash.NbSectorsToErase--;
558 /* Check if there are still sectors to erase*/
559 if(pFlash.NbSectorsToErase != 0U)
561 /*Indicate user which sector has been erased*/
562 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
564 /* Clear bank 2 End of Operation pending bit */
565 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
567 /*Increment sector number*/
568 pFlash.Sector++;
569 temp = pFlash.Sector;
570 FLASH_Erase_Sector(temp, FLASH_BANK_2, pFlash.VoltageForErase);
572 else
574 /* No more sectors to Erase, user callback can be called */
575 /* Reset Sector and stop Erase sectors procedure */
576 pFlash.Sector = 0xFFFFFFFFU;
577 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
579 /* FLASH EOP interrupt user callback */
580 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
582 /* Clear FLASH End of Operation pending bit */
583 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
586 else
588 procedure = pFlash.ProcedureOnGoing;
590 if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
592 /*MassErase ended. Return the selected bank*/
593 /* FLASH EOP interrupt user callback */
594 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2);
596 else if(procedure == FLASH_PROC_PROGRAM_BANK2)
598 /* Program ended. Return the selected address */
599 /* FLASH EOP interrupt user callback */
600 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
602 else
604 /* Nothing to do */
607 if((procedure != FLASH_PROC_SECTERASE_BANK1) && \
608 (procedure != FLASH_PROC_MASSERASE_BANK1) && \
609 (procedure != FLASH_PROC_PROGRAM_BANK1))
611 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
612 /* Clear FLASH End of Operation pending bit */
613 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
617 #endif /* DUAL_BANK */
619 /* Check FLASH Bank1 operation error flags */
620 #if defined (FLASH_SR_OPERR)
621 errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
622 FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1);
623 #else
624 errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
625 FLASH_FLAG_INCERR_BANK1);
626 #endif /* FLASH_SR_OPERR */
628 if(errorflag != 0U)
630 /* Save the error code */
631 pFlash.ErrorCode |= errorflag;
633 /* Clear error programming flags */
634 __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
636 procedure = pFlash.ProcedureOnGoing;
638 if(procedure == FLASH_PROC_SECTERASE_BANK1)
640 /* Return the faulty sector */
641 temp = pFlash.Sector;
642 pFlash.Sector = 0xFFFFFFFFU;
644 else if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
646 /* Return the faulty bank */
647 temp = FLASH_BANK_1;
649 else
651 /* Return the faulty address */
652 temp = pFlash.Address;
655 /* Stop the procedure ongoing*/
656 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
658 /* FLASH error interrupt user callback */
659 HAL_FLASH_OperationErrorCallback(temp);
662 #if (USE_FLASH_ECC == 1U)
663 /* Check FLASH Bank1 ECC single correction error flag */
664 errorflag = FLASH->SR1 & FLASH_FLAG_SNECCERR_BANK1;
666 if(errorflag != 0U)
668 /* Save the error code */
669 pFlash.ErrorCode |= errorflag;
671 /* Call User callback */
672 HAL_FLASHEx_EccCorrectionCallback();
674 /* Clear FLASH Bank1 ECC single correction error flag in order to allow new ECC error record */
675 __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
678 /* Check FLASH Bank1 ECC double detection error flag */
679 errorflag = FLASH->SR1 & FLASH_FLAG_DBECCERR_BANK1;
681 if(errorflag != 0U)
683 /* Save the error code */
684 pFlash.ErrorCode |= errorflag;
686 /* Call User callback */
687 HAL_FLASHEx_EccDetectionCallback();
689 /* Clear FLASH Bank1 ECC double detection error flag in order to allow new ECC error record */
690 __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
692 #endif /* USE_FLASH_ECC */
694 #if defined (DUAL_BANK)
695 /* Check FLASH Bank2 operation error flags */
696 #if defined (FLASH_SR_OPERR)
697 errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
698 FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2) & 0x7FFFFFFFU);
699 #else
700 errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
701 FLASH_FLAG_INCERR_BANK2) & 0x7FFFFFFFU);
702 #endif /* FLASH_SR_OPERR */
704 if(errorflag != 0U)
706 /* Save the error code */
707 pFlash.ErrorCode |= (errorflag | 0x80000000U);
709 /* Clear error programming flags */
710 __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
712 procedure = pFlash.ProcedureOnGoing;
714 if(procedure== FLASH_PROC_SECTERASE_BANK2)
716 /*return the faulty sector*/
717 temp = pFlash.Sector;
718 pFlash.Sector = 0xFFFFFFFFU;
720 else if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
722 /*return the faulty bank*/
723 temp = FLASH_BANK_2;
725 else
727 /*return the faulty address*/
728 temp = pFlash.Address;
731 /*Stop the procedure ongoing*/
732 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
734 /* FLASH error interrupt user callback */
735 HAL_FLASH_OperationErrorCallback(temp);
738 #if (USE_FLASH_ECC == 1U)
739 /* Check FLASH Bank2 ECC single correction error flag */
740 errorflag = FLASH->SR2 & FLASH_FLAG_SNECCERR_BANK2;
742 if(errorflag != 0U)
744 /* Save the error code */
745 pFlash.ErrorCode |= (errorflag | 0x80000000U);
747 /* Call User callback */
748 HAL_FLASHEx_EccCorrectionCallback();
750 /* Clear FLASH Bank2 ECC single correction error flag in order to allow new ECC error record */
751 __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
754 /* Check FLASH Bank2 ECC double detection error flag */
755 errorflag = FLASH->SR2 & FLASH_FLAG_DBECCERR_BANK2;
757 if(errorflag != 0U)
759 /* Save the error code */
760 pFlash.ErrorCode |= (errorflag | 0x80000000U);
762 /* Call User callback */
763 HAL_FLASHEx_EccDetectionCallback();
765 /* Clear FLASH Bank2 ECC double detection error flag in order to allow new ECC error record */
766 __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
769 #endif /* USE_FLASH_ECC */
770 #endif /* DUAL_BANK */
772 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
774 #if defined (FLASH_CR_OPERRIE)
775 /* Disable Bank1 Operation and Error source interrupt */
776 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
777 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
779 #if defined (DUAL_BANK)
780 /* Disable Bank2 Operation and Error source interrupt */
781 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
782 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
783 #endif /* DUAL_BANK */
784 #else
785 /* Disable Bank1 Operation and Error source interrupt */
786 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
787 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
789 #if defined (DUAL_BANK)
790 /* Disable Bank2 Operation and Error source interrupt */
791 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
792 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
793 #endif /* DUAL_BANK */
794 #endif /* FLASH_CR_OPERRIE */
796 /* Process Unlocked */
797 __HAL_UNLOCK(&pFlash);
802 * @brief FLASH end of operation interrupt callback
803 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
804 * Mass Erase: Bank number which has been requested to erase
805 * Sectors Erase: Sector which has been erased
806 * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
807 * Program: Address which was selected for data program
808 * @retval None
810 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
812 /* Prevent unused argument(s) compilation warning */
813 UNUSED(ReturnValue);
815 /* NOTE : This function Should not be modified, when the callback is needed,
816 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
821 * @brief FLASH operation error interrupt callback
822 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
823 * Mass Erase: Bank number which has been requested to erase
824 * Sectors Erase: Sector number which returned an error
825 * Program: Address which was selected for data program
826 * @retval None
828 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
830 /* Prevent unused argument(s) compilation warning */
831 UNUSED(ReturnValue);
833 /* NOTE : This function Should not be modified, when the callback is needed,
834 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
839 * @}
842 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
843 * @brief Management functions
845 @verbatim
846 ===============================================================================
847 ##### Peripheral Control functions #####
848 ===============================================================================
849 [..]
850 This subsection provides a set of functions allowing to control the FLASH
851 memory operations.
853 @endverbatim
854 * @{
858 * @brief Unlock the FLASH control registers access
859 * @retval HAL Status
861 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
863 if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
865 /* Authorize the FLASH Bank1 Registers access */
866 WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
867 WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
869 /* Verify Flash Bank1 is unlocked */
870 if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
872 return HAL_ERROR;
876 #if defined (DUAL_BANK)
877 if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
879 /* Authorize the FLASH Bank2 Registers access */
880 WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
881 WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
883 /* Verify Flash Bank2 is unlocked */
884 if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
886 return HAL_ERROR;
889 #endif /* DUAL_BANK */
891 return HAL_OK;
895 * @brief Locks the FLASH control registers access
896 * @retval HAL Status
898 HAL_StatusTypeDef HAL_FLASH_Lock(void)
900 /* Set the LOCK Bit to lock the FLASH Bank1 Control Register access */
901 SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
903 /* Verify Flash Bank1 is locked */
904 if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) == 0U)
906 return HAL_ERROR;
909 #if defined (DUAL_BANK)
910 /* Set the LOCK Bit to lock the FLASH Bank2 Control Register access */
911 SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
913 /* Verify Flash Bank2 is locked */
914 if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) == 0U)
916 return HAL_ERROR;
918 #endif /* DUAL_BANK */
920 return HAL_OK;
924 * @brief Unlock the FLASH Option Control Registers access.
925 * @retval HAL Status
927 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
929 if(READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
931 /* Authorizes the Option Byte registers programming */
932 WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
933 WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
935 /* Verify that the Option Bytes are unlocked */
936 if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
938 return HAL_ERROR;
942 return HAL_OK;
946 * @brief Lock the FLASH Option Control Registers access.
947 * @retval HAL Status
949 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
951 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
952 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
954 /* Verify that the Option Bytes are locked */
955 if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) == 0U)
957 return HAL_ERROR;
960 return HAL_OK;
964 * @brief Launch the option bytes loading.
965 * @retval HAL Status
967 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
969 HAL_StatusTypeDef status;
971 /* Wait for CRC computation to be completed */
972 if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
974 status = HAL_ERROR;
976 #if defined (DUAL_BANK)
977 else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
979 status = HAL_ERROR;
981 #endif /* DUAL_BANK */
982 else
984 status = HAL_OK;
987 if (status == HAL_OK)
989 /* Set OPTSTRT Bit */
990 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
992 /* Wait for OB change operation to be completed */
993 status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
996 return status;
1000 * @}
1003 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
1004 * @brief Peripheral Errors functions
1006 @verbatim
1007 ===============================================================================
1008 ##### Peripheral Errors functions #####
1009 ===============================================================================
1010 [..]
1011 This subsection permits to get in run-time Errors of the FLASH peripheral.
1013 @endverbatim
1014 * @{
1018 * @brief Get the specific FLASH error flag.
1019 * @retval HAL_FLASH_ERRORCode The returned value can be:
1020 * @arg HAL_FLASH_ERROR_NONE : No error set
1022 * @arg HAL_FLASH_ERROR_WRP_BANK1 : Write Protection Error on Bank 1
1023 * @arg HAL_FLASH_ERROR_PGS_BANK1 : Program Sequence Error on Bank 1
1024 * @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
1025 * @arg HAL_FLASH_ERROR_INC_BANK1 : Inconsistency Error on Bank 1
1026 * @arg HAL_FLASH_ERROR_OPE_BANK1 : Operation Error on Bank 1
1027 * @arg HAL_FLASH_ERROR_RDP_BANK1 : Read Protection Error on Bank 1
1028 * @arg HAL_FLASH_ERROR_RDS_BANK1 : Read Secured Error on Bank 1
1029 * @arg HAL_FLASH_ERROR_SNECC_BANK1: ECC Single Correction Error on Bank 1
1030 * @arg HAL_FLASH_ERROR_DBECC_BANK1: ECC Double Detection Error on Bank 1
1031 * @arg HAL_FLASH_ERROR_CRCRD_BANK1: CRC Read Error on Bank 1
1033 * @arg HAL_FLASH_ERROR_WRP_BANK2 : Write Protection Error on Bank 2
1034 * @arg HAL_FLASH_ERROR_PGS_BANK2 : Program Sequence Error on Bank 2
1035 * @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
1036 * @arg HAL_FLASH_ERROR_INC_BANK2 : Inconsistency Error on Bank 2
1037 * @arg HAL_FLASH_ERROR_OPE_BANK2 : Operation Error on Bank 2
1038 * @arg HAL_FLASH_ERROR_RDP_BANK2 : Read Protection Error on Bank 2
1039 * @arg HAL_FLASH_ERROR_RDS_BANK2 : Read Secured Error on Bank 2
1040 * @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
1041 * @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
1042 * @arg HAL_FLASH_ERROR_CRCRD_BANK2: CRC Read Error on Bank 2
1045 uint32_t HAL_FLASH_GetError(void)
1047 return pFlash.ErrorCode;
1051 * @}
1055 * @}
1058 /* Private functions ---------------------------------------------------------*/
1060 /** @addtogroup FLASH_Private_Functions
1061 * @{
1065 * @brief Wait for a FLASH operation to complete.
1066 * @param Timeout maximum flash operation timeout
1067 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
1068 * @retval HAL_StatusTypeDef HAL Status
1070 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
1072 /* Wait for the FLASH operation to complete by polling on QW flag to be reset.
1073 Even if the FLASH operation fails, the QW flag will be reset and an error
1074 flag will be set */
1076 uint32_t bsyflag = FLASH_FLAG_QW_BANK1;
1077 uint32_t errorflag = 0;
1078 uint32_t tickstart = HAL_GetTick();
1080 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1082 #if defined (DUAL_BANK)
1084 if (Bank == FLASH_BANK_2)
1086 /* Select bsyflag depending on Bank */
1087 bsyflag = FLASH_FLAG_QW_BANK2;
1089 #endif /* DUAL_BANK */
1091 while(__HAL_FLASH_GET_FLAG(bsyflag))
1093 if(Timeout != HAL_MAX_DELAY)
1095 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1097 return HAL_TIMEOUT;
1102 /* Get Error Flags */
1103 if (Bank == FLASH_BANK_1)
1105 errorflag = FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1;
1107 #if defined (DUAL_BANK)
1108 else
1110 errorflag = (FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2) | 0x80000000U;
1112 #endif /* DUAL_BANK */
1114 /* In case of error reported in Flash SR1 or SR2 register */
1115 if((errorflag & 0x7FFFFFFFU) != 0U)
1117 /*Save the error code*/
1118 pFlash.ErrorCode |= errorflag;
1120 /* Clear error programming flags */
1121 __HAL_FLASH_CLEAR_FLAG(errorflag);
1123 return HAL_ERROR;
1126 /* Check FLASH End of Operation flag */
1127 if(Bank == FLASH_BANK_1)
1129 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
1131 /* Clear FLASH End of Operation pending bit */
1132 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
1135 #if defined (DUAL_BANK)
1136 else
1138 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
1140 /* Clear FLASH End of Operation pending bit */
1141 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
1144 #endif /* DUAL_BANK */
1146 return HAL_OK;
1150 * @brief Wait for a FLASH Option Bytes change operation to complete.
1151 * @param Timeout maximum flash operation timeout
1152 * @retval HAL_StatusTypeDef HAL Status
1154 HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
1156 /* Get timeout */
1157 uint32_t tickstart = HAL_GetTick();
1159 /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset */
1160 while(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPT_BUSY) != 0U)
1162 if(Timeout != HAL_MAX_DELAY)
1164 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1166 return HAL_TIMEOUT;
1171 /* Check option byte change error */
1172 if(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPTCHANGEERR) != 0U)
1174 /* Save the error code */
1175 pFlash.ErrorCode |= HAL_FLASH_ERROR_OB_CHANGE;
1177 /* Clear the OB error flag */
1178 FLASH->OPTCCR |= FLASH_OPTCCR_CLR_OPTCHANGEERR;
1180 return HAL_ERROR;
1183 /* If there is no error flag set */
1184 return HAL_OK;
1188 * @brief Wait for a FLASH CRC computation to complete.
1189 * @param Timeout maximum flash operation timeout
1190 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
1191 * @retval HAL_StatusTypeDef HAL Status
1193 HAL_StatusTypeDef FLASH_CRC_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
1195 uint32_t bsyflag;
1196 uint32_t tickstart = HAL_GetTick();
1198 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1200 /* Select bsyflag depending on Bank */
1201 if(Bank == FLASH_BANK_1)
1203 bsyflag = FLASH_FLAG_CRC_BUSY_BANK1;
1205 else
1207 bsyflag = FLASH_FLAG_CRC_BUSY_BANK2;
1210 /* Wait for the FLASH CRC computation to complete by polling on CRC_BUSY flag to be reset */
1211 while(__HAL_FLASH_GET_FLAG(bsyflag))
1213 if(Timeout != HAL_MAX_DELAY)
1215 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1217 return HAL_TIMEOUT;
1222 /* Check FLASH CRC read error flag */
1223 if(Bank == FLASH_BANK_1)
1225 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1))
1227 /* Save the error code */
1228 pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK1;
1230 /* Clear FLASH CRC read error pending bit */
1231 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1);
1233 return HAL_ERROR;
1236 #if defined (DUAL_BANK)
1237 else
1239 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2))
1241 /* Save the error code */
1242 pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK2;
1244 /* Clear FLASH CRC read error pending bit */
1245 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2);
1247 return HAL_ERROR;
1250 #endif /* DUAL_BANK */
1252 /* If there is no error flag set */
1253 return HAL_OK;
1257 * @}
1260 #endif /* HAL_FLASH_MODULE_ENABLED */
1263 * @}
1267 * @}