Commit f99491cb authored by Ryan Shaw's avatar Ryan Shaw Committed by Damien George
Browse files

stmhal: uart.any() function now returns number of bytes available.

parent bd33aa31
......@@ -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);
......
......@@ -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);
......@@ -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
}
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment