esp8266: Add uart_rx_wait and uart_rx_char functions.

This commit is contained in:
Damien George 2016-04-21 15:19:00 +01:00
parent 495da15611
commit 7652ab77ef
2 changed files with 22 additions and 0 deletions

View File

@ -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) { int uart_rx_one_char(uint8 uart_no) {
if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) { if (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff; return READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;

View File

@ -91,6 +91,8 @@ typedef struct {
void uart_init(UartBautRate uart0_br, UartBautRate uart1_br); void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
int uart0_rx(void); int uart0_rx(void);
bool uart_rx_wait(uint32_t timeout_us);
int uart_rx_char(void);
void uart_tx_one_char(uint8 uart, uint8 TxChar); void uart_tx_one_char(uint8 uart, uint8 TxChar);
void uart_flush(uint8 uart); void uart_flush(uint8 uart);
void uart_os_config(int uart); void uart_os_config(int uart);