Contenuto principale

Customize C Functions Generated from Simulink Functions Scoped in Subsystems

In the generated model code, each Simulink function corresponds to one of the generated functions. The return value and input arguments of the generated function represent the Argument Inports and Argument Outports of the Simulink function, and the function body corresponds to the Simulink function logic.

A scoped Simulink® function is a Simulink Function block with the Function visibility parameter of its Trigger block specified as scoped. When a scoped Simulink function is nested inside a subsystem, it is callable from the subsystem it is located in and from any subsystem in its hierarchy. To read more about scoped Simulink functions in subsystems, see Scoped Simulink Function Blocks in Subsystems.

This example shows how to customize the name, return value, input arguments, and qualifier of a function generated from a scoped Simulink function nested inside a subsystem.

Tip: For this example, we refer to this Simulink function as nested.

Open Model and Explore Nested Simulink Function Block

Load the model SimFuncs and open the Simulink Function block nestedSimFunc inside the subsystem subsystemWithSF.

sfModel = "SimFuncs";
nestedSf = "SimFuncs/subsystemWithSF/nestedSimFunc";
load_system(sfModel)
open_system(nestedSf)

Simulink Function block nestedSimFunc is open in model SimFuncs. The name of the trigger block is n_func.

In nestedSimFunc, double-click the Trigger block n_func to open its Block Parameters dialog box.

Block Parameters dialog box of n_func. The Treat as Simulink function checkbox is selected. The Function visibility is specified as scoped.

Treat as Simulink function is selected and Function visibility is set to scoped.

Customize Generated Function Name and Argument Identifiers

You customize the names and argument identifiers of functions generated from nested Simulink functions (scoped inside any level of model hierarchy) by specifying the configuration parameters in the table. Specify each naming rule as a combination of valid C language characters and the available macros for the parameter. The specified naming rules apply to all nested Simulink functions in the model. Customizing names and argument identifiers of individual nested Simulink functions of the model is not supported.

Elements

Configuration Parameter

Available Macros

Function name

Subsystem methods

$F, $H, $M, $N, $R, and $U

See Subsystem methods for the definition of each macro.

Argument identifiers

Subsystem method arguments

$I, $M, $N, and $U

See Subsystem method arguments for the definition of each macro.

To specify the naming rules, open the Configuration Parameters dialog box and navigate to Code Generation > Identifiers. In the Identifier format control pane, specify Subsystem methods as custName_$N$M and specify Subsystem method arguments as custArg_$I_$N$M, respectively. Then, click Apply.

Configuration Parameter dialog box with the Code Generation > Identifiers selected. The configuration parameters System methods and System method arguments are highlighted with the values custName_$N$M and custArg_$I_$N$M, respectively.

In the naming rule for function names, custName_$N$M:

  • The macro $N is replaced by the Simulink Function block name.

  • The macro $M is required. It is replaced by an autogenerated name-mangling text to avoid naming collisions. If there is no name collision, this macro is ignored.

In the naming rule for argument identifiers, custArg_$I_$N$M:

  • The macro $I is replaced by the letter u for each input argument that corresponds to an Argument Inport block of the Simulink function, by the letter y for each input argument that corresponds to an Argument Outport block of the Simulink function, and by the letter pair uy for each combined input argument that corresponds to one Argument Inport block and one Argument Outport block of the Simulink function. For an example of using combined arguments, see Use Combined Inports and Outports.

  • The macro $N is replaced by the name of the Argument Inport block or Argument Outport block of the Simulink function.

  • The macro $M is required. It is replaced by an autogenerated name-mangling text to avoid naming collisions. If there is no name collision, this macro is ignored.

Generate model code and examine the function generated from the nested Simulink function:

  1. Open the Embedded Coder® app.

  2. In the Simulink Toolstrip, select the C Code tab.

  3. In the Generate Code section, click Generate Code to generate code from the model.

C Code tab is selected in the Simulink Editor Toolstrip, and the mouse arrow is clicking the Generate Code button.

In the Code pane, navigate to the generated source code file SimFuncs.c and search for n_func to see the prototype of the generated function.

Code pane showing the prototype of the generated function in the file SimFuncs.c. The function prototype is void custName_n_func(real_T custArg_u_n_in1, real_T custArg_u_n_in2, real_T star custArg_y_n_out1, real_T star custArg_y_n_out2).

The function name and argument identifiers adhere to the naming rules you specified.

Use Combined Inports and Outports

You can customize the generated function to have combined arguments, corresponding each to one Argument Inport and one Argument Outport of the Simulink function. A combined argument is passed by pointer and is used in the generated function for both input and output. To use combined arguments, specify the same identifier for both the Argument Inport and the Argument Outport in the Simulink Function block. In the example, update the Simulink function prototype with the argument n_in_out as both the first Argument Inport and the first Argument Outport.

In the Simulink Editor, navigate to subsystem block subsystemWithSF. The nested Simulink Function block, nestedSimFunc, is at the bottom. Above it is its Function Caller block, nestedSimFuncCaller.

Subsystem block subsystemWithSF is open in the Simulink Editor.

Update the prototype in the Simulink function block:

  1. Click once on the prototype of the Simulink Function block to turn on editing mode.

  2. Revise the prototype to [n_in_out,n_out2] = n_func(n_in_out,n_in2).

Prototype of Simulink Function block is [n_in_out,n_out2] = n_func(n_in_out,n_in2).

Update the Function Caller block:

  1. Double-click on the Simulink Function Caller block, nestedSimFuncCaller.

  2. In the Block parameters dialog box that opens, change the Function prototype parameter to the new Simulink function prototype.

  3. Click OK.

Block Parameters dialog box of nestedSimFuncCaller. The Function prototype is [n_in_out,n_out2] = n_func(n_in_out,n_in2).

Generate code from the model and examine the generated function in the Code pane. This time look at the definition of the function so you can see how it uses the combined argument for both input and output.

Code pane showing the definition of the generated function in the generated file SimFuncs.c. The function prototype is void custName_n_func(real_T custArg_u_n_in2, real_T star custArg_uy_n_in_out, real_T star custArg_y_n_out2). The argument custArg_uy_n_in_out is highlighted in the function prototype and twice in the function body, where it is being used.

In the generated function:

  • The $I part of the naming rule is replaced by uy in the combined argument.

  • The combined argument is used for both input and output in the function body. Its value is used in the calculation and is being updated.

Customize Function Qualifiers

You can customize the qualifier of the generated function to be static or extern by enabling on the configuration parameter for either qualifier. Enable the parameter for extern:

In the Configuration Parameters dialog box, navigate to Code Generation > Code Style. On the right, select Preserve extern keyword in function declarations. Click Apply.

Configuration Parameter dialog box. The parameter Preserve extern keyword in function declarations is highlighted and enabled. The parameter below it, Preserve static keyword in function declarations, is also highlighted. The mouse cursor is on the Apply button.

Note:

  • If both parameters are enabled, the code generator ignores the parameter for extern.

  • Customizing individual nested functions to use the static or extern qualifiers is not supported.

Generate code from the model again, and examine the declaration of the function in the Code pane:

Code pane showing the function declaration in the file SimFuncs.c.

The function declaration has the extern qualifier.

See Also

| |

Topics