SWO printing with STM32

SWO printing is needed for CI/CD processes as it allows for printf() messages to be displayed on a terminal, which can be remotely accessed. this can be achieved using openOCD, gbd and semihosting.

(we still need stm32cubeide for the initial setup, im not sure how can i set up everything in the terminal, unfortunately)

TLDR

  1. Set up semihosting printing with STM32CubeIDE and write the code for printing

  2. Use OpenOCD to connect to the physical board, and open up gdb server and a port for gdb to connect.

  3. Use gdb to connect to gdb server

so basically you would want 2 terminals one running openOCD and one running gdb.

Setting up the board for SWO

  • Physical board used: Nucleo F446RE

Setting up in STMcubeide (ew)

Setting up the debugger

Configure debugger settings to use openOCD in STM32CubeIDE. you can add new debug configurations that use openOCD.

you have to change Debug Probe to ST-Link(OpenOCD) and set the initialization commands to monitor arm semihosting enable

image.png
image-1.png

exclude syscalls.c by checking exclude resource from build

Right click on the main project root, go to properties and add rdimon in MCU Linker libraries

Sample code

main thing to note is the inclusion of

#include <stdio.h> #include <stdint.h>

external function

extern void initialise_monitor_handles(void);

finally in the main loop, initializing monitor handle and printing with print()

initialise_monitor_handles(); printf("hello world \n");

 

Using OpenOCD

STM32CubeIDE automatically generates openocd startup command for you, you can just copy and paste that by clicking on show command line

Just remember to add the extra command "-c" "init" "-c" "arm semihosting enable" at the end

Here is a sample command on my laptop

Using gdb

OpenOCD only serves as a server, a middleman between the stm32 chip and you, you would need an additional debugger to step through the breakpoints and control the debugging process. this is where gdb comes in.

To use gbd, first download the gnu arm toolchain here Downloads | GNU Arm Embedded Toolchain Downloads – Arm Developer

  1. cd to the path of toolchain and start gdb by calling arm-none-eabi-gdb.exe

  2. indicate which file you would want to debug with file [pathname], in case of stm32 series, this would be the path to your .elf file

  3. connect to openocds remote gdb server port with target extended-remote [network path]

Sample command

Sample output on the openOCD terminal