Thursday, September 14, 2023

STMViewer update: Trace Viewer module

 Hello, 


Today I want to give you a quick tour of the latest STMViewer update.


The update introduces a new module in the STMViewer which is called Trace Viewer. It serves as a visualizer for trace data read using an ST-Link programmer. SWO output is usually used to print messages from an embedded target to a console on the PC. However, I think there is an even better application for this excellent piece of hardware installed in Cortex M3/M4/M7/M33 cores. Since the trace output can be really fast, and the protocol is designed in a very bandwidth-efficient way (I might make a separate post about it someday), we can send a lot of data with precise timestamps that are automatically added by the trace peripheral. This sounds like a perfect way for profiling parts of a codebase, displaying interrupts and their durations, checking if a timer is configured properly, or displaying very fast "analog" signals, like ADC measurements. What's even better is that you only need one additional pin except SWDIO, SWCLK, and GND - the SWO pin. 


To send a byte with a so-called, "local timestamp" you simply write a register of the ITM peripheral (Instrumentation Trace Macrocell) like so:


ITM->PORT[<number>] .u8 = value; 


That's simple, isn't it? For the STMViewer to interpret the data as a high state you have to send 0xaa byte, for a low state you have to send 0xbb byte. You can also switch each channel to the "analog" domain and send full 8-,16- and 32-bit values that will be displayed. 


There is no need to configure the ITM peripheral as STMViewer does it using the debug interface on each trace start.


Below is a short demonstration GIF. In the beginning, you can see three digital channels of interrupts with different priorities (the highest priority is the top one, lowest is the bottom one). Preemption can be easily spotted by looking at the varying CH3 frequency. By using markers one can measure the time between two actions represented on the trace plots. Notice that the SysTick interrupt takes around 200ns (!) to complete and it's easily registered among much slower signals. 


The other part of the GIF shows a fast analog signal (each new sample is created every 10us) in combination with a high-priority interrupt that performs a memcopy of a large buffer.



This is just a demonstration, not a real application, however, I believe it will help to understand the use cases for this new STMViewer module. 


Please see the README.md file for a quick startup guide. 


Be sure to let me know if you have any feedback or feature ideas. Happy debugging!