Skip to main content
Associate
February 3, 2025
Question

Enable ITM printf output using openocd/gdb on STM32H7

  • February 3, 2025
  • 3 replies
  • 1722 views

Hello,

I am looking up on how to capture printf output using ITM on my stm32H723ZG board. Most tutorials I see online are using some sort of IDE. I tried using CubeMX and it works there but I would like to use gdb/openocd since that is my preferred way of debugging.

I am wondering how is it possible to enable printf output using gdb/openocd combo? What setup needs to be done on the gdb side and on the openocd side?

Code wise, I know I have to implement the _write function which I have done below for GCC.

 

int _write(int fd, char *ptr, int len){ 
 size_t i; 
 for(i=0; i<len;i++){ 
 ITM_SendChar(ptr[i]); 
 } 
 return len; 
} 
 

 

Also i added in the --specs=nosys.specs flag in my makefile

Do i need to setup some ITM hardware as well? Part of the confusion for me is that when i used CubeMx it doesn't seem to generate any additional ITM setup code so I not sure if I need to do.

Here is what I have for my gdbscript:

 

target extended-remote localhost:3333 
file build/bare_metal.elf 
load 
display /x $r0 
display /x $r1 
display /x $r2 
display /x $r3 
display /x $r4 
display /x $msp 
display /x $psp 
display /x $control 
display /x $lr 
display /x $xPSR 
 
monitor tpiu config internal itm.txt uart off 64000000 106667 
monitor itm port 0 on 

 

Here is my openocd:

 

# This is an NUCLEO-H723ZG board with a single STM32H723ZGTx chip 
# 
# Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s)
# https://github.com/zephyrproject-rtos/zephyr/issues/51508 
# https://github.com/zephyrproject-rtos/zephyr/issues/50590 
 
source [find interface/stlink-dap.cfg] 
transport select dapdirect_swd 
 
 
set WORKAREASIZE 0x8000 
 
 
set CHIPNAME STM32H723ZGTx 
set BOARDNAME NUCLEO-H723ZG 
 
source [find target/stm32h7x.cfg] 
 
# Enable debug when in low power modes 
set ENABLE_LOW_POWER 1 
 
# Stop Watchdog counters when halt 
set STOP_WATCHDOG 1 
 
# STlink Debug clock frequency 
set CLOCK_FREQ 8000 
 
# Reset configuration 
# use hardware reset, connect under reset 
# connect_assert_srst needed if low power mode application running (WFI...) 
reset_config srst_only srst_nogate connect_assert_srst 
set CONNECT_UNDER_RESET 1 
set CORE_RESET 0 
 
# ACCESS PORT NUMBER 
set AP_NUM 0 
# GDB PORT 
set GDB_PORT 3333 
# Due to the use of connect_assert_srst, running gdb requires 
# to reset halt just after openocd init. 
rename init old_init 
proc init {} { 
 old_init 
 reset halt 
} 
~ 

 

 

3 replies

Pavel A.
Super User
February 3, 2025

Hi,

The best way to start is to use the OpenOCD shipped with CubeIDE debugger. So yes, you install CubeIDE. Then either take a normal CubeIDE project (created with CubeMX, or some example or whatever else)  or bring your own ELF file built with your own tools, and debug it with CubeIDE debugger. In the latter case see CubeIDE user guide, 3.1.9. In the debug settings select OpenOCD and ITM. CubeIDE will create OpenOCD debug configuration file. Make sure it works. Then compare this configuration with yours and use with your own OpenOCD.

 

cdmeisterAuthor
Associate
February 4, 2025

Do you know if there is a way to see what the gdb configuration is that CubeIDE is using when you are using the OpenOCD. 

Do I need to setup the ITM hardware as well?

Pavel A.
Super User
February 6, 2025

Do you know if there is a way to see what the gdb configuration is that CubeIDE is using 

Usually nothing special . Gdb just connects to some interface exposed to it by the low level "target monitor" aka "transport", and does "target remote-extended" then "load"  ... and that's it. Gdb does not use ITM/SWO explicitly.

Anyway, there's the console log of gdb at your disposal.

Do I need to setup the ITM hardware as well?

Depends who are "you". If you asking from the target monitor POV, then yes you do all the configuration via the internal debug module registers of STM32. If you ask from the POV of user STM32 firmware - then no. The debugger will do all the work. The firmware only has to set up the core STM32 clock frequency as specified for the ITM, then not to touch the SWD & SWO pins and not to disturb the debugger interface.

When you run the ST-provided OpenOCD debugger and config files, you will see all this in action.

Associate
October 29, 2025

Hi,

 

Did you get it working?

I'm having issues with the VSCode setup, so I'm trying to get it working using CubeIDE, but no luck so far, also when using OpenOCD the .cfg file can be found in the screenshot, also you can provide your own .cfg file

 

I'm seeing online many issues regarding stm32h7 and ITM, also the default OpenOCD config file for stm32h7 specifies that if hla is used for transport then ITM does not work.

 

So I'm looking for some more info on this.

 

MudKIP_0-1761770786614.png