From 929ede74823a10916279d15eba29ff915b52ce52 Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Tue, 4 May 2021 08:01:34 -0500 Subject: [PATCH] add small delay to stdio_get_until to prevent starvation of USB IRQ handler due to in use mutex. build was non deterministic due to missing link wrapping of getchar (#364) --- src/rp2_common/pico_stdio/CMakeLists.txt | 1 + src/rp2_common/pico_stdio/stdio.c | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/rp2_common/pico_stdio/CMakeLists.txt b/src/rp2_common/pico_stdio/CMakeLists.txt index d7935cc..cdc9c3b 100644 --- a/src/rp2_common/pico_stdio/CMakeLists.txt +++ b/src/rp2_common/pico_stdio/CMakeLists.txt @@ -11,6 +11,7 @@ if (NOT TARGET pico_stdio) pico_wrap_function(pico_stdio vprintf) pico_wrap_function(pico_stdio puts) pico_wrap_function(pico_stdio putchar) + pico_wrap_function(pico_stdio getchar) if (TARGET pico_printf) target_link_libraries(pico_stdio INTERFACE pico_printf) diff --git a/src/rp2_common/pico_stdio/stdio.c b/src/rp2_common/pico_stdio/stdio.c index b237ca0..5aaf1a8 100644 --- a/src/rp2_common/pico_stdio/stdio.c +++ b/src/rp2_common/pico_stdio/stdio.c @@ -123,7 +123,9 @@ static int stdio_get_until(char *buf, int len, absolute_time_t until) { } } } - // todo maybe a little sleep here? + // we sleep here in case the in_chars methods acquire mutexes or disable IRQs and + // potentially starve out what they are waiting on (have seen this with USB) + busy_wait_us(1); } while (!time_reached(until)); return PICO_ERROR_TIMEOUT; } @@ -257,9 +259,9 @@ void stdio_init_all() { int WRAPPER_FUNC(getchar)(void) { char buf[1]; - if (0 == stdio_get_until(buf, sizeof(buf), at_the_end_of_time)) { - return PICO_ERROR_TIMEOUT; - } + int len = stdio_get_until(buf, 1, at_the_end_of_time); + if (len < 0) return len; + assert(len == 1); return (uint8_t)buf[0]; }