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 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, the Stateflow parser 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 Simulation.

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 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 Basics of Hardware-in-the-Loop simulation (Simscape).

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

  • ASAP2

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).

Design Tips for Optimizing Generated Code for Stateflow Objects

Be Explicit About the Inline Option of a Graphical Function

When you use a graphical function in a Stateflow chart, select Inline or Function for the property Function Inline Option. Otherwise, the code generated for a graphical function may not appear as you want. For more information, see Specify Properties of Graphical Functions.

Avoid Using Multiple Edge-Triggered Events in Stateflow Charts

If you use more than one trigger, you generate multiple code statements to handle rising or falling edge detections. If multiple triggers are required, use function-call events instead. For more information, see Activate a Stateflow Chart by Sending Input Events.

Combine Input Signals of a Chart Into a Single Bus Object

When you use a bus object, you reduce the number of parameters in the parameter list of a generated function. This guideline also applies to output signals of a chart. For more information, see Define Stateflow Structures.

Use Discrete Sample Times

The code generated for discrete charts that are not inside a triggered or enabled subsystem uses integer counters to track time instead of Simulink provided time. The generated code uses less memory, and enables code for use in Software-in-the-Loop (SIL) and Processor-in-the-Loop (PIL) simulation modes.

Generate Code for Rapid Prototyping and Production Deployment

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

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 for Library Charts (Simulink Coder)

Integrate External Code for 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:

  • 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).

Related Topics