From f1066f5f34445eb752b2900b8186af91b3bdd189 Mon Sep 17 00:00:00 2001 From: wuzhenghui Date: Wed, 16 Jul 2025 14:26:29 +0800 Subject: [PATCH] change(esp_hw_support): warning in esp_sleep_pd_config instead of assert Closes https://github.com/espressif/esp-idf/issues/16872#event-18649904083 --- components/esp_hw_support/sleep_modes.c | 30 +++++++++++++++++++------ 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index f793a314a0..514979ff94 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -2250,17 +2250,33 @@ esp_err_t esp_sleep_pd_config(esp_sleep_pd_domain_t domain, esp_sleep_pd_option_ if (domain >= ESP_PD_DOMAIN_MAX || option > ESP_PD_OPTION_AUTO) { return ESP_ERR_INVALID_ARG; } + esp_err_t err = ESP_OK; portENTER_CRITICAL_SAFE(&s_config.lock); - - int refs = (option == ESP_PD_OPTION_ON) ? s_config.domain[domain].refs++ \ - : (option == ESP_PD_OPTION_OFF) ? --s_config.domain[domain].refs \ - : s_config.domain[domain].refs; - if (refs == 0) { + int refs = 0; + if (s_config.domain[domain].pd_option == ESP_PD_OPTION_AUTO) { + s_config.domain[domain].refs = (option == ESP_PD_OPTION_ON) ? 1 : 0; s_config.domain[domain].pd_option = option; + } else { + if (option == ESP_PD_OPTION_AUTO) { + s_config.domain[domain].refs = 0; + s_config.domain[domain].pd_option = option; + } else { + refs = (option == ESP_PD_OPTION_ON) ? s_config.domain[domain].refs++ \ + : (option == ESP_PD_OPTION_OFF) ? --s_config.domain[domain].refs \ + : s_config.domain[domain].refs; + if (refs == 0) { + s_config.domain[domain].pd_option = option; + } else if (refs < 0) { + s_config.domain[domain].refs = 0; + err = ESP_ERR_INVALID_STATE; + } + } } portEXIT_CRITICAL_SAFE(&s_config.lock); - assert(refs >= 0); - return ESP_OK; + if (err == ESP_ERR_INVALID_STATE) { + ESP_LOGE(TAG, "Domain is already in ESP_PD_OPTION_OFF state, please check whether the domain pd_option is managed symmetrically."); + } + return err; } static const char* s_submode2str[] = {