Skip to main content
Olivier FAURAX
Associate II
January 2, 2017
Solved

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

  • January 2, 2017
  • 1 reply
  • 1601 views

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

    This topic has been closed for replies.
    Best answer by Tesla DeLorean
    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 */
    }
     
    //******************************************************************************
     �?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

    1 reply

    Tesla DeLorean
    Guru
    January 2, 2017
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Olivier FAURAX
    Associate II
    January 2, 2017
    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?

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    January 2, 2017
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..