From fdb36c9927789c4f4c993ba57e78504ae2fcb736 Mon Sep 17 00:00:00 2001 From: Stuart Longland Date: Thu, 9 Oct 2025 12:34:44 +1000 Subject: [PATCH 1/2] shell_uart: Stop polling if buffer is full Before calling `uart_poll_in`, check we have space to store the character in the ring buffer beforehand. If we do, *then* poll for the character. That way we don't miss out on serial traffic when our ring buffer is full unless we fill our hardware ring buffer too. Signed-off-by: Stuart Longland --- subsys/shell/backends/shell_uart.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/subsys/shell/backends/shell_uart.c b/subsys/shell/backends/shell_uart.c index 1d19ce19a3181..872cd83966301 100644 --- a/subsys/shell/backends/shell_uart.c +++ b/subsys/shell/backends/shell_uart.c @@ -265,7 +265,8 @@ static void polling_rx_timeout_handler(struct k_timer *timer) uint8_t c; struct shell_uart_polling *sh_uart = k_timer_user_data_get(timer); - while (uart_poll_in(sh_uart->common.dev, &c) == 0) { + while ((ring_buf_space_get(&sh_uart->rx_ringbuf) > 0) && + (uart_poll_in(sh_uart->common.dev, &c) == 0)) { if (ring_buf_put(&sh_uart->rx_ringbuf, &c, 1) == 0U) { /* ring buffer full. */ LOG_WRN("RX ring buffer full."); From c14fdd58bc5b45910963cfa3ce81fe47540687a2 Mon Sep 17 00:00:00 2001 From: Stuart Longland Date: Fri, 10 Oct 2025 20:57:13 +1000 Subject: [PATCH 2/2] shell_uart: Drop superfluous `if` statement We're now checking if there is space prior to executing the loop body, therefore guaranteeing this condition is never going to fire in a single-threaded context. Signed-off-by: Stuart Longland --- subsys/shell/backends/shell_uart.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/subsys/shell/backends/shell_uart.c b/subsys/shell/backends/shell_uart.c index 872cd83966301..2dc8bd120b610 100644 --- a/subsys/shell/backends/shell_uart.c +++ b/subsys/shell/backends/shell_uart.c @@ -267,10 +267,7 @@ static void polling_rx_timeout_handler(struct k_timer *timer) while ((ring_buf_space_get(&sh_uart->rx_ringbuf) > 0) && (uart_poll_in(sh_uart->common.dev, &c) == 0)) { - if (ring_buf_put(&sh_uart->rx_ringbuf, &c, 1) == 0U) { - /* ring buffer full. */ - LOG_WRN("RX ring buffer full."); - } + ring_buf_put(&sh_uart->rx_ringbuf, &c, 1); sh_uart->common.handler(SHELL_TRANSPORT_EVT_RX_RDY, sh_uart->common.context); } }