Create release.yml
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_flash.c
bloba336de4df59dd8c810f1862895a252e0b3f3741e
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 * <h2><center>&copy; 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
92 * @{
95 /** @defgroup FLASH FLASH
96 * @brief FLASH HAL module driver
97 * @{
100 #ifdef HAL_FLASH_MODULE_ENABLED
102 /* Private typedef -----------------------------------------------------------*/
103 /* Private define ------------------------------------------------------------*/
104 /** @addtogroup FLASH_Private_Constants
105 * @{
107 #define FLASH_TIMEOUT_VALUE 50000U /* 50 s */
109 * @}
111 /* Private macro -------------------------------------------------------------*/
112 /* Private variables ---------------------------------------------------------*/
113 FLASH_ProcessTypeDef pFlash;
114 /* Private function prototypes -----------------------------------------------*/
115 /* Exported functions ---------------------------------------------------------*/
117 /** @defgroup FLASH_Exported_Functions FLASH Exported functions
118 * @{
121 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
122 * @brief Programming operation functions
124 @verbatim
125 ===============================================================================
126 ##### Programming operation functions #####
127 ===============================================================================
128 [..]
129 This subsection provides a set of functions allowing to manage the FLASH
130 program operations.
132 @endverbatim
133 * @{
137 * @brief Program flash word at a specified address
138 * @param TypeProgram Indicate the way to program at a specified address.
139 * This parameter can be a value of @ref FLASH_Type_Program
140 * @param FlashAddress specifies the address to be programmed.
141 * @param DataAddress specifies the address of data to be programmed
143 * @retval HAL_StatusTypeDef HAL Status
145 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
147 #if !defined(FLASH_OPTCR_PG_OTP)
148 UNUSED(TypeProgram);
149 #endif
151 HAL_StatusTypeDef status;
152 __IO uint32_t *dest_addr = (__IO uint32_t *)FlashAddress;
153 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
154 uint32_t bank;
155 uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
157 /* Check the parameters */
158 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
159 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
161 /* Process Locked */
162 __HAL_LOCK(&pFlash);
164 #if defined (FLASH_OPTCR_PG_OTP)
165 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
166 #else
167 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
168 #endif /* FLASH_OPTCR_PG_OTP */
170 bank = FLASH_BANK_1;
172 #if defined (DUAL_BANK)
173 else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
175 bank = FLASH_BANK_2;
177 #endif /* DUAL_BANK */
178 else
180 return HAL_ERROR;
183 /* Reset error code */
184 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
186 /* Wait for last operation to be completed */
187 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
189 if(status == HAL_OK)
191 #if defined (DUAL_BANK)
192 if(bank == FLASH_BANK_1)
194 #if defined (FLASH_OPTCR_PG_OTP)
195 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
197 /* Set OTP_PG bit */
198 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
200 else
201 #endif /* FLASH_OPTCR_PG_OTP */
203 /* Set PG bit */
204 SET_BIT(FLASH->CR1, FLASH_CR_PG);
207 else
209 /* Set PG bit */
210 SET_BIT(FLASH->CR2, FLASH_CR_PG);
212 #else /* Single Bank */
213 #if defined (FLASH_OPTCR_PG_OTP)
214 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
216 /* Set OTP_PG bit */
217 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
219 else
220 #endif /* FLASH_OPTCR_PG_OTP */
222 /* Set PG bit */
223 SET_BIT(FLASH->CR1, FLASH_CR_PG);
225 #endif /* DUAL_BANK */
227 __ISB();
228 __DSB();
230 #if defined (FLASH_OPTCR_PG_OTP)
231 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
233 /* Program an OTP word (16 bits) */
234 *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
236 else
237 #endif /* FLASH_OPTCR_PG_OTP */
239 /* Program the flash word */
242 *dest_addr = *src_addr;
243 dest_addr++;
244 src_addr++;
245 row_index--;
246 } while (row_index != 0U);
249 __ISB();
250 __DSB();
252 /* Wait for last operation to be completed */
253 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
255 #if defined (DUAL_BANK)
256 #if defined (FLASH_OPTCR_PG_OTP)
257 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
259 /* If the program operation is completed, disable the OTP_PG */
260 CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
262 else
263 #endif /* FLASH_OPTCR_PG_OTP */
265 if(bank == FLASH_BANK_1)
267 /* If the program operation is completed, disable the PG */
268 CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
270 else
272 /* If the program operation is completed, disable the PG */
273 CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
276 #else /* Single Bank */
277 #if defined (FLASH_OPTCR_PG_OTP)
278 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
280 /* If the program operation is completed, disable the OTP_PG */
281 CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
283 else
284 #endif /* FLASH_OPTCR_PG_OTP */
286 /* If the program operation is completed, disable the PG */
287 CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
289 #endif /* DUAL_BANK */
292 /* Process Unlocked */
293 __HAL_UNLOCK(&pFlash);
295 return status;
299 * @brief Program flash words of 256 bits at a specified address with interrupt enabled.
300 * @param TypeProgram Indicate the way to program at a specified address.
301 * This parameter can be a value of @ref FLASH_Type_Program
302 * @param FlashAddress specifies the address to be programmed.
303 * @param DataAddress specifies the address of data (256 bits) to be programmed
305 * @retval HAL Status
307 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
309 #if !defined(FLASH_OPTCR_PG_OTP)
310 UNUSED(TypeProgram);
311 #endif
313 HAL_StatusTypeDef status;
314 __IO uint32_t *dest_addr = (__IO uint32_t*)FlashAddress;
315 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
316 uint32_t bank;
317 uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
319 /* Check the parameters */
320 assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
321 assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
323 /* Process Locked */
324 __HAL_LOCK(&pFlash);
326 /* Reset error code */
327 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
329 #if defined (FLASH_OPTCR_PG_OTP)
330 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
331 #else
332 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
333 #endif /* FLASH_OPTCR_PG_OTP */
335 bank = FLASH_BANK_1;
337 #if defined (DUAL_BANK)
338 else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
340 bank = FLASH_BANK_2;
342 #endif /* DUAL_BANK */
343 else
345 return HAL_ERROR;
348 /* Wait for last operation to be completed */
349 status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
351 if (status != HAL_OK)
353 /* Process Unlocked */
354 __HAL_UNLOCK(&pFlash);
356 else
358 pFlash.Address = FlashAddress;
360 #if defined (DUAL_BANK)
361 if(bank == FLASH_BANK_1)
363 /* Set internal variables used by the IRQ handler */
364 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
366 #if defined (FLASH_OPTCR_PG_OTP)
367 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
369 /* Set OTP_PG bit */
370 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
372 else
373 #endif /* FLASH_OPTCR_PG_OTP */
375 /* Set PG bit */
376 SET_BIT(FLASH->CR1, FLASH_CR_PG);
379 /* Enable End of Operation and Error interrupts for Bank 1 */
380 #if defined (FLASH_CR_OPERRIE)
381 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
382 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
383 #else
384 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
385 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
386 #endif /* FLASH_CR_OPERRIE */
388 else
390 /* Set internal variables used by the IRQ handler */
391 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
393 /* Set PG bit */
394 SET_BIT(FLASH->CR2, FLASH_CR_PG);
396 /* Enable End of Operation and Error interrupts for Bank2 */
397 #if defined (FLASH_CR_OPERRIE)
398 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
399 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
400 #else
401 __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
402 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
403 #endif /* FLASH_CR_OPERRIE */
405 #else /* Single Bank */
406 /* Set internal variables used by the IRQ handler */
407 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
409 #if defined (FLASH_OPTCR_PG_OTP)
410 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
412 /* Set OTP_PG bit */
413 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
415 else
416 #endif /* FLASH_OPTCR_PG_OTP */
418 /* Set PG bit */
419 SET_BIT(FLASH->CR1, FLASH_CR_PG);
422 /* Enable End of Operation and Error interrupts for Bank 1 */
423 #if defined (FLASH_CR_OPERRIE)
424 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
425 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
426 #else
427 __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
428 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
429 #endif /* FLASH_CR_OPERRIE */
430 #endif /* DUAL_BANK */
432 __ISB();
433 __DSB();
435 #if defined (FLASH_OPTCR_PG_OTP)
436 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
438 /* Program an OTP word (16 bits) */
439 *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
441 else
442 #endif /* FLASH_OPTCR_PG_OTP */
444 /* Program the flash word */
447 *dest_addr = *src_addr;
448 dest_addr++;
449 src_addr++;
450 row_index--;
451 } while (row_index != 0U);
454 __ISB();
455 __DSB();
458 return status;
462 * @brief This function handles FLASH interrupt request.
463 * @retval None
465 void HAL_FLASH_IRQHandler(void)
467 uint32_t temp;
468 uint32_t errorflag;
469 FLASH_ProcedureTypeDef procedure;
471 /* Check FLASH Bank1 End of Operation flag */
472 if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP) != RESET)
474 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
476 /* Nb of sector to erased can be decreased */
477 pFlash.NbSectorsToErase--;
479 /* Check if there are still sectors to erase */
480 if(pFlash.NbSectorsToErase != 0U)
482 /* Indicate user which sector has been erased */
483 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
485 /* Clear bank 1 End of Operation pending bit */
486 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
488 /* Increment sector number */
489 pFlash.Sector++;
490 temp = pFlash.Sector;
491 FLASH_Erase_Sector(temp, FLASH_BANK_1, pFlash.VoltageForErase);
493 else
495 /* No more sectors to Erase, user callback can be called */
496 /* Reset Sector and stop Erase sectors procedure */
497 pFlash.Sector = 0xFFFFFFFFU;
498 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
500 /* FLASH EOP interrupt user callback */
501 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
503 /* Clear FLASH End of Operation pending bit */
504 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
507 else
509 procedure = pFlash.ProcedureOnGoing;
511 if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
513 /* MassErase ended. Return the selected bank */
514 /* FLASH EOP interrupt user callback */
515 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1);
517 else if(procedure == FLASH_PROC_PROGRAM_BANK1)
519 /* Program ended. Return the selected address */
520 /* FLASH EOP interrupt user callback */
521 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
523 else
525 /* Nothing to do */
528 if((procedure != FLASH_PROC_SECTERASE_BANK2) && \
529 (procedure != FLASH_PROC_MASSERASE_BANK2) && \
530 (procedure != FLASH_PROC_PROGRAM_BANK2))
532 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
533 /* Clear FLASH End of Operation pending bit */
534 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
539 #if defined (DUAL_BANK)
540 /* Check FLASH Bank2 End of Operation flag */
541 if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP) != RESET)
543 if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
545 /*Nb of sector to erased can be decreased*/
546 pFlash.NbSectorsToErase--;
548 /* Check if there are still sectors to erase*/
549 if(pFlash.NbSectorsToErase != 0U)
551 /*Indicate user which sector has been erased*/
552 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
554 /* Clear bank 2 End of Operation pending bit */
555 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
557 /*Increment sector number*/
558 pFlash.Sector++;
559 temp = pFlash.Sector;
560 FLASH_Erase_Sector(temp, FLASH_BANK_2, pFlash.VoltageForErase);
562 else
564 /* No more sectors to Erase, user callback can be called */
565 /* Reset Sector and stop Erase sectors procedure */
566 pFlash.Sector = 0xFFFFFFFFU;
567 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
569 /* FLASH EOP interrupt user callback */
570 HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
572 /* Clear FLASH End of Operation pending bit */
573 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
576 else
578 procedure = pFlash.ProcedureOnGoing;
580 if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
582 /*MassErase ended. Return the selected bank*/
583 /* FLASH EOP interrupt user callback */
584 HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2);
586 else if(procedure == FLASH_PROC_PROGRAM_BANK2)
588 /* Program ended. Return the selected address */
589 /* FLASH EOP interrupt user callback */
590 HAL_FLASH_EndOfOperationCallback(pFlash.Address);
592 else
594 /* Nothing to do */
597 if((procedure != FLASH_PROC_SECTERASE_BANK1) && \
598 (procedure != FLASH_PROC_MASSERASE_BANK1) && \
599 (procedure != FLASH_PROC_PROGRAM_BANK1))
601 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
602 /* Clear FLASH End of Operation pending bit */
603 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
607 #endif /* DUAL_BANK */
609 /* Check FLASH Bank1 operation error flags */
610 #if defined (FLASH_SR_OPERR)
611 errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
612 FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1);
613 #else
614 errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
615 FLASH_FLAG_INCERR_BANK1);
616 #endif /* FLASH_SR_OPERR */
618 if(errorflag != 0U)
620 /* Save the error code */
621 pFlash.ErrorCode |= errorflag;
623 /* Clear error programming flags */
624 __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
626 procedure = pFlash.ProcedureOnGoing;
628 if(procedure == FLASH_PROC_SECTERASE_BANK1)
630 /* Return the faulty sector */
631 temp = pFlash.Sector;
632 pFlash.Sector = 0xFFFFFFFFU;
634 else if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
636 /* Return the faulty bank */
637 temp = FLASH_BANK_1;
639 else
641 /* Return the faulty address */
642 temp = pFlash.Address;
645 /* Stop the procedure ongoing*/
646 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
648 /* FLASH error interrupt user callback */
649 HAL_FLASH_OperationErrorCallback(temp);
652 #if defined (DUAL_BANK)
653 /* Check FLASH Bank2 operation error flags */
654 #if defined (FLASH_SR_OPERR)
655 errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
656 FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2) & 0x7FFFFFFFU);
657 #else
658 errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
659 FLASH_FLAG_INCERR_BANK2) & 0x7FFFFFFFU);
660 #endif /* FLASH_SR_OPERR */
662 if(errorflag != 0U)
664 /* Save the error code */
665 pFlash.ErrorCode |= (errorflag | 0x80000000U);
667 /* Clear error programming flags */
668 __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
670 procedure = pFlash.ProcedureOnGoing;
672 if(procedure== FLASH_PROC_SECTERASE_BANK2)
674 /*return the faulty sector*/
675 temp = pFlash.Sector;
676 pFlash.Sector = 0xFFFFFFFFU;
678 else if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
680 /*return the faulty bank*/
681 temp = FLASH_BANK_2;
683 else
685 /*return the faulty address*/
686 temp = pFlash.Address;
689 /*Stop the procedure ongoing*/
690 pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
692 /* FLASH error interrupt user callback */
693 HAL_FLASH_OperationErrorCallback(temp);
695 #endif /* DUAL_BANK */
697 if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
699 #if defined (FLASH_CR_OPERRIE)
700 /* Disable Bank1 Operation and Error source interrupt */
701 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
702 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
704 #if defined (DUAL_BANK)
705 /* Disable Bank2 Operation and Error source interrupt */
706 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
707 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
708 #endif /* DUAL_BANK */
709 #else
710 /* Disable Bank1 Operation and Error source interrupt */
711 __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1 | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
712 FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
714 #if defined (DUAL_BANK)
715 /* Disable Bank2 Operation and Error source interrupt */
716 __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2 | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
717 FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
718 #endif /* DUAL_BANK */
719 #endif /* FLASH_CR_OPERRIE */
721 /* Process Unlocked */
722 __HAL_UNLOCK(&pFlash);
727 * @brief FLASH end of operation interrupt callback
728 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
729 * Mass Erase: Bank number which has been requested to erase
730 * Sectors Erase: Sector which has been erased
731 * (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
732 * Program: Address which was selected for data program
733 * @retval None
735 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
737 /* Prevent unused argument(s) compilation warning */
738 UNUSED(ReturnValue);
740 /* NOTE : This function Should not be modified, when the callback is needed,
741 the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
746 * @brief FLASH operation error interrupt callback
747 * @param ReturnValue The value saved in this parameter depends on the ongoing procedure
748 * Mass Erase: Bank number which has been requested to erase
749 * Sectors Erase: Sector number which returned an error
750 * Program: Address which was selected for data program
751 * @retval None
753 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
755 /* Prevent unused argument(s) compilation warning */
756 UNUSED(ReturnValue);
758 /* NOTE : This function Should not be modified, when the callback is needed,
759 the HAL_FLASH_OperationErrorCallback could be implemented in the user file
764 * @}
767 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
768 * @brief Management functions
770 @verbatim
771 ===============================================================================
772 ##### Peripheral Control functions #####
773 ===============================================================================
774 [..]
775 This subsection provides a set of functions allowing to control the FLASH
776 memory operations.
778 @endverbatim
779 * @{
783 * @brief Unlock the FLASH control registers access
784 * @retval HAL Status
786 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
788 if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
790 /* Authorize the FLASH Bank1 Registers access */
791 WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
792 WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
794 /* Verify Flash Bank1 is unlocked */
795 if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
797 return HAL_ERROR;
801 #if defined (DUAL_BANK)
802 if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
804 /* Authorize the FLASH Bank2 Registers access */
805 WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
806 WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
808 /* Verify Flash Bank2 is unlocked */
809 if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
811 return HAL_ERROR;
814 #endif /* DUAL_BANK */
816 return HAL_OK;
820 * @brief Locks the FLASH control registers access
821 * @retval HAL Status
823 HAL_StatusTypeDef HAL_FLASH_Lock(void)
825 /* Set the LOCK Bit to lock the FLASH Bank1 Control Register access */
826 SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
828 /* Verify Flash Bank1 is locked */
829 if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) == 0U)
831 return HAL_ERROR;
834 #if defined (DUAL_BANK)
835 /* Set the LOCK Bit to lock the FLASH Bank2 Control Register access */
836 SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
838 /* Verify Flash Bank2 is locked */
839 if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) == 0U)
841 return HAL_ERROR;
843 #endif /* DUAL_BANK */
845 return HAL_OK;
849 * @brief Unlock the FLASH Option Control Registers access.
850 * @retval HAL Status
852 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
854 if(READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
856 /* Authorizes the Option Byte registers programming */
857 WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
858 WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
860 /* Verify that the Option Bytes are unlocked */
861 if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
863 return HAL_ERROR;
867 return HAL_OK;
871 * @brief Lock the FLASH Option Control Registers access.
872 * @retval HAL Status
874 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
876 /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
877 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
879 /* Verify that the Option Bytes are locked */
880 if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) == 0U)
882 return HAL_ERROR;
885 return HAL_OK;
889 * @brief Launch the option bytes loading.
890 * @retval HAL Status
892 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
894 HAL_StatusTypeDef status;
896 /* Wait for CRC computation to be completed */
897 if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
899 status = HAL_ERROR;
901 #if defined (DUAL_BANK)
902 else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
904 status = HAL_ERROR;
906 #endif /* DUAL_BANK */
907 else
909 status = HAL_OK;
912 if (status == HAL_OK)
914 /* Set OPTSTRT Bit */
915 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
917 /* Wait for OB change operation to be completed */
918 status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
921 return status;
925 * @}
928 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
929 * @brief Peripheral Errors functions
931 @verbatim
932 ===============================================================================
933 ##### Peripheral Errors functions #####
934 ===============================================================================
935 [..]
936 This subsection permits to get in run-time Errors of the FLASH peripheral.
938 @endverbatim
939 * @{
943 * @brief Get the specific FLASH error flag.
944 * @retval HAL_FLASH_ERRORCode The returned value can be:
945 * @arg HAL_FLASH_ERROR_NONE : No error set
947 * @arg HAL_FLASH_ERROR_WRP_BANK1 : Write Protection Error on Bank 1
948 * @arg HAL_FLASH_ERROR_PGS_BANK1 : Program Sequence Error on Bank 1
949 * @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
950 * @arg HAL_FLASH_ERROR_INC_BANK1 : Inconsistency Error on Bank 1
951 * @arg HAL_FLASH_ERROR_OPE_BANK1 : Operation Error on Bank 1
952 * @arg HAL_FLASH_ERROR_RDP_BANK1 : Read Protection Error on Bank 1
953 * @arg HAL_FLASH_ERROR_RDS_BANK1 : Read Secured Error on Bank 1
954 * @arg HAL_FLASH_ERROR_SNECC_BANK1: ECC Single Correction Error on Bank 1
955 * @arg HAL_FLASH_ERROR_DBECC_BANK1: ECC Double Detection Error on Bank 1
956 * @arg HAL_FLASH_ERROR_CRCRD_BANK1: CRC Read Error on Bank 1
958 * @arg HAL_FLASH_ERROR_WRP_BANK2 : Write Protection Error on Bank 2
959 * @arg HAL_FLASH_ERROR_PGS_BANK2 : Program Sequence Error on Bank 2
960 * @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
961 * @arg HAL_FLASH_ERROR_INC_BANK2 : Inconsistency Error on Bank 2
962 * @arg HAL_FLASH_ERROR_OPE_BANK2 : Operation Error on Bank 2
963 * @arg HAL_FLASH_ERROR_RDP_BANK2 : Read Protection Error on Bank 2
964 * @arg HAL_FLASH_ERROR_RDS_BANK2 : Read Secured Error on Bank 2
965 * @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
966 * @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
967 * @arg HAL_FLASH_ERROR_CRCRD_BANK2: CRC Read Error on Bank 2
970 uint32_t HAL_FLASH_GetError(void)
972 return pFlash.ErrorCode;
976 * @}
980 * @}
983 /* Private functions ---------------------------------------------------------*/
985 /** @addtogroup FLASH_Private_Functions
986 * @{
990 * @brief Wait for a FLASH operation to complete.
991 * @param Timeout maximum flash operation timeout
992 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
993 * @retval HAL_StatusTypeDef HAL Status
995 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
997 /* Wait for the FLASH operation to complete by polling on QW flag to be reset.
998 Even if the FLASH operation fails, the QW flag will be reset and an error
999 flag will be set */
1001 uint32_t bsyflag = FLASH_FLAG_QW_BANK1;
1002 uint32_t errorflag = FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1;
1003 uint32_t tickstart = HAL_GetTick();
1005 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1007 #if defined (DUAL_BANK)
1009 if (Bank == FLASH_BANK_2)
1011 /* Get Error Flags */
1012 errorflag = (FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2) | 0x80000000U;
1013 /* Select bsyflag depending on Bank */
1014 bsyflag = FLASH_FLAG_QW_BANK2;
1016 #endif /* DUAL_BANK */
1018 while(__HAL_FLASH_GET_FLAG(bsyflag))
1020 if(Timeout != HAL_MAX_DELAY)
1022 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1024 return HAL_TIMEOUT;
1029 /* In case of error reported in Flash SR1 or SR2 register */
1030 if((errorflag & 0x7FFFFFFFU) != 0U)
1032 /*Save the error code*/
1033 pFlash.ErrorCode |= errorflag;
1035 /* Clear error programming flags */
1036 __HAL_FLASH_CLEAR_FLAG(errorflag);
1038 return HAL_ERROR;
1041 /* Check FLASH End of Operation flag */
1042 if(Bank == FLASH_BANK_1)
1044 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
1046 /* Clear FLASH End of Operation pending bit */
1047 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
1050 #if defined (DUAL_BANK)
1051 else
1053 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
1055 /* Clear FLASH End of Operation pending bit */
1056 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
1059 #endif /* DUAL_BANK */
1061 return HAL_OK;
1065 * @brief Wait for a FLASH Option Bytes change operation to complete.
1066 * @param Timeout maximum flash operation timeout
1067 * @retval HAL_StatusTypeDef HAL Status
1069 HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
1071 /* Get timeout */
1072 uint32_t tickstart = HAL_GetTick();
1074 /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset */
1075 while(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPT_BUSY) != 0U)
1077 if(Timeout != HAL_MAX_DELAY)
1079 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1081 return HAL_TIMEOUT;
1086 /* Check option byte change error */
1087 if(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPTCHANGEERR) != 0U)
1089 /* Save the error code */
1090 pFlash.ErrorCode |= HAL_FLASH_ERROR_OB_CHANGE;
1092 /* Clear the OB error flag */
1093 FLASH->OPTCCR |= FLASH_OPTCCR_CLR_OPTCHANGEERR;
1095 return HAL_ERROR;
1098 /* If there is no error flag set */
1099 return HAL_OK;
1103 * @brief Wait for a FLASH CRC computation to complete.
1104 * @param Timeout maximum flash operation timeout
1105 * @param Bank flash FLASH_BANK_1 or FLASH_BANK_2
1106 * @retval HAL_StatusTypeDef HAL Status
1108 HAL_StatusTypeDef FLASH_CRC_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
1110 uint32_t bsyflag;
1111 uint32_t tickstart = HAL_GetTick();
1113 assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1115 /* Select bsyflag depending on Bank */
1116 if(Bank == FLASH_BANK_1)
1118 bsyflag = FLASH_FLAG_CRC_BUSY_BANK1;
1120 else
1122 bsyflag = FLASH_FLAG_CRC_BUSY_BANK2;
1125 /* Wait for the FLASH CRC computation to complete by polling on CRC_BUSY flag to be reset */
1126 while(__HAL_FLASH_GET_FLAG(bsyflag))
1128 if(Timeout != HAL_MAX_DELAY)
1130 if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1132 return HAL_TIMEOUT;
1137 /* Check FLASH CRC read error flag */
1138 if(Bank == FLASH_BANK_1)
1140 if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1))
1142 /* Save the error code */
1143 pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK1;
1145 /* Clear FLASH CRC read error pending bit */
1146 __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1);
1148 return HAL_ERROR;
1151 #if defined (DUAL_BANK)
1152 else
1154 if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2))
1156 /* Save the error code */
1157 pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK2;
1159 /* Clear FLASH CRC read error pending bit */
1160 __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2);
1162 return HAL_ERROR;
1165 #endif /* DUAL_BANK */
1167 /* If there is no error flag set */
1168 return HAL_OK;
1172 * @}
1175 #endif /* HAL_FLASH_MODULE_ENABLED */
1178 * @}
1182 * @}
1185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/