Code Mappings Editor — C
Associate model elements with code interface definitions
Description
The Code Mappings editor is a graphical interface where you associate model elements with code interface definitions. When you generate code from the model, the code generator applies the specified definitions to the code it generates from the associated model elements. Each model in a model reference hierarchy has its own code mappings.
Definitions can include:
Storage Classes — a storage class is a set of specifications for generating code from model data elements. It defines properties such as appearance and data types.
Function Customization Templates — a function customization template is a set of specifications for generating entry-point functions. It defines properties such as naming rules for function names and argument related identifiers.
Memory Sections — a memory section is a location in the computer memory, designated to contain specific code elements.
Service Interfaces — a service interface is set of specifications that defines a function prototype and a communication method. Each such interface corresponds to a function that is available to be invoked by the generated model code on the target platform. Tuning and measurement services are used by the generated model code to tune and measure model elements, respectively. Sender, receiver, and data transfer services are used for model elements communication. Timer services are used to obtain real-time information about the target platform clock. To learn more about service interfaces, see Service Interfaces.
The system target file you specify for your model, as well as other settings, determine which code interface definition types are available for configuration. The rest are determined automatically by the code generator.
With models configured to use the GRT system target file you can only configure storage class definitions. For each data element category, associate a default storage class to use throughout the model. Then, override the defaults, as needed, by specifying storage classes for individual data elements.
Models configured to use the ERT system target file can be configured with either data interface configuration or with service interface configuration.
With ERT models configured with data interface configuration, associate each category
of data elements with a default storage class to use throughout the model, and associate
each category of entry-point functions with a default function customization template to
use throughout the model. Then, override the default definitions, as needed, by
specifying storage classes for individual data elements and by specifying function
customization templates for individual entry-point functions. In some configurations,
you can explicitly specify a memory section on top of the specified storage class or
function customization template. This is possible when the definition of the specified
storage class or function customization template does not include a memory section. For
example, when you specify Default as the default storage
class for the model Inports category, as shown in this screenshot:

A service interfaces dictionary is an Embedded Coder® dictionary that contains service interface definitions, sorted by types. For each service type, one of the definitions is selected in the dictionary as the default service for the type. In addition, the dictionary contains definitions of storage classes, memory sections, and function customization templates. The dictionary specifies the default function customization template for each entry-point function category.
When an ERT model is linked to a shared Embedded Coder dictionary that defines service code interfaces, it means that the model is configured with service interfaces configuration. By default, the code generated from such models adheres to the default definitions, as specified in the dictionary. You can, however, use the Code Mappings editor to override the default code interface definitions, by associating individual entry-point functions with function customization templates from the dictionary and by associating individual model elements with services from the dictionary.
To configure model elements for code generation, use the Code Mappings editor tabs:
Data Defaults (only applicable for GRT models and ERT models with data interface configuration)
Function Defaults (only applicable for ERT models with data interface configuration)
Functions (only applicable for ERT models)
Inports
Outports
Data Transfers (only applicable for ERT models with service interface configuration)
Parameters
Data Stores
Signals/States
Depending on the type of element and the specified value, there could be
additional configurable code mapping properties you can set. To configure the additional
properties, select the pencil icon
at the end of the row to open a dialog box. For
example, this screenshot shows the dialog box that opens when you click the pencil icon
for a root-level inport with Auto storage class.

