Generate C Code by Using Simulink Coder Quick Start Tool
rtwdemo_secondOrderSystem for code generation and
generate C89/C90 compliant C code by using the Simulink®
Coder™ Quick Start tool. Then, inspect the generated code.
Generate Code with Quick Start Tool
The Quick Start tool chooses fundamental code generation settings based on your goals and application. For example, the Quick Start tool configures the model with a fixed-step solver, which is required for code generation.
rtwdemo_secondOrderSystemby entering the model name in the Command Window.
Save a copy of the model to a writeable location on the MATLAB® path.
If the C Code tab is not already open, in the Apps gallery, under Code Generation, click Simulink Coder.
Open the Simulink Coder Quick Start tool. On the C Code tab, click Quick Start.
Advance through the steps of the Quick Start tool. Each step asks questions about the code that you want to generate. For this tutorial, use the default settings. The tool validates your selections against the model and presents the parameter changes required to generate code.
In the Generate Code step, apply the proposed changes and generate code from the model by clicking Next.
Click Finish. In the Simulink Editor, return to the C Code tab. Configure code generation customizations, regenerate code, and check results in the code generation report.
Inspect the Generated Code
The code generator creates folder
rtwdemo_secondOrderSystem_grt_rtw in your current working
folder and places source code files in that folder. The generated code is in two
rtwdemo_secondOrderSystem.h. The file
rtwdemo_secondOrderSystem.c contains the algorithm code,
including the ODE solver code. Model data and entry-point functions are accessible
to a caller by including
rtwdemo_secondOrderSystem.h file includes the
extern declarations for block outputs, continuous states,
model output, entry points, and timing data.
In your current folder, the code generator creates an
slprj/grt/_sharedutils folder. This folder contains the file
rtwtypes.h, which defines standard data types that the
generated code uses by default. This sibling folder contains generated files that
can or must be shared between multiple models.
The code that you generate from a model includes entry-point functions, which you call from application code, such as an external main program. For a rate-based model, these functions include an initialization function, an execution function, and, optionally, terminate and reset functions. The functions exchange data with your application code through a data interface that you control.
Open the code generation report. In the C Code tab, click Open Report.
Open the Code Interface Report section. Review the entry-point functions that the code generator produces for the model. For the initialize, execution (step), and terminate functions, the code generator uses these names:
The functions have a
void-voidinterface, which means that they do not pass arguments. The functions gain access to data through shared data structures. Examples of such data include system-level input and output that the functions exchange with application code.
Review the entry-point functions in the generated code. In the left pane of the code generation report, under Code, click file name
rtwdemo_secondOrderSystem.c. Use the search field to find instances of the string
secondOrderSystem_step. Use the arrows to step through each instance. Do the same for the header file
rtwdemo_secondOrderSystem.h. Then, review code for the initialize and terminate functions.
Next, verify whether model simulation results match generated executable program results.