Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bleutooth: add ble tx power configuration #355

Open
wants to merge 1 commit into
base: zephyr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a code style with opening parentheses on the new line?

// 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here and in other esp_bt_adapter.c

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change this approach and ping you back.

// 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;
}