S-function is eliminated through code optimization
Show older comments
Hi all
I want to create an S-function by using the S-function Builder to generate a wrapper function which calls external code. The example purpose is to read a pressure sensor. It accepts an index input for which pressure sensor 1 through 8 we want to get the pressure from, and will output the pressure:
- Input: uint8 index
- Output: single pressure_volts
When I create such an S-function using the S-function Builder, and connect a Constant block with the index of the pressure sensor, the wrapper function is optimized away by code generation.
I have made an example below, which can also be found in the attachment (R2020b):

- The function 'A' is the function with an input and an output, of which the input is connected to a Constant block with value '3'.
- The function 'B' is a function with just an output.
- The function 'C' is a function with just an input.
When we go look in the generated code, we only find back the wrapper functions for functions 'B' and 'C' in the step-function:
* Model step function */
void test_codegen_sfunctions_step(void)
{
get_pressure_B_Outputs_wrapper(&test_codegen_sfunctions_Y.output_B);
get_pressure_C_Outputs_wrapper(&test_codegen_sfunctions_U.input_C);
}
The 'output_A' the 'A'-function is connected to, is initialized to 0 and never updated again:
/* Model initialize function */
void test_codegen_sfunctions_initialize(void)
{
(...)
test_codegen_sfunctions_Y.output_A = 0.0F;
}
This means the wrapper function for function 'A' is never called.
How do I force Embedded Coder to properly generate code from this S-function, and not optimize it away? Of course, the code generator may inline the Constant value '3' on the location where it calls the wrapper function, but it may not negate the call completely.
Thanks in advance!
Jonas
Accepted Answer
More Answers (0)
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!