メインコンテンツ

Customize Generated Entry-Point C Function Arguments for Component 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 component deployment type. To learn how to apply these customizations in a model configured with subcomponent deployment type, see Customize Generated Entry-Point C Function Arguments for Subcomponent 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.

Customize Generated Argument Identifiers

The code generator uses the naming rules of global variables to create identifiers for global variables in the generated code. It also uses these naming rules to create identifiers for arguments of entry-point functions. You specify these naming rules as the values of specific model configuration parameters. Each of these parameters has a set of predefined macros that you use, in combination with valid C language characters, to specify the naming rule.

Each identifier is generated for a specific code object. Some of these objects correspond to model elements, such as model block or model signals, or the model itself. Other code objects, such as the structure that contains model inports, do not directly correspond to actual elements in the model. In this example, you specify the naming rules for the three the configuration parameters in the table.

Elements

Configuration Parameter

Available Macros

Suggested Value For Example

Argument identifiers

Global variables

$M, $N, $R, and $U

See Global variables for the definition of each macro.

gArg_$N$M

Argument type identifiers

Global types

$M, $N, $R, and $U

See Global types for the definition of each macro.

gType_$N$M

Argument field identifiers

Field name of global types

$A, $H, $M, $N, and $U

See Field name of global types for the definition of each macro.

gField_$N$M

Specify the naming rules as suggested in the table. The suggested values have no special significance. They are only meant to demonstrate how the code generator uses the values of these parameters as naming rules for generating identifiers that are related to entry-point functions. In the specified naming rules:

  • The macro $N is replaced by the name of the code object for which the identifier is created. When the code object corresponds to a model element, the object name is the name of the corresponding model element. When the code object does not correspond to any model element, its name is determined by the code generator based on its role in the code. For example, the name of the structure that contains the variables corresponding to root-level model inports is gType_ExtU.

  • 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 parameters and specify their values. Then. click Apply.

Configuration Parameters dialog box. The parameters Global variables, Global types, and Field name of global types are being edited with the values gArg_$N$M, gType_$N$M, and gField_$N$M, respectively.

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. This way, entry-point functions can access the variables directly and do not have to rely on arguments for reading data from them or for changing their values. When the model is configured for component deployment type, the default prototype of each nonreusable generated entry-point function is void-void.

Generate model code:

  1. Open the Embedded Coder® app.

  2. In the Simulink® Toolstrip, select the C Coder tab.

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

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

In the Code pane, locate the generated header file EntryPoints.h and examine the definitions of the structure types gType_ExtU and gType_ExtY.

Note that:

  • The definitions of structures for root-level inports and outports are declared as global types.

  • The identifies of the structures and the identifiers of their fields were created according to the naming rules you specified.

Code pane showing the definition of two structure types, gType_ExtU and gType_ExtY, in the generated header file EntryPoints.h. Comments state that gType_ExtU is for external inputs and gType_ExtY is for external outputs. Each structure type has two fields of datatype real_T. The fields of gType_ExtU are gField_data_in1 and gField_data_in2, and the fields of gType_ExtY are gField_data_out1 and gField_data_out2.

In the Code pane, locate the generated source code file EntryPoints.c and examine the instantiation of the root-level inports and root-level outports structures as global variables. Also observe how the step function directly accesses these global variables to read the inport values and to update the outport values.

Code pane showing the instantiation of the structures gArg_U and gArg_Y as global variables in the source code file EntryPoints.c. The Code pane also shows the definition of the void-void function EntryPoints_step.

In the generated code:

  • The identifiers of the type definition structures, gType_ExtU and gType_ExtY, adhere to the naming rule you specified in the configuration parameter Global types. The identifiers of the fields in the structure types adhere to the naming rule you specified in the configuration parameter Field name of global types.

  • The root-level inports structure, gArg_U, and outports structure, gArg_Y, are instantiated as global variables of types gType_ExtU and gType_ExtY, respectively. Their identifiers adhere to the naming rule you specified in the configuration parameter Global variables.

  • The function EntryPoints_step has no input arguments.

  • The function EntryPoints_step directly accesses the fields of gArg_U to read their values and directly accesses the fields of gArg_Y to update their values.

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 as reusable, in the Configuration Parameters dialog box, navigate to Code Generation > Interface. From the Code interface packaging list, select Reusable function.

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

With this configuration, the prototype of each generated entry-point function includes at least one input argument, which is a reference to the real-time model instance. In real time, the generated function can use the real-time model instance to access variables it needs to read or update. However, there are several alternative configuration that specify how root-level inports and outports are passed to the function:

Pass Root-Level Ports as Part of Model Data Structure

Configure the model to generate reusable entry-point functions that do not accept arguments for root-level ports. In this setting, the generated function extracts the root-level ports from the real-time instance reference, which is the first input argument.

To configure this option, in the Configuration Parameters dialog box, navigate to Code Generation > Interface. Locate the Code Interface > Component section on the right, and from the Pass root-level I/O as list, select Part of model data structure.

Configuration Parameters dialog box. The value Part of model data structure is selected from the Pass root-level I/O as list.

Generate model code again and examine the generated step function in the Code pane.

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

The function extracts the references to the root-level port structures from the single argument, which is the real-time model data structure.

Pass Root-Level Ports as Structured Reference Arguments

You can configure the model to generate reusable entry-point functions that accept root-level ports as references to structures, one structure for the inports and one for the outports.

To configure this option, in the Configuration Parameters dialog box, navigate to Code Generation > Interface. Locate the Code Interface > Component section on the right, and from the Pass root-level I/O as list, select Structure reference.

Configuration Parameters dialog box. The value Structure reference is selected from the Pass root-level I/O as list.

Generate code again and examine the generated step function. The first argument corresponds to the real-time object. The two additional arguments correspond to the root-level inports structure and the root-level outports structure.

Code pane showing the definition of the function EntryPoints_step in the generated source code file EntryPoints.c. The input arguments gArg_U and gArg_Y are highlighted.

Note how the function:

  • Accesses root-level inports through the gArg_U structure.

  • Accesses root-level outports through the gArg_Y structure.

Pass Root-Level Ports as Individual Arguments

In this configuration, individual root-level ports are passed to the generated function as arguments. Unnecessary ports are not passed.

To configure this option, in the Configuration Parameters dialog box, navigate to Code Generation > Interface. Locate the Code Interface > Component section on the right, and from the Pass root-level I/O as list, select Individual arguments.

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

Generate code again and examine the generated step function.

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

In the generated code:

  • 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_.

  • Because the function only needs to read inport values, each root-level inport is passed by value.

  • Because the function needs to update outport values, each root-level outport is passed by reference.

See Also

| |

Topics