Skip to content
systemcoding.dev
RTOS

Read Zephyr Console Output Over UART and RTT

Send Zephyr printk and log output to a UART terminal or to SEGGER RTT, with the exact Kconfig lines, the chosen node and what to do when nothing prints.

6 min read
Summarize in AI
Flat illustration of RTOS tasks taking turns on a timeline, with the C, Arm and Rust logos.

The first thing every Zephyr program needs is a way to say something. A board with no screen and no operating system prompt gives you two practical channels: a serial line, called a UART, and SEGGER RTT, which moves bytes through the debug probe without using a pin. This post shows how to configure both in Zephyr 4.2, how to read each one from a computer, and how to tell which one a board is using. It assumes the application layout from the post on how a Zephyr application is structured.

How output reaches the console

printk() and the logging subsystem do not write to a serial port directly. They write to a console driver, and Zephyr picks that driver with Kconfig. The most common one is CONFIG_UART_CONSOLE, which sends characters to the UART that devicetree names as zephyr,console. The alternative here is CONFIG_RTT_CONSOLE, which sends them to an RTT channel. Exactly one needs to be on for printk(), and that is normally decided by the board's defconfig, so most boards print on UART with no setting from you.

Devicetree holds the "which UART" half of the answer. Every board file has a chosen node that points at the devices the kernel should use:

dts
chosen {
	zephyr,console = &uart0;
	zephyr,shell-uart = &uart0;
};

That means "console output goes to the node labeled uart0". The kernel reads the label at build time, so a different board can send the same program to a different UART with no source change.

Printing over a UART

Use this program and prj.conf:

src/main.cc
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

int main(void)
{
	int count = 0;

	while (1) {
		printk("tick %d\n", count++);
		k_sleep(K_MSEC(500));
	}
	return 0;
}
prj.confText
CONFIG_PRINTK=y
CONFIG_SERIAL=y
CONFIG_UART_CONSOLE=y

CONFIG_SERIAL enables the serial driver class, and CONFIG_UART_CONSOLE routes console output to it. On most boards both are already on, so listing them only makes the intent explicit. The loop prints a counter twice a second.

Connect the board's USB serial port and open it at 115200 baud, which is the default for zephyr,console nodes unless the board says otherwise:

Shell
screen /dev/ttyACM0 115200

On Linux the device is usually /dev/ttyACM0 or /dev/ttyUSB0, and on macOS it is /dev/cu.usbmodemXXXX. The terminal shows:

Text
*** Booting Zephyr OS build v4.2.0 ***
tick 0
tick 1
tick 2

The device name and the exact banner differ per machine. If the terminal is silent, check the baud rate first, then check that the board's second USB port is not the one you opened. Some development boards expose two serial ports, and only one is wired to the console UART.

Printing over RTT

RTT, short for Real Time Transfer, is a SEGGER protocol that places ring buffers in the target's RAM. The debug probe reads them over the same two debug wires you already use for flashing. It needs no extra pins, and it is faster than a UART at the same cost, but it needs a SEGGER J-Link probe or a compatible one, plus a host program that reads the buffers.

Turn it on and turn the UART console off, so that output is not duplicated:

prj.confText
CONFIG_PRINTK=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_RTT_CONSOLE=y
CONFIG_UART_CONSOLE=n

CONFIG_USE_SEGGER_RTT pulls in the RTT library, which ships as a Zephyr module. CONFIG_RTT_CONSOLE makes printk() write to RTT channel 0. Setting CONFIG_UART_CONSOLE=n frees the UART for your own use.

Read the buffer with the J-Link tools. Start a J-Link GDB server or the RTT viewer, then attach a telnet client to the RTT port, 19021 by default:

Shell
JLinkExe -device nRF52840_xxAA -if SWD -speed 4000 -autoconnect 1

and in a second terminal:

Shell
JLinkRTTClient

The client prints the same text as the UART version:

