Manage Multiple Code Mappings for a Model
Code mappings separate code generation configurations from model designs. This separation enables you to design models that are platform-neutral and ready to deploy to different target platform environments. If you have access to the necessary products, you can associate a model with code mappings for several different platforms.
Platform | System Target File | Programming Language | Required Products |
---|---|---|---|
C rapid prototyping | GRT-based | C | Simulink® Coder™ |
C production | ERT-based | C | Embedded Coder® |
C++ production | ERT-based | C++ | Embedded Coder |
AUTOSAR classic | AUTOSAR | C | AUTOSAR Blockset, Embedded Coder (for generating code) |
AUTOSAR adaptive | AUTOSAR Adaptive | C++ | AUTOSAR Blockset, Embedded Coder (for generating code) |
You can generate code for the different platforms from a model or model hierarchy. Associate the models in a model hierarchy with the same platform.
Simulink activates an existing code mapping when you do one of the following:
In the Simulink Editor, open a coder app for a model and specify an output type that supports code mappings.
Use the code mappings API to create a code mapping.
The Code Mappings editor and code mappings programming interface display and operate on the active code mapping.
Configure Alternative Code Mappings by Using Configuration Sets
You can configure alternative code mappings for a model by creating multiple model configuration sets and changing which configuration set is active. Simulink creates or opens an existing code mapping for a model based on settings of these model configuration parameters:
System target file
Language
Code interface packaging
Create a configuration set for each platform for which you want to generate code for a model. Code mappings are supported for these combinations of parameter settings.
Platform | System target file | Language | Code interface packaging |
---|---|---|---|
C/C++ rapid prototyping | grt.tlc or derivative of
grt.tlc | C/C++ | Nonreusable function or Reusable
function |
C/C++ production | ert.tlc or derivative of
ert.tlc | C/C++ | Nonreusable function or Reusable
function |
C++ production | ert.tlc or derivative of
ert.tlc | C++ | C++ class |
AUTOSAR classic | autosar.tlc | C | Nonreusable function or Reusable
function |
AUTOSAR adaptive | autosar_adaptive.tlc | C++ | C++ class |
For C++ platforms, reusable and nonreusable functions use C code mappings.
Activate the configuration set of interest. Then, open the corresponding code mappings in the Simulink Editor, as described in Activate Code Mappings in the Simulink Editor or the code mappings API in Create and Activate Code Mappings Programmatically.
Activate Code Mappings in the Simulink Editor
When you open a model in the Simulink Editor, Simulink activates an existing code mapping for the model based on settings of these model configuration parameters:
System target file
Language
Code interface packaging
When you open one of the following coder apps, Simulink checks whether the system target file setting of the active configuration set aligns with your coder app selection, which can be one of these:
Simulink Coder (GRT-based system target file)
Embedded Coder (ERT-based system target file)
AUTOSAR Component Designer (AUTOSAR system target file)
When Simulink detects a misalignment, the editor displays a message that identifies the current system target file setting and questions whether you want to change it and open the selected coder app. You can cancel the operation and open the app that aligns with the current system target file setting or continue with changing the system target file and open the app that you selected.
If the current system target file aligns with your coder app selection, Simulink opens the coder app and adds a code tab (for example, C Code or AUTOSAR) to the editor toolstrip.
After a coder app opens, on the toolstrip, for Output, select the C or C++ code setting or, for AUTOSAR, select one of the AUTOSAR platforms. Then, use the Code Interface menu to open the Code Mappings editor.
Create and Activate Code Mappings Programmatically
You can use the code mappings functions coder.mapping.utils.create
and coder.mapping.api.get
to create and activate code
mappings for GRT- and ERT-based model platforms.
Set up multiple model configuration set objects (
Simulink.ConfigSet
) for the model. For example, you might set up GRT and ERT C code configurations or two ERT code configurations, one for generating reusable functions and one for generating nonreusable function code. Name the configuration sets and attach them to your model.grtConfigObj = Simulink.ConfigSet; set_param(grtConfigObj,'Name','grtConfig'); attachConfigSet('myModel',grtConfigObj); ertConfigObj = Simulink.ConfigSet; set_param(ertConfigObj,'Name','ertConfig'); attachConfigSet('myModel',ertConfigObj);
Activate and create a code mapping for one of the configuration sets by calling
setActiveConfigSet
andcoder.mapping.utils.create
.setActiveConfigSet('myModel','grtConfig'); grt_cm = coder.mapping.utils.create('myModel');
Activate and create a code mapping for the second configuration set.
setActiveConfigSet('myModel','ertConfig') ert_cm = coder.mapping.utils.create('myModel');
Activate the model code mappings by using calls to
coder.mapping.api.get
that specify the code mapping type,SimulinkCoderC
orEmbeddedCoderC
.grt_cm = coder.mapping.api.get('myModel','SimulinkCoderC'); . . . # Configure model data elements . . .. ert_cm = coder.mapping.api.get('myModel','EmbeddedCoderC');
Code Mappings and Embedded Coder Dictionary
If you have Embedded Coder, you can use the Embedded Coder Dictionary to generate standardized code from multiple models. Because the definitions in the Embedded Coder Dictionary for a model are separate from your model elements, you can share the definitions by storing the Embedded Coder Dictionary in a Simulink data dictionary outside of your model. See Separate C Code Interface Configuration Definition from Model-Specific Configurations (Embedded Coder).
See Also
Code
Mappings Editor – C | Code Mappings –
C++ Editor (Embedded Coder) | Simulink.ConfigSet
| setActiveConfigSet
| coder.mapping.api.CodeMapping