From bd21c215c7529460a4993314f6a56958d8e605bb Mon Sep 17 00:00:00 2001 From: zwl Date: Fri, 18 Aug 2023 11:55:48 +0800 Subject: [PATCH] ble: supported trace function and adv report flow control on esp32c2 --- components/bt/controller/esp32c2/Kconfig.in | 13 +++ components/bt/controller/esp32c2/bt.c | 96 +++++++++++++++---- .../bt/controller/lib_esp32c2/esp32c2-bt-lib | 2 +- .../bt/include/esp32c2/include/esp_bt.h | 8 ++ components/esp_rom/esp32c2/ld/esp32c2.rom.ld | 11 --- 5 files changed, 100 insertions(+), 30 deletions(-) diff --git a/components/bt/controller/esp32c2/Kconfig.in b/components/bt/controller/esp32c2/Kconfig.in index dbaf805faca..02b1d3f001e 100644 --- a/components/bt/controller/esp32c2/Kconfig.in +++ b/components/bt/controller/esp32c2/Kconfig.in @@ -253,6 +253,19 @@ config BT_LE_CONTROLLER_TASK_STACK_SIZE help This configures stack size of NimBLE controller task +config BT_LE_CONTROLLER_LOG_ENABLED + bool "Controller log enable" + default n + help + Enable controller log module + +config BT_LE_CONTROLLER_LOG_DUMP_ONLY + bool "Controller log dump mode only" + depends on BT_LE_CONTROLLER_LOG_ENABLED + default y + help + Only operate in dump mode + config BT_LE_LL_RESOLV_LIST_SIZE int "BLE LL Resolving list size" range 1 5 diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index bdf1d9f36ae..73ce2db305a 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -72,12 +72,12 @@ #ifdef CONFIG_BT_BLUEDROID_ENABLED /* ACL_DATA_MBUF_LEADINGSPCAE: The leadingspace in user info header for ACL data */ #define ACL_DATA_MBUF_LEADINGSPCAE 4 -#endif +#endif // CONFIG_BT_BLUEDROID_ENABLED + /* Types definition ************************************************************************ */ - struct osi_coex_funcs_t { uint32_t _magic; uint32_t _version; @@ -110,13 +110,20 @@ struct ext_funcs_t { uint32_t magic; }; +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /* External functions or variables ************************************************************************ */ - extern int ble_osi_coex_funcs_register(struct osi_coex_funcs_t *coex_funcs); extern int ble_controller_init(esp_bt_controller_config_t *cfg); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +extern int ble_log_init_async(interface_func_t bt_controller_log_interface, bool task_create); +extern int ble_log_deinit_async(void); +extern void ble_log_async_output_dump_all(bool output); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED extern int ble_controller_deinit(void); extern int ble_controller_enable(uint8_t mode); extern int ble_controller_disable(void); @@ -167,8 +174,9 @@ static int hci_uart_config_wrapper(int uart_no, int32_t speed, uint8_t databits, static int hci_uart_close_wrapper(int uart_no); static void hci_uart_blocking_tx_wrapper(int port, uint8_t data); static int hci_uart_init_wrapper(int uart_no, void *cfg); -#endif -static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, void *arg, void **ret_handle_in); +#endif // CONFIG_BT_LE_HCI_INTERFACE_USE_UART +static int esp_intr_alloc_wrapper(int source, int flags, intr_handler_t handler, + void *arg, void **ret_handle_in); static int esp_intr_free_wrapper(void **ret_handle); static void osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2); static uint32_t osi_random_wrapper(void); @@ -176,10 +184,12 @@ static void esp_reset_rpa_moudle(void); static int esp_ecc_gen_key_pair(uint8_t *pub, uint8_t *priv); static int esp_ecc_gen_dh_key(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y, const uint8_t *our_priv_key, uint8_t *out_dhkey); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED /* Local variable definition *************************************************************************** */ - /* Static variable declare */ static DRAM_ATTR esp_bt_controller_status_t ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; @@ -188,7 +198,7 @@ static bool s_ble_active = false; #ifdef CONFIG_PM_ENABLE static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock = NULL; #define BTDM_MIN_TIMER_UNCERTAINTY_US (200) -#endif /* #ifdef CONFIG_PM_ENABLE */ +#endif // CONFIG_PM_ENABLE #define BLE_RTC_DELAY_US (1800) @@ -233,8 +243,12 @@ static void IRAM_ATTR esp_reset_rpa_moudle(void) DPORT_CLEAR_PERI_REG_MASK(SYSTEM_CORE_RST_EN_REG, BLE_RPA_REST_BIT); } -static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, uint32_t param1, uint32_t param2) +static void IRAM_ATTR osi_assert_wrapper(const uint32_t ln, const char *fn, + uint32_t param1, uint32_t param2) { +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + esp_ble_controller_log_dump_all(true); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED BT_ASSERT_PRINT("BLE assert: line %d in function %s, param: 0x%x, 0x%x", ln, fn, param1, param2); assert(0); } @@ -248,17 +262,17 @@ static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status) { #if CONFIG_SW_COEXIST_ENABLE coex_schm_status_bit_set(type, status); -#endif +#endif // CONFIG_SW_COEXIST_ENABLE } static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status) { #if CONFIG_SW_COEXIST_ENABLE coex_schm_status_bit_clear(type, status); -#endif +#endif // CONFIG_SW_COEXIST_ENABLE } -#ifdef CONFIG_BT_BLUEDROID_ENABLED +#ifdef CONFIG_BT_BLUEDROID_ENABLED bool esp_vhci_host_check_send_available(void) { if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { @@ -322,7 +336,6 @@ void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) assert(os_mbuf_append(om, &data[1], len - 1) == 0); ble_hci_trans_hs_acl_tx(om); } - } esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback) @@ -335,8 +348,7 @@ esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callba return ESP_OK; } - -#endif +#endif // CONFIG_BT_BLUEDROID_ENABLED static int task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id) { return (uint32_t)xTaskCreatePinnedToCore(task_func, name, stack_depth, param, prio, task_handle, (core_id < portNUM_PROCESSORS ? core_id : tskNO_AFFINITY)); @@ -366,6 +378,23 @@ 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) { @@ -381,8 +410,9 @@ static int hci_uart_init_cbs_wrapper(int uart_no, hci_uart_tx_char tx_func, } -static int hci_uart_config_wrapper(int port_num, int32_t baud_rate, uint8_t data_bits, uint8_t stop_bits, - uart_parity_t parity, uart_hw_flowcontrol_t flow_ctl) +static int hci_uart_config_wrapper(int port_num, int32_t baud_rate, uint8_t data_bits, + uint8_t stop_bits,uart_parity_t parity, + uart_hw_flowcontrol_t flow_ctl) { int rc = -1; rc = hci_uart_config(port_num, baud_rate, data_bits, stop_bits, parity, flow_ctl); @@ -597,7 +627,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) if (ble_osi_coex_funcs_register((struct osi_coex_funcs_t *)&s_osi_coex_funcs_ro) != 0) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "osi coex funcs reg failed"); ret = ESP_ERR_INVALID_ARG; - goto free_controller; + goto modem_deint; } #if CONFIG_SW_COEXIST_ENABLE @@ -606,9 +636,23 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) ret = ble_controller_init(cfg); if (ret != ESP_OK) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_init failed %d", ret); - goto free_controller; + goto modem_deint; } +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + interface_func_t bt_controller_log_interface; + bt_controller_log_interface = esp_bt_controller_log_interface; +#if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY + ret = ble_log_init_async(bt_controller_log_interface, false); +#else + ret = ble_log_init_async(bt_controller_log_interface, true); +#endif // CONFIG_BT_CONTROLLER_LOG_DUMP + if (ret != ESP_OK) { + ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_log_init failed %d", ret); + goto controller_init_err; + } +#endif // CONFIG_BT_CONTROLLER_LOG_ENABLED + ret = controller_sleep_init(); if (ret != ESP_OK) { ESP_LOGW(NIMBLE_PORT_LOG_TAG, "controller_sleep_init failed %d", ret); @@ -629,7 +673,12 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) return ESP_OK; free_controller: controller_sleep_deinit(); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +controller_init_err: + ble_log_deinit_async(); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); +modem_deint: esp_phy_modem_deinit(); #if CONFIG_BT_NIMBLE_ENABLED ble_npl_eventq_deinit(nimble_port_get_dflt_eventq()); @@ -652,6 +701,9 @@ esp_err_t esp_bt_controller_deinit(void) controller_sleep_deinit(); +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + ble_log_deinit_async(); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED ble_controller_deinit(); #if CONFIG_BT_NIMBLE_ENABLED @@ -931,6 +983,14 @@ uint8_t esp_ble_get_chip_rev_version(void) return efuse_ll_get_chip_wafer_version_minor(); } +#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED +void esp_ble_controller_log_dump_all(bool output) +{ + BT_ASSERT_PRINT("\r\n[DUMP_START:"); + ble_log_async_output_dump_all(output); + BT_ASSERT_PRINT("]\r\n"); +} +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED #if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true) diff --git a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib index d17188c9f61..b43913f03e9 160000 --- a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib +++ b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib @@ -1 +1 @@ -Subproject commit d17188c9f61400792a8248bf53378ee92e6f21a4 +Subproject commit b43913f03e949c7d32073f6e4f3828a915dd3486 diff --git a/components/bt/include/esp32c2/include/esp_bt.h b/components/bt/include/esp32c2/include/esp_bt.h index ecf908d5de0..9707caaebb9 100644 --- a/components/bt/include/esp32c2/include/esp_bt.h +++ b/components/bt/include/esp32c2/include/esp_bt.h @@ -415,6 +415,14 @@ esp_err_t esp_bt_mem_release(esp_bt_mode_t mode); */ 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 + * @param output : true for log dump, false will take no effect + */ +void esp_ble_controller_log_dump_all(bool output); +#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED + #ifdef __cplusplus } #endif diff --git a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld index 976fa69abe7..39076ceed1e 100644 --- a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld +++ b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld @@ -563,10 +563,6 @@ bt_rf_coex_dft_pti_get_default = 0x40000ab4; bt_rf_coex_hooks_p_set = 0x40000ab8; r__os_mbuf_copypkthdr = 0x40000abc; r_ble_controller_get_rom_compile_version = 0x40000ac4; -r_ble_hci_ram_hs_acl_tx = 0x40000ac8; -r_ble_hci_ram_hs_cmd_tx = 0x40000acc; -r_ble_hci_ram_ll_acl_tx = 0x40000ad0; -r_ble_hci_ram_ll_evt_tx = 0x40000ad4; r_ble_hci_ram_reset = 0x40000ad8; r_ble_hci_ram_set_acl_free_cb = 0x40000adc; r_ble_hci_trans_acl_buf_alloc = 0x40000ae0; @@ -579,10 +575,6 @@ r_ble_hci_uart_acl_tx = 0x40000b00; r_ble_hci_uart_cmdevt_tx = 0x40000b04; r_ble_hci_uart_config = 0x40000b08; r_ble_hci_uart_free_pkt = 0x40000b0c; -r_ble_hci_uart_hs_acl_tx = 0x40000b10; -r_ble_hci_uart_hs_cmd_tx = 0x40000b14; -r_ble_hci_uart_ll_acl_tx = 0x40000b18; -r_ble_hci_uart_ll_evt_tx = 0x40000b1c; r_ble_hci_uart_rx_acl = 0x40000b20; r_ble_hci_uart_rx_char = 0x40000b24; r_ble_hci_uart_rx_cmd = 0x40000b28; @@ -825,7 +817,6 @@ r_ble_ll_hci_cb_set_ctrlr_to_host_fc = 0x40000f9c; r_ble_ll_hci_cb_set_event_mask = 0x40000fa0; r_ble_ll_hci_cb_set_event_mask2 = 0x40000fa4; r_ble_ll_hci_chk_phy_masks = 0x40000fa8; -r_ble_ll_hci_cmd_proc = 0x40000fac; r_ble_ll_hci_cmd_rx = 0x40000fb0; r_ble_ll_hci_disconnect = 0x40000fbc; r_ble_ll_hci_ev_conn_update = 0x40000fc4; @@ -979,7 +970,6 @@ r_ble_ll_scan_reset = 0x40001258; r_ble_ll_scan_rx_pkt_in = 0x4000125c; r_ble_ll_scan_rx_pkt_in_restore_addr_data = 0x40001268; r_ble_ll_scan_rxed = 0x4000126c; -r_ble_ll_scan_send_adv_report = 0x40001270; r_ble_ll_scan_send_truncated = 0x40001274; r_ble_ll_scan_set_peer_rpa = 0x4000127c; r_ble_ll_scan_set_perfer_addr = 0x40001280; @@ -1116,7 +1106,6 @@ r_ble_lll_conn_coex_dpc_update_on_event_scheduled = 0x400014e4; r_ble_lll_conn_coex_dpc_update_on_event_started = 0x400014e8; r_ble_lll_conn_cth_flow_alloc_credit = 0x400014ec; r_ble_lll_conn_current_sm_over = 0x400014f4; -r_ble_lll_conn_event_end = 0x40001504; r_ble_lll_conn_event_end_timer_cb = 0x40001508; r_ble_lll_conn_event_is_over = 0x40001510; r_ble_lll_conn_event_start_cb = 0x40001514;