Electronic – STM32 (SWD) printf not working

debugwirestm32trace

I'm new using STM32 microcontrollers, I have been trying to use the printf tracing in my code without success, anything 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);
}

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

Best Answer

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.
Related Topic