Skip to content

Commit

Permalink
Merge pull request #35 from brentru/add-esp8266
Browse files Browse the repository at this point in the history
Add ESP8266 Support
  • Loading branch information
brentru authored Dec 3, 2021
2 parents 9d6f116 + a5e5b6a commit b8d2b35
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Adafruit_SleepyDog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* 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).
* ESP32/ESP32-S2
* ESP8266
*
* Adafruit Trinket and other boards using ATtiny MCUs are NOT supported.
*/
Expand Down
3 changes: 3 additions & 0 deletions Adafruit_SleepyDog.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ typedef WatchdogNRF WatchdogType;
#elif defined(ARDUINO_ARCH_ESP32)
#include "utility/WatchdogESP32.h"
typedef WatchdogESP32 WatchdogType;
#elif defined(ARDUINO_ARCH_ESP8266)
#include "utility/WatchdogESP8266.h"
typedef WatchdogESP8266 WatchdogType;
#else
#error Unsupported platform for the Adafruit Watchdog library!
#endif
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Currently supports the following hardware:
* Partial support for Teensy 3.X and LC (watchdog, no sleep).
* ATtiny 24/44/84 and 25/45/85
* ESP32, ESP32-S2
* ESP8266 WITH CAVEAT: The software and hardware watchdog timers are fixed to specific
intervals and not programmable. Notes about this are within the `utility/WatchdogESP8266.cpp` file.
Empty file.
Empty file.
18 changes: 13 additions & 5 deletions examples/BasicUsage/BasicUsage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

void setup() {
Serial.begin(115200);
while(!Serial) delay(10);
while (!Serial)
delay(10);
// wait for Arduino Serial Monitor (native USB boards)

Serial.println("Adafruit Watchdog Library Demo!");
Expand All @@ -29,8 +30,9 @@ void setup() {

// Now loop a few times and periodically reset the watchdog.
Serial.println("Looping ten times while resetting the watchdog...");
for(int i = 1; i <= 10; ++i) {
Serial.print("Loop #"); Serial.println(i, DEC);
for (int i = 1; i <= 10; ++i) {
Serial.print("Loop #");
Serial.println(i, DEC);
delay(1000);
// Reset watchdog with every loop to make sure the sketch keeps running.
// If you comment out this call watch what happens in about 4 iterations!
Expand All @@ -41,7 +43,7 @@ void setup() {
#ifndef NRF52_SERIES // cannot disable nRF's WDT
// Disable the watchdog entirely by calling Watchdog.disable();
Watchdog.disable();
#endif
#endif

// Finally demonstrate the watchdog resetting by enabling it for a shorter
// period of time and waiting a long time without a reset. Notice you can
Expand All @@ -54,7 +56,13 @@ void setup() {
Serial.print(countdownMS, DEC);
Serial.println(" milliseconds!");
Serial.println();
delay(countdownMS+1000);
#ifndef ARDUINO_ARCH_ESP8266
delay(countdownMS + 1000);
#else
// Calls to delay() and yield() feed the ESP8266's
// hardware and software watchdog timers, delayMicroseconds does not.
delayMicroseconds(countdownMS * 1000);
#endif

// Execution will never get here because the watchdog resets the Arduino!
}
Expand Down
Empty file removed examples/Sleep/.esp32.test.skip
Empty file.
Empty file removed examples/Sleep/.esp8266.test.skip
Empty file.
Empty file removed examples/Sleep/.nrf52840.test.skip
Empty file.
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.5.0
version=1.6.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, esp32
architectures=avr,samd,nrf52,teensy,esp32,esp8266
76 changes: 76 additions & 0 deletions utility/WatchdogESP8266.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#if defined(ARDUINO_ARCH_ESP8266)

#include "WatchdogESP8266.h"

/**********************************************************************************************/
/*!
@brief Initializes the ESP8266's software WDT
@param maxPeriodMS
Timeout period of WDT in milliseconds
@return The actual period (in milliseconds) before a watchdog timer
reset is returned, 0 otherwise.
NOTE: Configuring the software WDT timeout maxPeriodMS value is NOT
IMPLEMENTED in the ESP8266 BSP [1]. Further, an investigation into the
default software WDT time yielded a fixed timeout period of 3.2 seconds [2].
[1] https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Esp.h#L91
[2]https://sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html#ESP8266_WDT_TIMEOUT
*/
/**********************************************************************************************/
int WatchdogESP8266::enable(int maxPeriodMS) {
if (maxPeriodMS < 0)
return 0;

// Enable the WDT
ESP.wdtEnable(0);

_wdto = maxPeriodMS;
return maxPeriodMS;
}

/**************************************************************************/
/*!
@brief Feeds the Watchdog timer.
NOTE: Calling yield() or delay() also feeds the hardware and software
watchdog timers.
*/
/**************************************************************************/
void WatchdogESP8266::reset() { ESP.wdtFeed(); }

/**************************************************************************/
/*!
@brief Disables the Watchdog Timer.
NOTE: Please don't stop the software WDT too long
(less than 6 seconds), otherwise it will trigger a hardware
watchdog reset!
*/
/**************************************************************************/
void WatchdogESP8266::disable() { ESP.wdtDisable(); }

/**************************************************************************/
/*!
@brief Configures the ESP8266 to enter a low-power sleep mode for a
desired amount of time.
@param maxPeriodMS
Time to sleep the ESP8266, 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 WatchdogESP8266::sleep(int maxPeriodMS) {
if (maxPeriodMS < 0)
return 0;
// Convert from MS to microseconds
uint64_t sleepTime = maxPeriodMS * 1000;

// Assert that we can not sleep longer than the max. time calculated by ESP
if (sleepTime > ESP.deepSleepMax())
return 0;

// Enters deep sleep with mode WAKE_RF_DEFAULT
ESP.deepSleep(sleepTime);

return maxPeriodMS;
}

#endif // ARDUINO_ARCH_ESP8266
40 changes: 40 additions & 0 deletions utility/WatchdogESP8266.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
* @file WatchdogESP8266.h
*
* Support for ESP8266 WDT 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 WATCHDOGESP8266_H_
#define WATCHDOGESP8266_H_

// #include "esp_sleep.h"
// #include "esp_task_wdt.h"
#include "Esp.h"

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

private:
int _wdto;
};

#endif // WATCHDOGESP8266_H

0 comments on commit b8d2b35

Please sign in to comment.