Skip to content

Commit

Permalink
Merge pull request #33 from brentru/add-esp32-support
Browse files Browse the repository at this point in the history
Add support for ESP32
  • Loading branch information
brentru authored Aug 9, 2021
2 parents d9e5fc3 + 2ff9a16 commit 9d6f116
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Adafruit_SleepyDog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ typedef WatchdogKinetisLseries WatchdogType;
#elif defined(NRF52_SERIES)
#include "utility/WatchdogNRF.h"
typedef WatchdogNRF WatchdogType;
#elif defined(ARDUINO_ARCH_ESP32)
#include "utility/WatchdogESP32.h"
typedef WatchdogESP32 WatchdogType;
#else
#error Unsupported platform for the Adafruit Watchdog library!
#endif
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Currently supports the following hardware:
* Arduino Leonardo or other 32u4-based boards (e.g. Adafruit Feather) WITH CAVEAT: USB Serial connection is clobbered on sleep; if sketch does not require Serial comms, this is not a concern. The example sketches all print to Serial and appear frozen, but the logic does otherwise continue to run. You can restore the USB serial connection after waking up using `USBDevice.attach();` and then reconnect to USB serial from the host machine.
* Partial support for Teensy 3.X and LC (watchdog, no sleep).
* ATtiny 24/44/84 and 25/45/85
* ESP32, ESP32-S2
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Adafruit SleepyDog Library
version=1.4.0
version=1.5.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library to use the watchdog timer for system reset and low power sleep.
paragraph=Arduino library to use the watchdog timer for system reset and low power sleep.
category=Other
url=https://github.com/adafruit/Adafruit_SleepyDog
architectures=avr,samd,nrf52,teensy
architectures=avr,samd,nrf52,teensy, esp32
83 changes: 83 additions & 0 deletions utility/WatchdogESP32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#if defined(ARDUINO_ARCH_ESP32)

#include "WatchdogESP32.h"

/**************************************************************************/
/*!
@brief Initializes the ESP32's Task Watchdog Timer (TWDT) and
subscribes to the current running task.
@param maxPeriodMS
Timeout period of TWDT in seconds
@return The actual period (in milliseconds) before a watchdog timer
reset is returned. 0 otherwise.
*/
/**************************************************************************/
int WatchdogESP32::enable(int maxPeriodMS) {
if (maxPeriodMS < 0)
return 0;

// ESP32 expects TWDT in seconds
uint32_t maxPeriod = maxPeriodMS / 1000;
// Enable the TWDT and execute the esp32 panic handler when TWDT times out
esp_err_t err = esp_task_wdt_init(maxPeriod, true);
if (err != ESP_OK)
return 0; // Initialization failed due to lack of memory

// NULL to subscribe the current running task to the TWDT
err = esp_task_wdt_add(NULL);
if (err != ESP_OK)
return 0;

_wdto = maxPeriodMS;
return maxPeriodMS;
}

/**************************************************************************/
/*!
@brief Resets the Task Watchdog Timer (TWDT) on behalf
of the currently running task.
*/
/**************************************************************************/
void WatchdogESP32::reset() {
// NOTE: This blindly resets the TWDT and does not return the esp_err.
esp_task_wdt_reset();
}

/**************************************************************************/
/*!
@brief Disables the TWDT by unsubscribing the currently running task
from the TWDT.
*/
/**************************************************************************/
void WatchdogESP32::disable() { esp_task_wdt_delete(NULL); }

/**************************************************************************/
/*!
@brief Configures the ESP32 to enter a low-power sleep mode for a
desired amount of time.
@param maxPeriodMS
Time to sleep the ESP32, in millis.
@return The actual period (in milliseconds) that the hardware was
asleep will be returned. Otherwise, 0 will be returned if the
hardware could not enter the low-power mode.
*/
/**************************************************************************/
int WatchdogESP32::sleep(int maxPeriodMS) {
if (maxPeriodMS < 0)
return 0;
// Convert from MS to microseconds
uint64_t sleepTime = maxPeriodMS * 1000;
// Enable wakeup by timer
esp_err_t err = esp_sleep_enable_timer_wakeup(sleepTime);
if (err != ESP_OK) {
return 0; // sleepTime is out of range
}
// Enter light sleep with the timer wakeup option configured
err = esp_light_sleep_start();
if (err != ESP_OK) {
return 0; // ESP_ERR_INVALID_STATE if WiFi or BT is not stopped
}
return maxPeriodMS;
}

#endif // ARDUINO_ARCH_ESP32
38 changes: 38 additions & 0 deletions utility/WatchdogESP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
* @file WatchdogESP32.h
*
* Support for ESP32 TWDT and low-power sleep modes.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Brent Rubell for Adafruit Industries.
*
* MIT License, all text here must be included in any redistribution.
*
*/
#ifndef WATCHDOGESP32_H_
#define WATCHDOGESP32_H_
#include "esp_sleep.h"
#include "esp_task_wdt.h"

/**************************************************************************/
/*!
@brief Class that contains functions for interacting with the ESP32's
WDT and low-power sleep functions.
*/
/**************************************************************************/
class WatchdogESP32 {
public:
WatchdogESP32() : _wdto(-1){};
int enable(int maxPeriodMS = 0);
void reset();
void disable();
int sleep(int maxPeriodMS = 0);

private:
int _wdto;
};

#endif // WATCHDOGESP32_H

0 comments on commit 9d6f116

Please sign in to comment.