cancel
Showing results for 
Search instead for 
Did you mean: 

Why Flash is not erased Properly using HAL API? What would be the flash value after erasing? should it return 0xFF?

STura.1
Associate II

I am working on STM32H743 series, and I want to do Write operation continuously. When the flash is erased by STM32CubeProgrammer it returns 0xFF, but after erasing with HAL API (HAL_FLASHEx_Erase()), I got the value other than 0xFF (i.e previously flashed value). Does it mean it is not erased properly? if Yes then what is missing here?

Here is the code that i am using to erase the flash.

#define APP_ADDRESS ((uint32_t)0x08100000)

 HAL_FLASH_Unlock();

     EraseInitStruct.TypeErase   = FLASH_TYPEERASE_SECTORS;

    EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

    EraseInitStruct.Banks     = FLASH_BANK_2;

    EraseInitStruct.Sector = APP_ADDRESS;

    EraseInitStruct.NbSectors   = 1;

  FlashStatus = FLASH_WaitForLastOperation(HAL_MAX_DELAY, flash_ptr);

  printf("FlashStatus: %d\r\n", FlashStatus);

  if (FlashStatus != HAL_OK) {

  printf("Failed to complete last operation\r\n");

  } else {

  printf(" Last operation completed\r\n");

  }

  while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) { }

  while((*(__IO uint32_t*)flash_ptr) != 0xFFFFFFFF) {

    printf("Sector not Erased.. Read %lx\r\n", (*(__IO uint32_t*)flash_ptr));

    FlashStatus = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);

    if (FlashStatus != HAL_OK) {

    HAL_FLASH_Lock();

      return FlashStatus;

    } else {

    printf("Sector Erased.. Read %lx\r\n", (*(__IO uint32_t*)flash_ptr));

      printf("Erase - No error flags\r\n");

      HAL_FLASH_Lock();

      return FlashStatus;

    }

  }

2 REPLIES 2
TDK
Guru

> #define APP_ADDRESS ((uint32_t)0x08100000)

>    EraseInitStruct.Sector = APP_ADDRESS;

This is an address, not a sector number. It needs a sector number between 0 and FLASH_SECTOR_TOTAL - 1.

Implement Error_Handler so it catches error like this.

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Guru

Also, yes, the value should be 0xFF after erasing.

If you feel a post has answered your question, please click "Accept as Solution".