Deploy Code Generation Definitions

As described in Define 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 a package by using the Custom Storage Class Designer, for each user, place the folder containing the package folder on the MATLAB® path. For more information, see What Is the MATLAB Search Path?.

  • When you create definitions in an Embedded Coder Dictionary, store the definitions in a Simulink® data dictionary. Then, link each model to a separate data dictionary. Configure each separate dictionary to refer to the dictionary that contains the code generation definitions. For an example, see Share Embedded Coder Dictionary Definition Between Models.

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 link a model to a data dictionary that was saved in a previous version of Simulink (for example, you link a model that you develop in R2018b with a dictionary saved in R2018a).

  • 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

While setting up an Embedded Coder Dictionary intended to be shared by multiple models, you can map default code definitions for categories of model data and functions. In this example, you map default code definitions for the Embedded Coder Dictionary in exSharedCodeDefs.sldd.

  1. If not already defined, define code definitions (storage classes, function customization templates, and memory sections) in a data dictionary. For this example, you define code definitions in data dictionary exSharedCodeDefs.sldd. Place a copy of this file in a writeable location.

  2. In Current Folder pane of the MATLAB Command Window, double-click the file name of your copy of exSharedCodeDefs.sldd.

  3. In the Model Hierarchy pane of the Model Explorer, right-click the dictionary node and select Show Empty Sections. Model Explorer lists nodes for Design Data, Configurations, Embedded Coder Dictionary, and Other data.

  4. Open the Embedded Coder Dictionary. In the Model Hierarchy pane, click Embedded Coder Dictionary. Then, in the right pane, click Open Embedded Coder Dictionary.

  5. In the Embedded Coder Dictionary dialog box, click Configure Defaults.

  6. In the Configure Dictionary Defaults dialog box, configure default code definitions for categories of data and functions.

    • On the Data Defaults tab, configure the storage class and, if applicable, a memory section, for these categories of data:

      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.
      ConstantsConstant-value block output and constant parameters in a model.
    • On the Function Defaults tab, configure the function customization template and, if applicable, a memory section, for these categories of functions:

      Model Function CategoryDescription
      Initialize/TerminateEntry-point functions for initialization and termination
      ExecutionEntry-point functions for initiating execution and resets
      Shared utilityShared utility functions

    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.

  7. Apply your changes and close the dialog box. Click Apply. Then, click OK.

  8. Close the Embedded Coder Dictionary and Model Explorer.

Share Embedded Coder Dictionary Definition Between Models

This example shows how to create a storage class that yields separate global variables in the generated code and share the storage class between two models, rtwdemo_roll and rtwdemo_configinterface. Then, you configure each model to use the storage class as the default code generation setting for internal data, which includes block states.

It is a best practice to store shared code generation definitions in a standalone data dictionary, which means that no models are linked directly to the dictionary. Then, you can link the target models to one or more additional, intermediate dictionaries, which you configure to refer to the standalone dictionary. With this modular dictionary hierarchy, each model can store other data, such as design data, separately from the code generation definitions.

In a hierarchy of referenced data dictionaries (see Dictionary Referencing), only one dictionary can store code generation definitions in an Embedded Coder Dictionary. If a model has access to multiple Embedded Coder Dictionaries through referenced data dictionaries, use the functions coder.dictionary.move and coder.dictionary.remove to transfer and delete Embedded Coder Dictionaries until only one exists in the dictionary hierarchy.

The figure shows an example dictionary hierarchy. In the figure:

  • modelOne.slx and modelTwo.slx are different model files.

  • dataForModelOne.sldd and dataForModelTwo.sldd are data dictionaries. Each dictionary stores design data, such as numeric MATLAB variables, for one of the models.

  • coderDefs.sldd is a shared data dictionary that stores code generation definitions for both models. The other dictionaries reference coderDefs.sldd.

Create Code Generation Definition in Standalone Data Dictionary

Create a data dictionary. Then, create the storage class in the dictionary.

  1. At the command prompt, open the Model Explorer.

    daexplr

  2. In the Model Explorer, select File > New > Data Dictionary.

  3. Name the dictionary coderDefs.sldd.

  4. In the Model Explorer Model Hierarchy pane, right-click coderDefs and select Show Empty Sections.

  5. Select the Embedded Coder Dictionary node.

  6. In the Dialog pane (the right pane), click Open Embedded Coder Dictionary.

  7. In the Embedded Coder Dictionary dialog box, click Add.

  8. For the new storage class, in the Property Inspector pane, set Name to internalsInFile.

  9. In the Model Explorer Model Hierarchy pane, right-click coderDefs and select Save Changes.

Link Models to Data Dictionaries

