Internal Watchdog of Xiao

Hello,

I would like to use the Watchdog of the Xiao. Unfortunately I don´t find any examples.
How to use the internal Watchdog?
Cheers

Boris

https://github.com/adafruit/Adafruit_SleepyDog/ @boklu This should work. I haven’t tested it.

SleepyDog does work for the XIAO SAMD21 Cortex M0+, but because I couldn’t find out what the macro is for distinguishing an XIAO SAMD21 Cortex M0+, I had to remove all the device selection code from the Adafruit_SleepyDog.h file. I also had to move the code from the .cpp file, into the .h file, to get it to work [because I don’t know how to get the Arduino IDE to compile .cpp files]:

/*!
 * @file Adafruit_SleepyDog.h
 */

#ifndef ADAFRUIT_SLEEPYDOG_H
#define ADAFRUIT_SLEEPYDOG_H

// Platform-specific code goes below.  Each #ifdef should check for the presence
// of their platform... yeah, right... if I could only find it!  Until then, it's
// hard-coded for the XIAO!
#include "utility/WatchdogSAMD.h"
typedef WatchdogSAMD WatchdogType;

extern WatchdogType Watchdog;

// -----------------------------------------------------------
// Moved from the Adifruit_SleepDog.cpp file:
/*!
 * @brief Global instance of the main class for sketches to use.
 */
WatchdogType Watchdog;
// -----------------------------------------------------------

#endif

WARNING: You can brick your XIAO [and probably any Arduino device]!! If you inadvertently put the device in a short timeout loop, it may become impossible to upload a new image because the USB driver gets interrupted, over and over, causing it to repeatedly disconnect! This happened to me, and I was only able to save the thing because the watchdog timeout was set to 1 second (it took several tries). And, yeah, I get that it wasn’t actually “bricked” - but it could have been :wink::laughing:

The XIAO, in particular, is vulnerable to unstable code, in general, so when I’m developing, I add a 10 second delay to the Setup() function, as an “anti-brick” measure. 10 seconds is probably overkill, but haven’t found the time to titrate that to an optimal interval.