diff --git a/components/bt/controller/esp32c3/Kconfig.in b/components/bt/controller/esp32c3/Kconfig.in index 76756fdbcb..b5c98c843a 100644 --- a/components/bt/controller/esp32c3/Kconfig.in +++ b/components/bt/controller/esp32c3/Kconfig.in @@ -550,6 +550,7 @@ config BT_CTRL_BLE_SECURITY_ENABLE depends on BT_CTRL_RUN_IN_FLASH_ONLY && BT_CONTROLLER_ONLY bool "Enable BLE security feature" default y + config BT_CTRL_CHECK_CONNECT_IND_ACCESS_ADDRESS bool "Enable enhanced Access Address check in CONNECT_IND" default n @@ -557,3 +558,71 @@ config BT_CTRL_CHECK_CONNECT_IND_ACCESS_ADDRESS Enabling this option will add stricter verification of the Access Address in the CONNECT_IND PDU. This improves security by ensuring that only connection requests with valid Access Addresses are accepted. If disabled, only basic checks are applied, improving compatibility. + +menu "Controller debug log Options (Experimental)" + config BT_CTRL_LE_LOG_EN + depends on BT_CTRL_RUN_IN_FLASH_ONLY + bool "Enable BLE debug log" + default n + + 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 + 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" + range 0 4095 + default 4093 + + config BT_CTRL_LE_LOG_LEVEL + depends on BT_CTRL_LE_LOG_EN + int "The level of BLE log" + range 0 5 + default 2 + + config BT_CTRL_LE_LOG_BUF1_SIZE + depends on BT_CTRL_LE_LOG_EN + int "The size of BLE log buffer1" + default 1024 + + config BT_CTRL_LE_LOG_HCI_BUF_SIZE + depends on BT_CTRL_LE_LOG_EN + int "The size of BLE log HCI buffer" + default 1024 + + config BT_CTRL_LE_LOG_BUF2_SIZE + depends on BT_CTRL_LE_LOG_EN + int "The size of BLE log buffer2" + default 1024 +endmenu diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 2706d4b9e3..5eba750d36 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -52,6 +52,10 @@ #if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED #include "ble_log/ble_log_spi_out.h" #endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#include "esp_partition.h" +#include "hal/wdt_hal.h" +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN #if CONFIG_BT_ENABLED /* Macro definition @@ -118,12 +122,20 @@ do{\ } while(0) #define OSI_FUNCS_TIME_BLOCKING 0xffffffff -#define OSI_VERSION 0x00010009 +#define OSI_VERSION 0x0001000A #define OSI_MAGIC_VALUE 0xFADEBEAD #define BLE_PWR_HDL_INVL 0xFFFF #define BLE_CONTROLLER_MALLOC_CAPS (MALLOC_CAP_INTERNAL|MALLOC_CAP_DMA) + +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#define MAX_STORAGE_SIZE (CONFIG_BT_CTRL_LE_LOG_PARTITION_SIZE) +#define BLOCK_SIZE (4096) +#define THRESHOLD (3072) +#define PARTITION_NAME "bt_ctrl_log" +#endif + /* Types definition ************************************************************************ */ @@ -219,8 +231,13 @@ struct osi_funcs_t { void (* _btdm_rom_table_ready)(void); bool (* _coex_bt_wakeup_request)(void); void (* _coex_bt_wakeup_request_end)(void); + int64_t (*_get_time_us)(void); + void (* _assert)(void); }; +#if CONFIG_BT_CTRL_LE_LOG_EN +typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end); +#endif // CONFIG_BT_CTRL_LE_LOG_EN /* External functions or values ************************************************************************ @@ -280,6 +297,15 @@ extern void ets_backup_dma_copy(uint32_t reg, uint32_t mem_addr, uint32_t num, b extern void btdm_cca_feature_enable(void); extern void btdm_aa_check_enhance_enable(void); +/* BLE Log module */ +#if CONFIG_BT_CTRL_LE_LOG_EN +extern int r_ble_log_init_async(interface_func_t bt_controller_log_interface, bool task_create, uint8_t buffers, uint32_t *bufs_size); +extern int r_ble_log_deinit_async(void); +extern void r_ble_log_async_select_dump_buffers(uint8_t buffers); +extern void r_ble_log_async_output_dump_all(bool output); +extern void esp_panic_handler_feed_wdts(void); +#endif // CONFIG_BT_CTRL_LE_LOG_EN + extern uint32_t _bt_bss_start; extern uint32_t _bt_bss_end; extern uint32_t _bt_controller_bss_start; @@ -341,6 +367,8 @@ static void btdm_backup_dma_copy_wrapper(uint32_t reg, uint32_t mem_addr, uint32 static void btdm_funcs_table_ready_wrapper(void); static bool coex_bt_wakeup_request(void); static void coex_bt_wakeup_request_end(void); +static int64_t get_time_us_wrapper(void); +static void assert_wrapper(void); static void btdm_slp_tmr_callback(void *arg); @@ -348,6 +376,15 @@ static esp_err_t try_heap_caps_add_region(intptr_t start, intptr_t end); static void bt_controller_deinit_internal(void); +#if CONFIG_BT_CTRL_LE_LOG_EN +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end); +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +void esp_bt_read_ctrl_log_from_flash(bool output); +static int esp_bt_controller_log_storage(uint32_t len, const uint8_t *addr, bool end); +static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void); +#endif // #if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#endif // CONFIG_BT_CTRL_LE_LOG_EN + /* Local variable definition *************************************************************************** */ @@ -413,6 +450,8 @@ static const struct osi_funcs_t osi_funcs_ro = { ._btdm_rom_table_ready = btdm_funcs_table_ready_wrapper, ._coex_bt_wakeup_request = coex_bt_wakeup_request, ._coex_bt_wakeup_request_end = coex_bt_wakeup_request_end, + ._get_time_us = get_time_us_wrapper, + ._assert = assert_wrapper, }; static DRAM_ATTR struct osi_funcs_t *osi_funcs_p; @@ -441,6 +480,255 @@ static DRAM_ATTR esp_pm_lock_handle_t s_pm_lock; static DRAM_ATTR esp_pm_lock_handle_t s_light_sleep_pm_lock; #endif +#if CONFIG_BT_CTRL_LE_LOG_EN +enum log_out_mode { + LOG_DUMP_MEMORY, + LOG_ASYNC_OUT, + LOG_STORAGE_TO_FLASH, + LOG_SPI_OUT, +}; + +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_DUMP_ONLY +uint8_t log_output_mode = LOG_DUMP_MEMORY; +#else +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +uint8_t log_output_mode = LOG_STORAGE_TO_FLASH; +#elif CONFIG_BT_CTRL_LE_LOG_SPI_OUT_EN +uint8_t log_output_mode = LOG_SPI_OUT; +#else +uint8_t log_output_mode = LOG_ASYNC_OUT; +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#endif // CONFIG_BT_CTRL_LE_LOG_DUMP_ONLY +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +static const esp_partition_t *log_partition; +static uint32_t write_index = 0; +static uint32_t next_erase_index = BLOCK_SIZE; +static bool block_erased = false; +static bool stop_write = false; +static bool is_filled = false; +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + +static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + if (log_output_mode == LOG_STORAGE_TO_FLASH) { +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + esp_bt_controller_log_storage(len, addr, end); +#endif //CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + } else { + portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED; + portENTER_CRITICAL_SAFE(&spinlock); + esp_panic_handler_feed_wdts(); + for (int i = 0; i < len; i++) { + esp_rom_printf("%02x ", addr[i]); + } + + if (end) { + esp_rom_printf("\n"); + } + portEXIT_CRITICAL_SAFE(&spinlock); + } +} + +#if CONFIG_BT_CTRL_LE_LOG_SPI_OUT_EN +static IRAM_ATTR void esp_bt_controller_spi_log_interface(uint32_t len, const uint8_t *addr, bool end) +{ + return ble_log_spi_out_write(BLE_LOG_SPI_OUT_SOURCE_ESP_LEGACY, addr, len); +} +#endif // CONFIG_BT_CTRL_LE_LOG_SPI_OUT_EN + +void esp_ble_controller_log_dump_all(bool output) +{ + if (log_output_mode == LOG_STORAGE_TO_FLASH) { +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + esp_bt_read_ctrl_log_from_flash(output); +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + } else { + portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED; + portENTER_CRITICAL_SAFE(&spinlock); + esp_panic_handler_feed_wdts(); + esp_rom_printf("\r\n[DUMP_START:"); + r_ble_log_async_output_dump_all(output); + esp_rom_printf(":DUMP_END]\r\n"); + portEXIT_CRITICAL_SAFE(&spinlock); + } +} + +void esp_bt_log_output_mode_set(uint8_t output_mode) +{ + log_output_mode = output_mode; +} + +uint8_t esp_bt_log_output_mode_get(void) +{ + return log_output_mode; +} + +esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode) +{ + esp_err_t ret = ESP_OK; + interface_func_t bt_controller_log_interface; + bt_controller_log_interface = esp_bt_controller_log_interface; + bool task_create; + uint8_t buffers = 0; + + if (log_is_inited) { + return ret; + } + +#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 + + switch (log_output_mode) { + case LOG_DUMP_MEMORY: + task_create = false; + break; + case LOG_ASYNC_OUT: + case LOG_STORAGE_TO_FLASH: + task_create = true; +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + if (log_output_mode == LOG_STORAGE_TO_FLASH) { + esp_bt_ctrl_log_partition_get_and_erase_first_block(); + } +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN + break; + case LOG_SPI_OUT: + task_create = true; +#if CONFIG_BT_CTRL_LE_LOG_SPI_OUT_EN + bt_controller_log_interface = esp_bt_controller_spi_log_interface; +#endif // CONFIG_BT_CTRL_LE_LOG_SPI_OUT_EN + break; + default: + assert(0); + } + + ret = r_ble_log_init_async(bt_controller_log_interface, task_create, buffers, (uint32_t *)log_bufs_size); + if (ret == ESP_OK) { + log_is_inited = true; + } + + return ret; +} + +void esp_bt_ontroller_log_deinit(void) +{ + r_ble_log_deinit_async(); + log_is_inited = false; +} + +#if CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void) +{ + log_partition = NULL; + assert(MAX_STORAGE_SIZE % BLOCK_SIZE == 0); + // Find the partition map in the partition table + log_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARTITION_NAME); + assert(log_partition != NULL); + // Prepare data to be read later using the mapped address + ESP_ERROR_CHECK(esp_partition_erase_range(log_partition, 0, BLOCK_SIZE)); + write_index = 0; + next_erase_index = BLOCK_SIZE; + block_erased = false; + is_filled = false; + stop_write = false; +} + +static int esp_bt_controller_log_storage(uint32_t len, const uint8_t *addr, bool end) +{ + if (len > MAX_STORAGE_SIZE) { + return -1; + } + + if (stop_write) { + return 0; + } + + if (((write_index) % BLOCK_SIZE) >= THRESHOLD && !block_erased) { + // esp_rom_printf("Ers nxt: %d,%d\n", next_erase_index, write_index); + esp_partition_erase_range(log_partition, next_erase_index, BLOCK_SIZE); + next_erase_index = (next_erase_index + BLOCK_SIZE) % MAX_STORAGE_SIZE; + block_erased = true; + } + + if (((write_index + len) / BLOCK_SIZE) > (write_index / BLOCK_SIZE)) { + block_erased = false; + } + + if (write_index + len <= MAX_STORAGE_SIZE) { + esp_partition_write(log_partition, write_index, addr, len); + write_index = (write_index + len) % MAX_STORAGE_SIZE; + } else { + uint32_t first_part_len = MAX_STORAGE_SIZE - write_index; + esp_partition_write(log_partition, write_index, addr, first_part_len); + esp_partition_write(log_partition, 0, addr + first_part_len, len - first_part_len); + write_index = len - first_part_len; + is_filled = true; + // esp_rom_printf("old idx: %d,%d\n",next_erase_index, write_index); + } + + return 0; +} + +void esp_bt_read_ctrl_log_from_flash(bool output) +{ + esp_partition_mmap_handle_t mmap_handle; + uint32_t read_index; + const void *mapped_ptr; + const uint8_t *buffer; + uint32_t print_len; + uint32_t max_print_len; + esp_err_t err; + + print_len = 0; + max_print_len = 4096; + err = esp_partition_mmap(log_partition, 0, MAX_STORAGE_SIZE, ESP_PARTITION_MMAP_DATA, &mapped_ptr, &mmap_handle); + if (err != ESP_OK) { + ESP_LOGE("FLASH", "Mmap failed: %s", esp_err_to_name(err)); + return; + } + + portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED; + portENTER_CRITICAL_SAFE(&spinlock); + esp_panic_handler_feed_wdts(); + r_ble_log_async_output_dump_all(true); + esp_bt_ontroller_log_deinit(); + stop_write = true; + + buffer = (const uint8_t *)mapped_ptr; + esp_panic_handler_feed_wdts(); + if (is_filled) { + read_index = next_erase_index; + } else { + read_index = 0; + } + + esp_rom_printf("\r\nREAD_CHECK:%ld,%ld,%d\r\n",read_index, write_index, is_filled); + esp_rom_printf("\r\n[DUMP_START:"); + while (read_index != write_index) { + esp_rom_printf("%02x ", buffer[read_index]); + if (print_len > max_print_len) { + esp_panic_handler_feed_wdts(); + print_len = 0; + } + + print_len++; + read_index = (read_index + 1) % MAX_STORAGE_SIZE; + } + + esp_rom_printf(":DUMP_END]\r\n"); + portEXIT_CRITICAL_SAFE(&spinlock); + esp_partition_munmap(mmap_handle); + err = esp_bt_controller_log_init(log_output_mode); + assert(err == ESP_OK); +} +#endif // CONFIG_BT_CTRL_LE_LOG_STORAGE_EN +#endif // CONFIG_BT_CTRL_LE_LOG_EN + void IRAM_ATTR btdm_hw_mac_power_down_wrapper(void) { #if CONFIG_MAC_BB_PD @@ -994,6 +1282,18 @@ static void coex_bt_wakeup_request_end(void) return; } +static IRAM_ATTR int64_t get_time_us_wrapper(void) +{ + return esp_timer_get_time(); +} + +static IRAM_ATTR void assert_wrapper(void) +{ +#if CONFIG_BT_CTRL_LE_LOG_EN + esp_ble_controller_log_dump_all(true); +#endif // CONFIG_BT_CTRL_LE_LOG_EN +} + bool esp_vhci_host_check_send_available(void) { if (btdm_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { @@ -1456,6 +1756,14 @@ 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); +#if CONFIG_BT_CTRL_LE_LOG_EN + err = esp_bt_controller_log_init(log_output_mode); + if (err != ESP_OK) { + ESP_LOGW(BT_LOG_TAG, "ble_controller_log_init failed %d", err); + goto error; + } +#endif // CONFIG_BT_CTRL_LE_LOG_EN + err = btdm_controller_init(cfg); if (err != 0) { @@ -1563,6 +1871,10 @@ static void bt_controller_deinit_internal(void) #endif esp_phy_modem_deinit(); +#if CONFIG_BT_CTRL_LE_LOG_EN + esp_bt_ontroller_log_deinit(); +#endif // CONFIG_BT_CTRL_LE_LOG_EN + if (osi_funcs_p != NULL) { free(osi_funcs_p); osi_funcs_p = NULL; diff --git a/components/bt/controller/lib_esp32c3_family b/components/bt/controller/lib_esp32c3_family index ed99228396..2ce747aec8 160000 --- a/components/bt/controller/lib_esp32c3_family +++ b/components/bt/controller/lib_esp32c3_family @@ -1 +1 @@ -Subproject commit ed99228396aaa18935b575d600bc19da38dc4746 +Subproject commit 2ce747aec8008d008fe34fa375a2aea3e7e48e9a diff --git a/components/bt/include/esp32c3/include/esp_bt.h b/components/bt/include/esp32c3/include/esp_bt.h index d40a654bf3..56ccc1328b 100644 --- a/components/bt/include/esp32c3/include/esp_bt.h +++ b/components/bt/include/esp32c3/include/esp_bt.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -30,7 +30,7 @@ extern "C" { * * @note Please do not modify this value */ -#define ESP_BT_CTRL_CONFIG_VERSION 0x02410230 +#define ESP_BT_CTRL_CONFIG_VERSION 0x02502230 /** * @brief Internal use only @@ -323,6 +323,24 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); #define BLE_CTRL_CHECK_CONNECT_IND_ACCESS_ADDRESS_ENABLED 0 #endif +#if defined(CONFIG_BT_CTRL_LE_LOG_EN) +#define BT_BLE_LOG_EN CONFIG_BT_CTRL_LE_LOG_EN +#else +#define BT_BLE_LOG_EN (0) +#endif + +#if defined(CONFIG_BT_CTRL_LE_LOG_MODE_EN) +#define BLE_LOG_MODE_EN CONFIG_BT_CTRL_LE_LOG_MODE_EN +#else +#define BLE_LOG_MODE_EN (0) +#endif + +#if defined(CONFIG_BT_CTRL_LE_LOG_LEVEL) +#define BLE_LOG_LEVEL CONFIG_BT_CTRL_LE_LOG_LEVEL +#else +#define BLE_LOG_LEVEL (0) +#endif + #define BT_CONTROLLER_INIT_CONFIG_DEFAULT() { \ .magic = ESP_BT_CTRL_CONFIG_MAGIC_VAL, \ .version = ESP_BT_CTRL_CONFIG_VERSION, \ @@ -369,6 +387,8 @@ typedef void (* esp_bt_hci_tl_callback_t) (void *arg, uint8_t status); .master_en = BT_CTRL_BLE_MASTER, \ .scan_en = BT_CTRL_BLE_SCAN, \ .ble_aa_check = BLE_CTRL_CHECK_CONNECT_IND_ACCESS_ADDRESS_ENABLED, \ + .ble_log_mode_en = BLE_LOG_MODE_EN, \ + .ble_log_level = BLE_LOG_LEVEL, \ } #else @@ -494,6 +514,8 @@ typedef struct { bool master_en; /*!< In the flash mode, True if the master feature is enabled (default); false otherwise. Configurable in menuconfig.*/ bool scan_en; /*!< In the flash mode, True if the scan feature is enabled (default); false otherwise. Configurable in menuconfig.*/ bool ble_aa_check; /*!< True if adds a verification step for the Access Address within the CONNECT_IND PDU; false otherwise. Configurable in menuconfig */ + uint32_t ble_log_mode_en; /*!< BLE log mode enable */ + uint8_t ble_log_level; /*!< BLE log level */ } esp_bt_controller_config_t; /** @@ -904,6 +926,16 @@ void esp_vhci_host_send_packet(uint8_t *data, uint16_t len); */ esp_err_t esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback); +/** + * @brief Select buffers +*/ +typedef enum { + ESP_BLE_LOG_BUF_HCI = 0x02, + ESP_BLE_LOG_BUF_CONTROLLER = 0x05, +} esp_ble_log_buf_t; + +void esp_ble_controller_log_dump_all(bool output); + #ifdef __cplusplus } #endif