Skip to content

Commit 5a21fd1

Browse files
committed
Merge branch 'feat/add_spi_output' into 'master'
Feat/add spi output Closes BLERP-1526 See merge request espressif/esp-idf!36612
2 parents 5fd47af + 9c98c7e commit 5a21fd1

File tree

11 files changed

+503
-0
lines changed

11 files changed

+503
-0
lines changed

components/bt/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ set(common_include_dirs
2424
common/btc/profile/esp/blufi/include
2525
common/btc/profile/esp/include
2626
common/hci_log/include
27+
common/ble_log/include
2728
)
2829

2930
set(ble_mesh_include_dirs
@@ -132,6 +133,10 @@ if(CONFIG_BT_ENABLED)
132133
"porting/mem/bt_osi_mem.c"
133134
)
134135

136+
if(CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED)
137+
list(APPEND srcs "common/ble_log/ble_log_spi_out.c")
138+
endif()
139+
135140
# Host Bluedroid
136141
if(CONFIG_BT_BLUEDROID_ENABLED)
137142

@@ -872,6 +877,7 @@ idf_component_register(SRCS "${srcs}"
872877
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
873878
REQUIRES esp_timer esp_wifi
874879
PRIV_REQUIRES nvs_flash soc esp_pm esp_phy esp_coex mbedtls esp_driver_uart vfs esp_ringbuf
880+
esp_driver_spi
875881
LDFRAGMENTS "${ldscripts}")
876882

