Gary-Hobson commented on code in PR #7554: URL: https://github.com/apache/incubator-nuttx/pull/7554#discussion_r1029161663
########## drivers/segger/serial_rtt.c: ########## @@ -0,0 +1,550 @@ +/**************************************************************************** + * drivers/segger/serial_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 <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> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct rtt_serial_dev_s +{ + int channel; + uint32_t rxsize; + uint32_t txsize; + char *devname; +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_RTT_SERIAL_CONSOLE +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); +#endif + +#ifdef CONFIG_RTT_SERIAL +static int rtt_serial_open(FAR struct file *filep); +static int rtt_serial_close(FAR struct file *filep); +static ssize_t rtt_serial_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); +static ssize_t rtt_serial_write(FAR struct file *filep, + FAR const char *buffer, size_t buflen); +static int rtt_serial_ioctl(FAR struct file *filep, int cmd, + unsigned long arg); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct rtt_serial_dev_s g_serial_priv[] = +{ +#if defined(CONFIG_RTT_SERIAL0) || defined(CONFIG_RTT_SERIAL_CONSOLE) + { + .channel = 0, + .devname = "/dev/rtt0", + }, +#endif +#if defined(CONFIG_RTT_SERIAL1) + { + .channel = 1, + .devname = "/dev/rtt1", + .rxsize = CONFIG_RTT_SERIAL1_RXBUF_SIZE, + .txsize = CONFIG_RTT_SERIAL1_TXBUF_SIZE + }, +#endif +#if defined(CONFIG_RTT_SERIAL2) + { + .channel = 2, + .devname = "/dev/rtt2", + .rxsize = CONFIG_RTT_SERIAL2_RXBUF_SIZE, + .txsize = CONFIG_RTT_SERIAL2_TXBUF_SIZE + }, +#endif +}; + +#ifdef CONFIG_RTT_SERIAL_CONSOLE +static char g_console_rxbuf[CONFIG_RTT_SERIAL_CONSOLE_RXBUF_SIZE]; +static char g_console_txbuf[CONFIG_RTT_SERIAL_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 = &g_serial_priv, + .xmit = + { + .size = CONFIG_RTT_SERIAL_CONSOLE_TXBUF_SIZE, + .buffer = g_console_txbuf, + }, + .recv = + { + .size = CONFIG_RTT_SERIAL_CONSOLE_RXBUF_SIZE, + .buffer = g_console_rxbuf, + }, +}; +#endif + +#ifdef CONFIG_RTT_SERIAL +static const struct file_operations g_rtt_serial_ops = +{ + rtt_serial_open, /* open */ + rtt_serial_close, /* close */ + rtt_serial_read, /* read */ + rtt_serial_write, /* write */ + NULL, /* seek */ + rtt_serial_ioctl, /* ioctl */ + NULL /* poll */ +#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS + , NULL /* unlink */ +#endif +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifdef CONFIG_RTT_SERIAL_CONSOLE + +/**************************************************************************** + * 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; + FAR struct rtt_serial_dev_s *priv = dev->priv; + + *status = 0; + SEGGER_RTT_ReadNoLock(priv->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) +{ + FAR struct uart_dmaxfer_s *xfer = &dev->dmatx; + FAR struct rtt_serial_dev_s *priv = dev->priv; + size_t len = xfer->length + xfer->nlength; + + if (len > xfer->length) + { + SEGGER_RTT_Write(priv->channel, xfer->buffer, xfer->length); + SEGGER_RTT_Write(priv->channel, xfer->nbuffer, len - xfer->length); + } + else + { + SEGGER_RTT_Write(priv->channel, xfer->buffer, len); + } + + xfer->nbytes = len; + uart_xmitchars_done(dev); +} + +/**************************************************************************** + * Name: rtt_dmasend + ****************************************************************************/ + +static void rtt_dmareceive(FAR struct uart_dev_s *dev) +{ + FAR struct rtt_serial_dev_s *priv = dev->priv; + FAR struct uart_dmaxfer_s *xfer = &dev->dmarx; Review Comment: Alignment of equal signs is recommended in nuttx's C Coding Standard https://nuttx.apache.org/docs/latest/contributing/coding_style.html#:~:text=Columnar%20Organization -- 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