Text
tick 0
tick 1
tick 2

The boot banner may be missing, because it is printed before the RTT control block is found if the viewer attaches late. Resetting the board with the viewer already open shows it.

Using the logging subsystem instead of printk

For anything beyond a hello world, use logging. It adds levels, module names and deferred output, and it uses the same backends:

src/main.cc
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);

int main(void)
{
	LOG_INF("started");
	LOG_WRN("battery low: %d%%", 12);
	return 0;
}
prj.confText
CONFIG_LOG=y
CONFIG_LOG_BACKEND_UART=y

LOG_MODULE_REGISTER names the module and sets its compile-time level. With the UART backend, the terminal shows lines tagged with their level and module:

Text
[00:00:00.000,000] <inf> app: started
[00:00:00.000,000] <wrn> app: battery low: 12%

The timestamp comes from the kernel clock and will differ on your board. For RTT use CONFIG_LOG_BACKEND_RTT=y in place of the UART backend. The logging subsystem is described in the Zephyr logging documentation, and the RTT options are listed in the RTT section of the Kconfig reference.

Choosing between them

UARTRTT
Extra pinsTwo (TX and RX)None
Hardware neededUSB serial adapter, often on the boardJ-Link or compatible probe
Works with the chip asleepOften notOnly while the debug link is alive
ThroughputLimited by baud rateLimited by probe speed
Blocks the program if nobody readsNo, UART drains by itselfDepends on the buffer mode
Best forShipping builds, field logsDevelopment, high-rate traces

RTT has one trap. In its default blocking-friendly configuration, a full buffer can stall the writing thread, or drop data, depending on CONFIG_SEGGER_RTT_MODE_*. If a program freezes when you unplug the probe, that is the cause, and switching to a no-block-skip mode fixes it.

When nothing prints

Work through this list in order. It covers most real failures:

  1. Build for the right board and confirm with west build -b.

  2. Open build/zephyr/.config and search for CONFIG_UART_CONSOLE and CONFIG_RTT_CONSOLE. Exactly the one you expect is y.

  3. Open build/zephyr/zephyr.dts and find zephyr,console. It names the node you are reading.

  4. Confirm that the node's status is "okay".

  5. Check the cable: some boards charge on one port and expose the serial line on another.

Never put a long printk() in an interrupt handler on a slow UART. The call blocks until the characters drain, and it stalls everything of lower priority.

Reading the console from a script

Interactive terminals are fine for exploration, and a script is better for anything repeatable. Python's pyserial package reads the same device and can stop when a marker line appears, which makes it a basic test harness:

capture.pypython
import serial

with serial.Serial("/dev/ttyACM0", 115200, timeout=5) as port:
    for _ in range(20):
        line = port.readline().decode(errors="replace").rstrip()
        print(line)
        if line == "tick 3":
            break
Text
*** Booting Zephyr OS build v4.2.0 ***
tick 0
tick 1
tick 2
tick 3

The timeout=5 argument makes readline() return after five seconds of silence, so a dead board ends the script instead of hanging it. Zephyr's own test runner, Twister, uses the same idea: it watches console output for expected strings, which is why a clear, stable console line is also the simplest test oracle you can write.

Output that arrives late or out of order

Two behaviors surprise beginners. First, printk() is synchronous and written directly, but the logging subsystem defers its output by default and formats it in a separate thread, so log lines and printk() lines can interleave in an order that differs from the order of the calls. If strict ordering matters, set CONFIG_LOG_MODE_IMMEDIATE=y while debugging. It makes logging write on the calling thread, at the cost of blocking that thread for the duration of the write.

Second, a crash that happens before the console driver initializes prints nothing. The UART console is ready only after the driver's init level has run, so very early faults need a debugger. The west debug command in the flashing post is the tool for that case.

Also inDebugging
Share

Keep reading

Get new posts by email

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

Unsubscribe in one click. No spam, ever.