fix(esp_hw_support): use same lock for lightsleep/deepsleep process

This commit is contained in:
wuzhenghui
2026-06-22 11:30:09 +08:00
parent aa222369e7
commit 988c8cb6ab

View File

@@ -290,10 +290,6 @@ static sleep_config_t s_config = {
expected when determining wakeup cause. */
static bool s_light_sleep_wakeup = false;
/* Updating RTC_MEMORY_CRC_REG register via set_rtc_memory_crc()
is not thread-safe, so we need to disable interrupts before going to deep sleep. */
static portMUX_TYPE spinlock_rtc_deep_sleep = portMUX_INITIALIZER_UNLOCKED;
static const char *TAG = "sleep";
/* APP core of esp32 can't access to RTC FAST MEMORY, do not define it with RTC_IRAM_ATTR */
@@ -452,28 +448,28 @@ esp_err_t esp_deep_sleep_try(uint64_t time_in_us)
static esp_err_t s_sleep_hook_register(esp_deep_sleep_cb_t new_cb, esp_deep_sleep_cb_t s_cb_array[MAX_DSLP_HOOKS])
{
portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
portENTER_CRITICAL(&s_config.lock);
for (int n = 0; n < MAX_DSLP_HOOKS; n++) {
if (s_cb_array[n]==NULL || s_cb_array[n]==new_cb) {
s_cb_array[n]=new_cb;
portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
portEXIT_CRITICAL(&s_config.lock);
return ESP_OK;
}
}
portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
portEXIT_CRITICAL(&s_config.lock);
ESP_LOGE(TAG, "Registered deepsleep callbacks exceeds MAX_DSLP_HOOKS");
return ESP_ERR_NO_MEM;
}
static void s_sleep_hook_deregister(esp_deep_sleep_cb_t old_cb, esp_deep_sleep_cb_t s_cb_array[MAX_DSLP_HOOKS])
{
portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
portENTER_CRITICAL(&s_config.lock);
for (int n = 0; n < MAX_DSLP_HOOKS; n++) {
if(s_cb_array[n] == old_cb) {
s_cb_array[n] = NULL;
}
}
portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
portEXIT_CRITICAL(&s_config.lock);
}
esp_err_t esp_deep_sleep_register_hook(esp_deep_sleep_cb_t new_dslp_cb)
@@ -1163,7 +1159,7 @@ static esp_err_t IRAM_ATTR deep_sleep_start(bool allow_sleep_rejection)
// Must acquire all spinlocks which may be acquired during sleep process before stalling other core,
// otherwise deadlock may occur.
portENTER_CRITICAL(&spinlock_rtc_deep_sleep);
portENTER_CRITICAL(&s_config.lock);
#if !CONFIG_FREERTOS_UNICORE
extern portMUX_TYPE rtc_spinlock;
portENTER_CRITICAL_SAFE(&rtc_spinlock); // Maybe acquired from temp_sensor_get_raw_value by phy_close_rf callback
@@ -1247,7 +1243,7 @@ static esp_err_t IRAM_ATTR deep_sleep_start(bool allow_sleep_rejection)
esp_clk_private_unlock();
portEXIT_CRITICAL_SAFE(&rtc_spinlock);
#endif
portEXIT_CRITICAL(&spinlock_rtc_deep_sleep);
portEXIT_CRITICAL(&s_config.lock);
return err;
}