This is an automated email from the ASF dual-hosted git repository. casaroli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 841980192e92840b07a45f412418f8e603dc0fad Author: Alan Carvalho de Assis <[email protected]> AuthorDate: Tue Nov 10 16:29:48 2026 -0300 drivers/misc/lwl_console: Make the LWL console interrupt driven. The Lightweight Link console busy-waited for the host: a task reading the console spun until a key was pressed, so nsh kept the CPU 100% busy while waiting for input, and every word written spun until the host collected the previous one. It was also slow, three bytes per debugger round trip, had no buffering, wrote forever when no host was connected and did not work on CPUs with a data cache. Rework it as a lower half of the generic serial driver: * The console gets RX/TX buffers, termios processing, poll() and the TTY signals from the serial upper half. * Optional doorbell (CONFIG_LWL_CONSOLE_DOORBELL): after every update of the link the host writes a register of the interrupt controller that makes an unused interrupt pending, and the target handles the data in the interrupt handler. No polling and no busy waiting at all. The board gives the IRQ, the register address and the value (e.g. an NVIC Interrupt Set-Pending Register on Cortex-M), and the firmware publishes them to the host, so neither the driver nor the host is tied to an architecture. * Without the doorbell the link is polled by a watchdog timer (CONFIG_LWL_CONSOLE_POLL_INTERVAL), which works with any host. * The link is serialized with the upper half and with up_putc() by the per-device uart lock (uart_spinlock()) instead of the global critical section. * Output ring buffer in the control block (CONFIG_LWL_CONSOLE_RINGSIZE): hosts that support it collect a whole ring of output in one debugger access instead of three bytes per round trip. * The downword is left unacknowledged while the RX buffer is full, so input is never lost. Without a host the TX buffer keeps the most recent output, shown when the host connects, instead of blocking. * The line being output, typically the shell prompt, is sent again to every new host, so the prompt shows up without pressing Enter. * The target adopts the host's state when the link is opened and clears it when the link is closed, so a new host always resynchronises. * The control block is cache line aligned, the fields written by the host and by the target are in different cache lines and the data cache is maintained around each access, since the debugger accesses RAM behind it. * up_putc() gives up waiting when an open link is not served anymore instead of hanging the system. The control block is extended after a second signature; the word protocol is unchanged, so old and new host tools and firmware interoperate (old host tools need a firmware without the doorbell). Tested on weact-stm32h743 with an ST-Link/V2 and OpenOCD 0.12: nsh uses 0% CPU waiting for input (100% before), the output runs at ~28 KB/s (~1.8 KB/s before), typed-ahead input is kept, with D-cache write-through and write-back. Signed-off-by: Alan Carvalho de Assis <[email protected]> Assisted-by: Claude Code Opus 5.5 --- .../stm32f4/stm32f4discovery/configs/lwl/defconfig | 1 - drivers/misc/Kconfig | 88 +- drivers/misc/lwl_console.c | 1014 +++++++++++++++++--- 3 files changed, 942 insertions(+), 161 deletions(-) diff --git a/boards/arm/stm32f4/stm32f4discovery/configs/lwl/defconfig b/boards/arm/stm32f4/stm32f4discovery/configs/lwl/defconfig index 5f14cb57fe7..b03fc1130e8 100644 --- a/boards/arm/stm32f4/stm32f4discovery/configs/lwl/defconfig +++ b/boards/arm/stm32f4/stm32f4discovery/configs/lwl/defconfig @@ -8,7 +8,6 @@ # CONFIG_ARCH_FPU is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_CMDOPT_HEXDUMP is not set -# CONFIG_SERIAL is not set CONFIG_ARCH="arm" CONFIG_ARCH_BOARD="stm32f4discovery" CONFIG_ARCH_BOARD_STM32F4_DISCOVERY=y diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index f59169b9383..0895a983ddf 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -149,13 +149,95 @@ config ARCH_HAVE_RDWR_MEM_CPU_RUN default n config LWL_CONSOLE -bool "Lightweight Link Console Support" + bool "Lightweight Link Console Support" default n - depends on DEV_CONSOLE && ARCH_HAVE_RDWR_MEM_CPU_RUN + depends on DEV_CONSOLE && ARCH_HAVE_RDWR_MEM_CPU_RUN && SERIAL ---help--- Use the lightweight link console which provides console over a debug channel by means of shared memory. A terminal application - for openocd as the debugger is available in tools/ocdconsole.py. + for openocd as the debugger is available in tools/lwl/ocdconsole.py. + See Documentation/guides/drivers/lwl.rst. + +if LWL_CONSOLE + +config LWL_CONSOLE_RXBUFSIZE + int "Receive buffer size" + default 64 + ---help--- + Size of the buffer that holds the characters received from the + host until they are read. While it is full the host is held off, + so no input is lost. + +config LWL_CONSOLE_TXBUFSIZE + int "Transmit buffer size" + default 256 + ---help--- + Size of the buffer that holds the characters waiting to be + collected by the host. While no host is connected the most recent + output is kept here and shown when the host connects. + +config LWL_CONSOLE_RINGSIZE + int "Output ring buffer size" + default 256 + ---help--- + Size of the ring buffer in the control block from which the host + collects the output, many bytes per debugger access instead of + three per word. This is what makes the output fast. It must be + a multiple of 4. 0 disables it: the output then only uses the + word protocol, as with old host tools. + +config LWL_CONSOLE_DOORBELL + bool "Doorbell interrupt" + default n + ---help--- + Let the host raise an interrupt every time it updates the link, by + writing a register of the interrupt controller that makes an + unused interrupt pending. The console is then entirely interrupt + driven: the CPU does no work at all while the console is idle. + It requires a host that rings the doorbell, such as + tools/lwl/ocdconsole.py. Without the doorbell the link is polled, + see LWL_CONSOLE_POLL_INTERVAL. + +if LWL_CONSOLE_DOORBELL + +config LWL_CONSOLE_DOORBELL_IRQ + int "Doorbell IRQ number" + ---help--- + The NuttX IRQ number used as the doorbell. It must be implemented + by the chip but not used by any other driver. + +config LWL_CONSOLE_DOORBELL_ADDR + hex "Doorbell register address" + default 0x0 + ---help--- + Address of the register the host writes to make the doorbell IRQ + pending. The debugger must be able to write it while the CPU is + running. On ARMv6-M/ARMv7-M/ARMv8-M this is the NVIC Interrupt + Set-Pending Register 0xe000e200 + 4 * ((IRQ - 16) / 32). The + interrupt handler writes 0 to it, which must either clear the + request or have no effect. + +config LWL_CONSOLE_DOORBELL_VALUE + hex "Doorbell register value" + default 0x0 + ---help--- + Value the host writes to LWL_CONSOLE_DOORBELL_ADDR. On + ARMv6-M/ARMv7-M/ARMv8-M this is 1 << ((IRQ - 16) % 32). + +endif # LWL_CONSOLE_DOORBELL + +config LWL_CONSOLE_POLL_INTERVAL + int "Polling interval (usec)" + default 10000 + range 1 1000000 + depends on !LWL_CONSOLE_DOORBELL + ---help--- + Interval at which the link is polled while the console is open, + when the doorbell is not used. It works with any host, but the + CPU wakes up at this rate and the output throughput is limited + by it. + +endif # LWL_CONSOLE menu "Buffering" diff --git a/drivers/misc/lwl_console.c b/drivers/misc/lwl_console.c index fc089fed12b..92c079236ac 100644 --- a/drivers/misc/lwl_console.c +++ b/drivers/misc/lwl_console.c @@ -27,267 +27,952 @@ #include <nuttx/config.h> #include <sys/types.h> +#include <sys/param.h> +#include <stdbool.h> +#include <stdint.h> +#include <string.h> #include <errno.h> -#include <nuttx/debug.h> -#include <nuttx/fs/fs.h> +#include <nuttx/arch.h> +#include <nuttx/cache.h> +#include <nuttx/clock.h> +#include <nuttx/irq.h> +#include <nuttx/wdog.h> #include <nuttx/drivers/drivers.h> +#include <nuttx/serial/serial.h> /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -/* Lightweight Link (lwl) +/* Lightweight Link (LWL) * ====================== * * Lightweight bidirectional communication between target and debug host - * without any need for additional hardware. - * - * Works with openOCD and other debuggers that are capable of reading and - * writing memory while the target is running. - * - * Principle of operation is simple; An 'upword' of 32 bits communicates - * from the target to the host, a 'downword' of the same size runs in the - * opposite direction. These two words can be in any memory that is - * read/write access for both the target and the debug host. A simple ping - * pong handshake protocol over these words allows up/down link - * communication. On the upside no additional integration is needed. On - * the downside it may be necessary to feed lwl with cycles to poll for - * changes in the downword, depending on the use case. - * - * Bit configuration - * ----------------- - * - * Downword (Host to target); - * - * A D U VV XXX - * - * A 31 1 - Service Active (Set by host) - * D 30 1 - Downsense (Toggled when there is data) - * U 29 1 - Upsense ack (Toggled to acknowledge receipt of uplink data) - * VV 28-27 2 - Valid Octets (Number of octets valid in the message) - * XXX 26-24 3 - Port in use (Type of the message) - * O2 23-16 8 - Octet 2 - * O1 15-08 8 - Octet 1 - * O0 07-00 8 - Octet 0 - * - * Upword (Target to Host); - * - * A 31 1 - Service Active (Set by device) - * D 30 1 - Downsense ack (Toggled to acknowledge receipt of downlink - * data) - * U 29 1 - Upsense (Toggled when there is data) - * VV 28-27 2 - Valid upword octets - * XXX 26-24 3 - Port in use (Type of the message) - * O2 23-16 8 - Octet 2 - * O1 15-08 8 - Octet 1 - * O0 07-00 8 - Octet 0 + * without any need for additional hardware. It works with OpenOCD and any + * other debugger that can read and write target memory while the CPU is + * running. See Documentation/guides/drivers/lwl.rst for the full + * description. * + * A 'downword' of 32 bits carries data from the host to the target and an + * 'upword' of the same size carries data in the opposite direction, with a + * ping-pong handshake on two 'sense' bits: + * + * Bit Downword (host -> target) Upword (target -> host) + * 31 A link active (host) A link active (target) + * 30 D downsense (new data) D downsense ack + * 29 U upsense ack U upsense (new data) + * 28-27 VV number of valid octets VV number of valid octets + * 26-24 XXX port (1 = console) XXX port (1 = console) + * 23-0 O2 O1 O0 data octets O2 O1 O0 data octets + * + * Hosts that support it can instead collect the output from a ring buffer + * in the control block, many bytes per debugger access: the target then + * reports the A and D bits in the 'status' word and the upword is unused. + * + * The driver is a lower half of the generic serial driver, so the console + * gets proper RX/TX buffering, termios processing, poll() and signals. + * It never busy-waits for the host: the link is serviced either from a + * "doorbell" interrupt that the host raises after every update or, when + * the doorbell is not configured, from a low rate watchdog timer. */ /* Protocol bits */ -#define LWL_GETACTIVE(x) (((x) & (1 << 31)) != 0) -#define LWL_ACTIVE(x) (((x)&1) << 31) +#define LWL_ACTIVEBIT (1u << 31) +#define LWL_GETACTIVE(x) (((x) & LWL_ACTIVEBIT) != 0) + +#define LWL_DNSENSEBIT (1u << 30) +#define LWL_DNSENSE(x) ((x) & LWL_DNSENSEBIT) +#define LWL_UPSENSEBIT (1u << 29) +#define LWL_UPSENSE(x) ((x) & LWL_UPSENSEBIT) +#define LWL_SENSEMASK (LWL_DNSENSEBIT | LWL_UPSENSEBIT) + +#define LWL_GETOCTVAL(x) (((x) >> 27) & 3) +#define LWL_OCTVAL(x) (((uint32_t)(x) & 3) << 27) +#define LWL_GETPORT(x) (((x) >> 24) & 7) +#define LWL_PORT(x) (((uint32_t)(x) & 7) << 24) +#define LWL_MAXOCTETS 3 + +#define LWL_PORT_CONSOLE 1 + +/* Host capabilities, written by the host before it opens the link */ + +#define LWL_CAP_RING (1u << 0) /* The host reads the ring buffer */ + +/* Signatures that let the host locate the control block in target RAM. + * LWL_EXT_SIG tells the host that the fields that follow the upword are + * valid (legacy firmware does not have them). + */ + +#define LWL_ID_SIG 0x7216a318 +#define LWL_EXT_SIG 0x4c574c32 /* "LWL2" */ + +/* Doorbell: the host writes 'dbval' to 'dbaddr' after an update of the + * link. The board chooses a register that the debugger can write while + * the CPU is running and whose write makes the doorbell IRQ pending, for + * instance an NVIC Interrupt Set-Pending Register on Cortex-M. The + * interrupt handler writes 0 to it, which clears level-type software + * interrupts (e.g. the RISC-V CLINT msip) and is ignored by set-pending + * registers. + */ + +#ifdef CONFIG_LWL_CONSOLE_DOORBELL +# define LWL_DBIRQ CONFIG_LWL_CONSOLE_DOORBELL_IRQ +# define LWL_DBADDR CONFIG_LWL_CONSOLE_DOORBELL_ADDR +# define LWL_DBVAL CONFIG_LWL_CONSOLE_DOORBELL_VALUE +# if LWL_DBADDR == 0 || LWL_DBVAL == 0 +# error "LWL doorbell needs CONFIG_LWL_CONSOLE_DOORBELL_ADDR and _VALUE" +# endif +#else +# define LWL_DBADDR 0 +# define LWL_DBVAL 0 +#endif + +/* Without the doorbell the link is polled */ + +#ifndef CONFIG_LWL_CONSOLE_DOORBELL +# define LWL_POLL 1 +# define LWL_POLLINTERVAL CONFIG_LWL_CONSOLE_POLL_INTERVAL +# define LWL_POLLTICKS MAX(USEC2TICK(LWL_POLLINTERVAL), 1) +#endif + +#define LWL_RINGSIZE CONFIG_LWL_CONSOLE_RINGSIZE + +#if LWL_RINGSIZE % 4 != 0 +# error "CONFIG_LWL_CONSOLE_RINGSIZE must be a multiple of 4" +#endif + +/* The debugger accesses memory behind the data cache. The fields written + * by the host and the ones written by the target are kept in different + * 64 byte groups, so cleaning a line never writes stale host data back. + */ + +#define LWL_GROUP 64 + +#ifdef CONFIG_ARCH_DCACHE +# define LWL_ALIGN aligned_data(LWL_GROUP) +#else +# define LWL_ALIGN +#endif + +/* Number of consecutive busy polls after which an output that must not + * block (up_putc() and writes from interrupt context) considers the host + * gone and starts dropping data instead of hanging the system. + */ + +#define LWL_STALL_SPINS 0x400000 -#define LWL_DNSENSEBIT (1 << 30) -#define LWL_DNSENSE(x) ((x)&LWL_DNSENSEBIT) -#define LWL_UPSENSEBIT (1 << 29) -#define LWL_UPSENSE(x) ((x)&LWL_UPSENSEBIT) -#define LWL_SENSEMASK (3 << 29) +/* Longest output line that is shown again when a host connects */ -#define LWL_GETOCTVAL(x) (((x) >> 27) & 3) -#define LWL_OCTVAL(x) (((x)&3) << 27) -#define LWL_GETPORT(x) (((x) >> 24) & 7) -#define LWL_PORT(x) (((x)&7) << 24) +#define LWL_LINESIZE 80 -#define LWL_PORT_CONSOLE 1 -#define LWL_ID_SIG 0x7216A318 +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* The control block shared with the host. The host relies on the offsets + * of the fields, so they never change. + */ + +struct LWL_ALIGN lwl_entry_s +{ + /* 0x00: written by the host, static target data and the legacy upword */ + + uint32_t sig; /* LWL_ID_SIG: location signature */ + volatile uint32_t downword; /* Host to target word */ + volatile uint32_t upword; /* Target to host word, word protocol only */ + uint32_t extsig; /* LWL_EXT_SIG: the fields below are valid */ + uint32_t dbaddr; /* Doorbell address, 0 if there is none */ + uint32_t dbval; /* Value the host writes to dbaddr */ + uint32_t ringsize; /* Size of ring[], 0 if there is none */ + uint32_t reserved0[9]; + + /* 0x40: written by the host */ + + volatile uint32_t hostcaps; /* LWL_CAP_* supported by the host */ + volatile uint32_t uptail; /* Next ring[] octet the host will read */ + uint32_t reserved1[14]; + + /* 0x80: written by the target */ + + volatile uint32_t status; /* A and D bits, ring protocol only */ + volatile uint32_t uphead; /* Next ring[] octet the target will write */ + uint32_t reserved2[14]; + + /* 0xc0: output ring buffer, written by the target */ + +#if LWL_RINGSIZE > 0 + uint8_t ring[LWL_RINGSIZE]; +#endif +}; + +struct lwl_priv_s +{ +#ifdef LWL_POLL + struct wdog_s wdog; /* Polling timer */ +#endif + uint32_t spins; /* Consecutive busy polls of the uplink */ + uint8_t rxoff; /* Octets of the downword already consumed */ + uint8_t linelen; /* Length of line[], > LWL_LINESIZE if lost */ + uint8_t replay; /* Octets of line[] already replayed */ + bool replayon; /* line[] is being sent to a new host */ + bool active; /* The link is open */ + bool usering; /* The host reads the ring buffer */ + bool attached; /* The serial device is open */ + bool rxint; /* "RX interrupt" enabled by the upper half */ + bool txint; /* "TX interrupt" enabled by the upper half */ + bool stalled; /* The host stopped collecting uplink data */ + + /* Last unterminated output line, e.g. the shell prompt */ + + char line[LWL_LINESIZE]; +}; /**************************************************************************** * Private Function Prototypes ****************************************************************************/ -static ssize_t lwlconsole_read(FAR struct file *filep, FAR char *buffer, - size_t buflen); -static ssize_t lwlconsole_write(FAR struct file *filep, - FAR const char *buffer, size_t buflen); +/* Serial lower half methods */ + +static int lwl_setup(FAR struct uart_dev_s *dev); +static void lwl_shutdown(FAR struct uart_dev_s *dev); +static int lwl_attach(FAR struct uart_dev_s *dev); +static void lwl_detach(FAR struct uart_dev_s *dev); +static int lwl_ioctl(FAR struct file *filep, int cmd, unsigned long arg); +static int lwl_receive(FAR struct uart_dev_s *dev, + FAR unsigned int *status); +static void lwl_rxint(FAR struct uart_dev_s *dev, bool enable); +static bool lwl_rxavailable(FAR struct uart_dev_s *dev); +static void lwl_send(FAR struct uart_dev_s *dev, int ch); +static void lwl_txint(FAR struct uart_dev_s *dev, bool enable); +static bool lwl_txready(FAR struct uart_dev_s *dev); +static bool lwl_txempty(FAR struct uart_dev_s *dev); +static ssize_t lwl_recvbuf(FAR struct uart_dev_s *dev, + FAR void *buf, size_t len); +static ssize_t lwl_sendbuf(FAR struct uart_dev_s *dev, + FAR const void *buf, size_t len); + +#ifdef LWL_POLL +static void lwl_poll(wdparm_t arg); +#endif /**************************************************************************** * Private Data ****************************************************************************/ -struct lwl_entry_s +static struct lwl_entry_s g_lwl = { - uint32_t sig; /* Location signature */ - volatile uint32_t downword; /* Host to Target word */ - uint32_t upword; /* Target to Host word */ + .sig = LWL_ID_SIG, + .extsig = LWL_EXT_SIG, + .dbaddr = LWL_DBADDR, + .dbval = LWL_DBVAL, + .ringsize = LWL_RINGSIZE, }; -static struct lwl_entry_s g_d = +static const struct uart_ops_s g_lwl_ops = { - LWL_ID_SIG + .setup = lwl_setup, + .shutdown = lwl_shutdown, + .attach = lwl_attach, + .detach = lwl_detach, + .ioctl = lwl_ioctl, + .receive = lwl_receive, + .rxint = lwl_rxint, + .rxavailable = lwl_rxavailable, + .send = lwl_send, + .txint = lwl_txint, + .txready = lwl_txready, + .txempty = lwl_txempty, + .recvbuf = lwl_recvbuf, + .sendbuf = lwl_sendbuf, }; -static const struct file_operations g_consoleops = +static char g_lwl_rxbuffer[CONFIG_LWL_CONSOLE_RXBUFSIZE]; +static char g_lwl_txbuffer[CONFIG_LWL_CONSOLE_TXBUFSIZE]; + +static struct lwl_priv_s g_lwl_priv; + +static struct uart_dev_s g_lwl_dev = { - NULL, /* open */ - NULL, /* close */ - lwlconsole_read, /* read */ - lwlconsole_write, /* write */ + .isconsole = true, + .recv = + { + .size = CONFIG_LWL_CONSOLE_RXBUFSIZE, + .buffer = g_lwl_rxbuffer, + }, + .xmit = + { + .size = CONFIG_LWL_CONSOLE_TXBUFSIZE, + .buffer = g_lwl_txbuffer, + }, + .ops = &g_lwl_ops, + .priv = &g_lwl_priv, }; /**************************************************************************** * Private Functions ****************************************************************************/ -static bool linkactive(void) +/**************************************************************************** + * Name: lwl_clean + * + * Description: + * Make target writes to the control block visible to the host. + * + ****************************************************************************/ + +static inline void lwl_clean(FAR volatile void *start, size_t len) +{ + up_clean_dcache((uintptr_t)start, (uintptr_t)start + len); +} + +/**************************************************************************** + * Name: lwl_getdown + * + * Description: + * Return the current downword, after making all the host writes visible. + * + ****************************************************************************/ + +static uint32_t lwl_getdown(void) { - return (LWL_GETACTIVE(g_d.downword) != 0); + up_invalidate_dcache((uintptr_t)&g_lwl, (uintptr_t)&g_lwl.status); + return g_lwl.downword; } -static bool writeword(uint32_t newupword) +/**************************************************************************** + * Name: lwl_getack / lwl_putack + * + * Description: + * Access the word holding the target's A and D bits: the status word for + * hosts that read the ring buffer, the upword for the others. + * + ****************************************************************************/ + +static inline uint32_t lwl_getack(FAR struct lwl_priv_s *priv) { - /* Check link is active */ + return priv->usering ? g_lwl.status : g_lwl.upword; +} - if (!linkactive()) +static void lwl_putack(FAR struct lwl_priv_s *priv, uint32_t word) +{ + if (priv->usering) { - return false; + g_lwl.status = word; + lwl_clean(&g_lwl.status, sizeof(g_lwl.status)); } + else + { + g_lwl.upword = word; + lwl_clean(&g_lwl.upword, sizeof(g_lwl.upword)); + } +} + +/**************************************************************************** + * Name: lwl_pollstart + * + * Description: + * (Re)start the polling timer while the device is open, if the link is + * polled (no doorbell). + * + ****************************************************************************/ - /* Spin waiting for previous data to be collected */ +static void lwl_pollstart(FAR struct uart_dev_s *dev) +{ +#ifdef LWL_POLL + FAR struct lwl_priv_s *priv = dev->priv; - while (LWL_UPSENSE(g_d.downword) != LWL_UPSENSE(g_d.upword)) + if (priv->attached && !WDOG_ISACTIVE(&priv->wdog)) { + wd_start(&priv->wdog, LWL_POLLTICKS, lwl_poll, (wdparm_t)dev); } +#endif +} - /* Load new data, toggling UPSENSE bit to show it is new */ +/**************************************************************************** + * Name: lwl_link + * + * Description: + * Track the link state requested by the host. When the host opens the + * link the target adopts the host's sense bits and empties the ring, so + * that a new host never sees stale data nor waits for an acknowledge. + * + * Returned Value: + * true if the link is active; the current downword is returned in *down. + * + ****************************************************************************/ + +static bool lwl_link(FAR struct uart_dev_s *dev, FAR uint32_t *down) +{ + FAR struct lwl_priv_s *priv = dev->priv; + uint32_t dn = lwl_getdown(); + + if (!LWL_GETACTIVE(dn)) + { + if (priv->active) + { + /* The host closed the link */ - g_d.upword = LWL_DNSENSE(g_d.upword) | newupword | - (LWL_UPSENSE(g_d.upword) ? 0 : LWL_UPSENSEBIT); + g_lwl.upword = 0; + g_lwl.status = 0; + lwl_clean(&g_lwl.upword, sizeof(g_lwl.upword)); + lwl_clean(&g_lwl.status, sizeof(g_lwl.status)); + priv->active = false; + priv->rxoff = 0; + priv->replayon = false; + } + + return false; + } + + if (!priv->active) + { + priv->active = true; + priv->usering = LWL_RINGSIZE > 0 && + (g_lwl.hostcaps & LWL_CAP_RING) != 0; + priv->rxoff = 0; + priv->spins = 0; + priv->stalled = false; + + if (priv->usering) + { + g_lwl.uphead = g_lwl.uptail % MAX(LWL_RINGSIZE, 1); + lwl_clean(&g_lwl.uphead, sizeof(g_lwl.uphead)); + lwl_putack(priv, LWL_ACTIVEBIT | LWL_DNSENSE(dn)); + } + else + { + lwl_putack(priv, LWL_ACTIVEBIT | (dn & LWL_SENSEMASK)); + } + + /* The new host has not seen the line in progress (typically the + * shell prompt), which was collected by a previous host or before + * the link went down: send it again. + */ + + priv->replay = 0; + priv->replayon = priv->linelen > 0 && priv->linelen <= LWL_LINESIZE; + } + + *down = dn; return true; } -static bool write8bits(uint8_t port, uint8_t val) +/**************************************************************************** + * Name: lwl_txroom + * + * Description: + * Return how many octets can be sent to the host now. + * + ****************************************************************************/ + +static size_t lwl_txroom(FAR struct lwl_priv_s *priv, uint32_t down) { - /* Prepare new word */ +#if LWL_RINGSIZE > 0 + if (priv->usering) + { + uint32_t tail = g_lwl.uptail % LWL_RINGSIZE; - uint32_t newupword = LWL_ACTIVE(true) | LWL_OCTVAL(1) | - LWL_PORT(port) | (val & 0xff); + return (tail + LWL_RINGSIZE - g_lwl.uphead - 1) % LWL_RINGSIZE; + } +#endif - return writeword(newupword); + return LWL_UPSENSE(down) == LWL_UPSENSE(g_lwl.upword) ? LWL_MAXOCTETS : 0; } -static bool write16bits(uint8_t port, uint32_t val) +/**************************************************************************** + * Name: lwl_put + * + * Description: + * Send octets to the host, there must be room for at least one. + * + * Returned Value: + * The number of octets sent. + * + ****************************************************************************/ + +static size_t lwl_put(FAR struct lwl_priv_s *priv, uint32_t down, + FAR const uint8_t *src, size_t len) { - /* Prepare new word */ + uint32_t upword; + size_t i; + +#if LWL_RINGSIZE > 0 + if (priv->usering) + { + uint32_t head = g_lwl.uphead; + + /* Copy the data first, then publish the new head */ - uint32_t newupword = LWL_ACTIVE(true) | LWL_OCTVAL(2) | - LWL_PORT(port) | (val & 0xffff); + len = MIN(len, MIN(lwl_txroom(priv, down), LWL_RINGSIZE - head)); + memcpy(&g_lwl.ring[head], src, len); + lwl_clean(&g_lwl.ring[head], len); - return writeword(newupword); + g_lwl.uphead = (head + len) % LWL_RINGSIZE; + lwl_clean(&g_lwl.uphead, sizeof(g_lwl.uphead)); + return len; + } +#endif + + len = MIN(len, LWL_MAXOCTETS); + upword = g_lwl.upword; + upword = LWL_ACTIVEBIT | LWL_OCTVAL(len) | LWL_PORT(LWL_PORT_CONSOLE) | + LWL_DNSENSE(upword) | (LWL_UPSENSE(upword) ^ LWL_UPSENSEBIT); + + for (i = 0; i < len; i++) + { + upword |= (uint32_t)src[i] << (8 * i); + } + + g_lwl.upword = upword; + lwl_clean(&g_lwl.upword, sizeof(g_lwl.upword)); + return len; } -static bool write24bits(uint8_t port, uint32_t val) +/**************************************************************************** + * Name: lwl_replay + * + * Description: + * Send the next octets of the line replayed to a new host. + * + ****************************************************************************/ + +static void lwl_replay(FAR struct lwl_priv_s *priv, uint32_t down) +{ + priv->replay += lwl_put(priv, down, + (FAR const uint8_t *)&priv->line[priv->replay], + priv->linelen - priv->replay); + priv->replayon = priv->replay < priv->linelen; +} + +/**************************************************************************** + * Name: lwl_rxack + * + * Description: + * Acknowledge the current downword, the host may then send the next one. + * + ****************************************************************************/ + +static void lwl_rxack(FAR struct lwl_priv_s *priv, uint32_t down) { - /* Prepare new word */ + priv->rxoff = 0; + lwl_putack(priv, (lwl_getack(priv) & ~LWL_DNSENSEBIT) | LWL_DNSENSE(down)); +} - uint32_t newupword = LWL_ACTIVE(true) | LWL_OCTVAL(3) | - LWL_PORT(port) | (val & 0xffffff); +/**************************************************************************** + * Name: lwl_rxpending + * + * Description: + * Return true if the downword holds data not acknowledged yet. + * + ****************************************************************************/ - return writeword(newupword); +static inline bool lwl_rxpending(FAR struct lwl_priv_s *priv, uint32_t down) +{ + return LWL_DNSENSE(down) != LWL_DNSENSE(lwl_getack(priv)); } -static bool read8bits(uint8_t port, FAR uint8_t *store) +/**************************************************************************** + * Name: lwl_service + * + * Description: + * Move data between the control block and the serial buffers. Called + * from the doorbell interrupt, from the polling timer and whenever the + * upper half enables the RX or TX "interrupts". The uart lock + * serializes it with the upper half and with up_putc(), and keeps the + * scheduler locked while uart_recvchars()/uart_xmitchars() wake up + * readers and writers. + * + ****************************************************************************/ + +static void lwl_service(FAR struct uart_dev_s *dev) { - if (!linkactive()) + FAR struct lwl_priv_s *priv = dev->priv; + irqstate_t flags; + uint32_t down; + + flags = uart_spinlock(dev, true); + + if (lwl_link(dev, &down)) { - return false; + if (priv->rxint) + { + uart_recvchars(dev); + } + + down = lwl_getdown(); + if (priv->replayon && lwl_txroom(priv, down) > 0) + { + lwl_replay(priv, down); + } + + if (!priv->replayon && priv->txint && lwl_txroom(priv, down) > 0) + { + uart_xmitchars(dev); + } + } + else if (priv->txint) + { + FAR struct uart_buffer_s *xmit = &dev->xmit; + int head = xmit->head + 1 < xmit->size ? xmit->head + 1 : 0; + + /* No host is attached. Keep the most recent output, so that it is + * shown when the host connects, but drop the oldest data when the + * buffer is full instead of blocking the writer forever. + */ + + if (head == xmit->tail) + { + xmit->tail = (xmit->tail + MAX(xmit->size / 4, 1)) % xmit->size; + uart_datasent(dev); + } + } + + uart_spinunlock(dev, true, flags); +} + +/**************************************************************************** + * Name: lwl_poll + * + * Description: + * Polling timer expiration. + * + ****************************************************************************/ + +#ifdef LWL_POLL +static void lwl_poll(wdparm_t arg) +{ + FAR struct uart_dev_s *dev = (FAR struct uart_dev_s *)arg; + + lwl_service(dev); + lwl_pollstart(dev); +} +#endif + +/**************************************************************************** + * Name: lwl_interrupt + * + * Description: + * Doorbell interrupt, raised by the host after it updated the link. + * + ****************************************************************************/ + +#ifdef CONFIG_LWL_CONSOLE_DOORBELL +static int lwl_interrupt(int irq, FAR void *context, FAR void *arg) +{ + FAR struct uart_dev_s *dev = arg; + + /* Acknowledge first, so that a new ring during the service is kept */ + + *(FAR volatile uint32_t *)LWL_DBADDR = 0; + + lwl_service(dev); + return OK; +} +#endif + +/**************************************************************************** + * Name: lwl_setup + ****************************************************************************/ + +static int lwl_setup(FAR struct uart_dev_s *dev) +{ + return OK; +} + +/**************************************************************************** + * Name: lwl_shutdown + ****************************************************************************/ + +static void lwl_shutdown(FAR struct uart_dev_s *dev) +{ +} + +/**************************************************************************** + * Name: lwl_attach + ****************************************************************************/ + +static int lwl_attach(FAR struct uart_dev_s *dev) +{ + FAR struct lwl_priv_s *priv = dev->priv; +#ifdef CONFIG_LWL_CONSOLE_DOORBELL + int ret; + + ret = irq_attach(LWL_DBIRQ, lwl_interrupt, dev); + if (ret < 0) + { + return ret; } - /* Spin waiting for a byte to be received */ + up_enable_irq(LWL_DBIRQ); +#endif - while (LWL_DNSENSE(g_d.downword) == LWL_DNSENSE(g_d.upword)) + priv->attached = true; + lwl_pollstart(dev); + return OK; +} + +/**************************************************************************** + * Name: lwl_detach + ****************************************************************************/ + +static void lwl_detach(FAR struct uart_dev_s *dev) +{ + FAR struct lwl_priv_s *priv = dev->priv; + +#ifdef CONFIG_LWL_CONSOLE_DOORBELL + up_disable_irq(LWL_DBIRQ); + irq_detach(LWL_DBIRQ); +#endif + + priv->attached = false; +#ifdef LWL_POLL + wd_cancel(&priv->wdog); +#endif +} + +/**************************************************************************** + * Name: lwl_ioctl + ****************************************************************************/ + +static int lwl_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + return -ENOTTY; +} + +/**************************************************************************** + * Name: lwl_recvbuf + * + * Description: + * Copy the unread octets of the current downword, acknowledging it when + * all of them have been consumed. + * + ****************************************************************************/ + +static ssize_t lwl_recvbuf(FAR struct uart_dev_s *dev, + FAR void *buf, size_t len) +{ + FAR struct lwl_priv_s *priv = dev->priv; + FAR uint8_t *dest = buf; + uint32_t down; + size_t nbytes = 0; + + if (lwl_link(dev, &down) && lwl_rxpending(priv, down)) { + while (nbytes < len && priv->rxoff < LWL_GETOCTVAL(down)) + { + dest[nbytes++] = (uint8_t)(down >> (8 * priv->rxoff++)); + } + + if (priv->rxoff >= LWL_GETOCTVAL(down)) + { + lwl_rxack(priv, down); + } } - *store = g_d.downword & 255; + return nbytes; +} - /* Flip the bit to indicate the datum is read */ +/**************************************************************************** + * Name: lwl_receive + ****************************************************************************/ - g_d.upword = (g_d.upword & ~LWL_DNSENSEBIT) | LWL_DNSENSE(g_d.downword); +static int lwl_receive(FAR struct uart_dev_s *dev, FAR unsigned int *status) +{ + uint8_t ch = 0; - return true; + *status = lwl_recvbuf(dev, &ch, 1) == 1 ? 0 : -EAGAIN; + return ch; } /**************************************************************************** - * Name: lwlconsole_read + * Name: lwl_rxint ****************************************************************************/ -static ssize_t lwlconsole_read(FAR struct file *filep, FAR char *buffer, - size_t buflen) +static void lwl_rxint(FAR struct uart_dev_s *dev, bool enable) { - if (buflen == 0) + FAR struct lwl_priv_s *priv = dev->priv; + + priv->rxint = enable; + if (enable) { - return 0; + /* Data may be waiting because the RX buffer was full */ + + lwl_service(dev); } +} - if (!read8bits(LWL_PORT_CONSOLE, (FAR uint8_t *) buffer)) +/**************************************************************************** + * Name: lwl_rxavailable + * + * Description: + * Return true if there is downlink data that can be stored. While the RX + * buffer is full the downword is left unacknowledged, which stops the + * host until the application reads, so no input is ever lost. + * + ****************************************************************************/ + +static bool lwl_rxavailable(FAR struct uart_dev_s *dev) +{ + FAR struct lwl_priv_s *priv = dev->priv; + FAR struct uart_buffer_s *recv = &dev->recv; + uint32_t down; + int head; + + if (!lwl_link(dev, &down) || !lwl_rxpending(priv, down)) { - return 0; + return false; + } + + if (LWL_GETPORT(down) != LWL_PORT_CONSOLE || + priv->rxoff >= LWL_GETOCTVAL(down)) + { + /* Nothing for us in this word, just let the host go on */ + + lwl_rxack(priv, down); + return false; } - return 1; + head = recv->head + 1 < recv->size ? recv->head + 1 : 0; + return head != recv->tail; } /**************************************************************************** - * Name: lwlconsole_write + * Name: lwl_sendbuf + * + * Description: + * Send octets to the host. The data is dropped if there is no host to + * collect it. + * ****************************************************************************/ -static ssize_t lwlconsole_write(FAR struct file *filep, - FAR const char *buffer, size_t buflen) +static ssize_t lwl_sendbuf(FAR struct uart_dev_s *dev, + FAR const void *buf, size_t len) { - uint32_t oc = 0; + FAR struct lwl_priv_s *priv = dev->priv; + FAR const uint8_t *src = buf; + uint32_t down; + size_t i; + + if (!lwl_link(dev, &down) || lwl_txroom(priv, down) == 0) + { + return len; + } + + /* The line replayed to a new host goes first */ + + if (priv->replayon) + { + lwl_replay(priv, down); + return 0; + } + + len = lwl_put(priv, down, src, len); + + /* Remember the line in progress, to replay it to the next host */ - while (buflen) + for (i = 0; i < len; i++) { - switch (buflen) + if (src[i] == '\n') + { + priv->linelen = 0; + } + else if (priv->linelen < LWL_LINESIZE) { - case 0: - return oc; - - case 1: - if (write8bits(LWL_PORT_CONSOLE, buffer[0])) - { - oc++; - buffer++; - buflen--; - } - break; - - case 2: - if (write16bits(LWL_PORT_CONSOLE, buffer[0] | (buffer[1] << 8))) - { - oc += 2; - buffer += 2; - buflen -= 2; - } - break; - - default: - if (write24bits(LWL_PORT_CONSOLE, buffer[0] | - (buffer[1] << 8) | (buffer[2] << 16))) - { - oc += 3; - buffer += 3; - buflen -= 3; - } - break; + priv->line[priv->linelen++] = src[i]; + } + else + { + priv->linelen = LWL_LINESIZE + 1; } } - return oc; + return len; +} + +/**************************************************************************** + * Name: lwl_send + ****************************************************************************/ + +static void lwl_send(FAR struct uart_dev_s *dev, int ch) +{ + uint8_t c = ch; + + lwl_sendbuf(dev, &c, 1); +} + +/**************************************************************************** + * Name: lwl_txint + ****************************************************************************/ + +static void lwl_txint(FAR struct uart_dev_s *dev, bool enable) +{ + FAR struct lwl_priv_s *priv = dev->priv; + + priv->txint = enable; + if (enable) + { + lwl_service(dev); + } +} + +/**************************************************************************** + * Name: lwl_txready + * + * Description: + * Return true if lwl_sendbuf() can be called: there is room for the data, + * or there is no host and the data will be dropped. + * + ****************************************************************************/ + +static bool lwl_txready(FAR struct uart_dev_s *dev) +{ + FAR struct lwl_priv_s *priv = dev->priv; + uint32_t down; + + if (!lwl_link(dev, &down) || lwl_txroom(priv, down) > 0) + { + priv->spins = 0; + priv->stalled = false; + return true; + } + + /* Writers that cannot sleep (up_putc() and writes from interrupt + * context) spin here. Don't let a vanished host hang them forever. + */ + + if (!priv->stalled && ++priv->spins >= LWL_STALL_SPINS) + { + priv->stalled = true; + } + + return priv->stalled; +} + +/**************************************************************************** + * Name: lwl_txempty + ****************************************************************************/ + +static bool lwl_txempty(FAR struct uart_dev_s *dev) +{ + FAR struct lwl_priv_s *priv = dev->priv; + uint32_t down; + + if (!lwl_link(dev, &down)) + { + return true; + } + +#if LWL_RINGSIZE > 0 + if (priv->usering) + { + return lwl_txroom(priv, down) == LWL_RINGSIZE - 1; + } +#endif + + return lwl_txroom(priv, down) > 0; } /**************************************************************************** @@ -296,25 +981,40 @@ static ssize_t lwlconsole_write(FAR struct file *filep, /**************************************************************************** * Name: up_putc + * + * Description: + * Low-level, unbuffered console output used by syslog and by the assert + * logic. It waits for room in the link. + * ****************************************************************************/ void up_putc(int ch) { - /* If link not initialize just return */ + irqstate_t flags; + uint8_t c = ch; + + flags = uart_spinlock(&g_lwl_dev, false); - if (!linkactive()) + do { - return; + while (!lwl_txready(&g_lwl_dev)) + { + } } + while (lwl_sendbuf(&g_lwl_dev, &c, 1) == 0); - write8bits(LWL_PORT_CONSOLE, (uint8_t)ch); + uart_spinunlock(&g_lwl_dev, false, flags); } /**************************************************************************** * Name: lwlconsole_init + * + * Description: + * Register the LWL console as /dev/console. + * ****************************************************************************/ void lwlconsole_init(void) { - register_driver("/dev/console", &g_consoleops, 0620, NULL); + uart_register("/dev/console", &g_lwl_dev); }
