Write data to flash when power supply drops
Hello,
I am using a STM32G473VET6 on my board, and I want to save a few data bytes in Flash when the 24V power supply drops. I have more than 400ms available between the detection by the MCU and the 3.3V dropping.
When the ADC triggers this event, the associated ISR try to write the data in Flash. The HAL functions don't seem to return any kind of error, but the only thing I can see inside the memory is a set of 0x0 at the programmed addresses.
But there is something else strange : if my function is called in another way, from code, the write in Flash is working successfully.
If I manually set the content of my @data, it works
It is like a cannot retrieve the content of @data from the ISR.
@data is declared as a static uint64_t array of my class
Here, the function called from the ADC ISR when a power down event occurs :
bool Config::writeFlash(){
uint32_t pageError;
static FLASH_EraseInitTypeDef pEraseInit;
pEraseInit.Banks = FLASH_BANK_2;
pEraseInit.NbPages = 1;
pEraseInit.Page = 3;
pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
//data is uint64_t array
//currentConfig is the struct I want to save in Flash
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;
}The write attempt occurs only once. Once done, ADC ISR are disabled.
To sum up, I can use the function above to write in flash when it is called from main program, but from ADC ISR, it doesn't work
Do you have any idea of the problem ?
Thank you ! =)
