Using SWV (SWO) on STM32 with OpenOCD – the Easy Way

Debug printf via STM32 SWO can be very valuable in tracking application flow and debugging applications that have to run in the long term. If you search online for “STM32 SWV”, you can find many tutorials for STM32CubeIDE, but they are not applicable when you are using CLion (or not using IDE at all). This article proposes a quick and simple way to enable SWO output with barebone OpenOCD, without any dependencies.

Put this to your OpenOCD cfg file (remember to change stm32g4x to your stm32 series; 96000000 to your core clock rate)

itm ports on
stm32g4x.tpiu enable
stm32g4x.tpiu configure -protocol uart -traceclk 96000000 -output :22888 -formatter off

Put this to your main.c (if you are using cpp, remember to add extern "C" directive):

int __io_putchar(int ch)
  while (ITM->PORT[0].u32 == 0UL)
  ITM->PORT[0].u8 = (uint8_t) ch;
  return ch;

Add some printf to your main code, like the following (make sure not to print too fast, or it will encounter errors):

  int t = 0;
  while (1)
    for (int i = 0; i < 10000000; i++)
    printf("test %d\n", t);

Start debugging, and you should see the following lines on your OCD console:

Now run nc localhost 22888 to see outputs:

CC BY-SA 4.0 本作品使用基于以下许可授权:Creative Commons Attribution-ShareAlike 4.0 International License.

WordPress Appliance - Powered by TurnKey Linux