From 13a405a32bbc4f1f8c6880b089e48e9fe301c730 Mon Sep 17 00:00:00 2001 From: Lou Tianhao Date: Fri, 13 Oct 2023 16:19:53 +0800 Subject: [PATCH] change(pm): Change sleep callback implement --- .../include/esp_private/sleep_event.h | 8 ++- components/esp_hw_support/sleep_event.c | 23 +++++---- components/esp_hw_support/sleep_modes.c | 4 +- components/esp_pm/pm_impl.c | 51 +++++++++---------- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/components/esp_hw_support/include/esp_private/sleep_event.h b/components/esp_hw_support/include/esp_private/sleep_event.h index a869c69a55..7a7efaabfc 100644 --- a/components/esp_hw_support/include/esp_private/sleep_event.h +++ b/components/esp_hw_support/include/esp_private/sleep_event.h @@ -44,7 +44,7 @@ typedef enum { * @return None */ -typedef void (*esp_sleep_event_cb_t)(void *user_arg, void *ext_arg); +typedef esp_err_t (*esp_sleep_event_cb_t)(void *user_arg, void *ext_arg); /** * @brief Function entry parameter types for light sleep event callback functions (if CONFIG_FREERTOS_USE_TICKLESS_IDLE) @@ -111,11 +111,9 @@ esp_err_t esp_sleep_unregister_event_callback(esp_sleep_event_cb_index_t event_i * * @param event_id Designed to annotate the corresponding event_cb in g_sleep_event_cbs_config * @param ext_arg Designed to pass external parameters - * @return - * - ESP_OK on success - * - ESP_ERR_INVALID_ARG if event_id is out of range + * @return None */ -esp_err_t esp_sleep_execute_event_callbacks(esp_sleep_event_cb_index_t event_id, void *ext_arg); +void esp_sleep_execute_event_callbacks(esp_sleep_event_cb_index_t event_id, void *ext_arg); #ifdef __cplusplus } diff --git a/components/esp_hw_support/sleep_event.c b/components/esp_hw_support/sleep_event.c index 6829ed6d8f..02bc704f58 100644 --- a/components/esp_hw_support/sleep_event.c +++ b/components/esp_hw_support/sleep_event.c @@ -16,6 +16,8 @@ #include "esp_check.h" #include "freertos/FreeRTOS.h" +static __attribute__((unused)) const char *TAG = "sleep_event"; + #if CONFIG_ESP_SLEEP_EVENT_CALLBACKS esp_sleep_event_cbs_config_t g_sleep_event_cbs_config; static portMUX_TYPE s_sleep_event_mutex = portMUX_INITIALIZER_UNLOCKED; @@ -70,22 +72,21 @@ esp_err_t esp_sleep_unregister_event_callback(esp_sleep_event_cb_index_t event_i } #endif -#if CONFIG_ESP_SLEEP_EVENT_CALLBACKS -esp_err_t IRAM_ATTR esp_sleep_execute_event_callbacks(esp_sleep_event_cb_index_t event_id, void *ext_arg) +void IRAM_ATTR esp_sleep_execute_event_callbacks(esp_sleep_event_cb_index_t event_id, void *ext_arg) { +#if CONFIG_ESP_SLEEP_EVENT_CALLBACKS if (event_id >= SLEEP_EVENT_CB_INDEX_NUM) { - return ESP_ERR_INVALID_ARG; + ESP_EARLY_LOGW(TAG, "event_id out of range"); + return; } esp_sleep_event_cb_config_t *current = g_sleep_event_cbs_config.sleep_event_cb_config[event_id]; while (current != NULL) { - (current->cb)(current->user_arg, ext_arg); + if (current->cb != NULL) { + if (ESP_OK != (*current->cb)(current->user_arg, ext_arg)) { + ESP_EARLY_LOGW(TAG, "esp_sleep_execute_event_callbacks has an err, current->cb = %p", current->cb); + } + } current = current->next; } - return ESP_OK; -} -#else -esp_err_t IRAM_ATTR esp_sleep_execute_event_callbacks(esp_sleep_event_cb_index_t event_id, void *ext_arg) -{ - return ESP_OK; -} #endif +} diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index b8c6a13592..0f0fb5c1e8 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -833,13 +833,13 @@ static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t m rtc_clk_cpu_freq_set_config(&cpu_freq_config); } - esp_sleep_execute_event_callbacks(SLEEP_EVENT_SW_CLK_READY, (void *)0); - if (cpu_freq_config.source == SOC_CPU_CLK_SRC_PLL) { // Turn up MSPI speed if switch to PLL mspi_timing_change_speed_mode_cache_safe(false); } + 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(); misc_modules_wake_prepare(); diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index fa9776d547..bbf4eea7b6 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -209,7 +209,7 @@ pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg) /** * @brief Function entry parameter types for light sleep callback functions (if CONFIG_FREERTOS_USE_TICKLESS_IDLE) */ -typedef struct { +struct _esp_pm_sleep_cb_config_t { /** * Callback function defined by user. */ @@ -226,7 +226,9 @@ typedef struct { * Next callback function defined by user. */ struct _esp_pm_sleep_cb_config_t *next; -} esp_pm_sleep_cb_config_t; +}; + +typedef struct _esp_pm_sleep_cb_config_t esp_pm_sleep_cb_config_t; static esp_pm_sleep_cb_config_t *s_light_sleep_enter_cb_config; static esp_pm_sleep_cb_config_t *s_light_sleep_exit_cb_config; @@ -324,24 +326,30 @@ esp_err_t esp_pm_light_sleep_unregister_cbs(esp_pm_sleep_cbs_register_config_t * return ESP_OK; } -static esp_err_t IRAM_ATTR esp_pm_execute_enter_sleep_callbacks(int64_t sleep_time_us) +static void IRAM_ATTR esp_pm_execute_enter_sleep_callbacks(int64_t sleep_time_us) { esp_pm_sleep_cb_config_t *enter_current = s_light_sleep_enter_cb_config; while (enter_current != NULL) { - enter_current->cb(sleep_time_us, enter_current->arg); + if (enter_current->cb != NULL) { + if (ESP_OK != (*enter_current->cb)(sleep_time_us, enter_current->arg)) { + ESP_EARLY_LOGW(TAG, "esp_pm_execute_enter_sleep_callbacks has an err, enter_current = %p", enter_current); + } + } enter_current = enter_current->next; } - return ESP_OK; } -static esp_err_t IRAM_ATTR esp_pm_execute_exit_sleep_callbacks(int64_t sleep_time_us) +static void IRAM_ATTR esp_pm_execute_exit_sleep_callbacks(int64_t sleep_time_us) { esp_pm_sleep_cb_config_t *exit_current = s_light_sleep_exit_cb_config; while (exit_current != NULL) { - exit_current->cb(sleep_time_us, exit_current->arg); + if (exit_current->cb != NULL) { + if (ESP_OK != (*exit_current->cb)(sleep_time_us, exit_current->arg)) { + ESP_EARLY_LOGW(TAG, "esp_pm_execute_exit_sleep_callbacks has an err, exit_current = %p", exit_current); + } + } exit_current = exit_current->next; } - return ESP_OK; } #endif @@ -770,21 +778,13 @@ void IRAM_ATTR vApplicationSleep( TickType_t xExpectedIdleTime ) int64_t time_until_next_alarm = next_esp_timer_alarm - now; int64_t wakeup_delay_us = portTICK_PERIOD_MS * 1000LL * xExpectedIdleTime; int64_t sleep_time_us = MIN(wakeup_delay_us, time_until_next_alarm); - if (sleep_time_us >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP * portTICK_PERIOD_MS * 1000LL) { - int64_t slept_us = 0; + int64_t slept_us = 0; #if CONFIG_PM_LIGHT_SLEEP_CALLBACKS - if (s_light_sleep_enter_cb_config != NULL && s_light_sleep_enter_cb_config->cb) { - uint32_t cycle = esp_cpu_get_cycle_count(); - esp_err_t err = esp_pm_execute_enter_sleep_callbacks(sleep_time_us); - if (err != ESP_OK) { - portEXIT_CRITICAL(&s_switch_lock); - return; - } - sleep_time_us -= (esp_cpu_get_cycle_count() - cycle) / (esp_clk_cpu_freq() / 1000000ULL); - } - if (sleep_time_us >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP * portTICK_PERIOD_MS * 1000LL) - { + uint32_t cycle = esp_cpu_get_cycle_count(); + esp_pm_execute_enter_sleep_callbacks(sleep_time_us); + sleep_time_us -= (esp_cpu_get_cycle_count() - cycle) / (esp_clk_cpu_freq() / 1000000ULL); #endif + if (sleep_time_us >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP * portTICK_PERIOD_MS * 1000LL) { esp_sleep_enable_timer_wakeup(sleep_time_us - LIGHT_SLEEP_EARLY_WAKEUP_US); #if CONFIG_PM_TRACE && SOC_PM_SUPPORT_RTC_PERIPH_PD /* to force tracing GPIOs to keep state */ @@ -823,13 +823,10 @@ void IRAM_ATTR vApplicationSleep( TickType_t xExpectedIdleTime ) #endif } other_core_should_skip_light_sleep(core_id); -#if CONFIG_PM_LIGHT_SLEEP_CALLBACKS - } - if (s_light_sleep_exit_cb_config != NULL && s_light_sleep_exit_cb_config->cb) { - esp_pm_execute_exit_sleep_callbacks(slept_us); - } -#endif } +#if CONFIG_PM_LIGHT_SLEEP_CALLBACKS + esp_pm_execute_exit_sleep_callbacks(slept_us); +#endif } portEXIT_CRITICAL(&s_switch_lock); }