Portenta H7: debug messages via SWO Viewer

Do you know it is possible to send debug messages also via a debugger, as SWO Viewer?
Instead of debug messages via UART - with an external and connected SWD Debugger (e.g. ST-LINK) you can also get debug messages via it.

What is needed?

  • have an external debugger, e.g. ST-LINK, its associated tool, e.g. STM32-Link-Utility (which should have an SWO Viewer)

  • add the code provided here, do the ITM_enable(); and print at any point in your via ITM_printf(str); messages you want to trace (see) when your software is running

Code files needed?
Here the two files you need to add to your project:
ITM_print_log.h (654 Bytes)
ITM_print_log.cpp (2.4 KB)

ATT: it disables SD Card!
Attention: the PB3 signal is needed: it is SWO. But it is also SDC_D2!
You will lose your SD Card (you cannot use SD Card anymore).
If you configure SD Card - do this ITM_enable(); afterwards - not before SD Card setup. One or the other would be lost. SD Card and SWO Viewer are exclusive.
Portenta H7 has a usage conflict on PB3!

Wiring
See this drawing: it needs the Portenta H7 breakout board (to access all the SWD signals).
It is intended to use an ST-LINK debug dongle via USB. But it can be any other debug adapter with SWD and SWO Viewer support.

How to see ITM debug messages?
Important is that you know your MCU SystemCoreClock frequency. I have changed to 480 MHz (not just 400 MHz as Arduino sets it up). This frequency has to be used on ITM_enable() as well as the SWO Viewer (in debugger tool). And MCU core clock frequency must be correct.

I use STM32-Link-Utility: it has a menu button for "ST-LINK". Under it you find "Printf via SWO Viewer". It asks you about the MCU core frequency (my is 480000000) and a channel (channel 0 is used).
If you press "Start" - it will display the debug messages.

The beauty: the debug messages are transmitted pretty fast, with 2 Mbps. And it works in addition to use the UART.
And I think: the debug logic inside MCU (ITM) is smart: if a debugger is not connected or SWO Viewer is not opened - even you print messages via ITM_printf(str); all the time - it does not hang and it should not waste time by trying to send anything but no debugger as receiver is there.

It is a nice "backdoor" to have a look into your running system, w/o to generate a debug compile version or no need to use the user UART for debug messages.

What is ITM?
ITM is hardware logic inside ARM cores, called "Internal/Integrated Trace Macro". It is a HW debug infrastructure in chip where the MCU code execution can be traced/monitored (e.g. as TRACE32).
As you see: it can be used also to send any data, here debug text messages. And an external debugger can connect to this ITM (TRACE, SWO), enable it (from the outside) and get the data sent out from MCU.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.