From 679ae472718b2c5cde5ec64ce313b52fe3dd3399 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Mon, 19 Jun 2023 19:32:34 +0800 Subject: [PATCH 1/2] sleep: enable sleep reject when entering deep sleep and return err if sleep request is rejected --- components/esp_hw_support/include/esp_sleep.h | 12 --------- components/esp_hw_support/sleep_modes.c | 25 +++++++++---------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/components/esp_hw_support/include/esp_sleep.h b/components/esp_hw_support/include/esp_sleep.h index 27549d8edb..a836a399c2 100644 --- a/components/esp_hw_support/include/esp_sleep.h +++ b/components/esp_hw_support/include/esp_sleep.h @@ -441,18 +441,6 @@ esp_err_t esp_light_sleep_start(void); * Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup * followed by a call to esp_deep_sleep_start. * - * esp_deep_sleep does not shut down WiFi, BT, and higher level protocol - * connections gracefully. - * Make sure relevant WiFi and BT stack functions are called to close any - * connections and deinitialize the peripherals. These include: - * - esp_bluedroid_disable - * - esp_bt_controller_disable - * - esp_wifi_stop - * - * This function does not return. - * - * @note The device will wake up immediately if the deep-sleep time is set to 0 - * * @param time_in_us deep-sleep time, unit: microsecond */ void esp_deep_sleep(uint64_t time_in_us) __attribute__((__noreturn__)); diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 12c2b84ff6..14eb8eb9ae 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -527,7 +527,7 @@ inline static void IRAM_ATTR misc_modules_wake_prepare(void) inline static uint32_t call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu, bool dslp); -static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mode) +static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mode) { // Stop UART output so that output is not lost due to APB frequency change. // For light sleep, suspend UART output — it will resume after wakeup. @@ -618,13 +618,10 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mo } #endif - uint32_t reject_triggers = 0; - if (!deep_sleep) { - /* Light sleep, enable sleep reject for faster return from this function, - * in case the wakeup is already triggerred. - */ - reject_triggers = (s_config.wakeup_triggers & RTC_SLEEP_REJECT_MASK) | sleep_modem_reject_triggers(); - } + /* Enable sleep reject for faster return from this function, + * in case the wakeup is already triggerred. + */ + uint32_t reject_triggers = (s_config.wakeup_triggers & RTC_SLEEP_REJECT_MASK) | sleep_modem_reject_triggers(); //Append some flags in addition to power domains uint32_t sleep_flags = pd_flags; @@ -756,7 +753,7 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mo // re-enable UART output resume_uarts(); s_lightsleep_cnt++; - return result; + return result ? ESP_ERR_SLEEP_REJECT : ESP_OK; } inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers, uint32_t lslp_mem_inf_fpu, bool dslp) @@ -829,7 +826,9 @@ void IRAM_ATTR esp_deep_sleep_start(void) #endif // Enter sleep - esp_sleep_start(force_pd_flags | pd_flags, ESP_SLEEP_MODE_DEEP_SLEEP); + if (esp_sleep_start(force_pd_flags | pd_flags, ESP_SLEEP_MODE_DEEP_SLEEP) == ESP_ERR_SLEEP_REJECT) { + ESP_EARLY_LOGW(TAG, "Deep sleep request is rejected"); + } // Because RTC is in a slower clock domain than the CPU, it // can take several CPU cycles for the sleep mode to start. @@ -852,7 +851,7 @@ static esp_err_t esp_light_sleep_inner(uint32_t pd_flags, uint32_t flash_enable_time_us) { // Enter sleep - uint32_t reject = esp_sleep_start(pd_flags, ESP_SLEEP_MODE_LIGHT_SLEEP); + esp_err_t reject = esp_sleep_start(pd_flags, ESP_SLEEP_MODE_LIGHT_SLEEP); #if SOC_CONFIGURABLE_VDDSDIO_SUPPORTED rtc_vddsdio_config_t vddsdio_config = rtc_vddsdio_get_config(); @@ -869,7 +868,7 @@ static esp_err_t esp_light_sleep_inner(uint32_t pd_flags, esp_rom_delay_us(flash_enable_time_us); } - return reject ? ESP_ERR_SLEEP_REJECT : ESP_OK; + return reject; } /** From 6fe405bffc1635a482ebc45a5e9619619ee6cffb Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Wed, 21 Jun 2023 15:24:42 +0800 Subject: [PATCH 2/2] ci: fix failed ci test jobs --- .../test_apps/rtc_clk/main/test_rtc_clk.c | 10 +++++----- .../test_apps/esp_system_unity_tests/main/test_sleep.c | 2 +- .../timer_group/gptimer/pytest_gptimer_example.py | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/components/esp_hw_support/test_apps/rtc_clk/main/test_rtc_clk.c b/components/esp_hw_support/test_apps/rtc_clk/main/test_rtc_clk.c index 3425455c1e..bd41fefff0 100644 --- a/components/esp_hw_support/test_apps/rtc_clk/main/test_rtc_clk.c +++ b/components/esp_hw_support/test_apps/rtc_clk/main/test_rtc_clk.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -384,11 +384,11 @@ static void trigger_deepsleep(void) esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() / 2); // Delay for error accumulation. - vTaskDelay(pdMS_TO_TICKS(1000)); + vTaskDelay(pdMS_TO_TICKS(10*1000)); // Save start time. Deep sleep. start = esp_rtc_get_time_us(); - esp_sleep_enable_timer_wakeup(1000); + esp_sleep_enable_timer_wakeup(5000); // In function esp_deep_sleep_start() uses function esp_sync_timekeeping_timers() // to prevent a negative time after wake up. esp_deep_sleep_start(); @@ -404,11 +404,11 @@ static void check_time_deepsleep_1(void) esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() * 2); // Delay for error accumulation. - vTaskDelay(pdMS_TO_TICKS(1000)); + vTaskDelay(pdMS_TO_TICKS(10*1000)); start = esp_rtc_get_time_us(); - esp_sleep_enable_timer_wakeup(1000); + esp_sleep_enable_timer_wakeup(5000); // In function esp_deep_sleep_start() uses function esp_sync_timekeeping_timers() // to prevent a negative time after wake up. esp_deep_sleep_start(); diff --git a/components/esp_system/test_apps/esp_system_unity_tests/main/test_sleep.c b/components/esp_system/test_apps/esp_system_unity_tests/main/test_sleep.c index 3e653a5f86..577c211e1c 100644 --- a/components/esp_system/test_apps/esp_system_unity_tests/main/test_sleep.c +++ b/components/esp_system/test_apps/esp_system_unity_tests/main/test_sleep.c @@ -586,7 +586,7 @@ static void trigger_deepsleep(void) // Deinit NVS to prevent Unity from complaining "The test leaked too much memory" TEST_ESP_OK(nvs_flash_deinit()); - esp_sleep_enable_timer_wakeup(1000); + esp_sleep_enable_timer_wakeup(5000); // In function esp_deep_sleep_start() uses function esp_sync_timekeeping_timers() // to prevent a negative time after wake up. esp_deep_sleep_start(); diff --git a/examples/peripherals/timer_group/gptimer/pytest_gptimer_example.py b/examples/peripherals/timer_group/gptimer/pytest_gptimer_example.py index 4c65b72a7e..29a534d611 100644 --- a/examples/peripherals/timer_group/gptimer/pytest_gptimer_example.py +++ b/examples/peripherals/timer_group/gptimer/pytest_gptimer_example.py @@ -23,7 +23,7 @@ def test_gptimer_example(dut: Dut) -> None: dut.expect_exact('Start timer, auto-reload at alarm event', timeout=5) res = dut.expect(r'Timer reloaded, count=(\d+)', timeout=5) reloaded_count = res.group(1).decode('utf8') - assert 0 <= int(reloaded_count) < 10 + assert 0 <= int(reloaded_count) < 20 dut.expect_exact('Stop timer') dut.expect_exact('Start timer, update alarm value dynamically')