Watchdog Timer (ESP32)

 

// ESP32_WDT_1.ino, Study set timer0,1,2,3 as Watchdog Timer

hw_timer_t *watchdogTimer = NULL;
long looptime = 0;
byte myTimer = 3; //0 | 1 | 2 |3
byte LED0 = 16;
bool state = LOW;

void setup()
{
Serial.begin(115200);
pinMode(LED0, OUTPUT);

//Blink LED0 to show system starting up!!!
for(byte i=0;i < 8;i++){
digitalWrite(LED0, state);
delay(100);
state = !state;
Serial.print(” .. “);
}

Serial.println();
Serial.println(“Setting timer in setup”);

//from 0 to 3, since we have 4 hardware timers),
//the value of the prescaler and a flag indicating
//if the counter should count up (true) or down (false).
watchdogTimer = timerBegin(myTimer, 80, true); //timer 0 divisor 80

//timerAlarmWrite(hw_timer_t *timer, uint64_t alarm_value, bool autoreload)”
//alarm_value: we set it to 1000000 = 1 us.
//autoreload: if it is true, timer will repeat.
timerAlarmWrite(watchdogTimer, 2000000, false); // set time in us must be fed within this time or reboot

//timerAttachInterrupt(hw_timer_t *timer, void (*fn)(void), bool edge)”
//fn: is the callback function that will be invoked when timer timeout and timer interrupt will be invoked.
//edge: if it is true, an alarm will generate an edge type interrupt.
timerAttachInterrupt(watchdogTimer, &interruptReboot, true);

timerAlarmEnable(watchdogTimer); // enable interrupt
}

void loop()
{
//Lab test by setting timerAlarmWrite(watchdogTimer, 900000, false);
timerWrite(watchdogTimer, 0); // reset timer feed dog
looptime = millis();
Serial.println(“Main Loop Running”);
delay(999);
looptime = millis() – looptime;
Serial.print(“Time in loop: “);
Serial.print(looptime);
Serial.println(“ms \n\n”);

state = !state; //Toggle state
digitalWrite(LED0, state);
}

void interruptReboot()
{
esp_restart_noos();
}

// ############## END ESP32_WDT_1.ino ##############

// ESP32_WDT_2.ino, Study set timer0 = Watchdog Timer
// set Timer1 = Sampling for Toggle LED

#define LED0 16

hw_timer_t *watchdogTimer = NULL;
hw_timer_t *timerSampling = NULL;

long looptime = 0;
byte myTimerWDT = 0;
byte Timer1 = 1;
bool state = LOW;

void onTimer(){
timerWrite(watchdogTimer, 0);
digitalWrite(LED0, !digitalRead(LED0));
}

void setup()
{
Serial.begin(115200);
pinMode(LED0, OUTPUT);

//Blink LED0 to show system starting up!!!
for(byte i=0;i < 8;i++){
digitalWrite(LED0, state);
delay(100);
state = !state;
Serial.print(” .. “);
}

Serial.println();
Serial.println(“Setting timer in setup”);

//setup Watchdog Timer
watchdogTimer = timerBegin(myTimerWDT, 80, true);
timerAlarmWrite(watchdogTimer, 2000000, false);
timerAttachInterrupt(watchdogTimer, &interruptReboot, true);
timerAlarmEnable(watchdogTimer); //End setup WDT

//setup Timer1 for Sampling mode (Interrupt)
timerSampling = timerBegin(Timer1, 80, true);
timerAlarmWrite(timerSampling, 1000000, true);
timerAttachInterrupt(timerSampling, &onTimer, true);
timerAlarmEnable(timerSampling); //End setup Timer1
}

void loop(){
//to Test WDT function by setting
//timerAlarmWrite(watchdogTimer, 900000, false);
}

void interruptReboot()
{
esp_restart_noos();
}

// ######### END ESP32_WDT_2.ino ###