How printf() is calling up the UART driver

shariffmd wrote on Thursday, December 25, 2014:

I am newbie to freertos, please help me understand how printf() is hooked/landed up to UART driver API.

I have seen reference driver serial driver, in general serial drivers are implementing,
XXX_serial_init(), XXX_serial_get_char(), XXX_serial_put_char(), XXX_serial_put_string() APIs.

Could some please explain how printf() call flows to UART driver? Also please point me to reference driver.

heinbali01 wrote on Thursday, December 25, 2014:

If I’m not mistaken, this happens in printf-stdarg.c

If snprintf() is called with NULL as as buffer, the function vOutputChar() will be called for every byte:

extern void vOutputChar( const char cChar, const TickType_t xTicksToWait  );

/* here the logging function is called printk()
(just like Linux). It calls print() with a NULL
pointer: */

int printk(const char *format, ...)
        va_list args;
        va_start( args, format );
        return print( 0, format, args );

This function vOutputChar(), which should be provided by the application, shall forward the byte to a serial console or whatever.

“printf-stdarg.c” never got a standard location in projects, you’ll find it in the home directory of many projects.

The above method of logging is very economic: each byte formatted goes straight to the serial driver.
You’ll get a smoother output when XXX_serial_put_string() is used for a whole string.


jradoslav wrote on Friday, December 26, 2014:

Low driver functions are interfacing this feature.
standard output/input function std_put, STD_get , this is function pointers , have to be set as serial driver functions uart_put, uart_get

rtel wrote on Friday, December 26, 2014:

Really this question falls outside the scope of FreeRTOS support as it is completely dependent on the development environment you are using - and FreeRTOS support more than 18.

As per Radoslav’s comment - first you need the low level driver functions to perform outout to whichever interface you want (UART, CDC, TCP, etc.) so make sure that is working.

Then you need to read the library section of the documentation for whichever development environment you are using to find out how you connect that to the libraries. Sometimes it is as easy as providing your won write() function that calls your low lever drivers, sometimes it is not that easy.

As per Hein’s reply there is a file printf_stdarg.c that can be used, but this is not part of the kernel itself - just included in lots of different demos. If you choose that you can replace the sprintf/printf function provided by your library with a much simpler, leaner, and so also less powerful and slower version (the big advantage of is it uses very little stack) and follow Hein’s example.