Main Content

Deploy Code Generation Definitions

As described in Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture, you can create code generation definitions that you and other users can apply to model elements such as data and functions. The code definitions control the appearance of those elements in the generated code.

In a large organization with multiple models and users, you can share code generation definitions by storing them in a mutually accessible location. The process of sharing code definitions is called deployment.

Share Code Generation Definitions Between Multiple Models and Users

When you create custom code generation definitions, to share the definitions between models and other users:

  • When you create definitions in an Embedded Coder Dictionary, save the definitions in a SLDD file. Then, attach the SLDD file that contains the code generation definitions to each model. For an example, see Share Code Interface Configuration Between Models.

  • When you create definitions in a package by using the Custom Storage Class Designer, for each user, place the folder that contains the package namespace folder on the MATLAB® search path. For more information, see What Is the MATLAB Search Path?.

Dictionary Usage for Models Created with Different Versions of Simulink

Simulink® provides version handling for Embedded Coder® Dictionaries. When these events occur, Simulink synchronizes data in a data dictionary, including the Embedded Coder Dictionaries, for use with a model regardless of the Simulink version used to create the model.

  • You attach a data dictionary that was saved in a previous version of Simulink to a model (for example, you attach a dictionary saved in R2018a to a model that you develop in R2018b).

  • You open a model, which has a local Embedded Coder Dictionary, in a version of Simulink that is older than the current version (for example, you developed a model that uses a local dictionary in R2018a and you open that model in R2018b to continue development).

You also have the option to export (save) an Embedded Coder Dictionary for use with models created with a different version (previous or older) of the code generator.

For more information, see Dictionary Usage for Models Created with Different Versions of Simulink.

Configure Default Code Mapping in a Shared Dictionary

When you set up a shared Embedded Coder Dictionary, you can map default code definitions for categories of model data and functions in the dictionary. The dictionary default definitions apply in the mapping by default for the models that use the dictionary. In this example, you create the Embedded Coder Dictionary exSharedCodeDefs.sldd and map default definitions in the dictionary. If you have not created an Embedded Coder Dictionary, create one.

  1. In the Model Explorer, click File > New > Embedded Coder Dictionary.

  2. Specify the dictionary file name exSharedCodeDefs.sldd.

  3. Choose to create either a data interface or service interface configuration. For this example, create a service interface configuration. Click Create.

  4. Create code interface definitions in the dictionary. For this example, create a receiver service definition with these settings:

    • NameSharedReceiverService

    • Data Communication Method — Outside Execution

    • Function Naming Ruleget_data_$X$N

The type of code interface configuration that you choose determines what categories are available for you to set the default definitions.

Set Dictionary Defaults for a Service Interface Configuration

For a service interface configuration, you can select dictionary defaults for the function categories and service interface categories.

  1. In the Embedded Coder Dictionary dialog box, on the left pane, click the category of service interface that you want to configure. On the row for the service interface that you want to select, click the circle in the Dictionary Default column. You can select default service interface definitions for these interface categories.

    Service Interface CategoryDescription
    ReceiverCalls to the target platform receiver service, represented as root-level inports in the model
    SenderCalls to the target platform sender service, represented as root-level outports in the model
    Data transferCalls to the target platform data transfer service, represented as signal lines between callable functions within a component model
    TimerCalls to the target platform clock tick, represented as Discrete-time integrator, Weighted sample time, and Digital clock blocks in aperiodic export-function models
    Parameter tuningRepresentation of parameters that are accessible in memory for tuning
    Parameter argument tuningRepresentation of model parameter arguments that are accessible in memory for tuning
    MeasurementRepresentation of signals, states, and data stores so that data is preserved in generated code for measurement

    For this example, set the service interface for receivers to SharedReceiverService, which you created in the previous section.

  2. On the left pane, click the category of function template that you want to configure. On the row for the function template that you want to select, click the circle in the Dictionary Default column. You can select default function templates for these function categories.

    Model Function CategoryDescription
    Initialize and TerminateEntry-point functions generated for Initialize function and Terminate function blocks
    Periodic and AperiodicEntry-point functions generated for Function-call subsystem blocks, function-call models, Simulink function blocks, and S-function blocks
    Subcomponent Initialize and TerminateInitialize and termination functions of subcomponent models
    Subcomponent Periodic and AperiodicPeriodic and aperiodic functions of subcomponent models
    Shared UtilityShared utility functions

  3. Apply your changes by clicking Save and close the Embedded Coder Dictionary and Model Explorer.

