fprintf - Semihosting and Directing to UART

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

fprintf - Semihosting and Directing to UART

1,309 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kwgilpin on Fri Jan 13 16:58:54 MST 2012
Is there a way, based on a file descriptor, to direct the output of fprintf to either the debugger or the UART.  For example, using a file descriptor of 1, I'd like fprintf output to be displayed in the debug terminal.  Using a file descriptor > 2, I'd like fprintf output to be sent to the UART. 

When I retarget printf to the UART using my own __sys_write(), I lose the ability to send debugging info to the debug console.  I want to avoid this.

Thanks,
Kyle
0 Kudos
Reply
5 Replies

960 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by js-nxp on Sat Jan 14 15:30:18 MST 2012
Unfortunately, particularly for me :mad: I haven't got my head around printf YET with the LPCXpresso environment.
http://knowledgebase.nxp.com/showthread.php?t=2696
Been too busy trying to get other stuff happening.

Regardless of people's feelings about whether printf etc. should be used in a small embedded environment I WANT to use it as it is a simple, STANDARD way, of printing messages like in your case. If a micro has more than 1 UART (don't know if this is the case here) then fprintf it's a nice way of doing both jobs.

fprintf is also nice to use with a LCD or other display attached.
fprintf (display,("\rV %c.%c"), version_major, version_minor);
printing to stdout (USART here)
printf (ASCII_OUTPUT);   //ASCII Output instead of binary
the hex file for the project where the above is used is less than 16K (35K in hex format).
0 Kudos
Reply

960 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kwgilpin on Sat Jan 14 13:18:38 MST 2012
Okay, sounds totally reasonable and more likely to produce readable code. 

Thanks for the quick response, especially over the weekend!
0 Kudos
Reply

960 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Sat Jan 14 11:31:39 MST 2012
I would suggest that a better way to achieve your aim of having output to both UART and debug console would perhaps be to implement your own UART outputting function which sits on top of vsprintf:

[SIZE=2][FONT=Courier New]int vsprintf (char * str, const char * format, va_list arg );[/FONT][/SIZE]

There is a simple of example of doing this at:

http://www.cplusplus.com/reference/clibrary/cstdio/vsprintf/

Then you could just call printf to output to the debug console, and your vsprintf based function to output to the UART.

Redlib's printf actually uses vsnprintf rather vsprintf, which is very similar but the caller also specifies the maximum size of the buffer.

[SIZE=2] [FONT=Courier New]int vsnprintf(char *buffer, size_t bufsize, const  char *format, va_list ap);[/FONT][/SIZE]

Regards,
CodeRedSupport
0 Kudos
Reply

960 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kwgilpin on Fri Jan 13 21:24:16 MST 2012
UART will be used to communicate with a modem (that takes AT-commands). 

The semihosted debugging console will still be used for true debugging strings.
0 Kudos
Reply

960 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by js-nxp on Fri Jan 13 21:17:19 MST 2012
If you have a debugger why do you want to send printed stuff out for debugging? (I know that OCCASIONALLY it may be necessary to do it)

And if you need to you can always attach a terminal program to the UART.

Untill I started playing with the LPCXpresso I NEVER have used printf (fprintf) as a debugging tool, 100% of the cases it has been used to talk to the hardware even if I need to print something like modified variables. It goes to the UART or whatever stream.
0 Kudos
Reply