From b3c56e716909f650d571cbb83ae78bc2e430fbf8 Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Mon, 20 Jun 2022 09:52:06 -0500 Subject: [PATCH] Add stderr support and remove 1us timeout for timeouts of 0us (#858) --- src/rp2_common/pico_stdio/stdio.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/rp2_common/pico_stdio/stdio.c b/src/rp2_common/pico_stdio/stdio.c index e2eb5ff..52a1c0c 100644 --- a/src/rp2_common/pico_stdio/stdio.c +++ b/src/rp2_common/pico_stdio/stdio.c @@ -29,6 +29,10 @@ #include "pico/stdio_semihosting.h" #endif +#define STDIO_HANDLE_STDIN 0 +#define STDIO_HANDLE_STDOUT 1 +#define STDIO_HANDLE_STDERR 2 + static stdio_driver_t *drivers; static stdio_driver_t *filter; @@ -131,11 +135,13 @@ static int stdio_get_until(char *buf, int len, absolute_time_t until) { } } } + if (time_reached(until)) { + return PICO_ERROR_TIMEOUT; + } // 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; + } while (true); } int WRAPPER_FUNC(putchar)(int c) { @@ -165,14 +171,14 @@ int puts_raw(const char *s) { } int _read(int handle, char *buffer, int length) { - if (handle == 0) { + if (handle == STDIO_HANDLE_STDIN) { return stdio_get_until(buffer, length, at_the_end_of_time); } return -1; } int _write(int handle, char *buffer, int length) { - if (handle == 1) { + if (handle == STDIO_HANDLE_STDOUT || handle == STDIO_HANDLE_STDERR) { stdio_put_string(buffer, length, false, false); return length; }