Skip to content

Commit 38e7b84

Browse files
committed
ports: Implement simple write polling for stdout.
This is a best-effort implementation of write polling. It's difficult to do correctly because if there are multiple output streams (eg UART and USB CDC) then some may not be writeable while others are. A full solution should also have a return value from mp_hal_stdout_tx_strn(), returning the number of bytes written to the stream(s). That's also hard to define. The renesas-ra and stm32 ports already implement a similar best-effort mechanism for write polling. Fixes issue micropython#11026. Signed-off-by: Damien George <[email protected]>
1 parent 6c76248 commit 38e7b84

File tree

10 files changed

+39
-0
lines changed

10 files changed

+39
-0
lines changed

Diff for: ports/esp32/mphalport.c

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
8888
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
8989
ret |= MP_STREAM_POLL_RD;
9090
}
91+
if (poll_flags & MP_STREAM_POLL_WR) {
92+
ret |= MP_STREAM_POLL_WR;
93+
}
9194
return ret;
9295
}
9396

Diff for: ports/esp8266/esp_mphal.c

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
6262
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
6363
ret |= MP_STREAM_POLL_RD;
6464
}
65+
if (poll_flags & MP_STREAM_POLL_WR) {
66+
ret |= mp_uos_dupterm_poll(poll_flags);
67+
}
6568
return ret;
6669
}
6770

Diff for: ports/mimxrt/mphalport.c

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
8686
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
8787
ret |= MP_STREAM_POLL_RD;
8888
}
89+
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
90+
ret |= MP_STREAM_POLL_WR;
91+
}
8992
#if MICROPY_PY_OS_DUPTERM
9093
ret |= mp_uos_dupterm_poll(poll_flags);
9194
#endif

Diff for: ports/nrf/drivers/bluetooth/ble_uart.c

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
164164
&& !isBufferEmpty(mp_rx_ring_buffer)) {
165165
ret |= MP_STREAM_POLL_RD;
166166
}
167+
if ((poll_flags & MP_STREAM_POLL_WR) && ble_uart_enabled()) {
168+
ret |= MP_STREAM_POLL_WR;
169+
}
167170
return ret;
168171
}
169172
#endif

Diff for: ports/nrf/drivers/usb/usb_cdc.c

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
213213
ret |= MP_STREAM_POLL_RD;
214214
}
215215
}
216+
if (poll_flags & MP_STREAM_POLL_WR) {
217+
ret |= MP_STREAM_POLL_WR;
218+
}
216219
return ret;
217220
}
218221

Diff for: ports/nrf/mphalport.c

+3
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
179179
&& uart_rx_any(MP_STATE_PORT(board_stdio_uart))) {
180180
ret |= MP_STREAM_POLL_RD;
181181
}
182+
if ((poll_flags & MP_STREAM_POLL_WR) && MP_STATE_PORT(board_stdio_uart) != NULL) {
183+
ret |= MP_STREAM_POLL_WR;
184+
}
182185
return ret;
183186
}
184187

Diff for: ports/pic16bit/pic16bit_mphal.c

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
5757
if ((poll_flags & MP_STREAM_POLL_RD) && uart_rx_any()) {
5858
ret |= MP_STREAM_POLL_RD;
5959
}
60+
if (poll_flags & MP_STREAM_POLL_WR) {
61+
ret |= MP_STREAM_POLL_WR;
62+
}
6063
return ret;
6164
}
6265

Diff for: ports/rp2/mphalport.c

+9
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
9898
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
9999
ret |= MP_STREAM_POLL_RD;
100100
}
101+
if (poll_flags & MP_STREAM_POLL_WR) {
102+
#if MICROPY_HW_ENABLE_UART_REPL
103+
ret |= MP_STREAM_POLL_WR;
104+
#else
105+
if (tud_cdc_connected() && tud_cdc_write_available() > 0) {
106+
ret |= MP_STREAM_POLL_WR;
107+
}
108+
#endif
109+
}
101110
#endif
102111
#if MICROPY_PY_OS_DUPTERM
103112
ret |= mp_uos_dupterm_poll(poll_flags);

Diff for: ports/samd/mphalport.c

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
162162
ret |= MP_STREAM_POLL_RD;
163163
}
164164

165+
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
166+
ret |= MP_STREAM_POLL_WR;
167+
}
168+
165169
#if MICROPY_PY_OS_DUPTERM
166170
ret |= mp_uos_dupterm_poll(poll_flags);
167171
#endif

Diff for: ports/teensy/teensy_hal.c

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
3232
ret |= MP_STREAM_POLL_RD;
3333
}
3434
}
35+
if (poll_flags & MP_STREAM_POLL_WR) {
36+
if (MP_STATE_PORT(pyb_stdio_uart) != NULL || usb_vcp_is_enabled()) {
37+
ret |= MP_STREAM_POLL_WR;
38+
}
39+
}
3540
return ret;
3641
}
3742

0 commit comments

Comments
 (0)