Skip to content

Instantly share code, notes, and snippets.

@PRosenb
Last active April 6, 2019 04:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PRosenb/83c531210b59ef44a8bd9bd28c49f5fb to your computer and use it in GitHub Desktop.
Save PRosenb/83c531210b59ef44a8bd9bd28c49f5fb to your computer and use it in GitHub Desktop.
When using ESP32 with the Arduino IDE, it does not support the native watchdog timer. The following gist does not use the official watchdog timer but employs a normal timer to reset the CPU if wdt_reset() is not called in time.
#include "esp32_wdt.h"
#include <esp32-hal-timer.h>
static hw_timer_t *timer = NULL;
/**
* iInterrupt service routine called when the timer expires.
*/
void IRAM_ATTR resetModule() {
ets_printf("watchdog reboot\n");
esp_restart_noos();
}
void wdt_enable(const unsigned long durationMs) {
//timer 0, div 80
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &resetModule, true);
//set time in us
timerAlarmWrite(timer, durationMs * 1000, false);
//enable interrupt
timerAlarmEnable(timer);
}
void wdt_disable() {
if (timer != NULL) {
//disable interrupt
timerDetachInterrupt(timer);
timerEnd(timer);
timer = NULL;
}
}
void wdt_reset() {
//reset timer (feed watchdog)
if (timer != NULL) {
timerWrite(timer, 0);
}
}
/**
When using ESP32 with the Arduino IDE, it does not support the native watchdog timer.
The following watchdog implementation does not use the official watchdog timer
but employs a normal timer to reset the CPU if wdt_reset() is not called in time.
Inspired by the following discussion:
https://github.com/espressif/arduino-esp32/issues/841
Credit goes to @me-no-dev.
*/
#ifndef WDT_H
#define WDT_H
/**
Enable the watchdog timer, configuring it for expiry
after durationMs milliseconds.
*/
void wdt_enable(const unsigned long durationMs);
/**
Disable the watchdog timer.
*/
void wdt_disable();
/**
Reset the watchdog timer. When the watchdog timer is enabled,
a call to this method is required before the timer expires,
otherwise a watchdog-initiated device reset will occur.
*/
void wdt_reset();
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment