What if is necessary to use a watchdog for some applications using the Trinket M0 ?
In this article some notes about to do so from the Arduino environment.
What is the watchdog ?
A quick description of what is a watchdog.
Almost all the microcontrollers have a dedicated timer used for the watchdog function, the Trinket M0 has one.
A watchdog is basically a circuit (as said, a timer) that can be enabled and started.
Note that by default normally the watchdog is disabled.
If nothing is done to the timer (like stopping it or reloading it) when expire triggers a reset to the microcontroller.
In the running code thus is necessary to add a call to the watchdog with the purpose to renew the timer setting. If for any reason this call doesn't happens, maybe because the code failed or the micro freeze, the watchdog timer expires resetting the micro and thus restarting the code.
Use the watchdog - Arduino IDE
There are many different watchdog libraries out there, is necessary to find and install a specific one compatible with the specific MCU.
For the Trinket M0, the library to use is wdt_samd21 (see details in the github for the library)
First install the library in the Arduino IDE, then import it in the code two lines, one on the setup, just before to exit it :
// Setup watchdog - 8 seconds
wdt_init ( WDT_CONFIG_PER_8K );
The wdt_init starts the watchdog for a specific time.
In the example above, 8 seconds.
This what is possible to use :
WDT_CONFIG_PER_8 8 clock cycles ( 7.8 msec.)
WDT_CONFIG_PER_16 16 clock cycles (15.6 msec.)
WDT_CONFIG_PER_32 32 clock cycles (31.2 msec.)
WDT_CONFIG_PER_64 64 clock cycles (62.5 msec.)
WDT_CONFIG_PER_128 128 clock cycles ( 125 msec.)
WDT_CONFIG_PER_256 256 clock cycles ( 250 msec.)
WDT_CONFIG_PER_512 512 clock cycles ( 500 msec.)
WDT_CONFIG_PER_1K 1024 clock cycles ( 1 sec.)
WDT_CONFIG_PER_2K 2048 clock cycles ( 2 sec.)
WDT_CONFIG_PER_4K 4096 clock cycles ( 4 sec.)
WDT_CONFIG_PER_8K 8192 clock cycles ( 8 sec.)
WDT_CONFIG_PER_16K 16384 clock cycles (16 sec.)
Then in the loop section, just use :
wdt_reset();
To keep alive the watchdog timer.
Note
Programming the Trinket if the watchdog is active can be tricky!
Every time the watchdog expires it reset the Trinket, so if you connect it to the USB port and you can't program it BEFORE the watchdog expires, the micro will be reset blocking the programming.
In this case, follow this procedure :
- connect the micro to the USB
- press twice the reset button, this force the Trinket in bootload mode (the LED should become green) - the watchdog will be stopped
- program the micro
No comments:
Post a Comment