Set Dictionary Defaults for a Data Interface Configuration

For a data interface configuration, you can select dictionary defaults for categories of model data elements and model functions.

  1. In the Embedded Coder Dictionary dialog box, on the left pane, click Data Defaults.

  2. In the Data Defaults table, configure default code definitions for categories of data and functions.

    Model Element CategoryDescription
    InportsRoot-level input ports of a model, such as Inport and In Bus Element blocks.
    OutportsRoot-level output ports of a model, such as Outport and Out Bus Element blocks.
    Signals, states, and internal dataData elements that are internal to the model, such as block output signals, discrete block states, data stores, and zero-crossing signals.
    Shared local data storesData Store Memory blocks that have 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. 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.
    Model parameter argumentsParameters 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 parametersParameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments.
    External parametersParameters 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 default mapping for a category applies to elements in a category throughout a model.

    For this example, set the storage class for data categories Inports, Outports, and Global data stores to ExportedGlobal.

  3. In the Embedded Coder Dictionary dialog box, on the left pane, click Function Defaults. Configure the function customization template and, if applicable, a memory section, for these categories of functions:

    Model Function CategoryDescription
    InitializeTerminateEntry-point functions for initialization and termination
    ExecutionEntry-point functions for initiating execution and resets
    Shared utilityShared utility functions
  4. Apply your changes by clicking Save and close the Embedded Coder Dictionary and Model Explorer.

Share Code Interface Configuration Between Models

You can generate standardized code for the same target execution environment from different models by sharing the same code interface configuration with the models through a shared Embedded Coder Dictionary. Attach the .sldd file that contains the Embedded Coder Dictionary to the models by setting the model configuration parameter Shared coder dictionary.

The dictionary CoderDictionary.sldd is attached to model A and model B.

For this example, configure the models RollAxisAutopilot and ConfigurationInterface to use the shared coder dictionary that you created in Configure Default Code Mapping in a Shared Dictionary.

  1. Open the models.

    ConfigurationInterface
    openExample("RollAxisAutopilot");

  2. In RollAxisAutopilot, open the Embedded Coder app.

  3. On the C Code tab, click Code Interface > Set up shared Embedded Coder Dictionary.

  4. In the dialog box, you can either open an existing Embedded Coder Dictionary or create a new one. For this example, select Open Embedded Coder Dictionary and specify the dictionary file exSharedCodeDefs.sldd. Click Next.

    The model now uses the shared coder dictionary.

  5. Repeat this process for the model ConfigurationInterface.

Now, for each model, the configuration parameter Shared coder dictionary is set to exSharedCodeDefs.sldd. The models use the default definitions from the shared dictionary, and the definitions from the dictionary are available in the Code Mappings panel for each model. You can alternatively configure multiple models to use the same dictionary by sharing the configuration set that has the parameter Shared coder dictionary set to the name of the dictionary file.

Apply Code Generation Definition in Models

Confirm that the shared definitions are available for use in the models by reviewing the code mapping settings and inspecting the generated code.

  1. In RollAxisAutopilot, open the Code Mappings panel. on the C Code tab, click Code Interface > Component Interface.

  2. On the Inports tab, confirm that the receiver services are set to Dictionary default: SharedReceiverService. The other receiver service definitions from the dictionary are available for selection in the Receiver Service lists.

  3. Generate code from RollAxisAutopilot.

  4. In the Code view, inspect the generated file RollAxisAutopilot.c. The code calls receiver services according to the default definition that you specified. For example, the code for the Switch block calls the receiver service get_data_RollAxisAutopilot_step_AP_Eng.

      /* Switch: '<S4>/Enable' incorporates:
       *  Inport: '<Root>/AP_Eng'
       *  Logic: '<S3>/NotEngaged'
       */
      if (!get_data_RollAxisAutopilot_step_AP_Eng()) {
        /* Switch: '<S3>/RefSwitch' incorporates:
         *  Constant: '<S3>/LoThr'
         *  Constant: '<S3>/UpThr'
         *  Constant: '<S3>/Zero'
         *  Inport: '<Root>/Phi'
         *  Logic: '<S3>/Or'
         *  RelationalOperator: '<S3>/RefThreshold1'
         *  RelationalOperator: '<S3>/RefThreshold2'
         *  UnitDelay: '<S4>/FixPt Unit Delay1'
         */
        if ((get_data_RollAxisAutopilot_step_Phi() >= 6.0F) ||
            (get_data_RollAxisAutopilot_step_Phi() <= -6.0F)) {
          rtDW.FixPtUnitDelay1_DSTATE = get_data_RollAxisAutopilot_step_Phi();
        } else {
          rtDW.FixPtUnitDelay1_DSTATE = 0.0F;
        }
    
        /* End of Switch: '<S3>/RefSwitch' */
      }

  5. Optionally, generate code from ConfigurationInterface and confirm that the code appears as expected.

