Skip to content

Commit

Permalink
change(pm): Change sleep callback implement
Browse files Browse the repository at this point in the history
  • Loading branch information
10086loutianhao authored and espressif-bot committed Nov 2, 2023
1 parent 925da11 commit 42ec7a5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
8 changes: 3 additions & 5 deletions components/esp_hw_support/include/esp_private/sleep_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions components/esp_hw_support/sleep_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
51 changes: 24 additions & 27 deletions components/esp_pm/pm_impl.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -206,7 +206,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.
*/
Expand All @@ -223,7 +223,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;
Expand Down Expand Up @@ -321,24 +323,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

Expand Down Expand Up @@ -768,21 +776,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 */
Expand Down Expand Up @@ -821,13 +821,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);
}
esp_pm_execute_exit_sleep_callbacks(slept_us);
#endif
}
}
portEXIT_CRITICAL(&s_switch_lock);
}
Expand Down

0 comments on commit 42ec7a5

Please sign in to comment.