Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_some_ble_bug_v5.0' into 'release/v5.0'
Browse files Browse the repository at this point in the history
Fixed some BLE bugs (backport v5.0)

See merge request espressif/esp-idf!26031
  • Loading branch information
Isl2017 committed Sep 19, 2023
2 parents 69b0a57 + 9af8a1e commit caf6660
Show file tree
Hide file tree
Showing 32 changed files with 346 additions and 88 deletions.
24 changes: 19 additions & 5 deletions components/bt/controller/esp32c3/Kconfig.in
Expand Up @@ -78,15 +78,29 @@ config BT_CTRL_ADV_DUP_FILT_MAX
help
The maxinum number of suplicate scan filter

config BT_CTRL_HW_CCA
bool "HW CCA check enable"
default n
choice BT_BLE_CCA_MODE
prompt "BLE CCA mode"
default BT_BLE_CCA_MODE_NONE
help
It enables HW CCA feature in controller
Define BT BLE CCA mode

config BT_BLE_CCA_MODE_NONE
bool "NONE"
config BT_BLE_CCA_MODE_HW
bool "Hardware"
config BT_BLE_CCA_MODE_SW
bool "Software"
endchoice

config BT_BLE_CCA_MODE
int
default 0 if BT_BLE_CCA_MODE_NONE
default 1 if BT_BLE_CCA_MODE_HW
default 2 if BT_BLE_CCA_MODE_SW

config BT_CTRL_HW_CCA_VAL
int "CCA threshold value"
range 20 60
range 20 100
default 20
help
It is the threshold value of HW CCA, if the value is 30, it means CCA threshold is -30 dBm.
Expand Down
43 changes: 32 additions & 11 deletions components/bt/controller/esp32c3/bt.c
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -111,7 +111,7 @@ do{\
} while(0)

#define OSI_FUNCS_TIME_BLOCKING 0xffffffff
#define OSI_VERSION 0x00010006
#define OSI_VERSION 0x00010007
#define OSI_MAGIC_VALUE 0xFADEBEAD

/* Types definition
Expand Down Expand Up @@ -193,6 +193,8 @@ struct osi_funcs_t {
void (* _esp_hw_power_down)(void);
void (* _esp_hw_power_up)(void);
void (* _ets_backup_dma_copy)(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_rem);
void (* _ets_delay_us)(uint32_t us);
void (* _btdm_rom_table_ready)(void);
};


Expand Down Expand Up @@ -252,6 +254,8 @@ extern void esp_mac_bb_power_up(void);
extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
#endif

extern void btdm_cca_feature_enable(void);

extern uint32_t _bt_bss_start;
extern uint32_t _bt_bss_end;
extern uint32_t _btdm_bss_start;
Expand Down Expand Up @@ -311,6 +315,7 @@ static void interrupt_off_wrapper(int intr_num);
static void btdm_hw_mac_power_up_wrapper(void);
static void btdm_hw_mac_power_down_wrapper(void);
static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32_t num, bool to_mem);
static void btdm_funcs_table_ready_wrapper(void);

static void btdm_slp_tmr_callback(void *arg);

Expand Down Expand Up @@ -375,6 +380,8 @@ static const struct osi_funcs_t osi_funcs_ro = {
._esp_hw_power_down = btdm_hw_mac_power_down_wrapper,
._esp_hw_power_up = btdm_hw_mac_power_up_wrapper,
._ets_backup_dma_copy = btdm_backup_dma_copy_wrapper,
._ets_delay_us = esp_rom_delay_us,
._btdm_rom_table_ready = btdm_funcs_table_ready_wrapper,
};

static DRAM_ATTR struct osi_funcs_t *osi_funcs_p;
Expand Down Expand Up @@ -899,6 +906,13 @@ static void async_wakeup_request_end(int event)
return;
}

static void btdm_funcs_table_ready_wrapper(void)
{
#if BT_BLE_CCA_MODE == 2
btdm_cca_feature_enable();
#endif
}

static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
{
#if CONFIG_SW_COEXIST_ENABLE
Expand Down Expand Up @@ -1327,14 +1341,10 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
periph_module_enable(PERIPH_BT_MODULE);
periph_module_reset(PERIPH_BT_MODULE);

esp_phy_enable();
s_lp_stat.phy_enabled = 1;

if (btdm_controller_init(cfg) != 0) {
err = ESP_ERR_NO_MEM;
goto error;
}
coex_pti_v2();

btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;

Expand Down Expand Up @@ -1364,11 +1374,6 @@ static void bt_controller_deinit_internal(void)
{
periph_module_disable(PERIPH_BT_MODULE);

if (s_lp_stat.phy_enabled) {
esp_phy_disable();
s_lp_stat.phy_enabled = 0;
}

// deinit low power control resources
do {

Expand Down Expand Up @@ -1462,6 +1467,12 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
return ESP_ERR_INVALID_ARG;
}

/* Enable PHY when enabling controller to reduce power dissipation after controller init
* Notice the init order: esp_phy_enable() -> bt_bb_v2_init_cmplx() -> coex_pti_v2()
*/
esp_phy_enable();
s_lp_stat.phy_enabled = 1;

