S-function is eliminated through code optimization

2 ビュー (過去 30 日間)
Jonas 2021 年 6 月 9 日
編集済み: Jonas 2021 年 6 月 9 日
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)
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 2021 年 6 月 9 日
編集済み: Jonas 2021 年 6 月 9 日
One solution is to create a Simulink.Parameter object with the value of the index, and Storage Class Const. Then use the parameter as input to the S-function. The wrapper function will not be eliminated and the named constant object is provided as an input.
However, this employs creating additional variables which encumbers the visibility of which index is called.

その他の回答 (0 件)


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!

Translated by