Gary-Hobson commented on code in PR #7554: URL: https://github.com/apache/incubator-nuttx/pull/7554#discussion_r1029599377
########## drivers/segger/console_rtt.c: ########## @@ -0,0 +1,328 @@ +/**************************************************************************** + * drivers/segger/console_rtt.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <assert.h> +#include <debug.h> +#include <errno.h> +#include <fcntl.h> +#include <stdint.h> + +#include <nuttx/config.h> +#include <nuttx/fs/ioctl.h> +#include <nuttx/kmalloc.h> +#include <nuttx/kthread.h> +#include <nuttx/serial/serial.h> + +#include <sys/types.h> + +#include <SEGGER_RTT.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rtt_setup(FAR struct uart_dev_s *dev); +static void rtt_shutdown(FAR struct uart_dev_s *dev); +static int rtt_attach(FAR struct uart_dev_s *dev); +static void rtt_detach(FAR struct uart_dev_s *dev); +static int rtt_ioctl(FAR struct file *filep, int cmd, unsigned long arg); +static int rtt_receive(FAR struct uart_dev_s *dev, unsigned int *status); +static void rtt_rxint(FAR struct uart_dev_s *dev, bool enable); +static bool rtt_rxavailable(FAR struct uart_dev_s *dev); +static void rtt_send(FAR struct uart_dev_s *dev, int ch); +static void rtt_txint(FAR struct uart_dev_s *dev, bool enable); +static bool rtt_txready(FAR struct uart_dev_s *dev); +static bool rtt_txempty(FAR struct uart_dev_s *dev); +static void rtt_dmasend(FAR struct uart_dev_s *dev); +static void rtt_dmareceive(FAR struct uart_dev_s *dev); +static void rtt_dmarxfree(FAR struct uart_dev_s *dev); +static void rtt_dmatxavail(FAR struct uart_dev_s *dev); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static char g_console_rxbuf[CONFIG_RTT_CONSOLE_RXBUF_SIZE]; +static char g_console_txbuf[CONFIG_RTT_CONSOLE_TXBUF_SIZE]; + +static const struct uart_ops_s g_rtt_console_ops = +{ + .setup = rtt_setup, + .shutdown = rtt_shutdown, + .attach = rtt_attach, + .detach = rtt_detach, + .ioctl = rtt_ioctl, + .receive = rtt_receive, + .rxint = rtt_rxint, + .dmasend = rtt_dmasend, + .dmareceive = rtt_dmareceive, + .dmarxfree = rtt_dmarxfree, + .dmatxavail = rtt_dmatxavail, + .rxavailable = rtt_rxavailable, + .send = rtt_send, + .txint = rtt_txint, + .txready = rtt_txready, + .txempty = rtt_txempty, +}; + +static struct uart_dev_s g_rtt_console = +{ + .isconsole = true, + .ops = &g_rtt_console_ops, + .priv = ((void *) 0), + .xmit = + { + .size = CONFIG_RTT_CONSOLE_TXBUF_SIZE, + .buffer = g_console_txbuf, + }, + .recv = + { + .size = CONFIG_RTT_CONSOLE_RXBUF_SIZE, + .buffer = g_console_rxbuf, + }, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtt_setup + ****************************************************************************/ + +static int rtt_setup(FAR struct uart_dev_s *dev) +{ + return OK; +} + +/**************************************************************************** + * Name: rtt_shutdown + ****************************************************************************/ + +static void rtt_shutdown(FAR struct uart_dev_s *dev) +{ +} + +/**************************************************************************** + * Name: rtt_attach + ****************************************************************************/ + +static int rtt_attach(FAR struct uart_dev_s *dev) +{ + return OK; +} + +/**************************************************************************** + * Name: rtt_detach + ****************************************************************************/ + +static void rtt_detach(FAR struct uart_dev_s *dev) +{ +} + +/**************************************************************************** + * Name: rtt_ioctl + ****************************************************************************/ + +static int rtt_ioctl(FAR struct file *filep, int cmd, unsigned long arg) +{ + return -ENOTTY; +} + +/**************************************************************************** + * Name: rtt_receive + ****************************************************************************/ + +static int rtt_receive(FAR struct uart_dev_s *dev, unsigned int *status) +{ + int ch = 0; + int channel = (int) dev->priv; + + *status = 0; + SEGGER_RTT_ReadNoLock(channel, &ch, 1); + return ch; +} + +/**************************************************************************** + * Name: rtt_rxint + ****************************************************************************/ + +static void rtt_rxint(FAR struct uart_dev_s *dev, bool enable) +{ +} + +/**************************************************************************** + * Name: rtt_dmasend + ****************************************************************************/ + +static void rtt_dmasend(FAR struct uart_dev_s *dev) +{ + int channel = (int) dev->priv; + FAR struct uart_dmaxfer_s *xfer = &dev->dmatx; + size_t len = xfer->length + xfer->nlength; + + if (len > xfer->length) + { + SEGGER_RTT_Write(channel, xfer->buffer, xfer->length); + SEGGER_RTT_Write(channel, xfer->nbuffer, len - xfer->length); + } + else + { + SEGGER_RTT_Write(channel, xfer->buffer, len); + } + + xfer->nbytes = len; + uart_xmitchars_done(dev); +} + +/**************************************************************************** + * Name: rtt_dmasend + ****************************************************************************/ + +static void rtt_dmareceive(FAR struct uart_dev_s *dev) +{ + int channel = (int) dev->priv; + FAR struct uart_dmaxfer_s *xfer = &dev->dmarx; + uint32_t len = SEGGER_RTT_HasData(channel); + size_t space = xfer->length + xfer->nlength; + + if (len > space) + { + len = space; + } + + if (len > xfer->length) + { + SEGGER_RTT_Read(channel, xfer->buffer, xfer->length); + SEGGER_RTT_Read(channel, xfer->nbuffer, len - xfer->length); + } + else + { + SEGGER_RTT_Read(channel, xfer->buffer, len); + } + + xfer->nbytes = len; + uart_recvchars_done(dev); +} + +/**************************************************************************** + * Name: rtt_dmasend + ****************************************************************************/ + +static void rtt_dmarxfree(FAR struct uart_dev_s *dev) +{ +} + +/**************************************************************************** + * Name: rtt_dmatxavail + ****************************************************************************/ + +static void rtt_dmatxavail(FAR struct uart_dev_s *dev) +{ + uart_xmitchars_dma(dev); +} + +/**************************************************************************** + * Name: rtt_rxavailable + ****************************************************************************/ + +static bool rtt_rxavailable(FAR struct uart_dev_s *dev) +{ + int channel = (int) dev->priv; + + return !!SEGGER_RTT_HasData(channel); +} + +/**************************************************************************** + * Name: rtt_send + ****************************************************************************/ + +static void rtt_send(FAR struct uart_dev_s *dev, int ch) +{ + int channel = (int) dev->priv; + + SEGGER_RTT_PutChar(channel, ch); +} + +/**************************************************************************** + * Name: rtt_txint + ****************************************************************************/ + +static void rtt_txint(FAR struct uart_dev_s *dev, bool enable) +{ +} + +/**************************************************************************** + * Name: uart_txready + ****************************************************************************/ + +static bool rtt_txready(FAR struct uart_dev_s *dev) +{ + return true; +} + +/**************************************************************************** + * Name: rtt_txempty + ****************************************************************************/ + +static bool rtt_txempty(FAR struct uart_dev_s *dev) +{ + return true; +} + +/**************************************************************************** + * Name: rtt_uartloop + ****************************************************************************/ + +static int rtt_uartloop(int argc, FAR char *argv[]) +{ + while (1) + { + if (SEGGER_RTT_HasKey()) + { + uart_recvchars_dma(&g_rtt_console); + } + else + { + usleep(USEC_PER_TICK); Review Comment: The default value of CONFIG USEC_PER_TICK is 10ms, Is it still small? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org