cancel
Showing results for 
Search instead for 
Did you mean: 

F0 Discovery - fprintf function implementation

wbarkley
Associate II
Posted on August 02, 2012 at 22:05

Wondering if anyone in the forums has made an fprintf function or equivalent?  I got characters spitting out to hyperterminal, now just trying to figure out to spit out variable info to the screen.

6 REPLIES 6
Posted on August 02, 2012 at 22:32

Or sprintf()?

Things like Keil support printf(), and output via the serial port of the target, presumably you're using something else. Printing hex or decimal numbers isn't that hard.

char *dec_u32(unsigned long i)
{
static char str[16];
char *s = str + sizeof(str);
*--s = 0;
do
{
*--s = '0' + (char)(i % 10);
i /= 10;
}
while(i);
return(s);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
wbarkley
Associate II
Posted on August 06, 2012 at 20:56

Can you give me an example of using printf.  My code throws an exception when I use printf.  Code compiles just fine, just crashed when it runs.  How does printf know to use USART1 or USART2?

Andrew Neil
Evangelist
Posted on August 06, 2012 at 21:00

''Can you give me an example of using printf''

The obvious place to look would be the documentation for the specific toolset that you are using.

You need to study that documentation carefully for any particular requirements of that toolset...
Posted on August 06, 2012 at 21:07

Can you give me an example of using printf. My code throws an exception when I use printf. Code compiles just fine, just crashed when it runs. How does printf know to use USART1 or USART2?

Sounds like you're linking the wrong libraries, or not hosting/retargeting properly. You don't specify a tool chain, I'm not interested in guessing. In Keil, you'd code the USART desired into the retargeting code.

//******************************************************************************
// Hosting of stdio functionality through USART2
//******************************************************************************
#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)
{
static int last;
if ((ch == (int)'
') && (last != (int)'
'))
{
last = (int)'
';
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, last);
}
else
last = ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET);
ch = USART_ReceiveData(USART2);
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
static int last;
if ((ch == (int)'
') && (last != (int)'
'))
{
last = (int)'
';
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, last);
}
else
last = ch;
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, 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..
wbarkley
Associate II
Posted on August 08, 2012 at 16:58

I am a NOOB so bare with me.

I am using Keil.

I will have to find info for toolchain to get through some of this confusion.

Is the redirect code a standard header/c provided by Keil that I should see once I include in program and then modify?  Or you write you own and somehow the toolchain knows to look for it via some setting in the Keil environment?

Sorry, learning curve - been out of the game for a while.

Posted on August 08, 2012 at 17:10

Pasting the above code, along with your own code to initialize the pins and the USART should be sufficient. Modify as required.

The #pragma causes the provided code to be called by puts(), printf(), fputs(), etc.

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