4 years, 1 month ago.

detect power down and restart bin on power up


i am using SMTF401RE and want to be able to

  1. detect power down. In this case to just be able to save some state to an external memory (supply through battery after power down)
  2. detect power up to restart the bin.

What is the needed configuration and/or source code to be able to achieve these 2 points?


What do you mean with 'detect power down'? Detect when the MCU goes into standby/deepsleep mode? Or when the device gets physically disconnected?

posted by Jan Jongboom 30 Aug 2017

when there is no power supply anymore, hence the power supply is physically disconnected

posted by anteo c 30 Aug 2017

You generally assume power could be disconnected from the micro at any time. You don't try to detect this because, well, power is already disconnected at that point and your micro can't run. So if you need to save a state, you do it at a regular interval. The micro will automatically restart from the beginning of the program when power comes back. An external eeprom is popular for saving a few bytes of state data. There are a number of libraries for interfacing to various EEPROM chips, often over I2C interface. The parts can also self-write to their flash memory. This has limitations but can work for saving some data. Here is a library that emulates eeprom on an ST micro by using flash memory - may or may not work for f401, I don't know.


posted by Graham S. 31 Aug 2017

thanks Graham for your explanation. My solution contains also a battery which works as power supply when the external power supply is disconnected. Hence the micro can still run. At the moment the external power supply gets disconnected i can use the energy supplied by the battery to save the state, i just do not know how to detect this disconnection. At the moment the power supply is connected again (and the micro was running because the battery was maybe still not empty) i want the bin to be restarted (since the bin is still running during this time). Is this possible? How to detect external power disconnection/connection?

posted by anteo c 31 Aug 2017

4 Answers

4 years, 1 month ago.

Hi anteo,
you can use a RTC and 20 back-up registers(32bit x 20 = 80 bytes RAM) which F401RE CPU have these function.
Please refer my note book as follow.

My recomendation is,
1) Use Mbed-OS5 and assign one specific thread. The thread can save sate information peoridically(for e.g. 100mS interval) during power is on.
2) For saveing the state, it's better to save not only one but also few data using ring buffer. If so you can check the state change.
3) During power down, you don't need any special sequence.
4) After power up, you can check RTC survival or not. If it survived, you can read the sate.

In my notebook you can see backup circuit for the RTC and backup RAM.
Please use it. F401 has auto detection function for power down (unfortunately this function cannot connect an interrupt).

4 years, 1 month ago.

You can use I2C EERAM. This device is a SRAM with EEPROM. Write number is unlimited, because it uses the SRAM when receives data from I2C. It detects power down, and writes its content to the internal EEPROM. The only external part is a 10 uF capacitor. On startup it immediately restores data from EEPROM to SRAM. http://www.microchip.com/design-centers/memory/serial-eeram

4 years, 1 month ago.

You would probably want a combination circuit that provides either external power while charching your battery or switch over to battery power when the external power is lost. You also want alerts when external power is lost or returns. Look for a solution like this one: www.sparkfun.com/products/13777. It is intended for USB power with battery backup and automatic switchover. The PowerGood pin can be used to monitor the external power state and go to sleep mode. The pin can also be used to wake up your device when power returns. The Sparkfun board also has a battery monitor. This is just one example. There are alternative devices that work in more or less similar ways. The processor state can be saved in eeprom as explained above. You can force your cpu to restart the bin by generating a hardware reset pulse or by an NVIC call to the reset vector from the location where it restarts after waking up from sleep mode.

4 years, 1 month ago.


For what you describe, your mbed hardware system design should support power from either the battery or the external source. I don't know about the other parts, but if you look at the LPC1768 mbed schematic, you can see an example combining external and USB power.

Then, because these two power supplies are independent, you could monitor each with separate analog or digital inputs.

Since power will not catastrophically fail (because of the battery), the external power failure could activate an interrupt, or more simply you could just poll for power off.

Then run the process to save the state - eerom, flash, uSD card, or wherever.

Probably the last step, which would also need to be supported by the hardware design, is to then put the micro into a low power sleep state, or activate some digital output that disconnects the micro from the battery - then no power is drawn until the external source reappears.

I can picture the schematic and the software logic in my head, but I don't have any easy links to provide.