cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F446RE freezes when trying to write to flash (sector 7)

g239955_stm1
Associate II

Hello,

I am trying to write to flash memory from the firmware itself

It looks like the Erase process hangs for 2 seconds or so but works, but the writing process seems to totally freeze the MCU. Below is the code I am using. Any idea ?

FLASH_EraseInitTypeDef flashErase;
flashErase.TypeErase = TYPEERASE_SECTORS;
flashErase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
flashErase.Sector = FLASH_SECTOR_7;
flashErase.NbSectors = 1;
 
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);
 
uint32_t sectorError;
if (HAL_FLASHEx_Erase(&flashErase, &sectorError) != HAL_OK) {
  error = true;
}
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, 0x08060000, 123) != HAL_OK) {
  error = true;
}
HAL_FLASH_Lock();

1 REPLY 1
g239955_stm1
Associate II

Actually, an HARD FAULT is triggered instantly when trying to write in the flash, the micro controller doesn't hang