Skip to content

Commit

Permalink
ports: Implement simple write polling for stdout.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dpgeorge committed Mar 23, 2023
1 parent 6c76248 commit 38e7b84
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
ret |= MP_STREAM_POLL_RD;
}
if (poll_flags & MP_STREAM_POLL_WR) {
ret |= MP_STREAM_POLL_WR;
}
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions ports/esp8266/esp_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if ((poll_flags & MP_STREAM_POLL_RD) && stdin_ringbuf.iget != stdin_ringbuf.iput) {
ret |= MP_STREAM_POLL_RD;
}
if (poll_flags & MP_STREAM_POLL_WR) {
ret |= mp_uos_dupterm_poll(poll_flags);
}
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions ports/mimxrt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
ret |= MP_STREAM_POLL_WR;
}
#if MICROPY_PY_OS_DUPTERM
ret |= mp_uos_dupterm_poll(poll_flags);
#endif
Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/drivers/bluetooth/ble_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
&& !isBufferEmpty(mp_rx_ring_buffer)) {
ret |= MP_STREAM_POLL_RD;
}
if ((poll_flags & MP_STREAM_POLL_WR) && ble_uart_enabled()) {
ret |= MP_STREAM_POLL_WR;
}
return ret;
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/drivers/usb/usb_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
ret |= MP_STREAM_POLL_RD;
}
}
if (poll_flags & MP_STREAM_POLL_WR) {
ret |= MP_STREAM_POLL_WR;
}
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
&& uart_rx_any(MP_STATE_PORT(board_stdio_uart))) {
ret |= MP_STREAM_POLL_RD;
}
if ((poll_flags & MP_STREAM_POLL_WR) && MP_STATE_PORT(board_stdio_uart) != NULL) {
ret |= MP_STREAM_POLL_WR;
}
return ret;
}

Expand Down
3 changes: 3 additions & 0 deletions ports/pic16bit/pic16bit_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if ((poll_flags & MP_STREAM_POLL_RD) && uart_rx_any()) {
ret |= MP_STREAM_POLL_RD;
}
if (poll_flags & MP_STREAM_POLL_WR) {
ret |= MP_STREAM_POLL_WR;
}
return ret;
}

Expand Down
9 changes: 9 additions & 0 deletions ports/rp2/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
ret |= MP_STREAM_POLL_RD;
}
if (poll_flags & MP_STREAM_POLL_WR) {
#if MICROPY_HW_ENABLE_UART_REPL
ret |= MP_STREAM_POLL_WR;
#else
if (tud_cdc_connected() && tud_cdc_write_available() > 0) {
ret |= MP_STREAM_POLL_WR;
}
#endif
}
#endif
#if MICROPY_PY_OS_DUPTERM
ret |= mp_uos_dupterm_poll(poll_flags);
Expand Down
4 changes: 4 additions & 0 deletions ports/samd/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
ret |= MP_STREAM_POLL_RD;
}

if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
ret |= MP_STREAM_POLL_WR;
}

#if MICROPY_PY_OS_DUPTERM
ret |= mp_uos_dupterm_poll(poll_flags);
#endif
Expand Down
5 changes: 5 additions & 0 deletions ports/teensy/teensy_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
ret |= MP_STREAM_POLL_RD;
}
}
if (poll_flags & MP_STREAM_POLL_WR) {
if (MP_STATE_PORT(pyb_stdio_uart) != NULL || usb_vcp_is_enabled()) {
ret |= MP_STREAM_POLL_WR;
}
}
return ret;
}

Expand Down

0 comments on commit 38e7b84

Please sign in to comment.