Embedded Coder Dictionary
Create code definitions to control code generation for model data and functions
Open the Embedded Coder Dictionary
To open an Embedded Coder Dictionary, in a model window, open the Embedded Coder app by opening the Apps gallery and clicking Embedded Coder. On the C Code tab, select Code Interface > Embedded Coder Dictionary. The Embedded Coder Dictionary window displays the code interface configuration that is stored in the dictionary.
To open the Embedded Coder Dictionary in a Simulink data dictionary, in the Model Explorer Model Hierarchy pane:
Under the dictionary node, select the Embedded Coder node.
If you do not see the node, right-click the dictionary node and select Show Empty Sections.
In the Dialog pane (the right pane), click Open Embedded Coder Dictionary.
Examples
Create and Verify Storage Class
In a model, create a storage class that aggregates internal model data, including block states, into a structure whose characteristics you can control. Then, verify the storage class by generating code from the model.
Open the model
RollAxisAutopilot
by entering this in the MATLAB® Command Window:openExample("RollAxisAutopilot");
If the model does not open in the Embedded Coder app, open the app and click the C Code tab.
On the C Code tab, select Code Interface > Embedded Coder Dictionary. The Embedded Coder Dictionary window displays code generation definitions that are stored in the model file.
On the left pane, click Storage Class. In the Storage Classes section, click Create.
Select the new storage class that appears at the bottom of the list,
StorageClass1
. In the pane on the right, set the property values listed in this table.Property Value Name InternalStruct
Header File internalData_$R.h
Definition File internalData_$R.c
Storage Type Structured
Structure Properties > Type Name internalData_T_$M
Structure Properties > Instance Name internalData_$M
After making your changes, in the bottom pane, verify that the pseudocode preview reflects what you expect.
Return to the model editor. To open the Interface editor, below the canvas, double-click Code Mappings. On the Data Defaults tab, expand the Signals section. Select the Signals, states, and internal data row and set Storage Class to
InternalStruct
.In the Configuration Parameters dialog box, on the Code Generation > Code Placement pane, set File packaging format to
Modular
.Generate code.
In the Simulink Editor Code view, open and inspect the file
internalData_RollAxisAutopilot.h
. The file defines the structure typeinternalData_T_
, whose fields represent block states in the model./* Storage class 'InternalStruct', for system '<Root>' */ typedef struct { real32_T FixPtUnitDelay1_DSTATE; /* '<S7>/FixPt Unit Delay1' */ real32_T Integrator_DSTATE; /* '<S1>/Integrator' */ int8_T Integrator_PrevResetState; /* '<S1>/Integrator' */ } internalData_T_;
The file also declares a global structure variable named
internalData_
./* Storage class 'InternalStruct' */ extern internalData_T_ internalData_;
Open and inspect the file
internalData_RollAxisAutopilot.c
. The file allocates memory forinternalData_
./* Storage class 'InternalStruct' */ internalData_T_ internalData_;
Create Function Customization Template
With a function template, you can specify a rule that governs the names of generated entry-point functions. This technique helps save time and maintenance effort in a model that has many entry-point functions, such as an export-function model or a multirate, multitasking model.
This example shows how to create a function template that specifies the naming
rule func_$N_$R
. $N
is the base name of each
generated function and $R
is the name of the Simulink model.
Open the example model
MultirateMultitaskingBareBoard
.Update the block diagram. This multitasking model has two execution rates, so the generated code includes two corresponding entry-point functions.
In the model, set model configuration parameter System target file to
ert.tlc
. To use a function customization template, you must use an ERT-based system target file.In the Simulink Editor, open the Embedded Coder app and open the Embedded Coder Dictionary.
In the Embedded Coder Dictionary, on the Function Customization Template tab, click Create.
For the new function template, set these properties:
Name to
myFunctions
.Function Name to
func_$N_$R
.
After making your changes, verify that the pseudocode preview reflects what you expect.
In the model window, open the Code Mappings editor. On the Function Defaults tab, for the Initialize/Terminate and Execution rows, set Function Customization Template to
myFunctions
.Generate code.
In the Code view, open and inspect the file
MultirateMultitaskingBareBoard.c
. The file defines the two execution functions,func_step0_MultirateMultitaskingBareBoard
andfunc_step1_MultirateMultitaskingBareBoard
, whose names conform to the rule that you specified in the function template.
For an example that shows how to create a memory section, see Control Data and Function Placement in Memory by Inserting Pragmas.
Create Storage Class for Use with Statically and Dynamically Initialized Data
This example shows how to create a storage class that places global variable definitions and declarations in files whose names depend on the model name. You create two copies of the storage class so that you can use one copy with parameter data (the data category Model parameters) and one copy with other data.
Typically, the generated code initializes parameter data statically, outside any function, and initializes other data dynamically, in the model initialization function. When you create a storage class by using the Custom Storage Class Designer or an Embedded Coder Dictionary, you set the Data Initialization property to specify the initialization mechanism.
In an Embedded Coder Dictionary, for each storage class, you must select
Static
or Dynamic
initialization. Consider creating one copy of the storage class for parameter data
(Static
) and one copy for other data
(Dynamic
).
Open the model
RollAxisAutopilot
by typing this command:openExample("RollAxisAutopilot");
If the C Code tab is not open, open the Embedded Coder app and click the C Code tab.
Select Code Interface > Embedded Coder Dictionary.
On the left pane, click Storage Class. In the Storage Classes section, click Create.
For the new storage class, set these properties:
Name to
SigsStates
Header File to
$R_my_data.h
Definition File to
$R_my_data.c
Data Initialization to
Dynamic
After making your changes, verify that the pseudocode preview reflects what you expect.
Place your cursor over the row for the storage class SigsStates and click Duplicate code definition. A new storage class,
SigsStates_copy
, appears.For the new storage class, set these properties:
Name to
Params
Data Initialization to
Static
Verify that the pseudocode preview reflects what you expect.
In the model, open the Code Mappings editor. Below the model canvas, double-click Code Mappings - Component Interface.
On the Data Defaults tab, for the Parameters > Model Parameters row, in the Storage Class column, select
Params
.For the Signals > Signals, states, and internal data row, set Storage Class to
SigsStates
.Configure some parameter data elements in the model so that optimizations do not eliminate those elements from the generated code. On the Modeling tab, click Design > Model Workspace.
In the Model Explorer, on the center pane, select the three rows that correspond to the variables
dispGain
,intGain
, andrateGain
in the model workspace.Right-click one of the rows and click
Convert to parameter object
. The Model Data Editor converts the workspace variables toSimulink.Parameter
objects.In the row for the parameter
dispGain
, in the Storage Class column, click Configure. The model window highlights the row for thedispGain
parameter in the Code Mappings editor.For each variable, in the Storage Class column, select
Model default: Params
, which means that they acquire the default storage class that you specified for Model parameters.In the Configuration Parameters dialog box, on the Code Generation > Code Placement pane, set File packaging format to
Modular
.Generate code.
In the Code view, open and inspect the files
RollAxisAutopilot_my_data.c
andRollAxisAutopilot_my_data.h
. These files define and declare global variables that correspond to the parameter objects and some block states, such as the state of the Integrator block in theBasicRollMode
subsystem./* Storage class 'SigsStates' */ real32_T rtFixPtUnitDelay1_DSTATE; real32_T rtIntegrator_DSTATE; int8_T rtIntegrator_PrevResetState; /* Storage class 'Params' */ real32_T dispGain = 0.75F; real32_T intGain = 0.5F; real32_T rateGain = 2.0F;
Refer to Code Generation Definitions in a Package
You can configure an Embedded Coder Dictionary to refer to code generation
definitions that you store in a package (see Create Code Definitions for External Data Objects). Those
definitions then appear available for selection in the Code Mappings editor. In this
example, you configure the Embedded Coder Dictionary in
RollAxisAutopilot
to refer to definitions stored in the
built-in example package ECoderDemos
.
Open the Embedded Coder Dictionary for
RollAxisAutopilot
. For instructions, see Create and Verify Storage Class.In the Embedded Coder Dictionary window, on the Memory tab, click Manage Packages.
In the Manage Packages dialog box, click Refresh. Wait until more options appear in the Select package drop-down list.
Set Select package to
ECoderDemos
and click Load.In the Embedded Coder Dictionary window, on the Storage Classes tab, the table shows the storage classes defined in the
ECoderDemos
package. Now, inRollAxisAutopilot
, you can select these storage classes in the Code Mappings editor on the Data Defaults tab.To unload the package, in the Manage Packages dialog box, select the package in the Select package drop-down list and click Unload.
For an example that shows how to share code generation definitions between models by using data dictionaries, see Share Code Interface Configuration Between Models.
For an example that shows how to configure default code mappings in a shared Embedded Coder Dictionary, see Configure Default Code Mapping in a Shared Dictionary.
Related Examples
- Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture
- Deploy Code Generation Definitions
- Generate Code to Conform to Software Architecture by Sharing and Copying Default Settings Between Models
- Flexible Storage Class for Different Model Hierarchy Contexts
Parameters
Limitations
A storage class or function customization template that you create in an Embedded Coder Dictionary cannot use a memory section that you load from a package (as described in Refer to Code Generation Definitions in a Package). Use a memory section defined in the Embedded Coder Dictionary.
A storage class that you create in an Embedded Coder Dictionary that has
Function
data access is not supported for the storage class of a data store, parameter argument tuning interface, or measurement interface.You cannot create code generation definitions in a
.mdl
model file.
For additional limitations for code generation definitions in the Embedded Coder
Dictionary of a data dictionary (.sldd
file), see Deploy Code Generation Definitions.