From 15d88a149b2920d2cb1f700166970dcd40fc423e Mon Sep 17 00:00:00 2001 From: Wang Mengyang Date: Sun, 3 Aug 2025 19:18:58 +0800 Subject: [PATCH] fix(esp_phy): Add protection of consecutive disable operations by single modem source --- components/esp_phy/src/phy_common.c | 15 ++++++++++++++- components/esp_phy/src/phy_init.c | 3 ++- components/esp_phy/src/phy_init_esp32hxx.c | 5 +++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/components/esp_phy/src/phy_common.c b/components/esp_phy/src/phy_common.c index b6097fdc38..eb5e666d3f 100644 --- a/components/esp_phy/src/phy_common.c +++ b/components/esp_phy/src/phy_common.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -126,6 +126,16 @@ void phy_track_pll_deinit(void) } #endif +static const char *phy_get_modem_str(esp_phy_modem_t modem) +{ + switch (modem) { + case PHY_MODEM_WIFI: return "Wi-Fi"; + case PHY_MODEM_BT: return "Bluetooth"; + case PHY_MODEM_IEEE802154: return "IEEE 802.15.4"; + default: return ""; + } +} + void phy_set_modem_flag(esp_phy_modem_t modem) { s_phy_modem_flag |= modem; @@ -133,6 +143,9 @@ void phy_set_modem_flag(esp_phy_modem_t modem) void phy_clr_modem_flag(esp_phy_modem_t modem) { + if ((s_phy_modem_flag & modem) == 0) { + ESP_LOGW(TAG, "Clear the flag of %s before it's set", phy_get_modem_str(modem)); + } s_phy_modem_flag &= ~modem; } diff --git a/components/esp_phy/src/phy_init.c b/components/esp_phy/src/phy_init.c index c33b6cb5d9..16ca899e31 100644 --- a/components/esp_phy/src/phy_init.c +++ b/components/esp_phy/src/phy_init.c @@ -357,8 +357,9 @@ void esp_phy_disable(esp_phy_modem_t modem) #if CONFIG_ESP_PHY_RECORD_USED_TIME phy_record_time(false, modem); #endif + esp_phy_modem_t saved_modem = phy_get_modem_flag(); phy_clr_modem_flag(modem); - if (phy_get_modem_flag() == 0) { + if (saved_modem == modem) { // ESP32 will track pll in the wifi/BT modem interrupt handler. #if !CONFIG_IDF_TARGET_ESP32 && !CONFIG_ESP_PHY_DISABLE_PLL_TRACK phy_track_pll_deinit(); diff --git a/components/esp_phy/src/phy_init_esp32hxx.c b/components/esp_phy/src/phy_init_esp32hxx.c index af45ae733a..a3de469155 100644 --- a/components/esp_phy/src/phy_init_esp32hxx.c +++ b/components/esp_phy/src/phy_init_esp32hxx.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -134,8 +134,9 @@ void esp_phy_disable(esp_phy_modem_t modem) #if CONFIG_ESP_PHY_RECORD_USED_TIME phy_record_time(false, modem); #endif + esp_phy_modem_t saved_modem = phy_get_modem_flag(); phy_clr_modem_flag(modem); - if (phy_get_modem_flag() == 0) { + if (saved_modem == modem) { #if !CONFIG_ESP_PHY_DISABLE_PLL_TRACK phy_track_pll_deinit();