WDT_T4 - Watchdog Library for Teensy 4

tonton81

Well-known member
Since Paul found a patch for the reset I started working back on the watchdog interface. The previous version was only for watchdog1 (WDOG1). This redesigned model is templated, and works on ALL 3 watchdogs (WDOG1,WDOG2,RTWDOG(WDOG3)).
I also added Paul's fix to the library so it works on previous teensyduinos as well

I will shortly try to finish up the examples for all the 3 of the watchdogs. WDOG2 is slightly different than WDOG1 as it doesn't actually do an internal reset, and the callback is fired based on the watchdog feeding trigger threshold. However, WDOG1 and WDOG2 both can drive gpios LOW to reset external devices, only WDOG1 can do an internal reset.

The 3 watchdogs have different behaviours.

WDOG1 pins implemented and tested. Expected behaviour: after a watchdog reset (not callback fire), the pins are constantly driven LOW, a POR is needed to reset this state.
WDOG2 pins implemented and tested. Expected behaviour: on timeout (not callback fire), the GPIO goes LOW for a brief second by hardware and then restores GPIO state. T4 does not reset as WDOG2 doesn't do software reset, only GPIO. Not feeding the dog will trigger that pulse ONLY once, your callback will not refire, nor will Teensy reset or toggle the GPIO. When feeding the watchdog, the callback will re-fire and the GPIO will toggle again.

WDOG1 available pins: 19, 20
WDOG2 available pins: 13, 24

WDOG3 (RTWDOG) is different than the other two. It's based not on time, but cycles. To make this easier for the user, the LPO clock is 32KHz, with a 16bit timeout field. The library does the necessary calculations for a user to input timeout value between 32(ms) -> 522.232(seconds).
It features a windowMode, that when enabled, if you feed the dog faster than your threshold, it will reset teensy. It will still reset if the dog is NOT fed whether you use windowMode or not.
The callback for WDOG3 is also different from the other 2 watchdogs, it is not called earlier like a trigger, it's fired 255 cycles before the actual reset, so you have little time to collect diagnostic info before the reset happens.

I also added reset() to be able to reset the MCU immediately when requested.

https://github.com/tonton81/WDT_T4

EDIT, all 3 demos have been posted. WDOG2 demonstrates the GPIO usage, the config.pin can be applied to WDOG1 also for pins 19 or 20 only. WDOG3 pins not implemented yet.
 
Last edited:
Really cool that you got all three WDT's working in a library format will play with it soonish.
 
External Watchdog Monitor support has been added, with window mode support. The counters are 8 bits so your timeout can be anywhere between 7ms to 2 seconds. Window support is also supported to fire a callback if you fed the EWM too fast. The window must be as usual smaller than the timeout value. Will post example soon..

WDOG3 does not have any pins that I have found, but EWM has 2 available pin outputs available on T4, 21 and 25. The EWM_in pin however is unlisted in the datasheet in terms of GPIO support. Without this pin checked via hardware, a software pin polling would be necessary to manually feed() the dog, although that is not efficient, you could always use it as just another watchdog by feeding. The interrupt on this EWM module has no status and only fires once after assertion of the timeout (OR window) and remains set with/without the output of the pin until a reset is issued, reset() by software also will work. Basically the callback is fired only once and then immediately disabled, or else it will re-enter indefinately. You can still use your program as normal but the watchdog has done what it needs to do and you can manage your software and follow up with a reset() at a later point.

Also, to use EWM the imxrt.h has to be updated with the correct memory location, submitted an issue request

Code:
#define IMXRT_EWM		(*(IMXRT_REGISTER8_t *)0x402D8000) 

to

#define IMXRT_EWM		(*(IMXRT_REGISTER8_t *)0x400B4000)
 
Hi and thanks for your work on Teensy 4 board... :)

I have used the watchdog in several projects, always on controllers of the ATMEL family, and always in a simple way as in the following example:

#include<avr/wdt.h> /* Header for watchdog timers in AVR */
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
Serial.println("Watchdog Demo Starting");
pinMode(13, OUTPUT);
wdt_disable(); /* Disable the watchdog and wait for more than 2 seconds */
delay(3000); /* Done so that the Arduino doesn't keep resetting infinitely in case of wrong configuration */
wdt_enable(WDTO_2S); /* Enable the watchdog with a timeout of 2 seconds */
}

void loop() {
for(int i = 0; i<20; i++) /* Blink LED for some time */
{
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
wdt_reset(); /* Reset the watchdog */
}
while(1); /* Infinite loop. Will cause watchdog timeout and system reset. */
}

It's possibile to use your library in this classic and simple mode?
Can you give me a simple example of this kind of use?

Very thanks...
A:I:)
 
check out watchdog1 demo on github

