Arduino due watchdog recommendations

Post Reply
User avatar
rstevens81
Posts: 349
Joined: Sun Dec 22, 2019 10:36 am
Location: Bristol, UK
Has thanked: 21 times
Been thanked: 91 times

Arduino due watchdog recommendations

Post by rstevens81 »

Unfortunately the default Arduino watchdog is not compatible with the due.
https://www.arduino.cc/reference/en/libraries/watchdog/
A quick Google Yeilds this:
https://forum.arduino.cc/index.php?topic=506052.0

Code: Select all


#define WDT_KEY (0xA5)
/********************************************************************************
  extern "C" void _watchdogDefaultSetup (void) { WDT_Disable (WDT); }

  void watchdogSetup (void) __attribute__ ((weak, alias("_watchdogDefaultSetup")));
*********************************************************************************/

/*This function is called from init(). If the user does not provide
  this function, then the default action is to disable watchdog.
  This function has to be overriden, otherwise watchdog won't work !! */

void watchdogSetup(void) {/*** watchdogDisable (); ***/}

//void watchdogDefaultSetup (void) { }

//void watchdogSetup(void) { watchdogDefaultSetup ();}

void setup()
{
  // Enable watchdog.
  WDT->WDT_MR = WDT_MR_WDD(0xFFF)
                | WDT_MR_WDRPROC
                | WDT_MR_WDRSTEN
                | WDT_MR_WDV(256 * 2); // Watchdog triggers a reset after 2 seconds if underflow
                                       // 2 seconds equal 84000000 * 2 = 168000000 clock cycles
  /* Slow clock is running at 32.768 kHz
    watchdog frequency is therefore 32768 / 128 = 256 Hz
    WDV holds the periode in 256 th of seconds  */

  Serial.begin(250000);
  uint32_t status = (RSTC->RSTC_SR & RSTC_SR_RSTTYP_Msk) >> 8; // Get status from the last Reset
  Serial.print("RSTTYP = 0b"); Serial.println(status, BIN);  // Should be 0b010 after first watchdog reset
}

void loop()
{

  //Restart watchdog
  WDT->WDT_CR = WDT_CR_KEY(WDT_KEY)
                | WDT_CR_WDRSTT;

  Serial.println("Enter the main loop : Restart watchdog");
  delay(500);

  while (true)
  {
    Serial.println("the software becomes trapped in a deadlock !");
    delay(500);
    /* If the software becomes trapped in a deadlock,
       watchdog triggers a reset and software restarts with stored values
       in General Purpose Back up Registers*/
  }
}

Just wanted to check if anyone had any experience or recommendations as I expect this problem has been ran into before.
Rule 1 of EV Club is don't buy a rust bucket....
Which rule does everyone forget 🤪
arber333
Posts: 3241
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 74 times
Been thanked: 223 times
Contact:

Re: Arduino due watchdog recommendations

Post by arber333 »

rstevens81 wrote: Tue Feb 09, 2021 11:03 am Just wanted to check if anyone had any experience or recommendations as I expect this problem has been ran into before.
Thank you much for the variation!

I have tested this code. However i cant measure if it is in fact working.... it doesnt seem to trip anymore.
Do you think i could set a flag for watchdog reset, save it to EEPROM and count how many times is was tripped?

Code: Select all


Watchdog
https://forum.arduino.cc/t/watchdog-timer-arm-arduino-due-board-ide-1-8-9/663311
https://forum.arduino.cc/t/arduino-due-watchdog/486187

rstc_start_software_reset(RSTC); //DUE reset command   
  
*/
/********************************************************************************
  extern "C" void _watchdogDefaultSetup (void) { WDT_Disable (WDT); }

  void watchdogSetup (void) __attribute__ ((weak, alias("_watchdogDefaultSetup")));
*********************************************************************************/

/*This function is called from init(). If the user does not provide
  this function, then the default action is to disable watchdog.
  This function has to be overriden, otherwise watchdog won't work !! */

#define WDT_KEY (0xA5)
//These two lines redefine any serial calls to the native Serial port on the CANdue or EVTVDue and enables syntactic sugar to allow streaming to serial port Serial<<"Text";

void watchdogSetup(void) {/*** watchdogDisable (); ***/}

//void watchdogDefaultSetup (void) { }
//void watchdogSetup(void) { watchdogDefaultSetup ();}

  //Void setup Enable watchdog.
  WDT->WDT_MR = WDT_MR_WDD(0xFFF)
                | WDT_MR_WDRPROC
                | WDT_MR_WDRSTEN
                | WDT_MR_WDV(256 * 2); // Watchdog triggers a reset after 2 seconds if underflow
   // 2 seconds equal 84000000 * 2 = 168000000 clock cycles
  /* Slow clock is running at 32.768 kHz
    watchdog frequency is therefore 32768 / 128 = 256 Hz
    WDV holds the periode in 256 th of seconds  */

 //Inside Void loop restart watchdog
  WDT->WDT_CR = WDT_CR_KEY(WDT_KEY)
                | WDT_CR_WDRSTT;

    /* If the software becomes trapped in a deadlock,
       watchdog triggers a reset and software restarts with stored values
       in General Purpose Back up Registers*/

Post Reply