-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from brentru/add-esp32-support
Add support for ESP32
- Loading branch information
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |