Skip to content

Commit e8798ac

Browse files
committed
Add neverSleep and neverSleepOff to Esp class, use them in core and libs where feasible.
1 parent a9d9cda commit e8798ac

File tree

7 files changed

+49
-13
lines changed

7 files changed

+49
-13
lines changed

cores/esp8266/Esp.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,24 @@ void EspClass::autoSleepOff() {
300300
saved_sleep_type = NONE_SLEEP_T;
301301
}
302302

303+
void EspClass::neverSleep() {
304+
const auto active_sleep_type = wifi_get_sleep_type();
305+
if (NONE_SLEEP_T == active_sleep_type) {
306+
return;
307+
}
308+
wifi_fpm_close();
309+
saved_sleep_type = active_sleep_type;
310+
wifi_set_sleep_type(NONE_SLEEP_T);
311+
}
312+
313+
void EspClass::neverSleepOff() {
314+
if (NONE_SLEEP_T == saved_sleep_type) {
315+
return;
316+
}
317+
wifi_set_sleep_type(saved_sleep_type);
318+
saved_sleep_type = NONE_SLEEP_T;
319+
}
320+
303321
/*
304322
Layout of RTC Memory is as follows:
305323
Ref: Espressif doc 2C-ESP8266_Non_OS_SDK_API_Reference, section 3.3.23 (system_rtc_mem_write)

cores/esp8266/Esp.h

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ class EspClass {
114114
/// it would have to be restored explicitly.
115115
static void autoSleepOff();
116116

117+
static void neverSleep();
118+
/// Any prior sleep type is restored, but only as automatic.
119+
/// If any forced sleep mode was effective before neverSleep,
120+
/// it would have to be restored explicitly.
121+
static void neverSleepOff();
122+
117123
static bool rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size);
118124
static bool rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size);
119125

cores/esp8266/Updater.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
109109
_md5 = MD5Builder();
110110

111111
#ifndef HOST_MOCK
112-
wifi_set_sleep_type(NONE_SLEEP_T);
112+
ESP.neverSleep();
113113
#endif
114114

115115
//address where we will start writing the update

doc/libraries.rst

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Some ESP-specific APIs related to the deep, modem, and light sleep modes, RTC an
9191

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

94+
``ESP.neverSleep()`` immediately puts the chip into ``NONE_SLEEP`` mode.
95+
96+
``ESP.neverSleepOff()`` returns the chip to any automatic sleep mode that was effective before the preceding call to ``ESP.neverSleep``.
97+
9498
``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.
9599

96100
``ESP.restart()`` restarts the CPU.

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,18 @@ bool ESP8266WiFiGenericClass::setSleepMode(WiFiSleepType_t type, uint8_t listenI
348348
return ret;
349349
}
350350

351+
bool ESP8266WiFiGenericClass::setSleep(bool enable) {
352+
if (enable)
353+
{
354+
ESP.neverSleepOff();
355+
}
356+
else
357+
{
358+
ESP.neverSleep();
359+
}
360+
return true;
361+
}
362+
351363
/**
352364
* get Sleep mode
353365
* @return sleep_type_t

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,11 @@ class ESP8266WiFiGenericClass {
8585

8686
bool setSleepMode(WiFiSleepType_t type, uint8_t listenInterval = 0);
8787
/**
88-
* Set modem sleep mode (ESP32 compatibility)
88+
* Set ESP866 to never sleep or return to previous mode (ESP32 compatibility)
8989
* @param enable true to enable
9090
* @return true if succeeded
9191
*/
92-
bool setSleep(bool enable)
93-
{
94-
if (enable)
95-
{
96-
return setSleepMode(WIFI_MODEM_SLEEP);
97-
}
98-
else
99-
{
100-
return setSleepMode(WIFI_NONE_SLEEP);
101-
}
102-
}
92+
bool setSleep(bool enable);
10393
/**
10494
* Set sleep mode (ESP32 compatibility)
10595
* @param mode wifi_ps_type_t

tests/host/common/MockEsp.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ void EspClass::autoLightSleep() {
125125
void EspClass::autoSleepOff() {
126126
}
127127

128+
void EspClass::neverSleep() {
129+
}
130+
131+
void EspClass::neverSleepOff() {
132+
}
133+
128134

129135
void EspClass::restart()
130136
{

0 commit comments

Comments
 (0)