diff --git a/components/esp_phy/CMakeLists.txt b/components/esp_phy/CMakeLists.txt index 6b68cc9596..f6cea7452d 100644 --- a/components/esp_phy/CMakeLists.txt +++ b/components/esp_phy/CMakeLists.txt @@ -1,6 +1,6 @@ idf_build_get_property(idf_target IDF_TARGET) -set(srcs "src/phy_override.c" "src/lib_printf.c") +set(srcs "src/phy_override.c" "src/lib_printf.c" "src/phy_common.c") if(CONFIG_APP_NO_BLOBS) set(link_binary_libs 0) diff --git a/components/esp_phy/include/esp_phy_init.h b/components/esp_phy/include/esp_phy_init.h index 4813e5bdee..46c093348e 100644 --- a/components/esp_phy/include/esp_phy_init.h +++ b/components/esp_phy/include/esp_phy_init.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -271,6 +271,17 @@ esp_err_t esp_phy_apply_phy_init_data(uint8_t *init_data); */ char * get_phy_version_str(void); +/** + * @brief Enable phy track pll + * + */ +void phy_track_pll_init(void); + +/** + * @brief Disable phy track pll + * + */ +void phy_track_pll_deinit(void); #ifdef __cplusplus } #endif diff --git a/components/esp_phy/src/phy_common.c b/components/esp_phy/src/phy_common.c new file mode 100644 index 0000000000..b71b3aa0e8 --- /dev/null +++ b/components/esp_phy/src/phy_common.c @@ -0,0 +1,46 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_timer.h" +#include + + +extern void bt_track_pll_cap(void); +static esp_timer_handle_t phy_track_pll_timer; +static volatile int64_t s_previous_timestamp; +#define PHY_TRACK_PLL_PERIOD_IN_US 1000000 + +static void phy_track_pll_timer_callback(void* arg) +{ +#if IEEE802154_ENABLED || BT_ENABLED + bt_track_pll_cap(); +#endif + s_previous_timestamp = esp_timer_get_time(); +} + +void phy_track_pll_init(void) +{ + // Light sleep scenario: enabling and disabling PHY frequently, the timer will not get triggered. + // Using a variable to record the previously tracked time when PLL was last called. + // If the duration is larger than PHY_TRACK_PLL_PERIOD_IN_US, then track PLL. + int64_t now = esp_timer_get_time(); + if (now - s_previous_timestamp > PHY_TRACK_PLL_PERIOD_IN_US) { + phy_track_pll_timer_callback((void* )0); + } + + const esp_timer_create_args_t phy_track_pll_timer_args = { + .callback = &phy_track_pll_timer_callback, + .name = "phy-track-pll-timer" + }; + ESP_ERROR_CHECK(esp_timer_create(&phy_track_pll_timer_args, &phy_track_pll_timer)); + ESP_ERROR_CHECK(esp_timer_start_periodic(phy_track_pll_timer, PHY_TRACK_PLL_PERIOD_IN_US)); +} + +void phy_track_pll_deinit(void) +{ + ESP_ERROR_CHECK(esp_timer_stop(phy_track_pll_timer)); + ESP_ERROR_CHECK(esp_timer_delete(phy_track_pll_timer)); +} diff --git a/components/esp_phy/src/phy_init_esp32hxx.c b/components/esp_phy/src/phy_init_esp32hxx.c index 36c6f4965a..52d7722a65 100644 --- a/components/esp_phy/src/phy_init_esp32hxx.c +++ b/components/esp_phy/src/phy_init_esp32hxx.c @@ -53,6 +53,7 @@ void esp_phy_enable(void) } else { phy_wakeup_init(); } + phy_track_pll_init(); } s_phy_access_ref++; @@ -69,6 +70,7 @@ void esp_phy_disable(void) } if (s_phy_access_ref == 0) { + phy_track_pll_deinit(); phy_close_rf(); phy_xpd_tsens(); }