Main Content

Code Mappings Editor – C

Associate model elements with code interface definitions

Since R2020b

Description

The Code Mappings editor is a graphical interface where you can configure data elements in a model for code generation. Each model in a model reference hierarchy has its own code mappings. Associate each category of model data element with a specific storage class throughout a model. Then, override those settings, as needed, for specific data elements.

A storage class defines properties such as appearance and location, which the code generator uses when producing code for associated data.

To configure data elements and functions for code generation, use the tabs in the Code Mappings editor display:

  • Data Defaults

  • Inports

  • Outports

  • Parameters

  • Data Stores

  • Signals/States

When there are additional code mapping properties that can be configured for a model element, you can configure these properties by selecting a row in the active table and clicking the Icon to configure additional code mapping properties icon.

Before you can configure a signal for code generation, add the signal to the model code mappings. Add and remove signals from the code mappings by pausing on the ellipsis that appears above or below a signal line to open the action bar. Click the Add Signal or Remove Signal button. These buttons are also available in the Code Mappings editor on the Signals/States tab.

Open the Code Mappings Editor – C

Do one of the following:

  • Open the Simulink® Coder™ app. On the C Code tab, select Code Interface > Default Code Mappings or Code Interface > Individual Element Code Mappings.

  • Open the Simulink Coder app. On the C Code tab, in the bottom left corner of the Simulink Editor window, click the Code Mappings tab.

  • In the model canvas of the Simulink Editor window, click the perspective control in the lower-right corner and select Code. Then, click the Code Mappings tab.

Examples

Configure Code Generation for Root-Level Inport and Outport Blocks

Configure code generation for the root-level Inport and Outport blocks throughout a model. Applying default configurations can save time, especially for large-scale models that use a significant amount of data. After applying default mappings, you can adjust mappings for individual data elements.

Set Up Example Environment
  1. Open the model.

    openExample('ConfigurationRapidPrototypingInterface')

  2. Open the Simulink Coder app. The C Code tab includes the Code Mappings editor.

Configure Default Mappings

Configure the code generator to declare and define global variables for inports and outports in generated files ConfigurationRapidPrototypingInterface.h and ConfigurationRapidPrototypingInterface.c.

  1. In the C Code tab, select Code Interface > Default Code Mappings.

  2. In the Data Defaults tab, under Inports and Outports, select the row for Inports. Then, set the storage class to ImportedExternPointer. Set the storage class for Outports to ExportedGlobal. The editor updates the default storage class setting for the two selected data element categories.

Configure Individual Inports and Outports for Default Configuration
  1. In the Code Mappings editor, click the Inports tab. When the storage class is set to Auto, 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 configuration.

  2. Force the code generator to use the default configuration for inports, which is storage class ImportedExternPointer. Press the Ctrl key and select the inports. For one of the selected inports, set the storage class to Model default: ImportedExternPointer. The editor updates the storage class setting for the selected inports.

  3. Force the code generator to use storage class ExportedGlobal for the model root outport. Click the Outports tab. Select the row for Out2. Then, set the storage class to Model default: ExportedGlobal.

Configure Individual Data Elements

To configure properties for individual data elements, for example, if you need to override default configuration settings, use the tabs for the different data element types. For this example, override the default storage class setting for Inport block In1.

By default, the code generator names inport and outport variables based on the Inport or Outport block name in the model. When you configure data elements with a storage class setting other than Auto, you can override that default setting for individual elements by setting storage class property Identifier. This property enables you to specify an identifier for the code without modifying the model design.

For this example, set Identifier for the Inport and Outport blocks.

  1. In the Code Mappings editor, click the Inports tab.

  2. For In1, set the storage class to ImportedExtern.

  3. For each inport, select the row. Then, click the Icon to configure additional code mapping properties icon. Set the Identifier property as follows:

    • Set In1 to input1.

    • Set In2 to input2.

    • Set In3 to input3.

    • Set In4 to input4.

  4. Click Outports.

  5. Select outport Out1. Click the Icon to configure additional code mapping properties icon and set the Identifier property to output.

Generate and Verify Code

