How to do a "last thing", when reset pin is triggered ?

Hi,

to be honest I'm a little bit lost at this question (I'm using arduino on a custom PCB with ATMEGA 32U4 5V@16MHz):

is it possible by using a interrupt (attachInterrupt) on the RESET Pin, to execute one quick "last task" (in my case: writing a log entry into ext. FRAM) before the MCU "shuts down" (goes into its RESET-State) ?

or is the ATMEA's RESET interrupt-source triggered on a POWER-UP event, only ?

Background:

There is a Vss controlled "watchdog" circuit on this custom PCB that pulls the RESET-Pin permanently down (=RESET-State), if the Vss drops below a certain value (here: 4,5V)

So what I want to do is to log this event ("power off" or "Voltage below 4,5V") in my ext. FRAM

The reset pin can be programmed to not be a reset pin. Then you can make your change.

No I'm not going to look it up, dig in the datasheet yourself. It's most often used on ATtiny projects.

Without a functioning reset pin does not the bootloader cease to work and you also need to use a high voltage ISP programer ?

So what I want to do is to log this event (“power off” […]) in my ext. FRAM [with timestamp]

since I do not want to change anything on my custom PCB:
maybe another idea would be to monitor permanently the MCU’s Vcc indirectly via an ADC channel and the internal reference voltage (2,56 V) or the internal bandgap voltage (1,09V@5Vcc).

I found this example here how to do that → » Measuring VCC via the bandgap » JeeLabs
[edit:] here’s another way to do the same: https://forum.arduino.cc/index.php?topic=38119.msg282519#msg282519

But I am not sure if the accuracy is high enough to trigger the “Power Loss” -event at let’s say 4,75Vcc
(normal operation is 5,00V <-> reset is triggered at 4,50V by external circuit → so I have to be in between)

Have you looked into the on-board brown-out detection?

The point of the reset pin is that it does thoroughly reset the chip. It's an interrupt that nothing can ignore.

I'm not sure that the built-in brown-out detection will do what you want as it's a similar kind of reset. It prevents any code from running in any way while the input power is low.

I think you are on the right track with the Vcc detection. If you can sense the drop to 4.75V and you have enough on-board capacitance to keep it running for a few milliseconds then that would be the best time to write something to the EEPROM. Note that EEPROM writes take a lot more power than the average power consumption, so you will need more capacitance than you think.

DKWatson: Have you looked into the on-board brown-out detection?

yes, but IMHO I think it is not useful for my purpose, since the MCU's brown-out-detection results in a RESET as well, leaving me with the same problem: how can I execute one quick "last task" when RESET is triggered...


MorganS: I think you are on the right track with the Vcc detection. If you can sense the drop to 4.75V and you have enough on-board capacitance to keep it running for a few milliseconds then that would be the best time to write something to the EEPROM.

OK I will try that ... fortunately the board has huge capacitance - I think I have some milliseconds I am not using internal EEPROM but external I2C-FRAM (Fujitsu MB85RC256V)

I'm not sure about BOD either except there's a flag set and a time-out before reset. Maybe, detecting the flag and setting the time-out will offer enough time to do something.

Just read the app note on BOD - not a solution.

Why not use another pin , with interrupt capability as your “ reset pin”. You can then arrange for that interrupt to execute a task then generate a board reset. Not sure why you want to press reset anyway .. the answer may be in there somewhere...

Could you maybe parallel the reset button to a digital IO pin so the digital IO reads the reset button press immediately but the press is delayed in reaching the reset pin by using a capacitor delay?

i’ve done similar in the past - both with capacity to ‘run down’ after primary supply fails - and/or a soft reset button. In fact, I didn’t even put a hard reset button on the boards - so players can’t bypass the orderly restart process.

questions:

hammy: Why not use another pin , with interrupt capability as your “ reset pin”.

Riva: Could you maybe parallel the reset button to a digital IO pin so the digital IO reads the reset button [...]

answer:

Dirk67: since I do not want to change anything on my custom PCB:

. .

hammy: Not sure why you want to press reset anyway ..

I never said that ... ;-) (there isn't even a reset button on my board) . .

MorganS: I think you are on the right track with the Vcc detection. If you can sense the drop to 4.75V and you have enough on-board capacitance to keep it running for a few milliseconds then that would be the best time to write something to the EEPROM.

I just finished testing this approach this afternoon, and it works like a charm. I can measure the Vcc in steps of around +/- 0,05V, so I can clearly detect if the Voltage decreases to 4,75V and have enough time to do "shutdown things", until the 4,50V are reached when the external reset circuit (Microchip MCP120T-450I) pulls the Reset-Pin down permanently ...

Thanks a lot @all for your tips.

The last quote in the post above was from MorganS and not me.

Dirk67: So what I want to do is to log this event ("power off" or "Voltage below 4,5V") in my ext. FRAM

With what timestamp accuracy?

Dirk67: I can measure the Vcc in steps of around +/- 0,05V, so I can clearly detect if the Voltage decreases to 4,75V and have enough time to do "shutdown things"

You assume the voltage will always drop "slowly". Is that guaranteed?