cancel
Showing results for 
Search instead for 
Did you mean: 

flash erase fail

lbapplem
Associate II

hello:

      I use STM32H7A3VGT6 in our board, two flash regions are available in the MCU and their start address are 0x08000000 and 0x08080000, all in bank1, when I use STM32CubeProgrammer to erase more than 3 sectors from 0x08080000, it works well, but if I do the coding and erase over 3 sectors from 0x08080000,it fails when erase at the 3rd sector, crash in FLASH_WaitForLastOperation, the flash erase code is below, can you help me find out the casue? Thanks.

Bill

static uint32_t GetSector(uint32_t Address)
{
uint32_t sector = 0;

if (Address < (FLASH_BASE + FLASH_BANK_SIZE))
{
sector = (Address - FLASH_BASE) / FLASH_SECTOR_SIZE;
}
else
{
sector = (Address - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_SECTOR_SIZE;
}

return sector;
}

HAL_StatusTypeDef flash_erase(uint32_t dest, uint32_t size)
{
uint32_t FirstSector = 0, NbOfSectors = 0, SECTORError = 0;
HAL_StatusTypeDef status = HAL_OK;
FLASH_EraseInitTypeDef EraseInitStruct;

if(size > FLASH_BANK_SIZE
|| dest < FLASH_BANK1_BASE
|| dest >= FLASH_BANK2_BASE)
{
return HAL_ERROR;
}
/* Disable instruction cache prior to internal cacheable memory update */
SCB_DisableICache();

/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();

/* Get the 1st sector to erase */
FirstSector = GetSector(dest);
/* Get the number of sector to erase from 1st sector*/
NbOfSectors = GetSector(dest + size) - FirstSector + 1;

/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = NbOfSectors;

if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
/*
Error occurred while sector erase.
User can add here some code to deal with this error.
SECTORError will contain the faulty sector and then to know the code error on this sector,
user can call function 'HAL_FLASH_GetError()'
*/
status = HAL_ERROR;
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();

/* Enable instruction cache prior to internal cacheable memory update */
SCB_EnableICache();

return status;
}

4 REPLIES 4

Use the code pasting tool, </>

Don't you want this to scope the END of BANK2?

if(size > FLASH_BANK_SIZE
|| dest < FLASH_BANK1_BASE
|| dest >= FLASH_BANK2_BASE)

 

if(size > FLASH_BANK_SIZE
|| dest < FLASH_BANK1_BASE
|| dest >= (FLASH_BANK2_BASE+FLASH_BANK_SIZE))
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

Show your call to flash_erase along with its arguments.

Show contents of EraseInitStruct when you call HAL_FLASHEx_Erase. Probably an invalid value somewhere.

 

If you want to restrict it to bank 1, probably wanted to check:

 

if (size > FLASH_BANK_SIZE
    || dest < FLASH_BANK1_BASE
    || dest + size >= FLASH_BANK2_BASE)

 

Should also check to ensure dest is at the start of a sector.

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

#define WRITE_SIZE (24 * 1024)
uint8_t* data = pvPortMalloc(WRITE_SIZE);

memset(data, 0x33, WRITE_SIZE);
uint32_t end = 0;
flash_erase(0x08080000, WRITE_SIZE);
flash_write(0x08080000, (uint32_t)data, WRITE_SIZE);

if I set the destination address in first flash region, which starts from 0x08000000 with size of 512KB, the code works well, only fail in second region starts from 0x08080000

TDK
Guru

Where is your code at that erasing 0x08000000 works?

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