KugleFirmware/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_flash.c
/**
  * @brief  Wait for a FLASH operation to complete.
  * @param  Timeout maximum flash operation timeout
  * @param  Bank flash FLASH_BANK_1 or FLASH_BANK_2
  * @retval HAL_StatusTypeDef HAL Status
  */
@brief  Wait for a FLASH operation to complete.
@param  Timeout maximum flash operation timeout
@param  Bank flash FLASH_BANK_1 or FLASH_BANK_2
@retval HAL_StatusTypeDef HAL Status
@brief Wait for a FLASH operation to complete . @param Timeout maximum flash operation timeout @param Bank flash FLASH_BANK_1 or FLASH_BANK_2 @retval HAL_StatusTypeDef HAL Status
HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout, uint32_t Bank)
{
  uint32_t bsyflag, errorflag = 0;  
  uint32_t timeout = HAL_GetTick() + Timeout;
  assert_param(IS_FLASH_BANK_EXCLUSIVE(Bank));
  if(Bank == FLASH_BANK_1)
  {
    bsyflag = FLASH_FLAG_BSY_BANK1 | FLASH_FLAG_QW_BANK1;
    if((FLASH->OPTCR & FLASH_OPTCR_SWAP_BANK) == 0)
    {
      bsyflag |= FLASH_FLAG_WBNE_BANK1;
    }
    else
    {
      bsyflag |= FLASH_FLAG_WBNE_BANK2;
    }      
  }
  else
  {
    bsyflag = FLASH_FLAG_BSY_BANK2 | FLASH_FLAG_QW_BANK2;
    if((FLASH->OPTCR & FLASH_OPTCR_SWAP_BANK) == 0)
    {
      bsyflag |= FLASH_FLAG_WBNE_BANK2;
    }
    else
    {
      bsyflag |= FLASH_FLAG_WBNE_BANK1;
    }    
  }
  while(__HAL_FLASH_GET_FLAG(bsyflag)) 
  { 
    if(Timeout != HAL_MAX_DELAY)
    {
      if(HAL_GetTick() >= timeout)
      {
        return HAL_TIMEOUT;
      }
    } 
  }
  if((Bank == FLASH_BANK_1) && ((FLASH->SR1 & FLASH_FLAG_ALL_ERRORS_BANK1) != RESET))
  {
    errorflag = FLASH_FLAG_ALL_ERRORS_BANK1;
  }
  else if((Bank == FLASH_BANK_2) && ((FLASH->SR2 & FLASH_FLAG_ALL_ERRORS_BANK2 & 0x7FFFFFFF) != RESET))
  {
    errorflag = FLASH_FLAG_ALL_ERRORS_BANK2;
  }
  if(errorflag != 0)
  {
    FLASH_SetErrorCode(Bank);
    __HAL_FLASH_CLEAR_FLAG(errorflag);
    return HAL_ERROR;
  }
  return HAL_OK;  
}
HAL_StatusTypeDef FLASH_WaitForLastOperation ( uint32_t Timeout , uint32_t Bank ) { uint32_t bsyflag , errorflag = 0 ; uint32_t timeout = HAL_GetTick ( ) + Timeout ; assert_param ( IS_FLASH_BANK_EXCLUSIVE ( Bank ) ) ; if ( Bank == FLASH_BANK_1 ) { bsyflag = FLASH_FLAG_BSY_BANK1 | FLASH_FLAG_QW_BANK1 ; if ( ( FLASH -> OPTCR & FLASH_OPTCR_SWAP_BANK ) == 0 ) { bsyflag |= FLASH_FLAG_WBNE_BANK1 ; } else { bsyflag |= FLASH_FLAG_WBNE_BANK2 ; } } else { bsyflag = FLASH_FLAG_BSY_BANK2 | FLASH_FLAG_QW_BANK2 ; if ( ( FLASH -> OPTCR & FLASH_OPTCR_SWAP_BANK ) == 0 ) { bsyflag |= FLASH_FLAG_WBNE_BANK2 ; } else { bsyflag |= FLASH_FLAG_WBNE_BANK1 ; } } while ( __HAL_FLASH_GET_FLAG ( bsyflag ) ) { if ( Timeout != HAL_MAX_DELAY ) { if ( HAL_GetTick ( ) >= timeout ) { return HAL_TIMEOUT ; } } } if ( ( Bank == FLASH_BANK_1 ) && ( ( FLASH -> SR1 & FLASH_FLAG_ALL_ERRORS_BANK1 ) != RESET ) ) { errorflag = FLASH_FLAG_ALL_ERRORS_BANK1 ; } else if ( ( Bank == FLASH_BANK_2 ) && ( ( FLASH -> SR2 & FLASH_FLAG_ALL_ERRORS_BANK2 & 0x7FFFFFFF ) != RESET ) ) { errorflag = FLASH_FLAG_ALL_ERRORS_BANK2 ; } if ( errorflag != 0 ) { FLASH_SetErrorCode ( Bank ) ; __HAL_FLASH_CLEAR_FLAG ( errorflag ) ; return HAL_ERROR ; } return HAL_OK ; }
@brief  Wait for a FLASH operation to complete.
@brief Wait for a FLASH operation to complete .
/* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.
     Even if the FLASH operation fails, the BUSY flag will be reset and an error
     flag will be set */ /*Save the error code*/ /* Clear error programming flags */ /* If there is an error flag set */