Skip to content

Commit

Permalink
Add neverSleep and neverSleepOff to Esp class, use them in core and l…
Browse files Browse the repository at this point in the history
…ibs where feasible.
  • Loading branch information
dok-net committed Mar 7, 2023
1 parent 84145db commit f783789
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
18 changes: 18 additions & 0 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ void EspClass::autoSleepOff() {
saved_sleep_type = NONE_SLEEP_T;
}

void EspClass::neverSleep() {
const auto active_sleep_type = wifi_get_sleep_type();
if (NONE_SLEEP_T == active_sleep_type) {
return;
}
wifi_fpm_close();
saved_sleep_type = active_sleep_type;
wifi_set_sleep_type(NONE_SLEEP_T);
}

void EspClass::neverSleepOff() {
if (NONE_SLEEP_T == saved_sleep_type) {
return;
}
wifi_set_sleep_type(saved_sleep_type);
saved_sleep_type = NONE_SLEEP_T;
}

/*
Layout of RTC Memory is as follows:
Ref: Espressif doc 2C-ESP8266_Non_OS_SDK_API_Reference, section 3.3.23 (system_rtc_mem_write)
Expand Down
6 changes: 6 additions & 0 deletions cores/esp8266/Esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class EspClass {
/// it would have to be restored explicitly.
static void autoSleepOff();

static void neverSleep();
/// Any prior sleep type is restored, but only as automatic.
/// If any forced sleep mode was effective before neverSleep,
/// it would have to be restored explicitly.
static void neverSleepOff();

static bool rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
static bool rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size);

Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
_md5 = MD5Builder();

#ifndef HOST_MOCK
wifi_set_sleep_type(NONE_SLEEP_T);
ESP.neverSleep();
#endif

//address where we will start writing the update
Expand Down
4 changes: 4 additions & 0 deletions doc/libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Some ESP-specific APIs related to the deep, modem, and light sleep modes, RTC an

``ESP.autoSleepOff()`` returns the chip to the automatic sleep mode that was effective before the preceding call to either ``ESP.autoModemSleep`` or ``ESP.autoLightSleep``.

``ESP.neverSleep()`` immediately puts the chip into ``NONE_SLEEP`` mode.

``ESP.neverSleepOff()`` returns the chip to any automatic sleep mode that was effective before the preceding call to ``ESP.neverSleep``.

``ESP.rtcUserMemoryWrite(offset, &data, sizeof(data))`` and ``ESP.rtcUserMemoryRead(offset, &data, sizeof(data))`` allow data to be stored in and retrieved from the RTC user memory of the chip respectively. ``offset`` is measured in blocks of 4 bytes and can range from 0 to 127 blocks (total size of RTC memory is 512 bytes). ``data`` should be 4-byte aligned. The stored data can be retained between deep sleep cycles, but might be lost after power cycling the chip. Data stored in the first 32 blocks will be lost after performing an OTA update, because they are used by the Core internals.

``ESP.restart()`` restarts the CPU.
Expand Down
12 changes: 12 additions & 0 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type, uint8_t listenI
return ret;
}

bool ESP8266WiFiGenericClass::setSleep(bool enable) {
if (enable)
{
ESP.neverSleepOff();
}
else
{
ESP.neverSleep();
}
return true;
}

/**
* get Sleep mode
* @return sleep_type_t
Expand Down
14 changes: 2 additions & 12 deletions libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,11 @@ class ESP8266WiFiGenericClass {

bool setSleepMode(WiFiSleepType_t type, uint8_t listenInterval = 0);
/**
* Set modem sleep mode (ESP32 compatibility)
* Set ESP866 to never sleep or return to previous mode (ESP32 compatibility)
* @param enable true to enable
* @return true if succeeded
*/
bool setSleep(bool enable)
{
if (enable)
{
return setSleepMode(WIFI_MODEM_SLEEP);
}
else
{
return setSleepMode(WIFI_NONE_SLEEP);
}
}
bool setSleep(bool enable);
/**
* Set sleep mode (ESP32 compatibility)
* @param mode wifi_ps_type_t
Expand Down
6 changes: 6 additions & 0 deletions tests/host/common/MockEsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ void EspClass::autoLightSleep() {
void EspClass::autoSleepOff() {
}

void EspClass::neverSleep() {
}

void EspClass::neverSleepOff() {
}


void EspClass::restart()
{
Expand Down

0 comments on commit f783789

Please sign in to comment.