This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 4796d625441 serial/uart_ram: register as a chardev on the shared rings
4796d625441 is described below
commit 4796d625441d55ce766b545204de22d5fe328167
Author: Bowen Wang <[email protected]>
AuthorDate: Tue Oct 27 11:43:29 2026 +0800
serial/uart_ram: register as a chardev on the shared rings
uart_ram went through the serial upper half, so every byte was copied
into a local xmit/recv buffer on its way to the shared ring, and the
wdog moved the data in interrupt context.
Register it as a file_operations character device instead: read() and
write() copy between the caller's buffer and the ring, a mutex per side
keeps each ring single-producer/single-consumer, and the wdog only binds
the slave, wakes waiters and reports poll events. FIONREAD and
TIOCVHANGUP are kept for rpmsg_port_uart.
The shared memory layout changes, so both ends must be updated together.
Signed-off-by: Bowen Wang <[email protected]>
---
arch/sim/src/sim/sim_uart.c | 7 +-
drivers/serial/Kconfig | 19 +-
drivers/serial/uart_ram.c | 675 +++++++++++++++++++++-------------------
include/nuttx/serial/uart_ram.h | 20 +-
4 files changed, 380 insertions(+), 341 deletions(-)
diff --git a/arch/sim/src/sim/sim_uart.c b/arch/sim/src/sim/sim_uart.c
index 98e5ed53c65..7060cb958b4 100644
--- a/arch/sim/src/sim/sim_uart.c
+++ b/arch/sim/src/sim/sim_uart.c
@@ -293,6 +293,7 @@ static void uart_nputs(int fd, const char *buf, size_t size)
while (size > 0)
{
int ret = host_uart_puts(fd, buf, size);
+
if (ret < 0)
{
continue;
@@ -705,13 +706,12 @@ static bool tty_txempty(struct uart_dev_s *dev)
static int sim_uartram_register(const char *devname, bool slave)
{
char name[NAME_MAX];
- struct uart_rambuf_s *shmem;
+ uart_rambuf_t *shmem;
strlcpy(name, strrchr(devname, '/') + 1, NAME_MAX);
- shmem = host_allocshmem(name, sizeof(struct uart_rambuf_s) * 2);
+ shmem = host_allocshmem(name, sizeof(uart_rambuf_t) * 2);
DEBUGASSERT(shmem);
- memset(shmem, 0, sizeof(struct uart_rambuf_s) * 2);
return uart_ram_register(devname, shmem, slave);
}
#endif
@@ -800,6 +800,7 @@ void up_putc(int ch)
{
#ifdef USE_DEVCONSOLE
char c = ch;
+
up_nputs(&c, 1);
#endif
}
diff --git a/drivers/serial/Kconfig b/drivers/serial/Kconfig
index 9766cc5da36..6ee4986516d 100644
--- a/drivers/serial/Kconfig
+++ b/drivers/serial/Kconfig
@@ -893,25 +893,34 @@ source "drivers/serial/Kconfig-sci"
config RAM_UART
bool "RAM uart driver"
- select SERIAL_RXDMA
- select SERIAL_TXDMA
default n
---help---
It uses the memory block in the kernel as a communication
medium,
- which can communicate between different processes or different
CPUs
+ which can communicate between different processes or different
CPUs.
+ It is a character device on two shared rings, without the serial
+ upper half.
if RAM_UART
config RAM_UART_POLLING_INTERVAL
int "RAM uart buffer check interval"
default USEC_PER_TICK
---help---
- Interval in milliseconds to check for new data on uart ram
buffer
+ Interval in microseconds between polls of the RAM uart buffer
config RAM_UART_BUFSIZE
int "RAM_UART buffer size"
default 1024
+ range 2 16777152
+ ---help---
+ The capacity in bytes of the shared ring in each direction. The
+ upper bound keeps the rounded-up ring within the 24-bit ring
index.
+
+config RAM_UART_NPOLLWAITERS
+ int "RAM uart poll waiters"
+ default 2
---help---
- The size of the RAM_UART buffer in bytes
+ Maximum number of threads that can be waiting for POLL events
on a
+ single RAM uart device.
config RAM_UART0
bool "RAM uart driver 0"
diff --git a/drivers/serial/uart_ram.c b/drivers/serial/uart_ram.c
index a172396a404..069adc62d76 100644
--- a/drivers/serial/uart_ram.c
+++ b/drivers/serial/uart_ram.c
@@ -24,13 +24,20 @@
* Included Files
****************************************************************************/
-#include <assert.h>
+#include <nuttx/config.h>
+
#include <errno.h>
-#include <string.h>
+#include <fcntl.h>
+#include <poll.h>
#include <sys/types.h>
+#include <nuttx/fs/fs.h>
+#include <nuttx/fs/ioctl.h>
#include <nuttx/kmalloc.h>
-#include <nuttx/serial/serial.h>
+#include <nuttx/mutex.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/spinlock.h>
+#include <nuttx/serial/tioctl.h>
#include <nuttx/serial/uart_ram.h>
#include <nuttx/wdog.h>
@@ -41,182 +48,86 @@
#define UART_RAMBUF_SECTION(n) \
locate_data(CONFIG_RAM_UART_BUFFER_SECTION "." #n)
+#define UART_RAM_INITIALIZER(buf) \
+ { \
+ .rxlock = NXMUTEX_INITIALIZER, \
+ .txlock = NXMUTEX_INITIALIZER, \
+ .lock = SP_UNLOCKED, \
+ .recvsem = SEM_INITIALIZER(0), \
+ .sendsem = SEM_INITIALIZER(0), \
+ .rambuf = (buf), \
+ }
+
/****************************************************************************
* Private Types
****************************************************************************/
+/* The peer raises no interrupt, so the wdog stands in for it: it binds the
+ * slave and wakes waiters.
+ */
+
struct uart_ram_s
{
- struct uart_dev_s uart;
- struct wdog_s wdog;
- FAR struct uart_rambuf_s *tx;
- FAR struct uart_rambuf_s *rx;
+ struct wdog_s wdog;
+ mutex_t rxlock; /* Single consumer on rx */
+ mutex_t txlock; /* Single producer on tx */
+ spinlock_t lock; /* Shared with the wdog */
+ sem_t recvsem;
+ sem_t sendsem;
+ uint16_t crefs;
+ FAR struct pollfd_s *fds[CONFIG_RAM_UART_NPOLLWAITERS];
+ struct ring_s tx;
+ struct ring_s rx;
+ FAR uart_rambuf_t *rambuf; /* [0] master writes, [1] slave */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
-static int uart_ram_setup(FAR struct uart_dev_s *dev);
-static void uart_ram_shutdown(FAR struct uart_dev_s *dev);
-static int uart_ram_attach(FAR struct uart_dev_s *dev);
-static void uart_ram_detach(FAR struct uart_dev_s *dev);
-static int uart_ram_ioctl(FAR struct file *filep, int cmd,
- unsigned long arg);
-static int uart_ram_receive(FAR struct uart_dev_s *dev,
- FAR unsigned int *status);
-static void uart_ram_rxint(FAR struct uart_dev_s *dev, bool enable);
-static bool uart_ram_rxavailable(FAR struct uart_dev_s *dev);
-static void uart_ram_dmasend(FAR struct uart_dev_s *dev);
-static void uart_ram_dmareceive(FAR struct uart_dev_s *dev);
-static void uart_ram_dmarxfree(FAR struct uart_dev_s *dev);
-static void uart_ram_dmatxavail(FAR struct uart_dev_s *dev);
-static void uart_ram_send(FAR struct uart_dev_s *dev, int ch);
-static void uart_ram_txint(FAR struct uart_dev_s *dev, bool enable);
-static bool uart_ram_txready(FAR struct uart_dev_s *dev);
-static bool uart_ram_txempty(FAR struct uart_dev_s *dev);
-
-static void uart_ram_wdog(wdparm_t arg);
+static int uart_ram_open(FAR struct file *filep);
+static int uart_ram_close(FAR struct file *filep);
+static ssize_t uart_ram_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen);
+static ssize_t uart_ram_write(FAR struct file *filep,
+ FAR const char *buffer, size_t buflen);
+static int uart_ram_ioctl(FAR struct file *filep, int cmd,
+ unsigned long arg);
+static int uart_ram_poll(FAR struct file *filep,
+ FAR struct pollfd_s *fds, bool setup);
+
+static void uart_ram_wdog(wdparm_t arg);
/****************************************************************************
* Private Data
****************************************************************************/
-static const struct uart_ops_s g_uart_ram_ops =
+static const struct file_operations g_uart_ram_fops =
{
- uart_ram_setup,
- uart_ram_shutdown,
- uart_ram_attach,
- uart_ram_detach,
+ uart_ram_open,
+ uart_ram_close,
+ uart_ram_read,
+ uart_ram_write,
+ NULL, /* seek */
uart_ram_ioctl,
- uart_ram_receive,
- uart_ram_rxint,
- uart_ram_rxavailable,
-#ifdef CONFIG_SERIAL_IFLOWCONTROL
- NULL,
-#endif
- uart_ram_dmasend,
- uart_ram_dmareceive,
- uart_ram_dmarxfree,
- uart_ram_dmatxavail,
- uart_ram_send,
- uart_ram_txint,
- uart_ram_txready,
- uart_ram_txempty,
+ NULL, /* mmap */
+ NULL, /* truncate */
+ uart_ram_poll,
};
#ifdef CONFIG_RAM_UART0
-static struct uart_rambuf_s UART_RAMBUF_SECTION(0) g_uart_rambuf0[2];
-
-static struct uart_ram_s g_uart_ram0 =
-{
- .uart =
- {
- .xmit =
- {
-# ifdef CONFIG_RAM_UART0_SLAVE
- .buffer = g_uart_rambuf0[1].buffer,
-# else
- .buffer = g_uart_rambuf0[0].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .recv =
- {
-# ifdef CONFIG_RAM_UART0_SLAVE
- .buffer = g_uart_rambuf0[0].buffer,
-# else
- .buffer = g_uart_rambuf0[1].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .ops = &g_uart_ram_ops,
- .priv = &g_uart_ram0,
- },
-# ifdef CONFIG_RAM_UART0_SLAVE
- .tx = &g_uart_rambuf0[1],
- .rx = &g_uart_rambuf0[0],
-# else
- .tx = &g_uart_rambuf0[0],
- .rx = &g_uart_rambuf0[1],
-# endif
-};
+static uart_rambuf_t UART_RAMBUF_SECTION(0) g_uart_rambuf0[2];
+static struct uart_ram_s g_uart_ram0 = UART_RAM_INITIALIZER(g_uart_rambuf0);
#endif
#ifdef CONFIG_RAM_UART1
-static struct uart_rambuf_s UART_RAMBUF_SECTION(1) g_uart_rambuf1[2];
-
-static struct uart_ram_s g_uart_ram1 =
-{
- .uart =
- {
- .xmit =
- {
-# ifdef CONFIG_RAM_UART1_SLAVE
- .buffer = g_uart_rambuf1[1].buffer,
-# else
- .buffer = g_uart_rambuf1[0].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .recv =
- {
-# ifdef CONFIG_RAM_UART1_SLAVE
- .buffer = g_uart_rambuf1[0].buffer,
-# else
- .buffer = g_uart_rambuf1[1].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .ops = &g_uart_ram_ops,
- .priv = &g_uart_ram1,
- },
-# ifdef CONFIG_RAM_UART1_SLAVE
- .tx = &g_uart_rambuf1[1],
- .rx = &g_uart_rambuf1[0],
-# else
- .tx = &g_uart_rambuf1[0],
- .rx = &g_uart_rambuf1[1],
-# endif
-};
+static uart_rambuf_t UART_RAMBUF_SECTION(1) g_uart_rambuf1[2];
+static struct uart_ram_s g_uart_ram1 = UART_RAM_INITIALIZER(g_uart_rambuf1);
#endif
#ifdef CONFIG_RAM_UART2
-static struct uart_rambuf_s UART_RAMBUF_SECTION(2) g_uart_rambuf2[2];
-
-static struct uart_ram_s g_uart_ram2 =
-{
- .uart =
- {
- .xmit =
- {
-# ifdef CONFIG_RAM_UART2_SLAVE
- .buffer = g_uart_rambuf2[1].buffer,
-# else
- .buffer = g_uart_rambuf2[0].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .recv =
- {
-# ifdef CONFIG_RAM_UART2_SLAVE
- .buffer = g_uart_rambuf2[0].buffer,
-# else
- .buffer = g_uart_rambuf2[1].buffer,
-# endif
- .size = CONFIG_RAM_UART_BUFSIZE,
- },
- .ops = &g_uart_ram_ops,
- .priv = &g_uart_ram2,
- },
-# ifdef CONFIG_RAM_UART2_SLAVE
- .tx = &g_uart_rambuf2[1],
- .rx = &g_uart_rambuf2[0],
-# else
- .tx = &g_uart_rambuf2[0],
- .rx = &g_uart_rambuf2[1],
-# endif
-};
+static uart_rambuf_t UART_RAMBUF_SECTION(2) g_uart_rambuf2[2];
+static struct uart_ram_s g_uart_ram2 = UART_RAM_INITIALIZER(g_uart_rambuf2);
#endif
/****************************************************************************
@@ -224,216 +135,296 @@ static struct uart_ram_s g_uart_ram2 =
****************************************************************************/
/****************************************************************************
- * Name: uart_rambuf_txready
+ * Name: uart_ram_post
****************************************************************************/
-static size_t uart_rambuf_txready(FAR struct uart_rambuf_s *buf)
+static void uart_ram_post(FAR sem_t *sem)
{
- int wroff = atomic_read(&buf->wroff);
- int rdoff = atomic_read(&buf->rdoff);
- return rdoff > wroff ? rdoff - wroff - 1 :
- sizeof(buf->buffer) - wroff + rdoff - 1;
-}
+ int semcount;
-/****************************************************************************
- * Name: uart_rambuf_rxavailable
- ****************************************************************************/
-
-static size_t uart_rambuf_rxavailable(FAR struct uart_rambuf_s *buf)
-{
- int wroff = atomic_read(&buf->wroff);
- int rdoff = atomic_read(&buf->rdoff);
+ /* The wdog posts on every tick */
- return wroff >= rdoff ? wroff - rdoff :
- sizeof(buf->buffer) - rdoff + wroff;
+ nxsem_get_value(sem, &semcount);
+ if (semcount < 1)
+ {
+ nxsem_post(sem);
+ }
}
/****************************************************************************
- * Name: uart_ram_setup
+ * Name: uart_ram_wait_ready
****************************************************************************/
-static int uart_ram_setup(FAR struct uart_dev_s *dev)
+static int uart_ram_wait_ready(FAR struct file *filep,
+ FAR struct ring_s *ring, FAR sem_t *sem)
{
- return OK;
-}
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ irqstate_t flags;
+ bool ready;
+ int ret;
-/****************************************************************************
- * Name: uart_ram_shutdown
- ****************************************************************************/
+ /* A ring never goes back to unbound, so this is checked once */
-static void uart_ram_shutdown(FAR struct uart_dev_s *dev)
-{
+ for (; ; )
+ {
+ flags = spin_lock_irqsave(&priv->lock);
+ ready = ring_is_ready(ring);
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ if (ready)
+ {
+ return OK;
+ }
+ else if (filep->f_oflags & O_NONBLOCK)
+ {
+ return -EAGAIN;
+ }
+
+ ret = nxsem_wait(sem);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
}
/****************************************************************************
- * Name: uart_ram_attach
+ * Name: uart_ram_open
****************************************************************************/
-static int uart_ram_attach(FAR struct uart_dev_s *dev)
+static int uart_ram_open(FAR struct file *filep)
{
- FAR struct uart_ram_s *priv = dev->priv;
- wd_start(&priv->wdog, USEC2TICK(CONFIG_RAM_UART_POLLING_INTERVAL),
- uart_ram_wdog, (wdparm_t)dev);
- return OK;
-}
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ irqstate_t flags;
-/****************************************************************************
- * Name: uart_ram_detach
- ****************************************************************************/
-
-static void uart_ram_detach(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
- wd_cancel(&priv->wdog);
-}
+ flags = spin_lock_irqsave(&priv->lock);
-/****************************************************************************
- * Name: uart_ram_ioctl
- ****************************************************************************/
+ DEBUGASSERT(priv->crefs < UINT16_MAX);
+ if (priv->crefs++ == 0)
+ {
+ wd_start(&priv->wdog, USEC2TICK(CONFIG_RAM_UART_POLLING_INTERVAL),
+ uart_ram_wdog, (wdparm_t)priv);
+ }
-static int uart_ram_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
-{
- return -ENOTTY;
+ spin_unlock_irqrestore(&priv->lock, flags);
+ return OK;
}
/****************************************************************************
- * Name: uart_ram_receive
+ * Name: uart_ram_close
****************************************************************************/
-static int uart_ram_receive(FAR struct uart_dev_s *dev,
- FAR unsigned int *status)
+static int uart_ram_close(FAR struct file *filep)
{
- FAR struct uart_ram_s *priv = dev->priv;
- int rdoff;
- int ch;
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ irqstate_t flags;
- while (!uart_rambuf_rxavailable(priv->rx));
+ flags = spin_lock_irqsave(&priv->lock);
- rdoff = atomic_read(&priv->rx->rdoff);
- ch = priv->rx->buffer[rdoff];
- if (++rdoff >= sizeof(priv->rx->buffer))
+ DEBUGASSERT(priv->crefs > 0);
+ if (--priv->crefs == 0)
{
- rdoff = 0;
+ wd_cancel(&priv->wdog);
}
- atomic_set(&priv->rx->rdoff, rdoff);
-
- *status = 0;
- return ch;
+ spin_unlock_irqrestore(&priv->lock, flags);
+ return OK;
}
/****************************************************************************
- * Name: uart_ram_rxint
+ * Name: uart_ram_read
****************************************************************************/
-static void uart_ram_rxint(FAR struct uart_dev_s *dev, bool enable)
+static ssize_t uart_ram_read(FAR struct file *filep, FAR char *buffer,
+ size_t buflen)
{
-}
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ ssize_t ret;
-/****************************************************************************
- * Name: uart_ram_rxavailable
- ****************************************************************************/
+ if (buflen == 0)
+ {
+ return 0;
+ }
-static bool uart_ram_rxavailable(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
- return uart_rambuf_rxavailable(priv->rx) != 0;
-}
+ ret = nxmutex_lock(&priv->rxlock);
+ if (ret < 0)
+ {
+ return ret;
+ }
-/****************************************************************************
- * Name: uart_ram_dmasend
- ****************************************************************************/
+ ret = uart_ram_wait_ready(filep, &priv->rx, &priv->recvsem);
-static void uart_ram_dmasend(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
+ while (ret >= 0)
+ {
+ ret = ring_spsc_dequeue(&priv->rx, buffer, buflen);
+ if (ret > 0)
+ {
+ break;
+ }
+
+ if (filep->f_oflags & O_NONBLOCK)
+ {
+ ret = -EAGAIN;
+ break;
+ }
+
+ ret = nxsem_wait(&priv->recvsem);
+ }
- atomic_set(&priv->tx->wroff, dev->xmit.head);
+ nxmutex_unlock(&priv->rxlock);
+ return ret;
}
/****************************************************************************
- * Name: uart_ram_dmareceive
+ * Name: uart_ram_write
****************************************************************************/
-static void uart_ram_dmareceive(FAR struct uart_dev_s *dev)
+static ssize_t uart_ram_write(FAR struct file *filep,
+ FAR const char *buffer, size_t buflen)
{
- FAR struct uart_ram_s *priv = dev->priv;
- dev->dmarx.nbytes = uart_rambuf_rxavailable(priv->rx);
- uart_recvchars_done(dev);
-}
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ size_t nwritten = 0;
+ ssize_t ret;
-/****************************************************************************
- * Name: uart_ram_dmarxfree
- ****************************************************************************/
+ if (buflen == 0)
+ {
+ return 0;
+ }
-static void uart_ram_dmarxfree(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
+ ret = nxmutex_lock(&priv->txlock);
+ if (ret < 0)
+ {
+ return ret;
+ }
- /* When the dma RX buffer is free, update the read data position */
+ ret = uart_ram_wait_ready(filep, &priv->tx, &priv->sendsem);
- atomic_set(&priv->rx->rdoff, dev->recv.tail);
+ while (ret >= 0 && nwritten < buflen)
+ {
+ ret = ring_spsc_enqueue(&priv->tx, &buffer[nwritten],
+ buflen - nwritten);
+ if (ret > 0)
+ {
+ nwritten += ret;
+ ret = nwritten;
+ continue;
+ }
+
+ if (filep->f_oflags & O_NONBLOCK)
+ {
+ ret = nwritten > 0 ? (ssize_t)nwritten : -EAGAIN;
+ break;
+ }
+
+ ret = nxsem_wait(&priv->sendsem);
+ }
+
+ nxmutex_unlock(&priv->txlock);
+ return nwritten > 0 ? (ssize_t)nwritten : ret;
}
/****************************************************************************
- * Name: uart_ram_dmatxavail
+ * Name: uart_ram_ioctl
****************************************************************************/
-static void uart_ram_dmatxavail(FAR struct uart_dev_s *dev)
+static int uart_ram_ioctl(FAR struct file *filep, int cmd,
+ unsigned long arg)
{
- if (dev->dmatx.length == 0)
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ int ret = OK;
+
+ switch (cmd)
{
- uart_xmitchars_dma(dev);
- }
-}
+ case FIONREAD:
+ {
+ FAR int *nbytes = (FAR int *)(uintptr_t)arg;
-/****************************************************************************
- * Name: uart_ram_send
- ****************************************************************************/
+ if (nbytes == NULL)
+ {
+ ret = -EINVAL;
+ break;
+ }
-static void uart_ram_send(FAR struct uart_dev_s *dev, int ch)
-{
- FAR struct uart_ram_s *priv = dev->priv;
- int wroff;
+ *nbytes = ring_is_ready(&priv->rx) ? ring_count(&priv->rx) : 0;
+ }
+ break;
- while (!uart_rambuf_txready(priv->tx));
+ /* Nothing to release; rpmsg_port_uart sends it on peer poweroff */
- wroff = atomic_read(&priv->tx->wroff);
- priv->tx->buffer[wroff] = ch;
- if (++wroff >= sizeof(priv->tx->buffer))
- {
- wroff = 0;
+ case TIOCVHANGUP:
+ break;
+
+ default:
+ ret = -ENOTTY;
+ break;
}
- atomic_set(&priv->tx->wroff, wroff);
+ return ret;
}
/****************************************************************************
- * Name: uart_ram_txint
+ * Name: uart_ram_poll
****************************************************************************/
-static void uart_ram_txint(FAR struct uart_dev_s *dev, bool enable)
+static int uart_ram_poll(FAR struct file *filep,
+ FAR struct pollfd_s *fds,
+ bool setup)
{
-}
+ FAR struct uart_ram_s *priv = filep->f_inode->i_private;
+ pollevent_t eventset = 0;
+ irqstate_t flags;
+ int ret = OK;
+ int i;
-/****************************************************************************
- * Name: uart_ram_txready
- ****************************************************************************/
+ flags = spin_lock_irqsave_nopreempt(&priv->lock);
-static bool uart_ram_txready(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
- return uart_rambuf_txready(priv->tx) != 0;
-}
+ if (setup)
+ {
+ for (i = 0; i < CONFIG_RAM_UART_NPOLLWAITERS; i++)
+ {
+ if (priv->fds[i] == NULL)
+ {
+ priv->fds[i] = fds;
+ fds->priv = &priv->fds[i];
+ break;
+ }
+ }
+
+ if (i >= CONFIG_RAM_UART_NPOLLWAITERS)
+ {
+ ret = -EBUSY;
+ goto errout;
+ }
+
+ if (ring_is_ready(&priv->rx) && ring_count(&priv->rx) > 0)
+ {
+ eventset |= POLLIN;
+ }
+
+ if (ring_is_ready(&priv->tx) && ring_space(&priv->tx) > 0)
+ {
+ eventset |= POLLOUT;
+ }
+
+ if (eventset != 0)
+ {
+ poll_notify(&fds, 1, eventset);
+ }
+ }
+ else
+ {
+ FAR struct pollfd_s **slot = (FAR struct pollfd_s **)fds->priv;
-/****************************************************************************
- * Name: uart_ram_txempty
- ****************************************************************************/
+ if (slot != NULL)
+ {
+ *slot = NULL;
+ fds->priv = NULL;
+ }
+ }
-static bool uart_ram_txempty(FAR struct uart_dev_s *dev)
-{
- FAR struct uart_ram_s *priv = dev->priv;
- return uart_rambuf_rxavailable(priv->tx) == 0;
+errout:
+ spin_unlock_irqrestore_nopreempt(&priv->lock, flags);
+ return ret;
}
/****************************************************************************
@@ -442,31 +433,52 @@ static bool uart_ram_txempty(FAR struct uart_dev_s *dev)
static void uart_ram_wdog(wdparm_t arg)
{
- FAR struct uart_dev_s *dev = (FAR struct uart_dev_s *)arg;
- FAR struct uart_ram_s *priv = dev->priv;
+ FAR struct uart_ram_s *priv = (FAR struct uart_ram_s *)arg;
+ FAR struct ring_s *tx = &priv->tx;
+ FAR struct ring_s *rx = &priv->rx;
+ pollevent_t eventset = 0;
+ irqstate_t flags;
+
+ flags = spin_lock_irqsave_nopreempt(&priv->lock);
- /* When the read and write pointers of the tx buffer are same,
- * it means that the data transmission is completed
- */
+ /* No-op once bound; the slave binds once the master is up */
- if (dev->dmatx.length > 0 && !uart_rambuf_rxavailable(priv->tx))
+ ring_spsc_init(tx, priv->rambuf[1], sizeof(priv->rambuf[1]), 0, true);
+ ring_spsc_init(rx, priv->rambuf[0], sizeof(priv->rambuf[0]), 0, true);
+
+ if (ring_is_ready(tx) && ring_is_ready(rx))
{
- dev->dmatx.nbytes = dev->dmatx.length + dev->dmatx.nlength;
- uart_xmitchars_done(dev);
- uart_ram_dmatxavail(dev);
+ if (ring_count(rx) > 0)
+ {
+ uart_ram_post(&priv->recvsem);
+ eventset |= POLLIN;
+ }
+
+ if (ring_space(tx) > 0)
+ {
+ uart_ram_post(&priv->sendsem);
+ eventset |= POLLOUT;
+ }
+
+ /* fds is written by poll() under the same lock, and the callback
+ * may wake a task, so the lock must span poll_notify()
+ */
+
+ if (eventset != 0)
+ {
+ poll_notify(priv->fds, CONFIG_RAM_UART_NPOLLWAITERS, eventset);
+ }
}
- /* When the read and write pointers of the rx buffer are different,
- * it means that the data is received
- */
+ /* Under the lock, so that a racing last close() stops it */
- if (dev->dmarx.length == 0 && uart_rambuf_rxavailable(priv->rx))
+ if (priv->crefs > 0)
{
- uart_recvchars_dma(dev);
+ wd_start(&priv->wdog, USEC2TICK(CONFIG_RAM_UART_POLLING_INTERVAL),
+ uart_ram_wdog, arg);
}
- wd_start(&priv->wdog, USEC2TICK(CONFIG_RAM_UART_POLLING_INTERVAL),
- uart_ram_wdog, (wdparm_t)dev);
+ spin_unlock_irqrestore_nopreempt(&priv->lock, flags);
}
/****************************************************************************
@@ -477,8 +489,7 @@ static void uart_ram_wdog(wdparm_t arg)
* Name: uart_ram_register
****************************************************************************/
-int uart_ram_register(FAR const char *devname,
- struct uart_rambuf_s rambuf[2],
+int uart_ram_register(FAR const char *devname, uart_rambuf_t rambuf[2],
bool slave)
{
FAR struct uart_ram_s *priv;
@@ -490,32 +501,28 @@ int uart_ram_register(FAR const char *devname,
return -ENOMEM;
}
- atomic_set(&rambuf[0].wroff, 0);
- atomic_set(&rambuf[0].rdoff, 0);
- atomic_set(&rambuf[1].wroff, 0);
- atomic_set(&rambuf[1].rdoff, 0);
+ nxmutex_init(&priv->rxlock);
+ nxmutex_init(&priv->txlock);
+ spin_lock_init(&priv->lock);
+ nxsem_init(&priv->recvsem, 0, 0);
+ nxsem_init(&priv->sendsem, 0, 0);
+ priv->rambuf = rambuf;
- if (slave)
- {
- priv->tx = rambuf + 1;
- priv->rx = rambuf;
- }
- else
+ /* Only the master resets the rings; the slave binds from the wdog */
+
+ if (!slave)
{
- priv->tx = rambuf;
- priv->rx = rambuf + 1;
+ ring_spsc_init(&priv->tx, rambuf[0], sizeof(rambuf[0]), 0, false);
+ ring_spsc_init(&priv->rx, rambuf[1], sizeof(rambuf[1]), 0, false);
}
- priv->uart.priv = priv;
- priv->uart.ops = &g_uart_ram_ops;
- priv->uart.xmit.buffer = priv->tx->buffer;
- priv->uart.recv.buffer = priv->rx->buffer;
- priv->uart.xmit.size = CONFIG_RAM_UART_BUFSIZE;
- priv->uart.recv.size = CONFIG_RAM_UART_BUFSIZE;
-
- ret = uart_register(devname, &priv->uart);
+ ret = register_driver(devname, &g_uart_ram_fops, 0666, priv);
if (ret < 0)
{
+ nxmutex_destroy(&priv->rxlock);
+ nxmutex_destroy(&priv->txlock);
+ nxsem_destroy(&priv->recvsem);
+ nxsem_destroy(&priv->sendsem);
kmm_free(priv);
}
@@ -529,14 +536,32 @@ int uart_ram_register(FAR const char *devname,
void ram_serialinit(void)
{
#ifdef CONFIG_RAM_UART0
- uart_register("/dev/tty0", &g_uart_ram0.uart);
+# ifndef CONFIG_RAM_UART0_SLAVE
+ ring_spsc_init(&g_uart_ram0.tx, g_uart_rambuf0[0],
+ sizeof(g_uart_rambuf0[0]), 0, false);
+ ring_spsc_init(&g_uart_ram0.rx, g_uart_rambuf0[1],
+ sizeof(g_uart_rambuf0[1]), 0, false);
+# endif
+ register_driver("/dev/tty0", &g_uart_ram_fops, 0666, &g_uart_ram0);
#endif
#ifdef CONFIG_RAM_UART1
- uart_register("/dev/tty1", &g_uart_ram1.uart);
+# ifndef CONFIG_RAM_UART1_SLAVE
+ ring_spsc_init(&g_uart_ram1.tx, g_uart_rambuf1[0],
+ sizeof(g_uart_rambuf1[0]), 0, false);
+ ring_spsc_init(&g_uart_ram1.rx, g_uart_rambuf1[1],
+ sizeof(g_uart_rambuf1[1]), 0, false);
+# endif
+ register_driver("/dev/tty1", &g_uart_ram_fops, 0666, &g_uart_ram1);
#endif
#ifdef CONFIG_RAM_UART2
- uart_register("/dev/tty2", &g_uart_ram2.uart);
+# ifndef CONFIG_RAM_UART2_SLAVE
+ ring_spsc_init(&g_uart_ram2.tx, g_uart_rambuf2[0],
+ sizeof(g_uart_rambuf2[0]), 0, false);
+ ring_spsc_init(&g_uart_ram2.rx, g_uart_rambuf2[1],
+ sizeof(g_uart_rambuf2[1]), 0, false);
+# endif
+ register_driver("/dev/tty2", &g_uart_ram_fops, 0666, &g_uart_ram2);
#endif
}
diff --git a/include/nuttx/serial/uart_ram.h b/include/nuttx/serial/uart_ram.h
index f9cd25c46a7..6e13df8ed17 100644
--- a/include/nuttx/serial/uart_ram.h
+++ b/include/nuttx/serial/uart_ram.h
@@ -32,22 +32,26 @@
#ifdef CONFIG_RAM_UART
#include <stdbool.h>
-#include <nuttx/atomic.h>
+#include <nuttx/ring.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
+/* Rounded up so that in an array of two the second blob stays aligned */
+
+#define UART_RAMBUF_BLOBSIZE \
+ ALIGN_UP(sizeof(struct ring_blob_s) + CONFIG_RAM_UART_BUFSIZE, \
+ RING_BLOB_ALIGN)
+
/****************************************************************************
* Type Declarations
****************************************************************************/
-struct uart_rambuf_s
-{
- char buffer[CONFIG_RAM_UART_BUFSIZE];
- atomic_t wroff;
- atomic_t rdoff;
-};
+/* One direction of the link: a ring_spsc blob written by exactly one side */
+
+typedef uint8_t uart_rambuf_t[UART_RAMBUF_BLOBSIZE]
+ aligned_data(RING_BLOB_ALIGN);
/****************************************************************************
* Public Data
@@ -67,7 +71,7 @@ extern "C"
****************************************************************************/
int uart_ram_register(FAR const char *devname,
- FAR struct uart_rambuf_s buffer[2], bool slave);
+ FAR uart_rambuf_t buffer[2], bool slave);
/****************************************************************************
* Name: ram_serialinit