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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

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

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

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.

154199_154199.pngpastedImage_1.png

 

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

154200_154200.pngpastedImage_2.png

 

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

154202_154202.pngpastedImage_13.png

 

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.

154205_154205.pngpastedImage_16.png

 

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

154204_154204.pngpastedImage_15.png

 

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

154206_154206.pngpastedImage_17.png

 

/*************************************************************************************************************************************************************************************/

/*************************************************************************************************************************************************************************************/

/*************************************************************************************************************************************************************************************/

 

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:

154207_154207.pngpastedImage_18.png

 

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.

154208_154208.pngpastedImage_5.png

 

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

154209_154209.pngpastedImage_6.png

 

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.

154210_154210.pngpastedImage_24.png

 

 

 

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

 

Regards,

Martin

标签 (1)
附件
评论

Thanks for the info. Do you have any hints on how to do the same for a project using the "newlib" C library ? 

无评分
版本历史
最后更新:
‎06-23-2016 05:24 AM
更新人: