Skip to content

Commit e676b58

Browse files
committed
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 <[email protected]>
1 parent 32c65ad commit e676b58

File tree

1 file changed

+24
-7
lines changed
  • ports/nrf/modules/machine

1 file changed

+24
-7
lines changed

ports/nrf/modules/machine/uart.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
#include "nrfx_uarte.h"
4747
#endif
4848

49+
#if defined(NRF52832)
50+
// The nRF52832 cannot write more than 255 bytes at a time.
51+
#define UART_MAX_TX_CHUNK (255)
52+
#endif
53+
4954
typedef struct _machine_uart_buf_t {
5055
uint8_t tx_buf[1];
5156
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
456461
#endif
457462

458463
machine_uart_obj_t *self = self_in;
459-
nrfx_err_t err = nrfx_uart_tx(self->p_uart, buf, size);
460-
if (err == NRFX_SUCCESS) {
464+
465+
// Send data out, in chunks if needed.
466+
mp_uint_t remaining = size;
467+
while (remaining) {
468+
#ifdef UART_MAX_TX_CHUNK
469+
mp_uint_t chunk = MIN(UART_MAX_TX_CHUNK, remaining);
470+
#else
471+
mp_uint_t chunk = remaining;
472+
#endif
473+
nrfx_err_t err = nrfx_uart_tx(self->p_uart, buf, chunk);
474+
if (err != NRFX_SUCCESS) {
475+
*errcode = mp_hal_status_to_errno_table[err];
476+
return MP_STREAM_ERROR;
477+
}
461478
while (nrfx_uart_tx_in_progress(self->p_uart)) {
462479
MICROPY_EVENT_POLL_HOOK;
463480
}
464-
// return number of bytes written
465-
return size;
466-
} else {
467-
*errcode = mp_hal_status_to_errno_table[err];
468-
return MP_STREAM_ERROR;
481+
buf += chunk;
482+
remaining -= chunk;
469483
}
484+
485+
// return number of bytes written
486+
return size;
470487
}
471488

472489
static mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {

0 commit comments

Comments
 (0)