From 8cdca030e3e219cb7465f56157fcba687e9a44ec Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Sat, 12 Oct 2024 11:29:35 +0800 Subject: [PATCH] fix(esp_hw_support): skip some wakeup steps if sleep is rejected 1. Skip esp_timer time compensation to avoid introducing errors into rtc_timer 2. Ignore sleep_time_overhead_out measurements when sleep is rejected --- components/esp_hw_support/sleep_modes.c | 45 +++++++++++++------------ 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 62c652c537..cad6abee84 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -973,12 +973,14 @@ static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t m esp_sleep_execute_event_callbacks(SLEEP_EVENT_SW_CLK_READY, (void *)0); if (!deep_sleep) { - s_config.ccount_ticks_record = esp_cpu_get_cycle_count(); + if (result == ESP_OK) { + s_config.ccount_ticks_record = esp_cpu_get_cycle_count(); #if SOC_PM_RETENTION_SW_TRIGGER_REGDMA - if (pd_flags & PMU_SLEEP_PD_TOP) { - sleep_retention_do_system_retention(false); - } + if (pd_flags & PMU_SLEEP_PD_TOP) { + sleep_retention_do_system_retention(false); + } #endif + } misc_modules_wake_prepare(); } @@ -1126,7 +1128,7 @@ static esp_err_t esp_light_sleep_inner(uint32_t pd_flags, #endif // If SPI flash was powered down, wait for it to become ready - if (pd_flags & RTC_SLEEP_PD_VDDSDIO) { + if (!reject && (pd_flags & RTC_SLEEP_PD_VDDSDIO)) { #if SOC_PM_SUPPORT_TOP_PD if (pd_flags & PMU_SLEEP_PD_TOP) { uint32_t flash_ready_hw_waited_time_us = pmu_sleep_get_wakup_retention_cost(); @@ -1347,22 +1349,18 @@ esp_err_t esp_light_sleep_start(void) // System timer has been stopped for the duration of the sleep, correct for that. uint64_t rtc_ticks_at_end = rtc_time_get(); - uint64_t rtc_time_diff = rtc_time_slowclk_to_us(rtc_ticks_at_end - s_config.rtc_ticks_at_sleep_start, s_config.rtc_clk_cal_period); -#if CONFIG_ESP_SLEEP_DEBUG - if (s_sleep_ctx != NULL) { - s_sleep_ctx->sleep_out_rtc_time_stamp = rtc_ticks_at_end; + if (s_light_sleep_wakeup) { + uint64_t rtc_time_diff = rtc_time_slowclk_to_us(rtc_ticks_at_end - s_config.rtc_ticks_at_sleep_start, s_config.rtc_clk_cal_period); + /** + * If sleep duration is too small(less than 1 rtc_slow_clk cycle), rtc_time_diff will be zero. + * In this case, just ignore the time compensation and keep esp_timer monotonic. + */ + if (rtc_time_diff > 0) { + esp_timer_private_set(high_res_time_at_start + rtc_time_diff); + } + esp_set_time_from_rtc(); } -#endif - - /** - * If sleep duration is too small(less than 1 rtc_slow_clk cycle), rtc_time_diff will be zero. - * In this case, just ignore the time compensation and keep esp_timer monotonic. - */ - if (rtc_time_diff > 0) { - esp_timer_private_set(high_res_time_at_start + rtc_time_diff); - } - esp_set_time_from_rtc(); esp_clk_private_unlock(); esp_timer_private_unlock(); @@ -1380,7 +1378,6 @@ esp_err_t esp_light_sleep_start(void) wdt_hal_disable(&rtc_wdt_ctx); wdt_hal_write_protect_enable(&rtc_wdt_ctx); } - portEXIT_CRITICAL(&s_config.lock); #if CONFIG_ESP_TASK_WDT_USE_ESP_TIMER /* Restart the Task Watchdog timer as it was stopped before sleeping. */ @@ -1390,13 +1387,19 @@ esp_err_t esp_light_sleep_start(void) #endif // CONFIG_ESP_TASK_WDT_USE_ESP_TIMER esp_sleep_execute_event_callbacks(SLEEP_EVENT_SW_EXIT_SLEEP, (void *)0); - s_config.sleep_time_overhead_out = (esp_cpu_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL); #if CONFIG_ESP_SLEEP_DEBUG if (s_sleep_ctx != NULL) { + s_sleep_ctx->sleep_out_rtc_time_stamp = rtc_ticks_at_end; s_sleep_ctx->sleep_request_result = err; } #endif + + if (s_light_sleep_wakeup) { + s_config.sleep_time_overhead_out = (esp_cpu_get_cycle_count() - s_config.ccount_ticks_record) / (esp_clk_cpu_freq() / 1000000ULL); + } + + portEXIT_CRITICAL(&s_config.lock); return err; }