summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrobert-hh <robert@hammelrath.com>2022-08-26 18:08:29 +0200
committerDamien George <damien@micropython.org>2022-08-31 00:18:49 +1000
commita39b88f0fbffe802dc8a80980e6fd675c9ef5a65 (patch)
tree4c74a9c009620259fc9bd1906ac74df714ed1442
parentcc0249c936091889c9f2bda0ecafc522e06364a3 (diff)
cc3200/mods/pybuart: 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 FIFO and the baud rate. ret = uart.txdone() ret is True if no transfer is in progress. ret is False otherwise.
-rw-r--r--ports/cc3200/mods/pybuart.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/ports/cc3200/mods/pybuart.c b/ports/cc3200/mods/pybuart.c
index 6e774b70d..a81954abb 100644
--- a/ports/cc3200/mods/pybuart.c
+++ b/ports/cc3200/mods/pybuart.c
@@ -66,6 +66,7 @@
#define PYBUART_TX_MAX_TIMEOUT_MS (5)
#define PYBUART_RX_BUFFER_LEN (256)
+#define PYBUART_TX_BUFFER_LEN (17)
// interrupt triggers
#define UART_TRIGGER_RX_ANY (0x01)
@@ -558,6 +559,17 @@ invalid_args:
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_irq_obj, 1, pyb_uart_irq);
+STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
+ pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ if (MAP_UARTBusy(self->reg) == false) {
+ return mp_const_true;
+ } else {
+ return mp_const_false;
+ }
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
+
STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
// instance methods
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) },
@@ -565,6 +577,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_uart_any_obj) },
{ MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&pyb_uart_sendbreak_obj) },
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pyb_uart_irq_obj) },
+ { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
/// \method read([nbytes])
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
@@ -574,6 +587,7 @@ STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
/// \method write(buf)
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
+ { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
// class constants
{ MP_ROM_QSTR(MP_QSTR_RX_ANY), MP_ROM_INT(UART_TRIGGER_RX_ANY) },
@@ -635,6 +649,21 @@ STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t a
if ((flags & MP_STREAM_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) {
ret |= MP_STREAM_POLL_WR;
}
+ } else if (request == MP_STREAM_FLUSH) {
+ // The timeout is estimated using the buffer size and the baudrate.
+ // Take the worst case assumptions at 13 bit symbol size times 2.
+ uint64_t timeout = mp_hal_ticks_ms() +
+ (PYBUART_TX_BUFFER_LEN) * 13000 * 2 / self->baudrate;
+
+ do {
+ if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
+ return 0;
+ }
+ MICROPY_EVENT_POLL_HOOK
+ } while (mp_hal_ticks_ms() < timeout);
+
+ *errcode = MP_ETIMEDOUT;
+ ret = MP_STREAM_ERROR;;
} else {
*errcode = MP_EINVAL;
ret = MP_STREAM_ERROR;