Uno R4 Wifi Watchdog Timer

Hey, i want to use the Watchdog Timer on my new Uno R4 Wifi. I've tried using the avr/wdt.h libary, but i got an error "No such file or directory" in the #include <avr/wdt.h> line. This makes sense to me because the r4 uses an renessas architecture unlike the r3. But how can i use the watchdog timer on the arduino R4?

Have you looked at the WatchdogRefresh example for the Uno R4 WiFi available in the IDE ?

2 Likes

I thought i looked in the examples, but i didn't see this one until now. Thank you!

I have the same problem with my Uno R4 Wifi.

Where can I find the WatchdogRefresh example? And which library?

Thanks for your time

In the IDE, I can find an example for the Watchdog on the ESP32. I assume this should work on the R4.

The example has: #include "esp_system.h"
but during complile on the ArduinoCloud it can not find the library, even though Cloud should have all libraries installed.
What am I missing? Am I looking at the wrong example?

In the IDE examples for the Uno R4 WiFi you will find a section entitled WDT and in there is a sketch named WatchdogRefresh. That is what I had in mind but I have no experience of using it

Thanks, I found the example and was able to add the WDT to my code.

Unfortunately, the WDT has a maximum timeout of 5592ms on the Uno R4.
This proves to be absolutely useless when using the Uno R4 on the ArduinoCloud as some cloud stuff takes longer than 5592ms (for example reconnecting to WiFi or sometimes even connecting to the ArduinoCloud).

If anyone has a better WDT solution for the Uno R4 that will keep it alive while it's doing the integrated WiFi and Cloud stuff, I would love to hear it.

Did you found a solution?

Yes I did.

I used this article to create a timer that runs once a second:
https://www.pschatzmann.ch/home/2023/07/01/under-the-hood-arduino-uno-r4-timers/

This timer even runs when Arduino is busy doing cloud stuff. So inside this timer I kick the watchdog to keep it alive.
Inside my main loop I set a counter to millis() so I can check inside the timer how long it's been since the main loop ran.

#include "FspTimer.h"
#include "WDT.h"

FspTimer background_timer;
unsigned long TimeoutCounter = 0;

void setup() {
  if (WDT.begin(5000)) {
    WDT.refresh();
  } else {
    Serial.println("Error initializing watchdog");
  }
  beginTimer(1);
}

void loop() {
  TimeoutCounter = millis();  // Kick the timeout counter to avoid reset.
}

void timer_callback(timer_callback_args_t __attribute((unused)) *p_args) {
  WDT.refresh();
  if (millis() > (TimeoutCounter + (1000 * 60 * 5))) NVIC_SystemReset(); // More than 5 minutes unresponsive
}

bool beginTimer(float rate) {
  uint8_t timer_type = GPT_TIMER;
  int8_t tindex = FspTimer::get_available_timer(timer_type);
  if (tindex < 0) {
    tindex = FspTimer::get_available_timer(timer_type, true);
  }
  if (tindex < 0) {
    return false;
  }

  FspTimer::force_use_of_pwm_reserved_timer();

  if (!background_timer.begin(TIMER_MODE_PERIODIC, timer_type, tindex, rate, 0.0f, timer_callback)) {
    return false;
  }

  if (!background_timer.setup_overflow_irq()) {
    return false;
  }

  if (!background_timer.open()) {
    return false;
  }

  if (!background_timer.start()) {
    return false;
  }
  return true;
}

Nice. Does it also reset the ESP32 when the watchdog triggers so it gets completely new connection?

Yes it does.

So does the NVIC_SystemReset() command that I call when the main loop is unresponsive

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.