Main Content

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.

PlatformSystem Target FileProgramming LanguageRequired Products
C rapid prototypingGRT-basedCSimulink® Coder™
C productionERT-basedCEmbedded Coder®
C++ productionERT-basedC++Embedded Coder
AUTOSAR classic AUTOSARCAUTOSAR Blockset, Embedded Coder (for generating code)
AUTOSAR adaptiveAUTOSAR AdaptiveC++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.

PlatformSystem target fileLanguageCode interface packaging
C/C++ rapid prototypinggrt.tlc or derivative of grt.tlcC/C++ Nonreusable function or Reusable function
C/C++ productionert.tlc or derivative of ert.tlcC/C++ Nonreusable function or Reusable function
C++ productionert.tlc or derivative of ert.tlcC++C++ class
AUTOSAR classicautosar.tlcC Nonreusable function or Reusable function
AUTOSAR adaptiveautosar_adaptive.tlcC++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.

  1. 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);
    
  2. Activate and create a code mapping for one of the configuration sets by calling setActiveConfigSet and coder.mapping.utils.create.

    setActiveConfigSet('myModel','grtConfig');
    grt_cm = coder.mapping.utils.create('myModel');
    
  3. Activate and create a code mapping for the second configuration set.

    setActiveConfigSet('myModel','ertConfig')
    ert_cm = coder.mapping.utils.create('myModel');
    
  4. Activate the model code mappings by using calls to coder.mapping.api.get that specify the code mapping type, SimulinkCoderC or EmbeddedCoderC.

    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

| (Embedded Coder) | | |

Related Topics