Simulink Coder 8.0: Generate C and C++ Code From Simulink and Stateflow Models
Simulink Coder 8.0: Generate C and C++ Code From Simulink and Stateflow Models
Simulink Coder 8.0: Generate C and C++ Code From Simulink and Stateflow Models
0
Generate C and C++ code from Simulink and Stateflow models
Introduction
Simulink Coder™ (formerly Real-Time Workshop®) generates and executes C and C++ code from Simulink®
diagrams, Stateflow® charts, and MATLAB® functions. The generated source code can be used for real-time and
nonreal-time applications, including simulation acceleration, rapid prototyping, and hardware-in-the-loop
testing. You can tune and monitor the generated code using Simulink or run and interact with the code outside
MATLAB and Simulink.
Key Features
▪ ANSI/ISO C and C++ code and executables for discrete, continuous, or hybrid Simulink and Stateflow
models
1
Selecting and Working with Targets
The Simulink Model Explorer provides a single user interface in Simulink to configure all code generation
settings. From the Model Explorer you can:
The Code Generation Advisor checks your model and code generation settings, and then offers advice on how to
improve your configuration based on your needs. Once you have identified the optimal settings for your project,
you can save them as a configuration set to reuse with other models.
Large aircraft system modeled using Simulink. Simulink Coder supports advanced Simulink features for large-scale
modeling, including Model Blocks, variant subsystems, and arrays of buses.
Selecting Targets
Simulink Coder uses system target files to translate your models into source code and executables. Target files
specify the environment on which your generated code will run. Simulink Coder includes target files for several
ready-to-run configurations, and supports third-party and custom targets as well. Built-in targets include:
Generic Real-Time Target — Generates code for interactive tuning of model parameters, logs and displays real
time simulation results, and allocates data statically for efficient real-time execution
Generic Real-Time Malloc Target — Uses dynamic memory allocation in the generated code, enabling you to
include multiple instances of your model or multiple models in one executable
2
Rapid Simulation Target (RSim) — Provides a fast, flexible test platform for performing batch or Monte Carlo
simulation studies using fixed-step or variable-step solvers, and lets you save data for each run to unique output
files
You can extend these targets to create customized run-time interfaces and device driver files, which enables you to
target your specific environment and access Simulink Coder execution and debug capabilities in it.
Simulink Coder offers built-in support for compilation and execution of generated code with popular desktop
environments, including:
▪ Eclipse™ IDE
You can generate and deploy optimized C and C++ code in real-time embedded systems using Embedded Coder.
Simulink Coder enables you to define and control how the model data appears in the generated code. You can also
manage your data by:
Simulink Coder generates code from data stored in your Simulink diagram or in a data dictionary accessed via the
Simulink Model Explorer. By employing different data dictionary sets, you can redeploy code from a single model
to multiple targets.
Simulink Model Explorer showing various data types and storage classes used in a model. Note that custom storage
classes require Embedded Coder.
3
Generating Code
Simulink Coder offers comprehensive code generation support for Simulink and Stateflow features and
components, including:
▪ Atomic subsystems and atomic subcharts for reusing code via reentrant C functions
▪ Bus objects and arrays of buses for generating structures in your code
▪ MATLAB function blocks in Simulink and MATLAB functions in Stateflow for generating code from models
with MATLAB components
Simulink Coder also supports the Simulink product family, including fixed-point modeling, physical modeling,
signal processing and communications modeling, and rapid prototyping and hardware-in the-loop (HIL) testing
products.
Simulink Coder provides user-selectable options to facilitate execution of generated code in desktop and
simulation environments. These options include:
▪ Code reuse
▪ Expression folding
▪ Parameter inlining
Generated code using for-loop fusion and expression folding for a 10-element array model. Simulink Coder produces
efficient and readable code for single and multidimensional signals.
4
Executing Code in a Real-Time Environment
Simulink Coder provides a complete framework for executing the generated code in real time and incorporating it
into your execution environment. It generates single-rate or multirate code based on the periodic sample times
you specify in the model.
Execution Modes
Code can be deployed with or without an RTOS, and in single-tasking, multitasking, or asynchronous mode.
Single-Tasking Mode
In single-tasking mode, a simple scheduler invokes the generated code in a single thread of execution, preventing
preemption between rates.
Multitasking Mode
In multitasking mode, a deterministic, rate-monotonic scheduler invokes the generated code, enabling
preemption between rates. In a bare-board environment, you preempt the code with nested interrupts. In an
RTOS environment, you use task priorities and task preemption.
Asynchronous Mode
In asynchronous mode, nonperiodic or asynchronous rates are specified using Simulink S-functions. Simulink
Coder translates these rates into the code necessary for your execution environment. You model and generate
code for events, such as hardware interrupts, and trigger subsystems as independent tasks. You can use the
included asynchronous block library as a template for creating a library appropriate for other environments.
Real-Time Deployment
Simulink and Simulink Coder provide a complete set of target-independent capabilities for real-time deployment.
These include:
▪ Task priorities that you can specify for each rate in your model
▪ Production-quality counters and timers for computing absolute and elapsed time
▪ A Rate Transition block to specify data transfer mechanisms between rates (for example, semaphore, mutex,
and double buffering), enabling you to make trade-offs among data integrity, determinism, and performance
You can customize the generated code and its run-time environment by incorporating custom legacy C, Fortran,
Ada, and C++ code for simulation and code generation. Customization tools in Simulink Coder include a Target
Language Compiler (TLC), custom code blocks, template makefiles, build process APIs, and S-Function Builder.
Using the Legacy Code Tool, you can rapidly integrate existing C and C++ functions into your Simulink models,
which is particularly useful for integrating discrete-time code typically used in embedded systems.
Simulink Coder helps you optimize your algorithm’s performance and improve code execution behavior by
enabling you to interact with and analyze the generated executable.
Simulink Coder lets you monitor and tune block signals and parameters using the following data interfaces:
Target-based C API — Enables user-written code to access block outputs and parameters outside MATLAB and
Simulink
5
Host-based ASAP2 data exchange file — Lets you use the ASAP2 standard data description for data
measurement, calibration, and diagnostic systems outside MATLAB and Simulink
Simulink External mode — Lets you download new parameter values and upload signal values to view in
Simulink or to record in the MATLAB workspace. (Opening a dialog box for a source block causes Simulink to
pause. While Simulink is paused, you can edit the parameter values. You must close the dialog box to have the
changes take effect and allow Simulink to continue.)
Resources
Product Details, Demos, and System Requirements Online User Community
www.mathworks.com/products/simulink-coder www.mathworks.com/matlabcentral
Trial Software Training Services
www.mathworks.com/trialrequest www.mathworks.com/training
Sales Third-Party Products and Services
www.mathworks.com/contactsales www.mathworks.com/connections
Technical Support Worldwide Contacts
www.mathworks.com/support www.mathworks.com/contact
© 2011 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See www.mathworks.com/trademarks
for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders. 6