Code Mappings Editor – C
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 class and 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:
Data Defaults (data interface only)
Function Defaults (data interface only)
Functions
Inports
Outports
Data Transfers (service interface only)
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.
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 Embedded Coder app. For a data interface configuration, on the C Code tab, select Code Interface > Default Code Mappings or Code Interface > Individual Element Code Mappings. For a service interface configuration, C Code tab, select Code Interface > Component Interface or Code Interface > Subcomponent Interface.
Open the Embedded 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 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.
Open the model
RollAxisAutopilot
by entering this command at the MATLAB® Command Window:The current working directory contains these external code files.openExample("RollAxisAutopilot")
roll_input_data.c
roll_input_data.h
roll_heading_mode.c
roll_heading_mode.h
Open the Embedded Coder app.
Configure the code generator to:
Use header file
roll_input_data.h
to declare the variables representing model Inport blocks.Represent variables for model Outport blocks as separate global variables.
Define output variables in
roll_output_data.c
and 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 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.h
and 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
ImportFromFile
with 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_Mode
row.Set Storage Class to
ImportFromFile
.Click the 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.h
androll_heading_mode.h
. On the Inports tab, select each Inport block, click the icon and set Identifier to the block name. When the storage class is set to a value other thanAuto
, 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 Output blocks appears as you expect.
RollAxisAutopilot.h
includes 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.c
includesroll_heading_mode.h
and defines variableHDG_Mode
.#include "roll_heading_mode.h" boolean_T HDG_Mode;
roll_input_data.c
defines 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.c
includes this exported data definition:real32_T Ail_Cmd;
roll_output_data.h
includes this exported data declaration:extern real32_T Ail_Cmd;
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
, 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
MultirateMultitasking
by 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
Default
tomyproj_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
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
RollAxisAutopilot
by entering this command at the MATLAB Command Window:The current working directory contains these external code files.openExample("RollAxisAutopilot")
roll_input_data.c
roll_input_data.h
roll_heading_mode.c
roll_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
step
function. Open the configuration dialog box for thestep
function 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_Mode
Inport, 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 updatedstep
function (roll_run
), use the Search field.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)
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
Parameters
Version History
Introduced in R2018a