# How to figure out how much time you have for writing to flash when voltage source breaks down?

I try to figure out how I can make sure, that my controller (STM32F030K6) is always able to save a 16 bit integer to the flash when the power is breaking down rapidly.

This controller has an Analog Watchdog that fires an interrupt when it detects a value below a certain threshold.

The ADC runs in continious mode and multiplexes 4 channels of which one is the critical and is used for the Analog Watchdog. So I figured I get a new value for this channel every 350 cycles ( 87.5*4, to scan and convert 1 channel the adc needs 71+12.5 = 87.5 cycles). When the IRQ fires it runs this function:

uint16_t data=...;
...

{
HAL_FLASH_Unlock();
HAL_FLASH_Program(TYPEPROGRAM_HALFWORD,0x8007C00, data);
HAL_FLASH_Lock();

}


According to the data sheet, it takes this chip ~50µs to program a 16 bit value to the flash.

My set up is a PCB with a 24V supply, an LM317 generating 3,3V and a charge pump for a MOSFET, which takes the clock from the controller. I thought about shutting everything down before writing to flash, to save some power, but shutting down costs cycles...

I've got 100nF on the 24V input and 1µ on the input and output of the LM317 voltage regulator.

How can I make sure I can always save that value, even if the voltage on the input breaks down immidiately?

schematic http://i59.tinypic.com/29qld7o.jpg

• Store some charge behind a rectifier, and detect the power failure separately. – Ignacio Vazquez-Abrams Jul 2 '15 at 17:32
• @IgnacioVazquez-Abrams Sorry, took me a bit to upload a schematic. Imgur seems to have problems today. I only have DC voltages, no rectifying anywhere. – JavaForStarters Jul 2 '15 at 17:35
• You can't add one before the regulator? Because all you'd need is two diodes and a large cap. – Ignacio Vazquez-Abrams Jul 2 '15 at 17:35
• Oh, now I get what you mean. A diode before the input cap for the regulator? – JavaForStarters Jul 2 '15 at 17:39
• I could increase the capacity, but only if they stay SMD. But how big would they have to be if the controller dies at around 2,4V? – JavaForStarters Jul 2 '15 at 17:41