mirror of
https://github.com/espressif/esp-idf.git
synced 2025-10-08 05:48:36 +00:00
feat(phy): support to query phy used time
This commit is contained in:
@@ -161,6 +161,60 @@ static phy_country_to_bin_type_t s_country_code_map_type_table[] = {
|
||||
{"US", ESP_PHY_INIT_DATA_TYPE_FCC},
|
||||
};
|
||||
#endif
|
||||
|
||||
#if CONFIG_ESP_PHY_RECORD_USED_TIME
|
||||
#define ESP_PHY_MODEM_COUNT_MAX (__builtin_ffs(PHY_MODEM_MAX - 1))
|
||||
#define ESP_PHY_IS_VALID_MODEM(modem) (__builtin_popcount(modem) == 1 && __builtin_ctz(modem) < ESP_PHY_MODEM_COUNT_MAX)
|
||||
|
||||
static DRAM_ATTR struct {
|
||||
uint64_t used_time;
|
||||
uint64_t enabled_time;
|
||||
uint64_t disabled_time;
|
||||
} s_phy_rf_used_info[ESP_PHY_MODEM_COUNT_MAX];
|
||||
|
||||
static IRAM_ATTR void phy_record_time(bool enabled, esp_phy_modem_t modem) {
|
||||
uint8_t index = __builtin_ctz(modem);
|
||||
if (enabled) {
|
||||
s_phy_rf_used_info[index].enabled_time = esp_timer_get_time();
|
||||
} else {
|
||||
s_phy_rf_used_info[index].disabled_time = esp_timer_get_time();
|
||||
s_phy_rf_used_info[index].used_time += s_phy_rf_used_info[index].disabled_time - s_phy_rf_used_info[index].enabled_time;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t phy_query_used_time(uint64_t *used_time, esp_phy_modem_t modem) {
|
||||
if (!ESP_PHY_IS_VALID_MODEM(modem)) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
uint8_t index = __builtin_ctz(modem);
|
||||
_lock_acquire(&s_phy_access_lock);
|
||||
*used_time = s_phy_rf_used_info[index].used_time;
|
||||
if (s_phy_rf_used_info[index].disabled_time < s_phy_rf_used_info[index].enabled_time) {
|
||||
// phy is being used
|
||||
*used_time += esp_timer_get_time() - s_phy_rf_used_info[index].enabled_time;
|
||||
}
|
||||
_lock_release(&s_phy_access_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t phy_clear_used_time(esp_phy_modem_t modem) {
|
||||
if (!ESP_PHY_IS_VALID_MODEM(modem)) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
uint8_t index = __builtin_ctz(modem);
|
||||
_lock_acquire(&s_phy_access_lock);
|
||||
if (s_phy_rf_used_info[index].enabled_time > s_phy_rf_used_info[index].disabled_time) {
|
||||
// phy is being used
|
||||
s_phy_rf_used_info[index].enabled_time = esp_timer_get_time();
|
||||
} else {
|
||||
s_phy_rf_used_info[index].enabled_time = s_phy_rf_used_info[index].disabled_time;
|
||||
}
|
||||
s_phy_rf_used_info[index].used_time = 0;
|
||||
_lock_release(&s_phy_access_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
uint32_t IRAM_ATTR phy_enter_critical(void)
|
||||
{
|
||||
if (xPortInIsrContext()) {
|
||||
@@ -293,13 +347,18 @@ void esp_phy_enable(esp_phy_modem_t modem)
|
||||
phy_track_pll();
|
||||
#endif
|
||||
|
||||
#if CONFIG_ESP_PHY_RECORD_USED_TIME
|
||||
phy_record_time(true, modem);
|
||||
#endif
|
||||
_lock_release(&s_phy_access_lock);
|
||||
}
|
||||
|
||||
void esp_phy_disable(esp_phy_modem_t modem)
|
||||
{
|
||||
_lock_acquire(&s_phy_access_lock);
|
||||
|
||||
#if CONFIG_ESP_PHY_RECORD_USED_TIME
|
||||
phy_record_time(false, modem);
|
||||
#endif
|
||||
phy_clr_modem_flag(modem);
|
||||
if (phy_get_modem_flag() == 0) {
|
||||
// ESP32 will track pll in the wifi/BT modem interrupt handler.
|
||||
|
Reference in New Issue
Block a user