From 5e9c710f6ba5ac8192198302a715f14a82218e48 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 11:34:02 -0500 Subject: [PATCH 1/6] add ESP8266 interface for sleepydog --- Adafruit_SleepyDog.cpp | 2 + Adafruit_SleepyDog.h | 3 ++ README.md | 1 + examples/BasicUsage/.esp32.test.skip | 0 library.properties | 4 +- utility/WatchdogESP8266.cpp | 72 ++++++++++++++++++++++++++++ utility/WatchdogESP8266.h | 40 ++++++++++++++++ 7 files changed, 120 insertions(+), 2 deletions(-) delete mode 100644 examples/BasicUsage/.esp32.test.skip create mode 100644 utility/WatchdogESP8266.cpp create mode 100644 utility/WatchdogESP8266.h diff --git a/Adafruit_SleepyDog.cpp b/Adafruit_SleepyDog.cpp index ad8feed..bdf2663 100644 --- a/Adafruit_SleepyDog.cpp +++ b/Adafruit_SleepyDog.cpp @@ -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. */ diff --git a/Adafruit_SleepyDog.h b/Adafruit_SleepyDog.h index e97e5b8..00814a5 100644 --- a/Adafruit_SleepyDog.h +++ b/Adafruit_SleepyDog.h @@ -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 diff --git a/README.md b/README.md index 140542e..31c2f7f 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ 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 diff --git a/examples/BasicUsage/.esp32.test.skip b/examples/BasicUsage/.esp32.test.skip deleted file mode 100644 index e69de29..0000000 diff --git a/library.properties b/library.properties index 009bb32..7d15e9d 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=Adafruit SleepyDog Library -version=1.5.0 +version=1.6.0 author=Adafruit maintainer=Adafruit 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 diff --git a/utility/WatchdogESP8266.cpp b/utility/WatchdogESP8266.cpp new file mode 100644 index 0000000..ad185e0 --- /dev/null +++ b/utility/WatchdogESP8266.cpp @@ -0,0 +1,72 @@ +#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. +*/ +/**************************************************************************/ +int WatchdogESP8266::enable(int maxPeriodMS) { + ESP.wdtDisable(); + if (maxPeriodMS < 0) + return 0; + + // Enable the WDT + ESP.wdtEnable((uint32_t) maxPeriodMS); + + _wdto = maxPeriodMS; + return maxPeriodMS; +} + +/**************************************************************************/ +/*! + @brief Feeds the Watchdog Timer. +*/ +/**************************************************************************/ +void WatchdogESP8266::reset() { + ESP.wdtFeed(); +} + +/**************************************************************************/ +/*! + @brief Disables the Watchdog Timer. + NOTE: Please don't stop software watchdog too long + (less than 6 seconds), otherwise it will trigger the hardware + watchdog reset. +*/ +/**************************************************************************/ +void WatchdogESP8266::disable() { ESP.wdtDisable(); } + +/**************************************************************************/ +/*! + @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 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 \ No newline at end of file diff --git a/utility/WatchdogESP8266.h b/utility/WatchdogESP8266.h new file mode 100644 index 0000000..dfb42de --- /dev/null +++ b/utility/WatchdogESP8266.h @@ -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 ESP32'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 // WATCHDOGESP32_H \ No newline at end of file From aefe63a1af435fbfe946fbe861a54a3846560f52 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 11:38:24 -0500 Subject: [PATCH 2/6] clang-format --- utility/WatchdogESP8266.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utility/WatchdogESP8266.cpp b/utility/WatchdogESP8266.cpp index ad185e0..3046cb5 100644 --- a/utility/WatchdogESP8266.cpp +++ b/utility/WatchdogESP8266.cpp @@ -17,7 +17,7 @@ int WatchdogESP8266::enable(int maxPeriodMS) { return 0; // Enable the WDT - ESP.wdtEnable((uint32_t) maxPeriodMS); + ESP.wdtEnable((uint32_t)maxPeriodMS); _wdto = maxPeriodMS; return maxPeriodMS; @@ -28,9 +28,7 @@ int WatchdogESP8266::enable(int maxPeriodMS) { @brief Feeds the Watchdog Timer. */ /**************************************************************************/ -void WatchdogESP8266::reset() { - ESP.wdtFeed(); -} +void WatchdogESP8266::reset() { ESP.wdtFeed(); } /**************************************************************************/ /*! From d03cd2aaaa1570389602da99fd911c12e4c431c8 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 13:27:33 -0500 Subject: [PATCH 3/6] add information regarding software timeout and how it actually works --- utility/WatchdogESP8266.cpp | 13 +++++++++---- utility/WatchdogESP8266.h | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/utility/WatchdogESP8266.cpp b/utility/WatchdogESP8266.cpp index 3046cb5..f9e1dc2 100644 --- a/utility/WatchdogESP8266.cpp +++ b/utility/WatchdogESP8266.cpp @@ -2,22 +2,27 @@ #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. + 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) { ESP.wdtDisable(); if (maxPeriodMS < 0) return 0; // Enable the WDT - ESP.wdtEnable((uint32_t)maxPeriodMS); + ESP.wdtEnable(0); _wdto = maxPeriodMS; return maxPeriodMS; diff --git a/utility/WatchdogESP8266.h b/utility/WatchdogESP8266.h index dfb42de..a677451 100644 --- a/utility/WatchdogESP8266.h +++ b/utility/WatchdogESP8266.h @@ -21,8 +21,8 @@ /**************************************************************************/ /*! - @brief Class that contains functions for interacting with the ESP32's - WDT and low-power sleep functions. + @brief Class that contains functions for interacting with the + ESP8266's WDT and low-power sleep functions. */ /**************************************************************************/ class WatchdogESP8266 { From ea6155d5c057da8ccdcc54b8e942e1ea6eb9aeae Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 13:49:37 -0500 Subject: [PATCH 4/6] add esp8266 comments and specfic code --- examples/BasicUsage/.esp8266.test.skip | 0 examples/BasicUsage/BasicUsage.ino | 18 +++++++++++++----- examples/Sleep/.esp32.test.skip | 0 examples/Sleep/.esp8266.test.skip | 0 examples/Sleep/.nrf52840.test.skip | 0 utility/WatchdogESP8266.cpp | 11 ++++++----- 6 files changed, 19 insertions(+), 10 deletions(-) delete mode 100644 examples/BasicUsage/.esp8266.test.skip delete mode 100644 examples/Sleep/.esp32.test.skip delete mode 100644 examples/Sleep/.esp8266.test.skip delete mode 100644 examples/Sleep/.nrf52840.test.skip diff --git a/examples/BasicUsage/.esp8266.test.skip b/examples/BasicUsage/.esp8266.test.skip deleted file mode 100644 index e69de29..0000000 diff --git a/examples/BasicUsage/BasicUsage.ino b/examples/BasicUsage/BasicUsage.ino index 06df4b3..edc48de 100644 --- a/examples/BasicUsage/BasicUsage.ino +++ b/examples/BasicUsage/BasicUsage.ino @@ -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!"); @@ -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! @@ -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 @@ -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! } diff --git a/examples/Sleep/.esp32.test.skip b/examples/Sleep/.esp32.test.skip deleted file mode 100644 index e69de29..0000000 diff --git a/examples/Sleep/.esp8266.test.skip b/examples/Sleep/.esp8266.test.skip deleted file mode 100644 index e69de29..0000000 diff --git a/examples/Sleep/.nrf52840.test.skip b/examples/Sleep/.nrf52840.test.skip deleted file mode 100644 index e69de29..0000000 diff --git a/utility/WatchdogESP8266.cpp b/utility/WatchdogESP8266.cpp index f9e1dc2..8f937e4 100644 --- a/utility/WatchdogESP8266.cpp +++ b/utility/WatchdogESP8266.cpp @@ -17,7 +17,6 @@ */ /**********************************************************************************************/ int WatchdogESP8266::enable(int maxPeriodMS) { - ESP.wdtDisable(); if (maxPeriodMS < 0) return 0; @@ -30,7 +29,9 @@ int WatchdogESP8266::enable(int maxPeriodMS) { /**************************************************************************/ /*! - @brief Feeds the Watchdog Timer. + @brief Feeds the Watchdog timer. + NOTE: Calling yield() or delay() also feeds the hardware and software + watchdog timers. */ /**************************************************************************/ void WatchdogESP8266::reset() { ESP.wdtFeed(); } @@ -38,9 +39,9 @@ void WatchdogESP8266::reset() { ESP.wdtFeed(); } /**************************************************************************/ /*! @brief Disables the Watchdog Timer. - NOTE: Please don't stop software watchdog too long - (less than 6 seconds), otherwise it will trigger the hardware - watchdog reset. + 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(); } From 2a69d4c5f72da65f6c580ccb8ade5ecaf673a550 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 13:51:17 -0500 Subject: [PATCH 5/6] add caveat to readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 31c2f7f..945bc17 100644 --- a/README.md +++ b/README.md @@ -12,4 +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 +* 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. From a5e5b6af8d1ac2a3776dce5b8c184560fb758dea Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 3 Dec 2021 13:59:51 -0500 Subject: [PATCH 6/6] remove refs to 32 --- utility/WatchdogESP8266.cpp | 4 ++-- utility/WatchdogESP8266.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/utility/WatchdogESP8266.cpp b/utility/WatchdogESP8266.cpp index 8f937e4..75bba9a 100644 --- a/utility/WatchdogESP8266.cpp +++ b/utility/WatchdogESP8266.cpp @@ -48,10 +48,10 @@ void WatchdogESP8266::disable() { ESP.wdtDisable(); } /**************************************************************************/ /*! - @brief Configures the ESP32 to enter a low-power sleep mode for a + @brief Configures the ESP8266 to enter a low-power sleep mode for a desired amount of time. @param maxPeriodMS - Time to sleep the ESP32, in millis. + 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. diff --git a/utility/WatchdogESP8266.h b/utility/WatchdogESP8266.h index a677451..009ec82 100644 --- a/utility/WatchdogESP8266.h +++ b/utility/WatchdogESP8266.h @@ -37,4 +37,4 @@ class WatchdogESP8266 { int _wdto; }; -#endif // WATCHDOGESP32_H \ No newline at end of file +#endif // WATCHDOGESP8266_H \ No newline at end of file