summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--stmhal/uart.c18
-rw-r--r--stmhal/uart.h2
-rw-r--r--teensy/uart.c4
-rw-r--r--tests/pyb/uart.py.exp2
4 files changed, 14 insertions, 12 deletions
diff --git a/stmhal/uart.c b/stmhal/uart.c
index 3745451636..8b72327c24 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 5b223c5c61..a859f722b3 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 81035aed6a..8b73d733ff 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 40170eea97..ea300c90cb 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