assemblystm32flash-memorystm32cubeide

Erasing a flash sector for STM32G474RE is not working


I am trying to design a UART bootloader for STM32G474RE microcontroller. I am having problem in Flash erasing part of the code. Before designing bootloader for STM32G474RE microcontroller, I tried for STM32F767Zi Nucleo kit. and it is working fine on that kit. The code for the Nucleo kit is as follows:

if( is_first_block )
{

  printf("Erasing the Flash memory...\r\n");
  //Erase the Flash
  FLASH_EraseInitTypeDef EraseInitStruct;
  uint32_t SectorError;

  EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
  EraseInitStruct.Sector        = FLASH_SECTOR_5;
  EraseInitStruct.NbSectors     = 2;                    //erase 2 sectors(5,6)
  EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;

  ret = HAL_FLASHEx_Erase( &EraseInitStruct, &SectorError );
  if( ret != HAL_OK )
  {
    break;
  }
}

Here I am erasing 2 sectors.

Now when I am trying for STM32G474RE, I am having following issues:

  1. There is no option to erase sectors (Only pages)
  2. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3; this command is giving error when I compile the code. (I have checked stm32g4xx_hal_flash.h file but there is no information about it whereas stm32f7xx_hal_flash.h file has information about it.). I get the following two errors:
    - The macro FLASH_VOLTAGE_RANGE_3 is not #defined
    - The structure FLASH_EraseInitTypeDef has no member named VoltageRange

It would be great if some could help me and guide me about these issue from their experience.


Solution

  • There is no option to erase sectors (Only pages)

    Did you mean: "No option to erase pages (only sectors)"?

    Or did you mean: "No option to erase a complete bank"?

    HAL_FLASHEx_Erase erases one or more sectors, not pages. A sector consists of multiple pages.

    And a sector is the smallest unit that can be erased due to hardware restrictions.

    You cannot erase less than a whole sector because the hardware does not support erasing parts of a sector.

    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

    I just looked into the header files of STM32F4xx and STM32L4xx:

    STM32F4xx has a field named VoltageRange in the structure FLASH_EraseInitTypeDef, STM32L4xx does not have such a field.

    And of course the macro FLASH_VOLTAGE_RANGE_3 is also not defined for the STM32L4xx because the corresponding field is missing.

    You are using an STM32G4xx. If that CPU does not take the VoltageRange information, you must simply remove the complete line (EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3) from your code.