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

adccapacitormicrocontrollerprogramming

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=...;
...

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

    HAL_ADC_IRQHandler(&hadc);
}

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

Best Answer

Something like this should work.

Since C1 only supplies the current for the LM317 + controller, you know it is discharging with ~35mA. If you set the threshold at (say) 10V, then you have at least 5V delta-V for C1 before U1 starts to drop out.

Say you want it to stay up for 1ms after power fail is detected, then

C1 = (0.001 * 0.035)/5 = 7uF. So you could use a 10uF cap.

schematic

simulate this circuit – Schematic created using CircuitLab