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; }