Skip to content

Commit 91eb015

Browse files
mhoffmadpgeorge
authored andcommitted
esp8266/uart: Add support for polling uart device.
1 parent 90ab191 commit 91eb015

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

esp8266/machine_uart.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,22 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
255255
}
256256

257257
STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
258-
*errcode = MP_EINVAL;
259-
return MP_STREAM_ERROR;
258+
pyb_uart_obj_t *self = self_in;
259+
mp_uint_t ret;
260+
if (request == MP_STREAM_POLL) {
261+
mp_uint_t flags = arg;
262+
ret = 0;
263+
if ((flags & MP_STREAM_POLL_RD) && uart_rx_any(self->uart_id)) {
264+
ret |= MP_STREAM_POLL_RD;
265+
}
266+
if ((flags & MP_STREAM_POLL_WR) && uart_tx_any_room(self->uart_id)) {
267+
ret |= MP_STREAM_POLL_WR;
268+
}
269+
} else {
270+
*errcode = MP_EINVAL;
271+
ret = MP_STREAM_ERROR;
272+
}
273+
return ret;
260274
}
261275

262276
STATIC const mp_stream_p_t uart_stream_p = {

esp8266/uart.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ bool uart_rx_wait(uint32_t timeout_us) {
200200
}
201201
}
202202

203+
int uart_rx_any(uint8 uart) {
204+
if (input_buf.iget != input_buf.iput) {
205+
return true; // have at least 1 char ready for reading
206+
}
207+
return false;
208+
}
209+
210+
int uart_tx_any_room(uint8 uart) {
211+
uint32_t fifo_cnt = READ_PERI_REG(UART_STATUS(uart)) & (UART_TXFIFO_CNT << UART_TXFIFO_CNT_S);
212+
if ((fifo_cnt >> UART_TXFIFO_CNT_S & UART_TXFIFO_CNT) >= 126) {
213+
return false;
214+
}
215+
return true;
216+
}
217+
203218
// Returns char from the input buffer, else -1 if buffer is empty.
204219
int uart_rx_char(void) {
205220
return ringbuf_get(&input_buf);

esp8266/uart.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,8 @@ void uart_tx_one_char(uint8 uart, uint8 TxChar);
9999
void uart_flush(uint8 uart);
100100
void uart_os_config(int uart);
101101
void uart_setup(uint8 uart);
102+
// check status of rx/tx
103+
int uart_rx_any(uint8 uart);
104+
int uart_tx_any_room(uint8 uart);
102105

103106
#endif // _INCLUDED_UART_H_

0 commit comments

Comments
 (0)