#if CONFIG_SW_COEXIST_ENABLE
coex_enable();
#endif
Expand All @@ -1486,6 +1497,8 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
goto error;
}

coex_pti_v2();

btdm_controller_status = ESP_BT_CONTROLLER_STATUS_ENABLED;

return ret;
Expand All @@ -1508,6 +1521,10 @@ esp_err_t esp_bt_controller_enable(esp_bt_mode_t mode)
#if CONFIG_SW_COEXIST_ENABLE
coex_disable();
#endif
if (s_lp_stat.phy_enabled) {
esp_phy_disable();
s_lp_stat.phy_enabled = 0;
}
return ret;
}

Expand All @@ -1526,6 +1543,10 @@ esp_err_t esp_bt_controller_disable(void)
#if CONFIG_SW_COEXIST_ENABLE
coex_disable();
#endif
if (s_lp_stat.phy_enabled) {
esp_phy_disable();
s_lp_stat.phy_enabled = 0;
}

btdm_controller_status = ESP_BT_CONTROLLER_STATUS_INITED;

Expand Down
2 changes: 1 addition & 1 deletion components/bt/controller/lib_esp32c3_family
14 changes: 14 additions & 0 deletions components/bt/host/bluedroid/Kconfig.in
Expand Up @@ -1150,3 +1150,17 @@ config BT_BLE_FEAT_PERIODIC_ADV_SYNC_TRANSFER
default n
help
This enables BLE periodic advertising sync transfer feature

config BT_BLE_FEAT_PERIODIC_ADV_ENH
bool "Enable periodic adv enhancements(adi support)"
depends on (BT_BLUEDROID_ENABLED && BT_BLE_50_FEATURES_SUPPORTED && SOC_ESP_NIMBLE_CONTROLLER)
default n
help
Enable the periodic advertising enhancements

