Control Data and Function Interface in Generated Code - MATLAB & Simulink (original) (raw)

To use the code that you generate from a model, you call generated entry-point functions such as step and initialize. The calling environment and the generated functions exchange input and output data through global variables or through formal parameters (arguments). This data and the exchange mechanisms constitute the interfaces of the entry-point functions. For information about the default interfaces for reentrant and nonreentrant models in the generated code, see How Generated Code Exchanges Data with an Environment.

By controlling the interfaces that appear in the generated code, you can:

Control Type Names, Field Names, and Variable Names of Standard I/O Structures (Embedded Coder)

By default, for nonreentrant code, Inport blocks at the root level of the model appear in the generated code as fields of a global structure variable. Similarly,Outport blocks appear in a different structure. For reentrant code, depending on your setting for model configuration parameter , the code generator can also package input and output data into standard structures.

With Embedded CoderĀ®, you can control these names. See Manage Replacement of Simulink Data Types in Generated Code.

Control Names of Generated Entry-Point Functions (Embedded Coder)

To use the generated code, you write code that calls generated entry-point functions. For example, entry-point functions include_`model`__step,_`model`__initialize, and top-level functions generated from an export-function model. To control the names of the model entry-point functions, use the Code Mappings editor (requires Embedded Coder) to apply a combination of these techniques:

Control Data Interface for Nonreentrant Code

When you set the model configuration parameter to Nonreusable function (the default), generated entry-point functions are not reentrant. Typically, the functions exchange data with the calling environment through direct access to global variables.

Configure Inport or Outport Block as Separate Global Variable

To remove the block from the standard I/O structures by creating a separate global variable, apply a storage class, such as ExportedGlobal orExportToFile, to the signal that the block represents. You can configure a default storage class for categories of data elements, such asInport blocks. As you add such blocks to the model, they acquire the storage class that you specify. You can use the Code Mappings editor also to configure individual blocks. You might do this if a model has just a few data elements of a specific category or override the default configuration settings.

For an example, see Design Data Interface by Configuring Inport and Outport Blocks. For general information about configuring data for code generation, see C Data Code Interface Configuration for Model Interface Elements.

Configure Generated Code to Read or Write to Global Variables Defined by External Code

If your calling code already defines a global variable that you want the generated code to use as input data or use to store output data, you can reuse the variable by preventing the code generator from duplicating the definition. Apply a storage class to the corresponding Inport or Outport block in the model. Choose a storage class that specifies an imported data scope, such asImportedExtern or ImportFromFile. For information about applying storage classes, see C Data Code Interface Configuration for Model Interface Elements and Organize Parameter Data into a Structure by Using Struct Storage Class.

Package Multiple Inputs or Outputs into Custom Structure

You can configure a single Inport or Outport block to appear in the generated code as a custom structure that contains multiple input or output signals. You can also configure the block to appear as a substructure of the default I/O structures or as a separate structure variable.

Configure the block as a nonvirtual bus by using a Simulink.Bus object as the data type of the block. If your external code defines the structure type, consider using the Simulink.importExternalCTypes function to generate the bus object.

For more information about grouping signals into custom structures in the generated code, see Organize Data into Structures in Generated Code.

Configure Inport or Outport Block as Function Call (Embedded Coder)

If your external code defines a function that returns input data for the generated code or accepts output data that the generated code produces, you can configure anInport or Outport block so that the generated code calls the function instead of accessing a global variable. Apply the Embedded Coder storage class GetSet. For more information, see Access Data Through Functions with Storage Class GetSet.

Pass Inputs and Outputs Through Function Arguments (Embedded Coder)

With Embedded Coder, you can optionally configure the model step (execution) function to access root-level input and output through arguments instead of directly reading and writing to global variables. Completely control the argument characteristics such as name, order, and passing mechanism (by reference or by value). This level of configuration can help to integrate generated code with your external code.

To pass inputs and outputs through arguments, in the Configure C Step Function interface dialog box, select Configure arguments for Step function prototype. Each Inport and Outport block at the root level of the model appears in the code as an argument of the execution function. For more information, see Configure Generated C Function Interface for Model Entry-Point Functions.

Configure Referenced Model Inputs and Outputs as Global Variables (void-void)

By default, for a nonreentrant referenced model, the generated code passes root-level input and output through function arguments. A nonreentrant referenced model is one in which you set model configuration parameter Total number of instances allowed per top model to One.

To pass this data through global variables instead (for a void-void interface), in the referenced model, apply storage classes such asExportedGlobal and ExportToFile to root-levelInport and Outport blocks.

Control Data Interface for Reentrant Code

When you set Code interface packaging to Reusable function, generated entry-point functions are reentrant. The functions exchange data with the calling environment through formal parameters (arguments). By default, each root-level Inport and Outport block appears in the generated code as a separate argument instead of a field of the standard I/O structures.

Prevent Unintended Changes to the Interface

Some changes that you make to a model change the entry-point function interfaces in the generated code. For example, if you change the name of the model, the names of the functions can change. If you configure the model code to exchange data through arguments, when you add or remove Inport or Outport blocks or change the names of the blocks, the corresponding arguments can change.

For easier maintenance of your calling code, prevent changes to the interfaces of the entry-point functions.

Reduce Number of Arguments by Using Structures

Reducing the number of arguments of a function can improve the readability of the code and reduce consumption of stack memory. To create a structure argument that can pass multiple pieces of data at one time, use these techniques:

Control Data Types of Arguments

You can configure the generated entry-point functions to exchange data through arguments. For a scalar or array argument, to control the name of the primitive data type, use a Simulink.AliasType object to either set the data type of the corresponding block or to configure data type replacements for the entire model. These techniques require Embedded Coder. For more information, see Manage Replacement of Simulink Data Types in Generated Code.

Promote Data Item to the Interface

By default, the code generator assumes that Inport andOutport blocks at the root level of the model constitute the data interface of the model. You can promote an arbitrary signal, block parameter, or block state to the interface so that other systems and components can access it. See Promote Internal Data to the Interface.