Main Content

Generate C or C++ Code from Stateflow Blocks

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 make your code more readable, more compact, and faster to execute.

Generate Code by Using Simulink Coder

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 non-real-time applications, including:

  • Simulation acceleration

  • Rapid prototyping

  • Hardware-in-the-loop (HIL) testing

Using Simulink Coder also allows you access to 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 model is valid.

For more information, see Generate Code Using Simulink Coder (Simulink Coder).

Generate Code by Using Embedded 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, and ASAP 2.

Embedded Coder also provides support packages with advanced optimizations and device drivers for specific hardware.

For more information, see Generate Code Using Embedded Coder (Embedded Coder).

Optimizing Generated Code for Stateflow

For design tips to optimizing generated code from Stateflow objects, see Design Techniques to Optimize Stateflow Objects for Code Generation (Embedded Coder).

Generate Code for Rapid Prototyping and Production Deployment

Goal

Simulink Coder Documentation

Embedded Coder Documentation

Generate C/C++ source code

Source Code Generation (Simulink Coder)

Source Code Generation (Embedded Coder)

Generate C/C++ source code and build executable

Generated Code Compilation (Simulink Coder)

Generated Code Compilation (Embedded Coder)

Integrate external code

External Code Import (Simulink Coder)

External Code Import (Embedded Coder)

Include external code only for library charts in a portable, self-contained library for use in multiple models

Integrate External Code with Library Charts (Simulink Coder)

Integrate External Code with Library Charts (Embedded Coder)

Optimize generated code

Code Efficiency (Simulink Coder)

Code Efficiency (Embedded Coder)

Traceability of Stateflow Objects in Generated Code

Traceability comments provide a way to:

  • Validate 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 more information, see Trace Stateflow Elements in Generated Code (Embedded Coder) and Navigate Between Simulink Model and HDL Code by Using Traceability (HDL Coder).

Note

Stateflow blocks do not support code generation if one of the data uses an alias type and has variable size. This limitation does not apply to chart-level input, output, or local data. For more information on defining variable-size data, see Declare Variable-Size Data in Stateflow Charts.

Related Topics