How can you determine what caused a watchdog timer reset

Started by thinkpeace, July 28, 2014, 04:14:36 PM

thinkpeace

I'm using a watchdog timer on the Moetino, and when a WDT reset occurs, I would like to log it the the flash so I can keep a count of resets.

Is there a way in Setup() to determine of the Moetino was reset do to a watchgod timer reset?

void setup()
{
  Serial.begin(9600);

  if( reset_caused_by_watch_dog )  // <----- How can I do this?
   {
       Serial.println( "Reset was caused by watchdog");
   }
    wdt_enable(WDTO_1S);
}
 
void loop()
{
  // the program is alive...for now. 
  wdt_reset();
 

  doSomeStuff();
}

Felix

You can check the bits in the status register corresponding to those resets:

  byte statusReg = MCUSR;
  if (statusReg & _BV(WDRF))
  {
    Serial.println("WDT reset");
  }
  else if (statusReg & _BV(EXTRF))
  {
    Serial.println("External reset");
  }


But note that Obtiboot bootloader does this check itself and resets the MCUSR = 0

thinkpeace

Does this mean I would need to modify the bootloader to save the MCUSR state?

Felix

Probably.
You need to keep the MCUSR preserved (ie just comment out the line that resets it).
However it may be a good idea to reset it sometime early in your program.