From f799d1d5f61ab61a2a9bca90ee981d0f7927a454 Mon Sep 17 00:00:00 2001 From: Sylvio Alves Date: Thu, 31 Oct 2024 20:38:41 -0300 Subject: [PATCH] bleutooth: add ble tx power configuration Allow bluetooth controller to be initialized with default custom tx power level. Signed-off-by: Sylvio Alves --- zephyr/esp32/include/bt/esp_bt.h | 7 +++++++ zephyr/esp32/src/bt/esp_bt_adapter.c | 20 ++++++++++++++++++++ zephyr/esp32c3/include/bt/esp_bt.h | 9 ++++++++- zephyr/esp32c3/src/bt/esp_bt_adapter.c | 26 +++++++++++++++++++++++++- zephyr/esp32s3/include/bt/esp_bt.h | 9 ++++++++- zephyr/esp32s3/src/bt/esp_bt_adapter.c | 24 ++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 3 deletions(-) diff --git a/zephyr/esp32/include/bt/esp_bt.h b/zephyr/esp32/include/bt/esp_bt.h index 49d98ddd11..e766273b37 100644 --- a/zephyr/esp32/include/bt/esp_bt.h +++ b/zephyr/esp32/include/bt/esp_bt.h @@ -643,6 +643,13 @@ void esp_wifi_bt_power_domain_on(void); */ void esp_wifi_bt_power_domain_off(void); +/** + * @brief Convert Zephyr TX power enum to ESP BT model + * + * @return ESP BT controller power level + */ +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power); + #ifdef __cplusplus } #endif diff --git a/zephyr/esp32/src/bt/esp_bt_adapter.c b/zephyr/esp32/src/bt/esp_bt_adapter.c index 3af22c0dd2..76adf5da05 100644 --- a/zephyr/esp32/src/bt/esp_bt_adapter.c +++ b/zephyr/esp32/src/bt/esp_bt_adapter.c @@ -1262,6 +1262,10 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode) btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED; + /* set default TX power level */ + esp_power_level_t level = zephyr_to_esp_ble_power_level(CONFIG_BT_CTLR_TX_PWR_DBM); + esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, level); + return ESP_OK; } @@ -1398,3 +1402,19 @@ static void esp_bt_free(void *mem) { esp_bt_free_func(mem); } + +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power) { + // Map the Zephyr power levels to the closest ESP-IDF levels + if (tx_power == 9) return ESP_PWR_LVL_P9; + else if (tx_power == 6) return ESP_PWR_LVL_P6; + else if (tx_power == 3) return ESP_PWR_LVL_P3; + else if (tx_power == 0) return ESP_PWR_LVL_N0; + else if (tx_power == -3) return ESP_PWR_LVL_N3; + else if (tx_power == -6) return ESP_PWR_LVL_N6; + else if (tx_power == -9) return ESP_PWR_LVL_N9; + else if (tx_power == -12) return ESP_PWR_LVL_N12; + + LOG_ERR("Invalid TX power level: %d", tx_power); + + return 0xFF; +} diff --git a/zephyr/esp32c3/include/bt/esp_bt.h b/zephyr/esp32c3/include/bt/esp_bt.h index 63d0945e18..8c06764e5c 100644 --- a/zephyr/esp32c3/include/bt/esp_bt.h +++ b/zephyr/esp32c3/include/bt/esp_bt.h @@ -240,7 +240,7 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); .hci_tl_funcs = NULL, \ .txant_dft = 0, \ .rxant_dft = 0, \ - .txpwr_dft = 8, \ + .txpwr_dft = zephyr_to_esp_ble_power_level(CONFIG_BT_CTLR_TX_PWR_DBM), \ .cfg_mask = CFG_MASK, \ .scan_duplicate_mode = SCAN_DUPLICATE_MODE, \ .scan_duplicate_type = SCAN_DUPLICATE_TYPE_VALUE, \ @@ -659,6 +659,13 @@ void esp_wifi_bt_power_domain_off(void); */ esp_bt_sleep_clock_t esp_bt_get_lpclk_src(void); +/** + * @brief Convert Zephyr TX power enum to ESP BT model + * + * @return ESP BT controller power level + */ +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power); + #ifdef __cplusplus } #endif diff --git a/zephyr/esp32c3/src/bt/esp_bt_adapter.c b/zephyr/esp32c3/src/bt/esp_bt_adapter.c index ebe589a128..6d26e4e2c5 100644 --- a/zephyr/esp32c3/src/bt/esp_bt_adapter.c +++ b/zephyr/esp32c3/src/bt/esp_bt_adapter.c @@ -1566,4 +1566,28 @@ static void * coex_schm_curr_phase_get_wrapper(void) #else return NULL; #endif -} \ No newline at end of file +} + +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power) { + // Map the Zephyr power levels to the closest ESP-IDF levels + if (tx_power == 21) return ESP_PWR_LVL_P21; + else if (tx_power == 18) return ESP_PWR_LVL_P18; + else if (tx_power == 15) return ESP_PWR_LVL_P15; + else if (tx_power == 12) return ESP_PWR_LVL_P12; + else if (tx_power == 9) return ESP_PWR_LVL_P9; + else if (tx_power == 6) return ESP_PWR_LVL_P6; + else if (tx_power == 3) return ESP_PWR_LVL_P3; + else if (tx_power == 0) return ESP_PWR_LVL_N0; + else if (tx_power == -3) return ESP_PWR_LVL_N3; + else if (tx_power == -6) return ESP_PWR_LVL_N6; + else if (tx_power == -9) return ESP_PWR_LVL_N9; + else if (tx_power == -12) return ESP_PWR_LVL_N12; + else if (tx_power == -15) return ESP_PWR_LVL_N15; + else if (tx_power == -18) return ESP_PWR_LVL_N18; + else if (tx_power == -21) return ESP_PWR_LVL_N21; + else if (tx_power == -24) return ESP_PWR_LVL_N24; + + LOG_ERR("Invalid TX power level: %d", tx_power); + + return ESP_PWR_LVL_INVALID; +} diff --git a/zephyr/esp32s3/include/bt/esp_bt.h b/zephyr/esp32s3/include/bt/esp_bt.h index 63d0945e18..8c06764e5c 100644 --- a/zephyr/esp32s3/include/bt/esp_bt.h +++ b/zephyr/esp32s3/include/bt/esp_bt.h @@ -240,7 +240,7 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); .hci_tl_funcs = NULL, \ .txant_dft = 0, \ .rxant_dft = 0, \ - .txpwr_dft = 8, \ + .txpwr_dft = zephyr_to_esp_ble_power_level(CONFIG_BT_CTLR_TX_PWR_DBM), \ .cfg_mask = CFG_MASK, \ .scan_duplicate_mode = SCAN_DUPLICATE_MODE, \ .scan_duplicate_type = SCAN_DUPLICATE_TYPE_VALUE, \ @@ -659,6 +659,13 @@ void esp_wifi_bt_power_domain_off(void); */ esp_bt_sleep_clock_t esp_bt_get_lpclk_src(void); +/** + * @brief Convert Zephyr TX power enum to ESP BT model + * + * @return ESP BT controller power level + */ +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power); + #ifdef __cplusplus } #endif diff --git a/zephyr/esp32s3/src/bt/esp_bt_adapter.c b/zephyr/esp32s3/src/bt/esp_bt_adapter.c index 32d87cb507..8dd2e4f5a2 100644 --- a/zephyr/esp32s3/src/bt/esp_bt_adapter.c +++ b/zephyr/esp32s3/src/bt/esp_bt_adapter.c @@ -1552,3 +1552,27 @@ static void esp_bt_free(void *mem) { esp_bt_free_func(mem); } + +esp_power_level_t zephyr_to_esp_ble_power_level(int tx_power) { + // Map the Zephyr power levels to the closest ESP-IDF levels + if (tx_power == 21) return ESP_PWR_LVL_P21; + else if (tx_power == 18) return ESP_PWR_LVL_P18; + else if (tx_power == 15) return ESP_PWR_LVL_P15; + else if (tx_power == 12) return ESP_PWR_LVL_P12; + else if (tx_power == 9) return ESP_PWR_LVL_P9; + else if (tx_power == 6) return ESP_PWR_LVL_P6; + else if (tx_power == 3) return ESP_PWR_LVL_P3; + else if (tx_power == 0) return ESP_PWR_LVL_N0; + else if (tx_power == -3) return ESP_PWR_LVL_N3; + else if (tx_power == -6) return ESP_PWR_LVL_N6; + else if (tx_power == -9) return ESP_PWR_LVL_N9; + else if (tx_power == -12) return ESP_PWR_LVL_N12; + else if (tx_power == -15) return ESP_PWR_LVL_N15; + else if (tx_power == -18) return ESP_PWR_LVL_N18; + else if (tx_power == -21) return ESP_PWR_LVL_N21; + else if (tx_power == -24) return ESP_PWR_LVL_N24; + + LOG_ERR("Invalid TX power level: %d", tx_power); + + return ESP_PWR_LVL_INVALID; +}