Main Content

Define Service Interfaces, 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 service interfaces, 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 in the Code Mappings Editor

To control the appearance of model elements in the generated code, you apply code definitions to the elements by using the Code Mappings editor. To create code definitions that you and other users can use in the Code Mappings editor, use a code interface configuration in an Embedded Coder Dictionary. Decide what type of code interface configuration to use by considering what type of code you want to generate from your model and how you want to deploy the code in the target execution environment.

  • Data interface configuration — The code generator creates a program intended to run on the target device. The generated code uses the data interfaces that you define. For more information, see C Data Code Interface Configuration for Model Interface Elements.

  • Service interface configuration — The code generator creates an algorithm that you intend to deploy within a larger application for a target platform. The platform middleware calls the algorithm code and provides the services for the target device. The generated code calls the services according to your definitions in the service interface configuration. To create a service interface configuration, you must use an Embedded Coder Dictionary that is saved in a SLDD file. For more information, see Service Interfaces.

When you create definitions in an Embedded Coder Dictionary, decide where to save the definitions by considering how you intend to use the definitions and the type of code interface configuration. This table lists where to store the code definitions.

Code Interface Configuration TypeUsage of DefinitionsLocation for Embedded Coder Dictionary
Service interface configurationMultiple models or one modelSLDD file that all of the models are configured to use for the coder dictionary
Data interface configurationMultiple models — sharing a set of code definitions to standardize codeSLDD file that all of the models are configured to use for the coder dictionary
Data interface configurationOne model — prototyping generated code by using one modelLocal Embedded Coder Dictionary of the model.

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

Create Code Definitions for External Data Objects

To create definitions for data objects that you store in the base workspace or in a Simulink® data dictionary, use the Custom Storage Class designer to create a definition in a package. For these data objects, you apply code generation settings to the data object instead of applying them to model elements in the model code mappings. When an item in a model uses the data object and its code generation settings, the code mapping for the item reflects the specification of the data object. 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.

Create an Embedded Coder Dictionary

By default, a model contains a local Embedded Coder Dictionary that you can use to create code definitions for the model. To share code definitions with multiple models, you must create an Embedded Coder Dictionary that you save in a SLDD file. You then configure models to reference the SLDD file in order to use the code definitions in those models.

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

  2. Specify a name for the SLDD file that will contain the Embedded Coder Dictionary. For example, code_definitions.sldd.

  3. Choose to create either a data interface or service interface configuration in the dictionary.

  4. Add definitions to the dictionary and save the SLDD file.

Use Package Code Definitions in Embedded Coder Dictionaries

If you want to use the same code generation definition in the Code Mappings editor and in data objects, 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.

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.

For more information about maintaining definitions in packages and dictionaries, see Migrate Package Definitions to Embedded Coder Dictionary to Use in Code Mappings.

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 Auto or None. Then, in the Property Inspector, under Allowed Usage, select Parameters, Signals, or Parameters and Signals. For more information, see Embedded Coder Dictionary.

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.

Interact with Code Definitions in Data Interface Configuration Programmatically

You can programmatically 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. 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 Data Interface Configuration Programmatically.

See Also

|

Related Topics