mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-01 11:44:31 +02:00
feat(ble): support ble log simple output via SPI interface for ESP32-C5
This commit is contained in:
@@ -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
|
||||
*/
|
||||
@@ -57,6 +57,11 @@
|
||||
|
||||
#include "hal/efuse_hal.h"
|
||||
#include "soc/rtc.h"
|
||||
|
||||
#if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
#include "ble_log/ble_log_spi_out.h"
|
||||
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
|
||||
/* Macro definition
|
||||
************************************************************************
|
||||
*/
|
||||
@@ -100,7 +105,7 @@ struct ext_funcs_t {
|
||||
};
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end);
|
||||
typedef void (*interface_func_t) (uint32_t len, const uint8_t *addr, uint32_t len_append, const uint8_t *addr_append, uint32_t flag);
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
|
||||
/* External functions or variables
|
||||
@@ -109,8 +114,10 @@ typedef void (*interface_func_t) (uint32_t len, const uint8_t*addr, bool end);
|
||||
extern int ble_osi_coex_funcs_register(struct osi_coex_funcs_t *coex_funcs);
|
||||
extern int r_ble_controller_init(esp_bt_controller_config_t *cfg);
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
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_init_async(interface_func_t interface, bool task_create, uint8_t buffers, uint32_t *bufs_size);
|
||||
extern int r_ble_log_deinit_async(void);
|
||||
extern int r_ble_log_init_simple(interface_func_t interface, void *handler);
|
||||
extern void r_ble_log_deinit_simple(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);
|
||||
@@ -173,15 +180,232 @@ 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);
|
||||
#if !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, uint32_t len_append, const uint8_t *addr_append, uint32_t flag);
|
||||
#endif // !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
static void esp_bt_ctrl_log_partition_get_and_erase_first_block(void);
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
#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;
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
const static uint32_t log_bufs_size[] = {CONFIG_BT_LE_LOG_CTRL_BUF1_SIZE, CONFIG_BT_LE_LOG_HCI_BUF_SIZE, CONFIG_BT_LE_LOG_CTRL_BUF2_SIZE};
|
||||
static bool log_is_inited = false;
|
||||
|
||||
esp_err_t esp_bt_controller_log_init(void)
|
||||
{
|
||||
if (log_is_inited) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
if (ble_log_spi_out_init() != 0) {
|
||||
goto spi_out_init_failed;
|
||||
}
|
||||
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
if (r_ble_log_init_simple(ble_log_spi_out_ll_write, ble_log_spi_out_ll_log_ev_proc) != 0) {
|
||||
goto log_init_failed;
|
||||
}
|
||||
#else // !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
uint8_t buffers = 0;
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
|
||||
buffers |= ESP_BLE_LOG_BUF_CONTROLLER;
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
|
||||
buffers |= ESP_BLE_LOG_BUF_HCI;
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
|
||||
|
||||
bool task_create = true;
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
|
||||
task_create = false;
|
||||
#elif CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
esp_bt_ctrl_log_partition_get_and_erase_first_block();
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
|
||||
if (r_ble_log_init_async(esp_bt_controller_log_interface, task_create, buffers, (uint32_t *)log_bufs_size) != 0) {
|
||||
goto log_init_failed;
|
||||
}
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
|
||||
if (r_ble_log_ctrl_level_and_mod(CONFIG_BT_LE_CONTROLLER_LOG_OUTPUT_LEVEL, CONFIG_BT_LE_CONTROLLER_LOG_MOD_OUTPUT_SWITCH) != ESP_OK) {
|
||||
goto ctrl_level_init_failed;
|
||||
}
|
||||
log_is_inited = true;
|
||||
return ESP_OK;
|
||||
|
||||
ctrl_level_init_failed:
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
r_ble_log_deinit_simple();
|
||||
#else // !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
r_ble_log_deinit_async();
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
log_init_failed:
|
||||
#if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
ble_log_spi_out_deinit();
|
||||
spi_out_init_failed:
|
||||
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
void esp_bt_controller_log_deinit(void)
|
||||
{
|
||||
#if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
ble_log_spi_out_deinit();
|
||||
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
r_ble_log_deinit_simple();
|
||||
#else // !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
r_ble_log_deinit_async();
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
|
||||
log_is_inited = false;
|
||||
}
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
#include "esp_partition.h"
|
||||
#include "hal/wdt_hal.h"
|
||||
|
||||
#define MAX_STORAGE_SIZE (CONFIG_BT_LE_CONTROLLER_LOG_PARTITION_SIZE)
|
||||
#define BLOCK_SIZE (4096)
|
||||
#define THRESHOLD (3072)
|
||||
#define PARTITION_NAME "bt_ctrl_log"
|
||||
|
||||
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;
|
||||
|
||||
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_controller_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();
|
||||
assert(err == ESP_OK);
|
||||
}
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE
|
||||
void esp_task_wdt_isr_user_handler(void)
|
||||
{
|
||||
esp_ble_controller_log_dump_all(true);
|
||||
}
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_TASK_WDT_USER_HANDLER_ENABLE
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE
|
||||
void __real_esp_panic_handler(void *info);
|
||||
void __wrap_esp_panic_handler (void *info)
|
||||
{
|
||||
esp_ble_controller_log_dump_all(true);
|
||||
__real_esp_panic_handler(info);
|
||||
}
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_WRAP_PANIC_HANDLER_ENABLE
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
|
||||
/* This variable tells if BLE is running */
|
||||
@@ -664,20 +888,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg)
|
||||
#endif // CONFIG_SW_COEXIST_ENABLE
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
interface_func_t bt_controller_log_interface;
|
||||
bt_controller_log_interface = esp_bt_controller_log_interface;
|
||||
uint8_t buffers = 0;
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
|
||||
buffers |= ESP_BLE_LOG_BUF_CONTROLLER;
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_CTRL_ENABLED
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
|
||||
buffers |= ESP_BLE_LOG_BUF_HCI;
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_HCI_ENABLED
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_DUMP_ONLY
|
||||
ret = r_ble_log_init_async(bt_controller_log_interface, false, buffers, (uint32_t *)log_bufs_size);
|
||||
#else
|
||||
ret = r_ble_log_init_async(bt_controller_log_interface, true, buffers, (uint32_t *)log_bufs_size);
|
||||
#endif // CONFIG_BT_CONTROLLER_LOG_DUMP
|
||||
ret = esp_bt_controller_log_init();
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGW(NIMBLE_PORT_LOG_TAG, "ble_controller_log_init failed %d", ret);
|
||||
goto modem_deint;
|
||||
@@ -738,7 +949,7 @@ free_controller:
|
||||
modem_deint:
|
||||
esp_ble_unregister_bb_funcs();
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
r_ble_log_deinit_async();
|
||||
esp_bt_controller_log_deinit();
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
esp_phy_modem_deinit();
|
||||
// modem_clock_deselect_lp_clock_source(PERIPH_BT_MODULE);
|
||||
@@ -774,7 +985,7 @@ esp_err_t esp_bt_controller_deinit(void)
|
||||
r_ble_controller_deinit();
|
||||
esp_ble_unregister_bb_funcs();
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
r_ble_log_deinit_async();
|
||||
esp_bt_controller_log_deinit();
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
|
||||
#if CONFIG_BT_NIMBLE_ENABLED
|
||||
@@ -1092,26 +1303,45 @@ 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)
|
||||
#if !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
static void esp_bt_controller_log_interface(uint32_t len, const uint8_t *addr, uint32_t len_append, const uint8_t *addr_append, uint32_t flag)
|
||||
{
|
||||
bool end = flag ? true : false;
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
esp_bt_controller_log_storage(len, addr, end);
|
||||
#else // !CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
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);
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
}
|
||||
#endif // !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
|
||||
void esp_ble_controller_log_dump_all(bool output)
|
||||
{
|
||||
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||
#if CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
ble_log_spi_out_dump_all();
|
||||
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED
|
||||
|
||||
#if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
|
||||
esp_bt_read_ctrl_log_from_flash(output);
|
||||
#elif !CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
|
||||
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||
portENTER_CRITICAL_SAFE(&spinlock);
|
||||
esp_panic_handler_feed_wdts();
|
||||
BT_ASSERT_PRINT("\r\n[DUMP_START:");
|
||||
r_ble_log_async_output_dump_all(output);
|
||||
BT_ASSERT_PRINT(":DUMP_END]\r\n");
|
||||
portEXIT_CRITICAL_SAFE(&spinlock);
|
||||
#endif
|
||||
}
|
||||
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
|
||||
|
||||
|
Reference in New Issue
Block a user