tynnor
Junior Member level 1
Hello,
I use a STM32G473 on my board, and I need to save a few bytes of data when the 24V power supply falls. I have more than 400ms to perform the saving, more than enough.
I am facing a weird issue : when I call the following function, from the Main loop (or ISR, same behavior), at power down, the only thing written in flash is a set of 0x0, such as my data buffer is empty. But I am pretty sure it is not.
However, when I use the following function, directly in my code, to save my data in Flash, it works. Or, when I directly write the buffer content inside this function, it works, too ! This drive my crazy
Reference manual, page 92
Do you have an idea to solve this problem ?
Thank you for your help
I use a STM32G473 on my board, and I need to save a few bytes of data when the 24V power supply falls. I have more than 400ms to perform the saving, more than enough.
I am facing a weird issue : when I call the following function, from the Main loop (or ISR, same behavior), at power down, the only thing written in flash is a set of 0x0, such as my data buffer is empty. But I am pretty sure it is not.
However, when I use the following function, directly in my code, to save my data in Flash, it works. Or, when I directly write the buffer content inside this function, it works, too ! This drive my crazy
C++:
bool Config::saveConfigFlash(){
uint32_t pageError;
FLASH_EraseInitTypeDef pEraseInit;
pEraseInit.Banks = FLASH_BANK_2;
pEraseInit.NbPages = 1;
pEraseInit.Page = 127;
pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
//currentConfig is the struct I want to save in Flash
static uint64_t data[1 + sizeof(TerrariumConfig)/sizeof(uint64_t)];
memcpy(data, (void*) ¤tConfig, sizeof(TerrariumConfig));
HAL_StatusTypeDef status = HAL_FLASH_Unlock();
SET_BIT(FLASH->SR, FLASH_SR_PGSERR);
status = HAL_FLASHEx_Erase(&pEraseInit, &pageError);
for(unsigned int i = 0; i < 1 + sizeof(TerrariumConfig)/sizeof(uint64_t); i++)
{
//write the data, with 64bit alignment
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addressDataFlash + i*8, data[i]);
}
FLASH_FlushCaches();
HAL_FLASH_Lock();
return true;
}
Reference manual, page 92
Do you have an idea to solve this problem ?
Thank you for your help