printf() using ST-Link debug interface (SWD, ITM View)

Intention

ARM created the possibility to use a printf() like serial output using the SWD interface (ITM port 0). This example describe theusage using a Nucleo-64 board, ST-Link v2.1 and the STM32CubeIDE.

Note: The Cortex-M0 don’t have the required hardware in the mcu. So it is not possible on there.

switch printf() to the debug interface

Often you find the describtion to overwrite _write(). That’s correct. In case of the STM32CubeIDE generated code it is also possible to overwrite the more low level __io_putchar().

/* USER CODE BEGIN 4 */
int __io_putchar(int ch) {
    ITM_SendChar(ch);
    return ch;
}
/* USER CODE END 4 */

Debug Settings

You have to enable the Serial Wire Viewer (SWV). The most relevant setting is the Core clock. You have to configure the right one, that you initialize within your application.

STM32CubeIDE Debug Configuration
STM32CubeIDE Debug Configuration SWV

Prepare the Debug view

  • Open the SWV ITM Console
open SWV ITM Console
open SWV ITM Console

printf() use the SWO connector and the trace port 0.

  • Add ITM port 0
ITM port 0
ITM port 0
  • Enable ITM port 0
ITM port 0
ITM port 0
  • Start trace to view the printf() output
Start Trace
Start Trace

That’s all.

output
printf output

Alternative ST-Link Utility

ST-Link_Utility config
ST-Link_Utility config