diff options
| author | Damien George <damien@micropython.org> | 2025-04-22 11:52:35 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-05-21 13:14:26 +1000 |
| commit | e676b58d9fae4c4ebebfe0ea7fb787317dafba38 (patch) | |
| tree | 52193477581ef72c02f5612f360c26e338be6b7e | |
| parent | 32c65ad4552d7e7d88c06e94fd903dcea5080c5f (diff) | |
nrf: Fix UART write on parts that can't write more than 255 bytes.
Some MCUs cannot write more than 255 bytes to the UART at once. Eg writing
256 bytes gets truncated to 0, writing 257 gets truncated to 1, etc.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | ports/nrf/modules/machine/uart.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 45c79cda1..4c75bf820 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -46,6 +46,11 @@ #include "nrfx_uarte.h" #endif +#if defined(NRF52832) +// The nRF52832 cannot write more than 255 bytes at a time. +#define UART_MAX_TX_CHUNK (255) +#endif + typedef struct _machine_uart_buf_t { uint8_t tx_buf[1]; uint8_t rx_buf[1]; @@ -456,17 +461,29 @@ static mp_uint_t mp_machine_uart_write(mp_obj_t self_in, const void *buf, mp_uin #endif machine_uart_obj_t *self = self_in; - nrfx_err_t err = nrfx_uart_tx(self->p_uart, buf, size); - if (err == NRFX_SUCCESS) { + + // Send data out, in chunks if needed. + mp_uint_t remaining = size; + while (remaining) { + #ifdef UART_MAX_TX_CHUNK + mp_uint_t chunk = MIN(UART_MAX_TX_CHUNK, remaining); + #else + mp_uint_t chunk = remaining; + #endif + nrfx_err_t err = nrfx_uart_tx(self->p_uart, buf, chunk); + if (err != NRFX_SUCCESS) { + *errcode = mp_hal_status_to_errno_table[err]; + return MP_STREAM_ERROR; + } while (nrfx_uart_tx_in_progress(self->p_uart)) { MICROPY_EVENT_POLL_HOOK; } - // return number of bytes written - return size; - } else { - *errcode = mp_hal_status_to_errno_table[err]; - return MP_STREAM_ERROR; + buf += chunk; + remaining -= chunk; } + + // return number of bytes written + return size; } static mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
