Skip to content

Commit 9b26012

Browse files
committed
Disable modem sleep by default on S2 for now.
1 parent aa7b015 commit 9b26012

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

libraries/WiFi/src/WiFiGeneric.cpp

+25-16
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,13 @@ static std::vector<WiFiEventCbList_t> cbEventList;
619619
bool WiFiGenericClass::_persistent = true;
620620
bool WiFiGenericClass::_long_range = false;
621621
wifi_mode_t WiFiGenericClass::_forceSleepLastMode = WIFI_MODE_NULL;
622+
#if CONFIG_IDF_TARGET_ESP32S2
623+
wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_NONE;
624+
#else
625+
wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_MIN_MODEM;
626+
#endif
622627

623-
WiFiGenericClass::WiFiGenericClass()
628+
WiFiGenericClass::WiFiGenericClass()
624629
{
625630

626631
}
@@ -790,6 +795,9 @@ esp_err_t WiFiGenericClass::_eventCallback(arduino_event_t *event)
790795
WiFiSTAClass::_setStatus(WL_DISCONNECTED);
791796
setStatusBits(STA_STARTED_BIT);
792797
tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, WiFiSTAClass::_hostname.c_str());
798+
if(esp_wifi_set_ps(_sleepEnabled) != ESP_OK){
799+
log_e("esp_wifi_set_ps failed");
800+
}
793801
} else if(event->event_id == ARDUINO_EVENT_WIFI_STA_STOP) {
794802
WiFiSTAClass::_setStatus(WL_NO_SHIELD);
795803
clearStatusBits(STA_STARTED_BIT | STA_CONNECTED_BIT | STA_HAS_IP_BIT | STA_HAS_IP6_BIT);
@@ -1054,13 +1062,22 @@ bool WiFiGenericClass::enableAP(bool enable)
10541062
* @param enable bool
10551063
* @return ok
10561064
*/
1057-
bool WiFiGenericClass::setSleep(bool enable)
1065+
bool WiFiGenericClass::setSleep(bool enabled){
1066+
return setSleep(enabled?WIFI_PS_MIN_MODEM:WIFI_PS_NONE);
1067+
}
1068+
1069+
bool WiFiGenericClass::setSleep(wifi_ps_type_t sleepType)
10581070
{
1059-
if((getMode() & WIFI_MODE_STA) == 0){
1060-
log_w("STA has not been started");
1061-
return false;
1071+
if(sleepType != _sleepEnabled){
1072+
_sleepEnabled = sleepType;
1073+
if((getMode() & WIFI_MODE_STA) != 0){
1074+
if(esp_wifi_set_ps(_sleepEnabled) != ESP_OK){
1075+
log_e("esp_wifi_set_ps failed!");
1076+
}
1077+
}
1078+
return true;
10621079
}
1063-
return esp_wifi_set_ps(enable?WIFI_PS_MIN_MODEM:WIFI_PS_NONE) == ESP_OK;
1080+
return false;
10641081
}
10651082

10661083
/**
@@ -1081,17 +1098,9 @@ bool WiFiGenericClass::setSleep(wifi_ps_type_t mode)
10811098
* get modem sleep enabled
10821099
* @return true if modem sleep is enabled
10831100
*/
1084-
bool WiFiGenericClass::getSleep()
1101+
wifi_ps_type_t WiFiGenericClass::getSleep()
10851102
{
1086-
wifi_ps_type_t ps;
1087-
if((getMode() & WIFI_MODE_STA) == 0){
1088-
log_w("STA has not been started");
1089-
return false;
1090-
}
1091-
if(esp_wifi_get_ps(&ps) == ESP_OK){
1092-
return ps == WIFI_PS_MIN_MODEM;
1093-
}
1094-
return false;
1103+
return _sleepEnabled;
10951104
}
10961105

10971106
/**

libraries/WiFi/src/WiFiGeneric.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ class WiFiGenericClass
163163
bool enableSTA(bool enable);
164164
bool enableAP(bool enable);
165165

166-
bool setSleep(bool enable);
167-
bool setSleep(wifi_ps_type_t mode);
168-
bool getSleep();
166+
bool setSleep(bool enabled);
167+
bool setSleep(wifi_ps_type_t sleepType);
168+
wifi_ps_type_t getSleep();
169169

170170
bool setTxPower(wifi_power_t power);
171171
wifi_power_t getTxPower();
@@ -179,10 +179,11 @@ class WiFiGenericClass
179179
static bool _persistent;
180180
static bool _long_range;
181181
static wifi_mode_t _forceSleepLastMode;
182+
static wifi_ps_type_t _sleepEnabled;
182183

183184
static int setStatusBits(int bits);
184185
static int clearStatusBits(int bits);
185-
186+
186187
public:
187188
static int hostByName(const char *aHostname, IPAddress &aResult);
188189

0 commit comments

Comments
 (0)