You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/12/20 10:02:01 UTC

[nuttx] 01/02: sim/uart: check if txbuffer is not full before uart write

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit eb102a668f50f043e72bc11ebbd610c7cb62984e
Author: dongjiuzhu1 <do...@xiaomi.com>
AuthorDate: Fri Dec 9 22:10:39 2022 +0800

    sim/uart: check if txbuffer is not full before uart write
    
    Signed-off-by: dongjiuzhu1 <do...@xiaomi.com>
    Signed-off-by: ligd <li...@xiaomi.com>
---
 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;
+}