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

Share code examples or discuss embedded software, including device drivers, interrupt handlers, middleware and application code.

Moderators: Markus Girdland, Mattias Norlander

blargian
Posts: 1
Joined: Thu Feb 07, 2019 8:23 pm

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

Postby blargian » Thu Feb 07, 2019 9:12 pm

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.
You do not have the required permissions to view the files attached to this post.

frank_ee
Posts: 89
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

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

Postby frank_ee » Sun Feb 10, 2019 8:18 pm

Hello,

Did you select the debugger window to select ITM?

antm
Posts: 5
Joined: Mon Mar 11, 2019 10:35 am

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

Postby antm » Tue Mar 12, 2019 9:40 am

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
You do not have the required permissions to view the files attached to this post.

antm
Posts: 5
Joined: Mon Mar 11, 2019 10:35 am

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

Postby antm » Tue Mar 12, 2019 9:43 am

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
}

frank_ee
Posts: 89
Joined: Wed Apr 05, 2017 5:08 pm
Location: USA

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

Postby frank_ee » Sun Mar 24, 2019 10:49 pm

Hello,
Make sure not to use tinyprintf.

astefanuk
Posts: 2
Joined: Fri Mar 22, 2019 8:53 am

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

Postby astefanuk » Tue Mar 26, 2019 11:20 am

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.


Return to “Embedded target code development discussions”

Who is online

Users browsing this forum: No registered users and 1 guest