Skip to content

Commit b3c56e7

Browse files
authored
Add stderr support and remove 1us timeout for timeouts of 0us (raspberrypi#858)
1 parent 7858601 commit b3c56e7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/rp2_common/pico_stdio/stdio.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include "pico/stdio_semihosting.h"
3030
#endif
3131

32+
#define STDIO_HANDLE_STDIN 0
33+
#define STDIO_HANDLE_STDOUT 1
34+
#define STDIO_HANDLE_STDERR 2
35+
3236
static stdio_driver_t *drivers;
3337
static stdio_driver_t *filter;
3438

@@ -131,11 +135,13 @@ static int stdio_get_until(char *buf, int len, absolute_time_t until) {
131135
}
132136
}
133137
}
138+
if (time_reached(until)) {
139+
return PICO_ERROR_TIMEOUT;
140+
}
134141
// we sleep here in case the in_chars methods acquire mutexes or disable IRQs and
135142
// potentially starve out what they are waiting on (have seen this with USB)
136143
busy_wait_us(1);
137-
} while (!time_reached(until));
138-
return PICO_ERROR_TIMEOUT;
144+
} while (true);
139145
}
140146

141147
int WRAPPER_FUNC(putchar)(int c) {
@@ -165,14 +171,14 @@ int puts_raw(const char *s) {
165171
}
166172

167173
int _read(int handle, char *buffer, int length) {
168-
if (handle == 0) {
174+
if (handle == STDIO_HANDLE_STDIN) {
169175
return stdio_get_until(buffer, length, at_the_end_of_time);
170176
}
171177
return -1;
172178
}
173179

174180
int _write(int handle, char *buffer, int length) {
175-
if (handle == 1) {
181+
if (handle == STDIO_HANDLE_STDOUT || handle == STDIO_HANDLE_STDERR) {
176182
stdio_put_string(buffer, length, false, false);
177183
return length;
178184
}

0 commit comments

Comments
 (0)