summaryrefslogtreecommitdiff
path: root/ports/esp8266/machine_uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'ports/esp8266/machine_uart.c')
-rw-r--r--ports/esp8266/machine_uart.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c
index 4e88eee16..2fe6516dc 100644
--- a/ports/esp8266/machine_uart.c
+++ b/ports/esp8266/machine_uart.c
@@ -231,10 +231,20 @@ STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any);
+STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
+ pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
+
+ return uart_txdone(self->uart_id) == true ? mp_const_true : 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[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_uart_any_obj) },
+ { MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
+
+ { MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
@@ -305,6 +315,20 @@ 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) && uart_tx_any_room(self->uart_id)) {
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 = (uint64_t)(3 + 127) * 13000000ll * 2 / self->baudrate
+ + system_get_time();
+ do {
+ if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
+ return 0;
+ }
+ MICROPY_EVENT_POLL_HOOK
+ } while (system_get_time() < timeout);
+
+ *errcode = MP_ETIMEDOUT;
+ ret = MP_STREAM_ERROR;
} else {
*errcode = MP_EINVAL;
ret = MP_STREAM_ERROR;