Deploy Code Generation Definitions
As described in Define Storage Classes, Memory Sections, and Function Templates for Software Architecture, you can create code generation definitions that you and other users can apply to model elements such as data and functions. The code definitions control the appearance of those elements in the generated code.
In a large organization with multiple models and users, you can share code generation definitions by storing them in a mutually accessible location. The process of sharing code definitions is called deployment.
Share Code Generation Definitions Between Multiple Models and Users
When you create custom code generation definitions, to share the definitions between models and other users:
When you create definitions in a package by using the Custom Storage Class Designer, for each user, place the folder containing the package folder on the MATLAB® path. For more information, see What Is the MATLAB Search Path?.
When you create definitions in an Embedded Coder Dictionary, store the definitions in a Simulink® data dictionary. Then, link each model to a separate data dictionary. Configure each separate dictionary to refer to the dictionary that contains the code generation definitions. For an example, see Share Embedded Coder Dictionary Definition Between Models.
Dictionary Usage for Models Created with Different Versions of Simulink
Simulink provides version handling for Embedded Coder® Dictionaries. When these events occur, Simulink synchronizes data in a data dictionary, including the Embedded Coder Dictionaries, for use with a model regardless of the Simulink version used to create the model.
You link a model to a data dictionary that was saved in a previous version of Simulink (for example, you link a model that you develop in R2018b with a dictionary saved in R2018a).
You open a model, which has a local Embedded Coder Dictionary, in a version of Simulink that is older than the current version (for example, you developed a model that uses a local dictionary in R2018a and you open that model in R2018b to continue development).
You also have the option to export (save) an Embedded Coder Dictionary for use with models created with a different version (previous or older) of the code generator.
For more information, see Dictionary Usage for Models Created with Different Versions of Simulink.
Configure Default Code Mapping in a Shared Dictionary
While setting up an Embedded Coder Dictionary intended to be shared by multiple
models, you can map default code definitions for categories of model data and
functions. In this example, you map default code definitions for the Embedded Coder
Dictionary in exSharedCodeDefs.sldd
.
If not already defined, define code definitions (storage classes, function customization templates, and memory sections) in a data dictionary. For this example, you define code definitions in data dictionary
exSharedCodeDefs.sldd
. Place a copy of this file in a writeable location.In Current Folder pane of the MATLAB Command Window, double-click the file name of your copy of
exSharedCodeDefs.sldd
.In the Model Hierarchy pane of the Model Explorer, right-click the dictionary node and select Show Empty Sections.
Open the Embedded Coder Dictionary. In the Model Hierarchy pane, click Embedded Coder Dictionary. Then, in the right pane, click Open Embedded Coder Dictionary.
In the Embedded Coder Dictionary dialog box, on the left pane, click Data Defaults.
In the Data Defaults table, configure default code definitions for categories of data and functions.
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.
The value is an array larger than the loop unrolling threshold.
The value address is needed in the code.
The default mapping for a category applies to elements in a category throughout a model.
For this example, set the storage class for data categories Inports, Outports, and Global data stores to
ExportedGlobal
.In the Embedded Coder Dictionary dialog box, on the left pane, click Function Defaults. Configure the function customization template and, if applicable, a memory section, for these categories of functions:
Model Function Category Description InitializeTerminate Entry-point functions for initialization and termination Execution Entry-point functions for initiating execution and resets Shared utility Shared utility functions Apply your changes by clicking Save and close the Embedded Coder Dictionary and Model Explorer.
Share Embedded Coder Dictionary Definition Between Models
This example shows how to create a storage class that yields separate global
variables in the generated code and share the storage class between two models,
rtwdemo_roll
and rtwdemo_configinterface
.
Then, you configure each model to use the storage class as the default code
generation setting for internal data, which includes block states.
It is a best practice to store shared code generation definitions in a standalone data dictionary, which means that no models are linked directly to the dictionary. Then, you can link the target models to one or more additional, intermediate dictionaries, which you configure to refer to the standalone dictionary. With this modular dictionary hierarchy, each model can store other data, such as design data, separately from the code generation definitions.
In a hierarchy of referenced data dictionaries (see Dictionary Referencing), only one dictionary can store code
generation definitions in an Embedded Coder Dictionary. If a model has access to
multiple Embedded Coder Dictionaries through referenced data dictionaries, use the
functions coder.dictionary.move
and coder.dictionary.remove
to transfer and delete Embedded Coder
Dictionaries until only one exists in the dictionary hierarchy.
The figure shows an example dictionary hierarchy. In the figure:
modelOne.slx
andmodelTwo.slx
are different model files.dataForModelOne.sldd
anddataForModelTwo.sldd
are data dictionaries. Each dictionary stores design data, such as numeric MATLAB variables, for one of the models.coderDefs.sldd
is a shared data dictionary that stores code generation definitions for both models. The other dictionaries referencecoderDefs.sldd
.
Create Code Generation Definition in Standalone Data Dictionary
Create a data dictionary. Then, create the storage class in the dictionary.
At the command prompt, open the Model Explorer.
daexplr
In the Model Explorer, select File > New > Data Dictionary.
Name the dictionary
coderDefs.sldd
.In the Model Explorer Model Hierarchy pane, right-click
coderDefs
and select Show Empty Sections.Select the Embedded Coder Dictionary node.
In the Dialog pane (the right pane), click Open Embedded Coder Dictionary.
In the Embedded Coder Dictionary dialog box, on the left pane, click Storage Class.
In the Storage Classes section, click Create.
For the new storage class, in the property inspector pane, set Name to
internalsInFile
.In the Embedded Coder Dictionary, click Save.
Link Models to Data Dictionaries
Link each model to a separate data dictionary. Configure each dictionary to
refer to the standalone dictionary (coderDefs.sldd
).
Open the models.
rtwdemo_configinterface rtwdemo_roll
In
rtwdemo_roll
, on the Modeling tab, in the Design gallery, click Link to Data Dictionary.In the Model Properties dialog box, click New.
Create a dictionary named
rtwdemo_roll_data.sldd
by using the Create a new Data Dictionary dialog box.In the Model Properties dialog box, click OK.
Simulink links the model to the new dictionary. The dictionary does not contain any data because the model does not use data in the base workspace.
Open the dictionary in the Model Explorer. In the lower-left corner of the model, click the model data badge, then click the External Data link. Under the External Data node, select
rtwdemo_roll_data.sldd
.In the Dialog pane, under Referenced Dictionaries, click Add.
In the Open Data Dictionary dialog box, double-click
coderDefs.sldd
.Use the Model Hierarchy pane to save the changes you made to
rtwdemo_roll_data.sldd
.Link
rtwdemo_configinterface
to a dictionary namedrtwdemo_configinterface_data.sldd
.rtwdemo_configinterface
uses a variable in the base workspace. To migrate the variable into the dictionary, in the Model Properties dialog box, click Migrate data. Then follow the instructions to link the model to the dictionary and copy the referenced variables.Configure
rtwdemo_configinterface_data.sldd
to refer tocoderDefs.sldd
.
Apply Code Generation Definition in Models
Confirm that the shared definition is available for use in the models by applying it as a default setting for internal data and inspecting the generated code.
In
rtwdemo_roll
, click the C Code tab. If the toolstrip does not show the C Code tab, open the Embedded Coder app.Under Code Mappings > Data Defaults, for the Signals, states, and internal data row, set Storage Class to
internalsInFile
.In
rtwdemo_configinterface
, use the Code Mappings editor to set Storage Class tointernalsInFile
for the Signals, states, and internal data row.Generate code from
rtwdemo_roll
.In the Code view, inspect the generated file
rtwdemo_roll.c
. The file defines global variables that correspond to state data in the model, such as the state of the Integrator block in theBasicRollMode
subsystem./* Storage class 'internalsInFile' */ real32_T rtwdemo__FixPtUnitDelay1_DSTATE; real32_T rtwdemo_roll_Integrator_DSTATE; int8_T rtwde_Integrator_PrevResetState;
Optionally, generate code from
rtwdemo_configinterface
and confirm that the code appears as expected.
Migrate Definitions from Model File to Shared Data Dictionary
If you create code generation definitions in the Embedded Coder Dictionary of a model, the definitions are stored in the model file. If you want to share the definitions between models:
Create a shared data dictionary as shown in Share Embedded Coder Dictionary Definition Between Models. Before you proceed to the next step, make sure all of the target models have access to the shared dictionary.
Use the
coder.dictionary.move
function to migrate the definitions from the source model file to the shared data dictionary.Optionally, copy Code Mappings editor settings from the source model to other models by using the programmatic interface of the Code Mappings editor (see Configure Default Data and Function Code Generation Programmatically).
In this example, you convert a subsystem to a referenced model. Then, you create a
storage class in the Embedded Coder Dictionary of the top model and use the storage
class in the Code Mappings editor. To use the same Code Mappings editor settings for
the top model and for the referenced model, you must extract the storage class into
a shared data dictionary (.sldd
).
Create Referenced Model
Open the example model
rtwdemo_roll
.rtwdemo_roll
In the model, right-click the
BasicRollMode
subsystem and select Subsystem & Model Reference > Convert to > Referenced Model.Click Convert.
The new referenced model,
BasicRollMode
, opens. The model file is in your current folder.
Create and Apply Storage Class
In the top model
rtwdemo_roll
, click the C Code tab. If the toolstrip does not show the C Code tab, open the Embedded Coder app.Click Code Interface > Embedded Coder Dictionary.
In the Embedded Coder Dictionary dialog box, on the Storage Class tab, click Create.
For the new storage class, set these property values:
Name to
internalsInFile
.Header File to
$R_internal.h
.Definition File to
$R_internal.c
.
In the model, in the Code Mappings editor, for the Data Defaults > Signals, states, and internal data row, set Storage Class to
internalsInFile
.
The referenced model cannot access the code generation definitions in the Embedded Coder Dictionary of the parent model. Now, move the storage class out of the parent model and into a shared data dictionary.
Move Storage Class to Shared Data Dictionary
As a best practice, link each model to a separate data dictionary
(.sldd
), create a shared, standalone data dictionary to
store the code generation definition (the storage class), and configure
dictionary referencing so that all of the models can
use the code generation definition.
To link the models to separate dictionaries:
In
rtwdemo_roll
, on the Modeling tab, in the Design gallery, click Link to Data Dictionary.In the Model Properties dialog box, click New.
Use the Create a new Data Dictionary dialog box to create a dictionary named
rtwdemo_roll_data.sldd
.In the Model Properties dialog box, click OK.
In the Link Model to Data Dictionary dialog box, click Change this model only.
In the
BasicRollMode
model, use the Model Properties dialog box to link the model to a new data dictionary namedbasicRollMode_data.sldd
.Click, OK.
To create the shared, standalone dictionary and store the code generation definition in it:
In both models, open the dictionary in the Model Explorer. At the lower-left corner of the block diagram, click the model data badge, then click the External Data link. Under the External Data node, select the dictionary.
In the Model Explorer, select File > New > Data Dictionary.
Create a data dictionary named
coderDefs.sldd
.In the Model Explorer Model Hierarchy pane, select the rtwdemo_roll_data node.
In the Dialog pane (the right pane), under Referenced Dictionaries, click Add.
Add a reference to
coderDefs.sldd
.Configure
basicRollMode_data.sldd
to refer tocoderDefs.sldd
.In the Model Hierarchy pane, right-click the rtwdemo_roll_data node and select Save Changes.
At the command prompt, move the code generation definitions from the
rtwdemo_roll
model file to thecoderDefs.sldd
data dictionary.coder.dictionary.move('rtwdemo_roll','coderDefs.sldd');
Now, both models have access to the storage class
internalsInFile
.
Copy Code Mappings Editor Settings from Parent Model to Referenced Model
The new referenced model does not use the Code Mappings editor settings that the parent model uses. To copy the settings from the parent model to the referenced model, use the programmatic interface of the Code Mappings editor.
At the command prompt, copy the storage class setting for Signals,
states, and internal data from rtwdemo_roll
to
BasicRollMode
.
roll_mapping = coder.mapping.api.get('rtwdemo_roll'); roll_sc = getDataDefault(roll_mapping,'InternalData','StorageClass'); mode_mapping = coder.mapping.api.get('BasicRollMode'); setDataDefault(mode_mapping,'InternalData','StorageClass',roll_sc);
Now, both models use the same Code Mappings editor settings. For more information about the programmatic interface of the Code Mappings editor, see Configure Default Data and Function Code Generation Programmatically.
Use Data Dictionary to Store Code Definitions but Not Design Data
A Simulink data dictionary can store design data such as MATLAB variables and Simulink.AliasType
objects (see Global and Shared Data: Data Dictionary). However, migrating the design data of a
large model or of multiple models from the base workspace to a data dictionary can
take time and careful planning.
If you want to store shared code generation definitions in a data dictionary but do not want to migrate the design data of a model, in the Model Properties dialog box, on the External Data tab, select the Enable model access to base workspace property. The model can use design data from the base workspace and code generation definitions from the dictionary or from a referenced dictionary. For more information, see Continue to Use Shared Data in the Base Workspace.
See Also
Related Topics
- Define Storage Classes, Memory Sections, and Function Templates for Software Architecture
- Generate Code to Conform to Software Architecture by Sharing and Copying Default Settings Between Models
- Partition Data for Model Reference Hierarchy Using Data Dictionaries
- Create Storage Classes by Using the Custom Storage Class Designer