What are ITM, SWO, ETM and RTT, and when do you use trace?
Short answer: They are ways to get data out of a running Cortex-M without stopping it. ITM (Instrumentation Trace Macrocell) sends software messages (printf-style output, event IDs) and hardware events (DWT data trace, PC sampling, exception entry/exit) over the single-wire SWO pin. ETM (Embedded Trace Macrocell) streams every executed instruction through a parallel trace port (or into an on-chip buffer like the ETB/MTB), giving a full execution history for timing analysis and code coverage. RTT (SEGGER Real-Time Transfer) uses ring buffers in RAM that the debug probe reads in the background, so it works on any core with just SWD.
| ITM/SWO | ETM (+ trace port) | MTB (Cortex-M0+) | RTT | |
|---|---|---|---|---|
| Data | software messages, PC samples, exception trace, data watch trace | full instruction trace | recent branches in RAM | software messages, bidirectional |
| Pins | 1 (SWO) | 4–5 (TRACECLK, TRACEDATA[3:0]) | none | none (SWD) |
| Probe | most probes | trace probes (J-Trace, Lauterbach, ULINKpro) | any | J-Link (and some others) |
| Use | logging, profiling, ISR timing | "what happened before the crash", coverage, WCET | post-mortem branch history | fast logging, shell |
Uses of trace
- Finding the code path just before a HardFault (ETM/MTB history).
- Statistical profiling: PC sampling every N cycles shows where CPU time goes.
- RTOS event trace: Percepio Tracealyzer and SEGGER SystemView record context switches, ISRs and API calls (via RTT or a RAM buffer) and display timelines.
- Code coverage on target with ETM, without instrumentation.
DWT cycle counter: DWT->CYCCNT (enable with CoreDebug->DEMCR |= TRCENA; DWT->CTRL |= 1) gives cycle-accurate timing of code sections, available on Cortex-M3 and above.