From 0666a8df906fa692aa3e281bf6ea6dc4493f7ea3 Mon Sep 17 00:00:00 2001 From: sibeibei Date: Mon, 29 Dec 2025 20:44:58 +0800 Subject: [PATCH] fix: clear modem RF flag before PMU sleep to prevent open rf failed on next wake-up --- components/esp_hw_support/sleep_modem.c | 2 +- components/esp_phy/include/esp_private/phy.h | 12 ++++++++++++ components/esp_phy/src/phy_init.c | 19 +++++++++++++++++-- components/esp_wifi/Kconfig | 9 +++++++++ components/esp_wifi/src/wifi_init.c | 9 +++++++++ .../esp32c6/include/soc/Kconfig.soc_caps.in | 4 ++++ components/soc/esp32c6/include/soc/soc_caps.h | 2 ++ 7 files changed, 54 insertions(+), 3 deletions(-) diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 613b9dd8dc..5b89376edd 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -44,7 +44,7 @@ static _lock_t s_modem_prepare_lock; #endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD #if CONFIG_MAC_BB_PD -#define MAC_BB_POWER_DOWN_CB_NO (3) +#define MAC_BB_POWER_DOWN_CB_NO (4) #define MAC_BB_POWER_UP_CB_NO (3) static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO]; diff --git a/components/esp_phy/include/esp_private/phy.h b/components/esp_phy/include/esp_private/phy.h index 3a2c638761..c5b5d86d02 100644 --- a/components/esp_phy/include/esp_private/phy.h +++ b/components/esp_phy/include/esp_private/phy.h @@ -13,6 +13,7 @@ extern "C" { #endif #define ESP_CAL_DATA_CHECK_FAIL 1 +#define ESP_MODEM_RF_FLAG_UPDATE_CB_REQUIRED (SOC_PM_MODEM_RF_FLAG_UPDATE_WORKAROUND || CONFIG_ESP_WIFI_MODEM_RF_FLAG_UPDATE_DEBUG) typedef enum { PHY_I2C_MST_CMD_TYPE_OFF = 0, @@ -191,6 +192,17 @@ void phy_track_pll(void); */ void phy_wakeup_from_modem_state_extra_init(void); #endif + +#if SOC_PM_SUPPORT_PMU_MODEM_STATE && CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP && ESP_MODEM_RF_FLAG_UPDATE_CB_REQUIRED +/** + * @brief Update modem RF flag + * + * This function is called as a callback during MAC/BB power down operations. + * It checks if modem RF is already enabled and clears the RF power state accordingly. + */ +void esp_phy_modem_rf_flag_update(void); +#endif + #ifdef __cplusplus } #endif diff --git a/components/esp_phy/src/phy_init.c b/components/esp_phy/src/phy_init.c index 8bd2c9e4ca..67443da9b4 100644 --- a/components/esp_phy/src/phy_init.c +++ b/components/esp_phy/src/phy_init.c @@ -54,6 +54,11 @@ extern wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb; #endif +#if SOC_PM_SUPPORT_PMU_MODEM_STATE && CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP +extern void pm_mac_modem_clear_rf_power_state(void); +extern bool pm_mac_modem_rf_already_enabled(void); +#endif + static const char* TAG = "phy_init"; static _lock_t s_phy_access_lock; @@ -285,6 +290,18 @@ static inline void phy_digital_regs_load(void) } #endif // SOC_PM_MODEM_RETENTION_BY_BACKUPDMA +#if SOC_PM_SUPPORT_PMU_MODEM_STATE && CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP && ESP_MODEM_RF_FLAG_UPDATE_CB_REQUIRED +void IRAM_ATTR esp_phy_modem_rf_flag_update(void) +{ + if (pm_mac_modem_rf_already_enabled()) { +#if CONFIG_ESP_WIFI_MODEM_RF_FLAG_UPDATE_DEBUG + assert(0); +#endif + pm_mac_modem_clear_rf_power_state(); + } +} +#endif + void esp_phy_enable(esp_phy_modem_t modem) { _lock_acquire(&s_phy_access_lock); @@ -302,7 +319,6 @@ void esp_phy_enable(esp_phy_modem_t modem) s_is_phy_calibrated = true; } else { #if SOC_PM_SUPPORT_PMU_MODEM_STATE && CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP - extern bool pm_mac_modem_rf_already_enabled(void); if (!pm_mac_modem_rf_already_enabled()) { if (sleep_modem_wifi_modem_state_enabled() && sleep_modem_wifi_modem_link_done()) { sleep_modem_wifi_do_phy_retention(true); @@ -363,7 +379,6 @@ void esp_phy_disable(esp_phy_modem_t modem) phy_digital_regs_store(); #endif #if SOC_PM_SUPPORT_PMU_MODEM_STATE && CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP - extern void pm_mac_modem_clear_rf_power_state(void); pm_mac_modem_clear_rf_power_state(); if (sleep_modem_wifi_modem_state_enabled()) { sleep_modem_wifi_do_phy_retention(false); diff --git a/components/esp_wifi/Kconfig b/components/esp_wifi/Kconfig index 72a317e3c6..f987e5b512 100644 --- a/components/esp_wifi/Kconfig +++ b/components/esp_wifi/Kconfig @@ -677,4 +677,13 @@ menu "Wi-Fi" Select this configuration to free dynamic buffers during WiFi enterprise connection. This will enable chip to reduce heap consumption during WiFi enterprise connection. + config ESP_WIFI_MODEM_RF_FLAG_UPDATE_DEBUG + bool "Enable debug assertions for modem RF flag update" + depends on ESP_WIFI_ENHANCED_LIGHT_SLEEP + default n + help + Enable debug assertions to verify modem RF flag update operations. + This option enables assert checks to verify that modem RF power state + is correctly cleared before pmu sleep. + endmenu # Wi-Fi diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index 57c10d3a7d..85e2750b2b 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -216,7 +216,10 @@ static esp_err_t wifi_deinit_internal(void) #if CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP esp_wifi_internal_modem_state_configure(false); esp_pm_unregister_skip_light_sleep_callback(sleep_modem_wifi_modem_state_skip_light_sleep); +#if ESP_MODEM_RF_FLAG_UPDATE_CB_REQUIRED + esp_unregister_mac_bb_pd_callback(esp_phy_modem_rf_flag_update); #endif +#endif /* CONFIG_ESP_WIFI_ENHANCED_LIGHT_SLEEP */ esp_phy_modem_deinit(); s_wifi_inited = false; @@ -401,6 +404,12 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) if (sleep_modem_wifi_modem_state_enabled()) { esp_pm_register_skip_light_sleep_callback(sleep_modem_wifi_modem_state_skip_light_sleep); esp_wifi_internal_modem_state_configure(true); /* require WiFi to enable automatically receives the beacon */ +#if ESP_MODEM_RF_FLAG_UPDATE_CB_REQUIRED + if (esp_register_mac_bb_pd_callback(esp_phy_modem_rf_flag_update) != ESP_OK) { + ESP_LOGE(TAG, "Failed to register modem RF flag update callback"); + goto _deinit; + } +#endif } #endif #if CONFIG_IDF_TARGET_ESP32 diff --git a/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in b/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in index 57a4ab170a..a2c016c559 100644 --- a/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32c6/include/soc/Kconfig.soc_caps.in @@ -1195,6 +1195,10 @@ config SOC_PM_RETENTION_HAS_CLOCK_BUG bool default y +config SOC_PM_MODEM_RF_FLAG_UPDATE_WORKAROUND + bool + default y + config SOC_PM_PAU_LINK_NUM int default 4 diff --git a/components/soc/esp32c6/include/soc/soc_caps.h b/components/soc/esp32c6/include/soc/soc_caps.h index 57e4c43876..48ecc212a5 100644 --- a/components/soc/esp32c6/include/soc/soc_caps.h +++ b/components/soc/esp32c6/include/soc/soc_caps.h @@ -500,6 +500,8 @@ #define SOC_PM_MODEM_RETENTION_BY_REGDMA (1) #define SOC_PM_RETENTION_HAS_CLOCK_BUG (1) +#define SOC_PM_MODEM_RF_FLAG_UPDATE_WORKAROUND (1) + #define SOC_PM_PAU_LINK_NUM (4) #define SOC_PM_PAU_REGDMA_UPDATE_CACHE_BEFORE_WAIT_COMPARE (1)