How to use ssRegister​DataTypeIn​teger with Simulink Coder ?

Hi everyone,
I'm struggling for a while now (about a month) to use the ssRegisterDataTypeInteger function in my app. I had to test it out of my application to find the issue. When i'm running my s-function using ssRegisterDataTypeInteger (libfixedpoint) through Simulink it works perfectly. Then I'm building with simulink (C Code -> Build), it's working almost like a charm (had some issues with linking libfixedpoint.lib but seems to be solved), and when i'm trying to run the resulting .exe it's failing when calling ssRegisterDataTypeInteger, any clue about what's going on here ?
I've also tried the proposition of Mikhail on the following thread and it has given me the same behaviour :
https://fr.mathworks.com/matlabcentral/answers/345944-how-to-create-64-bit-integer-data-type-in-simulink

 Risposta accettata

Andy Bartlett
Andy Bartlett il 12 Gen 2021
Modificato: Andy Bartlett il 12 Gen 2021
Hi
I've created an example for cases where the types can vary per instance of the s-function.
The core of the s-function is contained in a C code file and header.
This C file is shared between simulation and code generation.
This code recieves attributes of the input and output ports using a structure.
Based on the contents of these attribute structures the core code dispatches to the correct behavior for each instance.
The wrapper C code and TLC code for the s-function need to fill in the structures,
then call the core function.
How the attribute structures are filled in is distinct to C and TLC.
But the core all comes from the same C file and header.
The example shows one possible way to organize the port information and do the dispatching.
Using the same concept, use whatever specific way works best for your application.
This example if found at GitHub-DynamicTypeHandling.
Please review and run runMeFirst.m to exercise the example.
-Andy

1 Commento

Many thanks ! I'm currently working on a small project merging your kind example and sfcndemo_sfun_multiport, which is a simplified version of my real project ! I'll keep you posted in the few days, if the problem is completely solved !

Accedi per commentare.

Più risposte (2)

Hi Brieuc,
As I mentioned in the thread you've linked to (https://www.mathworks.com/matlabcentral/answers/345944-how-to-create-64-bit-integer-data-type-in-simulink#comment_850989)
4) And the worst. The S-Function using 64 bits data types must be inlined (this is from my experience, please tell me I'm wrong) for target-based builds. I'm not against throwing in some TLC, but I would really prefer not to inline S-Function in some cases. This also makes this API less capable than true built-in data types, where I don't have to worry about it.
So this might be problem.
But I can't tell fo sure unless you provide some reproduction steps and specific error messages.

1 Commento

Thanks for your answer,
It helps a lot, can i abuse your kindness and asks you a very simple example of inlining a sfunction for 64 bytes like a sfunction adding 1 to the input ?
Thanks again for you answer !

Accedi per commentare.

Andy Bartlett
Andy Bartlett il 11 Gen 2021
Modificato: Andy Bartlett il 11 Gen 2021
Hi
A low effort way to inline an s-function is to use "wrapping".
The general concept of "wrapping" an s-function is described here.
I've created an example that is more specific to use of integer types including int64.
The sfunction's c, h, and tlc files can be found on github.
Please review and run runMeFirst.m to exercise the example.
Please note, this example only covers cases where the inner function being wrapped has a statically typed interface. Such as the github example.
uint64_t func_core_hybrid_add(uint64_t const a, uint32_t const b);
In other words, no polymorphism via void pointers or such.
If you need an interface that handles port types that are different for different instances of the s-function, that can be wrapped too. But that needs a different wrapping technique.
-Andy

8 Commenti

Thanks a lot for the kind answers !
I've tried to run the RunMeFirst and i've this error
Error using coder.internal.callMakeHook
Embedded Coder is not licensed or not installed.
Is the Embedded Coder really needed for our purpose, or is this just a version problem (I'm downloading 2020b to edit the simulink models) ?
Hi,
S-Functions are inlined for several purposes.
Depending on the purpose, different licenses are required.
For simulation of a model in accelerator mode, Coder licenses are NOT required.
To build stand-alone C (C Code -> Build),
for Generic Real-Time target (GRT)
MATLAB Coder and Simulink Coder are required
for Embedded Real-Time target (ERT)
Embedded Coder, MATLAB Coder and Simulink Coder are required.
The example model was configured to use ERT target hence the need for all 3 C Coder licenses.
I've uploaded a change to GitHub to use GRT.
For building standalone C code, that will not require Embedded Coder,
but will require Simulink Coder and MATLAB coder for the (C Code -> Build)
-Andy
A great thank you, i was pretty sure i could use GRT, it's what we were doing in other projects, I just needed to access the .slx (with version 2020b). I'll keep you updated but i feel that you've given every ressources that i needed !
Greetings
B.Daniel
Thanks Andy!
I just wish ssRegisterDataTypeInteger was documented.
In fact i just mind I need to handle port types (was a bit late for me yesterday), have you a documentation to read to show the "other" wrapping technique that we need to use !
Thanks again for all the support on this request !
Hi B.Daniel,
Clarifying question:
>> I need to handle port types
Does this mean different instances of your s-function handle different port data types?
For example, suppose your s-function is in two places
myModel/mySfunBlockInstance1
myModel/mySfunBlockInstance2
and in both cases the block has two inputs and one output.
The 1st instance might have port data types uint8, int16, int32.
The 2nd instance might have a different set of port data types such as uint64, uint64, uint64.
Is that a correct understanding?
-Andy
>> I just wish ssRegisterDataTypeInteger was documented.
I've entered an documentation enhancement request. :-)
-Andy
Hi,
It's quite complex.
The sfunction is used in three mode:
  • One connecting through a domain
  • One publishing to a shared memory
  • One subscribing to a shared memory
We've got M input and N outputs (almost everytime M:M) and we can have every combinations of input ports. The number and datatypes of inputs are given though a simulink mask.
I don't know if i'm clear enough ! Feel free to ask complementary informations if needed !

Accedi per commentare.

Categorie

Scopri di più su Simulink Coder in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by