2021-12-16 12:33 AM
Hello STM Community! This is my first Question in this Forum.
I am using a STM32L081KZT and I need to save data, when the Power is unplugged.
My setup is powered by USART and can only be turned off by unplugging the UART cable. In that case I need to save my usage Data to the EEPROM, so I tried using the PVD. I searched the Forum on how to configure this. What I am using now is:
void HAL_PWR_PVDCallback(void)
{
save_archiv(); //This function saves data to the EEPROM
}
void PVD_Init(void)
{
/* Configure the PVD Level */
PWR_PVDTypeDef PVDStruct = {0};
PVDStruct.PVDLevel = PWR_PVDLEVEL_5;
PVDStruct.Mode = PWR_PVD_MODE_IT_RISING_RISING;
HAL_PWR_PVDConfig(&PVDStruct);
/* Enable the PVD Output */
HAL_PWR_EnablePVD();
}
And of course calling the PVD_Init() in my main() function.
The Issue here is:
When unplugging and reconnecting the UART (Power), I can clearly see data being saved, but all Variables are 0. First I thought, this might be because the Edge is detected too late and the RAM is already cleared. But now I think it just triggers the Interrupt during startup, when the variables in the RAM are still empty. Unplugging the power will (maybe) not trigger the PVD Interrupt. I already tried changing the mode to PWR_PVD_MODE_IT_RISING_FALLING.
Can anyone tell me, what I am missing?
Help is very appreciated!
2021-12-16 05:52 AM
> I can clearly see data being saved, but all Variables are 0.
How can you be so sure? As opposed to the values just being 0 and having never been changed?
It shouldn't be enabled during startup. Your code needs to progress through the startup code before the PVD interrupt is enabled. I suppose it's possible if your voltage rail is super slow to come up to 3.3V, or whatever.
Blink an LED or toggle a pin during the interrupt and observe on a scope to see exactly if and when it's called.
Also look at the power rail on a scope to see how much time you have between the PVD event and BOR when the chip will reset. Verify your code is fast enough to complete within this interval.
If you're only interested in saving on power loss, don't enable the rising edge detection.
You also need to enable the PVD NVIC interrupt, which your code above doesn't do. Maybe it's elsewhere.