Generate C Service Interface Code from Component Models
This example shows you how to generate C code that uses a service code interface. For C component deployment, a service code interface offers benefits such as:
Customized interface code that includes built-in safeguards for maintaining data coherence for outside-execution and during-execution data communication
Customized code for data transfers between functions outside of (before and after) function execution
Support for accessing time values in tasks
Flexible scheduling
Mapping of model elements to complete interface abstractions rather than to storage classes and specific property settings
For a comparison between service or data code interfaces and links to more information about service interfaces, see Choose Code Interface for Code Generation.
If you are new to Embedded Coder® or your application code customization requirements are minimal, you can use graphical tools and default code configuration settings to quickly generate production-quality code. You can produce customized code that meets code guidelines and standards for integration with existing external code. Tooling is available to configure the code generator to meet requirements for interfacing, code appearance, packaging, and optimizations.
Generating and reviewing code for deployment to an embedded system can be as simple as preparing the model for code generation with the Embedded Coder Quick Start. Then, with code tools accessible from the Embedded Coder app, you can configure code interfaces, initiate code generation, and review the generated code.
Example Model
The tutorial uses example model ComponentDeploymentFcn
.
Open model ComponentDeploymentFcn
.
open_system('ComponentDeploymentFcn')
This export-function model includes:
An Initialize Function block that reads a value from nonvolatile memory and uses that value to initialize a state variable.
A triggered function-call subsystem that models an integration algorithm. The subsystem applies a forward integration method, applies a gain value of 1.25 to the state variable, and sends its output to an accumulator subsystem. The integrator subsystem executes aperiodically.
A periodic function-call subsystem that receives the value of the state variable from the integrator subsystem, increments the value by one, and applies a gain value, which is tunable during execution. The accumulator function executes periodically, every 1 second, and maintains the most recent value of the state variable.
A Terminate Function block that writes the output of the accumulator subsystem to nonvolatile memory after the accumulator algorithm stops executing.
You will learn how to:
Generate code by using the Embedded Coder Quick Start tool.
Configure the service interface.
Configure a model parameter as a global variable for tuning during run time.
Compare model simulation and generated code results for numeric equivalency by using the example test harness model
ComponentDeploymentFcnHarness.
Deploy the generated code.
To start the tutorial, see Generate Code by Using Embedded Coder Quick Start.