Serial Wire Viewer (SWV) is a real-time trace functionality that is available in Arm Cortex-M microcontrollers. It allows real-time data to be transmitted from the Cortex-M CPU to an external debug probe, which can capture the data for viewing and analysis on a host computer. The key benefit of SWV is that it enables non-intrusive debugging of real-time systems, without halting the core or requiring extra pins like traditional debugging solutions.
Overview of SWV
SWV uses a serial wire output (SWO) port available in most Cortex-M devices to stream trace data out in real-time. This includes tracing program flow, data values, exception information, and custom instrumentation. By attaching an external debug probe like J-Link or ST-Link, the SWO data can be captured, decoded, and displayed on the debugging host for analysis. SWV tracing is enabled by the Debug Access Port (DAP) and Cortex-M debug peripheral built into the microcontroller.
Key Characteristics of SWV
- Non-intrusive debugging without halting the core
- Uses a single pin (SWO) for streaming data
- Tracing done in real-time at core speed
- Small trace buffer on-chip
- External debug probe captures and decodes data
- Various trace sources: program flow, data, exceptions, etc.
- Supported on Cortex-M0/M3/M4/M7
SWV vs Traditional Debugging
Compared to traditional debugging solutions like JTAG, SWV has some notable advantages:
- Real-time tracing on a production system without halting core
- Uses only 1 pin instead of 4+ pins for JTAG
- Higher data throughput than JTAG
- Trace buffer is independent of system memory
- No interference with system timing
However, SWV also has some limitations. The small on-chip trace buffer limits capture length. Complex triggers and breakpoints are not supported like in JTAG. Overall, SWV complements JTAG debugging nicely by providing real-time trace capabilities.
SWV Architecture and Components
The key components that enable SWV in Cortex-M devices are:
- Serial Wire Output (SWO) – Dedicated output pin that streams trace data.
- Debug Access Port (DAP) – Debug control and data transfer port.
- Instrumentation Trace Macrocell (ITM) – Generates SWV data packets.
- Trace Port Interface Unit (TPIU) – Handles formatting of SWV packets.
- Embedded Trace Macrocell (ETM) – Trace source for program flow tracing.
- External Debug Probe – Captures and decodes SWV data.
The DAP handles the debug interface between the external probe and the microcontroller. The ITM and TPIU modules generate the tracing data packets which get output over the SWO pin. An external probe like J-Link is used to capture this data for viewing on the host. The ETM provides advanced program flow tracing capabilities.
SWV Packet Format
SWV data is encapsulated in packets with the following format:
- Start Bit – Always 1
- Header – Indicates packet type (1-4 bits)
- Payload – Packet data (4-28 bits)
- Parity Bit – Even parity over header + payload
This allows efficient streaming of trace data over the single SWO pin to the external probe. The probe continuously clocks out the SWO pin data and reconstitutes the packets.
SWV Trace Sources
SWV supports tracing from a variety of sources within the Cortex-M system. The main trace sources are:
ITM Stimulus Ports
Stimulus ports are used to emit data from the application code using special register writes. They allow tracing data values, exception information, profiling data, and custom instrumentation. There are up to 32 stimulus ports in Cortex-M3/M4 and 64 in Cortex-M7 devices. Stimulus ports don’t require use of any special debugging logic.
ETM Program Trace
The Embedded Trace Macrocell (ETM) provides instruction and program flow tracing capabilities. It can trace program branches, function calls/returns, and exceptions. However, ETM consumes more system resources than stimulus ports. It also requires use of the Program Trace Macrocell (PTM).
DWT Data Watchpoint and Trace
The Data Watchpoint and Trace (DWT) unit allows tracing data accesses and watchpoints. It can trace load/store operations and data value matching. However, using DWT also utilizes debug resources similarly to ETM.
Instrumentation Trace Macrocell
The ITM provides high-speed tracing through stimulus ports, timestamping, and system profiling. It includes things like a global timestamp counter. The ITM controls formatting of packets by the TPIU before output over SWO pin.
Using SWV in Cortex-M Debugging
Here are the key steps to utilize SWV tracing in your Cortex-M firmware debugging:
- Enable SWV in debugger – Configure SWV clock speed, core clock, and enable trace capture in your IDE or debugger.
- Initialize SWV ports – Initialize ITM stimulus ports, DWT, ETM, etc. depending on sources used.
- Insert trace calls – Add API calls to send data to stimulus ports, watchpoints, etc.
- Capture SWV data – Run code and capture trace data to debugger via SWO pin.
- Analyze traces – View and analyze the program flow, data, timestamp, and other trace data.
- Refine debugging – Add additional traces and rerun to isolate issues.
SWV data can be captured in processors running at their full speed without intervention. So you get an unaltered view of the real-time system behavior.
SWV Debugging Tips
- Start with ITM stimulus ports for simple printf-style traces.
- Use DWT for selective data value tracing.
- Enable ETM for complete instruction flow tracing.
- Minimize tracing volume for better performance.
- Analyze idle times and timing issues using timestamps.
- Trace exceptions and interrupts to find glitches.
SWV Use Cases
Here are some common use cases where Serial Wire Viewer tracing provides significant benefit:
Real-Time Application Debugging
SWV allows non-stop tracing on live systems without any intrusion. This enables debugging complex real-time issues that only occur in the field.
Interrupt and Exception Tracing
SWV tracing of interrupts and exceptions helps identify glitches, lock-ups, and faults occurring in firmware.
Profiling and Optimization
Using ITM timestamps and program flow tracing assists in profiling code execution to identify hotspots for optimization.
Wireless Firmware Debugging
SWV allows debugging wireless systems like IoT devices without wires by transmitting trace data over the air.
Manufacturing Test and Diagnostics
Stimulus port tracing can embed diagnostics data to analyze issues in production programming and testing.
Data tracing with DWT and timestamps with ITM enables real-time monitoring of system performance.
While SWV brings several benefits to Cortex-M debugging, some limitations to consider are:
- Small trace buffer size on-chip – limits trace capture length.
- No support for complex breakpoints or triggers.
- ETM and DWT use significant resources when enabled.
- Debug probe and host must keep up with streaming data rate.
- Intrusive SWV initialization code can alter firmware behavior.
Despite these limitations, SWV enhances visibility into running Cortex-M systems and complements other Arm debugging solutions well.
In summary, Serial Wire Viewer is a real-time trace capability for Cortex-M microcontrollers. It streams trace data like program flow, data values, timestamps, exceptions, and custom instrumentation over a single pin. This enables non-intrusive debugging and profiling of Cortex-M designs without halting the processor. SWV is invaluable for understanding the real-time behavior of Cortex-M systems. Using the ITM, ETM, and DWT modules, detailed visibility into a running system can be achieved. While SWV has some limitations, it is a key tool for efficient Cortex-M debugging and complements other Arm debugging solutions.