Skip to content
systemcoding.dev
Debugging

Print formatted logs with SEGGER_RTT_printf

Log numbers and strings from firmware over SEGGER RTT with SEGGER_RTT_printf, which takes %d, %u, %x, %s and %c but no floating-point formats.

2 min read
Summarize in AI

RTT writes into a ring buffer in target RAM that the probe reads while the core runs, so it costs a few microseconds per message instead of the milliseconds a UART takes. SEGGER_RTT_printf is the formatted entry point in SEGGER's RTT sources, and the first argument is the up-buffer index, 0 for the default terminal. It is a small hand-written formatter, not libc's, and that is both the benefit and the limit.

rtt_log.cc
#include "SEGGER_RTT.h"

void log_sample(unsigned raw, int celsius) {
    SEGGER_RTT_printf(0, "temp=%d raw=0x%04x state=%s\n", celsius, raw, "ok");
}
Text
temp=23 raw=0x01ff state=ok

The call supports %d, %u, %x, %X, %s, %p and %c with optional width, and it has no %f. Pass a scaled integer and print it as two numbers, such as 23.5 as 235 with a note in the log text. A %f in the format string prints garbage or nothing, and the compiler does not warn because the function does not carry a format attribute.

Each call can block the CPU while it writes to the buffer, and what happens when the buffer is full depends on the mode set for it, which is a separate snippet. Messages from interrupt handlers are allowed because the ring buffer is lock-protected by disabling interrupts for a few instructions, which still counts as latency. The numbers above are an example.

Also inEmbedded
Taggedcrtt
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.