|
|
@@ -43,12 +43,14 @@
|
|
|
#include "esp_rom_uart.h"
|
|
|
#include "esp_rom_sys.h"
|
|
|
#include "brownout.h"
|
|
|
+#include "esp_private/sleep_retention.h"
|
|
|
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
|
|
#include "esp32/rom/cache.h"
|
|
|
#include "esp32/rom/rtc.h"
|
|
|
#include "esp32/clk.h"
|
|
|
#include "esp_private/gpio.h"
|
|
|
+#include "esp_private/sleep_gpio.h"
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
#include "esp32s2/clk.h"
|
|
|
#include "esp32s2/rom/cache.h"
|
|
|
@@ -60,18 +62,18 @@
|
|
|
#include "esp32s3/rom/cache.h"
|
|
|
#include "esp32s3/rom/rtc.h"
|
|
|
#include "soc/extmem_reg.h"
|
|
|
+#include "esp_private/sleep_mac_bb.h"
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
#include "esp32c3/clk.h"
|
|
|
#include "esp32c3/rom/cache.h"
|
|
|
#include "esp32c3/rom/rtc.h"
|
|
|
#include "soc/extmem_reg.h"
|
|
|
-#include "esp_heap_caps.h"
|
|
|
+#include "esp_private/sleep_mac_bb.h"
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
#include "esp32h2/clk.h"
|
|
|
#include "esp32h2/rom/cache.h"
|
|
|
#include "esp32h2/rom/rtc.h"
|
|
|
#include "soc/extmem_reg.h"
|
|
|
-#include "esp_heap_caps.h"
|
|
|
#endif
|
|
|
|
|
|
// If light sleep time is less than that, don't power down flash
|
|
|
@@ -148,13 +150,16 @@ typedef struct {
|
|
|
uint32_t sleep_time_overhead_out;
|
|
|
uint32_t rtc_clk_cal_period;
|
|
|
uint64_t rtc_ticks_at_sleep_start;
|
|
|
-#if SOC_PM_SUPPORT_CPU_PD
|
|
|
- void *cpu_pd_mem;
|
|
|
-#endif
|
|
|
} sleep_config_t;
|
|
|
|
|
|
static sleep_config_t s_config = {
|
|
|
- .pd_options = { ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO },
|
|
|
+ .pd_options = {
|
|
|
+ ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO, ESP_PD_OPTION_AUTO,
|
|
|
+#if SOC_PM_SUPPORT_CPU_PD
|
|
|
+ ESP_PD_OPTION_AUTO,
|
|
|
+#endif
|
|
|
+ ESP_PD_OPTION_AUTO
|
|
|
+ },
|
|
|
.ccount_ticks_record = 0,
|
|
|
.sleep_time_overhead_out = DEFAULT_SLEEP_OUT_OVERHEAD_US,
|
|
|
.wakeup_triggers = 0
|
|
|
@@ -183,95 +188,6 @@ static void touch_wakeup_prepare(void);
|
|
|
static void esp_deep_sleep_wakeup_prepare(void);
|
|
|
#endif
|
|
|
|
|
|
-#if CONFIG_MAC_BB_PD
|
|
|
-#define MAC_BB_POWER_DOWN_CB_NO 2
|
|
|
-#define MAC_BB_POWER_UP_CB_NO 2
|
|
|
-static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO];
|
|
|
-static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO];
|
|
|
-
|
|
|
-esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
|
|
|
-{
|
|
|
- int index = MAC_BB_POWER_DOWN_CB_NO;
|
|
|
- for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
|
|
|
- if (s_mac_bb_power_down_cb[i] == cb) {
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
-
|
|
|
- if (s_mac_bb_power_down_cb[i] == NULL) {
|
|
|
- index = i;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (index < MAC_BB_POWER_DOWN_CB_NO) {
|
|
|
- s_mac_bb_power_down_cb[index] = cb;
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
-
|
|
|
- return ESP_ERR_NO_MEM;
|
|
|
-}
|
|
|
-
|
|
|
-esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
|
|
|
-{
|
|
|
- for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
|
|
|
- if (s_mac_bb_power_down_cb[i] == cb) {
|
|
|
- s_mac_bb_power_down_cb[i] = NULL;
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
- }
|
|
|
- return ESP_ERR_INVALID_STATE;
|
|
|
-}
|
|
|
-
|
|
|
-static IRAM_ATTR void mac_bb_power_down_cb_execute(void)
|
|
|
-{
|
|
|
- for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) {
|
|
|
- if (s_mac_bb_power_down_cb[i]) {
|
|
|
- s_mac_bb_power_down_cb[i]();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
|
|
|
-{
|
|
|
- int index = MAC_BB_POWER_UP_CB_NO;
|
|
|
- for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
|
|
|
- if (s_mac_bb_power_up_cb[i] == cb) {
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
-
|
|
|
- if (s_mac_bb_power_up_cb[i] == NULL) {
|
|
|
- index = i;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (index < MAC_BB_POWER_UP_CB_NO) {
|
|
|
- s_mac_bb_power_up_cb[index] = cb;
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
-
|
|
|
- return ESP_ERR_NO_MEM;
|
|
|
-}
|
|
|
-
|
|
|
-esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
|
|
|
-{
|
|
|
- for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
|
|
|
- if (s_mac_bb_power_up_cb[i] == cb) {
|
|
|
- s_mac_bb_power_up_cb[i] = NULL;
|
|
|
- return ESP_OK;
|
|
|
- }
|
|
|
- }
|
|
|
- return ESP_ERR_INVALID_STATE;
|
|
|
-}
|
|
|
-
|
|
|
-static IRAM_ATTR void mac_bb_power_up_cb_execute(void)
|
|
|
-{
|
|
|
- for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) {
|
|
|
- if (s_mac_bb_power_up_cb[i]) {
|
|
|
- s_mac_bb_power_up_cb[i]();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-#endif ///CONFIG_MAC_BB_PD
|
|
|
-
|
|
|
/* Wake from deep sleep stub
|
|
|
See esp_deepsleep.h esp_wake_deep_sleep() comments for details.
|
|
|
*/
|
|
|
@@ -364,81 +280,33 @@ static void IRAM_ATTR resume_uarts(void)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu);
|
|
|
-
|
|
|
-#if SOC_PM_SUPPORT_CPU_PD
|
|
|
-esp_err_t esp_sleep_cpu_pd_low_init(bool enable)
|
|
|
+inline static void IRAM_ATTR misc_modules_sleep_prepare(void)
|
|
|
{
|
|
|
- if (enable) {
|
|
|
- if (s_config.cpu_pd_mem == NULL) {
|
|
|
- void *buf = heap_caps_aligned_alloc(SOC_RTC_CNTL_CPU_PD_DMA_ADDR_ALIGN,
|
|
|
- SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE,
|
|
|
- MALLOC_CAP_RETENTION | MALLOC_CAP_DEFAULT);
|
|
|
- if (buf) {
|
|
|
- memset(buf, 0, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE + RTC_HAL_DMA_LINK_NODE_SIZE);
|
|
|
- s_config.cpu_pd_mem = rtc_cntl_hal_dma_link_init(buf,
|
|
|
- buf + RTC_HAL_DMA_LINK_NODE_SIZE, SOC_RTC_CNTL_CPU_PD_RETENTION_MEM_SIZE, NULL);
|
|
|
- } else {
|
|
|
- return ESP_ERR_NO_MEM;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (s_config.cpu_pd_mem) {
|
|
|
- heap_caps_free(s_config.cpu_pd_mem);
|
|
|
- s_config.cpu_pd_mem = NULL;
|
|
|
- }
|
|
|
- }
|
|
|
- return ESP_OK;
|
|
|
-}
|
|
|
-#endif // SOC_PM_SUPPORT_CPU_PD
|
|
|
-
|
|
|
-#if SOC_GPIO_SUPPORT_SLP_SWITCH
|
|
|
+#if CONFIG_MAC_BB_PD
|
|
|
+ mac_bb_power_down_cb_execute();
|
|
|
+#endif
|
|
|
#if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
|
|
|
-static inline void gpio_sleep_mode_config_apply(void)
|
|
|
-{
|
|
|
- for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
|
|
|
- if (GPIO_IS_VALID_GPIO(gpio_num)) {
|
|
|
- gpio_sleep_pupd_config_apply(gpio_num);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-static inline void gpio_sleep_mode_config_unapply(void)
|
|
|
-{
|
|
|
- for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
|
|
|
- if (GPIO_IS_VALID_GPIO(gpio_num)) {
|
|
|
- gpio_sleep_pupd_config_unapply(gpio_num);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+ gpio_sleep_mode_config_apply();
|
|
|
+#endif
|
|
|
+#if SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD
|
|
|
+ sleep_enable_memory_retention();
|
|
|
#endif
|
|
|
-
|
|
|
-void esp_sleep_config_gpio_isolate(void)
|
|
|
-{
|
|
|
- ESP_LOGI(TAG, "Configure to isolate all GPIO pins in sleep state");
|
|
|
- for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
|
|
|
- if (GPIO_IS_VALID_GPIO(gpio_num)) {
|
|
|
- gpio_sleep_set_direction(gpio_num, GPIO_MODE_DISABLE);
|
|
|
- gpio_sleep_set_pull_mode(gpio_num, GPIO_FLOATING);
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
-void esp_sleep_enable_gpio_switch(bool enable)
|
|
|
+inline static void IRAM_ATTR misc_modules_wake_prepare(void)
|
|
|
{
|
|
|
- ESP_LOGI(TAG, "%s automatic switching of GPIO sleep configuration", enable ? "Enable" : "Disable");
|
|
|
- for (gpio_num_t gpio_num = GPIO_NUM_0; gpio_num < GPIO_NUM_MAX; gpio_num++) {
|
|
|
- if (GPIO_IS_VALID_GPIO(gpio_num)) {
|
|
|
- if (enable) {
|
|
|
- gpio_sleep_sel_en(gpio_num);
|
|
|
- } else {
|
|
|
- gpio_sleep_sel_dis(gpio_num);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+#if SOC_PM_SUPPORT_CPU_PD || SOC_PM_SUPPORT_TAGMEM_PD
|
|
|
+ sleep_disable_memory_retention();
|
|
|
+#endif
|
|
|
+#if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
|
|
|
+ gpio_sleep_mode_config_unapply();
|
|
|
+#endif
|
|
|
+#if CONFIG_MAC_BB_PD
|
|
|
+ mac_bb_power_up_cb_execute();
|
|
|
+#endif
|
|
|
}
|
|
|
-#endif // SOC_GPIO_SUPPORT_SLP_SWITCH
|
|
|
|
|
|
+inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu);
|
|
|
|
|
|
static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
|
|
|
{
|
|
|
@@ -467,10 +335,6 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
|
|
|
rtc_clk_cpu_freq_get_config(&cpu_freq_config);
|
|
|
rtc_clk_cpu_freq_set_xtal();
|
|
|
|
|
|
-#if CONFIG_MAC_BB_PD
|
|
|
- mac_bb_power_down_cb_execute();
|
|
|
-#endif
|
|
|
-
|
|
|
#if SOC_PM_SUPPORT_EXT_WAKEUP
|
|
|
// Configure pins for external wakeup
|
|
|
if (s_config.wakeup_triggers & RTC_EXT0_TRIG_EN) {
|
|
|
@@ -492,11 +356,10 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
|
|
|
if (s_config.wakeup_triggers & RTC_ULP_TRIG_EN) {
|
|
|
rtc_hal_ulp_wakeup_enable();
|
|
|
}
|
|
|
-#if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
|
|
|
- gpio_sleep_mode_config_apply();
|
|
|
-#endif
|
|
|
#endif
|
|
|
|
|
|
+ misc_modules_sleep_prepare();
|
|
|
+
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
|
|
|
if (deep_sleep) {
|
|
|
if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) {
|
|
|
@@ -576,17 +439,8 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags)
|
|
|
s_config.ccount_ticks_record = cpu_ll_get_cycle_count();
|
|
|
}
|
|
|
|
|
|
-#if SOC_PM_SUPPORT_CPU_PD
|
|
|
- rtc_cntl_hal_disable_cpu_retention();
|
|
|
-#endif
|
|
|
-
|
|
|
-#if CONFIG_GPIO_ESP32_SUPPORT_SWITCH_SLP_PULL
|
|
|
- gpio_sleep_mode_config_unapply();
|
|
|
-#endif
|
|
|
+ misc_modules_wake_prepare();
|
|
|
|
|
|
-#if CONFIG_MAC_BB_PD
|
|
|
- mac_bb_power_up_cb_execute();
|
|
|
-#endif
|
|
|
// re-enable UART output
|
|
|
resume_uarts();
|
|
|
|
|
|
@@ -770,10 +624,6 @@ esp_err_t esp_light_sleep_start(void)
|
|
|
|
|
|
periph_inform_out_light_sleep_overhead(s_config.sleep_time_adjustment - sleep_time_overhead_in);
|
|
|
|
|
|
-#if SOC_PM_SUPPORT_CPU_PD
|
|
|
- rtc_cntl_hal_enable_cpu_retention(s_config.cpu_pd_mem);
|
|
|
-#endif
|
|
|
-
|
|
|
rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config();
|
|
|
|
|
|
// Safety net: enable WDT in case exit from light sleep fails
|
|
|
@@ -1309,11 +1159,7 @@ static uint32_t get_power_down_flags(void)
|
|
|
}
|
|
|
|
|
|
#if SOC_PM_SUPPORT_CPU_PD
|
|
|
- if (s_config.cpu_pd_mem == NULL) {
|
|
|
- s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
|
|
|
- }
|
|
|
-#else
|
|
|
- if (s_config.pd_options[ESP_PD_DOMAIN_CPU] != ESP_PD_OPTION_ON) {
|
|
|
+ if (!cpu_domain_pd_allowed()) {
|
|
|
s_config.pd_options[ESP_PD_DOMAIN_CPU] = ESP_PD_OPTION_ON;
|
|
|
}
|
|
|
#endif
|