liveBook · Manning

RTT logging

As explained in Embedded Software with Rust

Real-Time Transfer is a low-intrusion mechanism for exchanging data between a running embedded target and a host through target-memory buffers.

Real-Time Transfer (RTT) provides runtime output for embedded firmware without a standard terminal. The target writes to a RAM-resident ring buffer, and a host tool reads that memory through a debug probe while the target runs. SWD and JTAG can provide the required background memory access. section 6.2 section 6.2.4

RTT supports non-blocking, low-overhead output, making it useful when semihosting would disturb timing-sensitive code. It still needs target RAM and a debug connection; UART is an alternative when output must work without a debugger. section 6.1.1 section 6.3.6

#![no_std]
#![no_main]

use cortex_m as _;
use cortex_m_rt::entry;
use panic_halt as _;
use rtt_target::{rprintln, rtt_init_print};

#[entry]
fn main() -> ! {
    rtt_init_print!();
    rprintln!("RTT log message");
    loop {}
}

Initialize RTT before printing with rprintln!. This minimal no_std Cortex-M program sends one message and then stays running. section 6.2.4

[dependencies]
cortex-m = { version = "0.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7"
panic-halt = "1.0.0"
rtt-target = "0.6"

These dependencies support the example, including rtt-target and the single-core critical-section feature. section 6.2.4

cargo install probe-rs-tools
probe-rs run --chip STM32F103C8T6 target/thumbv7m-none-eabi/release/app

Install the host tools, then flash the specified ELF and stream RTT output with probe-rs run. Replace the chip and ELF path for your target. section 6.2.4

For a configuration stored with the project, cargo-embed reads settings such as the chip, probe protocol, and RTT enablement from Embed.toml. Both cargo-embed and probe-rs run use probe-rs for probe communication and RTT capture; the main difference is their configuration workflow. section 6.2.4 section 2.6.1

For compact structured logs, defmt encodes binary frames and defmt-rtt transports them over RTT. The logging API and transport are separate: link only one defmt transport into a firmware image, or linking fails. section 2.6.3 section 6.3.2

Reporting timing measurements

Measure execution with the Cortex-M DWT cycle counter, or a SysTick fallback where DWT is unavailable, and send the results to the host through RTT. Avoid semihosting for profiling output because its calls halt the core and distort timing. section 6.6

How Embedded Software with Rust covers it

How deep it goes
Explains the RAM-buffer mechanism and provides firmware, dependency, and host-command examples.
Examples
Uses a no_std Cortex-M message, an STM32F103 probe command, and cycle-count reporting.
What you can do after
Apply it via rtt-target and probe-rs to initialize firmware output and stream messages on a host.
How it connects
Connects RTT with debug probes, SWD, JTAG, semihosting, UART, defmt, and profiling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.