https://github.com/tonton81/WDT_T4/blob/master/examples/watchdog1_demo/watchdog1_demo.ino

configuration is done in setup
trigger is how long before the watchdog callback fires
timeout is how long before not feeding will the watchdog reset

Hello again my friend, thanks for the tips... I had already seen your examples and I did some tests with the example you indicated.
I still have a question: how do I disable the watchdog?
How do I enable it with wdg.begin(config) is there any way to disable it? like with wdg.end()

Thanks...
A:I:)
 
watchdog cannot be disabled unless it is in debug mode, i will need to check the RF what that mode affects, if DBG mode hinders operational features then it wouldn't be good to add, however, maybe I can add the option for specific use case, but not default.
 
@tonton81: Thank you for putting together this library! I use a watchdog for most of my teensy 3.2 code, but that obviously didn't translate well for the teensy 4.x. This is a big help!!

Quick question: that is the role of the pin in the watchdog? I just tracked down an issue I was having where I was using serial5 on the 4.0, but then noticed the watchdog also used the same pin (21). Looking at the library I believe I'm OK changing to pin 25, but not exactly sure what the role of the pin is. Can you explain?

Thank you again!
David
 
the pin is used to drive the output should the watchdog be triggered. this allows the watchdog to reset an external board
 
That's a nice feature! if we don't need it for a particular application, is there a way to turn it off?
Thanks again,
David
 
if you dont set it, it is unused, just comment it out. Only valid watchdog pins would be set (provided you put in a matching pin), invalid ones or not set wont touch any pins
 
Last edited:
Hey, may be a stupid question about WDOG1:
Expected behaviour: after a watchdog reset (not callback fire), the pins are constantly driven LOW, a POR is needed to reset this state
is with POR meant a Power On Reset ?
 
yes, that is the expected behaviour for wdog1, that pin usually drives an external reset pin

also, after the watchdog reset, that pin is still driven LOW to make sure an external processor fully resets the system. Teensy will still boot and work fine after the wdt reset, despite that pin holding a LOW state

pretty sure last time i tested, replugging power to teensy deasserted the LOW signal, and also re-programming as well. Unless you have teensy as a sub processor of another system or integrated circuit, the pin is pretty much useless for you
 
Hi @tonton81,
I am trying to detect what the reset reason is for my teensy 4.0

I found this thread, but i doner get it yet. I also found this: https://forum.pjrc.com/threads/25370-Teensy-3-0-Watchdog-Timer?p=46299&viewfull=1#post46299
But that seems Teensy 3.0 specific. Or can I actually use this code for the 4.0 as well.

I am unclear on how to do the same thing on the Teensy 4.0. What I want to do, it to detect the reason for the reset, and then modify the setup behaviour accordingly.
Thanks for pointing me in the right direction,
Robert
 
I believe you should goto a system register, paul posted it somewhere, im not at home. it shows whether it was a POR (full repower or after programming), temperature, or wdt reset. i believe the WDT register also shows 2 of the 3 states
 
@tonton81: Thank you for putting together this library! I use a watchdog for most of my teensy 3.2 code, but that obviously didn't translate well for the teensy 4.x. This is a big help!!

David

Does this library work for teensy 4.0? If not, what else is available? I just want to do like a.hillingeri, in a simple way as for Arduino ATMega projects, but using my teensy 4.0.Thanx.
 
See post #1

Sorry about the French below " ", but when I compile, it says, "CAREFUL: the WDT_T4-master library suggests that it can be executed with teensy architecture and it may be incompatible with your actual board executed on avr. I have not yet uploaded onto my teensy 4.0 yet. I'm using Teensyduino 1.55 & Arduino 1.8.13 software versions.

"
ATTENTION : la bibliothèque WDT_T4-master prétend être exécutable sur la (ou les) architecture(s) teensy et peut être incompatible avec votre carte actuelle qui s'exécute sur avr."
Opening Teensy Loader...
 
never heard of that error before, make sure you have teensy selected as the board

The watchdog works fine but I still get that warning: "ATTENTION : la bibliothèque WDT_T4-master prétend être exécutable sur la (ou les) architecture(s) teensy et peut être incompatible avec votre carte actuelle qui s'exécute sur avr." It's not a problem but slightly annoying if I come back in 6 months & forget that this is a "working warning"

I put the library here: library.jpg
 
still that error makes no sense if you have it set to teensy T4.x. maybe its because it's a french IDE having incompatibilities with teensyduino? nothing in the library is avr related
 
still that error makes no sense if you have it set to teensy T4.x. maybe its because it's a french IDE having incompatibilities with teensyduino? nothing in the library is avr related

It doesn't make sense and it works so I'll just let it ride. I thought I maybe had done something wrong but it doesn't seem to be so. Thanx anyway!
 
Back
Top