Question
Erasing flash sector works, but writing it causes HARD FAULT
I am trying to write data in the flash of an STM32F446RE, sector 5.
My linker script is configured so that .user_data goes to 0x08020000, and:
__attribute__((__section__(".user_data"))) char userConfig[4] = {1, 2, 3, 4};
Is my user Data.
If I read it after flashing, I properly debug 1 2 3 4 values.
If I try to erase sector 5, it works and the values now display 255 255 255 255.
However, if I try to program anything, an HARD FAULT occurs (see code below).
Do you have any idea ?
FLASH_EraseInitTypeDef flashErase;
flashErase.TypeErase = TYPEERASE_SECTORS;
flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_1;
flashErase.Sector = FLASH_SECTOR_5;
flashErase.NbSectors = 1;
uint32_t sectorError;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR |
FLASH_FLAG_PGSERR);
if (HAL_FLASHEx_Erase(&flashErase, §orError) != HAL_OK) {
error = true;
} else {
// If this is not used, the values correctly goes to 255, else an HARD FAULT occurs...
if (HAL_FLASH_Program(TYPEPROGRAM_BYTE, (uint32_t)&userConfig[0], 123) != HAL_OK) {
error = true;
}
}
HAL_FLASH_Lock();