cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASHEx_Erase no effect on STMF401RE

JBark.3
Associate II

Hi. I am writing a bootlloader application that can erase, rewrite, and jump to a user application.

As a starting point, I want to make sure I can erase the user application, but the erase seems to have no effect. My sector erase code is below.

uint32_t PageError = 0;
FLASH_EraseInitTypeDef pEraseInit = {0};
HAL_StatusTypeDef status = HAL_OK;

  /* Unlock the Program memory */
if(HAL_FLASH_Unlock() != HAL_OK)
{
  return FLASH_PROTECTION_ERRROR;
}

/* Clear all FLASH flags */
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGSERR | FLASH_FLAG_WRPERR);

/* TODO Get the sector based on a passed memory address. Hardcoded for now */
/* user application is at 0x08040000 so hardcoding to sector 1 which lines up with this address */
pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
pEraseInit.Banks = FLASH_BANK_1;
pEraseInit.Sector = FLASH_SECTOR_1;
pEraseInit.NbSectors = 5;

/* Run the erase */
status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
// ^ status is alwasy HAL_OK. No page error

/* flush the data and instruction cache */
/* not sure if this is actually needed? */
__HAL_FLASH_DATA_CACHE_DISABLE();
__HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
__HAL_FLASH_DATA_CACHE_RESET();
__HAL_FLASH_INSTRUCTION_CACHE_RESET();
__HAL_FLASH_INSTRUCTION_CACHE_ENABLE();
__HAL_FLASH_DATA_CACHE_ENABLE();

/* Lock the Program memory */
HAL_FLASH_Lock();

if (status != HAL_OK)
{
  /* Error occurred while page erase */
  return FLASH_ERASE_ERROR;
}

return FLASH_OK;

This all runs ok, no errors reported. However, after running this code I can can still jump to the user application at 0x08040000 at the start of sector 1 and the code executes as normal as if it was never erased. 

Is there something I am missing? I am wondering if erase doesn't work the way I think it does, I was expecting there to be all 0x00's or 0xff's at these addresses.

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Bob S
Principal

0x0804 0000 is NOT sector 1.  Sector 1 is 0x0800 4000.  Flash address 0x0804 0000 is sector 6.

View solution in original post

2 REPLIES 2
Bob S
Principal

0x0804 0000 is NOT sector 1.  Sector 1 is 0x0800 4000.  Flash address 0x0804 0000 is sector 6.

Wow I am so embarrassed, I've looked at that number about 100 times and didn't spot the extra zero.

I will get that changed and confirm it's all working.