config BT_BLE_HIGH_DUTY_ADV_INTERVAL
bool "Enable BLE high duty advertising interval feature"
depends on BT_BLUEDROID_ENABLED
default n
help
This enable BLE high duty advertising interval feature
29 changes: 24 additions & 5 deletions components/bt/host/bluedroid/api/esp_gap_ble_api.c
Expand Up @@ -138,7 +138,7 @@ esp_err_t esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params)
if (ESP_BLE_IS_VALID_PARAM(params->min_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(params->max_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(params->timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(params->latency <= ESP_BLE_CONN_LATENCY_MAX || params->latency == ESP_BLE_CONN_PARAM_UNDEF) &&
(params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((params->timeout * 10) >= ((1 + params->latency) * ((params->max_int * 5) >> 1))) && params->min_int <= params->max_int) {

msg.sig = BTC_SIG_API_CALL;
Expand Down Expand Up @@ -354,7 +354,7 @@ esp_err_t esp_ble_gap_set_prefer_conn_params(esp_bd_addr_t bd_addr,
if (ESP_BLE_IS_VALID_PARAM(min_conn_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(max_conn_int, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(supervision_tout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(slave_latency <= ESP_BLE_CONN_LATENCY_MAX || slave_latency == ESP_BLE_CONN_PARAM_UNDEF) &&
(slave_latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((supervision_tout * 10) >= ((1 + slave_latency) * ((max_conn_int * 5) >> 1))) && min_conn_int <= max_conn_int) {

msg.sig = BTC_SIG_API_CALL;
Expand Down Expand Up @@ -1052,8 +1052,13 @@ esp_err_t esp_ble_gap_periodic_adv_set_params(uint8_t instance, const esp_ble_ga

}

#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH)
esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length,
const uint8_t *data, bool only_update_did)
#else
esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t length,
const uint8_t *data)
#endif
{
btc_msg_t msg;
btc_ble_5_gap_args_t arg;
Expand All @@ -1067,13 +1072,22 @@ esp_err_t esp_ble_gap_config_periodic_adv_data_raw(uint8_t instance, uint16_t le
arg.periodic_adv_cfg_data.instance = instance;
arg.periodic_adv_cfg_data.len = length;
arg.periodic_adv_cfg_data.data = (uint8_t *)data;
#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH)
arg.periodic_adv_cfg_data.only_update_did = only_update_did;
#else
arg.periodic_adv_cfg_data.only_update_did = false;
#endif

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_5_gap_args_t), btc_gap_ble_arg_deep_copy,
btc_gap_ble_arg_deep_free) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);

}

#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH)
esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance,bool include_adi)
#else
esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance)
#endif
{
btc_msg_t msg;
btc_ble_5_gap_args_t arg;
Expand All @@ -1084,6 +1098,11 @@ esp_err_t esp_ble_gap_periodic_adv_start(uint8_t instance)
msg.pid = BTC_PID_GAP_BLE;
msg.act = BTC_GAP_BLE_PERIODIC_ADV_START;

#if (CONFIG_BT_BLE_FEAT_PERIODIC_ADV_ENH)
arg.periodic_adv_start.include_adi = include_adi;
#else
arg.periodic_adv_start.include_adi = false;
#endif
arg.periodic_adv_start.instance = instance;

return (btc_transfer_context(&msg, &arg, sizeof(btc_ble_5_gap_args_t), NULL, NULL)
Expand Down Expand Up @@ -1298,7 +1317,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr,
if (ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_1m_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(phy_1m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_1m_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) &&
(phy_1m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((phy_1m_conn_params->supervision_timeout * 10) >= ((1 + phy_1m_conn_params->latency) * ((phy_1m_conn_params->interval_max * 5) >> 1))) &&
(phy_1m_conn_params->interval_min <= phy_1m_conn_params->interval_max)) {

Expand All @@ -1322,7 +1341,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr,
if (ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_2m_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(phy_2m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_2m_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) &&
(phy_2m_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((phy_2m_conn_params->supervision_timeout * 10) >= ((1 + phy_2m_conn_params->latency) * ((phy_2m_conn_params->interval_max * 5) >> 1))) &&
(phy_2m_conn_params->interval_min <= phy_2m_conn_params->interval_max)) {

Expand All @@ -1346,7 +1365,7 @@ esp_err_t esp_ble_gap_prefer_ext_connect_params_set(esp_bd_addr_t addr,
if (ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->interval_min, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->interval_max, ESP_BLE_CONN_INT_MIN, ESP_BLE_CONN_INT_MAX) &&
ESP_BLE_IS_VALID_PARAM(phy_coded_conn_params->supervision_timeout, ESP_BLE_CONN_SUP_TOUT_MIN, ESP_BLE_CONN_SUP_TOUT_MAX) &&
(phy_coded_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX || phy_coded_conn_params->latency == ESP_BLE_CONN_PARAM_UNDEF) &&
(phy_coded_conn_params->latency <= ESP_BLE_CONN_LATENCY_MAX) &&
((phy_coded_conn_params->supervision_timeout * 10) >= ((1 + phy_coded_conn_params->latency) * ((phy_coded_conn_params->interval_max * 5) >> 1))) &&
(phy_coded_conn_params->interval_min <= phy_coded_conn_params->interval_max)) {

Expand Down
16 changes: 9 additions & 7 deletions components/bt/host/bluedroid/api/include/api/esp_bt_defs.h
Expand Up @@ -125,18 +125,20 @@ typedef uint8_t esp_link_key[ESP_BT_OCTET16_LEN]; /* Link Key */
/// Default GATT interface id
#define ESP_DEFAULT_GATT_IF 0xff

#if BLE_HIGH_DUTY_ADV_INTERVAL
#define ESP_BLE_PRIM_ADV_INT_MIN 0x000008 /*!< Minimum advertising interval for undirected and low duty cycle directed advertising */
#else
#define ESP_BLE_PRIM_ADV_INT_MIN 0x000020 /*!< Minimum advertising interval for undirected and low duty cycle directed advertising */
#endif
#define ESP_BLE_PRIM_ADV_INT_MAX 0xFFFFFF /*!< Maximum advertising interval for undirected and low duty cycle directed advertising */
#define ESP_BLE_CONN_INT_MIN 0x0006 /*!< relate to BTM_BLE_CONN_INT_MIN in stack/btm_ble_api.h */
#define ESP_BLE_CONN_INT_MAX 0x0C80 /*!< relate to BTM_BLE_CONN_INT_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_LATENCY_MAX 499 /*!< relate to ESP_BLE_CONN_LATENCY_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_SUP_TOUT_MIN 0x000A /*!< relate to BTM_BLE_CONN_SUP_TOUT_MIN in stack/btm_ble_api.h */
#define ESP_BLE_CONN_SUP_TOUT_MAX 0x0C80 /*!< relate to ESP_BLE_CONN_SUP_TOUT_MAX in stack/btm_ble_api.h */
#define ESP_BLE_CONN_PARAM_UNDEF 0xffff /* use this value when a specific value not to be overwritten */ /* relate to ESP_BLE_CONN_PARAM_UNDEF in stack/btm_ble_api.h */
#define ESP_BLE_SCAN_PARAM_UNDEF 0xffffffff /* relate to ESP_BLE_SCAN_PARAM_UNDEF in stack/btm_ble_api.h */

/// Check the param is valid or not
#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) || ((x) == ESP_BLE_CONN_PARAM_UNDEF))
#define ESP_BLE_IS_VALID_PARAM(x, min, max) (((x) >= (min) && (x) <= (max)) )

/// UUID type
typedef struct {
Expand Down Expand Up @@ -166,10 +168,10 @@ typedef uint8_t esp_bd_addr_t[ESP_BD_ADDR_LEN];

/// BLE device address type
typedef enum {
BLE_ADDR_TYPE_PUBLIC = 0x00,
BLE_ADDR_TYPE_RANDOM = 0x01,
BLE_ADDR_TYPE_RPA_PUBLIC = 0x02,
BLE_ADDR_TYPE_RPA_RANDOM = 0x03,
BLE_ADDR_TYPE_PUBLIC = 0x00, /*!< Public Device Address */
BLE_ADDR_TYPE_RANDOM = 0x01, /*!< Random Device Address. To set this address, use the function esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) */
BLE_ADDR_TYPE_RPA_PUBLIC = 0x02, /*!< Resolvable Private Address (RPA) with public identity address */
BLE_ADDR_TYPE_RPA_RANDOM = 0x03, /*!< Resolvable Private Address (RPA) with random identity address. To set this address, use the function esp_ble_gap_set_rand_addr(esp_bd_addr_t rand_addr) */
} esp_ble_addr_type_t;

/// white list address type
Expand Down

0 comments on commit caf6660

Please sign in to comment.