diff options
author | Ryan Shaw <ryannathans@hotmail.com> | 2015-11-28 02:31:59 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-11-30 13:07:20 +0000 |
commit | f99491cbf743d51fd2aa7332c0e03da64448d60a (patch) | |
tree | 743473e4b62c3229d5fba0b2a284043bcef9ccd0 | |
parent | bd33aa313e03a9b2f07178d5a22d9891598c309f (diff) |
stmhal: uart.any() function now returns number of bytes available.
-rw-r--r-- | stmhal/uart.c | 18 | ||||
-rw-r--r-- | stmhal/uart.h | 2 | ||||
-rw-r--r-- | teensy/uart.c | 4 | ||||
-rw-r--r-- | tests/pyb/uart.py.exp | 2 |
4 files changed, 14 insertions, 12 deletions
diff --git a/stmhal/uart.c b/stmhal/uart.c index 374545163..8b72327c2 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -282,9 +282,15 @@ bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate) { } */ -bool uart_rx_any(pyb_uart_obj_t *self) { - return self->read_buf_tail != self->read_buf_head - || __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET; +mp_uint_t uart_rx_any(pyb_uart_obj_t *self) { + int buffer_bytes = self->read_buf_head - self->read_buf_tail; + if (buffer_bytes < 0) { + return buffer_bytes + self->read_buf_len; + } else if (buffer_bytes > 0) { + return buffer_bytes; + } else { + return __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET; + } } // Waits at most timeout milliseconds for at least 1 char to become ready for @@ -670,11 +676,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_deinit_obj, pyb_uart_deinit); /// Return `True` if any characters waiting, else `False`. STATIC mp_obj_t pyb_uart_any(mp_obj_t self_in) { pyb_uart_obj_t *self = self_in; - if (uart_rx_any(self)) { - return mp_const_true; - } else { - return mp_const_false; - } + return MP_OBJ_NEW_SMALL_INT(uart_rx_any(self)); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_uart_any_obj, pyb_uart_any); diff --git a/stmhal/uart.h b/stmhal/uart.h index 5b223c5c6..a859f722b 100644 --- a/stmhal/uart.h +++ b/stmhal/uart.h @@ -41,7 +41,7 @@ void uart_init0(void); void uart_deinit(void); void uart_irq_handler(mp_uint_t uart_id); -bool uart_rx_any(pyb_uart_obj_t *uart_obj); +mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj); int uart_rx_char(pyb_uart_obj_t *uart_obj); void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len); void uart_tx_strn_cooked(pyb_uart_obj_t *uart_obj, const char *str, uint len); diff --git a/teensy/uart.c b/teensy/uart.c index 81035aed6..8b73d733f 100644 --- a/teensy/uart.c +++ b/teensy/uart.c @@ -172,11 +172,11 @@ bool uart_init(pyb_uart_obj_t *uart_obj, uint32_t baudrate) { return uart_init2(uart_obj); } -bool uart_rx_any(pyb_uart_obj_t *uart_obj) { +mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj) { #if 0 return __HAL_UART_GET_FLAG(&uart_obj->uart, UART_FLAG_RXNE); #else - return false; + return 0; #endif } diff --git a/tests/pyb/uart.py.exp b/tests/pyb/uart.py.exp index 40170eea9..ea300c90c 100644 --- a/tests/pyb/uart.py.exp +++ b/tests/pyb/uart.py.exp @@ -14,7 +14,7 @@ UART YB ValueError Z UART(1, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64) UART(1, baudrate=2400, bits=8, parity=None, stop=1, timeout=1000, timeout_char=0, read_buf_len=64) -False +0 3 4 None |