Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_phy_pll_track_corner_case_backport_51' into …
Browse files Browse the repository at this point in the history
…'release/v5.1'

fix(phy): fix pll track when enabling and disabling PHY frequently(Backport V5.1)

See merge request espressif/esp-idf!29387
  • Loading branch information
jack0c committed Mar 5, 2024
2 parents 19022e0 + 006c058 commit 4395683
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
6 changes: 6 additions & 0 deletions components/esp_phy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@ menu "PHY"
high interference, enable this option will sacrifice Wi-Fi OFDM receive performance.
But to guarantee 11b receive performance serves as a bottom line in this case.

config ESP_PHY_PLL_TRACK_DEBUG
bool "Enable pll track logging"
default n
help
If enabled, there will be some logs while pll tracking

endmenu # PHY
8 changes: 7 additions & 1 deletion components/esp_phy/include/esp_private/phy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -174,6 +174,12 @@ esp_phy_modem_t phy_get_modem_flag(void);
*
*/
_lock_t phy_get_lock(void);

/**
* @brief Call this funnction to track pll immediately.
*
*/
void phy_track_pll(void);
#ifdef __cplusplus
}
#endif
51 changes: 34 additions & 17 deletions components/esp_phy/src/phy_common.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "esp_log.h"
#include "esp_timer.h"
#include "esp_phy_init.h"
#include "esp_private/phy.h"
Expand All @@ -20,6 +21,7 @@ static volatile int64_t s_wifi_prev_timestamp;
static volatile int64_t s_bt_154_prev_timestamp;
#endif
#define PHY_TRACK_PLL_PERIOD_IN_US 1000000
static void phy_track_pll_internal(void);

#if CONFIG_IEEE802154_ENABLED || CONFIG_BT_ENABLED || CONFIG_ESP_WIFI_ENABLED
bool phy_enabled_modem_contains(esp_phy_modem_t modem)
Expand All @@ -28,7 +30,28 @@ bool phy_enabled_modem_contains(esp_phy_modem_t modem)
}
#endif

static void phy_track_pll(void)
void phy_track_pll(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.
bool need_track_pll = false;
#if CONFIG_ESP_WIFI_ENABLED
if (phy_enabled_modem_contains(PHY_MODEM_WIFI)) {
need_track_pll = need_track_pll || ((esp_timer_get_time() - s_wifi_prev_timestamp) > PHY_TRACK_PLL_PERIOD_IN_US);
}
#endif
#if CONFIG_IEEE802154_ENABLED || CONFIG_BT_ENABLED
if (phy_enabled_modem_contains(PHY_MODEM_BT | PHY_MODEM_IEEE802154)) {
need_track_pll = need_track_pll || ((esp_timer_get_time() - s_bt_154_prev_timestamp) > PHY_TRACK_PLL_PERIOD_IN_US);
}
#endif
if (need_track_pll) {
phy_track_pll_internal();
}
}

static void phy_track_pll_internal(void)
{
bool wifi_track_pll = false;
bool ble_154_track_pll = false;
Expand All @@ -46,6 +69,14 @@ static void phy_track_pll(void)
}
#endif
if (wifi_track_pll || ble_154_track_pll) {
#if CONFIG_ESP_PHY_PLL_TRACK_DEBUG
#if CONFIG_IEEE802154_ENABLED || CONFIG_BT_ENABLED
ESP_LOGI("PLL_TRACK", "BT or IEEE802154 tracks PLL: %s", ble_154_track_pll ? "True" : "False");
#endif
#if CONFIG_ESP_WIFI_ENABLED
ESP_LOGI("PLL_TRACK", "Wi-Fi tracks PLL: %s", wifi_track_pll ? "True" : "False");
#endif
#endif
phy_param_track_tot(wifi_track_pll, ble_154_track_pll);
}
}
Expand All @@ -54,26 +85,12 @@ static void phy_track_pll_timer_callback(void* arg)
{
_lock_t phy_lock = phy_get_lock();
_lock_acquire(&phy_lock);
phy_track_pll();
phy_track_pll_internal();
_lock_release(&phy_lock);
}

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.
bool need_track_pll = false;
#if CONFIG_ESP_WIFI_ENABLED
need_track_pll = need_track_pll || ((esp_timer_get_time() - s_wifi_prev_timestamp) > PHY_TRACK_PLL_PERIOD_IN_US);
#endif
#if CONFIG_IEEE802154_ENABLED || CONFIG_BT_ENABLED
need_track_pll = need_track_pll || ((esp_timer_get_time() - s_bt_154_prev_timestamp) > PHY_TRACK_PLL_PERIOD_IN_US);
#endif
if (need_track_pll) {
phy_track_pll();
}

const esp_timer_create_args_t phy_track_pll_timer_args = {
.callback = &phy_track_pll_timer_callback,
.name = "phy-track-pll-timer"
Expand Down
4 changes: 4 additions & 0 deletions components/esp_phy/src/phy_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ void esp_phy_enable(esp_phy_modem_t modem)
#endif
}
phy_set_modem_flag(modem);
#if !CONFIG_IDF_TARGET_ESP32
// Immediately track pll when phy enabled.
phy_track_pll();
#endif

_lock_release(&s_phy_access_lock);
}
Expand Down
20 changes: 7 additions & 13 deletions components/esp_phy/src/phy_init_esp32hxx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -21,7 +21,6 @@ extern void phy_version_print(void);
static _lock_t s_phy_access_lock;

/* Reference count of enabling PHY */
static uint8_t s_phy_access_ref = 0;
static bool s_phy_is_enabled = false;

uint32_t IRAM_ATTR phy_enter_critical(void)
Expand Down Expand Up @@ -49,8 +48,7 @@ void IRAM_ATTR phy_exit_critical(uint32_t level)
void esp_phy_enable(esp_phy_modem_t modem)
{
_lock_acquire(&s_phy_access_lock);
phy_set_modem_flag(modem);
if (s_phy_access_ref == 0) {
if (phy_get_modem_flag() == 0) {
#if SOC_MODEM_CLOCK_IS_INDEPENDENT
modem_clock_module_enable(PERIPH_PHY_MODULE);
#endif
Expand All @@ -63,30 +61,26 @@ void esp_phy_enable(esp_phy_modem_t modem)
}
phy_track_pll_init();
}

s_phy_access_ref++;

phy_set_modem_flag(modem);
// Immediately track pll when phy enabled.
phy_track_pll();
_lock_release(&s_phy_access_lock);
}

void esp_phy_disable(esp_phy_modem_t modem)
{
_lock_acquire(&s_phy_access_lock);

if (s_phy_access_ref) {
s_phy_access_ref--;
}
phy_clr_modem_flag(modem);
if (phy_get_modem_flag() == 0) {

if (s_phy_access_ref == 0) {
phy_track_pll_deinit();
phy_close_rf();
phy_xpd_tsens();
#if SOC_MODEM_CLOCK_IS_INDEPENDENT
modem_clock_module_disable(PERIPH_PHY_MODULE);
#endif
}

phy_clr_modem_flag(modem);
_lock_release(&s_phy_access_lock);
}

Expand Down

0 comments on commit 4395683

Please sign in to comment.