Code Mappings Editor — C - Associate model elements with code interface definitions - MATLAB (original) (raw)

Associate model elements with code interface definitions

Description

The Code Mappings editor is a graphical interface where you can configure data elements and entry-point functions 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 classand each category of model entry-point function with a specific function customization template throughout a model. Then, override those settings, as needed, for specific data elements and functions. For models with an attached Embedded Coder® Dictionary that defines a service interface configuration, you can map elements of a model to service interfaces defined in a shared dictionary attached to the model.

A storage class defines properties such as appearance and location, which the code generator uses when producing code for associated data. Function customization templates define how the code generator produces code for associated functions. If you leave the storage class or function customization template set to Default, you can configure a memory section for that data or function category.

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

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. For example, this screenshot shows the dialog box that opens when you click the additional properties button for a root-level input port.

Code Mappings editor is open with the Inports tab selected. The mouse cursor is clicking on the pencil button to the right of the single entry in the Inports table. A dialog box with title Measurements is open above the pencil button. The dialog box has four labels with User Input controls next to each of the labels. The labels are "Calibration Access" with a drop list showing NoCalibration next to it, "Display Identifier" with an empty text box next to it, "Format" with an empty text box next to it, and "Compu Method" with an empty text box next to it. In the bottom of the dialog box there is a push button labeled "Open in Property Inspector".

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 theAdd Signal or Remove Signal button. These buttons are also available in the Code Mappings editor on theSignals/States tab.

Simulink model is open with the Code Mappings editor open, and the Data Defaults tab selected. The drop-down list of the Storage Class in the Output ports table entry is expanded.

Open the Code Mappings Editor — C

Do one of the following:

Examples

Configure Code Generation for Root-Level Inport and Outport Blocks

Configure code generation for the root Inport andOutport 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 RollAxisAutopilot by entering this command at the MATLAB® Command Window:
    openExample("RollAxisAutopilot")
    The current working directory contains these external code files.
    • roll_input_data.c
    • roll_input_data.h
    • roll_heading_mode.c
    • roll_heading_mode.h
  2. Open the Embedded Coder app.

Configure Default Mappings

Configure the code generator to:

  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 forInports. Then, set the storage class toImportFromFile.
  3. Click the Icon to configure additional code mapping properties. icon and set Header File to roll_input_data.h.
  4. Set the storage class for model element categoryOutports toExportToFile.
  5. Set Header File toroll_output_data.h and Definition File to roll_output_data.c.

Configure Individual Inports for Default Configuration

  1. In the Code Mappings editor, click the Inports tab. The storage class for each inport is set toAuto, which means that 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 default configuration for inports.
  2. Force the code generator to use the default configuration for inports, storage class ImportFromFile with external header file roll_input_data.h. Press theCtrl key and select the inports. For one of the selected inports, set the storage class to Model default: ImportFromFile. The editor updates the storage class setting for the selected inports.

Override Default Mappings

Override the default source location for inport variableHDG_Mode. That variable is declared in the external fileroll_heading_mode.h.

  1. In the Code Mappings editor, click the Inports tab.
  2. Select the HDG_Mode row.
  3. Set Storage Class toImportFromFile.
  4. Click the Icon to configure additional code mapping properties. icon and set Header File to roll_heading_mode.h.
  5. Configure the code generator to produce variable names in the code for the Inport blocks that match the variable names in external files roll_input_data.h androll_heading_mode.h. On theInports tab, select each Inport block, click the Icon to configure additional code mapping properties. icon and setIdentifier to the block name. When the storage class is set to a value other than Auto, you can specify a value for the Identifier storage class property. If you leave the Identifier property empty, the code generator uses the name of the block or signal associated with the modeling element.

Include External Source Files In Code Generation and Build Process

Include external source files roll_input_data.c androll_heading_mode.c in the code generation and build process. Set the model configuration parameter Source files to roll_input_data.c roll_heading_mode.c.

Save the model.

Generate and Verify Code

Generate code and verify that the code generated for the Inport and Outport blocks appears as you expect.

#include "roll_output_data.h"
#include "roll_input_data.h"
#include "roll_heading_mode.h"

#include "roll_heading_mode.h"
boolean_T HDG_Mode;

#include "roll_input_data.h"
boolean_T AP_Eng;
real32_T HDG_Ref;
real32_T Rate_FB;
real32_T Phi;
real32_T Psi;
real32_T TAS;
real32_T Turn_Knob;

Configure Default Function Names for Entry-Point Functions

