メインコンテンツ

Customize Arguments of Step C Function Generated from Single-Tasking Model

Entry-point functions are functions in the generated model code that interface with your external code. A step function is an entry-point function that contains the execution code for each step in a Simulink® model task. The return value and input arguments of the generated function represent the root-level inports and outports that are used by that task, and the function body corresponds to the task logic.

The examples Customize Generated Entry-Point C Function Arguments for Component Models and Customize Generated Entry-Point C Function Arguments for Subcomponent Models show how to customize the identifiers of entry-point function arguments and how to customize the way root-level inports and outports are passed to the generated entry-point functions.

When your model is configured as single-tasking, you can override these customizations for the generated step function. In these settings, you can directly customize the arguments of the step function:

  • You can specify the order of the arguments in the generated function prototype.

  • You can specify the identifier for each of the arguments.

  • You can customize the function to use combined arguments, corresponding each to one root-level inport and one root-level outport of the model.

This example shows how to configure your model to apply these customizations.

Note:

  • Applying these customizations does not affect other generated entry-point functions.

  • The customizations demonstrated in this example apply to both models configured for component deployment type, and models configured for subcomponent deployment type.

Limitations: To apply the customization options demonstrated in this example:

  • Your model must be configured as single-tasking.

  • The storage class of all root-level inports and outports of the model must be specified as Auto, or as Model default with the default storage class of the execution category specified as Default.

  • If your model is configured for component deployment type, it must be configured as nonreusable, or as reusable with the root-level ports passed as individual arguments.

  • If your model is configured for subcomponent deployment type, it must be configured as reusable, or as nonreusable with the root-level ports passed as individual arguments.

Open 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.

Prepare to Customize Step Function Arguments

To customize the step function arguments, use the Configure C Step Function Interface dialog box:

  1. Open the Embedded Coder® app.

  2. Open the Code Mappings editor and navigate to the Functions tab.

  3. Locate the entry with Periodic:D1 in the Source column and click on the Function Preview content of the entry.

Code Mappings editor open with the Functions tab selected. In the Source column, the entry Periodic:D1 is highlighted, and the mouse cursor is clicking the Function Preview content of that entry.

In the dialog box that opens, update the function name to custArgsStepFcn, and select Configure arguments for Step function prototype. Click the Get default button that appears to populate the dialog box parameters you can use for customizing the function arguments.

Configure C Step Function Interface dialog box. The value of C Step Function Name is custArgsStepFcn. Configure arguments for Step function prototype is selected. The mouse hand cursor is clicking Get default.

Designate Return Value

The C language syntax allows only one explicit return value. You can specify the return argument of the generated function as either void or as one of the root-level outports. Except for the root-level outport you designate to become the return value of the generated function, all root-level outports become pointer input arguments in the generated function. In this example, designate the root-level outport data_out2 as the return value:

In the Configure C Step Function Interface dialog box, from the C return argument list, select data_out2 to specify it as the return value of the generated function. The function prototype is updated accordingly: data_out2 becomes the return value, and data_out1 becomes a pointer input argument with the identifier arg_data_out1.

Configure C Step Function Interface dialog box. The value of C Step Function Name is custArgsStepFcn and the value data_out2 is selected from the C return argument list. The value of C function prototype is arg_data_out2 = custArgsStepFcn(arg_data_in1, arg_data_in2, * arg_data_out1).

Customize Argument Identifier, Type Qualifiers, and Order

The table in the lower part of the dialog box shows the root-level inports and outports of the model. Except for the root-level outport you designated as the return value of the generated function, each of the root-level ports appears as an entry in the table.

The first column of the table, Port Name, is the name of the root-level port block. The second column of the table, Port Type, indicates whether the port is an inport or an outport. The third column of the table, C Type Qualifier, indicates how the corresponding input argument is passed to the generated function. For the C Type Qualifier, select one of these values.

  • Value — The code generator attempts to have the argument passed by value. If the code generator cannot do that, the argument is passed as a pointer.

  • Const — The code generator attempts to have the argument passed by constant value. If the code generator cannot do that, the argument is passed as a pointer to constant.

  • Pointer — The argument is a pointer. This is the only option for arguments that correspond to root-level outports.

  • Pointer to const — The argument is a pointer to a constant.

  • Const Pointer to const — The argument is a constant pointer to constant.

