feat(bt): Update bt lib for ESP32-C3 and ESP32-S3(723439d)

- Added BLE controller debug log trace
- Added BLE controller log module
This commit is contained in:
chenjianhua
2025-02-25 15:17:49 +08:00
parent 61f992a061
commit d4c15e2cb6
4 changed files with 417 additions and 4 deletions

View File

@@ -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;