10000 cc3200/mods/pybuart: Implement uart.flush() and uart.txdone(). · lowfatcode/micropython@a39b88f · GitHub
[go: up one dir, main page]

Skip to content

Commit a39b88f

Browse files
robert-hhdpgeorge
authored andcommitted
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.
1 parent cc0249c commit a39b88f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ports/cc3200/mods/pybuart.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#define PYBUART_TX_MAX_TIMEOUT_MS (5)
6767

6868
#define PYBUART_RX_BUFFER_LEN (256)
69+
#define PYBUART_TX_BUFFER_LEN (17)
6970

7071
// interrupt triggers
7172
#define UART_TRIGGER_RX_ANY (0x01)
@@ -558,13 +559,25 @@ STATIC mp_obj_t pyb_uart_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
558559
}
559560
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_irq_obj, 1, pyb_uart_irq);
560561

562+
STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
563+
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
564+
565+
if (MAP_UARTBusy(self->reg) == false) {
566+
return mp_const_true;
567+
} else {
568+
return mp_const_false;
569+
}
570+
}
571+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
572+
561573
STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = {
562574
// instance methods
563575
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) },
564576
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_uart_deinit_obj) },
565577
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&pyb_uart_any_obj) },
566578
{ MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&pyb_uart_sendbreak_obj) },
567579
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&pyb_uart_irq_obj) },
580+
{ MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
568581

569582
/// \method read([nbytes])
570583
{ 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[] = {
574587
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
575588
/// \method write(buf)
576589
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
590+
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
577591

578592
// class constants
579593
{ 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
635649
if ((flags & MP_STREAM_POLL_WR) && MAP_UARTSpaceAvail(self->reg)) {
636650
ret |= MP_STREAM_POLL_WR;
637651
}
652+
} else if (request == MP_STREAM_FLUSH) {
653+
// The timeout is estimated using the buffer size and the baudrate.
654+
// Take the worst case assumptions at 13 bit symbol size times 2.
655+
uint64_t timeout = mp_hal_ticks_ms() +
656+
(PYBUART_TX_BUFFER_LEN) * 13000 * 2 / self->baudrate;
657+
658+
do {
659+
if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
660+
return 0;
661+
}
662+
MICROPY_EVENT_POLL_HOOK
663+
} while (mp_hal_ticks_ms() < timeout);
664+
665+
*errcode = MP_ETIMEDOUT;
666+
ret = MP_STREAM_ERROR;;
638667
} else {
639668
*errcode = MP_EINVAL;
640669
ret = MP_STREAM_ERROR;

0 commit comments

Comments
 (0)
0