From c04d2d691c77ca3a4a623e0af866e58f67914573 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 11 Oct 2025 14:41:13 +0200 Subject: [PATCH] qemu/uart: Implement uart_rx_any function. So it can be used for polling. Signed-off-by: iabdalkader --- ports/qemu/uart.c | 20 ++++++++++++++++++++ ports/qemu/uart.h | 1 + 2 files changed, 21 insertions(+) diff --git a/ports/qemu/uart.c b/ports/qemu/uart.c index fb9376d1fb..6bf4ba6e2e 100644 --- a/ports/qemu/uart.c +++ b/ports/qemu/uart.c @@ -57,6 +57,10 @@ int uart_rx_chr(void) { return UART0->DR; } +int uart_rx_any(void) { + return UART0->SR & UART_SR_RXNE; +} + void uart_tx_strn(const char *buf, size_t len) { for (size_t i = 0; i < len; ++i) { UART0->DR = buf[i]; @@ -94,6 +98,10 @@ int uart_rx_chr(void) { return UART0->RXD; } +int uart_rx_any(void) { + return UART0->RXDRDY ? 1 : 0; +} + void uart_tx_strn(const char *buf, size_t len) { for (size_t i = 0; i < len; ++i) { UART0->TXD = buf[i]; @@ -130,6 +138,10 @@ int uart_rx_chr(void) { return UART0->DATA; } +int uart_rx_any(void) { + return UART0->STATE & UART_STATE_RXFULL; +} + void uart_tx_strn(const char *buf, size_t len) { for (size_t i = 0; i < len; ++i) { while (UART0->STATE & UART_STATE_TXFULL) { @@ -170,6 +182,10 @@ int uart_rx_chr(void) { return UART1->URXD & 0xff; } +int uart_rx_any(void) { + return !(UART1->UTS1 & UART_UTS1_RXEMPTY); +} + void uart_tx_strn(const char *buf, size_t len) { for (size_t i = 0; i < len; ++i) { UART1->UTXD = buf[i]; @@ -200,6 +216,10 @@ int uart_rx_chr(void) { return UART_RX_NO_CHAR; } +int uart_rx_any(void) { + return UART0->LSR & UART_LSR_DR; +} + void uart_tx_strn(const char *buffer, size_t length) { for (size_t index = 0; index < length; index++) { while (!(UART0->LSR & UART_LSR_THRE)) { diff --git a/ports/qemu/uart.h b/ports/qemu/uart.h index 9c62a295d1..5363222f83 100644 --- a/ports/qemu/uart.h +++ b/ports/qemu/uart.h @@ -33,6 +33,7 @@ void uart_init(void); int uart_rx_chr(void); +int uart_rx_any(void); void uart_tx_strn(const char *buf, size_t len); #endif // MICROPY_INCLUDED_QEMU_ARM_UART_H