The wait function does not trigger the watchdog?


  • Plugin Developer

    I wanted to do a quick test of the watchdog function on the serial gateway.

    So originally I had a variable that would increment, and then make a wait() function take increasingly long. But to my surprise, this didn't trigger the watchdog.

    #include <MySensors.h>                              // The MySensors library, which takes care of creating the wireless network.
    #include <avr/wdt.h>                                // The watchdog timer - if the device becomes unresponsive and doesn't periodically reset the timer, then it will automatically reset once the timer reaches 0.
    
    // Clock for the watchdog
    #define INTERVAL 1000                               // Every second we reset the watchdog timer. If the device freezes, the watchdog will not re reset, and the device will reboot.
    unsigned long previousMillis = 0;                   // Used to run the internal clock
    
    int boogyman = 0;
    
    void setup()
    {
      Serial.begin(115200);                             // For serial debugging over USB.
      Serial.println(F("Hello, I am a Candle receiver"));
    
      wdt_enable(WDTO_2S);                              // Starts the watchdog timer. If it is not reset once every 2 seconds, then the entire device will automatically restart.                                 
    }
    
    void presentation()
    {
    	// The receiver does not have any extra children itself.
    }
    
    void loop()
    {
      if(millis() - previousMillis >= INTERVAL){        // Main loop, runs every second.
        previousMillis = millis();                      // Store the current time as the previous measurement start time.
        Serial.print("resetting watchdog. Boogyman:"); Serial.println(boogyman);
        wdt_reset();                                    // Reset the watchdog timer
        wait(boogyman);
        boogyman = boogyman + 300;
      }
                                  
      //delay(boogyman);
    }
    

    Even if the increasing variable would theoretically call the wait function for more than 2 seconds (the watchdog timeout), it just kept on increasing.

    if I changed the wait() into a delay(), it would actually trigger the watchdog.

    So, I'm curious: how does the wait function work? The Arduino can't do multi-tasking, right? So how is it possible that the loop that runs every second is still able to keep the watchdog at bay?

    While I'm at it, I am also curious how the MySensors library integrates itself into the Arduino. It seems to manage the messages in the background perfectly well. Is that because the 'interupt' acts like a very basic multi-tasking?

    I was under the assuption that using the loop above, this loop would never run 'paralel'. Is that assumption still correct?


  • Mod

    @alowhum most of these questions are way too advanced for me to answer, but I know that wait() calls _process() which processes messages and kicks the watchdog (through doYield()). See https://github.com/mysensors/MySensors/blob/development/core/MySensorsCore.cpp#L565

    while (hwMillis() - enteringMS < waitingMS) {
    _process();
    }

    By default, nrf24-based nodes do not use interrupts for message handling.

    There is nothing done in parallell.


 

297
Online

8.9k
Users

9.7k
Topics

101.2k
Posts