C Code Generation Configuration for Model Interface Elements
The code generator provides tooling for you to control the representation of the data and, if you have Embedded Coder®, entry-point functions in the generated code. For example, you can configure data and functions for code generation to comply with code standards and guidelines, integrate generated code with external code, or address memory requirements.
The code generator produces code for model data elements and functions:
Data such as inports, signals, states, and parameters, which your application code can read from and write to.
Entry-point functions such as
, which your application code calls to execute the model algorithm.
You can configure model data elements and functions for code generation by using the Code
Mappings editor or code mappings programming interface (
coder.mapping.api.CodeMapping). Embedded Coder is required to configure functions. For data elements, use the Code Mappings
editor or code mappings API instead of, or in addition to, creating and configuring Simulink
data objects. The editor and API enable you to configure:
Default code generation settings that apply throughout a model for a category of data elements
Configuration settings that apply to individual data elements
This diagram shows the overall workflow:
When you open a model created in a release before R2020b, Simulink® migrates code definitions specified in the model to the model code mappings.
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 include storage classes. A storage class defines properties, such as appearance and location, that the code generator uses when producing code for the associated data.
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 that you create by using the Embedded Coder dictionary appear in the Code Mappings editor and are accessible by using the code mappings API. 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 Simulink® data dictionary. With a data dictionary, to modify a shared definition, you change a definition in one place—the Embedded Coder Dictionary.
For more information, see Define Storage Classes, Memory Sections, and Function Templates for Software Architecture (Embedded Coder) and Create Storage Classes by Using the Custom Storage Class Designer (Embedded Coder).
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 and signals 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.
Model Data Categories
You configure data defaults by using the Code Mappings editor
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
|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 |
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.
|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 to prevent optimizations from eliminating specific data from the code?||Elimination of Categories of Data by Optimizations|
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 (Embedded Coder).
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 (Embedded Coder).
The optimizations can eliminate data in these categories only:
Model parameter arguments
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
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 (Embedded Coder).
Configurations for Individual Data Elements
After you configure model-wide default settings, you can override the defaults for individual data elements. An individual data element configuration applies to a specific data element in a model. Configure data elements 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
To avoid optimizations and force the code generator to use the default configuration, set the storage class to
To override the default configuration, specify the storage class that meets the code generation requirements for that data element.
If a data element in a model is unnamed (for example, a signal has no label or a state has no name), you must specify a code identifier for the data element when configuring it for C code generation. Set the code identifier by specifying a value for the storage class property Identifier.
Open the Simulink 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.
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.Parameterobjects. 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
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 to
For information on configuring specific types of data elements, see:
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
Refreshlink to the right of the category name.
Generate and review code.
For more details and examples, see:
- 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