How to use printf function in S32DS for Power Architecture using EWL library

Document created by Martin Kovar Employee on Jun 23, 2016Last modified by Diana Torres on Feb 24, 2017
Version 5Show Document
  • View in full screen mode

Function printf is C library function, which sends formatted output to stdout. Because microcontroller does not contain stdout, it it necessary to redirect it to a different type of output. One of the possible way is using UART. All MPC57xx has LinFlexD module, which supports UART mode.


For proper use the LinFlexD module, it is necessary to initialize it and also create simple functions for transmit and receive data. You can create your own file or use the file uart.c in the attachment. If you want to create your own file, please do not forget to implement the methods, which are called by MW MSL libraries to perform console IO (please look at the attached file uart.c).


In uart.h file, you will find correct function prototypes for functions used in uart.c and also some important enum data types.


Third important file is uart_console_io.c which you find in folder, where S32DS was installed. Open the folder and choose path: S32DS/e200_ewl2/EWL_C/src/sys. This file contains routines which implement low-level console IO routines.


Now, we have all basic files which help us to use printf function. So lets create new project and test printf. Example project is created for MPC5744P.


Create new project, choose EWL library and finish project wizard. Include stdio.h to the file, where you want to call printf function.


Now add uart.c and uart_console_io.c files to the project folder src. Add uart.h file to project folder include.


Initialize system clock and clock for peripherals (look at the attached project). In uart.c file, there are settings for GPIO pins, LinFlex initialization for UART mode and also baud rate calculation (please look at the uart.c file in the attachment).


Now it is possible to use printf function. Important point is, that \n character triggers the data sending. Also, if you do not use this \n character, you will get compile error, but I will explain this further in this tutorial.


Run some terminal emulation, for example Putty and use the following settings:


Now open terminal and  run the code in the microcontroller. This should be the result:






This was the easiest way, how to use printf function. But printf also provides some format characters for printing decimals, floating points, etc. All options you can find in ewl_c_reference.pdf document which is place in S32DS installation folder S32DS/help/pdf.


When you want to use some of the characters mentioned above, you get the following compile error:


When you do not use any format characters in printf argument, function _EWL_CDECL puts(const char_t * s) from puts.c file is called on background. But when you use format characters in printf argument, function int_t _EWL_CDECL printf(const char_t * _EWL_RESTRICT format, ...) from printf.c file is called instead of puts.


Variable result saves value, which function __pformatter returns. But function __pformatter is not implemented. Instead of __pformatter, function __pformatterFP (placed in __printformat.c file) is implemented.


So there are two possible workarounds.


First workaround is place the following code to the Linker flags in project properties:
-Xlinker --undefined=__pformatterFP -Xlinker --defsym=__pformatter=__pformatterFP


The code says that there is undefined symbol __pformatterFP (part -Xlinker --undefined=__pformatterFP) and define symbol alias __pformatter=__pformatterFP (-Xlinker --defsym=__pformatter=__pformatterFP ). So from my point of view, symbol __pformatter is replaced by __pformatterFP. Now the correct function is called and the project could be successfully built.



Second workaround is add the printf.c file from S32DS installation folder S32DS/e200_ewl2/EWL_C/src/stdio to the project src folder and exchange __pformatter for __pformaterFP. Clean project and recompile.




I hope this document helps you while implementing printf function in your projects.