xiaoxiang781216 commented on code in PR #7554: URL: https://github.com/apache/incubator-nuttx/pull/7554#discussion_r1029475341
########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE +config RTT_CONSOLE_TXBUF_SIZE + int "rtt console tx buffer size" + default 256 + ---help--- + Configure the rtt console tx buffer size + +config RTT_CONSOLE_RXBUF_SIZE + int "rtt console rx buffer size" + default 256 + ---help--- + Configure the rtt console rx buffer size +endif Review Comment: let's reuse SEGGER_RTT_BUFFER_SIZE_UP/SEGGER_RTT_BUFFER_SIZE_DOWN ########## drivers/segger/serial_rtt.c: ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * 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 <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 <sys/types.h> + +#include <SEGGER_RTT.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +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); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +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 +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtt_serial_open + ****************************************************************************/ + +static int rtt_serial_open(FAR struct file *filep) +{ + return 0; +} + +/**************************************************************************** + * Name: rtt_serial_close + ****************************************************************************/ + +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, + size_t buflen) +{ + int channel; + ssize_t res = 0; + FAR struct inode *inode; + + DEBUGASSERT(filep != NULL && filep->f_inode != NULL); + inode = filep->f_inode; + channel = (int)inode->i_private; + + res = SEGGER_RTT_Read(channel, buffer + res, buflen); Review Comment: ```suggestion return SEGGER_RTT_Read(channel, buffer, buflen); ``` ########## drivers/segger/serial_rtt.c: ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * 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 <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 <sys/types.h> + +#include <SEGGER_RTT.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +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); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +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 +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtt_serial_open + ****************************************************************************/ + +static int rtt_serial_open(FAR struct file *filep) +{ + return 0; +} + +/**************************************************************************** + * Name: rtt_serial_close + ****************************************************************************/ + +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, + size_t buflen) +{ + int channel; + ssize_t res = 0; + FAR struct inode *inode; + + DEBUGASSERT(filep != NULL && filep->f_inode != NULL); + inode = filep->f_inode; + channel = (int)inode->i_private; + + res = SEGGER_RTT_Read(channel, buffer + res, buflen); Review Comment: need read until get some data in NONBLOCK case ########## 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); Review Comment: chaange rtt_xxx to console_rtt_xxx ########## 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]; Review Comment: g_console_-> g_console_rtt_ ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE +config RTT_CONSOLE_TXBUF_SIZE + int "rtt console tx buffer size" Review Comment: change all rtt->RTT ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE +config RTT_CONSOLE_TXBUF_SIZE + int "rtt console tx buffer size" + default 256 + ---help--- + Configure the rtt console tx buffer size + +config RTT_CONSOLE_RXBUF_SIZE + int "rtt console rx buffer size" Review Comment: rtt->RTT ########## 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[]) Review Comment: console_rtt_loop ########## 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> Review Comment: remove all unused inclusion: #include <nuttx/kthread.h> #include <nuttx/serial/serial.h> #include <SEGGER_RTT.h> ########## drivers/segger/serial_rtt.c: ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * 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 <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 <sys/types.h> + +#include <SEGGER_RTT.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static int rtt_serial_open(FAR struct file *filep); Review Comment: rtt_serial_ to serial_rtt_ ########## drivers/segger/serial_rtt.c: ########## @@ -0,0 +1,181 @@ +/**************************************************************************** + * 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 <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 <sys/types.h> + +#include <SEGGER_RTT.h> + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +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); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +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 +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtt_serial_open + ****************************************************************************/ + +static int rtt_serial_open(FAR struct file *filep) +{ + return 0; +} + +/**************************************************************************** + * Name: rtt_serial_close + ****************************************************************************/ + +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, + size_t buflen) +{ + int channel; + ssize_t res = 0; + FAR struct inode *inode; + + DEBUGASSERT(filep != NULL && filep->f_inode != NULL); + inode = filep->f_inode; + channel = (int)inode->i_private; + + res = SEGGER_RTT_Read(channel, buffer + res, buflen); + return res; +} + +/**************************************************************************** + * Name: rtt_serial_ioctl + ****************************************************************************/ + +static int rtt_serial_ioctl(FAR struct file *filep, int cmd, + unsigned long arg) +{ + int ret = -ENOTTY; + int channel; + FAR struct inode *inode; + + DEBUGASSERT(filep != NULL && filep->f_inode != NULL); + inode = filep->f_inode; + channel = (int)inode->i_private; + + switch (cmd) + { + case FIONREAD: + ret = SEGGER_RTT_HasData(channel); + break; + case FIONWRITE: + ret = SEGGER_RTT_GetBytesInBuffer(channel); + break; + case FIONSPACE: + ret = SEGGER_RTT_GetAvailWriteSpace(channel); + break; + } + + return ret; +} + +/**************************************************************************** + * Name: rtt_serial_write + ****************************************************************************/ + +static ssize_t rtt_serial_write(FAR struct file *filep, + FAR const char *buffer, size_t buflen) +{ + FAR struct inode *inode; + int channel; + + DEBUGASSERT(filep != NULL && filep->f_inode != NULL); + inode = filep->f_inode; + channel = (int)inode->i_private; + return SEGGER_RTT_Write(channel, buffer, buflen); Review Comment: need loop until write some data in NONBLOCK case ########## 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; Review Comment: why not use zero as channel directly ########## 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; Review Comment: why not use zero as channel directly ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" Review Comment: ```suggestion bool "Segger RTT console" ``` ########## 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: too small ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE Review Comment: RTT_CONSOLE to CONSOLE_RTT ########## 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 = Review Comment: rtt_console_->console_rtt_ ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE +config RTT_CONSOLE_TXBUF_SIZE + int "rtt console tx buffer size" + default 256 + ---help--- + Configure the rtt console tx buffer size + +config RTT_CONSOLE_RXBUF_SIZE + int "rtt console rx buffer size" + default 256 + ---help--- + Configure the rtt console rx buffer size +endif + +config RTT_SERIAL Review Comment: RTT_SERIAL->SERIAL_RTT ########## 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; Review Comment: why not use zero as channel directly ########## drivers/segger/Kconfig: ########## @@ -79,3 +81,82 @@ config SEGGER_SYSVIEW_RAM_BASE endif endif + +config RTT_CONSOLE + bool "segger rtt console" + default n + select SERIAL_CONSOLE + select SEGGER_RTT + select SERIAL_RXDMA + select SERIAL_TXDMA + ---help--- + Enabling this option will use RTT as the terminal device, + which uses RTT channel 0 by default. + The buffer size of rtt channel 0 is configured by SEGGER_RTT_BUFFER_SIZE_UP + You need to turn off other console devices before turning it on + +if RTT_CONSOLE +config RTT_CONSOLE_TXBUF_SIZE + int "rtt console tx buffer size" + default 256 + ---help--- + Configure the rtt console tx buffer size + +config RTT_CONSOLE_RXBUF_SIZE + int "rtt console rx buffer size" + default 256 + ---help--- + Configure the rtt console rx buffer size +endif + +config RTT_SERIAL + bool "segger rtt serial " Review Comment: remove "segger rtt serial" -- 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