Skip to content

Commit 2488311

Browse files
robert-hhdpgeorge
authored andcommitted
rp2/machine_uart: Implement uart.flush() and uart.txdone().
uart.flush() flush() will wait until all characters have been sent. It may return while the last character is sent. if needed, the calling code has to add one character wait time. 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. It may return True if the last byte of a transfer is sent. ret is False otherwise.
1 parent 53ebbf1 commit 2488311

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ports/rp2/machine_uart.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,31 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) {
435435
}
436436
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak);
437437

438+
STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
439+
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
440+
441+
if (ringbuf_avail(&self->write_buffer) == 0 &&
442+
uart_get_hw(self->uart)->fr & UART_UARTFR_TXFE_BITS) {
443+
return mp_const_true;
444+
} else {
445+
return mp_const_false;
446+
}
447+
}
448+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
449+
438450
STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
439451
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) },
440452
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) },
441453

442454
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) },
455+
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
443456
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
444457
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
445458
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
446459
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
447460

448461
{ MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) },
462+
{ MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
449463

450464
{ MP_ROM_QSTR(MP_QSTR_INV_TX), MP_ROM_INT(UART_INVERT_TX) },
451465
{ MP_ROM_QSTR(MP_QSTR_INV_RX), MP_ROM_INT(UART_INVERT_RX) },
@@ -538,6 +552,19 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint
538552
if ((flags & MP_STREAM_POLL_WR) && ringbuf_free(&self->write_buffer) > 0) {
539553
ret |= MP_STREAM_POLL_WR;
540554
}
555+
} else if (request == MP_STREAM_FLUSH) {
556+
// The timeout is estimated using the buffer size and the baudrate.
557+
// Take the worst case assumptions at 13 bit symbol size times 2.
558+
uint64_t timeout = time_us_64() +
559+
(uint64_t)(33 + self->write_buffer.size) * 13000000ll * 2 / self->baudrate;
560+
do {
561+
if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
562+
return 0;
563+
}
564+
MICROPY_EVENT_POLL_HOOK
565+
} while (time_us_64() < timeout);
566+
*errcode = MP_ETIMEDOUT;
567+
ret = MP_STREAM_ERROR;
541568
} else {
542569
*errcode = MP_EINVAL;
543570
ret = MP_STREAM_ERROR;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy