diff options
author | Damien George <damien.p.george@gmail.com> | 2016-04-21 15:19:00 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-04-21 15:19:00 +0100 |
commit | 7652ab77efdd7ba4d5a49d6db006cdbd73dfc3b7 (patch) | |
tree | e6240b37773fe5d31bc1c70ee3aa0e16074f8cff /esp8266/uart.c | |
parent | 495da1561149d63b6caa061517c7dee7df2c1929 (diff) | |
download | micropython-7652ab77efdd7ba4d5a49d6db006cdbd73dfc3b7.tar.gz micropython-7652ab77efdd7ba4d5a49d6db006cdbd73dfc3b7.zip |
esp8266: Add uart_rx_wait and uart_rx_char functions.
Diffstat (limited to 'esp8266/uart.c')
-rw-r--r-- | esp8266/uart.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/esp8266/uart.c b/esp8266/uart.c index f371935399..3207029fd2 100644 --- a/esp8266/uart.c +++ b/esp8266/uart.c @@ -222,6 +222,26 @@ int uart0_rx(void) { } */ +// Waits at most timeout microseconds for at least 1 char to become ready for reading. +// Returns true if something available, false if not. +bool uart_rx_wait(uint32_t timeout_us) { + uint32_t start = system_get_time(); + for (;;) { + if (input_buf.iget != input_buf.iput) { + return true; // have at least 1 char ready for reading + } + if (system_get_time() - start >= timeout_us) { + return false; // timeout + } + ets_event_poll(); + } +} + +// Returns char from the input buffer, else -1 if buffer is empty. +int uart_rx_char(void) { + return ringbuf_get(&input_buf); +} + int uart_rx_one_char(uint8 uart_no) { if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; |