By default, the code generator uses the identifier naming rule$R$N to name entry-point functions. $R is the name of the root model. $N is the name of the function, for example, initialize, step, andterminate. To integrate generated code with existing external code or to comply with naming standards or guidelines, you can adjust the default naming rule. This example shows how to add the text stringmyproj_ as a prefix to $R$. Adjusting the default naming rule can save time, especially for multirate models for which the code generator produces a unique step function for each rate.

Set Up Example Environment

  1. Open the model MultirateMultitasking by entering this command at the MATLAB Command Window:
    openExample("MultirateMultitasking")
  2. Open the Embedded Coder app. The C Code tab opens on the Simulink Toolstrip. You can open the Code Mappings editor pane by selecting Code Mappings - Component Interface near the status bar.

Define Function Naming Rule

Create a function customization template that defines the naming rulemyproj_$R$N.

  1. Open the Embedded Coder Dictionary. In the C Code tab, select Code Interface > Embedded Coder Dictionary (Model).
  2. In the Data Interface pane, selectFunction Customization Templates.
  3. In the center pane, click Create.
  4. In the right pane, specify Name asmyproj_FunctionTemplate.
  5. Specify Function Naming Rule asmyproj_$R$N.
  6. Close the Embedded Coder Dictionary.

Configure Default Mappings

  1. In the C Code tab, select Code Interface > Default Code Mappings.
  2. Click the Function Defaults tab.
  3. For the Initialize/Terminate andExecution function categories, change the default function customization template from Default to myproj_FunctionTemplate.
  4. Save the model.

Generate and Review Code

Generate code and verify the entry-point function names.

void myproj_MultirateMultitasking_step0(void) /* Sample time: [1.0s, 0.0s] */ { (rtM->Timing.RateInteraction.TID0_1)++; if ((rtM->Timing.RateInteraction.TID0_1) > 1) { rtM->Timing.RateInteraction.TID0_1 = 0; }

if (rtM->Timing.RateInteraction.TID0_1 == 1) { rtDW.RateTransition = rtDW.RateTransition_Buffer0; } rtY.Out2 = 2.0 * rtDW.RateTransition + rtU.In1_1s; rtY.Out1 = (3.0 * rtDW.RateTransition + rtU.In1_1s) * 5.0 + rtY.Out2; }

/* Model step function for TID1 / void myproj_MultirateMultitasking_step1(void) / Sample time: [2.0s, 0.0s] */ { rtDW.RateTransition_Buffer0 = rtDW.Integrator_DSTATE; rtDW.Integrator_DSTATE += 2.0 * rtU.In2_2s; }

void myproj_MultirateMultitasking_initialize(void) { /* (no initialization code required) */ }

void myproj_MultirateMultitasking_terminate(void) { /* (no terminate code required) */ }

Customize Individual Entry-Point Functions

For your model, you can customize the names of most entry-point functions and the arguments of execution functions, such as step functions and Simulink functions. This example shows how to customize the entry-point functions for the model RollAxisAutopilot.

Set Up the Environment

  1. Open the model RollAxisAutopilot by entering this command at the MATLAB Command Window:
    openExample("RollAxisAutopilot")
    The current working directory contains these external code files.
    • roll_input_data.c
    • roll_input_data.h
    • roll_heading_mode.c
    • roll_heading_mode.h
  2. Open the Embedded Coder app. The C Code tab opens on the Simulink Toolstrip. You can open the Code Mappings editor pane by selecting Code Mappings - Component Interface near the status bar.

Customize Entry-Point Functions

  1. In the C Code tab, select Code Interface > Individual Element Code Mappings. The Code Mappings editor opens with theFunctions tab selected.
  2. Customize the name of the step (execution) function, withSource Periodic:D1. In the Function Name column, enter the nameroll_run.
  3. Customize arguments of the step function. Open the configuration dialog box for the step function by clicking the prototype hyperlink in the Function Preview column.
  4. Select Configure arguments for Step function prototype.
  5. To open a table that displays the default configurations for the arguments, click Get Default.
  6. Customize the arguments:
    • From the C return argument drop-down list, select Ail_Cmd.
    • For each port, in the C Identifier Name field, remove the arg_ prefix from their default names.
    • For the HDG_Mode Inport, from theC Type Qualifier drop-down list, selectPointer. In the C Identifier Name field change the name toHDG_Mode_Ptr
  7. Click Apply and verify that the function prototype reflects the changes.
  8. Validate the changes by clicking Validate.
  9. Click OK.

