Skip to content

Commit

Permalink
bleutooth: add ble tx power configuration
Browse files Browse the repository at this point in the history
Allow bluetooth controller to be initialized with
default custom tx power level.

Signed-off-by: Sylvio Alves <[email protected]>
  • Loading branch information
sylvioalves committed Nov 1, 2024
1 parent 0f18742 commit f799d1d
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
7 changes: 7 additions & 0 deletions zephyr/esp32/include/bt/esp_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions zephyr/esp32/src/bt/esp_bt_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion zephyr/esp32c3/include/bt/esp_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion zephyr/esp32c3/src/bt/esp_bt_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1566,4 +1566,28 @@ static void * coex_schm_curr_phase_get_wrapper(void)
#else
return NULL;
#endif
}
}

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;
}
9 changes: 8 additions & 1 deletion zephyr/esp32s3/include/bt/esp_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions zephyr/esp32s3/src/bt/esp_bt_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit f799d1d

Please sign in to comment.