Skip to content

Commit 5db0534

Browse files
committed
Merge branch 'feat/add_ble_spi_log_v5.3' into 'release/v5.3'
fix(ble/bluedroid): Support SPI log output options for HCI (v5.3) See merge request espressif/esp-idf!37340
2 parents 284120b + ac9ac0a commit 5db0534

File tree

9 files changed

+297
-10
lines changed

9 files changed

+297
-10
lines changed

components/bt/common/Kconfig.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ config BT_BLE_LOG_SPI_OUT_ENABLED
1212
help
1313
Output ble logs to SPI bus
1414

15+
config BT_BLE_LOG_SPI_OUT_HCI_ENABLED
16+
bool "Enable HCI log output to SPI"
17+
depends on BT_BLE_LOG_SPI_OUT_ENABLED
18+
default n
19+
help
20+
Enable logging of HCI packets to the SPI bus when BLE SPI log output is enabled.
21+
22+
config BT_BLE_LOG_SPI_OUT_HOST_ENABLED
23+
bool "Enable Host log output to SPI"
24+
depends on BT_BLE_LOG_SPI_OUT_ENABLED
25+
default n
26+
help
27+
This configuration applies to the logs of both Bluedroid Host and NimBLE Host.
28+
When BLE SPI log output is enabled, this option allows host logs to be transmitted via SPI.
29+
1530
config BT_BLE_LOG_SPI_OUT_QUEUE_SIZE
1631
int "Number of ble log async SPI output queues"
1732
depends on BT_BLE_LOG_SPI_OUT_ENABLED

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ IRAM_ATTR static void esp_timer_cb_flushout(void)
176176
if (trans_head->trans.length) {
177177
spi_out_append_trans();
178178
}
179-
} else {
180-
// Restart flushout timer
179+
}
180+
181+
// Restart flushout timer if not active
182+
if (!esp_timer_is_active(flushout_timer_handle)) {
181183
esp_timer_start_once(flushout_timer_handle, SPI_OUT_FLUSHOUT_TIMEOUT);
182184
}
183185

@@ -193,6 +195,9 @@ IRAM_ATTR static void esp_timer_cb_ts_sync(void)
193195
uint32_t lc_ts = 0;
194196
uint32_t esp_ts = 0;
195197

198+
// Toggle sync IO
199+
sync_io_level = !sync_io_level;
200+
196201
// Enter critical
197202
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
198203
portENTER_CRITICAL_SAFE(&spinlock);
@@ -205,7 +210,7 @@ IRAM_ATTR static void esp_timer_cb_ts_sync(void)
205210
lc_ts = r_os_cputime_get32();
206211
#endif // CONFIG_IDF_TARGET_ESP32C2
207212

208-
// Toggle Sync IO
213+
// Set sync IO level
209214
gpio_set_level(CONFIG_BT_BLE_LOG_SPI_OUT_SYNC_IO_NUM, (uint32_t)sync_io_level);
210215

211216
// Get ESP timestamp
@@ -219,9 +224,6 @@ IRAM_ATTR static void esp_timer_cb_ts_sync(void)
219224
memcpy(sync_frame + 1, &lc_ts, sizeof(lc_ts));
220225
memcpy(sync_frame + 5, &esp_ts, sizeof(esp_ts));
221226
ble_log_spi_out_write(BLE_LOG_SPI_OUT_SOURCE_SYNC, sync_frame, 9);
222-
223-
// Update IO level
224-
sync_io_level = !sync_io_level;
225227
}
226228
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_TS_SYNC_ENABLED
227229

@@ -351,6 +353,10 @@ void ble_log_spi_out_ts_sync_stop(void)
351353
if (esp_timer_is_active(ts_sync_timer_handle)) {
352354
esp_timer_stop(ts_sync_timer_handle);
353355
}
356+
357+
// Set sync IO to low level
358+
sync_io_level = 0;
359+
gpio_set_level(CONFIG_BT_BLE_LOG_SPI_OUT_SYNC_IO_NUM, (uint32_t)sync_io_level);
354360
}
355361
}
356362
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_TS_SYNC_ENABLED
@@ -437,6 +443,53 @@ IRAM_ATTR int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
437443
return 0;
438444
}
439445

