AnsweredAssumed Answered

Is there any chance to get "(f)printf" or similar IO functions to work? (MPC5744P)

Question asked by Markus Ransberger on Feb 27, 2018
Latest reply on Mar 21, 2018 by dumitru-daniel.popa

Hello community,

just a question: is it easily possible to get console IO functions to work within the MBD toolbox? I want to send formatted strings over UART (LINFlex).

What I tried so far:

  • The ASCII Encode block of the real-time toolbox in Simulink sadly fails to compile (actually without a real reason). Probably because of dynamic memory allocation or stating explicit console IO support.
  • Next try was to use a S-Function to accept input variables and using printf() within the source files. Additionally I tried to include the needed header/library files similar to the UART code example which comes with S32 IDE (io_console.h) and changed some flags accordingly. This leads to multiple problems emerging. Firstly multiple constants are defined twice since the S-Function compiler is using standard MinGW toolchain, secondly it goes deep down the rabbit hole of many compiler flags and stops somewhere at needing a memory space declared as heap (at least it tells me).
  • The only thing what works is a special construct as MATLAB function which I found on Stackoverflow. It reads as follows:

function s = printfUnsigned(f, varargin) % f = input string + specifier , varargin = variables

    len = 3;    % string length hardcoded
    s = char(zeros(len,1));
    %cs = [s; 0]; % NULL-terminated string for use in C
    coder.ceval('sprintf_s', coder.ref(s(1)), uint32(len), coder.rref(f(1)), varargin{:}); % s = cs

This uses sprintf_s with a hardcoded string length (so no buffer) and this actually works. Then you can just send the string out over the LinFlex block. The strings look horrible, though, because of the fixed size.


So is there a possibility to use any other standard C IO function or is it just not possible?


Many thanks,


EDIT: I also tried the built-in MATLAB conversion functions like int2str and similar, of course. These also allocate memory on the fly, so they don't work.