Link each model to a separate data dictionary. Configure each dictionary to refer to the standalone dictionary (coderDefs.sldd).

  1. Open the models.

    rtwdemo_configinterface
    rtwdemo_roll

  2. In rtwdemo_roll, on the Modeling tab, in the Design gallery, click Link to Data Dictionary.

  3. In the Model Properties dialog box, click New.

  4. Create a dictionary named rtwdemo_roll_data.sldd by using the Create a new Data Dictionary dialog box.

  5. In the Model Properties dialog box, click OK.

    Simulink links the model to the new dictionary. The dictionary does not contain any data because the model does not use data in the base workspace.

  6. Open the dictionary in the Model Explorer. In the lower-left corner of the model, click the model data badge, then click the External Data link. Under the External Data node, select rtwdemo_roll_data.sldd.

  7. In the Dialog pane, under Referenced Dictionaries, click Add.

  8. In the Open Data Dictionary dialog box, double-click coderDefs.sldd.

  9. Use the Model Hierarchy pane to save the changes you made to rtwdemo_roll_data.sldd.

  10. Link rtwdemo_configinterface to a dictionary named rtwdemo_configinterface_data.sldd.

    rtwdemo_configinterface uses a variable in the base workspace. To migrate the variable into the dictionary, in the Model Properties dialog box, click Migrate data. Then follow the instructions to link the model to the dictionary and copy the referenced variables.

  11. Configure rtwdemo_configinterface_data.sldd to refer to coderDefs.sldd.

Apply Code Generation Definition in Models

Confirm that the shared definition is available for use in the models by applying it as a default setting for internal data and inspecting the generated code.

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

  2. Under Code Mappings > Data Defaults, for the Signals, states, and internal data row, set Storage Class to internalsInFile.

  3. In rtwdemo_configinterface, use the Code Mappings editor to set Storage Class to internalsInFile for the Signals, states, and internal data row.

  4. Generate code from rtwdemo_roll.

  5. In the Code view, inspect the generated file rtwdemo_roll.c. The file defines global variables that correspond to state data in the model, such as the state of the Integrator block in the BasicRollMode subsystem.

    /* Storage class 'internalsInFile' */
    real32_T rtwdemo__FixPtUnitDelay1_DSTATE;
    real32_T rtwdemo_roll_Integrator_DSTATE;
    int8_T rtwde_Integrator_PrevResetState;

  6. Optionally, generate code from rtwdemo_configinterface and confirm that the code appears as expected.

Migrate Definitions 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 Embedded Coder Dictionary Definition 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.

    To migrate individual definitions, in the Embedded Coder Dictionary dialog box, use the Cut and Paste buttons on the quick access toolbar.

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

    rtwdemo_roll

  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 rtwdemo_roll, 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 Classes tab, click Add.

  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

As a best practice, link each model to a separate data dictionary (.sldd), create a shared, standalone data dictionary to store the code generation definition (the storage class), and configure dictionary referencing so that all of the models can use the code generation definition.

To link the models to separate dictionaries:

  1. In rtwdemo_roll, on the Modeling tab, in the Design gallery, click Link to Data Dictionary.

  2. In the Model Properties dialog box, click New.

  3. Use the Create a new Data Dictionary dialog box to create a dictionary named rtwdemo_roll_data.sldd.

  4. In the Model Properties dialog box, click OK.

  5. In the Link Model to Data Dictionary dialog box, click Change this model only.

  6. In the BasicRollMode model, use the Model Properties dialog box to link the model to a new data dictionary named basicRollMode_data.sldd.

  7. Click, OK.

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

  1. In both models, open the dictionary in the Model Explorer. At the lower-left corner of the block diagram, click the model data badge, then click the External Data link. Under the External Data node, select the dictionary.

  2. In the Model Explorer, select File > New > Data Dictionary.

  3. Create a data dictionary named coderDefs.sldd.

  4. In the Model Explorer Model Hierarchy pane, select the rtwdemo_roll_data node.

  5. In the Dialog pane (the right pane), under Referenced Dictionaries, click Add.

  6. Add a reference to coderDefs.sldd.

  7. Configure basicRollMode_data.sldd to refer to coderDefs.sldd.

  8. In the Model Hierarchy pane, right-click the rtwdemo_roll_data node and select Save Changes.

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

    coder.dictionary.move('rtwdemo_roll','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 rtwdemo_roll to BasicRollMode.

roll_mapping = coder.mapping.api.get('rtwdemo_roll');
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.

Use Data Dictionary to Store Code Definitions but Not Design Data

A Simulink data dictionary can store design data such as MATLAB variables and Simulink.AliasType objects (see Global and Shared Data: Data Dictionary). However, migrating the design data of a large model or of multiple models from the base workspace to a data dictionary can take time and careful planning.

If you want to store shared code generation definitions in a data dictionary but do not want to migrate the design data of a model, in the Model Properties dialog box, on the External Data tab, select the Enable model access to base workspace property. The model can use design data from the base workspace and code generation definitions from the dictionary or from a referenced dictionary. For more information, see Continue to Use Shared Data in the Base Workspace.

See Also

Related Topics