Question
Write to STM32 internal flash fails
On my stm32g0 mcu there is no eeprom. So, I am using internal flash to save one byte user data to retain it between power cycles.I am doing it the following way,
- Add Data section in memory in the linker script
MEMORY {
......
DATA (xrw) : ORIGIN = 0x800F800, LENGTH = 2K //Allocated one full flash page
}- Create user data section
.user_data :
{ . = ALIGN(4);
*(.user_data)
. = ALIGN(4);
} >DATA- Create a variable to store in flash
attribute((section(".user_data"))) const uint8_t userConfig[64]- Write data using following functions,
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
FLASH_PageErase(flash_page_number);
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)&userConfig[index], someData);
HAL_FLASH_Lock();When I try to write to the flash it fails with PGSERR flag set.
