logger: Add a Segger RTT backend

Add logger backend that uses Segger RTT for message output. Several
options are provided allowing configuration of up-buffer and logger
behaviour.

Signed-off-by: Pavel Kral <pavel.kral@omsquare.com>
This commit is contained in:
Pavel Kral 2018-10-17 10:17:14 +02:00 committed by Carles Cufí
commit 89b355c916
5 changed files with 274 additions and 7 deletions

View file

@ -1 +1 @@
add_subdirectory_ifdef(CONFIG_RTT_CONSOLE segger)
add_subdirectory(segger)

View file

@ -1,4 +1,4 @@
zephyr_include_directories(.)
zephyr_sources_ifdef(CONFIG_RTT_CONSOLE rtt/SEGGER_RTT.c)
zephyr_include_directories_ifdef(CONFIG_HAS_SEGGER_RTT .)
zephyr_sources_ifdef(CONFIG_HAS_SEGGER_RTT rtt/SEGGER_RTT.c)
zephyr_sources_ifdef(CONFIG_SEGGER_SYSTEMVIEW systemview/SEGGER_SYSVIEW.c)

View file

@ -23,3 +23,8 @@ zephyr_sources_ifdef(
CONFIG_LOG_BACKEND_NATIVE_POSIX
log_backend_native_posix.c
)
zephyr_sources_ifdef(
CONFIG_LOG_BACKEND_RTT
log_backend_rtt.c
)

View file

@ -294,6 +294,66 @@ config LOG_BACKEND_UART
help
When enabled backend is using UART to output logs.
config LOG_BACKEND_RTT
bool "Enable Segger J-Link RTT backend"
depends on HAS_SEGGER_RTT
default y
help
When enabled backend will use RTT for logging. This backend works on per
message basis. Only whole message (terminated with carrier return '\r')
is transferred to up-buffer at once depending on available space and
selected mode.
In panic mode backend always blocks and waits until there will be space
in up-buffer for a message and message is transferred to host.
if LOG_BACKEND_RTT
choice
prompt "Logger behaviour"
default LOG_BACKEND_RTT_MODE_BLOCK
config LOG_BACKEND_RTT_MODE_DROP
bool "Drop message that does not fits in up-buffer."
help
If there is not enough space in up-buffer for a message, drop it.
Number of dropped messages will be logged.
Increase up-buffer size helps to reduce dropping of messages
config LOG_BACKEND_RTT_MODE_BLOCK
bool "Block until message is transferred to host."
help
Waits until there will be enough space in the up-buffer for a message
and until the message is completely transferred to the host.
endchoice
config LOG_BACKEND_RTT_MESSAGE_SIZE
int "Size of internal buffer for storing messages."
range 32 256
default 128
help
This option defines maximum message size transferable to up-buffer.
config LOG_BACKEND_RTT_BUFFER
int "Buffer number used for logger output."
range 0 SEGGER_RTT_MAX_NUM_UP_BUFFERS
default 0
help
Select index of up-buffer used for logger output, by default is uses
terminal up-buffer and its settings.
if LOG_BACKEND_RTT_BUFFER > 0
config LOG_BACKEND_RTT_BUFFER_SIZE
int "Size of reserved up-buffer for logger output."
default 1024
help
Specify reserved size of up-buffer used for logger output.
endif # LOG_BACKEND_RTT_BUFFER
endif # LOG_BACKEND_RTT
config LOG_BACKEND_NATIVE_POSIX
bool "Enable native backend"
depends on ARCH_POSIX
@ -303,14 +363,14 @@ config LOG_BACKEND_NATIVE_POSIX
config LOG_BACKEND_SHOW_COLOR
bool "Enable colors in the backend"
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX || LOG_BACKEND_RTT
default y
help
When enabled UART backend prints errors in red and warning in yellow.
When enabled selected backend prints errors in red and warning in yellow.
config LOG_BACKEND_FORMAT_TIMESTAMP
bool "Enable timestamp formatting in the backend"
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX || LOG_BACKEND_RTT
default y
help
When enabled timestamp is formatted to hh:mm:ss:ms,us.

View file

