From 126b33c4886b6182732f0f7e5fe19a139656e7f6 Mon Sep 17 00:00:00 2001 From: yinshengkai Date: Sun, 27 Nov 2022 20:06:54 +0800 Subject: [PATCH] drivers/segger: add Segger RTT stream support Signed-off-by: yinshengkai --- drivers/segger/Make.defs | 4 + drivers/segger/stream_rtt.c | 196 ++++++++++++++++++++++++++++++++++++ include/nuttx/segger/rtt.h | 94 +++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 drivers/segger/stream_rtt.c create mode 100644 include/nuttx/segger/rtt.h diff --git a/drivers/segger/Make.defs b/drivers/segger/Make.defs index 722c3d3bc6..0849c4ebab 100644 --- a/drivers/segger/Make.defs +++ b/drivers/segger/Make.defs @@ -54,6 +54,10 @@ TARGET_ZIP += $(SGDIR)/RTT.zip endif +ifeq ($(CONFIG_SEGGER_RTT),y) + CSRCS += segger/stream_rtt.c +endif + ifeq ($(CONFIG_SYSLOG_RTT),y) CSRCS += segger/syslog_rtt.c endif diff --git a/drivers/segger/stream_rtt.c b/drivers/segger/stream_rtt.c new file mode 100644 index 0000000000..8afe1e0797 --- /dev/null +++ b/drivers/segger/stream_rtt.c @@ -0,0 +1,196 @@ +/**************************************************************************** + * drivers/segger/stream_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 +#include + +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rttstream_putc + ****************************************************************************/ + +static void rttstream_putc(FAR struct lib_outstream_s *this, int ch) +{ + FAR struct lib_rttoutstream_s *stream = + (FAR struct lib_rttoutstream_s *)this; + stream->public.nput += SEGGER_RTT_PutChar(stream->channel, ch); +} + +/**************************************************************************** + * Name: rttstream_puts + ****************************************************************************/ + +static int rttstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buf, int len) +{ + FAR struct lib_rttoutstream_s *stream = + (FAR struct lib_rttoutstream_s *)this; + int ret = SEGGER_RTT_Write(stream->channel, buf, len); + stream->public.nput += ret; + return ret; +} + +/**************************************************************************** + * Name: rttstream_getc + ****************************************************************************/ + +static int rttstream_getc(FAR struct lib_instream_s *this) +{ + FAR struct lib_rttinstream_s *stream = + (FAR struct lib_rttinstream_s *)this; + int ch = -1; + + DEBUGASSERT(stream); + stream->public.nget += SEGGER_RTT_Read(stream->channel, &ch, 1); + return ch; +} + +/**************************************************************************** + * Name: rttstream_gets + ****************************************************************************/ + +static int rttstream_gets(FAR struct lib_instream_s *this, + FAR void * buffer, int size) +{ + FAR struct lib_rttinstream_s *stream = + (FAR struct lib_rttinstream_s *)this; + int ret; + + DEBUGASSERT(stream); + ret = SEGGER_RTT_Read(stream->channel, buffer, size); + stream->public.nget += ret; + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_rttoutstream_open + * + * Description: + * Initializes a stream for use with the configured RTT interface. + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_rttoutstream_s to be initialized. + * channel - SEGGER RTT channel number + * bufsize - Size of the RTT buffer + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_rttoutstream_open(FAR struct lib_rttoutstream_s *stream, + int channel, size_t bufsize) +{ + if (channel) + { + bufsize = bufsize ? bufsize : BUFFER_SIZE_UP; + stream->buffer = (FAR char *)kmm_malloc(bufsize); + DEBUGASSERT(stream->buffer); + sprintf(stream->name, "rtt%d", channel); + SEGGER_RTT_ConfigUpBuffer(channel, stream->name, stream->buffer, + bufsize, SEGGER_RTT_MODE_DEFAULT); + } + + stream->public.put = rttstream_putc; + stream->public.puts = rttstream_puts; + stream->public.flush = lib_noflush; + stream->public.nput = 0; + stream->channel = channel; +} + +/**************************************************************************** + * Name: lib_rttoutstream_close + ****************************************************************************/ + +void lib_rttoutstream_close(FAR struct lib_rttoutstream_s *stream) +{ + if (stream->channel) + { + kmm_free(stream->buffer); + SEGGER_RTT_ConfigUpBuffer(stream->channel, NULL, NULL, + 0, SEGGER_RTT_MODE_DEFAULT); + } +} + +/**************************************************************************** + * Name: lib_rttinstream + * + * Description: + * Initializes input stream for use with the configured RTT interface. + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_rttinstream_s to be initialized. + * channel - SEGGER RTT channel number + * bufsize - Size of the RTT input buffer + * + ****************************************************************************/ + +void lib_rttinstream_open(FAR struct lib_rttinstream_s *stream, + int channel, size_t bufsize) +{ + if (channel) + { + bufsize = bufsize ? bufsize : BUFFER_SIZE_DOWN; + stream->buffer = (FAR char *)kmm_malloc(bufsize); + DEBUGASSERT(stream->buffer); + sprintf(stream->name, "rtt%d", channel); + SEGGER_RTT_ConfigDownBuffer(channel, stream->name, stream->buffer, + bufsize, SEGGER_RTT_MODE_DEFAULT); + } + + stream->public.get = rttstream_getc; + stream->public.gets = rttstream_gets; + stream->public.nget = 0; + stream->channel = channel; +} + +/**************************************************************************** + * Name: lib_rttinstream_close + ****************************************************************************/ + +void lib_rttinstream_close(FAR struct lib_rttinstream_s *stream) +{ + if (stream->channel) + { + kmm_free(stream->buffer); + SEGGER_RTT_ConfigDownBuffer(stream->channel, NULL, NULL, + 0, SEGGER_RTT_MODE_DEFAULT); + } +} diff --git a/include/nuttx/segger/rtt.h b/include/nuttx/segger/rtt.h new file mode 100644 index 0000000000..0d8f56a19f --- /dev/null +++ b/include/nuttx/segger/rtt.h @@ -0,0 +1,94 @@ +/**************************************************************************** + * include/nuttx/segger/rtt.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_SEGGER_RTT_H +#define __INCLUDE_NUTTX_SEGGER_RTT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Type Declarations + ****************************************************************************/ + +struct lib_rttoutstream_s +{ + struct lib_outstream_s public; + char name[32]; + FAR char *buffer; + int channel; +}; + +struct lib_rttinstream_s +{ + struct lib_instream_s public; + char name[32]; + FAR char *buffer; + int channel; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** +* Name: lib_rttoutstream_open +*****************************************************************************/ + +void lib_rttoutstream_open(FAR struct lib_rttoutstream_s *stream, + int channel, size_t bufsize); + +/**************************************************************************** + * Name: lib_rttoutstream_close + ****************************************************************************/ + +void lib_rttoutstream_close(FAR struct lib_rttoutstream_s *stream); + +/**************************************************************************** +* Name: lib_rttinstream_open +*****************************************************************************/ + +void lib_rttinstream_open(FAR struct lib_rttinstream_s *stream, + int channel, size_t size); + +/**************************************************************************** + * Name: lib_rttinstream_close + ****************************************************************************/ + +void lib_rttinstream_close(FAR struct lib_rttinstream_s *stream); + +#ifdef __cplusplus +} +#endif + +#endif