Associate model data elements and entry-point functions with code definitions
The Code Mappings editor is a graphical interface where you can configure model data elements and entry-point functions for code generation. Associate each category of model data element with a specific storage class throughout a model. A storage class defines properties such as appearance and location that the code generator uses when producing code for associated data. You can also associate each category of model entry-point functions with a specific function customization template. The templates define how the code generator produces code for associated functions. In the Code Mappings editor, you can override default mappings for specific entry-point functions. Override default mappings for specific data elements by using the Code view of the Model Data Editor.
The Code Mappings editor display consists of tabbed tables: Data Defaults, Function Defaults, and Functions. Use the tables to set code definitions for categories of model data elements and functions or individual functions. The Code section of the Property Inspector displays your selection and whether a memory section is defined for the storage class or function customization template.
In the Apps gallery, under Code generation, click Embedded Coder. The C Code tab opens, which includes the Code Mappings editor. Click the Code Mappings tab.
In the C Code tab, select Settings > Code Mappings.
In the model canvas, click the perspective control in the lower-right corner and select Code. Then, click the Code Mappings tab.
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 by using the Model Data Editor.
Copy external code files into a writable folder.
copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_input_data.c')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_input_data.h')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_heading_mode.c')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_heading_mode.h'));
Open model rtwdemo_roll
. Save a copy of the model
in the folder where you copied the external code files.
Open the Embedded Coder app. In the Apps gallery, under Code generation, click Embedded Coder . The C Code tab opens, which includes the Code Mappings editor.
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 in roll_output_data.h
.
Under Code Mappings, click the Refresh button.
Click the Data Defaults tab if not already selected.
Set the storage class for model element category
Inports to
ImportFromFile
.
In the Property Inspector, 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 to roll_output_data.c
.
Override the default source location for inport variable
HDG_Mode
. That variable is declared in the external file
roll_heading_mode.h
.
Open the Model Data Editor by clicking the Model Data Editor tab.
Select the HDG_Mode
row.
Set Storage Class to
ImportFromFile
.
In the Property Inspector, expand the Code
section. Set Header File to
roll_heading_mode.h
.
Configure the code generator to produce variable names in the code for
Inport and Outport blocks that match the variable names in external
files roll_input_data.h
and
roll_heading_mode.h
. Set the model configuration
parameter Global variables to
$N$M
, removing the rt
prefix
that the code generator applies by default.
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.
rtwdemo_roll.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
includes
roll_heading_mode.h
and defines variable
HDG_Mode
.
#include "roll_heading_mode.h" boolean_T HDG_Mode;
roll_input_data.c
defines the variables declared in
roll_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;
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 model rtwdemo_multirate_multitasking
. Save a
copy to a writable folder.
Open the Embedded Coder app. In the Apps gallery, under Code generation, click Embedded Coder. The C Code tab opens, which includes the Code Mappings editor.
Create a function customization template that defines the naming rule
myproj_$R$N
.
Open the Embedded Coder Dictionary. In the C Code tab, select Settings > Embedded Coder Dictionary.
Click the Function Customization Templates tab.
Click Add.
In the Name column of the new table row, name the
new template myproj_FunctionTemplate
.
In the Function Name column, enter the naming
rule myproj_$R$N
.
Close the Embedded Coder Dictionary.
In the C Code tab, under Code Mappings, click the Function Defaults tab.
For the Initialize/Terminate and
Execution function categories, change the
default function customization template from Default
to myproj_FunctionTemplate
.
Save the model.
Generate code and verify the entry-point function names.
void myproj_rtwdemo_multirate_multitasking_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_rtwdemo_multirate_multitasking_step1(void) /* Sample time: [2.0s, 0.0s] */ { rtDW.RateTransition_Buffer0 = rtDW.Integrator_DSTATE; rtDW.Integrator_DSTATE += 2.0 * rtU.In2_2s; } void myproj_rtwdemo_multirate_multitasking_initialize(void) { /* (no initialization code required) */ } void myproj_rtwdemo_multirate_multitasking_terminate(void) { /* (no terminate code required) */ }
You can customize the names of most entry-point functions and the arguments of
execution functions, such as step functions and Simulink functions, for a model.
This example shows how to customize the entry-point functions for the model
rtwdemo_roll
.
Copy external code files into a writable folder.
copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_input_data.c')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_input_data.h')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_heading_mode.c')); copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','roll_heading_mode.h'));
Open model rtwdemo_roll
. Save a copy of the model
in the folder where you copied the external code files.
Open the Embedded Coder app. In the Apps gallery, under Code generation, click Embedded Coder . The C Code tab opens, which includes the Code Mappings editor.
Under Code Mappings, click the Functions tab.
Customize the name of the step (execution) function. In the
Function Name column, enter the name
roll_run
.
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.
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, select
Pointer
. In the C
Identifier Name field change the name to
HDG_Ref
Click Apply and verify that the function prototype reflects the changes.
Validate the changes by clicking Validate.
Click OK.
Generate code.
In the Code view, verify the updates in the
generated C file rtwdemo_roll.c
. To find the updated
step
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, real32_T *HDG_Ref, real32_T Turn_Knob)
Model Element Category
— Category of model data elementsNames a category of Simulink® model 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. |
Outports | Root-level output ports of a model. |
Model parameters | Parameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments. |
Model parameter arguments | Block 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. |
External parameter objects | Parameters that you define as objects in the base workspace or in a data dictionary. Multiple models in an application can use these parameters. |
Shared local data stores | Data Store Memory blocks with 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 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. |
Internal data | Local data, such as data stores, discrete block states, block output signals, and zero-crossing signals. |
Constants | Constant-value block output and constant parameters in a model. |
Storage Class
— Code definition for model data elementsDefinition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for model data elements. See Choose Storage Class for Controlling Data Representation in Generated Code.
Model Function Category
— Category of model functionsNames 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 |
Function Customization Template
— Code definition for functionsDefinition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for model functions. You might need to define a function customization template in the Embedded Coder Dictionary. Templates are not available by default.
Source
— Type of entry-point functionIdentifies the type of entry-point function. For rate-based models, this
property provides the sample rate of step
functions.
Function Customization Template
— Code definition for functionDefinition (specification) that the code generator uses to determine properties, such as appearance and location, for code that it produces for a model function.
Function Name
— Name for functionName that the code generator uses for a model function. For the step function, base rate step function for rate based models, the Function Name table cell provides access to a button that opens the Configure C Step Function Interface dialog box. Click the function name, click the vertical dots, and click Configure Prototype. To customize the entire step function interface, use the Configure C Step Function Interface dialog box.
Function Preview
— Preview of function prototypeHyperlink 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 hyperlink. For more information, see Configure Default Settings for Functions.