From b115848dffcd5d7eefcfec878c31ee85562f82db Mon Sep 17 00:00:00 2001 From: zhuanghang Date: Fri, 23 Jan 2026 16:42:32 +0800 Subject: [PATCH] feat(802.15.4): get txpower table from phylib --- .../esp32c5/include/hal/ieee802154_ll.h | 4 -- .../esp32c6/include/hal/ieee802154_ll.h | 4 -- .../esp32h2/include/hal/ieee802154_ll.h | 4 -- .../esp32h4/include/hal/ieee802154_ll.h | 5 -- components/esp_phy/lib | 2 +- .../ieee802154/driver/esp_ieee802154_pib.c | 58 +++++++++++++++---- .../ieee802154/driver/esp_ieee802154_util.c | 8 ++- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/components/esp_hal_ieee802154/esp32c5/include/hal/ieee802154_ll.h b/components/esp_hal_ieee802154/esp32c5/include/hal/ieee802154_ll.h index d867995893..4f94a5224b 100644 --- a/components/esp_hal_ieee802154/esp32c5/include/hal/ieee802154_ll.h +++ b/components/esp_hal_ieee802154/esp32c5/include/hal/ieee802154_ll.h @@ -8,8 +8,4 @@ #include "hal/ieee802154_common_ll.h" -#define IEEE802154_TXPOWER_VALUE_MAX 20 -#define IEEE802154_TXPOWER_VALUE_MIN -15 -#define IEEE802154_TXPOWER_INDEX_MIN 3 - #define IEEE802154_RSSI_COMPENSATION_VALUE 0 diff --git a/components/esp_hal_ieee802154/esp32c6/include/hal/ieee802154_ll.h b/components/esp_hal_ieee802154/esp32c6/include/hal/ieee802154_ll.h index d867995893..4f94a5224b 100644 --- a/components/esp_hal_ieee802154/esp32c6/include/hal/ieee802154_ll.h +++ b/components/esp_hal_ieee802154/esp32c6/include/hal/ieee802154_ll.h @@ -8,8 +8,4 @@ #include "hal/ieee802154_common_ll.h" -#define IEEE802154_TXPOWER_VALUE_MAX 20 -#define IEEE802154_TXPOWER_VALUE_MIN -15 -#define IEEE802154_TXPOWER_INDEX_MIN 3 - #define IEEE802154_RSSI_COMPENSATION_VALUE 0 diff --git a/components/esp_hal_ieee802154/esp32h2/include/hal/ieee802154_ll.h b/components/esp_hal_ieee802154/esp32h2/include/hal/ieee802154_ll.h index ab1ad15e2a..6fc4a17ef6 100644 --- a/components/esp_hal_ieee802154/esp32h2/include/hal/ieee802154_ll.h +++ b/components/esp_hal_ieee802154/esp32h2/include/hal/ieee802154_ll.h @@ -14,10 +14,6 @@ extern "C" { extern uint32_t bt_bb_get_rssi_comp(void); -#define IEEE802154_TXPOWER_VALUE_MAX 20 -#define IEEE802154_TXPOWER_VALUE_MIN -24 -#define IEEE802154_TXPOWER_INDEX_MIN 0 - #define IEEE802154_RSSI_COMPENSATION_VALUE bt_bb_get_rssi_comp() #ifdef __cplusplus diff --git a/components/esp_hal_ieee802154/esp32h4/include/hal/ieee802154_ll.h b/components/esp_hal_ieee802154/esp32h4/include/hal/ieee802154_ll.h index 9e1c3b6b75..ad9913d4b6 100644 --- a/components/esp_hal_ieee802154/esp32h4/include/hal/ieee802154_ll.h +++ b/components/esp_hal_ieee802154/esp32h4/include/hal/ieee802154_ll.h @@ -12,11 +12,6 @@ extern "C" { #endif -// todo: TZ-2155 -#define IEEE802154_TXPOWER_VALUE_MAX 10 -#define IEEE802154_TXPOWER_VALUE_MIN -14 -#define IEEE802154_TXPOWER_INDEX_MIN 0 - #define IEEE802154_RSSI_COMPENSATION_VALUE 0 static inline uint32_t ieee802154_ll_get_rx_filter_not_work_cnt(void) diff --git a/components/esp_phy/lib b/components/esp_phy/lib index 3d57415af6..ac744ff2c5 160000 --- a/components/esp_phy/lib +++ b/components/esp_phy/lib @@ -1 +1 @@ -Subproject commit 3d57415af6e4c92eff2c4c3463e20a51d7340aba +Subproject commit ac744ff2c5c39c63f8cdd503d4074905647fdbb6 diff --git a/components/ieee802154/driver/esp_ieee802154_pib.c b/components/ieee802154/driver/esp_ieee802154_pib.c index bcfc13d290..65b410850a 100644 --- a/components/ieee802154/driver/esp_ieee802154_pib.c +++ b/components/ieee802154/driver/esp_ieee802154_pib.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,6 +15,30 @@ static ieee802154_pib_t s_ieee802154_pib; static bool is_pending = false; +extern const int8_t* bt_bb_get_tx_pwr_table(uint8_t *length); + +static inline int8_t IEEE802154_TXPOWER_VALUE_MAX(void) +{ + uint8_t length = 0; + const int8_t *tx_pwr_table = bt_bb_get_tx_pwr_table(&length); + if (!tx_pwr_table || length == 0) { + ESP_LOGE(IEEE802154_TAG, "Failed to get tx power table"); + return 0; + } + return tx_pwr_table[length - 1]; +} + +static inline int8_t IEEE802154_TXPOWER_VALUE_MIN(void) +{ + uint8_t length = 0; + const int8_t *tx_pwr_table = bt_bb_get_tx_pwr_table(&length); + if (!tx_pwr_table || length == 0) { + ESP_LOGE(IEEE802154_TAG, "Failed to get tx power table"); + return 0; + } + return tx_pwr_table[0]; +} + static inline void set_pending(void) { is_pending = true; @@ -42,22 +66,36 @@ void ieee802154_pib_init(void) s_ieee802154_pib.channel = 11; s_ieee802154_pib.cca_threshold = CONFIG_IEEE802154_CCA_THRESHOLD; s_ieee802154_pib.cca_mode = CONFIG_IEEE802154_CCA_MODE; - memset(&s_ieee802154_pib.power_table, IEEE802154_TXPOWER_VALUE_MAX, sizeof(s_ieee802154_pib.power_table)); + memset(&s_ieee802154_pib.power_table, IEEE802154_TXPOWER_VALUE_MAX(), sizeof(s_ieee802154_pib.power_table)); set_pending(); } IEEE802154_NOINLINE static uint8_t ieee802154_txpower_convert(int8_t txpower) { - uint8_t ieee820154_txpower_index = 0; - if (txpower >= IEEE802154_TXPOWER_VALUE_MAX) { - ieee820154_txpower_index = 15; - } else if (txpower <= IEEE802154_TXPOWER_VALUE_MIN) { - ieee820154_txpower_index = IEEE802154_TXPOWER_INDEX_MIN; - } else { - ieee820154_txpower_index = (uint8_t)((txpower - IEEE802154_TXPOWER_VALUE_MIN) / 3) + IEEE802154_TXPOWER_INDEX_MIN; + uint8_t ieee802154_txpower_index = 0; + uint8_t length = 0; + const int8_t *tx_pwr_table = bt_bb_get_tx_pwr_table(&length); + if (!tx_pwr_table || length == 0) { + ESP_LOGE(IEEE802154_TAG, "Failed to get tx power table"); + return 0; } - return ieee820154_txpower_index; + + if (txpower <= tx_pwr_table[0]) { + ieee802154_txpower_index = 0; + } else if (txpower >= tx_pwr_table[length - 1]) { + ieee802154_txpower_index = length - 1; + } else { + uint8_t i = 0; + for (i = length-1; i != 0; i--) { + if (tx_pwr_table[i] <= txpower) { + break; + } + } + ieee802154_txpower_index = i; + } + + return ieee802154_txpower_index; } void ieee802154_pib_update(void) diff --git a/components/ieee802154/driver/esp_ieee802154_util.c b/components/ieee802154/driver/esp_ieee802154_util.c index 73f31d2b52..3844b27ae8 100644 --- a/components/ieee802154/driver/esp_ieee802154_util.c +++ b/components/ieee802154/driver/esp_ieee802154_util.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -80,3 +80,9 @@ void ieee802154_etm_set_event_task(uint32_t channel, uint32_t event, uint32_t ta REG_WRITE(ETM_CHENSET_AD0_REG, (REG_READ(ETM_CHENSET_AD0_REG) | 1 << channel)); } + +__attribute__((weak)) const int8_t* bt_bb_get_tx_pwr_table(uint8_t *length) +{ + ESP_LOGE(IEEE802154_TAG, "bt_bb_get_tx_pwr_table is not implemented"); + return NULL; +}