Generate code and verify that the code generated for the Inport and Output blocks appears as you expect. For example:

  • ConfigurationRapidPrototypingInterface_private.h includes these declarations:

    /* Imported (extern) block signals */
    extern real_T input1;                  /* '<Root>/In1' */
    
    /* Exported data declaration */
    
    /* Data with Imported storage (pointer) */
    extern real_T *input2;                 /* '<Root>/In2' */
    extern real_T *input3;                 /* '<Root>/In3' */
    extern real_T *input4;                 /* '<Root>/In4' */
  • ConfigurationRapidPrototypingInterface.h includes these declarations.

    /* Data with Exported storage */
    extern real_T output;                  /* '<Root>/Out1' */
    
  • This code fragment shows the variable that represents In1, input1, being used in the generated entry-point step function.

    /* Model step function */
    void ConfigurationRapidPrototypingInterface_step(void)
    {
      /* DataStoreWrite: '<Root>/Data Store Write' incorporates:
       *  Constant: '<Root>/Constant1'
       *  Constant: '<Root>/Constant2'
       *  Inport: '<Root>/In1'
       *  Logic: '<Root>/LogOp'
       *  RelationalOperator: '<Root>/RelOp1'
       *  RelationalOperator: '<Root>/RelOp2'
       */
      mode = ((input1 > 10.0) || (input1 < -10.0));
    .
    .
    .
    

Related Examples

Parameters

expand all

Data Defaults

Names a category of Simulink model data elements. The storage class that you set for a category applies to elements in that category throughout the model.

Model Element CategoryDescription
InportsRoot-level input ports of a model, such as Inport and In Bus Element blocks.
OutportsRoot-level output ports of a model, such as Outport and Out Bus Element blocks.
Signals, states, and internal dataData elements that are internal to the model, such as block output signals, discrete block states, data stores, and zero-crossing signals.
Shared local data storesData 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 storesData 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 parametersParameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments.
External parametersParameters 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.

The Code Mappings editor presents valid storage class options for a given category. The options can include:

  • Unspecified storage class (Default). The code generator places the code for the category of data elements in standard structures, such as B_, ExtY_, ExtU_, DW_, and P_. See Data Structures in the Generated Code.

  • Relevant predefined storage classes, such as ExportedGlobal.

  • Relevant storage classes in an available package, such as ImportFromFile (requires Embedded Coder®) .

  • Storage class defined in an Embedded Coder Dictionary (requires Embedded Coder).

Definition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for model data elements. Valid settings are Default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Inports

Identifies a root Inport block or an element of an In Bus Element block (for example, InBus1.signal1) in the model. If the element resolves to a data object, the Code Mappings editor displays a resolve-to-signal-object icon to the right of the source name and resolves the configuration based on whether the storage class setting for the element is Auto. If the storage class is Auto, the data element assumes the code configuration that the data object specifies. The editor changes the display text in the Storage Class column to From signal object: followed by the name of the storage class of the data object. If the storage class is not Auto, the data element assumes the configuration that you specify in the Code Mappings editor.

Definition that the code generator uses to determine properties, such as appearance and location, for code that it produces for the root inport. Valid settings are Auto, Model default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Name for the variable that represents the inport in the generated code.

Select Calibration for the inport to enable the calibration. Select NoCalibration to view the value of the inport and disable the calibration.

Name of the method for converting the ECU-internal value to a physical value for easy readability.

Optional display name of the inport for the measurement purpose in the calibration tool, which is different than the inport name in the Simulink model.

Special display format to be specified for measurement in the calibration tool. This format specification overrules the display format specified in CompuMethod of the inport.

Outports

Identifies a root-level Outport block or an element of an Out Bus Element block (for example, OutBus1.signal1) in the model. If the element resolves to a data object, the Code Mappings editor displays a resolve-to-signal-object icon to the right of the source name and resolves the configuration based on whether the storage class setting for the element is Auto. If the storage class is Auto, the data element assumes the code configuration that the data object specifies. The editor changes the display text in the Storage Class column to From signal object: followed by the name of the storage class of the data object. If the storage class is not Auto, the data element assumes the configuration that you specify in the Code Mappings editor.

Definition that the code generator uses to determine properties, such as appearance and location, for code that it produces for the root outport. Valid settings are Auto, Model default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Name for the variable that represents the outport in the generated code.

Select Calibration for the outport to enable the calibration. Select NoCalibration to view the value of the outport and disable the calibration.

Name of the method for converting the ECU-internal value to a physical value for easy readability.

Optional display name of the outport for the measurement purpose in the calibration tool, which is different than the outport name in the Simulink model.

Special display format to be specified for measurement in the calibration tool. This format specification overrules the display format specified in CompuMethod of the outport.

Parameters

Identifies a parameter in the model. If the element resolves to a data object, the Code Mappings editor displays a resolve-to-parameter-object icon to the right of the source name and resolves the configuration based on whether the storage class setting for the element is Auto. If the storage class is Auto, the data element assumes the code configuration that the data object specifies. The editor changes the display text in the Storage Class column to From parameter object: followed by the name of the storage class of the data object. If the storage class is not Auto, the data element assumes the configuration that you specify in the code mappings.

Types of parameter elements are listed in this table.

Type of Parameter ElementDescription
Model parameterParameter that is defined within a model, such as a parameter in the model workspace. Excludes model arguments.
External parameterParameter that you define as an object in the base workspace or in a data dictionary. Multiple models in an application can use these parameters. This grouping of parameters appears in the editor only if the model uses such an element. 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.

Definition that the code generator uses to determine properties, such as appearance and location, for code that it produces for the parameter. For external parameters, after you click the Refresh link to the right of the category name, the compiled storage class (for example, the storage class configured for an external parameter) appears on the right side of the Storage Class column. Valid settings are Auto, Model default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Name for the variable that represents the model parameter or model parameter argument in the generated code.

Select Calibration for the model parameter to enable the calibration. Select NoCalibration to view the value of the model parameter and disable the calibration.

Name of the method for converting the ECU-internal value to a physical value for easy readability.

Optional display name of the model parameter for the measurement purpose in the calibration tool, which is different than the outport name in the Simulink model.

Special display format to be specified for measurement in the calibration tool. This format specification overrules the display format specified in CompuMethod of the model parameter.

Data Stores

Identifies a data store in the model. If the element resolves to a data object, the Code Mappings editor displays a resolve-to-signal-object icon to the right of the source name and resolves the configuration based on whether the storage class setting for the element is Auto. If the storage class is Auto, the data element assumes the code configuration that the data object specifies. The editor changes the display text in the Storage Class column to From signal object: followed by the name of the storage class of the data object. If the storage class is not Auto, the data element assumes the configuration that you specify in the code mappings.

Types of data store elements are listed in this table.

Type of Data Store ElementDescription
Local data storeData store that is accessible from anywhere in the model hierarchy that is at or below the level at which you define the data store. You can define a local data store graphically in a model by including a Data Store Memory block or by creating a signal object (synthesized data store) in the model workspace.
Shared local data storeData Store Memory block that has 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. This grouping of data stores appears in the editor only if such an element exists in the model.
Global data storeData store that is defined by a signal object in the base workspace or in a data dictionary. Multiple models in an application can use these data stores. These data stores are not configurable in the code mappings. After you click the refresh button, they appear in the Code Mappings editor in a read-only state for viewing purposes. This grouping of data stores appears in the editor only if the model uses such an element. 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.

Names of local and shared local data stores appear in the format block-name: data-store-name.

Depending on how the data store element is represented and configured in the model, local and shared local data stores can resolve to a signal object in the model workspace, based workspace, or a data dictionary. Global data stores resolve to a signal object in the base workspace or a data dictionary.

Definition that the code generator uses to determine properties, such as appearance and location, for code that it produces for the data store. For global data stores, after you click the Refresh link to the right of the category name, the compiled storage class (for example, the storage class configured for a global data store) appears on the right side of the Storage Class column. Valid settings are Auto, Model default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Link that you can click to highlight the data store in the model diagram.

Name for the variable that represents the data store in the generated code.

Select Calibration for the data store to enable the calibration. Select NoCalibration to view the value of the data store and disable the calibration.

Name of the method for converting the ECU-internal value to a physical value for easy readability.

Optional display name of the data store for the measurement purpose in the calibration tool, which is different than the outport name in the Simulink model.

Special display format to be specified for measurement in the calibration tool. This format specification overrules the display format specified in CompuMethod of the data store.

Signals/States

Identifies a signal line or state in the model. If the element resolves to a data object, the Code Mappings editor displays a resolve -to-signal-object icon to the right of the source name and resolves the configuration based on whether the storage class setting for the element is Auto. If the storage class is Auto, the data element assumes the code configuration that the data object specifies. The editor changes the display text in the Storage Class column to From signal object: followed by the name of the storage class of the data object. If the storage class is not Auto, the data element assumes the configuration that you specify in the Code Mappings editor.

The Code Mappings editor lists:

  • Named signals and states by using the data element name

  • Unnamed signals by using the format source-block: port-number

  • States used in multiple blocks by using the format block-name: state-name

To configure an individual signal line in the Code Mappings editor for a model, first you must add the signal to the mappings. See Configure Signal Data for C Code Generation.

Definition that the code generator uses to determine properties, such as appearance and location, for code that it produces for the signal line or state. Valid settings are Auto, Model default, ExportedGlobal, ImportedExtern, and ImportedExternPointer. See Choose Storage Class for Controlling Data Representation in Generated Code.

Link that you can click to highlight the signal line or block that uses the state in the model diagram.

Name for the variable that represents the signal or state in the generated code.

Select Calibration for the signal or state to enable the calibration. Select NoCalibration to view the value of the signal or state and disable the calibration.

Name of the method for converting the ECU-internal value to a physical value for easy readability.

Optional display name of the signal or state for the measurement purpose in the calibration tool, which is different than the outport name in the Simulink model.

Special display format to be specified for measurement in the calibration tool. This format specification overrules the display format specified in CompuMethod of the signal or state.

Version History

Introduced in R2020b