8
\$\begingroup\$

I'm new using STM32 microcontrollers. I have been trying to use the printf tracing in my code without success, nothing is printed on the console. I can start a debug session, I can place breakpoints on my code, inspect variables and all works as expected but not the printf.

My setup:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

I have reimplemented the _write method:

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

}

And placed a breakpoint on:

__STATIC_INLINE uint32_t ITM_SendChar (uint32_t ch)
{
  if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) &&      /* ITM enabled */
      ((ITM->TER & 1UL               ) != 0UL)   )     /* ITM Port #0 enabled */
  {
    while (ITM->PORT[0U].u32 == 0UL)
    {
      __NOP();
    }
    ITM->PORT[0U].u8 = (uint8_t)ch;
  }
  return (ch);
}

ITM->PORT[0U].u8 = (uint8_t)ch; is being executed, but there is no printf ouput on the console.

\$\endgroup\$

5 Answers 5

11
\$\begingroup\$

There are three extra magic steps to get this working:

  1. Serial Wire View (SWV) tracing must be enabled. You haven't specified, but the IDE you're using looks similar to Atollic TrueSTUDIO. In that IDE, you enable SWV in the Debug configuration by enabling the checkbox shown in this image:

Enable SWV in TrueSTUDIO

  1. You must "Start Trace" during every new debug session. In your screenshot it looks like you've already done it by clicking the red round button in the SWV ITM Data Console pane, but to make sure have another look around for a "SWV Console" window and click the red round button.
  2. And you must connect the SWV pin. SWD only requires GND, SWCLK and SWDIO for debugging. If you also want Trace functionality you need to connect the SWV pin. Confusingly, the SWV pin is often called the SWO pin. It's usually shared with the JTDO JTAG pin. Should be pin 39 (PB3) on the STM32F103 Blue Pill.
\$\endgroup\$
3
  • \$\begingroup\$ Hi, I forgot to mention it in my original post but I already did both things, also ITM->PORT[0U].u8 = (uint8_t)ch is only executed when the "red rounded button" is pressed. \$\endgroup\$
    – Marc
    Commented Jun 28, 2019 at 11:15
  • \$\begingroup\$ Well then you must be very close... have you actually connected the SWV pin? I can only see 3 wires connected in the photo. They'd have to be GND, SWCLK and SWDIO if you can debug. You also need SWV is you want Trace functionality. \$\endgroup\$ Commented Jun 29, 2019 at 23:03
  • \$\begingroup\$ that was it, now I'm getting garbage but at least I'm getting something, thanks! \$\endgroup\$
    – Marc
    Commented Jun 30, 2019 at 18:01
8
\$\begingroup\$

One thing that's not been mentioned is that assuming that all the connections are correct and that the IDE has been properly configured, printf may not show up in the console or may show up but be corrupted if \n isn't added to the printf function. eg.

printf("Hello World!");

Will either not work or show garbage, whereas:

printf("Hello World!\n");

Does work. This is true for STM32s CubeIDE

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Thanks man, I searched 2 hours until I found your post that solved my problem! \$\endgroup\$
    – Paul G.
    Commented Apr 20, 2020 at 14:58
  • \$\begingroup\$ Due to buffering? \$\endgroup\$
    – Oskar Skog
    Commented Sep 25, 2022 at 11:30
4
\$\begingroup\$

I had the same problem, I solved it by changing some settings. First take a look at your configuration in the ST_CUBE_MX in the clock configuration in the value of SYSCLK.

Show the configuration SYSCLK

And then this same value you must write in the debugger settings, like this:

Show the configuration debugger

I run this under Debian 10.

\$\endgroup\$
0
1
\$\begingroup\$

You will able to print debug info via SWO method or UART-TX (simplier and common):


SWO method:

In the Bluepill MCU (STM32F103), SWO pin is PB3: connect this pin with your stlinkv2 programmer:

bluepill pinout

Moreover, you will have to use a specific printf using SWO interface (see SWO_PrintString() in this tutorial).


UART-TX method:

Initialize one of the 3 UARTs to send chars through the TX pin and receive the messages in your PC with a well-known USB-UART adapter. Probably, you will use the implemented method in your favorite RTOS or framework to send strings to the UART.

\$\endgroup\$
6
  • \$\begingroup\$ Hi thanks I know, UART works fine I already tried but I want to understand why SWD printf is not working in my case. \$\endgroup\$
    – Marc
    Commented Jun 28, 2019 at 12:53
  • \$\begingroup\$ My ST-Link (very cheap one) does not have a 'SWO' pin, so it is impossible to do that for me. \$\endgroup\$
    – Sanbrother
    Commented Sep 30, 2020 at 9:08
  • \$\begingroup\$ @Sanbrother Just use one of those serial to USB converters(making sure to 3.3V). Then connect a terminal program to the comm port the converter ties to. The vast majority of debug libraries default to using async for SWO output. \$\endgroup\$
    – GB - AE7OO
    Commented Dec 29, 2020 at 9:39
  • \$\begingroup\$ @Sanbrother For what it's worth, with a bit of wire, a soldering iron, and some patience those cheap ST-Link clones can generally be modified to expose the 'SWO' pin. I've done so with one of mine and found it works quite well! \$\endgroup\$ Commented Mar 26, 2021 at 4:28
  • \$\begingroup\$ electronics.stackexchange.com/questions/206113/… \$\endgroup\$
    – Robin
    Commented Feb 3, 2023 at 10:36
0
\$\begingroup\$

This problem can be resolved by matching the clock configuration, system core clock and tge ST link debugger core clock. The default in the ST-link debugger doesn't match the core clock so we have to manually type and match it. I resolved my issue with this.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.