cancel
Showing results for 
Search instead for 
Did you mean: 

Power turn ON and Power turn OFF detection.

vbk22398
Senior

My requirement is as follows.
When I power ON the STM32L4 series MCU, a string must be sent on to the UART terminal like "Power is ON". Now abruptly I will power OFF. Now at this instance, another string must be sent to UART terminal which is "Power is OFF". How to do this? Kindly let me know the solution if anyone has come across such a situation.

Thank you!

18 REPLIES 18

We've done some projects with power-loss detection. IIRC the super cap is connected to the "main" power supply pin (not Vbat), and the power loss signal is a GPIO interrupt. STM32 has a voltage comparator so it can detect voltage drop by itself.

 


@Andrew Neil wrote:

@Ozone wrote:

This would be the job of the hardware designers.


Indeed.

Also the job of the hardware designers to provide a signal which indicates "power lost" (or, conversely,  "power good")


He could be the one who is responsible of both: software and hardware ;)

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

@SofLit wrote:


He could be the one that is responsible of both: software and hardware ;)


Indeed - so should have both hardware (including electronics) and software design skills & knowledge...

The company I did this project for back then went as cheap as possible.
Direct mains supply was already present, and they just added a simple power (mains) off detection capability.
The electrolytic cap capacity of the low-voltage supply stage was designed to keep this voltage about the brown-out level long enough, including a generous reserve for aging. Electrolytic caps lose quite a bit of capacity over a few years.


@Ozone wrote:

I had been working on a project were the main MCU stored about a dozen parameters into EEPROM during that remaining time.


Sir, my project is also to do the same where I have to store the log of ON time and OFF time of a MCU chip into an EEPROM using an RTC. Kindly could you explain how you managed to do the same with EEPROM. Could you explain in deep about your project! 

This project involved a Microchip PIC18 back then, so actual code is not relevant. But the concept remains the same.
As a side note, with "EEPROM", I mean byte- or word-programmable Flash memory. Not the standard Flash, were you can erase only sector-wise.

The idea is simple. First, you need a reliable mains-off detection. If mains power is off (for, say, 2 or 3 cycles), stop the cyclic operation, and disable interrupts related to cyclic processing. Proceed to write your persistent values to EEPROM, including a possible prior erase of the involved EEPROM cells.
Check the datasheet of the EEPROM you are using for worst-case erase / program times. This, times the number of cells you need to write, tells you how long your caps need to sustain the MCU until final brown-out.
Check the worst-case current consumption of the MCU and EEPROM, and estimate the capacity (Q = I * t) you need.
Take aging into consideration, electrolytic caps can lose about half of their initial capacity over the years.

Once done with updating the EEPROM, cycle to check the mains-off detection, until either mains returns, or the MCU is reset by brown-out.


@vbk22398 wrote:


Sir, my project is also to do the same where I have to store the log of ON time and OFF time of a MCU chip into an EEPROM using an RTC.


In that case why you don't use an external battery cell that powers supply VBAT and store that On/Off time in the backup registers? but still need to keep a power during that store operation (using capacitor large enough to complete the operation) and the detection of power off is mentioned previously (using a comparator).

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

@Ozone wrote:

First, you need a reliable mains-off detection. .


@vbk22398 you still haven't said what power supply your system has - is it mains? something else?

And what causes it to go OFF - is it a deliberate shutdown? power failure? what?

 


@Ozone wrote:

As a side note, with "EEPROM", I mean byte- or word-programmable Flash memory. Not the standard Flash, were you can erase only sector-wise.


@vbk22398  A few STM32s do have  such byte-programmable "EEPROM". On others, it is possible to "emulate" such EEPROM in the normal program Flash - but that's a separate topic!

But before all this, you still need the basics:

  1. detect that the power has gone OFF;
  2. have sufficient backup power to keep the microcontroller running long enough to complete all the things it needs to do on a Power-OFF.

Don't get distracted by other stuff before you've got these sorted!

> @vbk22398  A few STM32s do have  such byte-programmable "EEPROM". On others, it is possible to "emulate" such EEPROM in the normal program Flash - but that's a separate topic!

One needs to really think carefully if this is the case.
An "emulation" involves normal Flash, which is divided in "sectors" and "banks". While sectors can be erased separately, only a whole bank can be set into erase/program mode at a time. And during that time, no code can be executed from that bank - which means moving routines to RAM. And the majority of Cortex M devices (including STMs) are single-bank MCUs. When comparing, one will easily note the price difference ...

This emulation (and shifting code/data to RAM) is ok for bootloaders, but not for time- and energy-critical operations.
A cheap I2C or SPI serial EEPROM can save the day here, if necessary.