Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_flash.c
blobc6b7be8921997f698aacfcefd789ca412592b541
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 */
110 * @}
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
119 * @{
122 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
123 * @brief Programming operation functions
125 @verbatim
126 ===============================================================================
127 ##### Programming operation functions #####
128 ===============================================================================
129 [..]
130 This subsection provides a set of functions allowing to manage the FLASH
131 program operations.
133 @endverbatim
134 * @{
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)
148 UNUSED(TypeProgram);
150 HAL_StatusTypeDef status;
151 __IO uint32_t *dest_addr = (__IO uint32_t *)FlashAddress;
152 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
153 uint32_t bank;
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));
160 /* Process Locked */
161 __HAL_LOCK(&pFlash);
163 #if defined (FLASH_OPTCR_PG_OTP)
164 if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
165 #else
166 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
167 #endif /* FLASH_OPTCR_PG_OTP */
169 bank = FLASH_BANK_1;
171 else
173 bank = FLASH_BANK_2;
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);
182 if(status == HAL_OK)
184 if(bank == FLASH_BANK_1)
186 #if defined (FLASH_OPTCR_PG_OTP)
187 if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
189 /* Set OTP_PG bit */
190 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
192 else
193 #endif /* FLASH_OPTCR_PG_OTP */
195 /* Set PG bit */
196 SET_BIT(FLASH->CR1, FLASH_CR_PG);
199 else
201 /* Set PG bit */
202 SET_BIT(FLASH->CR2, FLASH_CR_PG);
205 __ISB();
206 __DSB();
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;
214 else
215 #endif /* FLASH_OPTCR_PG_OTP */
217 /* Program the flash word */
220 *dest_addr = *src_addr;
221 dest_addr++;
222 src_addr++;
223 row_index--;
224 } while (row_index != 0U);
227 __ISB();
228 __DSB();
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);
239 else
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);
247 else
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);
258 return status;
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
268 * @retval HAL Status
270 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
272 UNUSED(TypeProgram);
274 HAL_StatusTypeDef status;
275 __IO uint32_t *dest_addr = (__IO uint32_t*)FlashAddress;
276 __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
277 uint32_t bank;
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));
284 /* Process Locked */
285 __HAL_LOCK(&pFlash);
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)))
292 #else
293 if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
294 #endif /* FLASH_OPTCR_PG_OTP */
296 bank = FLASH_BANK_1;
298 else
300 bank = FLASH_BANK_2;
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);
311 else
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)
323 /* Set OTP_PG bit */
324 SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
326 else
327 #endif /* FLASH_OPTCR_PG_OTP */
329 /* Set PG bit */
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);
337 #else
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 */
342 else
344 /* Set internal variables used by the IRQ handler */
345 pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
347 /* Set PG bit */
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);
354 #else
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 */
360 __ISB();
361 __DSB();
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;
369 else
370 #endif /* FLASH_OPTCR_PG_OTP */
372 /* Program the flash word */
375 *dest_addr = *src_addr;
376 dest_addr++;
377 src_addr++;
378 row_index--;
379 } while (row_index != 0U);
382 __ISB();
383 __DSB();
386 return status;
390 * @brief This function handles FLASH interrupt request.
391 * @retval None
393 void HAL_FLASH_IRQHandler(void)
395 uint32_t temp;
396 uint32_t errorflag;
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 */
417 pFlash.Sector++;
418 temp = pFlash.Sector;
419 FLASH_Erase_Sector(temp, FLASH_BANK_1, pFlash.VoltageForErase);
421 else
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);
435 else
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);
451 else
453 /* Nothing to do */
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*/
485 pFlash.Sector++;
486 temp = pFlash.Sector;
487 FLASH_Erase_Sector(temp, FLASH_BANK_2, pFlash.VoltageForErase);
489 else
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);
503 else
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);
519 else
521 /* Nothing to do */
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);
539 #else
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 */
544 if(errorflag != 0U)
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 */
563 temp = FLASH_BANK_1;
565 else
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);
582 #else
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 */
587 if(errorflag != 0U)
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*/
606 temp = FLASH_BANK_2;
608 else
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);
631 #else
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
653 * @retval None
655 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
657 /* Prevent unused argument(s) compilation warning */
658 UNUSED(ReturnValue);
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
671 * @retval None
673 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
675 /* Prevent unused argument(s) compilation warning */
676 UNUSED(ReturnValue);
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
684 * @}
687 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
688 * @brief Management functions
690 @verbatim
691 ===============================================================================
692 ##### Peripheral Control functions #####
693 ===============================================================================
694 [..]
695 This subsection provides a set of functions allowing to control the FLASH
696 memory operations.
698 @endverbatim
699 * @{
703 * @brief Unlock the FLASH control registers access
704 * @retval HAL Status
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)
717 return HAL_ERROR;
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)
730 return HAL_ERROR;
734 return HAL_OK;
738 * @brief Locks the FLASH control registers access
739 * @retval HAL Status
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)
749 return HAL_ERROR;
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)
758 return HAL_ERROR;
761 return HAL_OK;
765 * @brief Unlock the FLASH Option Control Registers access.
766 * @retval HAL Status
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)
779 return HAL_ERROR;
783 return HAL_OK;
787 * @brief Lock the FLASH Option Control Registers access.
788 * @retval HAL Status
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)
798 return HAL_ERROR;
801 return HAL_OK;
805 * @brief Launch the option bytes loading.
806 * @retval HAL Status
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)
815 status = HAL_ERROR;
817 else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
819 status = HAL_ERROR;
821 else
823 status = 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);
835 return status;
839 * @}
842 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
843 * @brief Peripheral Errors functions
845 @verbatim
846 ===============================================================================
847 ##### Peripheral Errors functions #####
848 ===============================================================================
849 [..]
850 This subsection permits to get in run-time Errors of the FLASH peripheral.
852 @endverbatim
853 * @{
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;
890 * @}
894 * @}
897 /* Private functions ---------------------------------------------------------*/
899 /** @addtogroup FLASH_Private_Functions
900 * @{
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
913 flag will be set */
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;
925 else
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))
936 return HAL_TIMEOUT;
941 /* Get Error Flags */
942 if (Bank == FLASH_BANK_1)
944 errorflag = FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1;
946 else
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);
960 return HAL_ERROR;
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);
972 else
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);
981 return HAL_OK;
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)
991 /* Get 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))
1001 return HAL_TIMEOUT;
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;
1015 return HAL_ERROR;
1018 /* If there is no error flag set */
1019 return HAL_OK;
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)
1030 uint32_t bsyflag;
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;
1040 else
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))
1052 return HAL_TIMEOUT;
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);
1068 return HAL_ERROR;
1071 else
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);
1081 return HAL_ERROR;
1085 /* If there is no error flag set */
1086 return HAL_OK;
1090 * @}
1093 #endif /* HAL_FLASH_MODULE_ENABLED */
1096 * @}
1100 * @}
1103 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/