Clear Filters
Clear Filters

Level 2 S Function variable type input

5 views (last 30 days)
Frederick Cunningham
Frederick Cunningham on 28 May 2022
Edited: Aiswarya on 6 Oct 2023
I am trying to create a level two s function with custom C, TLC, and a generator script. I am working with a legacy codebase that I cannot copy paste into this question. However, what I am trying to do shouldn't be too complicated. I want an s function to be able to take as input any bus type. Then, in C, I cast that input variable to (void *) and pass it into my C function in mdlOutputs. The existing code has a mask parameter where you manually type the type name into the sfunc and then use ssRegisterTypeFromParameter, and ssSetInputPortDataType to set the input type of the block. To be honest I do not know enough about matlab and simulink to be doing this, but I am pretty sure there should be a way to inherit a bus type. I don't understand why it asks for the input port width when I am telling it that it will inherit the input port type? I have tried using ssSetInputPortDataType(S, 0, DYNAMICALLY_TYPED); and including the required helper functions
void mdlSetDefaultPortDataTypes(SimStruct *S)
{
if (ssGetInputPortDataType(S, 0) == DYNAMICALLY_TYPED) {
ssSetInputPortDataType(S, 0, SS_UINT32 );//I chose uint32 randomly??
}
}
and
void mdlSetInputPortDataType(SimStruct *S, int_T port, DTypeId id)
{
ssSetInputPortDataType(S, port, id);
}
but I keep getting errors about my input/output dimension. And errors that mention type. It would be really helpful if someone could point me to a code example of a level 2 s function written in C that can take any user defined bus type as an input. Or if that code example doesn't exist, what are the required setup functions and parameters to do something like this? As a last question, I am making this model for a code base that is converted into C code before it is compiled which I think is why there is a custom TLC included in the generator script, but if anyone could explain why the TLC is necessary/what it is doing that would be very helpful as well.
Sorry I can't post more specific examples, and thanks for looking at the question!

Answers (1)

Aiswarya
Aiswarya on 6 Oct 2023
Edited: Aiswarya on 6 Oct 2023
Hi Frederick,
I understand that you want to pass a bus type as input to a Level 2 S-function. I am attaching a zip file (sfun_bus.zip) which contains a Level 2 S-function file which takes a bus signal as input (sfun_bus_dynsize_ex.c) and a model (bus_sfun_ex.mdl) which uses the S-function.
You may refer to this MATLAB answer to understand more about the example code:
Another way to use bus as input in creating a S Function is to use a S-Function Builder. The following documentation gives a detailed explanation to do the same: https://www.mathworks.com/help/simulink/sfg/building-s-functions-automatically.html
TLC(Target Language Compiler) is used to customize generated code and produce platform-specific code. It also helps leverage the capabilities of Simulink while incorporating your legacy code. For more information on the working of TLC refer https://www.mathworks.com/help/rtw/tlc/what-is-the-target-language-compiler.html

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!