446+
IRAM_ATTR int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...)
447+
{
448+
// Get ESP timestamp
449+
uint32_t esp_ts = esp_timer_get_time();
450+
451+
// Create log prefix in the format: "[level][tag] "
452+
char prefix[32];
453+
int prefix_len = snprintf(prefix, sizeof(prefix), "[%d][%s] ", level, tag ? tag : "NULL");
454+
455+
// Compute the length of the formatted log message
456+
va_list args;
457+
va_start(args, format);
458+
va_list args_copy;
459+
va_copy(args_copy, args);
460+
int log_len = vsnprintf(NULL, 0, format, args_copy);
461+
va_end(args_copy);
462+
463+
// Validate length
464+
if (log_len < 0 || log_len > 0xFFFF) {
465+
va_end(args);
466+
return -1;
467+
}
468+
469+
// Compute total log length (prefix + formatted message)
470+
int total_len = prefix_len + log_len;
471+
472+
// Allocate memory for the complete log message
473+
uint8_t *buffer = malloc(total_len + 1);
474+
if (!buffer) {
475+
va_end(args);
476+
return -1;
477+
}
478+
479+
// Construct the final log message
480+
memcpy(buffer, prefix, prefix_len); // Copy the prefix
481+
vsnprintf((char *)(buffer + prefix_len), log_len + 1, format, args);
482+
va_end(args);
483+
484+
// Transmit log data via SPI
485+
ble_log_spi_out_write(source, (const uint8_t *)&esp_ts, 4);
486+
ble_log_spi_out_write(source, buffer, total_len);
487+
488+
// Free allocated memory
489+
free(buffer);
490+
return 0;
491+
}
492+
440493
IRAM_ATTR void ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len)
441494
{
442495
// Get esp timestamp

components/bt/common/ble_log/include/ble_log/ble_log_spi_out.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef __BT_SPI_OUT_H__
77
#define __BT_SPI_OUT_H__
88

9+
#include <stdarg.h>
910
#include <string.h>
1011
#include "driver/spi_master.h"
1112
#include "driver/gpio.h"
@@ -19,9 +20,19 @@
1920
#define BLE_LOG_SPI_OUT_SOURCE_NIMBLE 3
2021
#define BLE_LOG_SPI_OUT_SOURCE_HCI_UPSTREAM 4
2122
#define BLE_LOG_SPI_OUT_SOURCE_HCI_DOWNSTREAM 5
23+
#define BLE_LOG_SPI_OUT_SOURCE_USER 0x10
2224
#define BLE_LOG_SPI_OUT_SOURCE_SYNC 0xFE
2325
#define BLE_LOG_SPI_OUT_SOURCE_LOSS 0xFF
2426

27+
// SPI Log Level Definitions
28+
#define BLE_LOG_SPI_OUT_LEVEL_NONE 0 /*!< No log output */
29+
#define BLE_LOG_SPI_OUT_LEVEL_ERROR 1 /*!< Critical errors that SPI driver cannot recover from */
30+
#define BLE_LOG_SPI_OUT_LEVEL_WARN 2 /*!< Recoverable error conditions in SPI communication */
31+
#define BLE_LOG_SPI_OUT_LEVEL_INFO 3 /*!< Informational messages about SPI transactions */
32+
#define BLE_LOG_SPI_OUT_LEVEL_DEBUG 4 /*!< Detailed debug information, such as SPI register values */
33+
#define BLE_LOG_SPI_OUT_LEVEL_VERBOSE 5 /*!< Very detailed debugging logs, potentially flooding output */
34+
#define BLE_LOG_SPI_OUT_LEVEL_MAX 6 /*!< Number of SPI log levels supported */
35+
2536
// Public functions
2637
void ble_log_spi_out_init(void);
2738
void ble_log_spi_out_deinit(void);
@@ -30,6 +41,7 @@ void ble_log_spi_out_write_esp(uint32_t len, const uint8_t *addr, bool end);
3041
void ble_log_spi_out_ts_sync_start(void);
3142
void ble_log_spi_out_ts_sync_stop(void);
3243
int ble_log_spi_out_printf(uint8_t source, const char *format, ...);
44+
int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, const char *format, ...);
3345
void ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len);
3446

