From 32cb69706ba22f895b937ba174aabe469f9c7439 Mon Sep 17 00:00:00 2001 From: Zhou Xiao Date: Mon, 15 Sep 2025 14:22:52 +0800 Subject: [PATCH] feat(ble): supported ble log v2 for ESP32-C3/ESP32-S3 --- components/bt/controller/esp32c3/Kconfig.in | 69 ++++++++++++--------- components/bt/controller/esp32c3/bt.c | 69 ++++++++++++++++++++- 2 files changed, 105 insertions(+), 33 deletions(-) diff --git a/components/bt/controller/esp32c3/Kconfig.in b/components/bt/controller/esp32c3/Kconfig.in index da1793dd8e..43d714a5de 100644 --- a/components/bt/controller/esp32c3/Kconfig.in +++ b/components/bt/controller/esp32c3/Kconfig.in @@ -567,41 +567,50 @@ menu "Controller debug log Options (Experimental)" bool "Enable BLE debug log" default n + config BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 + bool "Utilize BLE Log v2 for controller log" + depends on BT_CTRL_LE_LOG_EN + default y + help + Utilize BLE Log v2 for controller log + + if !BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 + config BT_CTRL_LE_LOG_DUMP_ONLY + depends on BT_CTRL_LE_LOG_EN + bool "Enable BLE log dump only" + default n + + config BT_CTRL_LE_LOG_STORAGE_EN + depends on BT_CTRL_LE_LOG_EN + bool "Enable BLE log storage to flash" + default n + + config BT_CTRL_LE_LOG_PARTITION_SIZE + int "The size of ble controller log partition(Multiples of 4K)" + depends on BT_CTRL_LE_LOG_STORAGE_EN + default 65536 + help + The size of ble controller log partition shall be a multiples of 4K. + The name of log partition shall be "bt_ctrl_log". + The partition type shall be ESP_PARTITION_TYPE_DATA. + The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY. + + config BT_CTRL_LE_LOG_SPI_OUT_EN + bool "Output ble controller logs to SPI bus" + depends on BT_CTRL_LE_LOG_EN + depends on !BT_CTRL_LE_LOG_DUMP_ONLY + select BT_BLE_LOG_SPI_OUT_ENABLED + select BT_BLE_LOG_SPI_OUT_LL_ENABLED + default n + help + Output ble controller logs to SPI bus + endif + config BT_CTRL_LE_HCI_LOG_EN depends on BT_CTRL_LE_LOG_EN bool "Enable BLE HCI log" default n - config BT_CTRL_LE_LOG_DUMP_ONLY - depends on BT_CTRL_LE_LOG_EN - bool "Enable BLE log dump only" - default n - - config BT_CTRL_LE_LOG_STORAGE_EN - depends on BT_CTRL_LE_LOG_EN - bool "Enable BLE log storage to flash" - default n - - config BT_CTRL_LE_LOG_PARTITION_SIZE - int "The size of ble controller log partition(Multiples of 4K)" - depends on BT_CTRL_LE_LOG_STORAGE_EN - default 65536 - help - The size of ble controller log partition shall be a multiples of 4K. - The name of log partition shall be "bt_ctrl_log". - The partition type shall be ESP_PARTITION_TYPE_DATA. - The partition sub_type shall be ESP_PARTITION_SUBTYPE_ANY. - - config BT_CTRL_LE_LOG_SPI_OUT_EN - bool "Output ble controller logs to SPI bus" - depends on BT_CTRL_LE_LOG_EN - depends on !BT_CTRL_LE_LOG_DUMP_ONLY - select BT_BLE_LOG_SPI_OUT_ENABLED - select BT_BLE_LOG_SPI_OUT_LL_ENABLED - default n - help - Output ble controller logs to SPI bus - config BT_CTRL_LE_LOG_MODE_EN depends on BT_CTRL_LE_LOG_EN int "Enable log for specified BLE mode" diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index a0e31c15de..0d9e995af1 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -49,9 +49,13 @@ #else //CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/rom/rom_layout.h" #endif +#if CONFIG_BLE_LOG_ENABLED +#include "ble_log.h" +#else /* !CONFIG_BLE_LOG_ENABLED */ #if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED #include "ble_log/ble_log_spi_out.h" #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED +#endif /* CONFIG_BLE_LOG_ENABLED */ #if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN #include "esp_partition.h" #include "hal/wdt_hal.h" @@ -504,10 +508,14 @@ enum log_out_mode { LOG_ASYNC_OUT, LOG_STORAGE_TO_FLASH, LOG_SPI_OUT, + LOG_BLE_LOG_V2, }; const static uint32_t log_bufs_size[] = {CONFIG_BT_CTRL_LE_LOG_BUF1_SIZE, CONFIG_BT_CTRL_LE_LOG_HCI_BUF_SIZE, CONFIG_BT_CTRL_LE_LOG_BUF2_SIZE}; bool log_is_inited = false; +#if CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 +uint8_t log_output_mode = LOG_BLE_LOG_V2; +#else /* !CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 */ #if CONFIG_BT_CTRL_LE_LOG_DUMP_ONLY uint8_t log_output_mode = LOG_DUMP_MEMORY; #else @@ -519,6 +527,7 @@ uint8_t log_output_mode = LOG_SPI_OUT; uint8_t log_output_mode = LOG_ASYNC_OUT; #endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN #endif // CONFIG_BT_CTRL_LE_LOG_DUMP_ONLY +#endif /* CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 */ #if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN static const esp_partition_t *log_partition; static uint32_t write_index = 0; @@ -528,6 +537,17 @@ static bool stop_write = false; static bool is_filled = false; #endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#if CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 +static IRAM_ATTR void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + ble_log_write_hex_ll(len, addr, 0, NULL, 0); +} + +void esp_ble_controller_log_dump_all(bool output) +{ + ble_log_dump_to_console(); +} +#else /* !CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 */ static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) { if (log_output_mode == LOG_STORAGE_TO_FLASH) { @@ -572,6 +592,7 @@ void esp_ble_controller_log_dump_all(bool output) portEXIT_CRITICAL_SAFE(&spinlock); } } +#endif /* CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 */ void esp_bt_log_output_mode_set(uint8_t output_mode) { @@ -583,6 +604,31 @@ uint8_t esp_bt_log_output_mode_get(void) return log_output_mode; } +#if CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 +esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode) +{ + if (log_is_inited) { + return ESP_OK; + } + + esp_err_t ret = ESP_OK; + uint8_t buffers = 0; + +#if CONFIG_BT_CTRL_LE_LOG_EN + buffers |= ESP_BLE_LOG_BUF_CONTROLLER; +#endif // CONFIG_BT_CTRL_LE_LOG_EN +#if CONFIG_BT_CTRL_LE_HCI_LOG_EN + buffers |= ESP_BLE_LOG_BUF_HCI; +#endif // CONFIG_BT_CTRL_LE_HCI_LOG_EN + + ret = r_ble_log_init_async(esp_bt_controller_log_interface, true, buffers, (uint32_t *)log_bufs_size); + if (ret == ESP_OK) { + log_is_inited = true; + } + + return ret; +} +#else /* !CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2 */ esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode) { esp_err_t ret = ESP_OK; @@ -632,8 +678,9 @@ esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode) return ret; } +#endif /* CONFIG_BT_CTRL_LE_LOG_MODE_BLE_LOG_V2*/ -void esp_bt_ontroller_log_deinit(void) +void esp_bt_controller_log_deinit(void) { r_ble_log_deinit_async(); log_is_inited = false; @@ -714,7 +761,7 @@ void esp_bt_read_ctrl_log_from_flash(bool output) portENTER_CRITICAL_SAFE(&spinlock); esp_panic_handler_feed_wdts(); r_ble_log_async_output_dump_all(true); - esp_bt_ontroller_log_deinit(); + esp_bt_controller_log_deinit(); stop_write = true; buffer = (const uint8_t *)mapped_ptr; @@ -1807,6 +1854,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) coex_init(); #endif +#if CONFIG_BLE_LOG_ENABLED + if (!ble_log_init()) { + ESP_LOGE(BT_LOG_TAG, "BLE Log v2 init failed"); + err = ESP_ERR_NO_MEM; + goto error; + } +#else /* !CONFIG_BLE_LOG_ENABLED */ #if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED if (ble_log_spi_out_init() != 0) { ESP_LOGE(BT_LOG_TAG, "BLE Log SPI output init failed"); @@ -1814,6 +1868,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto error; } #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED +#endif /* CONFIG_BLE_LOG_ENABLED */ periph_module_enable(PERIPH_BT_MODULE); periph_module_reset(PERIPH_BT_MODULE); @@ -1847,9 +1902,13 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) error: +#if CONFIG_BLE_LOG_ENABLED + ble_log_deinit(); +#else /* !CONFIG_BLE_LOG_ENABLED */ #if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED ble_log_spi_out_deinit(); #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED +#endif /* CONFIG_BLE_LOG_ENABLED */ bt_controller_deinit_internal(); @@ -1862,9 +1921,13 @@ esp_err_t esp_bt_controller_deinit(void) return ESP_ERR_INVALID_STATE; } +#if CONFIG_BLE_LOG_ENABLED + ble_log_deinit(); +#else /* !CONFIG_BLE_LOG_ENABLED */ #if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED ble_log_spi_out_deinit(); #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED +#endif /* CONFIG_BLE_LOG_ENABLED */ #if (CONFIG_BT_BLUEDROID_ENABLED || CONFIG_BT_NIMBLE_ENABLED) scan_stack_enableAdvFlowCtrlVsCmd(false); @@ -1952,7 +2015,7 @@ static void bt_controller_deinit_internal(void) esp_phy_modem_deinit(); #if CONFIG_BT_CTRL_LE_LOG_EN - esp_bt_ontroller_log_deinit(); + esp_bt_controller_log_deinit(); #endif // CONFIG_BT_CTRL_LE_LOG_EN if (osi_funcs_p != NULL) {