cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly setup printf with Keil µVision and STM32L151C8T6A ?

Olivier FAURAX
Associate II

Posted on January 02, 2017 at 12:42

I'm unable to get the printf output in the keil µVision IDE, so I need a

https://community.st.com/tags♯/?tags=checklist

to know if my setup is correct.

In the Keil project, I configured ST-Link Trace with Core Clock 32Mhz and all ITM port ticked (only number 0 needed, perhaps ?).

I run the debugger with the 'printf window' open.

I tried:

  • printf
  • ITM_SendChar

And nothing shows in the printf window. Is there something I missed to check?

#checklist

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 02, 2017 at 14:57

It is where the trace data comes out, so yes.

For printf() to work in Keil you must supply supporting routines to use ITM_SendChar()

//******************************************************************************
// Rough SWV support in Keil, requires PB3/SWO to be connected to debugger
//******************************************************************************
 
#include <rt_misc.h>
 
#pragma import(__use_no_semihosting_swi)
 
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
 
int fputc(int ch, FILE *f)
{
 ITM_SendChar(ch);
 
 return(ch);
}
 
int fgetc(FILE *f)
{
 char ch;
 
ch = 1;
 
 return((int)ch);
}
 
int ferror(FILE *f)
{
 /* Your implementation of ferror */
 return EOF;
}
 
void _ttywrch(int ch)
{
 ITM_SendChar(ch);
}
 
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
 
//******************************************************************************
 �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4
Posted on January 02, 2017 at 12:50

Is PB3 SWO connected to the ST-LINK?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 02, 2017 at 14:35

Thanks for the reply and happy new year.

No, PB3 is not connected.

According to my doc, only PB4 (TRST), PA14 (SWCLK) and PA13 (SWIO) are connected.

I try to use Serial Wire, am I required to connect PB3?

Posted on January 02, 2017 at 14:57

It is where the trace data comes out, so yes.

For printf() to work in Keil you must supply supporting routines to use ITM_SendChar()

//******************************************************************************
// Rough SWV support in Keil, requires PB3/SWO to be connected to debugger
//******************************************************************************
 
#include <rt_misc.h>
 
#pragma import(__use_no_semihosting_swi)
 
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
 
int fputc(int ch, FILE *f)
{
 ITM_SendChar(ch);
 
 return(ch);
}
 
int fgetc(FILE *f)
{
 char ch;
 
ch = 1;
 
 return((int)ch);
}
 
int ferror(FILE *f)
{
 /* Your implementation of ferror */
 return EOF;
}
 
void _ttywrch(int ch)
{
 ITM_SendChar(ch);
}
 
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
 
//******************************************************************************
 �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 06, 2017 at 10:55

For the record, I added the connection to PB3, and I only needed to include stdio.h and to define fputc.