RTT & Defmt Support
Relevant source files
The following files were used as context for generating this wiki page:
ComChan provides high-performance Real-Time Transfer (RTT) and defmt logging support, allowing developers to stream logs directly from a microcontroller's memory via a debug probe (SWD/J-Link) instead of traditional physical UART serial ports. This system enables zero-latency log viewing and real-time data plotting for embedded firmware.
The integration utilizes probe-rs for hardware interaction and defmt-decoder to translate compressed binary logs into human-readable text. It supports both standard terminal monitoring and 2D/3D visualization modes.
Sources: README.md:144-148, src/rtt_reader.rs
Architecture and Components
The RTT and defmt support is centered around the RttDefmtReader struct, which manages the lifecycle of the debug session, the RTT control block discovery, and the decoding of the incoming data stream.
RTT Connection Lifecycle
The system follows a specific sequence to establish a connection with the target hardware:
- ELF Parsing: The system parses the provided ELF file to locate the
_SEGGER_RTTsymbol and thedefmttable. - Probe Discovery: It lists and opens available debug probes using the
ListerAPI. - Target Attachment: It attaches to the microcontroller (using an optional chip override) and establishes a session.
- Control Block Locating: It uses the address found in the ELF file for an "Instant Attach Fix," or falls back to scanning RAM if the symbol is missing.
Sources: src/rtt_reader.rs:20-80
Data Flow Diagram
The following diagram illustrates the flow from the embedded target to the ComChan UI:
The flow ensures that raw bytes from RTT up-channels are immediately processed by the stream decoder before being formatted for display or plotting.
Sources: src/rtt_reader.rs:88-105, src/monitor.rs:356-385
Implementation Details
RttDefmtReader Struct
The core logic resides in src/rtt_reader.rs. It encapsulates the hardware session and the decoder state.
| Field | Type | Description |
|---|---|---|
session | Session | The active probe-rs session with the debug probe. |
rtt | Rtt | The RTT instance managing communication channels. |
stream_decoder | Box<dyn StreamDecoder> | The decoder used to translate defmt frames. |
Sources: src/rtt_reader.rs:13-17
Decoding and Formatting
ComChan applies ANSI color codes to defmt log levels during the polling process. This provides visual clarity in the terminal.
| Log Level | ANSI Code | Color |
|---|---|---|
| ERROR | \x1b[31;1m | Bold Red |
| WARN | \x1b[33;1m | Bold Yellow |
| INFO | \x1b[32m | Green |
| DEBUG | \x1b[36m | Cyan |
| TRACE | \x1b[90m | Gray |
Sources: src/rtt_reader.rs:118-124
Integration with Monitor and Plotter
RTT support is integrated into the two primary application modes:
- Monitor Mode: Logs are printed sequentially to the terminal. If a connection is lost, the monitor enters a retry loop, attempting to re-attach to the RTT target.
- Plotter Mode: Data retrieved via RTT is passed to
ingest_line, which parses numeric values for real-time charting.
Sources: src/monitor.rs:356-385, src/plotter.rs:395-424
Configuration Options
Users configure RTT support via CLI flags or the comchan.toml configuration file. The --rtt flag is the primary toggle, and it requires the --elf path to function.
| Flag | Argument | Description | Default |
|---|---|---|---|
--rtt | N/A | Enables RTT/Defmt log viewing mode. | false |
--elf | <PATH> | Path to the firmware ELF file containing the defmt table. | None |
--chip | <NAME> | Target chip name for probe-rs (e.g., nRF52840_xxAA). | None |
Sources: src/config.rs:232-243, Cargo.toml:21
Sequence of Log Polling
This sequence diagram shows the iterative process of reading data from the debug probe and rendering it to the user.
Conclusion
The RTT & Defmt Support in ComChan provides a robust alternative to physical serial debugging. By leveraging probe-rs and optimized ELF symbol lookup, it offers a near-instantaneous attachment to target hardware and efficient, colorized log streaming suitable for both text-based monitoring and graphical data visualization.
Sources: src/rtt_reader.rs:35-43, README.md:144-148