From eb102a668f50f043e72bc11ebbd610c7cb62984e Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Fri, 9 Dec 2022 22:10:39 +0800 Subject: [PATCH] sim/uart: check if txbuffer is not full before uart write Signed-off-by: dongjiuzhu1 Signed-off-by: ligd --- arch/sim/src/sim/posix/sim_hostuart.c | 17 ++++++++++-- arch/sim/src/sim/sim_internal.h | 3 ++- arch/sim/src/sim/sim_uart.c | 39 ++++++++++++--------------- arch/sim/src/sim/win/sim_hostuart.c | 13 +++++++-- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/arch/sim/src/sim/posix/sim_hostuart.c b/arch/sim/src/sim/posix/sim_hostuart.c index 683b8bd9a9..b464301f75 100644 --- a/arch/sim/src/sim/posix/sim_hostuart.c +++ b/arch/sim/src/sim/posix/sim_hostuart.c @@ -199,10 +199,10 @@ int host_uart_setcflag(int fd, unsigned int cflag) } /**************************************************************************** - * Name: host_uart_checkc + * Name: host_uart_checkin ****************************************************************************/ -bool host_uart_checkc(int fd) +bool host_uart_checkin(int fd) { struct pollfd pfd; @@ -210,3 +210,16 @@ bool host_uart_checkc(int fd) pfd.events = POLLIN; return poll(&pfd, 1, 0) == 1; } + +/**************************************************************************** + * Name: host_uart_checkout + ****************************************************************************/ + +bool host_uart_checkout(int fd) +{ + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = POLLOUT; + return poll(&pfd, 1, 0) == 1; +} diff --git a/arch/sim/src/sim/sim_internal.h b/arch/sim/src/sim/sim_internal.h index 455488ebb1..2dc5267f3b 100644 --- a/arch/sim/src/sim/sim_internal.h +++ b/arch/sim/src/sim/sim_internal.h @@ -195,7 +195,8 @@ int host_uart_open(const char *pathname); void host_uart_close(int fd); int host_uart_putc(int fd, int ch); int host_uart_getc(int fd); -bool host_uart_checkc(int fd); +bool host_uart_checkin(int fd); +bool host_uart_checkout(int fd); int host_uart_setcflag(int fd, unsigned int cflag); int host_uart_getcflag(int fd, unsigned int *cflag); diff --git a/arch/sim/src/sim/sim_uart.c b/arch/sim/src/sim/sim_uart.c index 6a2086538e..12554cc9da 100644 --- a/arch/sim/src/sim/sim_uart.c +++ b/arch/sim/src/sim/sim_uart.c @@ -40,6 +40,13 @@ #define CONFIG_SIM_UART_BUFFER_SIZE 256 #endif +#define TTY_RECVSEND(port) \ + if (g_tty##port##_priv.fd > 0) \ + { \ + uart_recvchars(&g_tty##port##_dev); \ + uart_xmitchars(&g_tty##port##_dev); \ + } \ + /**************************************************************************** * Private Types ****************************************************************************/ @@ -391,7 +398,7 @@ static bool tty_rxavailable(struct uart_dev_s *dev) { struct tty_priv_s *priv = dev->priv; - return host_uart_checkc(dev->isconsole ? 0 : priv->fd); + return host_uart_checkin(dev->isconsole ? 0 : priv->fd); } /**************************************************************************** @@ -457,7 +464,9 @@ static void tty_txint(struct uart_dev_s *dev, bool enable) static bool tty_txready(struct uart_dev_s *dev) { - return true; + struct tty_priv_s *priv = dev->priv; + + return host_uart_checkout(dev->isconsole ? 0 : priv->fd); } /**************************************************************************** @@ -518,38 +527,24 @@ void sim_uartinit(void) void sim_uartloop(void) { #ifdef USE_DEVCONSOLE - if (host_uart_checkc(0)) - { - uart_recvchars(&g_console_dev); - } + uart_recvchars(&g_console_dev); + uart_xmitchars(&g_console_dev); #endif #ifdef CONFIG_SIM_UART0_NAME - if (g_tty0_priv.fd > 0 && host_uart_checkc(g_tty0_priv.fd)) - { - uart_recvchars(&g_tty0_dev); - } + TTY_RECVSEND(0) #endif #ifdef CONFIG_SIM_UART1_NAME - if (g_tty1_priv.fd > 0 && host_uart_checkc(g_tty1_priv.fd)) - { - uart_recvchars(&g_tty1_dev); - } + TTY_RECVSEND(1) #endif #ifdef CONFIG_SIM_UART2_NAME - if (g_tty2_priv.fd > 0 && host_uart_checkc(g_tty2_priv.fd)) - { - uart_recvchars(&g_tty2_dev); - } + TTY_RECVSEND(2) #endif #ifdef CONFIG_SIM_UART3_NAME - if (g_tty3_priv.fd > 0 && host_uart_checkc(g_tty3_priv.fd)) - { - uart_recvchars(&g_tty3_dev); - } + TTY_RECVSEND(3) #endif } diff --git a/arch/sim/src/sim/win/sim_hostuart.c b/arch/sim/src/sim/win/sim_hostuart.c index 014bf8400b..693018f2d4 100644 --- a/arch/sim/src/sim/win/sim_hostuart.c +++ b/arch/sim/src/sim/win/sim_hostuart.c @@ -126,10 +126,10 @@ int host_uart_setcflag(int fd, unsigned int cflag) } /**************************************************************************** - * Name: host_uart_checkc + * Name: host_uart_checkin ****************************************************************************/ -bool host_uart_checkc(int fd) +bool host_uart_checkin(int fd) { DWORD size; @@ -140,3 +140,12 @@ bool host_uart_checkc(int fd) return false; } + +/**************************************************************************** + * Name: host_uart_checkout + ****************************************************************************/ + +bool host_uart_checkout(int fd) +{ + return true; +}