From 67c8b38d2072ea934a26784584d19558ed6a5ee6 Mon Sep 17 00:00:00 2001 From: zwl Date: Thu, 7 Sep 2023 12:30:08 +0800 Subject: [PATCH 1/5] fix(ble): fixed crash when memory is insufficient on esp32c2 --- components/bt/controller/lib_esp32c2/esp32c2-bt-lib | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib index b43913f03e9..9da8ad3ebd7 160000 --- a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib +++ b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib @@ -1 +1 @@ -Subproject commit b43913f03e949c7d32073f6e4f3828a915dd3486 +Subproject commit 9da8ad3ebd7932ea6f92578fb1337c7df5000ac7 From 4955bb3bda9de3a66e3ad41cf0b744949f05998a Mon Sep 17 00:00:00 2001 From: zwl Date: Thu, 7 Sep 2023 14:13:39 +0800 Subject: [PATCH 2/5] feat(ble): enable adv report flow control on esp32c2 --- components/bt/controller/esp32c2/Kconfig.in | 33 +++++++++++++++++++ .../include/common/bluedroid_user_config.h | 4 +-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index 02b1d3f001e..ca1902ca94b 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -392,3 +392,36 @@ config BT_LE_USE_ESP_TIMER default y help Set this option to use Esp Timer which has higher priority timer instead of FreeRTOS timer + +config BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP + bool "BLE adv report flow control supported" + default y + help + The function is mainly used to enable flow control for advertising reports. When it is enabled, + advertising reports will be discarded by the controller if the number of unprocessed advertising + reports exceeds the size of BLE adv report flow control. + +config BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM + int "BLE adv report flow control number" + depends on BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP + range 50 1000 + default 100 + help + The number of unprocessed advertising report that bluetooth host can save.If you set + `BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` to a small value, this may cause adv packets lost. + If you set `BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` to a large value, bluetooth host may cache a + lot of adv packets and this may cause system memory run out. For example, if you set + it to 50, the maximum memory consumed by host is 35 * 50 bytes. Please set + `BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_NUM` according to your system free memory and handle adv + packets as fast as possible, otherwise it will cause adv packets lost. + +config BT_CTRL_BLE_ADV_REPORT_DISCARD_THRSHOLD + int "BLE adv lost event threshold value" + depends on BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP + range 1 1000 + default 20 + help + When adv report flow control is enabled, The ADV lost event will be generated when the number + of ADV packets lost in the controller reaches this threshold. It is better to set a larger value. + If you set `BT_CTRL_BLE_ADV_REPORT_DISCARD_THRSHOLD` to a small value or printf every adv lost event, it + may cause adv packets lost more. diff --git a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h index 08034e4a90f..f54c04e9b6b 100644 --- a/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h +++ b/components/bt/host/bluedroid/common/include/common/bluedroid_user_config.h @@ -222,7 +222,7 @@ #endif //CONFIG_IDF_TARGET_ESP32 -#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2) +#if (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2) //BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP #ifdef CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP #define UC_BTDM_BLE_ADV_REPORT_FLOW_CTRL_SUPP CONFIG_BT_CTRL_BLE_ADV_REPORT_FLOW_CTRL_SUPP @@ -244,7 +244,7 @@ #define UC_BTDM_BLE_ADV_REPORT_DISCARD_THRSHOLD 20 #endif -#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2) +#endif //(CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2) //BT ACL CONNECTIONS #ifdef CONFIG_BT_ACL_CONNECTIONS From 60d6b3c13e0d52dc83ce7158bc16071f996b0f66 Mon Sep 17 00:00:00 2001 From: luomanruo Date: Fri, 8 Sep 2023 11:46:49 +0800 Subject: [PATCH 3/5] ble: update esp32h2 and esp32c6 libble to a9fa2002 --- components/bt/controller/lib_esp32c6/esp32c6-bt-lib | 2 +- components/bt/controller/lib_esp32h2/esp32h2-bt-lib | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib index f3d17177b02..f0896d10805 160000 --- a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib +++ b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib @@ -1 +1 @@ -Subproject commit f3d17177b024d492920128cc977a303eb900190d +Subproject commit f0896d108055057f321afe5954ee6167d6e14936 diff --git a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib index 0f9c8a79eca..db2a7395b62 160000 --- a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib +++ b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib @@ -1 +1 @@ -Subproject commit 0f9c8a79ecaa8b5e756d0fa46a7fab6cb3a1fbb2 +Subproject commit db2a7395b629dfcaf015069c36f4a5ce441f2aba From 7fc9c0ad62938f51b33ae5bc7839f0ea963c4d0c Mon Sep 17 00:00:00 2001 From: cjin Date: Fri, 8 Sep 2023 15:50:28 +0800 Subject: [PATCH 4/5] ble: support esp32c2 wakeup overhead --- components/bt/controller/esp32c2/bt.c | 34 ++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index 6f440944274..e78cdb68627 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -46,6 +46,10 @@ #include "hci/hci_hal.h" #endif +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +#include "esp_private/sleep_modem.h" +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE + #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -139,6 +143,9 @@ extern uint32_t r_os_cputime_get32(void); extern uint32_t r_os_cputime_ticks_to_usecs(uint32_t ticks); extern void r_ble_lll_rfmgmt_set_sleep_cb(void *s_cb, void *w_cb, void *s_arg, void *w_arg, uint32_t us_to_enabled); extern void r_ble_rtc_wake_up_state_clr(void); +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +extern void esp_ble_set_wakeup_overhead(uint32_t overhead); +#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ extern int os_msys_init(void); extern void os_msys_buf_free(void); extern int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, @@ -459,6 +466,13 @@ static int esp_intr_free_wrapper(void **ret_handle) return rc; } +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE +void sleep_modem_light_sleep_overhead_set(uint32_t overhead) +{ + esp_ble_set_wakeup_overhead(overhead); +} +#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ + IRAM_ATTR void controller_sleep_cb(uint32_t enable_tick, void *arg) { if (!s_ble_active) { @@ -504,32 +518,40 @@ esp_err_t controller_sleep_init(void) if (rc != ESP_OK) { goto error; } - +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE esp_sleep_enable_bt_wakeup(); ESP_LOGW(NIMBLE_PORT_LOG_TAG, "Enable light sleep, the wake up source is BLE timer"); + rc = esp_pm_register_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set); + if (rc != ESP_OK) { + goto error; + } +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE return rc; error: +#if CONFIG_FREERTOS_USE_TICKLESS_IDLE + esp_sleep_disable_bt_wakeup(); + esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set); +#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */ /*lock should release first and then delete*/ if (s_pm_lock != NULL) { esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; } - - esp_sleep_disable_bt_wakeup(); - #endif //CONFIG_PM_ENABLE return rc; } void controller_sleep_deinit(void) { -#ifdef CONFIG_PM_ENABLE +#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE r_ble_rtc_wake_up_state_clr(); esp_sleep_disable_bt_wakeup(); esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_AUTO); - + esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set); +#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE +#ifdef CONFIG_PM_ENABLE /*lock should release first and then delete*/ esp_pm_lock_delete(s_pm_lock); s_pm_lock = NULL; From fa4f34e7911a0d4d6f455be20337a26650bdccdf Mon Sep 17 00:00:00 2001 From: luomanruo Date: Wed, 6 Sep 2023 21:51:45 +0800 Subject: [PATCH 5/5] change(ble): update controller log print interface --- components/bt/controller/esp32c2/bt.c | 31 +++++++++---------- components/bt/controller/esp32c6/bt.c | 31 +++++++++---------- components/bt/controller/esp32h2/bt.c | 31 +++++++++---------- .../bt/controller/lib_esp32c6/esp32c6-bt-lib | 2 +- .../bt/controller/lib_esp32h2/esp32h2-bt-lib | 2 +- .../bt/include/esp32c6/include/esp_bt.h | 2 +- .../bt/include/esp32h2/include/esp_bt.h | 2 +- 7 files changed, 46 insertions(+), 55 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index e78cdb68627..38ce3362508 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -385,23 +385,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer return rc; } -#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED -static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) -{ - if (!end) { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - - } else { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - esp_rom_printf("\n"); - } -} -#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED - #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART static void hci_uart_start_tx_wrapper(int uart_no) { @@ -1006,11 +989,25 @@ uint8_t esp_ble_get_chip_rev_version(void) } #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + for (int i = 0; i < len; i++) { + esp_rom_printf("%02x,", addr[i]); + } + if (end) { + esp_rom_printf("\n"); + } +} + void esp_ble_controller_log_dump_all(bool output) { + portMUX_TYPE spinlock; + + portENTER_CRITICAL_SAFE(&spinlock); BT_ASSERT_PRINT("\r\n[DUMP_START:"); ble_log_async_output_dump_all(output); BT_ASSERT_PRINT("]\r\n"); + portEXIT_CRITICAL_SAFE(&spinlock); } #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED diff --git a/components/bt/controller/esp32c6/bt.c b/components/bt/controller/esp32c6/bt.c index 75dc5870924..eb3fb862149 100644 --- a/components/bt/controller/esp32c6/bt.c +++ b/components/bt/controller/esp32c6/bt.c @@ -377,23 +377,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer return rc; } -#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED -static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) -{ - if (!end) { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - - } else { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - esp_rom_printf("\n"); - } -} -#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED - #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART static void hci_uart_start_tx_wrapper(int uart_no) { @@ -1174,11 +1157,25 @@ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t po } #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + for (int i = 0; i < len; i++) { + esp_rom_printf("%02x,", addr[i]); + } + if (end) { + esp_rom_printf("\n"); + } +} + void esp_ble_controller_log_dump_all(bool output) { + portMUX_TYPE spinlock; + + portENTER_CRITICAL_SAFE(&spinlock); BT_ASSERT_PRINT("\r\n[DUMP_START:"); ble_log_async_output_dump_all(output); BT_ASSERT_PRINT("]\r\n"); + portEXIT_CRITICAL_SAFE(&spinlock); } #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED diff --git a/components/bt/controller/esp32h2/bt.c b/components/bt/controller/esp32h2/bt.c index 29e5a85a366..d29822f90ca 100644 --- a/components/bt/controller/esp32h2/bt.c +++ b/components/bt/controller/esp32h2/bt.c @@ -374,23 +374,6 @@ static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer return rc; } -#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED -static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) -{ - if (!end) { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - - } else { - for (int i = 0; i < len; i++) { - esp_rom_printf("%02x,", addr[i]); - } - esp_rom_printf("\n"); - } -} -#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED - #ifdef CONFIG_BT_LE_HCI_INTERFACE_USE_UART static void hci_uart_start_tx_wrapper(int uart_no) { @@ -1161,11 +1144,25 @@ esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t po } #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + for (int i = 0; i < len; i++) { + esp_rom_printf("%02x,", addr[i]); + } + if (end) { + esp_rom_printf("\n"); + } +} + void esp_ble_controller_log_dump_all(bool output) { + portMUX_TYPE spinlock; + + portENTER_CRITICAL_SAFE(&spinlock); BT_ASSERT_PRINT("\r\n[DUMP_START:"); ble_log_async_output_dump_all(output); BT_ASSERT_PRINT("]\r\n"); + portEXIT_CRITICAL_SAFE(&spinlock); } #endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED diff --git a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib index f0896d10805..97499655f31 160000 --- a/components/bt/controller/lib_esp32c6/esp32c6-bt-lib +++ b/components/bt/controller/lib_esp32c6/esp32c6-bt-lib @@ -1 +1 @@ -Subproject commit f0896d108055057f321afe5954ee6167d6e14936 +Subproject commit 97499655f3148ee273baadb6b48bbb168207d90b diff --git a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib index db2a7395b62..ff664688f4f 160000 --- a/components/bt/controller/lib_esp32h2/esp32h2-bt-lib +++ b/components/bt/controller/lib_esp32h2/esp32h2-bt-lib @@ -1 +1 @@ -Subproject commit db2a7395b629dfcaf015069c36f4a5ce441f2aba +Subproject commit ff664688f4fedb16c7eafc5bc88a4dacbc3e8ba5 diff --git a/components/bt/include/esp32c6/include/esp_bt.h b/components/bt/include/esp32c6/include/esp_bt.h index 8015e67baf3..1e96bfd0d77 100644 --- a/components/bt/include/esp32c6/include/esp_bt.h +++ b/components/bt/include/esp32c6/include/esp_bt.h @@ -412,7 +412,7 @@ extern int esp_ble_hw_get_static_addr(esp_ble_addr_t *addr); #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /** - * @brief dump all controller log information cached in buffer + * @brief dump all log information cached in buffers. * @param output : true for log dump, false will take no effect */ void esp_ble_controller_log_dump_all(bool output); diff --git a/components/bt/include/esp32h2/include/esp_bt.h b/components/bt/include/esp32h2/include/esp_bt.h index 19c6638ed86..f5684e5c6ab 100644 --- a/components/bt/include/esp32h2/include/esp_bt.h +++ b/components/bt/include/esp32h2/include/esp_bt.h @@ -416,7 +416,7 @@ extern int esp_ble_hw_get_static_addr(esp_ble_addr_t *addr); #if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /** - * @brief dump all controller log information cached in buffer + * @brief dump all log information cached in buffers. * @param output : true for log dump, false will take no effect */ void esp_ble_controller_log_dump_all(bool output);