How to figure out how much time you have for writing to flash when voltage source breaks down?
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. 







simulate this circuit &ndash; Schematic created using CircuitLab