cancel
Showing results for 
Search instead for 
Did you mean: 

Printf in Keil

samiassaad
Associate II
Posted on March 19, 2011 at 09:36

Printf in Keil

13 REPLIES 13
Andrew Neil
Evangelist III
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?

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

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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist III
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)

Posted on May 17, 2011 at 14:28

Spooky

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
samiassaad
Associate II
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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
dannym
Associate II
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 II
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.