Page 1 of 1

Trouble getting printf() redirection using SWV/ITM to work

Posted: Thu Feb 07, 2019 9:12 pm
by blargian
Hello world,

I am an STM32 newbie. I recently got an STM32F429 Discovery dev board which I have been playing around with. Currently I'm trying to figure out how to use the debugging functions of this M4 Cortex, specifically the SWV ones.

I used STM32CubeMX to generate a project and then modified the main.c files infinite loop as follows to blink an LED on and off every 2 seconds and to printf a message to the console.

Code: Select all

  while (1)
  {
     HAL_GPIO_TogglePin(GPIOG,GPIO_PIN_13);
     HAL_Delay(2000);
     printf("hello");

  }


When I run the debug it programs the board fine. I followed the steps in this http://blog.atollic.com/cortex-m-debugging-printf-redirection-to-a-debugger-console-using-swv/itm-part-1 article which explains how to setup printf redirect to the console however I can't get it to do so. When I step through my code and get to that printf("hello") line it just skips over it like it doesn't exist and nothing is printed to the console.

I've attached a picture of my debug settings and below I have placed the modified version of int _write which resides in my syscalls.c file. I also have "#include "stm32f4xx.h" in that file.

Code: Select all

int _write(int32_t file, uint8_t *ptr, int32_t len)
{
   int i=0;
     for(i=0 ; i<len ; i++)
       ITM_SendChar((*ptr++));
     return len;
   
   errno = ENOSYS;
   return -1;
}


I would greatly appreciate if anyone could assist me in getting the printf to console to work.

Re: Trouble getting printf() redirection using SWV/ITM to work

Posted: Sun Feb 10, 2019 8:18 pm
by frank_ee
Hello,

Did you select the debugger window to select ITM?

Re: Trouble getting printf() redirection using SWV/ITM to work

Posted: Tue Mar 12, 2019 9:40 am
by antm
Maybe make sure the SWO pin is set correctly in CubeMX, and if any jumpers are required on the dev board.

I have had it working on an F7 Nucleo board, but I found that on a F401 Nucleo board it just does not work.

SWO.PNG

Re: Trouble getting printf() redirection using SWV/ITM to work

Posted: Tue Mar 12, 2019 9:43 am
by antm
You could also try poking the registers directly to see if that helps:

/* Debug MCU registers base address */
#define DBGMCU ((DBGMCU_TypeDef *) DBGMCU_BASE)
#define SWO_SPEED 2000000

static uint32_t CPUClockFrequencyInHz(void)
{
return(HAL_RCC_GetHCLKFreq());
}

void KernelDebugITM_Init(void)
{
const uint32_t SWOPrescaler = (CPUClockFrequencyInHz() / SWO_SPEED) - 1; // SWOSpeed in Hz

CoreDebug->DEMCR |= 1 << CoreDebug_DEMCR_TRCENA_Pos;

DBGMCU->CR = 0x00000027; //Enabling TRACE_IOEN, DBG_STANDBY, DBG_STOP, DBG_SLEEP
//Set TPIU register->Selected pinprotocol = 10b: Serial Wire Output - NRZ, 01b = SerialWire Output (Manchester)
*((volatile unsigned *) 0xE00400F0) = 0x00000002; // "Selected PIN Protocol Register": Select which protocol to use for trace output (2: SWO)
//Set TPIU -> Async Clock Prescaler Register [bits 0-12]
*((volatile unsigned *) 0xE0040010) = SWOPrescaler; // "Async Clock Prescaler Register". Scale the baud rate of the asynchronous output
//Lock Access Register
*((volatile unsigned *) 0xE0000FB0) = 0xC5ACCE55; // ITM Lock Access Register, C5ACCE55 enables more write access to Control Register 0xE00 :: 0xFFC
*((volatile unsigned *) 0xE0000E80) = 0x0001000D; // ITM Trace Control Register
*((volatile unsigned *) 0xE0000E40) = 0x0000000F; // ITM Trace Privilege Register
*((volatile unsigned *) 0xE0000E00) = 0x00000001; // ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port.
*((volatile unsigned *) 0xE0001000) |= 0x400003FE; // DWT_CTRL
//And this is really tricky!
*((volatile unsigned *) 0xE0040304) = 0x00000100; // Formatter and Flush Control Register
}

Re: Trouble getting printf() redirection using SWV/ITM to work

Posted: Sun Mar 24, 2019 10:49 pm
by frank_ee
Hello,
Make sure not to use tinyprintf.

Re: Trouble getting printf() redirection using SWV/ITM to work

Posted: Tue Mar 26, 2019 11:20 am
by astefanuk
Hello! Thanks for posting this because I think a lot of people have the same problem and you help them to find answer here. Would you like to read blog about it and I guess you will fix problem with SWV/ITM and it gonna work.