To generate C or C++ code from Simulink® models that include a Stateflow® chart, you must use Simulink Coder™. In addition to Simulink Coder, you may use Embedded Coder® to further enhance the generated code. Embedded Coder enhancements make your code more readable, more compact, and faster to execute.
When you generate code for a target, Stateflow automatically parses the Stateflow machine, which evaluates the graphical and nongraphical objects and data in each Stateflow machine against the supported chart notation and the action language syntax. For more information, see Detect Common Modeling Errors During Chart Simulation.
Simulink Coder allows you to generate C and C++ code from models that contain Stateflow charts. You can then use the generated code for real-time and nonreal-time applications, including:
Simulation acceleration
Rapid prototyping
Hardware-in-the-loop (HIL) testing
Using Simulink Coder also allows you access to Classic Accelerator and Rapid Accelerator modes. Accelerator modes work by generating target code, which is then used for execution. For more information about these modes, see How Acceleration Modes Work (Simulink).
HIL testing allows you to test your controller design and determine if your physical system (plant) model is valid. For more information about HIL testing, see What Is Hardware-In-The-Loop Simulation? (Simscape).
To get started setting up a model for code generation with Simulink Coder, see Configure Model and Generate Code (Simulink Coder).
With the addition of Embedded Coder you can generate C or C++ code that is more compact, easier to read, and faster to run. Embedded Coder additionally extends the abilities of Simulink Coder by allowing you control over generated functions, files, and data. Further, Embedded Coder enables easy integration for legacy code, data types, and calibration parameters. Embedded Coder supports software standards for:
AUTOSAR
MISRA C
ASAP2
Embedded Coder also provides support packages with advanced optimizations and device drivers for specific hardware.
To get started setting up a model for code generation with Embedded Coder, see Generate Code by Using the Quick Start Tool (Embedded Coder).
Do Not Access Machine-Parented Data
Be Explicit About the Inline Option of a Graphical Function
Avoid Using Multiple Edge-Triggered Events in Stateflow Charts
This table directs you to information about code generation based on your goals.
Goal | Simulink Coder Documentation | Embedded Coder Documentation |
---|---|---|
Generate C/C++ source code | Code Generation Basics (Simulink Coder) | Code Generation Basics (Embedded Coder) |
Generate C/C++ source code and build executable | Build Process (Simulink Coder) | Build Process (Embedded Coder) |
Integrate external code | External Code Integration (Simulink Coder) | Code Integration (Embedded Coder) |
Include external code only for library charts in a portable, self-contained library for use in multiple models | Integrate External Code for Library Charts (Simulink Coder) | Integrate External Code for Library Charts (Embedded Coder) |
Optimize generated code | Reduce Memory Usage for Boolean and State Configuration Variables (Simulink Coder) | Reduce Memory Usage for Boolean and State Configuration Variables (Embedded Coder) |
Traceability comments provide a way to:
Verify generated code. You can identify which Stateflow object corresponds to a line of code and track code from different objects that you have or have not reviewed.
Include comments in code generated for large-scale models. You can identify objects in generated code and avoid manually entering comments or descriptions.
To enable traceability comments, you must have Embedded
Coder or HDL Coder™ software. For C/C++ code generation, comments appear in the generated
code for embedded real-time (ert
) based targets only. For more
information, see Trace Stateflow Elements in Generated Code (Embedded Coder) and Navigate Between Simulink Model and HDL Code by Using Traceability (HDL Coder).