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

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

1,390 Views
markusransberge
Contributor III

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
    coder.cinclude('stdio.h');
    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
end

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,
Markus


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.

0 Kudos
4 Replies

1,125 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi Markus, 

Here is a basic example i've used to send some text over the UART. It is now pretty but it may be used to sent char by char of any text.

The problem is with variables since Ecoder does not support code generation for functions like num2str().

The model looks like this:

pastedImage_1.png

 Using the Byte_Pack/Byte_Unpack you may concatenate various strings. The string to char serialization is done in the state-flow.

Running this on the MPC5744P DevKit that is connected over the microUSB SDA port with produce:

 pastedImage_2.png

It will be nice if you could find a way to concatenate numbers. 

I see that Simulink has some specialized blocks for Encode/Decode that might do what you want:ASCII Encode/Decode - Simulink . Unfortunately, i do not have a license for that toolbox to give it a try.

Does it helps?

Best regards,

Daniel

0 Kudos

1,125 Views
markusransberge
Contributor III

Thank you for your suggestion. I can't open your Simulink model since I'm one version behind yours (2016b) but I do see what you did there. Your solution combined with mine would probably be the best but yours also has one drawback: the '\0' after each char. So: Would it be possible to get rid of the '\0' chars between the sent characters or is this limited/hard-coded by the LinFlex output block?

My solution right now is to use a hard-coded string length and the sprintf_s function which uses the predefined string as a buffer to format every value to. This has the disadvantage that it will always put out the whole string (not used space is filled with '\0's).

The ASCII encode/decode blocks are - as I already said - not an option because they fail to compile. (Heap related?)

Maybe just writing out raw data and formatting on the receiving end is the way to go. I can accept that if this is not easily possible like setting a compile flag or similar tricks.

Best regards,

Markus

0 Kudos

1,125 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi markusransberger‌,

I think you will find this interesting. With MATLAB/Simulink R2018a there are new block-set for STRING manipulation within SIMULINK models: What’s New in R2018a! » Guy on Simulink 

pastedImage_1.png

Best regards,
Daniel

0 Kudos

1,124 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi Markus, 

I have exported to 2015b to be easier for importing in various formats.

Would it be possible to get rid of the '\0' chars between the sent characters or is this limited/hard-coded by the LinFlex output block?

I shall check this tomorrow. Now that you pointed out - i also noticed. I used it on a different hardware S32K and it was compact.

The ASCII encode/decode blocks are - as I already said - not an option because they fail to compile. (Heap related?)

To0 bad i can't test. I'll try to get an evaluation license from MATHWORKS. What's the error? Perhaps we can tweak the linker command file.

Best regards,

Daniel

0 Kudos