Migrate Data Interface Configuration from Model File to Shared Data Dictionary

If you create code generation definitions in the Embedded Coder Dictionary of a model, the definitions are stored in the model file. If you want to share the definitions between models:

  1. Create a shared data dictionary as shown in Share Code Interface Configuration Between Models. Before you proceed to the next step, make sure all of the target models have access to the shared dictionary.

  2. Use the coder.dictionary.move function to migrate the definitions from the source model file to the shared data dictionary.

  3. Optionally, copy Code Mappings editor settings from the source model to other models by using the programmatic interface of the Code Mappings editor (see Configure Default Data and Function Code Generation Programmatically).

In this example, you convert a subsystem to a referenced model. Then, you create a storage class in the Embedded Coder Dictionary of the top model and use the storage class in the Code Mappings editor. To use the same Code Mappings editor settings for the top model and for the referenced model, you must extract the storage class into a shared data dictionary (.sldd).

Create Referenced Model

  1. Open the example model RollAxisAutopilot.

    openExample("RollAxisAutopilot");

  2. In the model, right-click the BasicRollMode subsystem and select Subsystem & Model Reference > Convert to > Referenced Model.

  3. Click Convert.

    The new referenced model, BasicRollMode, opens. The model file is in your current folder.

Create and Apply Storage Class

  1. In the top model RollAxisAutopilot, click the C Code tab. If the toolstrip does not show the C Code tab, open the Embedded Coder app.

  2. Click Code Interface > Embedded Coder Dictionary.

  3. In the Embedded Coder Dictionary dialog box, on the Storage Class table, click Create.

  4. For the new storage class, set these property values:

    • Name to internalsInFile.

    • Header File to $R_internal.h.

    • Definition File to $R_internal.c.

  5. In the model, in the Code Mappings editor, for the Data Defaults > Signals, states, and internal data row, set Storage Class to internalsInFile.

The referenced model cannot access the code generation definitions in the Embedded Coder Dictionary of the parent model. Now, move the storage class out of the parent model and into a shared data dictionary.

Move Storage Class to Shared Data Dictionary

To create the shared, standalone dictionary and store the code generation definition in it:

  1. In the Model Explorer, select File > New > Embedded Coder Dictionary.

  2. Create a coder dictionary named coderDefs.sldd. For this example, select the option to create a data interface configuration.

  3. Configure RollAxisAutopilot to refer to coderDefs.sldd. For the model, open the model configuration parameters and set Shared coder dictionary to coderDefs.sldd.

  4. Set the configuration parameter Shared coder dictionary for the model BasicRollMode to coderDefs.sldd.

  5. At the command prompt, move the code generation definitions from the RollAxisAutopilot model file to the coderDefs.sldd data dictionary.

    coder.dictionary.move("RollAxisAutopilot","coderDefs.sldd");

Now, both models have access to the storage class internalsInFile.

Copy Code Mappings Editor Settings from Parent Model to Referenced Model

The new referenced model does not use the Code Mappings editor settings that the parent model uses. To copy the settings from the parent model to the referenced model, use the programmatic interface of the Code Mappings editor.

At the command prompt, copy the storage class setting for Signals, states, and internal data from RollAxisAutopilot to BasicRollMode.

roll_mapping = coder.mapping.api.get("RollAxisAutopilot");
roll_sc = getDataDefault(roll_mapping,"InternalData","StorageClass");

mode_mapping = coder.mapping.api.get("BasicRollMode");
setDataDefault(mode_mapping,"InternalData","StorageClass",roll_sc);

Now, both models use the same Code Mappings editor settings. For more information about the programmatic interface of the Code Mappings editor, see Configure Default Data and Function Code Generation Programmatically.

See Also

Related Topics