Signals, by default, are not added to the code mappings of the model. To configure a signal for code generation, add the signal to the model code mappings. To learn more about adding signals, see Add Signals to Model Code Mappings. To learn more about removing signals, see Remove Signals from Model Code Mappings.
Open the Code Mappings Editor — C
To open the editor, do one of the following:
In the model canvas of the Simulink® editor, click the perspective control in the lower-right corner and select Code. Then, click the Code Mappings tab.
Open the Simulink Coder™ app or the Embedded Coder app. Then, In the bottom left corner of the Simulink Editor window, click the Code Mappings tab.
Open the Simulink Coder app or the Embedded Coder app. Then, on the C Code tab of the Simulink toolstrip, in the prepare section, select Code Interface > Individual Element Code Mappings.
Examples
Configure Code Generation for Root-Level Inports and Outports With Simulink Coder
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. To configure code generation for root-level In Bus Element blocks and Out Bus Element blocks use the same workflow as in the example.
Open the model
ConfigurationRapidPrototypingInterfaceby entering this command at the MATLAB® Command Window:openExample("ConfigurationRapidPrototypingInterface")Open the Simulink Coder app. The C Code tab includes the Code Mappings editor.
Configure the code generator to declare and define global variables for
inports and outports in generated files
ConfigurationRapidPrototypingInterface.h and
ConfigurationRapidPrototypingInterface.c.
In the C Code tab, select Code Interface > Default Code Mappings.
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 toExportedGlobal. The editor updates the default storage class setting for the two selected data element categories.
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.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 toModel default: ImportedExternPointer. The editor updates the storage class setting for the selected inports.Force the code generator to use storage class
ExportedGlobalfor the model root-level outport. Click the Outports tab. Select the row forOut2. Then, set the storage class toModel default: ExportedGlobal.
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.
In the Code Mappings editor, click the Inports tab.
For
In1, set the storage class toImportedExtern.For each inport, select the row. Then, click the pencil icon
and set the
Identifier property as follows:Set
In1toinput1.Set
In2toinput2.Set
In3toinput3.Set
In4toinput4.
Click Outports.
Select outport
Out1. Click the pencil icon
and set the
Identifier property to
output.
Generate code and verify that the code generated for the Inport and Outport blocks appears as you expect. For example:
ConfigurationRapidPrototypingInterface_private.hincludes 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.hincludes 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)); . . .
Configure Code Generation for Root-Level Inports and Outports With Embedded Coder Using a Data Code Interface
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. To configure code generation for root-level In Bus Element and Out Bus Element blocks use the same workflow as in the example.
Open the model
RollAxisAutopilotby entering this command at the MATLAB Command Window:The current working directory contains these external code files.openExample("RollAxisAutopilot")roll_input_data.croll_input_data.hroll_heading_mode.croll_heading_mode.h
Open the Embedded Coder app.
Configure the code generator to:
Use header file
roll_input_data.hto declare the variables representing model Inport blocks.Represent variables for model Outport blocks as separate global variables.
Define output variables in
roll_output_data.cand declare them inroll_output_data.h.Configure names that the code generator uses for variables it produces in the code for Inport blocks.
In the C Code tab, select Code Interface > Default Code Mappings.
In the Data Defaults tab, under Inports and Outports, select the row for Inports. Then, set the storage class to
ImportFromFile.Click the pencil icon
and set Header
File to roll_input_data.h.Set the storage class for model element category Outports to
ExportToFile.Set Header File to
roll_output_data.hand Definition File toroll_output_data.c.
In the Code Mappings editor, click the Inports tab. The storage class for each inport is set to
Auto, 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.Force the code generator to use the default configuration for inports, storage class
ImportFromFilewith external header fileroll_input_data.h. Press the Ctrl key and select the inports. For one of the selected inports, set the storage class toModel default: ImportFromFile. The editor updates the storage class setting for the selected inports.
Override the default source location for inport variable
HDG_Mode. That variable is declared in the external file
roll_heading_mode.h.
In the Code Mappings editor, click the Inports tab.
Select the
HDG_Moderow.Set Storage Class to
ImportFromFile.Click the pencil icon
and set Header
File to roll_heading_mode.h.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.handroll_heading_mode.h. On the Inports tab, select each Inport block, click the pencil icon
and set
Identifier 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 roll_input_data.c and
roll_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 code and verify that the code generated for the Inport and Outport blocks appears as you expect.
RollAxisAutopilot.hincludes these header files associated with storage classes:#include "roll_output_data.h" #include "roll_input_data.h" #include "roll_heading_mode.h"
roll_heading_mode.cincludesroll_heading_mode.hand defines variableHDG_Mode.#include "roll_heading_mode.h" boolean_T HDG_Mode;
roll_input_data.cdefines the variables declared inroll_input_data.h.#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;
roll_output_data.cincludes this exported data definition:real32_T Ail_Cmd;
roll_output_data.hincludes this exported data declaration:extern real32_T Ail_Cmd;
Configure Default Function Names for Entry-Point Functions With Embedded Coder Using a Data Code Interface
By default, the code generator uses the identifier naming rule
$R$N to name entry-point functions. $R is
the name of the top model. $N is the name of the function, for
example, initialize, step, and
terminate. 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 string
myproj_ 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.
Open the model
MultirateMultitaskingby entering this command at the MATLAB Command Window:openExample("MultirateMultitasking")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.
Create a function customization template that defines the naming rule
myproj_$R$N.
Open the Embedded Coder Dictionary. In the C Code tab, select Code Interface > Embedded Coder Dictionary (Model).
In the Data Interface pane, select Function Customization Templates.
In the center pane, click Create.
In the right pane, specify Name as
myproj_FunctionTemplate.Specify Function Naming Rule as
myproj_$R$N.Close the Embedded Coder Dictionary.
In the C Code tab, select Code Interface > Default Code Mappings.
Click the Function Defaults tab.
For the Initialize/Terminate and Execution function categories, change the default function customization template from
Defaulttomyproj_FunctionTemplate.
Save the model.
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 With Embedded Coder Using a Data Code Interface
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.
Open the model
RollAxisAutopilotby entering this command at the MATLAB Command Window:The current working directory contains these external code files.openExample("RollAxisAutopilot")roll_input_data.croll_input_data.hroll_heading_mode.croll_heading_mode.h
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.
In the C Code tab, select Code Interface > Individual Element Code Mappings. The Code Mappings editor opens with the Functions tab selected.
Customize the name of the step (execution) function, with Source
Periodic:D1. In the Function Name column, enter the nameroll_run.Customize arguments of the
stepfunction. Open the configuration dialog box for thestepfunction by clicking the prototype hyperlink in the Function Preview column.Select Configure arguments for Step function prototype.
To open a table that displays the default configurations for the arguments, click Get Default.
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_ModeInport, from the C Type Qualifier drop-down list, selectPointer. In the C Identifier Name field change the name toHDG_Mode_Ptr
Click Apply and verify that the function prototype reflects the changes.

Validate the changes by clicking Validate.
Click OK.
Generate code.
Verify the updates in the generated C file
RollAxisAutopilot.c. To find the updatedstepfunction (roll_run), use the Search field.Select the
stepfunction 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)
Related Examples
- C Data Code Interface Configuration for Model Interface Elements
- Choose Data Configuration Approach
- Choose Storage Class for Controlling Data Representation in Generated Code
- Configure Default C Code Generation for Categories of Data Elements and Functions
- Configure Generated C Function Interface for Model Entry-Point Functions
- Customize Generated Entry Point C Functions
- Configure Signal Data for C Code Generation
Parameters
Version History
Introduced in R2018a