Detect if reboot caused by power or pushbutton?

Hi all.

Can code detect if it was started or restarted by power or push button?

I'm looking to change program flow based on that.

Best regards
Dave

Hi Dave, what is "it"?

Please don't say "an Arduino". That's like saying "a Ford".

Also please explain why you need to do this.

Ahh missed that bit.

ATmega2560 on an MEGA 2560 PRO.
But it could have been a Nano if I had not run out of Flash...

I'm looking to change program flow based on push button or power. If power, I'll assume the attached devices have reset also. If by button, the attached are probably as they were.

Best regards
Dave

1 Like

There is a register called MCUSR that stores the reset info.

Unfortunately, it seems the bootloader clears that register so when you try to read it in your own application you read '0'.

If this is really important to your application you might consider moving to Atmel Studio 7.0 and program your part directly over SPI. That way you'd have clear access to the MCUSR.

MCUSR – MCU Status Register

The MCU Status Register provides information on which reset source caused
 an MCU reset.

•   Bit 4 – JTRF: JTAG Reset Flag

 This bit is set if a reset is being caused by a logic one in the JTAG Reset 
  Register selected by the JTAG instruction AVR_RESET.
 This bit is reset by a Power-on Reset, or by writing a logic zero to the flag.

•   Bit 3 – WDRF: Watchdog Reset Flag
 This bit is set if a Watchdog Reset occurs. 

 The bit is reset by a Power-on Reset, or by writing a logic zero to theflag.

•   Bit 2 – BORF: Brown-out Reset Flag

 This bit is set if a Brown-out Reset occurs. 

 The bit is reset by a Power-on Reset,  or by writing a logic zero to the flag.

•   Bit 1 – EXTRF: External Reset Flag

 This bit is set if an External Reset occurs. 
 The bit is reset by a Power-on Reset, or by writing a logic zero to the flag.

•   Bit 0 – PORF: Power-on Reset Flag

 This bit is set if a Power-on Reset occurs. 

 The bit is reset only by writing a logic zero to the flag.

To make use of the Reset Flags to identify a reset condition,
 the user should read and then Reset the MCUSR as early as possible
 in the program. If the register is cleared before another reset occurs,
 the source of the reset can be found by examining the Reset Flag

in data sheet 12.5.1

So as the bootloader code runs first, the bits are zeroed and there is no way for my idea.
It was just a thought.

thanks for the info.
Best regards
Dave

1 Like

You do not need to "move to Atmel Studio 7.0" to program over the top of the bootloader. You just need an ICSP programmer as you necessarily would to use Atmel Studio.

wortinguk:
If power, I'll assume the attached devices have reset also. If by button, the attached are probably as they were

Can/should you reset them anyway? If interrupted by the reset button during some interaction with the MCU, they can be left in some random state and need to be reset.

Can you connect the other devices directly to the Mega's reset pin? Or connect their reset pins to other mega output pins and reset them during setup()?

Don't newer versions of optiboot preserve the MCUSR?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.