Alternative Debugging Prints for STM32 without UART
Alternative Debugging Prints for STM32 without UART

Printf() is the common techniques for debugging the code flow with prints. In this Uart – serial console is most popularly used to get the prints during program runtime. That means printf() is redirected to Uart. There are other alternative methods for Uart debugging prints such as

  1. Serial Wire Viewer
  2. Semihosting
  3. Segger RTT

Serial Wire Viewer (SWV) 

Serial Wire Viewer (SWV) real-time tracing is supported by Cortex-M devices (such as STM32, Kinetis, LPC, EFM32, etc.) can be used for printf() re-direction such that the output goes to a console window in the debugger using the JTAG cable, removing the need for any USB or UART cable. The Instrumentation Trace Macrocell (ITM) enables applications to write arbitrary data to the SWO pin.  The data is transmitted from the Cortex-M core to the desktop PC using the JTAG cable, and modern debuggers can read and visualize that data.

The ARM CMSIS standard is to use ITM port 0 for this purpose, while the remaining 31 ports can be used for other purposes.

Enabling Serial Wire Viewer

In this post STM32Cubeide is used for building, flashing and debugging the project. The configuration has to be done for debugger to enable the SWV prints on the debugger console.

Debugger Configuration

Enable the Serial Wire Viewer with projects system core clock

int _write(int file, char *ptr, int len)
  /* Implement your write code here, this is used by puts and printf for example */
  int i=0;
  for(i=0 ; i<len ; i++) 
  return len;

Add the above code snippet before the main function. Now printf can be used to print any data at any part of the code for example the below code will print the value of count to debugger console.

  while (1)
	  printf("Hello SWV debugging prints...count = %d\n",count++);

	  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);

To enable the SWV ITM data console follow this steps.
Start Debug -> Window -> Show view ->SWV -> SWV ITM Data Console.
Here some other options are available like SWV data trace, SWV Trace log which are useful for runtime monitoring of variables.

Debug prints using SWV

To view the data on the ITM data console select or enable the “port 0” of serial wire viewer settings and start the trace by enabling Start Trace button on the SWV ITM Data Console.

Enabling Semihosted Printf

Semihosting is a neat way to redirect STDOUT and STDIN from an embedded system to a debug window on a PC. It is a debugging tool that is built in to most ARM-based microcontrollers. It allows you to use input and output functions on a host computer that get forwarded to your microcontroller over a hardware debugging tool (e.g. ST-LINK). Specifically, it allows you to use printf() debugging to output strings to your IDE’s console while running code on the microcontroller. Note that this will happen over the Serial Wire Debug (SWD) port.

For the STM32 , semihosting uses the existing USB STLINK h/w connection to the debugger (GDB), and outputs messages to the debug server’s console window (OpenOCD GDB server).

Steps to enable Semihosting

  1. Exclude syscalls.c from build.
  2. call function initialise_monitor_handles() in main function.
  3. Set additional flag ” -specs=rdimon.specs -lc -lrdimon ” for linker
  4. Use ST-LINK OpenOCD as debugger.
  5. Add initialization commands ” monitor arm semihosting enable ” in “Startup” tab in Debug Configuration.
Debug prints using semihosting

Segger RTT Debug Prints

SEGGER’s Real Time Transfer (RTT) is a technology for interactive user I/O in embedded applications. It combines the advantages of SWO and semihosting at very high performance. With RTT it is possible to output information from the target microcontroller as well as sending input to the application at a very high speed without affecting the target’s real time behavior. SEGGER RTT can be used with any J-Link model and any supported target processor which allows background memory access, which are Cortex-M.

STM32 microcontrollers uses ST-link H/W for flashing and debugging which is not supported by Segger RTT but ST-link can be converted to J-link which then supports Segger RTT view. Use this link from SEGGER STLinkReflash utility.

Segger provides necessary source files to add to our project in order to setup the RTT prints. These files can be downloaded by Segger System View. Target Sources files.

Segger RTT logs and debug prints can be seen using Segger RTT client. To get this application you need to install Segger J-link application.

Source files need to be added in project :

Segger RTT Source Code

Add their corresponding header file path to project configuration. After compiling change the debugger setting DEBUG PROBE – ST-link to SEGGER J-link and then start the debugging and open Segger RTT Client to view debug prints.

RTT Debug Prints

Segger RTT debug prints can be used with most of the microcontrollers which uses Segger J-link as there debugger and there also configuration is almost similar to this post.