Generate and Verify Code

  1. Generate code.
  2. Verify the updates in the generated C fileRollAxisAutopilot.c. To find the updatedstep function (roll_run), use the Search field.
  3. Select the step function to verify its prototype.
    real32_T roll_run(real32_T Phi, real32_T Psi, real32_T Rate_FB, real32_T TAS,
    boolean_T AP_Eng, boolean_T *HDG_Mode_Ptr, real32_T HDG_Ref,
    real32_T Turn_Knob)

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 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 parameterShare 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 theRefresh 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 Code Mappings editor presents valid storage class options for a given category. The options can include:

Function Defaults

Names a category of Simulink model functions. The function customization template that you set for a category applies to functions in that category throughout the model.

Model Function Category Description
Initialize/Terminate Entry-point functions for initialization and termination
Execution Entry-point functions for initiating execution and resets
Shared utility Shared utility functions

Definition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for model functions. Templates are not available by default. You might need to define a function customization template in the Embedded Coder Dictionary.

Functions

Identifies the type of entry-point function. For rate-based models, this property provides the sample rate of step functions.

Definition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for a model function.

Name that the code generator gives a model function.

Preview of the entry-point function prototype. To verify a prototype, review the prototype preview. To open a dialog box where you can customize the prototype, click the preview hyperlink. For more information, see Configure Default Settings for Functions.

Since R2022b

Name of a timer service interface defined in the Embedded Coder Dictionary. To use the dictionary default, specifyDictionary default.

This property is only applicable for exported functions.

To configure the timer service interface, you must attach an Embedded Coder Dictionary that defines a service interface configuration to the model. For more information, see Configure Timer Service Interfaces.

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.

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 inCompuMethod of the inport.

Since R2022b

Name of a receiver service interface defined in the Embedded Coder Dictionary. Within a target environment, a component receives data from other components by calling the target platform receiver service. To use the dictionary default, specify Dictionary default.

To configure the receiver service interface, you must attach an Embedded Coder Dictionary that defines a service interface configuration to the model. For more information, see Configure Sender and Receiver Service Interfaces for Model Inports and Outports.

Outports

Identifies a root-level Outport block or an element of anOut 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.

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 inCompuMethod of the outport.

Since R2022b

Name of a sender service interface defined in the Embedded Coder Dictionary. Within a target environment, a component sends data to other components by calling the target platform sender service. To use the dictionary default, specify Dictionary default.

To configure the sender service interface, you must attach an Embedded Coder Dictionary that defines a service interface configuration to the model. For more information, see Configure Sender and Receiver Service Interfaces for Model Inports and Outports.

Data Transfers

Since R2022b

Identifies a signal line that connects two blocks that result in callable entry-point functions in the generated code. To learn more about callable entry-point functions, see Model Code Interfaces.

Since R2022b

Data transfer service interface to use for code generation, specified as one of the service interfaces defined in the Embedded Coder Dictionary of the model. Within the target environment, the callable entry-point functions transfer data between them by calling the specified data transfer service of the target platform. To use the default data transfer service of the dictionary, specify Dictionary default. To configure a data transfer service interface, you must attach an Embedded Coder Dictionary that defines the service interface configuration to the model. For more information, see Configure Data Transfer Service Interfaces for Data Transfer Signals.

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 isAuto. 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 Element Description
Model parameter argument Block parameter in the model workspace that you configure as a model argument. The parameter is 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 parameter Parameter that is defined within a model, such as a parameter in the model workspace. Excludes model arguments.
External parameter Parameter 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. SeeChoose 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 inCompuMethod 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 Element Description
Local data store Data 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 store Data 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 store Data 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. SeeChoose 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 inCompuMethod of the data store.

Since R2022b

Name of a measurement service interface defined in the Embedded Coder Dictionary. By configuring the measurement service interface for signals, states, and data stores, you can preserve the data in the generated code for measurement. To use the dictionary default, specifyDictionary default. If you do not need to preserve data from the state in the code, specify Not measured.

To configure the measurement service interface, you must attach an Embedded Coder Dictionary that defines a service interface configuration to the model. For more information, see Configure Measurement Service Interfaces for Signals, States, and Data Stores.

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 isAuto. 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:

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.

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 inCompuMethod of the signal or state.

Since R2022b

Name of a measurement service interface defined in the Embedded Coder Dictionary. By configuring the measurement service interface for signals, states, and data stores, you can preserve the data in the generated code for measurement. To use the dictionary default, specifyDictionary default. If data from the state does not need to be preserved in the code, specify Not measured.

To configure the measurement service interface, you must attach an Embedded Coder Dictionary that defines a service interface configuration to the model. For more information, see Configure Measurement Service Interfaces for Signals, States, and Data Stores.

Version History

Introduced in R2018a