Watchdog for esp32


#1

Hi David ,

I think that in the embedded world , its a good thing to have a watchdog.
Do you think it’s usefull for the esp32 ?
I found an example for the esp32. Maybe you could implement it in ulisp ?
I was thinking of (watchdog-enable) , (watchdog-timeout ‘milliseconds’) , (watchdog-reset)

Below is the example.

Kind regards ,
Ronny Suy

#include "esp_system.h"

const int button = 0;         //gpio to use to trigger delay
const int wdtTimeout = 3000;  //time in ms to trigger the watchdog
hw_timer_t *timer = NULL;

void IRAM_ATTR resetModule() {
  ets_printf("reboot\n");
  esp_restart();
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("running setup");

  pinMode(button, INPUT_PULLUP);                    //init control pin
  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  timerAlarmEnable(timer);                          //enable interrupt
}

void loop() {
  Serial.println("running main loop");

  timerWrite(timer, 0); //reset timer (feed watchdog)
  long loopTime = millis();
  //while button is pressed, delay up to 3 seconds to trigger the timer
  while (!digitalRead(button)) {
    Serial.println("button pressed");
    delay(500);
  }
  delay(1000); //simulate work
  loopTime = millis() - loopTime;
  
  Serial.print("loop time is = ");
  Serial.println(loopTime); //should be under 3000
}

#2

Hi Ronny,
Thanks for the suggestion.

So timerWrite(timer, 0) tickles the timer? If it reaches wdtTimeout without being tickled resetModule() is called?

Does a timeout have to restart the ESP32, or could it just call a function and then carry on?

What is ets_printf?

Would we need (watchdog-enable)? Couldn’t (watchdog-timeout ‘milliseconds’) automatically enable the watchdog?

What would its application be in uLisp?

Thanks,
David


#3

Hi Ronny,
Thanks for the suggestion.

So timerWrite(timer, 0) tickles the timer? If it reaches wdtTimeout without being tickled resetModule() is called? -> yes indeed

Does a timeout have to restart the ESP32, or could it just call a function and then carry on? -> I think its best to reset the core , just as in the example

What is ets_printf ? -> no idea :-( , I think its debug info only

Would we need (watchdog-enable) ? Couldn’t (watchdog-timeout ‘milliseconds’) automatically enable the watchdog?
-> yes that’s ok

What would its application be in uLisp?
-> this way a program that get’s stuck for some reason resets the core , so no hangups are possible. In my former company I used it a lot on avr chips to prevent lock-ups on erratic situations.

Ronny.


#4

However in uLisp if you reset the ESP32 you’d lose all your functions and variables. How would you get around that?


#5

David ,

That’s no problem at all.
The core resets , the program is read back from sdcard or eeprom , and it starts again.
If variables are lost that’s ok , the main issue is that a reset is done.
Of course the program has to be stored with (save-image 'name).