Skip to content

Commit f7fde9e

Browse files
committed
pbdrv/uart_debug_first_port: fix bugs/va_list func
It's useful to have a vprintf version of the uart_debug functions. This will be used in a later change to assist with logging messages originating in BTStack. Also, there was a bug where the error from writing to the UART was not captured. This fixes that bug and logs the error on the next attempt to use the port.
1 parent 28d9654 commit f7fde9e

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

lib/pbio/drv/uart/uart_debug_first_port.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <pbdrv/uart.h>
99

1010
#include <pbio/os.h>
11+
#include <pbio_os_config.h>
1112

1213
#include <stdio.h>
1314
#include <stdarg.h>
1415
#include <string.h>
1516

16-
#define BUF_SIZE (256)
17+
#define BUF_SIZE (1024)
1718

1819
static uint8_t ring_buf[BUF_SIZE];
1920
static size_t ring_head = 0;
@@ -24,21 +25,16 @@ static pbdrv_uart_dev_t *debug_uart = NULL;
2425
/**
2526
* Formats and stores a string in the UART debug ring buffer.
2627
*
27-
* This function works similarly to printf, but instead of printing to the
28+
* This function works similarly to vprintf, but instead of printing to the
2829
* standard output. The formatted string will be written to the UART when the
2930
* buffer is processed.
3031
*
3132
* @param format The format string, similar to printf.
32-
* @param ... The variable arguments, similar to printf.
33+
* @param va_list The variable arguments, as a va_list.
3334
*/
34-
void pbdrv_uart_debug_printf(const char *format, ...) {
35-
35+
void pbdrv_uart_debug_vprintf(const char *format, va_list args) {
3636
char buf[BUF_SIZE];
37-
va_list args;
38-
va_start(args, format);
39-
vsnprintf(buf, sizeof(ring_buf), format, args);
40-
va_end(args);
41-
37+
vsnprintf(buf, sizeof(buf), format, args);
4238
size_t len = strlen(buf);
4339
for (size_t i = 0; i < len; i++) {
4440
ring_buf[ring_head] = buf[i];
@@ -54,6 +50,23 @@ void pbdrv_uart_debug_printf(const char *format, ...) {
5450
pbio_os_request_poll();
5551
}
5652

53+
/**
54+
* Formats and stores a string in the UART debug ring buffer.
55+
*
56+
* This function works similarly to printf, but instead of printing to the
57+
* standard output. The formatted string will be written to the UART when the
58+
* buffer is processed.
59+
*
60+
* @param format The format string, similar to printf.
61+
* @param ... The variable arguments, similar to printf.
62+
*/
63+
void pbdrv_uart_debug_printf(const char *format, ...) {
64+
va_list args;
65+
va_start(args, format);
66+
pbdrv_uart_debug_vprintf(format, args);
67+
va_end(args);
68+
}
69+
5770
/**
5871
* Gets a character from the UART debug port.
5972
*
@@ -86,16 +99,14 @@ static pbio_error_t pbdrv_uart_debug_process_thread(pbio_os_state_t *state, void
8699
PBIO_OS_AWAIT_UNTIL(state, ring_head != ring_tail);
87100

88101
// Write up to the end of the buffer without wrapping.
89-
size_t end = ring_head > ring_tail ? ring_head: BUF_SIZE;
102+
size_t end = ring_head > ring_tail ? ring_head : BUF_SIZE;
90103
write_size = end - ring_tail;
91-
PBIO_OS_AWAIT(state, &child, pbdrv_uart_write(&child, debug_uart, &ring_buf[ring_tail], write_size, 100));
104+
PBIO_OS_AWAIT(state, &child, (err = pbdrv_uart_write(&child, debug_uart, &ring_buf[ring_tail], write_size, 100)));
92105
ring_tail = (ring_tail + write_size) % BUF_SIZE;
93106

94-
// Reset on failure.
95107
if (err != PBIO_SUCCESS) {
96-
ring_head = 0;
108+
ring_head = snprintf((char *)ring_buf, BUF_SIZE, "UART debug write error %d\n", err);
97109
ring_tail = 0;
98-
continue;
99110
}
100111

101112
// Poll to write again if not fully finished, i.e. when wrapping.

lib/pbio/drv/uart/uart_debug_first_port.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
#include <stdint.h>
88
#include <stdbool.h>
9+
#include <stdarg.h>
910

1011
#include <pbdrv/config.h>
1112

1213
#if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT
1314

1415
void pbdrv_uart_debug_printf(const char *format, ...);
16+
void pbdrv_uart_debug_vprintf(const char *format, va_list argptr);
1517

1618
int32_t pbdrv_uart_debug_get_char(void);
1719

0 commit comments

Comments
 (0)