Code Mappings Editor

Associate model data elements and entry-point functions with code definitions

Description

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.

Open the Code Mappings Editor

  • 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.

Examples

Configure Code Generation for Root Inports and Outports

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.

Set Up Example Environment

  1. 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'));
  2. Open model rtwdemo_roll. Save a copy of the model in the folder where you copied the external code files.

  3. 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 Default Mappings

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.

  1. Under Code Mappings, click the Refresh button.

  2. Click the Data Defaults tab if not already selected.

  3. Set the storage class for model element category Inports to ImportFromFile.

  4. In the Property Inspector, set Header File to roll_input_data.h.

  5. Set the storage class for model element category Outports to ExportToFile.

  6. Set Header File to roll_output_data.h and Definition File to roll_output_data.c.

Override Default Mappings

Override the default source location for inport variable HDG_Mode. That variable is declared in the external file roll_heading_mode.h.

  1. Open the Model Data Editor by clicking the Model Data Editor tab.

  2. Select the HDG_Mode row.

  3. Set Storage Class to ImportFromFile.

  4. In the Property Inspector, expand the Code section. Set Header File to roll_heading_mode.h.

Configure Model-Wide Parameter Settings

  1. 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.

  2. 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.

  3. Save the model.

Generate and Verify Code

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;

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.

Set Up Example Environment

  1. Open model rtwdemo_multirate_multitasking. Save a copy to a writable folder.

  2. 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.

Define Function Naming Rule

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

  1. Open the Embedded Coder Dictionary. In the C Code tab, select Settings > Embedded Coder Dictionary.

  2. Click the Function Customization Templates tab.

  3. Click Add.

  4. In the Name column of the new table row, name the new template myproj_FunctionTemplate.

  5. In the Function Name column, enter the naming rule myproj_$R$N.

  6. Close the Embedded Coder Dictionary.

Configure Default Mappings

  1. In the C Code tab, under Code Mappings, click the Function Defaults tab.

  2. For the Initialize/Terminate and Execution function categories, change the default function customization template from Default to myproj_FunctionTemplate.

  3. Save the model.

Generate and Review Code

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) */
}

Customize Individual Entry-Point Functions

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.

Set up the environment

  1. 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'));
  2. Open model rtwdemo_roll. Save a copy of the model in the folder where you copied the external code files.

  3. 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.

Customize entry-point functions

  1. Under Code Mappings, click the Functions tab.

  2. Customize the name of the step (execution) function. In the Function Name column, enter the name roll_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 the C Type Qualifier drop-down list, select Pointer. In the C Identifier Name field change the name to HDG_Ref

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

  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, real32_T *HDG_Ref,
                      real32_T Turn_Knob)

Related Examples

Parameters

Data Defaults

Names 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 CategoryDescription
InportsRoot-level input ports of a model.
OutportsRoot-level output ports of a model.
Model parametersParameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments.
Model parameter argumentsBlock 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 objectsParameters 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 storesData 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 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.
Internal dataLocal data, such as data stores, discrete block states, block output signals, and zero-crossing signals.
ConstantsConstant-value block output and constant parameters in a model.

Definition (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.

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 CategoryDescription
Initialize/TerminateEntry-point functions for initialization and termination
ExecutionEntry-point functions for initiating execution and resets
Shared utilityShared 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. You might need to define a function customization template in the Embedded Coder Dictionary. Templates are not available by default.

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

Hyperlink 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.

Introduced in R2018a