drivers/segger: add Segger RTT stream support

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2022-11-27 20:06:54 +08:00 committed by Xiang Xiao
parent 4663987e66
commit 126b33c488
3 changed files with 294 additions and 0 deletions

View File

@ -54,6 +54,10 @@ TARGET_ZIP += $(SGDIR)/RTT.zip
endif endif
ifeq ($(CONFIG_SEGGER_RTT),y)
CSRCS += segger/stream_rtt.c
endif
ifeq ($(CONFIG_SYSLOG_RTT),y) ifeq ($(CONFIG_SYSLOG_RTT),y)
CSRCS += segger/syslog_rtt.c CSRCS += segger/syslog_rtt.c
endif endif

196
drivers/segger/stream_rtt.c Normal file
View File

@ -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 <assert.h>
#include <stdio.h>
#include <nuttx/config.h>
#include <nuttx/kmalloc.h>
#include <nuttx/streams.h>
#include <nuttx/segger/rtt.h>
#include <SEGGER_RTT.h>
/****************************************************************************
* 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);
}
}

View File

@ -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 <nuttx/config.h>
#include <nuttx/streams.h>
/****************************************************************************
* 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