Skip to content

Commit 1511552

Browse files
committed
feat(esp32): Add automatic lightSleep and WiFi listenInterval APIs
1 parent 8e5c957 commit 1511552

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

cores/esp32/esp32-hal-cpu.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "freertos/task.h"
1919
#include "esp_attr.h"
2020
#include "esp_log.h"
21+
#include "esp_pm.h"
2122
#include "soc/rtc.h"
2223
#if !defined(CONFIG_IDF_TARGET_ESP32C2) && !defined(CONFIG_IDF_TARGET_ESP32C6) && !defined(CONFIG_IDF_TARGET_ESP32H2)
2324
#include "soc/rtc_cntl_reg.h"
@@ -278,3 +279,16 @@ uint32_t getApbFrequency(){
278279
rtc_clk_cpu_freq_get_config(&conf);
279280
return calculateApb(&conf);
280281
}
282+
283+
bool setAutomaticLightSleep(bool enabled)
284+
{
285+
uint32_t cpuFreq = getCpuFrequencyMhz();
286+
287+
esp_pm_config_t pm_config = {
288+
.max_freq_mhz = cpuFreq,
289+
.min_freq_mhz = cpuFreq,
290+
.light_sleep_enable = enabled,
291+
};
292+
293+
return esp_pm_configure(&pm_config) == ESP_OK;
294+
}

cores/esp32/esp32-hal-cpu.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ uint32_t getCpuFrequencyMhz(); // In MHz
4141
uint32_t getXtalFrequencyMhz(); // In MHz
4242
uint32_t getApbFrequency(); // In Hz
4343

44+
/**
45+
* @brief Set automatic light sleep state. CPU will fo into light sleep if no ongoing activity (active task, peripheral activity etc.)
46+
* @param enabled true to enable automatic lightSleep
47+
* @return
48+
* - ESP_OK on success
49+
* - ESP_ERR_NOT_SUPPORTED if CONFIG_PM_ENABLE is not enabled in sdkconfig
50+
*/
51+
bool setAutomaticLightSleep(bool enabled);
52+
4453
#ifdef __cplusplus
4554
}
4655
#endif

libraries/WiFi/src/WiFiGeneric.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,27 @@ bool WiFiGenericClass::setSleep(wifi_ps_type_t sleepType)
13931393
return false;
13941394
}
13951395

1396+
bool WiFiGenericClass::setSleep(wifi_ps_type_t sleepType, uint16_t listenInterval)
1397+
{
1398+
if (!setSleep(sleepType))
1399+
return false;
1400+
1401+
wifi_config_t current_conf;
1402+
if(esp_wifi_get_config((wifi_interface_t)ESP_IF_WIFI_STA, &current_conf) != ESP_OK){
1403+
log_e("setSleep(wifi_ps_type_t sleepType, uint16_t listenInterval) failed: get current config failed!");
1404+
return false;
1405+
}
1406+
1407+
current_conf.sta.listen_interval = listenInterval;
1408+
1409+
if(esp_wifi_set_config((wifi_interface_t)ESP_IF_WIFI_STA, &current_conf) != ESP_OK){
1410+
log_e("setSleep(wifi_ps_type_t sleepType, uint16_t listenInterval) failed: set wifi config failed!");
1411+
return false;
1412+
}
1413+
1414+
return true;
1415+
}
1416+
13961417
/**
13971418
* get modem sleep enabled
13981419
* @return true if modem sleep is enabled

libraries/WiFi/src/WiFiGeneric.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ class WiFiGenericClass
182182

183183
bool setSleep(bool enabled);
184184
bool setSleep(wifi_ps_type_t sleepType);
185+
186+
/**
187+
* @brief Set modem sleep state
188+
* @param sleepType Modem sleep type, one of WIFI_PS_NONE, WIFI_PS_MIN_MODEM, WIFI_PS_MAX_MODEM
189+
* @param listenInterval Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set. Units: AP beacon intervals. Defaults to 3 if set to 0.
190+
* @return
191+
* - true on success
192+
* - false on internal error when parameters combination is not valid
193+
*/
194+
bool setSleep(wifi_ps_type_t sleepType, uint16_t listenInterval);
185195
wifi_ps_type_t getSleep();
186196

187197
bool setTxPower(wifi_power_t power);

0 commit comments

Comments
 (0)