Define Storage Classes, Memory Sections, and Function Templates for Software Architecture

In a team or large organization, to enable multiple users to generate code that conforms to a standard architecture, you can create and share code generation definitions, such as storage classes, with those users. When configuring code generation settings for different models, the users can apply the definitions to data and function elements in the models, generating standardized code.

Create Code Definitions for Use as Default Code Generation Settings

To control the default appearance of model elements in the generated code, you apply settings to categories of elements, such as data and functions, by using the Code Mappings editor. For more information, see Configure Default C Code Generation for Categories of Model Data and Functions.

To create definitions that you and other users can use in the Code Mappings editor, use an Embedded Coder Dictionary.

When you create definitions in an Embedded Coder Dictionary, you must decide where to store the definitions.

  • If you want to use the definitions in only one model, create the definitions in the Embedded Coder Dictionary of the model.

  • If you want to use the definitions in multiple models, including the models in a model reference hierarchy, store the definitions in the Embedded Coder Dictionary of a Simulink® data dictionary. For general information about data dictionaries, see What Is a Data Dictionary? (Simulink).

To use an Embedded Coder Dictionary, and for limitations with respect to code generation definitions in an Embedded Coder Dictionary, see Embedded Coder Dictionary.

Create Code Definitions to Override Default Settings

To override the default settings that you specify in the Code Mappings editor, you apply code generation settings to individual model elements by using other tools such as the Model Data Editor and the Property Inspector. For general information, see Environment for Configuring Model Data and Functions for Code Generation.

To create code definitions that you can apply through these other tools, use the Custom Storage Class Designer. You can define storage classes and memory sections, which you can apply only to data elements. For more information, see Create Storage Classes by Using the Custom Storage Class Designer and Control Data and Function Placement in Memory by Inserting Pragmas.

Constrain Use of Storage Class Code Mappings

When defining a storage class in the Embedded Coder Dictionary, you can specify whether users can map the storage class to parameters, signals, or parameters and signals. To constrain the use of a storage class, the Data Initialization property must be set to None. Then, in the Property Inspector, under Allowed Usage, select Parameters, Signals, or Parameters and Signals. For more information, see Embedded Coder Dictionary.

Avoid Maintaining Duplicate Definitions in Packages and Dictionaries

If you want to use the same code generation definition as a default setting and for direct application to individual model elements, you do not need to store one copy of the definition in an Embedded Coder Dictionary and another copy in a package. Instead, store the definition in a package (by using the Custom Storage Class Designer). Then, configure one or more Embedded Coder Dictionaries to refer to the definition in the package. With this technique, when you want to make changes to the definition, you make the changes in only one place, the package.

However, if you create a storage class by using the Custom Storage Class Designer and set Type to FlatStructure, as described in Generate Structured Data, or to Other, as described in Finely Control Data Representation by Writing TLC Code for a Storage Class, you cannot configure an Embedded Coder Dictionary to refer to the package. Therefore, you cannot use the storage class in the Code Mappings editor.

To create a code generation definition in a package by using the Custom Storage Class Designer, see Create Storage Classes by Using the Custom Storage Class Designer. Then, to configure an Embedded Coder Dictionary to refer to the package, see Refer to Code Generation Definitions in a Package.

Deploy Code Generation Definitions to Users

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. For more information, see Deploy Code Generation Definitions.

Maintain Code Generation Definitions

In a model, you apply a code definition to a model element by selecting the name of the definition from a list, for example, in the Model Data Editor or in the Code Mappings editor. Also, you can associate a memory section with a storage class or function customization template by using a list in the Custom Storage Class Designer or in an Embedded Coder Dictionary.

Depending on the storage location of the code definition, changing or deleting the definition can break these usage points, requiring you to take action.

Package Definitions

If you create a code definition in a package, changing the name or deleting the definition can break usage points, which you must fix manually.

  • To fix usage points for individual data elements in a model, optionally, you can write a script. See the interactive and programmatic examples in Apply Built-In and Customized Storage Classes to Data Elements.

  • To fix usage points in a package (for example, when you change the name of a memory section that a storage class uses), use the Custom Storage Class Designer. You cannot write a script.

If you load a package into Embedded Coder Dictionaries (see Refer to Code Generation Definitions in a Package), when you make changes to the code definition (including to properties other than the name) or delete the definition:

  1. Reload the package. In each dictionary, click the Manage Packages button to open the Manage Packages dialog box. From the Select package drop-down list, select the package. Use the buttons to unload, and then reload the package.

    Reloading the package breaks usage points in the Code Mappings editor for affected models, which you must fix manually.

  2. Fix usage points in the Code Mappings editor for affected models. To write a script that fixes Code Mappings editor usage points, see Configure Default Data and Function Code Generation Programmatically.

Embedded Coder Dictionary Definitions

If you create a code definition in an Embedded Coder Dictionary:

  • Changing the name of the code definition does not break usage points. Simulink propagates the new name to the usage points.

  • Deleting a code definition breaks usage points in the Code Mappings editor for affected models. In the editor, lists display Unresolved. Fix these usage points manually. To fix them by writing a script, see Configure Default Data and Function Code Generation Programmatically.

Interact with Code Generation Definitions Programmatically

You can create and interact with code definitions by using the Embedded Coder Dictionary API.

  1. Represent the Embedded Coder Dictionary by using a coder.Dictionary object. Use the object to access the sections of the dictionary: Storage Classes, Memory Sections, and Function Customization Templates. If you have a package of code definitions, use the coder.Dictionary object to load and refresh the package in the dictionary.

  2. Represent a section of the dictionary by using a coder.dictionary.Section object. Use the section to access code definitions.

  3. Represent a code definition by using a coder.dictionary.Entry object. Use the entry to interact with the code definition and its properties.

You can also use functions such as coder.dictionary.copy and coder.dictionary.move to perform operations on Embedded Coder Dictionaries. For more information, see Create Code Definitions Programmatically.

See Also

|

Related Topics