Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:07

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_flash.c
0004   * @author  MCD Application Team
0005   * @brief   FLASH HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the internal FLASH memory:
0008   *           + Program operations functions
0009   *           + Memory Control functions
0010   *           + Peripheral Errors functions
0011   *
0012  @verbatim
0013   ==============================================================================
0014                         ##### FLASH peripheral features #####
0015   ==============================================================================
0016 
0017   [..] The Flash memory interface manages CPU AXI I-Code and D-Code accesses
0018        to the Flash memory. It implements the erase and program Flash memory operations
0019        and the read and write protection mechanisms.
0020 
0021   [..] The FLASH main features are:
0022       (+) Flash memory read operations
0023       (+) Flash memory program/erase operations
0024       (+) Read / write protections
0025       (+) Option bytes programming
0026       (+) Error code correction (ECC) : Data in flash are 266-bits word
0027           (10 bits added per flash word)
0028 
0029                         ##### How to use this driver #####
0030  ==============================================================================
0031     [..]
0032       This driver provides functions and macros to configure and program the FLASH
0033       memory of all STM32H7xx devices.
0034 
0035       (#) FLASH Memory IO Programming functions:
0036            (++) Lock and Unlock the FLASH interface using HAL_FLASH_Unlock() and
0037                 HAL_FLASH_Lock() functions
0038            (++) Program functions: 256-bit word only
0039            (++) There Two modes of programming :
0040             (+++) Polling mode using HAL_FLASH_Program() function
0041             (+++) Interrupt mode using HAL_FLASH_Program_IT() function
0042 
0043       (#) Interrupts and flags management functions :
0044            (++) Handle FLASH interrupts by calling HAL_FLASH_IRQHandler()
0045            (++) Callback functions are called when the flash operations are finished :
0046                 HAL_FLASH_EndOfOperationCallback() when everything is ok, otherwise
0047                 HAL_FLASH_OperationErrorCallback()
0048            (++) Get error flag status by calling HAL_FLASH_GetError()
0049 
0050       (#) Option bytes management functions :
0051            (++) Lock and Unlock the option bytes using HAL_FLASH_OB_Unlock() and
0052                 HAL_FLASH_OB_Lock() functions
0053            (++) Launch the reload of the option bytes using HAL_FLASH_OB_Launch() function.
0054                 In this case, a reset is generated
0055     [..]
0056       In addition to these functions, this driver includes a set of macros allowing
0057       to handle the following operations:
0058        (+) Set the latency
0059        (+) Enable/Disable the FLASH interrupts
0060        (+) Monitor the FLASH flags status
0061      [..]
0062     (@) For any Flash memory program operation (erase or program), the CPU clock frequency
0063         (HCLK) must be at least 1MHz.
0064     (@) The contents of the Flash memory are not guaranteed if a device reset occurs during
0065         a Flash memory operation.
0066     (@) The application can simultaneously request a read and a write operation through each AXI
0067         interface.
0068         As the Flash memory is divided into two independent banks, the embedded Flash
0069         memory interface can drive different operations at the same time on each bank. For
0070         example a read, write or erase operation can be executed on bank 1 while another read,
0071         write or erase operation is executed on bank 2.
0072 
0073  @endverbatim
0074   ******************************************************************************
0075   * @attention
0076   *
0077   * Copyright (c) 2017 STMicroelectronics.
0078   * All rights reserved.
0079   *
0080   * This software is licensed under terms that can be found in the LICENSE file in
0081   * the root directory of this software component.
0082   * If no LICENSE file comes with this software, it is provided AS-IS.
0083   ******************************************************************************
0084   */
0085 
0086 /* Includes ------------------------------------------------------------------*/
0087 #include "stm32h7xx_hal.h"
0088 
0089 /** @addtogroup STM32H7xx_HAL_Driver
0090   * @{
0091   */
0092 
0093 /** @defgroup FLASH FLASH
0094   * @ingroup RTEMSBSPsARMSTM32H7
0095   * @brief FLASH HAL module driver
0096   * @{
0097   */
0098 
0099 #ifdef HAL_FLASH_MODULE_ENABLED
0100 
0101 /* Private typedef -----------------------------------------------------------*/
0102 /* Private define ------------------------------------------------------------*/
0103 /** @addtogroup FLASH_Private_Constants
0104   * @{
0105   */
0106 #define FLASH_TIMEOUT_VALUE              50000U /* 50 s */
0107 /**
0108   * @}
0109   */
0110 /* Private macro -------------------------------------------------------------*/
0111 /* Private variables ---------------------------------------------------------*/
0112 /** @addtogroup FLASH_Private_Variables
0113   * @{
0114   */
0115 FLASH_ProcessTypeDef pFlash;
0116 /**
0117   * @}
0118   */
0119 /* Private function prototypes -----------------------------------------------*/
0120 /* Exported functions ---------------------------------------------------------*/
0121 
0122 /** @defgroup FLASH_Exported_Functions FLASH Exported functions
0123   * @ingroup RTEMSBSPsARMSTM32H7
0124   * @{
0125   */
0126 
0127 /** @defgroup FLASH_Exported_Functions_Group1 Programming operation functions
0128   * @ingroup RTEMSBSPsARMSTM32H7
0129   *  @brief   Programming operation functions
0130   *
0131 @verbatim
0132  ===============================================================================
0133                   ##### Programming operation functions #####
0134  ===============================================================================
0135     [..]
0136     This subsection provides a set of functions allowing to manage the FLASH
0137     program operations.
0138 
0139 @endverbatim
0140   * @{
0141   */
0142 
0143 /**
0144   * @brief  Program a flash word at a specified address
0145   * @param  TypeProgram Indicate the way to program at a specified address.
0146   *         This parameter can be a value of @ref FLASH_Type_Program
0147   * @param  FlashAddress specifies the address to be programmed.
0148   *         This parameter shall be aligned to the Flash word:
0149   *          - 256 bits for STM32H74x/5X devices (8x 32bits words)
0150   *          - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
0151   *          - 256 bits for STM32H72x/3X devices (8x 32bits words)
0152   * @param  DataAddress specifies the address of data to be programmed.
0153   *         This parameter shall be 32-bit aligned
0154   *
0155   * @retval HAL_StatusTypeDef HAL Status
0156   */
0157 HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
0158 {
0159   HAL_StatusTypeDef status;
0160   __IO uint32_t *dest_addr = (__IO uint32_t *)FlashAddress;
0161   __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
0162   uint32_t bank;
0163   uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
0164 
0165   /* Check the parameters */
0166   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
0167   assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
0168 
0169   /* Process Locked */
0170   __HAL_LOCK(&pFlash);
0171 
0172 #if defined (FLASH_OPTCR_PG_OTP)
0173   if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
0174 #else
0175   if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
0176 #endif /* FLASH_OPTCR_PG_OTP */
0177   {
0178     bank = FLASH_BANK_1;
0179     /* Prevent unused argument(s) compilation warning */
0180     UNUSED(TypeProgram);
0181   }
0182 #if defined (DUAL_BANK)
0183   else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
0184   {
0185     bank = FLASH_BANK_2;
0186   }
0187 #endif /* DUAL_BANK */
0188   else
0189   {
0190     return HAL_ERROR;
0191   }
0192 
0193   /* Reset error code */
0194   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
0195 
0196   /* Wait for last operation to be completed */
0197   status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
0198 
0199   if(status == HAL_OK)
0200   {
0201 #if defined (DUAL_BANK)
0202     if(bank == FLASH_BANK_1)
0203     {
0204 #if defined (FLASH_OPTCR_PG_OTP)
0205       if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0206       {
0207         /* Set OTP_PG bit */
0208         SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0209       }
0210       else
0211 #endif /* FLASH_OPTCR_PG_OTP */
0212       {
0213         /* Set PG bit */
0214         SET_BIT(FLASH->CR1, FLASH_CR_PG);
0215       }
0216     }
0217     else
0218     {
0219       /* Set PG bit */
0220       SET_BIT(FLASH->CR2, FLASH_CR_PG);
0221     }
0222 #else /* Single Bank */
0223 #if defined (FLASH_OPTCR_PG_OTP)
0224       if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0225       {
0226         /* Set OTP_PG bit */
0227         SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0228       }
0229       else
0230 #endif /* FLASH_OPTCR_PG_OTP */
0231       {
0232         /* Set PG bit */
0233         SET_BIT(FLASH->CR1, FLASH_CR_PG);
0234       }
0235 #endif /* DUAL_BANK */
0236 
0237     __ISB();
0238     __DSB();
0239 
0240 #if defined (FLASH_OPTCR_PG_OTP)
0241     if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0242     {
0243       /* Program an OTP word (16 bits) */
0244       *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
0245     }
0246     else
0247 #endif /* FLASH_OPTCR_PG_OTP */
0248     {
0249       /* Program the flash word */
0250       do
0251       {
0252         *dest_addr = *src_addr;
0253         dest_addr++;
0254         src_addr++;
0255         row_index--;
0256      } while (row_index != 0U);
0257     }
0258 
0259     __ISB();
0260     __DSB();
0261 
0262     /* Wait for last operation to be completed */
0263     status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
0264 
0265 #if defined (DUAL_BANK)
0266 #if defined (FLASH_OPTCR_PG_OTP)
0267     if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0268     {
0269       /* If the program operation is completed, disable the OTP_PG */
0270       CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0271     }
0272     else
0273 #endif /* FLASH_OPTCR_PG_OTP */
0274     {
0275       if(bank == FLASH_BANK_1)
0276       {
0277         /* If the program operation is completed, disable the PG */
0278         CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
0279       }
0280       else
0281       {
0282         /* If the program operation is completed, disable the PG */
0283         CLEAR_BIT(FLASH->CR2, FLASH_CR_PG);
0284       }
0285     }
0286 #else /* Single Bank */
0287 #if defined (FLASH_OPTCR_PG_OTP)
0288     if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0289     {
0290       /* If the program operation is completed, disable the OTP_PG */
0291       CLEAR_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0292     }
0293     else
0294 #endif /* FLASH_OPTCR_PG_OTP */
0295     {
0296       /* If the program operation is completed, disable the PG */
0297       CLEAR_BIT(FLASH->CR1, FLASH_CR_PG);
0298     }
0299 #endif /* DUAL_BANK */
0300   }
0301 
0302   /* Process Unlocked */
0303   __HAL_UNLOCK(&pFlash);
0304 
0305   return status;
0306 }
0307 
0308 /**
0309   * @brief  Program a flash word at a specified address with interrupt enabled.
0310   * @param  TypeProgram Indicate the way to program at a specified address.
0311   *                      This parameter can be a value of @ref FLASH_Type_Program
0312   * @param  FlashAddress specifies the address to be programmed.
0313   *         This parameter shall be aligned to the Flash word:
0314   *          - 256 bits for STM32H74x/5X devices (8x 32bits words)
0315   *          - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
0316   *          - 256 bits for STM32H72x/3X devices (8x 32bits words)
0317   * @param  DataAddress specifies the address of data to be programmed.
0318   *         This parameter shall be 32-bit aligned
0319   *
0320   * @retval HAL Status
0321   */
0322 HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)
0323 {
0324   HAL_StatusTypeDef status;
0325   __IO uint32_t *dest_addr = (__IO uint32_t*)FlashAddress;
0326   __IO uint32_t *src_addr = (__IO uint32_t*)DataAddress;
0327   uint32_t bank;
0328   uint8_t row_index = FLASH_NB_32BITWORD_IN_FLASHWORD;
0329 
0330   /* Check the parameters */
0331   assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));
0332   assert_param(IS_FLASH_PROGRAM_ADDRESS(FlashAddress));
0333 
0334   /* Process Locked */
0335   __HAL_LOCK(&pFlash);
0336 
0337   /* Reset error code */
0338   pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
0339 
0340 #if defined (FLASH_OPTCR_PG_OTP)
0341   if((IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress)) || (IS_FLASH_PROGRAM_ADDRESS_OTP(FlashAddress)))
0342 #else
0343   if(IS_FLASH_PROGRAM_ADDRESS_BANK1(FlashAddress))
0344 #endif /* FLASH_OPTCR_PG_OTP */
0345   {
0346     bank = FLASH_BANK_1;
0347     /* Prevent unused argument(s) compilation warning */
0348     UNUSED(TypeProgram);
0349   }
0350 #if defined (DUAL_BANK)
0351   else if(IS_FLASH_PROGRAM_ADDRESS_BANK2(FlashAddress))
0352   {
0353     bank = FLASH_BANK_2;
0354   }
0355 #endif /* DUAL_BANK */
0356   else
0357   {
0358     return HAL_ERROR;
0359   }
0360 
0361   /* Wait for last operation to be completed */
0362   status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, bank);
0363 
0364   if (status != HAL_OK)
0365   {
0366     /* Process Unlocked */
0367     __HAL_UNLOCK(&pFlash);
0368   }
0369   else
0370   {
0371     pFlash.Address = FlashAddress;
0372 
0373 #if defined (DUAL_BANK)
0374     if(bank == FLASH_BANK_1)
0375     {
0376       /* Set internal variables used by the IRQ handler */
0377       pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
0378 
0379 #if defined (FLASH_OPTCR_PG_OTP)
0380       if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0381       {
0382         /* Set OTP_PG bit */
0383         SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0384       }
0385       else
0386 #endif /* FLASH_OPTCR_PG_OTP */
0387       {
0388         /* Set PG bit */
0389         SET_BIT(FLASH->CR1, FLASH_CR_PG);
0390       }
0391 
0392       /* Enable End of Operation and Error interrupts for Bank 1 */
0393 #if defined (FLASH_CR_OPERRIE)
0394       __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1     | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0395                                   FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
0396 #else
0397       __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1     | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0398                                   FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
0399 #endif /* FLASH_CR_OPERRIE */
0400     }
0401     else
0402     {
0403       /* Set internal variables used by the IRQ handler */
0404       pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK2;
0405 
0406       /* Set PG bit */
0407       SET_BIT(FLASH->CR2, FLASH_CR_PG);
0408 
0409       /* Enable End of Operation and Error interrupts for Bank2 */
0410 #if defined (FLASH_CR_OPERRIE)
0411       __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2     | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
0412                                   FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
0413 #else
0414       __HAL_FLASH_ENABLE_IT_BANK2(FLASH_IT_EOP_BANK2     | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
0415                                   FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
0416 #endif /* FLASH_CR_OPERRIE */
0417     }
0418 #else /* Single Bank */
0419     /* Set internal variables used by the IRQ handler */
0420     pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM_BANK1;
0421 
0422 #if defined (FLASH_OPTCR_PG_OTP)
0423     if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0424     {
0425       /* Set OTP_PG bit */
0426       SET_BIT(FLASH->OPTCR, FLASH_OPTCR_PG_OTP);
0427     }
0428     else
0429 #endif /* FLASH_OPTCR_PG_OTP */
0430     {
0431       /* Set PG bit */
0432       SET_BIT(FLASH->CR1, FLASH_CR_PG);
0433     }
0434 
0435       /* Enable End of Operation and Error interrupts for Bank 1 */
0436 #if defined (FLASH_CR_OPERRIE)
0437       __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1     | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0438                                   FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
0439 #else
0440       __HAL_FLASH_ENABLE_IT_BANK1(FLASH_IT_EOP_BANK1     | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0441                                   FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
0442 #endif /* FLASH_CR_OPERRIE */
0443 #endif /* DUAL_BANK */
0444 
0445     __ISB();
0446     __DSB();
0447 
0448 #if defined (FLASH_OPTCR_PG_OTP)
0449     if (TypeProgram == FLASH_TYPEPROGRAM_OTPWORD)
0450     {
0451       /* Program an OTP word (16 bits) */
0452       *(__IO uint16_t *)FlashAddress = *(__IO uint16_t*)DataAddress;
0453     }
0454     else
0455 #endif /* FLASH_OPTCR_PG_OTP */
0456     {
0457       /* Program the flash word */
0458       do
0459       {
0460         *dest_addr = *src_addr;
0461         dest_addr++;
0462         src_addr++;
0463         row_index--;
0464       } while (row_index != 0U);
0465     }
0466 
0467     __ISB();
0468     __DSB();
0469   }
0470 
0471   return status;
0472 }
0473 
0474 /**
0475   * @brief This function handles FLASH interrupt request.
0476   * @retval None
0477   */
0478 void HAL_FLASH_IRQHandler(void)
0479 {
0480   uint32_t temp;
0481   uint32_t errorflag;
0482   FLASH_ProcedureTypeDef procedure;
0483 
0484   /* Check FLASH Bank1 End of Operation flag  */
0485   if(__HAL_FLASH_GET_FLAG_BANK1(FLASH_SR_EOP) != RESET)
0486   {
0487     if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK1)
0488     {
0489       /* Nb of sector to erased can be decreased */
0490       pFlash.NbSectorsToErase--;
0491 
0492       /* Check if there are still sectors to erase */
0493       if(pFlash.NbSectorsToErase != 0U)
0494       {
0495         /* Indicate user which sector has been erased */
0496         HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
0497 
0498         /* Clear bank 1 End of Operation pending bit */
0499         __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
0500 
0501         /* Increment sector number */
0502         pFlash.Sector++;
0503         temp = pFlash.Sector;
0504         FLASH_Erase_Sector(temp, FLASH_BANK_1, pFlash.VoltageForErase);
0505       }
0506       else
0507       {
0508         /* No more sectors to Erase, user callback can be called */
0509         /* Reset Sector and stop Erase sectors procedure */
0510         pFlash.Sector = 0xFFFFFFFFU;
0511         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0512 
0513         /* FLASH EOP interrupt user callback */
0514         HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
0515 
0516         /* Clear FLASH End of Operation pending bit */
0517         __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
0518       }
0519     }
0520     else
0521     {
0522       procedure = pFlash.ProcedureOnGoing;
0523 
0524       if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
0525       {
0526         /* MassErase ended. Return the selected bank */
0527         /* FLASH EOP interrupt user callback */
0528         HAL_FLASH_EndOfOperationCallback(FLASH_BANK_1);
0529       }
0530       else if(procedure == FLASH_PROC_PROGRAM_BANK1)
0531       {
0532         /* Program ended. Return the selected address */
0533         /* FLASH EOP interrupt user callback */
0534         HAL_FLASH_EndOfOperationCallback(pFlash.Address);
0535       }
0536       else
0537       {
0538         /* Nothing to do */
0539       }
0540 
0541       if((procedure != FLASH_PROC_SECTERASE_BANK2) && \
0542          (procedure != FLASH_PROC_MASSERASE_BANK2) && \
0543          (procedure != FLASH_PROC_PROGRAM_BANK2))
0544       {
0545         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0546         /* Clear FLASH End of Operation pending bit */
0547         __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
0548       }
0549     }
0550   }
0551 
0552 #if defined (DUAL_BANK)
0553  /* Check FLASH Bank2 End of Operation flag  */
0554   if(__HAL_FLASH_GET_FLAG_BANK2(FLASH_SR_EOP) != RESET)
0555   {
0556     if(pFlash.ProcedureOnGoing == FLASH_PROC_SECTERASE_BANK2)
0557     {
0558       /*Nb of sector to erased can be decreased*/
0559       pFlash.NbSectorsToErase--;
0560 
0561       /* Check if there are still sectors to erase*/
0562       if(pFlash.NbSectorsToErase != 0U)
0563       {
0564         /*Indicate user which sector has been erased*/
0565         HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
0566 
0567         /* Clear bank 2 End of Operation pending bit */
0568         __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
0569 
0570         /*Increment sector number*/
0571         pFlash.Sector++;
0572         temp = pFlash.Sector;
0573         FLASH_Erase_Sector(temp, FLASH_BANK_2, pFlash.VoltageForErase);
0574       }
0575       else
0576       {
0577         /* No more sectors to Erase, user callback can be called */
0578         /* Reset Sector and stop Erase sectors procedure */
0579         pFlash.Sector = 0xFFFFFFFFU;
0580         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0581 
0582         /* FLASH EOP interrupt user callback */
0583         HAL_FLASH_EndOfOperationCallback(pFlash.Sector);
0584 
0585         /* Clear FLASH End of Operation pending bit */
0586         __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
0587       }
0588     }
0589     else
0590     {
0591       procedure = pFlash.ProcedureOnGoing;
0592 
0593       if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
0594       {
0595         /*MassErase ended. Return the selected bank*/
0596         /* FLASH EOP interrupt user callback */
0597         HAL_FLASH_EndOfOperationCallback(FLASH_BANK_2);
0598       }
0599       else if(procedure == FLASH_PROC_PROGRAM_BANK2)
0600       {
0601         /* Program ended. Return the selected address */
0602         /* FLASH EOP interrupt user callback */
0603         HAL_FLASH_EndOfOperationCallback(pFlash.Address);
0604       }
0605       else
0606       {
0607         /* Nothing to do */
0608       }
0609 
0610       if((procedure != FLASH_PROC_SECTERASE_BANK1) && \
0611          (procedure != FLASH_PROC_MASSERASE_BANK1) && \
0612          (procedure != FLASH_PROC_PROGRAM_BANK1))
0613       {
0614         pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0615         /* Clear FLASH End of Operation pending bit */
0616         __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
0617       }
0618     }
0619   }
0620 #endif /* DUAL_BANK */
0621 
0622   /* Check FLASH Bank1 operation error flags */
0623 #if defined (FLASH_SR_OPERR)
0624   errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
0625                             FLASH_FLAG_INCERR_BANK1 | FLASH_FLAG_OPERR_BANK1);
0626 #else
0627   errorflag = FLASH->SR1 & (FLASH_FLAG_WRPERR_BANK1 | FLASH_FLAG_PGSERR_BANK1 | FLASH_FLAG_STRBERR_BANK1 | \
0628                             FLASH_FLAG_INCERR_BANK1);
0629 #endif /* FLASH_SR_OPERR */
0630 
0631   if(errorflag != 0U)
0632   {
0633     /* Save the error code */
0634     pFlash.ErrorCode |= errorflag;
0635 
0636     /* Clear error programming flags */
0637     __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
0638 
0639     procedure = pFlash.ProcedureOnGoing;
0640 
0641     if(procedure == FLASH_PROC_SECTERASE_BANK1)
0642     {
0643       /* Return the faulty sector */
0644       temp = pFlash.Sector;
0645       pFlash.Sector = 0xFFFFFFFFU;
0646     }
0647     else if((procedure == FLASH_PROC_MASSERASE_BANK1) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
0648     {
0649       /* Return the faulty bank */
0650       temp = FLASH_BANK_1;
0651     }
0652     else
0653     {
0654       /* Return the faulty address */
0655       temp = pFlash.Address;
0656     }
0657 
0658     /* Stop the procedure ongoing*/
0659     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0660 
0661     /* FLASH error interrupt user callback */
0662     HAL_FLASH_OperationErrorCallback(temp);
0663   }
0664 
0665 #if (USE_FLASH_ECC == 1U)
0666   /* Check FLASH Bank1 ECC single correction error flag */
0667   errorflag = FLASH->SR1 & FLASH_FLAG_SNECCERR_BANK1;
0668 
0669   if(errorflag != 0U)
0670   {
0671     /* Save the error code */
0672     pFlash.ErrorCode |= errorflag;
0673 
0674     /* Call User callback */
0675     HAL_FLASHEx_EccCorrectionCallback();
0676 
0677     /* Clear FLASH Bank1 ECC single correction error flag in order to allow new ECC error record */
0678     __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
0679   }
0680 
0681   /* Check FLASH Bank1 ECC double detection error flag */
0682   errorflag = FLASH->SR1 & FLASH_FLAG_DBECCERR_BANK1;
0683 
0684   if(errorflag != 0U)
0685   {
0686     /* Save the error code */
0687     pFlash.ErrorCode |= errorflag;
0688 
0689     /* Call User callback */
0690     HAL_FLASHEx_EccDetectionCallback();
0691 
0692     /* Clear FLASH Bank1 ECC double detection error flag in order to allow new ECC error record */
0693     __HAL_FLASH_CLEAR_FLAG_BANK1(errorflag);
0694   }
0695 #endif /* USE_FLASH_ECC */
0696 
0697 #if defined (DUAL_BANK)
0698   /* Check FLASH Bank2 operation error flags */
0699 #if defined (FLASH_SR_OPERR)
0700   errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
0701                              FLASH_FLAG_INCERR_BANK2 | FLASH_FLAG_OPERR_BANK2) & 0x7FFFFFFFU);
0702 #else
0703   errorflag = FLASH->SR2 & ((FLASH_FLAG_WRPERR_BANK2 | FLASH_FLAG_PGSERR_BANK2 | FLASH_FLAG_STRBERR_BANK2 | \
0704                              FLASH_FLAG_INCERR_BANK2) & 0x7FFFFFFFU);
0705 #endif /* FLASH_SR_OPERR */
0706 
0707   if(errorflag != 0U)
0708   {
0709     /* Save the error code */
0710     pFlash.ErrorCode |= (errorflag | 0x80000000U);
0711 
0712     /* Clear error programming flags */
0713     __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
0714 
0715     procedure = pFlash.ProcedureOnGoing;
0716 
0717     if(procedure== FLASH_PROC_SECTERASE_BANK2)
0718     {
0719       /*return the faulty sector*/
0720       temp = pFlash.Sector;
0721       pFlash.Sector = 0xFFFFFFFFU;
0722     }
0723     else if((procedure == FLASH_PROC_MASSERASE_BANK2) || (procedure == FLASH_PROC_ALLBANK_MASSERASE))
0724     {
0725       /*return the faulty bank*/
0726       temp = FLASH_BANK_2;
0727     }
0728     else
0729     {
0730       /*return the faulty address*/
0731       temp = pFlash.Address;
0732     }
0733 
0734     /*Stop the procedure ongoing*/
0735     pFlash.ProcedureOnGoing = FLASH_PROC_NONE;
0736 
0737     /* FLASH error interrupt user callback */
0738     HAL_FLASH_OperationErrorCallback(temp);
0739   }
0740 
0741 #if (USE_FLASH_ECC == 1U)
0742   /* Check FLASH Bank2 ECC single correction error flag */
0743   errorflag = FLASH->SR2 & FLASH_FLAG_SNECCERR_BANK2;
0744 
0745   if(errorflag != 0U)
0746   {
0747     /* Save the error code */
0748     pFlash.ErrorCode |= (errorflag | 0x80000000U);
0749 
0750     /* Call User callback */
0751     HAL_FLASHEx_EccCorrectionCallback();
0752 
0753     /* Clear FLASH Bank2 ECC single correction error flag in order to allow new ECC error record */
0754     __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
0755   }
0756 
0757   /* Check FLASH Bank2 ECC double detection error flag */
0758   errorflag = FLASH->SR2 & FLASH_FLAG_DBECCERR_BANK2;
0759 
0760   if(errorflag != 0U)
0761   {
0762     /* Save the error code */
0763     pFlash.ErrorCode |= (errorflag | 0x80000000U);
0764 
0765     /* Call User callback */
0766     HAL_FLASHEx_EccDetectionCallback();
0767 
0768     /* Clear FLASH Bank2 ECC double detection error flag in order to allow new ECC error record */
0769     __HAL_FLASH_CLEAR_FLAG_BANK2(errorflag);
0770   }
0771 
0772 #endif /* USE_FLASH_ECC */
0773 #endif /* DUAL_BANK */
0774 
0775   if(pFlash.ProcedureOnGoing == FLASH_PROC_NONE)
0776   {
0777 #if defined (FLASH_CR_OPERRIE)
0778     /* Disable Bank1 Operation and Error source interrupt */
0779     __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1    | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0780                                  FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1 | FLASH_IT_OPERR_BANK1);
0781 
0782 #if defined (DUAL_BANK)
0783     /* Disable Bank2 Operation and Error source interrupt */
0784     __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2    | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
0785                                  FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2 | FLASH_IT_OPERR_BANK2);
0786 #endif /* DUAL_BANK */
0787 #else
0788     /* Disable Bank1 Operation and Error source interrupt */
0789     __HAL_FLASH_DISABLE_IT_BANK1(FLASH_IT_EOP_BANK1    | FLASH_IT_WRPERR_BANK1 | FLASH_IT_PGSERR_BANK1 | \
0790                                  FLASH_IT_STRBERR_BANK1 | FLASH_IT_INCERR_BANK1);
0791 
0792 #if defined (DUAL_BANK)
0793     /* Disable Bank2 Operation and Error source interrupt */
0794     __HAL_FLASH_DISABLE_IT_BANK2(FLASH_IT_EOP_BANK2    | FLASH_IT_WRPERR_BANK2 | FLASH_IT_PGSERR_BANK2 | \
0795                                  FLASH_IT_STRBERR_BANK2 | FLASH_IT_INCERR_BANK2);
0796 #endif /* DUAL_BANK */
0797 #endif /* FLASH_CR_OPERRIE */
0798 
0799     /* Process Unlocked */
0800     __HAL_UNLOCK(&pFlash);
0801   }
0802 }
0803 
0804 /**
0805   * @brief  FLASH end of operation interrupt callback
0806   * @param  ReturnValue The value saved in this parameter depends on the ongoing procedure
0807   *                  Mass Erase: Bank number which has been requested to erase
0808   *                  Sectors Erase: Sector which has been erased
0809   *                    (if 0xFFFFFFFF, it means that all the selected sectors have been erased)
0810   *                  Program: Address which was selected for data program
0811   * @retval None
0812   */
0813 __weak void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue)
0814 {
0815   /* Prevent unused argument(s) compilation warning */
0816   UNUSED(ReturnValue);
0817 
0818   /* NOTE : This function Should not be modified, when the callback is needed,
0819             the HAL_FLASH_EndOfOperationCallback could be implemented in the user file
0820    */
0821 }
0822 
0823 /**
0824   * @brief  FLASH operation error interrupt callback
0825   * @param  ReturnValue The value saved in this parameter depends on the ongoing procedure
0826   *                 Mass Erase: Bank number which has been requested to erase
0827   *                 Sectors Erase: Sector number which returned an error
0828   *                 Program: Address which was selected for data program
0829   * @retval None
0830   */
0831 __weak void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue)
0832 {
0833   /* Prevent unused argument(s) compilation warning */
0834   UNUSED(ReturnValue);
0835 
0836   /* NOTE : This function Should not be modified, when the callback is needed,
0837             the HAL_FLASH_OperationErrorCallback could be implemented in the user file
0838    */
0839 }
0840 
0841 /**
0842   * @}
0843   */
0844 
0845 /** @defgroup FLASH_Exported_Functions_Group2 Peripheral Control functions
0846   * @ingroup RTEMSBSPsARMSTM32H7
0847   *  @brief   Management functions
0848   *
0849 @verbatim
0850  ===============================================================================
0851                       ##### Peripheral Control functions #####
0852  ===============================================================================
0853     [..]
0854     This subsection provides a set of functions allowing to control the FLASH
0855     memory operations.
0856 
0857 @endverbatim
0858   * @{
0859   */
0860 
0861 /**
0862   * @brief  Unlock the FLASH control registers access
0863   * @retval HAL Status
0864   */
0865 HAL_StatusTypeDef HAL_FLASH_Unlock(void)
0866 {
0867   if(READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
0868   {
0869     /* Authorize the FLASH Bank1 Registers access */
0870     WRITE_REG(FLASH->KEYR1, FLASH_KEY1);
0871     WRITE_REG(FLASH->KEYR1, FLASH_KEY2);
0872 
0873     /* Verify Flash Bank1 is unlocked */
0874     if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) != 0U)
0875     {
0876       return HAL_ERROR;
0877     }
0878   }
0879 
0880 #if defined (DUAL_BANK)
0881   if(READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
0882   {
0883     /* Authorize the FLASH Bank2 Registers access */
0884     WRITE_REG(FLASH->KEYR2, FLASH_KEY1);
0885     WRITE_REG(FLASH->KEYR2, FLASH_KEY2);
0886 
0887     /* Verify Flash Bank2 is unlocked */
0888     if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) != 0U)
0889     {
0890       return HAL_ERROR;
0891     }
0892   }
0893 #endif /* DUAL_BANK */
0894 
0895   return HAL_OK;
0896 }
0897 
0898 /**
0899   * @brief  Locks the FLASH control registers access
0900   * @retval HAL Status
0901   */
0902 HAL_StatusTypeDef HAL_FLASH_Lock(void)
0903 {
0904   /* Set the LOCK Bit to lock the FLASH Bank1 Control Register access */
0905   SET_BIT(FLASH->CR1, FLASH_CR_LOCK);
0906 
0907   /* Verify Flash Bank1 is locked */
0908   if (READ_BIT(FLASH->CR1, FLASH_CR_LOCK) == 0U)
0909   {
0910     return HAL_ERROR;
0911   }
0912 
0913 #if defined (DUAL_BANK)
0914   /* Set the LOCK Bit to lock the FLASH Bank2 Control Register access */
0915   SET_BIT(FLASH->CR2, FLASH_CR_LOCK);
0916 
0917   /* Verify Flash Bank2 is locked */
0918   if (READ_BIT(FLASH->CR2, FLASH_CR_LOCK) == 0U)
0919   {
0920     return HAL_ERROR;
0921   }
0922 #endif /* DUAL_BANK */
0923 
0924   return HAL_OK;
0925 }
0926 
0927 /**
0928   * @brief  Unlock the FLASH Option Control Registers access.
0929   * @retval HAL Status
0930   */
0931 HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void)
0932 {
0933   if(READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
0934   {
0935     /* Authorizes the Option Byte registers programming */
0936     WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY1);
0937     WRITE_REG(FLASH->OPTKEYR, FLASH_OPT_KEY2);
0938 
0939     /* Verify that the Option Bytes are unlocked */
0940     if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) != 0U)
0941     {
0942       return HAL_ERROR;
0943     }
0944   }
0945 
0946   return HAL_OK;
0947 }
0948 
0949 /**
0950   * @brief  Lock the FLASH Option Control Registers access.
0951   * @retval HAL Status
0952   */
0953 HAL_StatusTypeDef HAL_FLASH_OB_Lock(void)
0954 {
0955   /* Set the OPTLOCK Bit to lock the FLASH Option Byte Registers access */
0956   SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK);
0957 
0958   /* Verify that the Option Bytes are locked */
0959   if (READ_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTLOCK) == 0U)
0960   {
0961     return HAL_ERROR;
0962   }
0963 
0964   return HAL_OK;
0965 }
0966 
0967 /**
0968   * @brief  Launch the option bytes loading.
0969   * @retval HAL Status
0970   */
0971 HAL_StatusTypeDef HAL_FLASH_OB_Launch(void)
0972 {
0973   HAL_StatusTypeDef status;
0974 
0975   /* Wait for CRC computation to be completed */
0976   if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_1) != HAL_OK)
0977   {
0978     status = HAL_ERROR;
0979   }
0980 #if defined (DUAL_BANK)
0981   else if (FLASH_CRC_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE, FLASH_BANK_2) != HAL_OK)
0982   {
0983     status = HAL_ERROR;
0984   }
0985 #endif /* DUAL_BANK */
0986   else
0987   {
0988     status = HAL_OK;
0989   }
0990 
0991   if (status == HAL_OK)
0992   {
0993     /* Set OPTSTRT Bit */
0994     SET_BIT(FLASH->OPTCR, FLASH_OPTCR_OPTSTART);
0995 
0996     /* Wait for OB change operation to be completed */
0997     status = FLASH_OB_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE);
0998   }
0999 
1000   return status;
1001 }
1002 
1003 /**
1004   * @}
1005   */
1006 
1007 /** @defgroup FLASH_Exported_Functions_Group3 Peripheral State and Errors functions
1008   * @ingroup RTEMSBSPsARMSTM32H7
1009   *  @brief   Peripheral Errors functions
1010   *
1011 @verbatim
1012  ===============================================================================
1013                 ##### Peripheral Errors functions #####
1014  ===============================================================================
1015     [..]
1016     This subsection permits to get in run-time Errors of the FLASH peripheral.
1017 
1018 @endverbatim
1019   * @{
1020   */
1021 
1022 /**
1023   * @brief  Get the specific FLASH error flag.
1024   * @retval HAL_FLASH_ERRORCode The returned value can be:
1025   *            @arg HAL_FLASH_ERROR_NONE       : No error set
1026   *
1027   *            @arg HAL_FLASH_ERROR_WRP_BANK1  : Write Protection Error on Bank 1
1028   *            @arg HAL_FLASH_ERROR_PGS_BANK1  : Program Sequence Error on Bank 1
1029   *            @arg HAL_FLASH_ERROR_STRB_BANK1 : Strobe Error on Bank 1
1030   *            @arg HAL_FLASH_ERROR_INC_BANK1  : Inconsistency Error on Bank 1
1031   *            @arg HAL_FLASH_ERROR_OPE_BANK1  : Operation Error on Bank 1
1032   *            @arg HAL_FLASH_ERROR_RDP_BANK1  : Read Protection Error on Bank 1
1033   *            @arg HAL_FLASH_ERROR_RDS_BANK1  : Read Secured Error on Bank 1
1034   *            @arg HAL_FLASH_ERROR_SNECC_BANK1: ECC Single Correction Error on Bank 1
1035   *            @arg HAL_FLASH_ERROR_DBECC_BANK1: ECC Double Detection Error on Bank 1
1036   *            @arg HAL_FLASH_ERROR_CRCRD_BANK1: CRC Read Error on Bank 1
1037   *
1038   *            @arg HAL_FLASH_ERROR_WRP_BANK2  : Write Protection Error on Bank 2
1039   *            @arg HAL_FLASH_ERROR_PGS_BANK2  : Program Sequence Error on Bank 2
1040   *            @arg HAL_FLASH_ERROR_STRB_BANK2 : Strobe Error on Bank 2
1041   *            @arg HAL_FLASH_ERROR_INC_BANK2  : Inconsistency Error on Bank 2
1042   *            @arg HAL_FLASH_ERROR_OPE_BANK2  : Operation Error on Bank 2
1043   *            @arg HAL_FLASH_ERROR_RDP_BANK2  : Read Protection Error on Bank 2
1044   *            @arg HAL_FLASH_ERROR_RDS_BANK2  : Read Secured Error on Bank 2
1045   *            @arg HAL_FLASH_ERROR_SNECC_BANK2: SNECC Error on Bank 2
1046   *            @arg HAL_FLASH_ERROR_DBECC_BANK2: Double Detection ECC on Bank 2
1047   *            @arg HAL_FLASH_ERROR_CRCRD_BANK2: CRC Read Error on Bank 2
1048   */
1049 
1050 uint32_t HAL_FLASH_GetError(void)
1051 {
1052    return pFlash.ErrorCode;
1053 }
1054 
1055 /**
1056   * @}
1057   */
1058 
1059 /**
1060   * @}
1061   */
1062 
1063 /* Private functions ---------------------------------------------------------*/
1064 
1065 /** @addtogroup FLASH_Private_Functions
1066   * @{
1067   */
1068 
1069 /**
1070   * @brief  Wait for a FLASH operation to complete.
1071   * @param  Timeout maximum flash operation timeout
1072   * @param  Bank flash FLASH_BANK_1 or FLASH_BANK_2
1073   * @retval HAL_StatusTypeDef HAL Status
1074   */
1075 HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
1076 {
1077   /* Wait for the FLASH operation to complete by polling on QW flag to be reset.
1078      Even if the FLASH operation fails, the QW flag will be reset and an error
1079      flag will be set */
1080 
1081   uint32_t bsyflag = FLASH_FLAG_QW_BANK1;
1082   uint32_t errorflag = 0;
1083   uint32_t tickstart = HAL_GetTick();
1084 
1085   assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1086 
1087 #if defined (DUAL_BANK)
1088 
1089   if (Bank == FLASH_BANK_2)
1090   {
1091     /* Select bsyflag depending on Bank */
1092     bsyflag = FLASH_FLAG_QW_BANK2;
1093   }
1094 #endif /* DUAL_BANK */
1095 
1096   while(__HAL_FLASH_GET_FLAG(bsyflag))
1097   {
1098     if(Timeout != HAL_MAX_DELAY)
1099     {
1100       if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1101       {
1102         return HAL_TIMEOUT;
1103       }
1104     }
1105   }
1106 
1107   /* Get Error Flags */
1108   if (Bank == FLASH_BANK_1)
1109   {
1110     errorflag = FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1;
1111   }
1112 #if defined (DUAL_BANK)
1113   else
1114   {
1115     errorflag = (FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2) | 0x80000000U;
1116   }
1117 #endif /* DUAL_BANK */
1118 
1119   /* In case of error reported in Flash SR1 or SR2 register */
1120   if((errorflag & 0x7FFFFFFFU) != 0U)
1121   {
1122     /*Save the error code*/
1123     pFlash.ErrorCode |= errorflag;
1124 
1125     /* Clear error programming flags */
1126     __HAL_FLASH_CLEAR_FLAG(errorflag);
1127 
1128     return HAL_ERROR;
1129   }
1130 
1131   /* Check FLASH End of Operation flag  */
1132   if(Bank == FLASH_BANK_1)
1133   {
1134     if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_EOP_BANK1))
1135     {
1136       /* Clear FLASH End of Operation pending bit */
1137       __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_EOP_BANK1);
1138     }
1139   }
1140 #if defined (DUAL_BANK)
1141   else
1142   {
1143     if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_EOP_BANK2))
1144     {
1145       /* Clear FLASH End of Operation pending bit */
1146       __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_EOP_BANK2);
1147     }
1148   }
1149 #endif /* DUAL_BANK */
1150 
1151   return HAL_OK;
1152 }
1153 
1154 /**
1155   * @brief  Wait for a FLASH Option Bytes change operation to complete.
1156   * @param  Timeout maximum flash operation timeout
1157   * @retval HAL_StatusTypeDef HAL Status
1158   */
1159 HAL_StatusTypeDef FLASH_OB_WaitForLastOperation(uint32_t Timeout)
1160 {
1161   /* Get timeout */
1162   uint32_t tickstart = HAL_GetTick();
1163 
1164   /* Wait for the FLASH Option Bytes change operation to complete by polling on OPT_BUSY flag to be reset */
1165   while(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPT_BUSY) != 0U)
1166   {
1167     if(Timeout != HAL_MAX_DELAY)
1168     {
1169       if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1170       {
1171         return HAL_TIMEOUT;
1172       }
1173     }
1174   }
1175 
1176   /* Check option byte change error */
1177   if(READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_OPTCHANGEERR) != 0U)
1178   {
1179     /* Save the error code */
1180     pFlash.ErrorCode |= HAL_FLASH_ERROR_OB_CHANGE;
1181 
1182     /* Clear the OB error flag */
1183     FLASH->OPTCCR |= FLASH_OPTCCR_CLR_OPTCHANGEERR;
1184 
1185     return HAL_ERROR;
1186   }
1187 
1188   /* If there is no error flag set */
1189   return HAL_OK;
1190 }
1191 
1192 /**
1193   * @brief  Wait for a FLASH CRC computation to complete.
1194   * @param  Timeout maximum flash operation timeout
1195   * @param  Bank flash FLASH_BANK_1 or FLASH_BANK_2
1196   * @retval HAL_StatusTypeDef HAL Status
1197   */
1198 HAL_StatusTypeDef FLASH_CRC_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
1199 {
1200   uint32_t bsyflag;
1201   uint32_t tickstart = HAL_GetTick();
1202 
1203   assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
1204 
1205   /* Select bsyflag depending on Bank */
1206   if(Bank == FLASH_BANK_1)
1207   {
1208     bsyflag = FLASH_FLAG_CRC_BUSY_BANK1;
1209   }
1210   else
1211   {
1212     bsyflag = FLASH_FLAG_CRC_BUSY_BANK2;
1213   }
1214 
1215   /* Wait for the FLASH CRC computation to complete by polling on CRC_BUSY flag to be reset */
1216   while(__HAL_FLASH_GET_FLAG(bsyflag))
1217   {
1218     if(Timeout != HAL_MAX_DELAY)
1219     {
1220       if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1221       {
1222         return HAL_TIMEOUT;
1223       }
1224     }
1225   }
1226 
1227   /* Check FLASH CRC read error flag  */
1228   if(Bank == FLASH_BANK_1)
1229   {
1230     if (__HAL_FLASH_GET_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1))
1231     {
1232       /* Save the error code */
1233       pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK1;
1234 
1235       /* Clear FLASH CRC read error pending bit */
1236       __HAL_FLASH_CLEAR_FLAG_BANK1(FLASH_FLAG_CRCRDERR_BANK1);
1237 
1238       return HAL_ERROR;
1239     }
1240   }
1241 #if defined (DUAL_BANK)
1242   else
1243   {
1244     if (__HAL_FLASH_GET_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2))
1245     {
1246       /* Save the error code */
1247       pFlash.ErrorCode |= HAL_FLASH_ERROR_CRCRD_BANK2;
1248 
1249       /* Clear FLASH CRC read error pending bit */
1250       __HAL_FLASH_CLEAR_FLAG_BANK2(FLASH_FLAG_CRCRDERR_BANK2);
1251 
1252       return HAL_ERROR;
1253     }
1254   }
1255 #endif /* DUAL_BANK */
1256 
1257   /* If there is no error flag set */
1258   return HAL_OK;
1259 }
1260 
1261 /**
1262   * @}
1263   */
1264 
1265 #endif /* HAL_FLASH_MODULE_ENABLED */
1266 
1267 /**
1268   * @}
1269   */
1270 
1271 /**
1272   * @}
1273   */
1274 
1275