Skip to content

Commit 49e17c8

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/machine_uart: Implement uart.flush() and uart.txdone().
uart.flush() flush() will wait until all characters have been sent.To avoid a permanent lock, a timeout applies depending on the size of txbuf and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. ret is False otherwise.
1 parent 5466f1b commit 49e17c8

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ports/mimxrt/machine_uart.c

+28
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,30 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) {
308308
}
309309
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak);
310310

311+
STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
312+
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
313+
314+
if (self->tx_status == kStatus_LPUART_TxIdle) {
315+
return mp_const_true;
316+
} else {
317+
return mp_const_false;
318+
}
319+
}
320+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
321+
311322
STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
312323
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) },
313324

314325
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) },
315326

327+
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
316328
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
317329
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
318330
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
319331
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
320332

321333
{ MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) },
334+
{ MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
322335

323336
{ MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERT_TX) },
324337
{ MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERT_RX) },
@@ -433,6 +446,21 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint
433446
if ((flags & MP_STREAM_POLL_WR)) {
434447
ret |= MP_STREAM_POLL_WR;
435448
}
449+
} else if (request == MP_STREAM_FLUSH) {
450+
// The timeout is estimated using the buffer size and the baudrate.
451+
// Take the worst case assumptions at 13 bit symbol size times 2.
452+
uint64_t timeout = (uint64_t)(3 + self->txbuf_len) * 13000000ll * 2 /
453+
self->config.baudRate_Bps + ticks_us64();
454+
455+
do {
456+
if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
457+
return 0;
458+
}
459+
MICROPY_EVENT_POLL_HOOK
460+
} while (ticks_us64() < timeout);
461+
462+
*errcode = MP_ETIMEDOUT;
463+
ret = MP_STREAM_ERROR;
436464
} else {
437465
*errcode = MP_EINVAL;
438466
ret = MP_STREAM_ERROR;

0 commit comments

Comments
 (0)