how to retarget newlib printf scanf to embedded systemUART ?

Anil Paranjape
Fri Aug 6 07:53:00 GMT 2004

Hi Wayne,

You need to do following things to redirect stdout to UART.
1) Please include syscalls.c in your project from following path,
2) Without changing any other functions from syscalls.c, change only _read and _write functions 
   like following.

	int _read (int file, char *ptr, int len)
    	    /* GetChar : Your implementation to receive the character 
      	 from the serial port.*/
    	       return (1);
	int _write(int file,char *ptr,int len)
	    int i;
 	    /* PutChar : Your implementation to send the character to the 
          serial port.*/
	    return len;
This implementation of redirecting stdin and stdout to serial port will override default 
implementation of stdin and stdout. 
Do not forget to include directories with required header files in your project.

This implementation works for H8/300 and SH targets. It should also work for ARM target.
Please refer to following link for more details,

Good Luck.

Anil Paranjpe
KPIT Cummins InfoSystems Ltd.
Pune, India

Free download of GNU based tool-chains for Renesas' SH and H8 Series.
The following site also offers free technical support to its users. 
Visit for details. 
Latest versions of KPIT GNU tools were released on June 1, 2004.

>>From: "wayne lee" <wnlee at cad dot csie dot ncku dot edu dot tw> 
>>To: <newlib at sources dot redhat dot com> 
>>Date: Fri, 6 Aug 2004 11:20:10 +0800 
>>Subject: how to retarget newlib printf scanf to embedded systemUART ? 

>>Hi folks, 
>> I successfully use GNU arm-elf toolchain to build a ROM image.
>> My program is linked with newlib.
>> Now, I am wonder how to retarget newlib stdin, stdout to UART
>> Does anyone who can give me hints?
>> I have ever use ARM ADS1.2 to build ROM image.
>> ADS provides a retarget mechanism likes bellows:
>>struct __FILE { int handle;   /* Add whatever you need here */};
>>FILE __stdout;
>>FILE __stdin;
>>void _ttywrch(int ch)
>>    char tempch = ch;
>>    write_ch_to_uart( &tempch );
>>int fputc(int ch, FILE *p_fd)
>>    /* Place your implementation of fputc here     */
>>    /* e.g. write a character to a UART, or to the */
>>    /* debugger console with SWI WriteC            */
>>    char tempch = ch;
>>    write_ch_to_uart(&tempch);
>>    return ch;
>>int fgetc(FILE *p_fd)
>>    /* Place your implementation of fgetc here        */
>>    /* e.g. read a character from a UART, or from the */
>>    /* debugger console with SWI ReadC                */
>>    char ch;
>>    ch = get_ch_from_uart();
>>    return ch;

More information about the Newlib mailing list