Main Content

Define the Interfaces Between the Generated Code and Your Deployment Platform

To generate code that you can deploy in your target environment, create custom code interface definitions that define how the generated code interacts with the target platform software. By defining the code interfaces separately from your models, you can:

  • Generate code that conforms to your software architecture by default by applying the code interface configuration to your models.

  • Use the same code interface definitions for multiple models that you intend to deploy to the same target environment, such as models across the model hierarchy.

  • Maintain separate configurations for separate target deployment environments and quickly switch your models to use a different code interface configuration.

Later in the workflow, you apply the code interface configuration to your models.

Choose a Code Interface Configuration for Your Deployment Goals

Depending on your deployment goals, you define one of these types of interface configurations:

  • Service interface configuration (shown in this example) — The code generator creates an algorithm that you intend to deploy within a larger application. 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 stored in a Simulink® data dictionary.

  • 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 in an Embedded Coder Dictionary. For more information, see Create and Verify Storage Class and Create Data Interface Configuration Programmatically.

  • AUTOSAR component configuration — If you have AUTOSAR Blockset, you can generate code for an AUTOSAR software component. For more information, see AUTOSAR Component Configuration (AUTOSAR Blockset).

Each type of interface configuration contains definitions for the different types of code interfaces. Later in the workflow, you map elements of your models to the associated code interface definitions so that the code generator generates the corresponding code.

If your deployment scenario requires more advanced customizations that are not available in the Embedded Coder Dictionary, you can create definitions by using the custom storage class designer. To use these definitions, you must configure and maintain model elements individually. For more information, see Create Storage Classes by Using the Custom Storage Class Designer.

Example Platform Middleware Services

For this example, the platform middleware contains a main function that calls the generated algorithm code and provides services that the generated code calls. In this case, the model should use a service interface configuration for code generation. When you set up the service interface configuration, you define the generated code interfaces to match the interfaces of the platform middleware.

The example main function is in the file EvPowerControllerIntegration.c.

 EvPowertrainControllerIntegration.c

The platform middleware provides the services in the file EvControllerServices.c.

 EvControllerServices.c

For more information about the platform middleware code that integrates the generated code, see Implement Integration Code.

Create an Embedded Coder Dictionary to Contain Interface Definitions

To contain the service interface configuration, create an Embedded Coder Dictionary that you share with the models from which you want to generate code. In the dictionary, you define the interfaces of the generated functions and the calls to platform-provided services.

  1. If you have not already done so, follow the steps in the previous part of the workflow, Configure Basic Code Generation Settings, or run the script.

    openExample("EvPowertrainController")
    ConfigureEvPowertrainModel

  2. From the model EvPowertrainController, open the Embedded Coder app. On the C Code tab, click Code Interface > Set up shared Embedded Coder Dictionary.

  3. In the Set Up Embedded Coder Dictionary dialog box, select Create Embedded Coder Dictionary. In the Create dictionary text box, enter the name CoderDict.sldd.

    Set Up Embedded Coder Dictionary dialog box.

    Embedded Coder creates this data dictionary to store the Embedded Coder Dictionary. Click Next.

  4. Select Service Interface and click Create.

    Set Up Embedded Coder Dictionary dialog box.

    The Embedded Coder Dictionary opens to display the service interface configuration. The configuration contains built-in interface definitions.

  5. On the General pane, set the Services Header File name to EvServices.h. This file is the header file for the services provided by the target platform software.

    Embedded Coder Dictionary showing the General pane.

Because you used the Set Up Embedded Coder Dictionary dialog, the dictionary is automatically shared with the referenced model BatteryManagementSystem. If you adapt this workflow for your own system, you might need to share the dictionary with other models or otherwise manage the code interface configuration. For more information, see Share Code Interface Configuration Between Models and Manage an Embedded Coder Dictionary.

Define Function Interfaces

To control the naming of generated functions, define function customization templates. The platform scheduler executes the generated code by calling the generated functions. Types of functions include:

  • Initialize and terminate functions, which are modeled by Initialize Function and Terminate Function blocks and contain startup and shutdown processing code.

  • Periodic and aperiodic functions, which contain the algorithm execution code for the model and execute at a periodic rate or aperiodically in response to events. Because this example uses an export-function model, these functions are represented by function-call subsystems.

For this example, add a custom definition for each type of function interface.

  1. In the Service Interface pane, click Initialize and Terminate.

  2. In the center pane, under Initialize and Terminate Functions, click Create.

  3. Select the new template definition and, on the right pane, set these properties:

    • NameInitTermFuncs

    • Function Naming Rule$R$N_func

    • Memory SectionNone

    When this definition is applied to the model EvPowertrainController, the generated initialize and terminate functions are named EvPowertrainController_init_func and EvPowertrainController_term_func.

  4. In the center pane, under Periodic and Aperiodic Functions, click Create.

  5. Select the new template definition and, on the right pane, set these properties:

    • NameExecutionFuncs

    • Function Naming Rule$R$N_func

    • Memory SectionNone

    When this definition is applied to the model EvPowertrainController, the generated execution functions are named EvPowertrainController_<export function name>_func.

  6. Select the definitions that you added as the dictionary default definitions for functions of the respective categories. In the row for each new entry, click the circle below Dictionary Default.

Coder dictionary showing the execution functions.

Define Service Interfaces

Specify how the generated code calls the services of the platform middleware by creating service interface definitions.

For this example, add definitions for the sender, receiver, and data transfer service interfaces.

  1. In the Service Interface pane, click Data Receiver.

  2. In the center pane, under Data Receiver, click Create.

  3. Select the new template definition and, on the right pane, set these properties:

    • NameDataReceiver

    • Data Communication MethodOutside Execution

    • Function Naming Ruleget_$X$N_outside_ex

    When this definition is applied to the model EvPowertrainController, each generated receiver function follows the naming rule and reads data from another function before the function starts executing.

  4. In the center pane, under Data Sender, click Create.

  5. Select the new template definition and, on the right pane, set these properties:

    • NameDataSender

    • Data Communication MethodOutside Execution

    • Function Naming Rule for Valueset_$X$N_outside_ex

    • Function Naming Rule for Value By Referencegetref_$X$N_outside_ex

    When this definition is applied to the model EvPowertrainController, each generated sender function follows the naming rule and sends data to another function before the function starts executing.

  6. Select the definitions that you added as the dictionary default definitions for functions of the respective categories. In the row for each new entry, click the circle below Dictionary Default.

  7. Make parameters tunable and preserve signals for measurement by selecting default definitions for these categories:

    • Parameter TuningParameterTuningExample1

    • Parameter Argument TuningParameterArgumentTuningExample1

    • MeasurementMeasurementExample1

  8. On the Dictionary tab, click Save.

Coder dictionary showing the service interfaces.

To see the other interface customization options that the Embedded Coder Dictionary provides, click the sections in the Service Interface pane on the left. For more information about defining service interfaces, see Create a Service Interface Configuration and Target Environment Services. For examples of service implementations for the communication methods, see Sample Target Environment Service Implementations of Data Communication Methods.

See Also

Topics