Skip to content

Commit

Permalink
Merge branch 'feature/enable_sleep_reject_for_deep_sleep_v5.1' into '…
Browse files Browse the repository at this point in the history
…release/v5.1'

sleep: enable sleep reject when entering deepsleep (backport v5.1)

See merge request espressif/esp-idf!24440
  • Loading branch information
jack0c committed Jul 4, 2023
2 parents 159e802 + 9817017 commit b0717c6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 32 deletions.
12 changes: 0 additions & 12 deletions components/esp_hw_support/include/esp_sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__));
Expand Down
25 changes: 12 additions & 13 deletions components/esp_hw_support/sleep_modes.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -475,7 +475,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.
Expand Down Expand Up @@ -579,13 +579,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;
Expand Down Expand Up @@ -721,7 +718,7 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t mo

// re-enable UART output
resume_uarts(suspended_uarts_bmap);
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)
Expand Down Expand Up @@ -794,7 +791,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.
Expand All @@ -817,7 +816,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();
Expand All @@ -834,7 +833,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;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions components/esp_hw_support/test_apps/rtc_clk/main/test_rtc_clk.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit b0717c6

Please sign in to comment.