Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: WDT_T4 - Watchdog Library for Teensy 4

  1. #1
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464

    WDT_T4 - Watchdog Library for Teensy 4

    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 by tonton81; 01-26-2020 at 05:43 PM.

  2. #2
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    5,894
    Really cool that you got all three WDT's working in a library format will play with it soonish.

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    7,074
    A library that I will use often.
    Thank you very much.

  4. #4
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464
    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)

  5. #5
    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

  6. #6
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464
    check out watchdog1 demo on github

    https://github.com/tonton81/WDT_T4/b...hdog1_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

  7. #7
    Quote Originally Posted by tonton81 View Post
    check out watchdog1 demo on github

    https://github.com/tonton81/WDT_T4/b...hdog1_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

  8. #8
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464
    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.

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Location
    Los Angeles
    Posts
    125
    @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

  10. #10
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464
    the pin is used to drive the output should the watchdog be triggered. this allows the watchdog to reset an external board

  11. #11
    Senior Member
    Join Date
    Nov 2012
    Location
    Los Angeles
    Posts
    125
    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

  12. #12
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,464
    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 by tonton81; 06-23-2020 at 06:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •