Skip to main content
samiassaad
Associate III
March 19, 2011
Question

Printf in Keil

  • March 19, 2011
  • 13 replies
  • 3594 views
Posted on March 19, 2011 at 09:36

Printf in Keil

    This topic has been closed for replies.

    13 replies

    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    printf is a standard library function; it is defined by the 'C' standard: it sends its output to stdout - nothing else.

    So adding a parameter is not just ''retargetting'' - it is completely changing the standard behaviour!

    If you want to specify the output destingation in a standard library file, you will have to us fprintf or similar.

    Or you could redefine stdout between calls.

    Or create your own function that specifies a destination.

    Or did you want a function that sends its output to two destinations simultaneously?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    Look at how you've hosting the lcd/usart now, and have your ''sendchar()'' function send characters to both.

    Or use sprintf() to generate data for both in your own ''DebugPrintf()'' function.

    #include <stdio.h>

    #include <rt_misc.h>

    #pragma import(__use_no_semihosting_swi)

    struct __FILE { int handle;     /* Add whatever you need here */};

    FILE __stdout;

    extern int sendchar (int ch);

    int fputc(int ch, FILE *f) {

      return (sendchar (ch));

    }

    int ferror(FILE *f) {

     /* Your implementation of ferror */

      return EOF;

    }

    void _ttywrch(int ch) {

      sendchar (ch);

    }

    void _sys_exit(int return_code) {

    label:  goto label;  /* endless loop */

    }
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    samiassaad
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    Yes I want a function that sends its output to two destinations simultaneously, any suggestions ?

    samiassaad
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    Thank you Clive

    already I know this and I am retarget it to my lcd routine, my question is it possible to retarget it to 2 routinesor more?

    seial send char, glcd ?

    Thank you

    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    Spooky

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Andrew Neil
    Super User
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    ''have your 'sendchar()' function send characters to both''

     

    See: 

    http://www.keil.com/forum/18511/

    (it's for C51, but the principle is the same)

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:28

    The point Andrew and I made is that you could do it like this :

    int sendchar (int ch) // Hosting function character output routine

    {

         sendchar_usart(ch);

        sendchar_lcd(ch);

        return(ch);

    }

    Why would this NOT work?

    If your clocks remain busted I could see the USART not outputing at the right baud rate. But that could be fixed by tweaking the baud rate register.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    dannym
    Associate III
    July 28, 2012
    Posted on July 28, 2012 at 07:47

    Yeah somebody help me out here.

    I'm using Keil and STLink on the STM32F4Discovery board and I see in uVision there's a window for ''Debug (printf) Viewer''.  Which I assume takes printf() and xfers it through the attached STLink I'm debugging with, which sounds great to me!

    But I use printf() and it jumps to the reset vector.  There's no compile warning about stdout being unimplemented or anything.  What do I need to put in to transfer the data across the STLink?

    dannym
    Associate III
    July 28, 2012
    Posted on July 28, 2012 at 09:01

    I've tried following the instructions here:

    http://armcortexm.blogs.upv.es/stm32f4-discovery-and-printf-redirection-to-debug-viewer-in-keil/

    Yeah the Keil tab I was using was ''STLink (deprecated)'' but just changing to ''STLink Debugger'' led to ''Device Not Found''.  I made all the changes specified up there and the debugger works again.  It doesn't bomb on printf, however, I don't see anything printing out in the printf() Viewer window.

    dannym
    Associate III
    July 29, 2012
    Posted on July 29, 2012 at 05:19

    Ah, got it!

    The instructions listed in that blog link will work.  However, as the author mentioned, it doesn't auto-program when entering Debug.  I got no printf() output because I was running stale code.  

    The fix is under ''Utilities->Update Target Before Debugging'' (check, obviously).