C Data Code Interface Configuration for Model Interface Elements
Configure a model to use a data code interface if you are deploying C code as an application. Code generated for application deployment is optimized to a specific scheduler (single-tasking, rate-monotonic, or concurrent) based on your model configuration.
Model Code Interfaces lists modeling elements
that you can use to represent interfaces for interacting with target environment resources.
To configure the modeling elements for code generation, in the Code Mappings editor or by using the code mappings programming interface
(coder.mapping.api.CodeMapping
), you map model interface elements to code
definitions.
When configuring a data code interface, you can:
Configure default code interface settings that apply throughout a model for categories of data elements or functions. For example, inports is a category of data elements and periodic functions is a category of functions.
Configure code interfaces settings that apply to individual data elements or functions.
Migration
When you open a model created in a release before R2020b, Simulink® migrates code definitions, such as embedded signal and parameter objects in the model workspace, to the model code mappings.
For a model configured to use an ERT-based system target file for code generation, if necessary, as part of the migration, Simulink configures the Embedded Coder Dictionary that the model uses. When you save the model, the code mappings are saved as part of the model.
For more information, such as effects of migration and what to consider before migrating, see Migration of Model Data Configurations to Code Mappings.
Code Definitions
For a data code interface configuration, code definitions include storage classes, function customization templates, and memory sections. A storage class defines properties, such as appearance and location, that the code generator uses when producing code for the associated data. A function customization template defines how the code generator produces code for functions. For a function category, you can define a rule for naming functions and a location in memory for function definitions (memory section).
Memory sections control the placement of data and function definitions in memory. In some cases, you can select a specific memory section that you want when configuring data elements and functions.
The code generator provides predefined code definitions for data elements. If you have Embedded Coder®, you can use the Embedded Coder Dictionary to create and share code definitions, for example, to standardize code that you generate from multiple models or to create definitions for different application run-time environments. Creating custom definitions enables you to achieve code generation objectives that predefined definitions cannot satisfy. For data elements, you also have the option of creating code definitions created in packages by using Embedded Coder Custom Storage Class Designer.
Code definitions appear in the Code Mappings editor and are accessible by using the code mappings programming interface. For some definitions that you create by using the Custom Storage Class Designer, you can make the definitions appear in the Code Mappings editor by configuring an Embedded Coder Dictionary to refer to them.
By default, code definitions that you create by using the Embedded Coder Dictionary for a model are stored in the model file. You can share code definitions between models and projects by storing the definitions in a shared Embedded Coder Dictionary. To modify a definition in a shared dictionary, you change the definition in one place—the Embedded Coder Dictionary.
For more information, see Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture and Create Storage Classes by Using the Custom Storage Class Designer.
Data Interface Code Mapping Options
Associate elements of a model that represent interfaces with code definitions by using the Code Mappings editor or the code mappings programming interface. The code generator uses the mapped code definitions to determine what the generated interface code should look like and how the code should be packaged.
Simplify the effort of configuring code interfaces by specifying default
configurations for the code generator. For data interface configurations, you specify
default configurations in the Code Mappings editor on the Data
Defaults and Function Defaults tabs or by calling
the setDataDefault
and setFunctionDefault
functions. You can let the code generator establish the default configuration by setting
the storage class or function customization template to
Default
or you can specify a named storage class or
function customization template. When you specify Default
,
the code generator determines how data elements and function prototypes appear in the
generated code. When you specify a named storage class or function customization
template the code generator applies the settings specified for that code
definition.
For individual model elements, you can override the default category code
configuration setting. Select the tab that corresponds to the element category or call
the corresponding element category
set
function (for
example, ElementCategory
setInport
or setState
). For an
individual element, you have three configuration options:
Let the code generator handle the configuration.
For data elements, select or specify
Auto
. ForAuto
the data element is subject to code generation optimizations, which can eliminate the element from the code or change the representation of the element. If optimizations do not eliminate the data element, the element acquires the default configuration for the corresponding data category. If a default is not configured for the category (on the Data Defaults tab, category is set toDefault
), the element appears as a field of a standard data structure in the generated code.For functions, select or specify
Default
.
Map the data or function element to the code definition that is configured as the default for the relevant data or function element category for this model. Specify
Model default:
orstorage class
Model default:
.function-template
Map the data or function element to a named storage class or function customization template that is not labeled as the model default.
This figure summarizes the relationship between default and individual configuration settings and available choices.
Data Default Configurations
Reduce the effort of preparing a model for C code generation by specifying default configurations for categories of data elements, for example inports or model workspace parameters, across a model. Applying default configurations can save time and reduce the risk of introducing errors in code, especially for larger models and models from which you generate multi-instance code.
Default configurations apply throughout a model, excluding referenced models. Simulink saves a separate set of code mappings as part of each model in a model reference hierarchy.
If a model includes a significant number of elements of a given category (for example, more than 10), it is more efficient to configure the category by using a default setting, and then override that setting for special cases.
The default settings can help you to reduce manual data entry.
As you add blocks to a model, new data elements inherit the default settings.
To change code generation settings for many data elements at once, make the changes in one place—the default settings.
For efficiency, use naming rules as you make changes elsewhere in the model.
You configure data defaults by using the Code Mappings Editor – C
Data Defaults tab or the setDataDefault
function.
Model Data Categories
You configure data defaults by using the Code Mappings Editor – C
Data Defaults tab or the setDataDefault
function. In the Data
Defaults tab or function call, you can select or specify these
categories of data elements.
Model Element Category | Description |
---|---|
Inports | Root-level input ports of a model, such as Inport and In Bus Element blocks. |
Outports | Root-level output ports of a model, such as Outport and Out Bus Element blocks. |
Signals, states, and internal data | Data elements that are internal to the model, such as block output signals, discrete block states, data stores, and zero-crossing signals. |
Shared local data stores | Data Store Memory blocks that have the block parameter Share across model instances set. These data stores are accessible only in the model where they are defined. The data store value is shared across instances of the model. |
Global data stores | Data stores that are defined by a signal object in the base workspace or in a
data dictionary. Multiple models in an application can use these data stores. To
view and configure these data stores in the Code Mappings editor, click the
Refresh link to the right of the category name. Clicking
this link updates the model diagram. |
Model parameter arguments | Parameters in the model workspace that you configure as model arguments. These parameters are exposed at the model block to enable each model instance to provide its own value. To specify a parameter as a model argument, select the Model Data Editor > Parameters > Argument check box. |
Model parameters | Parameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments. |
External parameters | Parameters that you define as objects in the base workspace or in a data dictionary. Multiple
models in an application can use these parameters. To view and configure these
parameters in the Code Mappings editor, click the Refresh
link to the right of the category name. Clicking this link updates the model
diagram. |
Constants | Constant-value block output and parameters that could not be inlined. These values are stored in variables for one of the following reasons.
|
If you set the default storage for a category of data
elements to Default
, you have the option of specifying a
memory section.
Considerations for Configuring Default Code Generation Settings for Data
Before configuring default code generation settings for data elements, take into account the considerations listed in this table.
Consideration | More Information |
---|---|
What categories are relevant to your model? | The data element categories in the preceding table |
Does the model use several instances of data that is in a category? If the answer is yes, applying default mappings is beneficial. Otherwise, consider configuring code generation for each data element individually. | |
Which storage class aligns with your code generation requirements for each category? | Choose Storage Class for Controlling Data Representation in Generated Code |
Do you want model data to be structured? For example, if you configured the model for multi-instance code generation, structures can improve code efficiency and readability. | Organize Data into Structures in Generated Code |
Is the code generator required to store data in specific areas of memory? For example, consider whether to store initialization data in slow memory and algorithmic or computational data in fast memory. | |
Do you want to prevent optimizations from eliminating specific data from the code? | Elimination of Categories of Data by Optimizations |
Do you need to define new storage classes that you can select in the Code Mapping editor? |
After you configure one or more categories of model elements, setting memory sections and Shared utilities identifier format model configuration parameters has no effect on the model configuration. When you open a coder app, Simulink migrates the model configuration parameter settings to the Code Mappings editor. As part of the migration, Simulink configures the Embedded Coder Dictionary that the model uses, as described in Migration of Memory Section and Shared Utility Settings from Configuration Parameters to Code Mappings.
Elimination of Categories of Data by Optimizations
Code generation optimizations can eliminate data from the code, which means your application code cannot interact with the data. For general information about data elimination by optimizations, see How Generated Code Stores Internal Signal, State, and Parameter Data.
The optimizations can eliminate data in these categories only:
Model parameters
Model parameter arguments
External parameters
Internal data
After eliminating data with optimizations, the code generator applies the default code generation settings in the code mappings to the remaining data. For parameters and signal lines, optimizations can eliminate data for an entire category. If this elimination occurs, default settings that you specify for that category do not apply to data.
To prevent optimizations from eliminating individual data elements, explicitly
apply a storage class to the individual element. The storage class controls the
appearance of the data in the generated code. To force a data element to use the
default storage class that you specify, explicitly apply the storage class
Model default
. See Choose Storage Class for Controlling Data Representation in Generated Code.
Shared Dictionary Data Defaults
If you link a model to a Simulink data dictionary, which includes a coder dictionary that configures
default code definitions for categories of data, you can use the Code Mappings
editor to apply the dictionary defaults. In the Code Mappings editor, on the
Data Defaults tab, select a category and set the storage
class to Dictionary Default
. If someone makes a change to
the default settings in the shared coder dictionary, the code generator applies the
updated default settings when producing code for your model. See Configure Default Code Mapping in a Shared Dictionary.
Function Default Configurations
Reduce the effort of preparing a model for C code generation by specifying default configurations for categories of and functions, such as initialize/terminate and execution functions, across a model. Applying default configurations can save time and reduce the risk of introducing errors in code, especially for larger models and models from which you generate multi-instance code.
Default configurations apply throughout a model, excluding referenced models. Simulink saves a separate set of code mappings as part of each model in a model reference hierarchy.
If a model includes a significant number of functions of a given category (for example, more than 10), it is more efficient to configure the category by using a default setting, and then override that setting for special cases.
The default settings can help you to reduce manual data entry.
As you add blocks and signals to a model, new functions inherit the default settings.
To change code generation settings for many functions at once, make the changes in one place—the default settings.
For efficiency, use naming rules as you make changes elsewhere in the model.
Model Function Categories
You configure function defaults by using the Code Mappings Editor – C
Function Defaults tab or the setDataDefault
function. In the Function
Defaults tab or function call, you can select or specify these
categories of functions.
Model Function Category | Description |
---|---|
Initialize/Terminate | Entry-point functions for initialization and termination |
Execution | Entry-point functions for initiating execution and resets |
Shared utility | Shared utility functions |
The code generator uses default naming rules to name entry-point functions. To
integrate generated code with existing external code or to comply with naming
standards or guidelines, you can adjust the default naming rule. Adjusting the
default naming rule can save time, especially for multirate models for which the
code generator produces a unique step
function for each
rate.
You might need to define a function customization template in the Embedded Coder
Dictionary. Unless someone has defined function customization templates for a model,
the Code Mappings editor presents Default
as the only template
option.
Considerations for Configuring Default Code Generation Settings for Functions
Before configuring default code generation settings for functions, take into account the considerations listed in this table.
Consideration | More Information |
---|---|
What categories are relevant to your model? | The function categories in the preceding table |
Does the model use several instances of functions that are in a category? If the answer is yes, applying default mappings is beneficial. Otherwise, consider configuring code generation for each function individually. | Configure Generated C Function Interface for Model Entry-Point Functions |
Which function customization template aligns with your code generation requirements for each category? | Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture |
Do categories require the code generator to store function code in specific areas of memory? For example, consider whether to store initialization data in slow memory and algorithmic or computational data in fast memory. | |
Do you need to define new function customization templates that you can select in the Code Mapping editor? | |
Do you have function naming requirements? If the answer is yes, what are they? To which categories do the requirements apply? |
After you configure one or more categories of model elements, setting memory sections and Shared utilities identifier format model configuration parameters has no effect on the model configuration. When you open a coder app, Simulink migrates the model configuration parameter settings to the Code Mappings editor. As part of the migration, Simulink configures the Embedded Coder Dictionary that the model uses, as described in Migration of Memory Section and Shared Utility Settings from Configuration Parameters to Code Mappings.
Shared Dictionary Function Defaults
If you link a model to a Simulink data dictionary, which includes a coder dictionary that configures
default code definitions for categories of functions, you can use the Code Mappings
editor to apply the dictionary defaults. In the Code Mappings editor, on the
Function Defaults tab, select a category and set the
function customization template to Dictionary Default
. If
someone makes a change to the default settings in the shared coder dictionary, the
code generator applies the updated default settings when producing code for your
model. See Configure Default Code Mapping in a Shared Dictionary.
Configurations for Individual Data Elements and Functions
After you configure model-wide default settings, you can override the defaults for individual data elements and functions. An individual data element or function configuration applies to a specific data element or function in a model. Configure data elements and functions individually when:
The model includes a few elements of a given category that have unique source, naming, or placement requirements.
You configured default settings and you want to override the default setting for an individual element.
For individual data elements, initially the storage class is set to
Auto
, which means that the code generator might eliminate
or change the representation of relevant code for optimization purposes. If
optimizations are not possible, the code generator applies the model default
configuration.
To avoid optimizations and force the code generator to use the default configuration, set the storage class to
Model default
.To override the default configuration, specify the storage class that meets the code generation requirements for that data element.
For individual functions, you can customize the function name. For execution functions, such as the step functions and Simulink functions for a model, you can configure function arguments.
Configure Data
Open the Embedded Coder app.
Configure default mappings for categories of data elements. Select Code Interface > Default Code Mappings. In the Code Mappings editor, on the Data Defaults tab,select a model element category and set the storage class. In the Property Inspector, set storage class properties to align with model requirements.
If you need to define a storage class, use the Embedded Coder Dictionary. Select Code Interface > Embedded Coder Dictionary.
Decide whether to override default configuration settings for individual data elements. If you choose not to override settings, go to step 8.
Identify MATLAB variables that you want to configure individually and store the configurations in the model file. In the Model Explorer, convert the variables to
Simulink.Parameter
objects. The resulting data objects appear in the Code Mappings editor as model parameters.Identify signals representing algorithm data that you want to monitor while the generated code executes and configure those signals individually. For each signal that you identify, do one of the following:
For signal data that is specific to a model (not shared), add the signals to the Code Mappings editor.
For shared signal data, create and configure
Simulink.Signal
objects.
Configure individual data elements. In the Code Mappings editor, click a category tab, select a data element, and set the storage class. In the Property Inspector, set storage class property values. To change the setting of model configuration parameter Default parameter behavior, click the link indicating the behavior when the storage class is set to
Auto
. In the Model Configuration Parameters dialog box, change the parameter setting toTunable
orInline
.To view and configure external data objects that are stored in the base workspace or a data dictionary, in the Code Mappings editor, on the Data Defaults tab, click the
Refresh
link to the right of the category name.Generate and review code.
For more details and examples, see:
Configure Functions
Open the Embedded Coder app.
Configure default mappings for categories of functions. Select Code Interface > Default Code Mappings. In the Code Mappings editor, on the Function Defaults tab, select a function category and set the function customization template.
You might need to define a function customization template. Templates are not available by default. To define a function customization template, use the Embedded Coder Dictionary. To open the Embedded Coder Dictionary, select Code Interface > Embedded Coder Dictionary.
Decide whether to override default configuration settings for individual functions. If you choose not to override settings, go to step 5.
Configure individual entry-point functions. In the Code Mappings editor, click Functions tab, select a function, and set the function customization template. Alternatively, you can specify a function name. For execution functions, such as a model step function, you can customize the function arguments. Clicking a function preview link opens a dialog box that you can use to customize the entire function interface.
Generate and review code.
For more details and examples, see:
See Also
Code Mappings Editor – C | Embedded Coder Dictionary | coder.mapping.api.CodeMapping
Related Topics
- Configure Root-Level Inport Blocks for C Code Generation
- Configure Root-Level Outport Blocks for C Code Generation
- Configure Signal Data for C Code Generation
- Configure Parameters for C Code Generation
- Configure Block States for C Code Generation
- Configure Data Stores for C Code Generation
- Choose Storage Class for Controlling Data Representation in Generated Code
- Configure Generated C Function Interface for Model Entry-Point Functions