メインコンテンツ

Customize Generated Entry-Point C Function Arguments for Subcomponent Models

Entry-point functions are functions in the generated model code that interface with your external code. You can customize the identifiers of entry-point function arguments and you can customize the way root-level inports and outports are passed to the generated functions. These configurations affect all the entry-point functions generated from the model. You can override these customizations for the step function generated from a single-tasking model, as shown in the example Customize Arguments of Step C Function Generated from Single-Tasking Model. This example shows how to apply these customizations in a model configured for subcomponent deployment type. To see how to apply these customizations in a model configured with component deployment type, see Customize Generated Entry-Point C Function Arguments for Component Models.

Open and Explore Model

Open the model EntryPoints.

epModel = "EntryPoints";
open_system(epModel)

Model EntryPoints. The two root-level inports are in a rectangle labeled Input arguments. The two root-level outports are in a rectangle labeled Output arguments. The three blocks between the inports and the outports are in a rectangle labeled Step Function body.

EntryPoints is a nonreusable, single-rate single-tasking model, configured to use the ERT system target file.

Set Model for Subcomponent Deployment Type

Configure the model for subcomponent deployment type:

  1. Open the Embedded Coder® app.

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

  3. In the Prepare section, select Automatic > Subcomponent.

C Code tab is selected in the Simulink Toolstrip. The Deployment Type drop-down list is open, and the arrow cursor is on the Subcomponent entry in the list.

Alternatively, you can specify the deployment type programmatically:

  1. Get a handle to the code mappings object of the model and store it in the variable codeMapObj.

  2. Use the setDeploymentType function with the code mappings object to specify the deployment type as Subcomponent.

codeMapObj = coder.mapping.api.get(epModel);
setDeploymentType(codeMapObj,"Subcomponent")

Generate Nonreusable Void-Void Entry-Point Functions

The model is currently configured as nonreusable, which means that there is only a single instance of the real-time model per process. With these settings, model elements can be represented by global variables in the generated code, and entry-point functions can access them directly without having to rely on arguments for reading data from them or for changing their values. When the model is configured for subcomponent deployment type, you can choose to use this option, and then the prototype of each nonreusable generated entry-point function is void-void. The model is currently configured to use this option.

Generate model code: in the C Coder tab of the Simulink Toolstrip, locate the Generate Code section, and click Generate Code button.

Simulink Toolstrip with the C Code tab selected. The arrow cursor points to the Generate Code button.

In the Code pane and in it, navigate to the generated header file EntryPoints_private.h, and see that the variables for root-level model ports are global.

Code pane showing the definition of the root-level model ports data_in1, data_in2, data_out1, and data_out2 as globals in the generated header file EntryPoints_private.h.

In the Code pane, navigate to the generated source code file EntryPoints.c, and locate the definition of the generated step function, EntryPoints.

Code pane showing the definition of the void-void function EntryPoints_step.

In the generated code, the function EntryPoints has no input arguments, and it directly accesses the global variables that represent the root-level ports to read and update their values.

Pass Root-Level Ports as Individual Arguments

When a subcomponent model is configured as nonreusable, you can choose to pass root-level ports to the generated functions as individual arguments. Unnecessary ports are not passed.

To configure this option:

  1. Open the Configuration Parameters dialog box and navigate to Code Generation > Interface.

  2. Locate the Code Interface > Subcomponent section on the right.

  3. From the Implement root-level I/O as list, select Individual arguments.

  4. Click Apply.

Configuration Parameters dialog box. The value Individual arguments is selected from the Implement root-level I/O as list.

Customize Generated Argument Identifiers

When the model is configured for subcomponent deployment type, the code generator creates the identifiers of entry-point arguments according to the naming rule specified in the configuration parameter Subsystem method arguments. The naming rule is specified as a combination of valid C language characters and the macros $I, $M, $N, and $U. For the definition of each macro, see Subsystem method arguments. In this example, specify the naming rule as subsysArg_$I_$N$M. In it:

  • The macro $I is replaced with the letter U for an input argument and the letter Y for an output argument.

  • The macro $N is replaced by the name of the object for which the identifier is created. For example, the name of the root-level port.

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

To specify the naming rules, in the Configuration Parameters dialog box, navigate to Code Generation > Identifiers. In the section Auto-generated identifier naming rules > Identifier format control, locate the parameter and specify its value. Then. click Apply.

Configuration Parameters dialog box. The parameter Subsystem method arguments is being edited with the value subsysArg_$I_$N$M.

Alternatively, you can use the function set_param to programmatically specify the value of this configure parameter:

set_param(epModel,CustomSymbolStrFcnArg="subsysArg_$I_$N$M")

Generate model code again and in the Code pane, navigate to the generated source file EntryPoints.c to examine the generated step function.

Code pane showing the definition of the step function in the source code file EntryPoints.c.

In the generated code:

  • Root-level port variables are passed as arguments to the step function EntryPoints.

  • Identifiers of arguments that correspond to root-level inports include the substring _u_, and identifiers of arguments that correspond to root-level outports include the substring _y_.

Generate Reusable Entry-Point Functions

To allow having multiple real-time instances of the model, configure the model as reusable. In this configuration, the generated entry-point functions are potentially used by more than one real-time model instance within a single process. As such, entry-point functions require arguments to access variables of the specific (real-time model) instance they are invoked by each time.

To configure the model to be reusable, you specify the total number of instances allowed per top model as Multiple:

  1. In the Configuration Parameters dialog box, navigate to Model Referencing.

  2. In the section Option for referencing this model, locate the parameter Total number of instances allowed per top model and from the list, select Multiple.

  3. Click Apply.

Configuration Parameters dialog box. The value Multiple is selected from the Total number of instances allowed per top model list.

The model is automatically set to be reusable. To see that, in the Configuration Parameters dialog box, navigate to Code Generation > Interface. In the Code Interface > Subcomponent section on the right, locate the parameter Code interface packaging. The selected value for the parameter is Reusable function.

Configuration Parameters dialog box. The value Reusable function is selected from the Code interface packaging list.

Note that now the parameter Implement root-level I/O as is missing. This is because when you configure a subcomponent model as reusable, its generated entry-point functions must rely on input arguments to access root-level ports.

You can generate model code again and see that root-level ports are passed to the step function as individual arguments.

See Also

| | |

Topics