3547
#endif // __BT_SPI_OUT_H__

components/bt/common/include/bt_common.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -84,6 +84,20 @@
8484
#define BT_HCI_LOG_INCLUDED FALSE
8585
#endif
8686

87+
// HCI LOG TO SPI
88+
#if UC_BT_BLE_LOG_SPI_OUT_HCI_ENABLED
89+
#define BT_BLE_LOG_SPI_OUT_HCI_ENABLED UC_BT_BLE_LOG_SPI_OUT_HCI_ENABLED
90+
#else
91+
#define BT_BLE_LOG_SPI_OUT_HCI_ENABLED FALSE
92+
#endif
93+
94+
// BLURDROID LOG TO SPI
95+
#if UC_BT_BLE_LOG_SPI_OUT_HOST_ENABLED
96+
#define BT_BLE_LOG_SPI_OUT_HOST_ENABLED UC_BT_BLE_LOG_SPI_OUT_HOST_ENABLED
97+
#else
98+
#define BT_BLE_LOG_SPI_OUT_HOST_ENABLED FALSE
99+
#endif
100+
87101
#if UC_BT_HCI_LOG_DATA_BUFFER_SIZE
88102
#define HCI_LOG_DATA_BUFFER_SIZE UC_BT_HCI_LOG_DATA_BUFFER_SIZE
89103
#else

components/bt/common/include/bt_user_config.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -126,6 +126,20 @@
126126
#define UC_BT_HCI_LOG_DEBUG_EN FALSE
127127
#endif
128128

129+
//HCI LOG TO SPI
130+
#ifdef CONFIG_BT_BLE_LOG_SPI_OUT_HCI_ENABLED
131+
#define UC_BT_BLE_LOG_SPI_OUT_HCI_ENABLED TRUE
132+
#else
133+
#define UC_BT_BLE_LOG_SPI_OUT_HCI_ENABLED FALSE
134+
#endif
135+
136+
//BLUEDROID LOG TO SPI
137+
#ifdef CONFIG_BT_BLE_LOG_SPI_OUT_HOST_ENABLED
138+
#define UC_BT_BLE_LOG_SPI_OUT_HOST_ENABLED TRUE
139+
#else
140+
#define UC_BT_BLE_LOG_SPI_OUT_HOST_ENABLED FALSE
141+
#endif
142+
129143
#ifdef CONFIG_BT_HCI_LOG_DATA_BUFFER_SIZE
130144
#define UC_BT_HCI_LOG_DATA_BUFFER_SIZE CONFIG_BT_HCI_LOG_DATA_BUFFER_SIZE
131145
#else

components/bt/host/bluedroid/api/esp_bluedroid_hci.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,6 +16,10 @@
1616

1717
#define LOG_TAG "HCI_API"
1818

19+
#if CONFIG_BT_BLE_LOG_SPI_OUT_HCI_ENABLED
20+
#include "ble_log/ble_log_spi_out.h"
21+
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_HCI_ENABLED
22+
1923
static esp_bluedroid_hci_driver_operations_t s_hci_driver_ops = { 0 };
2024

2125
esp_err_t esp_bluedroid_attach_hci_driver(const esp_bluedroid_hci_driver_operations_t *p_ops)
@@ -63,6 +67,9 @@ void hci_host_send_packet(uint8_t *data, uint16_t len)
6367
#if (BT_HCI_LOG_INCLUDED == TRUE)
6468
bt_hci_log_record_hci_data(data[0], &data[1], len - 1);
6569
#endif
70+
#if (BT_BLE_LOG_SPI_OUT_HCI_ENABLED && !SOC_ESP_NIMBLE_CONTROLLER)
71+
ble_log_spi_out_write_with_ts(BLE_LOG_SPI_OUT_SOURCE_HCI_DOWNSTREAM, data, len);
72+
#endif // (BT_BLE_LOG_SPI_OUT_HCI_ENABLED && !SOC_ESP_NIMBLE_CONTROLLER)
6673
#if (BT_CONTROLLER_INCLUDED == TRUE)
6774
esp_vhci_host_send_packet(data, len);
6875
#else /* BT_CONTROLLER_INCLUDED == TRUE */

0 commit comments

Comments
 (0)