@ -0,0 +1,202 @@
/*
* Copyright (c) 2018 omSquare s.r.o.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <logging/log_backend.h>
#include <logging/log_core.h>
#include <logging/log_msg.h>
#include <logging/log_output.h>
#include <rtt/SEGGER_RTT.h>
#if CONFIG_LOG_BACKEND_RTT_MODE_DROP
#define DROP_MESSAGE "\nmessages dropped: 99 \r"
#define DROP_MESSAGE_LEN (sizeof(DROP_MESSAGE) - 1)
static const char *drop_msg = DROP_MESSAGE;
static int drop_cnt = 0;
static int drop_warn = 0;
#else
#define DROP_MESSAGE_LEN 0
#endif /* CONFIG_LOG_BACKEND_RTT_MODE_DROP */
#if CONFIG_LOG_BACKEND_RTT_BUFFER > 0
static u8_t rtt_buf[CONFIG_LOG_BACKEND_RTT_BUFFER_SIZE];
#endif /* CONFIG_LOG_BACKEND_RTT_BUFFER > 0 */
static u8_t line_buf[CONFIG_LOG_BACKEND_RTT_MESSAGE_SIZE + DROP_MESSAGE_LEN];
static u8_t *line_pos;
static u8_t char_buf;
static int panic_mode;
static int msg_out(u8_t *data, size_t length, void *ctx);
static int line_out(u8_t data);
static void log_backend_rtt_flush(void);
static int log_backend_rtt_write(void);
static int log_backend_rtt_panic(u8_t *data, size_t length);
static int msg_out(u8_t *data, size_t length, void *ctx)
{
(void) ctx;
u8_t *pos;
if (panic_mode) {
return log_backend_rtt_panic(data, length);
}
for (pos = data; pos < data + length; pos++) {
if (line_out(*pos)) {
break;
}
}
return (int) (pos - data);
}
static int line_out(u8_t data)
{
if (data == '\r') {
if (log_backend_rtt_write()) {
return 1;
}
line_pos = line_buf;
return 0;
}
if (line_pos < line_buf + sizeof(line_buf) - 1) {
*line_pos++ = data;
}
/* not enough space in line buffer, we have to wait for EOL */
return 0;
}
#if CONFIG_LOG_BACKEND_RTT_MODE_DROP
static int log_backend_rtt_write(void)
{
*line_pos = '\r';
if (drop_cnt > 0 && !drop_warn) {
memmove(line_buf + DROP_MESSAGE_LEN, line_buf,
line_pos - line_buf);
memcpy(line_buf, drop_msg, DROP_MESSAGE_LEN);
line_pos += DROP_MESSAGE_LEN;
drop_warn = 1;
}
if (drop_warn) {
int cnt = min(drop_cnt, 99);
if (cnt < 10) {
line_buf[DROP_MESSAGE_LEN - 2] = ' ';
line_buf[DROP_MESSAGE_LEN - 3] = (u8_t) ('0' + cnt);
line_buf[DROP_MESSAGE_LEN - 4] = ' ';
} else {
line_buf[DROP_MESSAGE_LEN - 2] = (u8_t) ('0' + cnt % 10);
line_buf[DROP_MESSAGE_LEN - 3] = (u8_t) ('0' + cnt / 10);
line_buf[DROP_MESSAGE_LEN - 4] = '>';
}
}
int ret = SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER,
line_buf, line_pos - line_buf + 1);
if (!ret) {
drop_cnt++;
return 0;
}
drop_cnt = 0;
drop_warn = 0;
return 0;
}
#elif CONFIG_LOG_BACKEND_RTT_MODE_BLOCK
static int log_backend_rtt_write(void)
{
*line_pos = '\r';
if (SEGGER_RTT_WriteSkipNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER, line_buf,
line_pos - line_buf + 1)) {
log_backend_rtt_flush();
return 0;
}
return 1;
}
#endif
static int log_backend_rtt_panic(u8_t *data, size_t length)
{
unsigned int written;
written = SEGGER_RTT_WriteNoLock(CONFIG_LOG_BACKEND_RTT_BUFFER, data,
length);
log_backend_rtt_flush();
return written;
}
LOG_OUTPUT_DEFINE(log_output, msg_out, &char_buf, 1);
static void put(const struct log_backend *const backend,
struct log_msg *msg)
{
log_msg_get(msg);
u32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
flags |= LOG_OUTPUT_FLAG_COLORS;
}
if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
}
log_output_msg_process(&log_output, msg, flags);
log_msg_put(msg);
}
static void log_backend_rtt_init(void)
{
SEGGER_RTT_Init();
#if CONFIG_LOG_BACKEND_RTT_BUFFER > 0
SEGGER_RTT_ConfigUpBuffer(CONFIG_LOG_BACKEND_RTT_BUFFER, "Logger",
rtt_buf, sizeof(rtt_buf),
SEGGER_RTT_MODE_NO_BLOCK_SKIP);
#endif
panic_mode = 0;
line_pos = line_buf;
}
static void panic(struct log_backend const *const backend)
{
panic_mode = 1;
}
static void log_backend_rtt_flush(void)
{
while (SEGGER_RTT_HasDataUp(CONFIG_LOG_BACKEND_RTT_BUFFER)) {
/* pass */
}
}
const struct log_backend_api log_backend_rtt_api = {
.put = put,
.panic = panic,
.init = log_backend_rtt_init,
};
LOG_BACKEND_DEFINE(log_backend_rtt, log_backend_rtt_api, true);