change(pm): Change sleep callback implement

This commit is contained in:
Lou Tianhao 2023-10-13 16:19:53 +08:00 committed by BOT
parent 767595a22f
commit 13a405a32b
4 changed files with 41 additions and 45 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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();

View File

@ -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);
}