Gary-Hobson commented on code in PR #7554:
URL: https://github.com/apache/incubator-nuttx/pull/7554#discussion_r1028005202


##########
drivers/segger/console_rtt.c:
##########
@@ -0,0 +1,552 @@
+/****************************************************************************
+ * 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 <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_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);
+#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_SERIAL1) || defined(CONFIG_RTT_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_CONSOLE
+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,
+  .rxavailable = rtt_rxavailable,
+  .send        = rtt_send,
+  .txint       = rtt_txint,
+  .txready     = rtt_txready,
+  .txempty     = rtt_txempty,

Review Comment:
   done~



##########
drivers/segger/console_rtt.c:
##########
@@ -0,0 +1,552 @@
+/****************************************************************************
+ * 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 <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_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);
+#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_SERIAL1) || defined(CONFIG_RTT_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_CONSOLE
+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,
+  .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_CONSOLE_TXBUF_SIZE,
+      .buffer = g_console_txbuf,
+    },
+  .recv =
+    {
+      .size   = CONFIG_RTT_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

Review Comment:
   It‘s in rtt_serial_initialize



##########
drivers/segger/Kconfig:
##########
@@ -79,3 +81,76 @@ config SEGGER_SYSVIEW_RAM_BASE
 endif
 
 endif
+
+config RTT_SERIAL
+       bool
+       select SEGGER_RTT
+
+config RTT_CONSOLE

Review Comment:
   done



##########
drivers/segger/console_rtt.c:
##########
@@ -0,0 +1,552 @@
+/****************************************************************************
+ * 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 <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_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);
+#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_SERIAL1) || defined(CONFIG_RTT_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_CONSOLE
+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,
+  .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_CONSOLE_TXBUF_SIZE,
+      .buffer = g_console_txbuf,
+    },
+  .recv =
+    {
+      .size   = CONFIG_RTT_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_CONSOLE
+
+/****************************************************************************
+ * Name: rtt_setup
+ *
+ * Description:
+ *   Configure the UART baud, bits, parity, fifos, etc. This
+ *   method is called the first time that the serial port is
+ *   opened.
+ *
+ ****************************************************************************/
+
+static int rtt_setup(FAR struct uart_dev_s *dev)
+{
+  return OK;
+}
+
+/****************************************************************************
+ * Name: rtt_shutdown
+ *
+ * Description:
+ *   Disable the UART.  This method is called when the serial
+ *   port is closed
+ *
+ ****************************************************************************/
+
+static void rtt_shutdown(FAR struct uart_dev_s *dev)
+{
+}
+
+/****************************************************************************
+ * Name: rtt_attach
+ *
+ * Description:
+ *   Configure the UART to operation in interrupt driven mode.  This method
+ *   is called when the serial port is opened.  Normally, this is just after
+ *   the setup() method is called, however, the serial console may operate in
+ *   a non-interrupt driven mode during the boot phase.
+ *
+ *   RX and TX interrupts are not enabled when by the attach method (unless
+ *   the hardware supports multiple levels of interrupt enabling).  The RX
+ *   and TX interrupts are not enabled until the txint() and rxint() methods
+ *   are called.
+ *
+ ****************************************************************************/
+
+static int rtt_attach(FAR struct uart_dev_s *dev)
+{
+  return OK;
+}
+
+/****************************************************************************
+ * Name: rtt_detach
+ *
+ * Description:
+ *   Detach UART interrupts.  This method is called when the serial port is
+ *   closed normally just before the shutdown method is called.  The
+ *   exception is the serial console which is never shutdown.
+ *
+ ****************************************************************************/
+
+static void rtt_detach(FAR struct uart_dev_s *dev)
+{
+}
+
+/****************************************************************************
+ * Name: rtt_ioctl
+ *
+ * Description:
+ *   All ioctl calls will be routed through this method
+ *
+ ****************************************************************************/
+
+static int rtt_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
+{
+#ifdef CONFIG_SERIAL_TERMIOS
+  FAR struct inode *inode      = filep->f_inode;
+  FAR struct uart_dev_s *dev   = inode->i_private;
+  FAR struct rtt_priv_s *priv  = dev->priv;
+  FAR struct termios *termiosp = (struct termios *)(uintptr_t)arg;
+
+  if (!termiosp)
+    {
+      return -EINVAL;
+    }
+
+  switch (cmd)
+    {
+    case TCGETS:
+      return simuart_getcflag(dev->isconsole ? 0 : priv->channel,
+                              &termiosp->c_cflag);
+    case TCSETS:
+      return simuart_setcflag(dev->isconsole ? 0 : priv->channel,
+                              termiosp->c_cflag);
+    }
+#endif
+
+  return -ENOTTY;
+}
+
+/****************************************************************************
+ * Name: rtt_receive
+ *
+ * Description:
+ *   Called (usually) from the interrupt level to receive one
+ *   character from the UART.  Error bits associated with the
+ *   receipt are provided in the return 'status'.
+ *
+ ****************************************************************************/
+
+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
+ *
+ * Description:
+ *   Call to enable or disable RX interrupts
+ *
+ ****************************************************************************/
+
+static void rtt_rxint(FAR struct uart_dev_s *dev, bool enable)
+{
+}
+
+/****************************************************************************
+ * Name: rtt_rxavailable
+ *
+ * Description:
+ *   Return true if the receive fifo is not empty
+ *
+ ****************************************************************************/
+
+static bool rtt_rxavailable(FAR struct uart_dev_s *dev)
+{
+  struct rtt_serial_dev_s *priv = dev->priv;
+
+  return !!SEGGER_RTT_HasData(priv->channel);
+}
+
+/****************************************************************************
+ * Name: rtt_send
+ *
+ * Description:
+ *   This method will send one byte on the UART
+ *
+ ****************************************************************************/
+
+static void rtt_send(FAR struct uart_dev_s *dev, int ch)
+{
+  struct rtt_serial_dev_s *priv = dev->priv;
+
+  /* For console device */
+
+  SEGGER_RTT_PutChar(priv->channel, ch);
+}
+
+/****************************************************************************
+ * Name: rtt_txint
+ *
+ * Description:
+ *   Call to enable or disable TX interrupts
+ *
+ ****************************************************************************/
+
+static void rtt_txint(FAR struct uart_dev_s *dev, bool enable)
+{
+  if (enable)
+    {
+      uart_xmitchars(dev);
+    }
+}
+
+/****************************************************************************
+ * Name: uart_txready
+ *
+ * Description:
+ *   Return true if the tranmsit fifo is not full
+ *
+ ****************************************************************************/
+
+static bool rtt_txready(FAR struct uart_dev_s *dev)
+{
+  return true;
+}
+
+/****************************************************************************
+ * Name: rtt_txempty
+ *
+ * Description:
+ *   Return true if the transmit fifo is empty
+ *
+ ****************************************************************************/
+
+static bool rtt_txempty(FAR struct uart_dev_s *dev)
+{
+  return true;
+}
+
+/****************************************************************************
+ * Name: up_uartloop
+ ****************************************************************************/
+
+static int rtt_uartloop(int argc, FAR char **argv)
+{
+  while (1)
+    {
+      uart_recvchars(&g_rtt_console);
+      usleep(USEC_PER_TICK);
+    }
+
+  return 0;
+}
+
+#endif /* CONFIG_RTT_CONSOLE */
+
+#ifdef CONFIG_RTT_SERIAL
+
+/****************************************************************************
+ * Name: rtt_serial_ioctl
+ ****************************************************************************/
+
+static int rtt_serial_open(FAR struct file *filep)
+{
+  return 0;
+}
+
+/****************************************************************************
+ * Name: rtt_serial_ioctl
+ ****************************************************************************/
+
+static int rtt_serial_close(FAR struct file *filep)
+{
+  return 0;
+}
+
+/****************************************************************************
+ * Name: rtt_serial_read
+ ****************************************************************************/
+
+static ssize_t rtt_serial_read(FAR struct file *filep, FAR char *buffer,

Review Comment:
   RTT has a buffer inside, and the serial framework also needs to define a 
buffer, and more buffers will be needed
   Performance is better when separated, and requires less memory



##########
drivers/segger/console_rtt.c:
##########
@@ -0,0 +1,552 @@
+/****************************************************************************
+ * 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 <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_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);
+#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_SERIAL1) || defined(CONFIG_RTT_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_CONSOLE
+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,
+  .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_CONSOLE_TXBUF_SIZE,
+      .buffer = g_console_txbuf,
+    },
+  .recv =
+    {
+      .size   = CONFIG_RTT_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_CONSOLE
+
+/****************************************************************************
+ * Name: rtt_setup
+ *
+ * Description:
+ *   Configure the UART baud, bits, parity, fifos, etc. This
+ *   method is called the first time that the serial port is
+ *   opened.
+ *
+ ****************************************************************************/
+
+static int rtt_setup(FAR struct uart_dev_s *dev)
+{
+  return OK;
+}
+
+/****************************************************************************
+ * Name: rtt_shutdown
+ *
+ * Description:
+ *   Disable the UART.  This method is called when the serial
+ *   port is closed
+ *
+ ****************************************************************************/
+
+static void rtt_shutdown(FAR struct uart_dev_s *dev)
+{
+}
+
+/****************************************************************************
+ * Name: rtt_attach
+ *
+ * Description:
+ *   Configure the UART to operation in interrupt driven mode.  This method
+ *   is called when the serial port is opened.  Normally, this is just after
+ *   the setup() method is called, however, the serial console may operate in
+ *   a non-interrupt driven mode during the boot phase.
+ *
+ *   RX and TX interrupts are not enabled when by the attach method (unless
+ *   the hardware supports multiple levels of interrupt enabling).  The RX
+ *   and TX interrupts are not enabled until the txint() and rxint() methods
+ *   are called.
+ *
+ ****************************************************************************/
+
+static int rtt_attach(FAR struct uart_dev_s *dev)
+{
+  return OK;
+}
+
+/****************************************************************************
+ * Name: rtt_detach
+ *
+ * Description:
+ *   Detach UART interrupts.  This method is called when the serial port is
+ *   closed normally just before the shutdown method is called.  The
+ *   exception is the serial console which is never shutdown.
+ *
+ ****************************************************************************/
+
+static void rtt_detach(FAR struct uart_dev_s *dev)
+{
+}
+
+/****************************************************************************
+ * Name: rtt_ioctl
+ *
+ * Description:
+ *   All ioctl calls will be routed through this method
+ *
+ ****************************************************************************/
+
+static int rtt_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
+{
+#ifdef CONFIG_SERIAL_TERMIOS
+  FAR struct inode *inode      = filep->f_inode;
+  FAR struct uart_dev_s *dev   = inode->i_private;
+  FAR struct rtt_priv_s *priv  = dev->priv;
+  FAR struct termios *termiosp = (struct termios *)(uintptr_t)arg;
+
+  if (!termiosp)
+    {
+      return -EINVAL;
+    }
+
+  switch (cmd)
+    {
+    case TCGETS:
+      return simuart_getcflag(dev->isconsole ? 0 : priv->channel,

Review Comment:
   yes it doesn't need



-- 
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

Reply via email to