LowPowerLab Forum

Hardware support => Moteino => Topic started by: thinkpeace on July 28, 2014, 04:14:36 PM

Title: How can you determine what caused a watchdog timer reset
Post by: thinkpeace on July 28, 2014, 04:14:36 PM
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();
}
Title: Re: How can you determine what caused a watchdog timer reset
Post by: Felix on July 28, 2014, 05:04:07 PM
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
Title: Re: How can you determine what caused a watchdog timer reset
Post by: thinkpeace on July 28, 2014, 05:35:15 PM
Does this mean I would need to modify the bootloader to save the MCUSR state?
Title: Re: How can you determine what caused a watchdog timer reset
Post by: Felix on July 29, 2014, 08:21:46 AM
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.