From 1064875346c04157333ecb0bbe138cb4eb34edd5 Mon Sep 17 00:00:00 2001 From: James Aguilar Date: Wed, 29 Oct 2025 17:03:51 -0600 Subject: [PATCH] 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. We also increase the buffer size because when doing higher volume logging the 256 bytes alloted before were sometimes not enough. --- lib/pbio/drv/uart/uart_debug_first_port.c | 41 ++++++++++++++--------- lib/pbio/drv/uart/uart_debug_first_port.h | 3 ++ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/pbio/drv/uart/uart_debug_first_port.c b/lib/pbio/drv/uart/uart_debug_first_port.c index 8fe6e774a..06fedd327 100644 --- a/lib/pbio/drv/uart/uart_debug_first_port.c +++ b/lib/pbio/drv/uart/uart_debug_first_port.c @@ -11,9 +11,10 @@ #include #include +#include #include -#define BUF_SIZE (256) +#define BUF_SIZE (1024) static uint8_t ring_buf[BUF_SIZE]; static size_t ring_head = 0; @@ -24,21 +25,16 @@ static pbdrv_uart_dev_t *debug_uart = NULL; /** * Formats and stores a string in the UART debug ring buffer. * - * This function works similarly to printf, but instead of printing to the + * This function works similarly to vprintf, but instead of printing to the * standard output. The formatted string will be written to the UART when the * buffer is processed. * * @param format The format string, similar to printf. - * @param ... The variable arguments, similar to printf. + * @param va_list The variable arguments, as a va_list. */ -void pbdrv_uart_debug_printf(const char *format, ...) { - +void pbdrv_uart_debug_vprintf(const char *format, va_list args) { char buf[BUF_SIZE]; - va_list args; - va_start(args, format); - vsnprintf(buf, sizeof(ring_buf), format, args); - va_end(args); - + vsnprintf(buf, sizeof(buf), format, args); size_t len = strlen(buf); for (size_t i = 0; i < len; i++) { ring_buf[ring_head] = buf[i]; @@ -54,6 +50,23 @@ void pbdrv_uart_debug_printf(const char *format, ...) { pbio_os_request_poll(); } +/** + * Formats and stores a string in the UART debug ring buffer. + * + * This function works similarly to printf, but instead of printing to the + * standard output. The formatted string will be written to the UART when the + * buffer is processed. + * + * @param format The format string, similar to printf. + * @param ... The variable arguments, similar to printf. + */ +void pbdrv_uart_debug_printf(const char *format, ...) { + va_list args; + va_start(args, format); + pbdrv_uart_debug_vprintf(format, args); + va_end(args); +} + /** * Gets a character from the UART debug port. * @@ -86,16 +99,14 @@ static pbio_error_t pbdrv_uart_debug_process_thread(pbio_os_state_t *state, void PBIO_OS_AWAIT_UNTIL(state, ring_head != ring_tail); // Write up to the end of the buffer without wrapping. - size_t end = ring_head > ring_tail ? ring_head: BUF_SIZE; + size_t end = ring_head > ring_tail ? ring_head : BUF_SIZE; write_size = end - ring_tail; - PBIO_OS_AWAIT(state, &child, pbdrv_uart_write(&child, debug_uart, &ring_buf[ring_tail], write_size, 100)); + PBIO_OS_AWAIT(state, &child, (err = pbdrv_uart_write(&child, debug_uart, &ring_buf[ring_tail], write_size, 100))); ring_tail = (ring_tail + write_size) % BUF_SIZE; - // Reset on failure. if (err != PBIO_SUCCESS) { - ring_head = 0; + ring_head = snprintf((char *)ring_buf, BUF_SIZE, "UART debug write error %d\n", err); ring_tail = 0; - continue; } // Poll to write again if not fully finished, i.e. when wrapping. diff --git a/lib/pbio/drv/uart/uart_debug_first_port.h b/lib/pbio/drv/uart/uart_debug_first_port.h index e4ae4fbc2..ea8541dc7 100644 --- a/lib/pbio/drv/uart/uart_debug_first_port.h +++ b/lib/pbio/drv/uart/uart_debug_first_port.h @@ -6,12 +6,14 @@ #include #include +#include #include #if PBDRV_CONFIG_UART_DEBUG_FIRST_PORT void pbdrv_uart_debug_printf(const char *format, ...); +void pbdrv_uart_debug_vprintf(const char *format, va_list argptr); int32_t pbdrv_uart_debug_get_char(void); @@ -22,6 +24,7 @@ void pbdrv_uart_debug_init(void); #else // PBDRV_CONFIG_UART_DEBUG_FIRST_PORT #define pbdrv_uart_debug_printf(...) +#define pbdrv_uart_debug_vprintf(format, argptr) #define pbdrv_uart_debug_get_char() (-1)