Skip to content

Commit 7652ab7

Browse files
committed
esp8266: Add uart_rx_wait and uart_rx_char functions.
1 parent 495da15 commit 7652ab7

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

esp8266/uart.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,26 @@ int uart0_rx(void) {
222222
}
223223
*/
224224

225+
// Waits at most timeout microseconds for at least 1 char to become ready for reading.
226+
// Returns true if something available, false if not.
227+
bool uart_rx_wait(uint32_t timeout_us) {
228+
uint32_t start = system_get_time();
229+
for (;;) {
230+
if (input_buf.iget != input_buf.iput) {
231+
return true; // have at least 1 char ready for reading
232+
}
233+
if (system_get_time() - start >= timeout_us) {
234+
return false; // timeout
235+
}
236+
ets_event_poll();
237+
}
238+
}
239+
240+
// Returns char from the input buffer, else -1 if buffer is empty.
241+
int uart_rx_char(void) {
242+
return ringbuf_get(&input_buf);
243+
}
244+
225245
int uart_rx_one_char(uint8 uart_no) {
226246
if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
227247
return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;

esp8266/uart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ typedef struct {
9191

9292
void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
9393
int uart0_rx(void);
94+
bool uart_rx_wait(uint32_t timeout_us);
95+
int uart_rx_char(void);
9496
void uart_tx_one_char(uint8 uart, uint8 TxChar);
9597
void uart_flush(uint8 uart);
9698
void uart_os_config(int uart);

0 commit comments

Comments
 (0)