cancel
Showing results for 
Search instead for 
Did you mean: 

How to use ITM Printf inside a Console of STM32CubeIDE

Fabien B
Associate III

Hello,

I would like to use the ITM Printf directly inside a Console of STM32CubeIDE but It doesn't work on my side.

I was able to display a character inside a "SWC ITM data Console" by using ITM_SendChar(). But I have the next issues:

- For using printf, I read somewhere that I have to change the _write function inside syscalls.c like this:

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
	int DataIdx;
 
	for (DataIdx = 0; DataIdx < len; DataIdx++)
	{
		ITM_SendChar( *ptr++ );
	}
	return len;
}

But even with this change, printf doesn't work.

- It seems that it is mandatory to click on "Start Trace" on "SWV ITM Data Trace" each time I want to use the console display. So I have to do it every time I run a new debug session. There is a easy way to enable by default the "Start Trace " at each debug session?

Thansk for your help.

3 REPLIES 3
Flavio Alves
Associate II

Here it worked when I put the following instruction at the beginning of the main.c file:

#ifdef _GNUC_
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif / _GNUC_ /

Hope it helps.

Best regards,

Flavio

Fabien B
Associate III

Thanks a lot for your help.

I can't test it now, but I will do it.

Lama
Associate III

I think this answer deserves better explanation.

Well imho there are 3 options:

  1. You change the _write implementation
  2. You overwrite the "weak" implementation of _write with your own implementation
  3. You implement __io_putchar

If you choose 1 and the code is generated again then your changes are lost.