877883
if(CONFIG_BT_ENABLED)
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "ble_log/ble_log_spi_out.h"
7+
8+
// Private defines
9+
#define SPI_OUT_BUS SPI2_HOST
10+
11+
// Private typedefs
12+
typedef struct spi_out_trans
13+
{
14+
spi_transaction_t trans;
15+
struct spi_out_trans *next;
16+
} spi_out_trans_t;
17+
18+
// Private variables
19+
static spi_device_handle_t spi_handle = NULL;
20+
static spi_out_trans_t *trans_head = NULL;
21+
static SemaphoreHandle_t mutex_handle = NULL;
22+
static bool spi_out_inited = false;
23+
24+
// Private function declarations
25+
static void spi_out_init_trans(void);
26+
static void spi_out_deinit_trans(void);
27+
static void spi_out_recycle_trans(uint32_t ms_to_wait);
28+
static void spi_out_append_trans(void);
29+
30+
// Private functions
31+
static void spi_out_init_trans(void)
32+
{
33+
for (int i = 0; i < CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_QUEUE_SIZE; i++)
34+
{
35+
// Allocate memory for SPI transaction
36+
uint8_t *buf = (uint8_t *)spi_bus_dma_memory_alloc(SPI_OUT_BUS, CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE, 0);
37+
assert(buf);
38+
39+
// Initialize new trans
40+
spi_out_trans_t *new_trans = (spi_out_trans_t *)malloc(sizeof(spi_out_trans_t));
41+
assert(new_trans);
42+
memset(new_trans, 0, sizeof(spi_out_trans_t));
43+
new_trans->trans.tx_buffer = buf;
44+
new_trans->trans.length = 0;
45+
46+
// Append new trans to free trans list
47+
new_trans->next = trans_head;
48+
trans_head = new_trans;
49+
}
50+
return;
51+
}
52+
53+
static void spi_out_deinit_trans(void)
54+
{
55+
// Wait up to QUEUE_SIZE * 100 ms for all transactions to complete and be recycled
56+
spi_out_recycle_trans(100);
57+
58+
// Release memory
59+
spi_out_trans_t *next;
60+
while (trans_head != NULL)
61+
{
62+
next = trans_head->next;
63+
free((uint8_t *)trans_head->trans.tx_buffer);
64+
free(trans_head);
65+
trans_head = next;
66+
}
67+
trans_head = NULL;
68+
return;
69+
}
70+
71+
IRAM_ATTR static void spi_out_recycle_trans(uint32_t ms_to_wait)
72+
{
73+
// Try to recycle transaction
74+
spi_transaction_t *ret_trans;
75+
spi_out_trans_t *recycled_trans;
76+
while (ESP_OK == spi_device_get_trans_result(spi_handle, &ret_trans, pdMS_TO_TICKS(ms_to_wait)))
77+
{
78+
recycled_trans = __containerof(ret_trans, spi_out_trans_t, trans);
79+
recycled_trans->next = trans_head;
80+
trans_head = recycled_trans;
81+
trans_head->trans.length = 0;
82+
}
83+
}
84+
85+
IRAM_ATTR static void spi_out_append_trans(void)
86+
{
87+
// Transaction head shall not be NULL for appending
88+
assert(trans_head);
89+
90+
// Detach transaction head
91+
spi_out_trans_t *trans_to_append = trans_head;
92+
trans_head = trans_head->next;
93+
trans_to_append->next = NULL;
94+
95+
// CRITICAL: Length unit conversion from bytes to bits
96+
trans_to_append->trans.length *= 8;
97+
assert(ESP_OK == spi_device_queue_trans(spi_handle, &trans_to_append->trans, 0));
98+
99+
// Try to recycle trans
100+
spi_out_recycle_trans(0);
101+
}
102+
103+
// Public functions
104+
void ble_log_spi_out_init(void)
105+
{
106+
// Avoid double init
107+
if (spi_out_inited)
108+
{
109+
return;
110+
}
111+
112+
// Initialize SPI
113+
spi_bus_config_t bus_config = {
114+
.miso_io_num = -1,
115+
.mosi_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_MOSI_IO_NUM,
116+
.sclk_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_SCLK_IO_NUM,
117+
.quadwp_io_num = -1,
118+
.quadhd_io_num = -1,
119+
.max_transfer_sz = 10240
120+
};
121+
spi_device_interface_config_t dev_config = {
122+
.clock_speed_hz = SPI_MASTER_FREQ_20M,
123+
.mode = 0,
124+
.spics_io_num = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_CS_IO_NUM,
125+
.queue_size = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_QUEUE_SIZE
126+
};
127+
ESP_ERROR_CHECK(spi_bus_initialize(SPI_OUT_BUS, &bus_config, SPI_DMA_CH_AUTO));
128+
ESP_ERROR_CHECK(spi_bus_add_device(SPI_OUT_BUS, &dev_config, &spi_handle));
129+
130+
// Initialize transaction link nodes
131+
spi_out_init_trans();
132+
133+
// Initialize mutex
134+
mutex_handle = xSemaphoreCreateMutex();
135+
136+
// Set init flag
137+
spi_out_inited = true;
138+
}
139+
140+
void ble_log_spi_out_deinit(void)
141+
{
142+
// Avoid double deinit
143+
if (!spi_out_inited)
144+
{
145+
return;
146+
}
147+
148+
// Deinitialize transaction link nodes
149+
spi_out_deinit_trans();
150+
151+
// Deinitialize SPI
152+
ESP_ERROR_CHECK(spi_bus_remove_device(spi_handle));
153+
ESP_ERROR_CHECK(spi_bus_free(SPI_OUT_BUS));
154+
spi_handle = NULL;
155+
156+
// Deinitialize mutex
157+
vSemaphoreDelete(mutex_handle);
158+
mutex_handle = NULL;
159+
160+
// Reset init flag
161+
spi_out_inited = false;
162+
}
163+
164+
IRAM_ATTR void ble_log_spi_out_write(uint32_t len, const uint8_t *addr, spi_out_source_t source)
165+
{
166+
// Take semaphore
167+
assert(xSemaphoreTake(mutex_handle, portMAX_DELAY) == pdTRUE);
168+
169+
// Recycle trans if free buffer list is empty
170+
if (!trans_head)
171+
{
172+
spi_out_recycle_trans(0);
173+
}
174+
175+
// Length of 0 means flush out
176+
if (!len)
177+
{
178+
assert(trans_head);
179+
if (trans_head->trans.length)
180+
{
181+
spi_out_append_trans();
182+
}
183+
goto release;
184+
}
185+
186+
// Copy user data to buffer
187+
uint32_t copy_buf_len;
188+
uint32_t data_left_len = len;
189+
uint32_t empty_buf_len = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE - trans_head->trans.length;
190+
while (data_left_len)
191+
{
192+
// There shall always be available buffer in free buffer list during write operation
193+
assert(trans_head);
194+
195+
// Copy data to buffer and update length
196+
copy_buf_len = (data_left_len > empty_buf_len) ? empty_buf_len : data_left_len;
197+
memcpy((uint8_t *)trans_head->trans.tx_buffer + trans_head->trans.length, addr + (len - data_left_len), copy_buf_len);
198+
trans_head->trans.length += copy_buf_len;
199+
data_left_len -= copy_buf_len;
200+
201+
// Transaction buffer length shall never exceed buffer size
202+
assert(trans_head->trans.length <= CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE);
203+
204+
// If buffer is full, append transaction and reset buffer length
205+
if (trans_head->trans.length == CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE)
206+
{
207+
spi_out_append_trans();
208+
empty_buf_len = CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE;
209+
}
210+
}
211+
212+
release:
213+
xSemaphoreGive(mutex_handle);
214+
return;
215+
}
216+
217+
IRAM_ATTR void ble_log_spi_out_write_esp(uint32_t len, const uint8_t *addr, bool end)
218+
{
219+
ble_log_spi_out_write(len, addr, esp_controller);
220+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef __BT_SPI_OUT_H__
7+
#define __BT_SPI_OUT_H__
8+
9+
#include <string.h>
10+
#include "driver/spi_master.h"
11+
#include "freertos/semphr.h"
12+
13+
// Public typedefs
14+
typedef enum
15+
{
16+
esp_controller = 0,
17+
ceva_controller = 1
18+
} spi_out_source_t;
19+
20+
// Public functions
21+
void ble_log_spi_out_init(void);
22+
void ble_log_spi_out_deinit(void);
23+
void ble_log_spi_out_write(uint32_t len, const uint8_t *addr, spi_out_source_t source);
24+
void ble_log_spi_out_write_esp(uint32_t len, const uint8_t *addr, bool end);
25+
26+
#endif // __BT_SPI_OUT_H__

components/bt/controller/esp32c2/Kconfig.in

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,49 @@ config BT_LE_CONTROLLER_LOG_DUMP_ONLY
308308
help
309309
Only operate in dump mode
310310

311+
config BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
312+
bool "Output ble controller logs to SPI bus (Experimental)"
313+
depends on BT_LE_CONTROLLER_LOG_ENABLED
314+
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY
315+
default n
316+
help
317+
Output ble controller logs to SPI bus
318+
319+
config BT_LE_CONTROLLER_LOG_SPI_OUT_QUEUE_SIZE
320+
int "Number of ble controller log async SPI output queues"
321+
depends on BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
322+
default 4
323+
help
324+
The number of ble controller log async SPI output queues
325+
326+
config BT_LE_CONTROLLER_LOG_SPI_OUT_TRANS_BUF_SIZE
327+
int "Size of ble controller log async SPI output transaction buffer size"
328+
depends on BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
329+
default 512
330+
help
331+
The size of ble controller log async SPI output transaction buffer size
332+
333+
config BT_LE_CONTROLLER_LOG_SPI_OUT_MOSI_IO_NUM
334+
int "GPIO number of SPI MOSI"
335+
depends on BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
336+
default 1
337+
help
338+
GPIO number of SPI MOSI
339+
340+
config BT_LE_CONTROLLER_LOG_SPI_OUT_SCLK_IO_NUM
341+
int "GPIO number of SPI SCLK"
342+
depends on BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
343+
default 6
344+
help
345+
GPIO number of SPI SCLK
346+
347+
config BT_LE_CONTROLLER_LOG_SPI_OUT_CS_IO_NUM
348+
int "GPIO number of SPI CS"
349+
depends on BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
350+
default 7
351+
help
352+
GPIO number of SPI CS
353+
311354
config BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
312355
bool "Store ble controller logs to flash(Experimental)"
313356
depends on !BT_LE_CONTROLLER_LOG_DUMP_ONLY

components/bt/controller/esp32c2/bt.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@
6464
#include "hal/efuse_ll.h"
6565
#include "soc/rtc.h"
6666

67+
#if CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
68+
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
69+
#include "ble_log/ble_log_spi_out.h"
70+
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
71+
#endif // CONFIG_BT_LE_CONTROLLER_LOG_ENABLED
72+
6773
/* Macro definition
6874
************************************************************************
6975
*/
@@ -210,6 +216,7 @@ enum log_out_mode {
210216
LOG_DUMP_MEMORY,
211217
LOG_ASYNC_OUT,
212218
LOG_STORAGE_TO_FLASH,
219+
LOG_SPI_OUT,
213220
};
214221

215222
bool log_is_inited = false;
@@ -218,6 +225,8 @@ uint8_t log_output_mode = LOG_DUMP_MEMORY;
218225
#else
219226
#if CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
220227
uint8_t log_output_mode = LOG_STORAGE_TO_FLASH;
228+
#elif CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
229+
uint8_t log_output_mode = LOG_SPI_OUT;
221230
#else
222231
uint8_t log_output_mode = LOG_ASYNC_OUT;
223232
#endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
@@ -265,6 +274,13 @@ esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode)
265274
}
266275
#endif // CONFIG_BT_LE_CONTROLLER_LOG_STORAGE_ENABLE
267276
break;
277+
case LOG_SPI_OUT:
278+
task_create = true;
279+
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
280+
ble_log_spi_out_init();
281+
bt_controller_log_interface = ble_log_spi_out_write_esp;
282+
#endif // CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
283+
break;
268284
default:
269285
assert(0);
270286
}
@@ -280,6 +296,9 @@ esp_err_t esp_bt_controller_log_init(uint8_t log_output_mode)
280296
void esp_bt_ontroller_log_deinit(void)
281297
{
282298
ble_log_deinit_async();
299+
#if CONFIG_BT_LE_CONTROLLER_LOG_SPI_OUT_ENABLED
300+
ble_log_spi_out_deinit();
301+
#endif
283302
log_is_inited = false;
284303
}
285304

0 commit comments

Comments
 (0)