cancel
Showing results for 
Search instead for 
Did you mean: 

Write data to flash when power supply drops

ATurp.11
Associate II

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*) &currentConfig, 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 ! =)

22 REPLIES 22
ATurp.11
Associate II

Yes the page is really erased. If I perform only erasing in the ISR, it works. I can check it in memory at power on.

To debug, as you said, I simulate a power down to short-circuit the bottom resistor of voltage divider user to measure the voltage power supply. I put a breakpoint at the return line of the saveConfigFlash() function. Here a screenshot :

0693W000007CwfxQAC.png 

As you can see, there is something written in Flash ! :astonished_face:

In a second time, I do exactly the same without the breakpoint, but still in debug mode. In pause, I check the content, still fine, my data are here !

The third experiment, I run the code, I simulate a power down. Check the flash : once again, my data are into the flash

Now, I run the code, and try a real power down. At power on, nothing in flash, only 0x0

Try real power down wit htoggle as i write on every write. And too try change flash type to WORD or HALFWORD

ATurp.11
Associate II

It is what I did, I can see the different toggles for the writing steps

I may have wrong but I understand there is no WORD or HALFWORD mode. But there is a fast program mode that I am trying to make it work