From a25ce78392fa3450ddef2d5ec639a73d882a5f5e Mon Sep 17 00:00:00 2001 From: Li Shuai Date: Wed, 15 Mar 2023 10:39:52 +0800 Subject: [PATCH] sleep: fix sleep current issue caused by sar adc --- .../include/esp_private/sar_periph_ctrl.h | 10 ++++++++++ components/esp_hw_support/port/esp32/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32c2/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32c3/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32c6/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32h2/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32h4/sar_periph_ctrl.c | 5 +++++ .../esp_hw_support/port/esp32s2/sar_periph_ctrl.c | 7 +++++++ .../esp_hw_support/port/esp32s3/sar_periph_ctrl.c | 7 +++++++ components/esp_hw_support/sleep_modes.c | 3 +++ 10 files changed, 67 insertions(+) diff --git a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h index 3b15303af48..833ce1f4ff1 100644 --- a/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h +++ b/components/esp_hw_support/include/esp_private/sar_periph_ctrl.h @@ -62,6 +62,16 @@ void sar_periph_ctrl_pwdet_power_acquire(void); */ void sar_periph_ctrl_pwdet_power_release(void); +/** + * @brief Enable SAR power when system wakes up + */ +void sar_periph_ctrl_power_enable(void); + +/** + * @brief Disable SAR power when system goes to sleep + */ +void sar_periph_ctrl_power_disable(void); + #ifdef __cplusplus } #endif diff --git a/components/esp_hw_support/port/esp32/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32/sar_periph_ctrl.c index 90da25dd132..e14cbc40a85 100644 --- a/components/esp_hw_support/port/esp32/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32/sar_periph_ctrl.c @@ -32,6 +32,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_ON); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32c2/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32c2/sar_periph_ctrl.c index 9fc85e809bd..b9648a96dc9 100644 --- a/components/esp_hw_support/port/esp32c2/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32c2/sar_periph_ctrl.c @@ -34,6 +34,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c index 5c9c47cd110..00c9b62878b 100644 --- a/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32c3/sar_periph_ctrl.c @@ -34,6 +34,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32c6/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32c6/sar_periph_ctrl.c index 01b463ccc00..c0b853124af 100644 --- a/components/esp_hw_support/port/esp32c6/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32c6/sar_periph_ctrl.c @@ -31,6 +31,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_force_power_ctrl_from_pwdet(true); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32h2/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32h2/sar_periph_ctrl.c index fce3cc75617..79598ee0eb8 100644 --- a/components/esp_hw_support/port/esp32h2/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32h2/sar_periph_ctrl.c @@ -30,6 +30,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_force_power_ctrl_from_pwdet(true); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32h4/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32h4/sar_periph_ctrl.c index 437af44dfae..2b154503c77 100644 --- a/components/esp_hw_support/port/esp32h4/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32h4/sar_periph_ctrl.c @@ -29,6 +29,11 @@ void sar_periph_ctrl_init(void) //TODO: IDF-6123 } +void sar_periph_ctrl_power_enable(void) +{ + //TODO: IDF-6123 +} + void sar_periph_ctrl_power_disable(void) { //TODO: IDF-6123 diff --git a/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c index e23af13e430..c1c738ff586 100644 --- a/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32s2/sar_periph_ctrl.c @@ -34,6 +34,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/port/esp32s3/sar_periph_ctrl.c b/components/esp_hw_support/port/esp32s3/sar_periph_ctrl.c index b00236323f1..d8cd4fc522d 100644 --- a/components/esp_hw_support/port/esp32s3/sar_periph_ctrl.c +++ b/components/esp_hw_support/port/esp32s3/sar_periph_ctrl.c @@ -34,6 +34,13 @@ void sar_periph_ctrl_init(void) //Add other periph power control initialisation here } +void sar_periph_ctrl_power_enable(void) +{ + portENTER_CRITICAL_SAFE(&rtc_spinlock); + sar_ctrl_ll_set_power_mode(SAR_CTRL_LL_POWER_FSM); + portEXIT_CRITICAL_SAFE(&rtc_spinlock); +} + void sar_periph_ctrl_power_disable(void) { portENTER_CRITICAL_SAFE(&rtc_spinlock); diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index a4e71c9e66d..0faa5802d23 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -60,6 +60,7 @@ #include "esp_private/esp_clk.h" #include "esp_private/esp_task_wdt.h" #include "esp_private/spi_flash_os.h" +#include "esp_private/sar_periph_ctrl.h" #ifdef CONFIG_IDF_TARGET_ESP32 #include "esp32/rom/cache.h" @@ -404,6 +405,7 @@ inline static void IRAM_ATTR misc_modules_sleep_prepare(void) #if REGI2C_ANA_CALI_PD_WORKAROUND regi2c_analog_cali_reg_read(); #endif + sar_periph_ctrl_power_disable(); } /** @@ -411,6 +413,7 @@ inline static void IRAM_ATTR misc_modules_sleep_prepare(void) */ inline static void IRAM_ATTR misc_modules_wake_prepare(void) { + sar_periph_ctrl_power_enable(); #if SOC_PM_SUPPORT_CPU_PD && SOC_PM_CPU_RETENTION_BY_RTCCNTL sleep_disable_cpu_retention(); #endif