Skip to content

Commit a38893d

Browse files
committed
Merge branch 'feat/add_ble_log_spi_out_printf' into 'master'
feat(ble): add printf interface for ble log spi out Closes BLERP-1582 See merge request espressif/esp-idf!37162
2 parents a5fe1e2 + f8efa4c commit a38893d

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ IRAM_ATTR static void esp_timer_cb_flushout(void)
176176
if (trans_head->trans.length) {
177177
spi_out_append_trans();
178178
}
179-
}
180-
else {
179+
} else {
181180
// Restart flushout timer
182181
esp_timer_start_once(flushout_timer_handle, SPI_OUT_FLUSHOUT_TIMEOUT);
183182
}
@@ -374,8 +373,7 @@ IRAM_ATTR void ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16
374373
const uint8_t tail = SPI_OUT_TAIL;
375374

376375
// Write frame head first, then payload, finally frame tail
377-
do
378-
{
376+
do {
379377
if (spi_out_write(head, 4) != 0) {
380378
loss_frame_cnt++;
381379
break;
@@ -398,4 +396,54 @@ IRAM_ATTR void ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16
398396
return;
399397
}
400398

399+
IRAM_ATTR int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
400+
{
401+
// Get esp timestamp
402+
uint32_t esp_ts = esp_timer_get_time();
403+
404+
// Get arguments
405+
va_list args;
406+
va_start(args, format);
407+
408+
// Get len as ref to allocate heap memory
409+
va_list args_copy;
410+
va_copy(args_copy, args);
411+
int len = vsnprintf(NULL, 0, format, args_copy);
412+
va_end(args_copy);
413+
414+
// Length validation
415+
if ((len < 0) || (len > 0xFFFF)) {
416+
va_end(args);
417+
return -1;
418+
}
419+
420+
// Allocate memory
421+
uint8_t *buffer = malloc(len + 1);
422+
if (!buffer) {
423+
va_end(args);
424+
return -1;
425+
}
426+
427+
// Generate string
428+
vsnprintf((char *)buffer, len + 1, format, args);
429+
va_end(args);
430+
431+
// Write to SPI
432+
ble_log_spi_out_write(source, (const uint8_t *)&esp_ts, 4);
433+
ble_log_spi_out_write(source, (const uint8_t *)buffer, len);
434+
435+
// Release
436+
free(buffer);
437+
return 0;
438+
}
439+
440+
IRAM_ATTR void ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len)
441+
{
442+
// Get esp timestamp
443+
uint32_t esp_ts = esp_timer_get_time();
444+
445+
// Write to SPI
446+
ble_log_spi_out_write(source, (const uint8_t *)&esp_ts, 4);
447+
ble_log_spi_out_write(source, addr, len);
448+
}
401449
#endif // CONFIG_BT_BLE_LOG_SPI_OUT_ENABLED

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ void ble_log_spi_out_write(uint8_t source, const uint8_t *addr, uint16_t len);
2929
void ble_log_spi_out_write_esp(uint32_t len, const uint8_t *addr, bool end);
3030
void ble_log_spi_out_ts_sync_start(void);
3131
void ble_log_spi_out_ts_sync_stop(void);
32+
int ble_log_spi_out_printf(uint8_t source, const char *format, ...);
33+
void ble_log_spi_out_write_with_ts(uint8_t source, const uint8_t *addr, uint16_t len);
3234

3335
#endif // __BT_SPI_OUT_H__

0 commit comments

Comments
 (0)