Considerations / limitations for the C Type Qualifier:

  • The only option for arguments that correspond to root-level outports is Pointer, because the function needs to have the ability to change their values.

  • For complex variables, such as arrays, the code generator cannot honor Value or Const. In such cases, the argument is generated as Pointer or Pointer to const, respectively.

Use the last column of the table, C Identifier Name, to specify the identifier for the argument.

Use the drag-and-drop dotted areas on the left to reorder the entries. This is how you specify the order of the arguments in the generated function prototype.

In this example, order the arguments and specify their values according to this list:

  • Drag outport data_out1 to be the first entry, and specify its identifier as outArg1. Because it is an outport, its qualifier is Pointer (without the possibility to select a different qualifier).

  • Drag inport data_in2 to be the second entry, and specify its identifier as inArg2. From the list, select the qualifier Const for this port.

  • Drag inport data_in1 to be the third entry, and specify its identifier as inArg1. From the list, select the qualifier Pointer to const for this port.

Click Apply, and then click Validate to validate your specifications.

Configure C Step Function Interface dialog box. The table at the bottom of the dialog box has three entries. The Outport data_out1 with Pointer showing as the selected value for C Type Qualifier, and C Identifier Name specified as outArg1. The C Type Qualifier list of this variable is grayed out. The Inport data_in2 with Const selected as the C Type Qualifier, and C Identifier Name specified as inArg2. The Inport data_in1 with Pointer to const selected as the C Type Qualifier, and C Identifier Name specified as inArg1.

Generate code from the model: in the Simulink Toolstrip, select the C Coder tab. Then, in the Generate Code section, click Generate Code to generate code from the model.

Simulink Toolstrip with the C Code tab selected. The arrow cursor is clicking the Generate Code button.

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

Code pane showing the definition of the generated function custArgsStepFcn in the generated source code file EntryPoints.c. The function prototype is real_T custArgsStepFcn(real_T star outArg1, const real_T inArg2, const real_T star inArg1).

In the generated code:

  • The argument order, type qualifiers, and identifiers adhere to your specifications.

  • The return value is kept, during the running of the function, in the temporary variable arg_data_out2, which corresponds to root-level outport data_out2 that you designated to be the return value of the generated function.

Use Combined Inports and Outports

You can customize the generated function to have combined arguments, corresponding each to one root-level inport and one root-level outport. A combined argument is passed by pointer and is used in the generated function for both input and output. To use combined arguments, in the Configure C Step Function Interface dialog box, specify the same identifier for both the inport and the outport.

In the example, reopen the Configure C Step Function Interface dialog box and in it, specify these:

  • Specify C Step Function Name as sharedArgsStepFcn.

  • From the C return argument list, select void.

  • In the table, drag the ports to reorder them as data_in2, data_out2, data_in1, data_out1.

  • Specify each of the identifiers for data_in2 and data_out2 as sharedArg2.

  • Specify each of the identifiers for data_in1 and data_out1 as sharedArg1.

The C Type Qualifier value of all table entries is automatically updated to Pointer. This is because combined arguments must be passed as pointers to the generated function.

Click Apply, and then click Validate to validate your specifications.

Configure C Step Function Interface dialog box. The value of C Step Function Name is sharedArgsStepFcn. The selected option of C return argument is void. The order of table entries and the port identifiers are as described. The C function prototype is void sharedArgsStepFcn( star sharedArg2, star sharedArg1).

Generate code from the model, and in the Code pane examine the definition of the generated step function.

Code pane showing the definition of the generated step function.

In the code, each of the combined arguments is used for both input and output. The value of each argument is used in the calculations, and is being updated.

See Also

Topics