Main Content

Generate C Code by Using Simulink Coder Quick Start Tool

Prepare model 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.

  1. Open model SecondOrderSystem, if not already open, by entering the command openExample("SecondOrderSystem") in the command Window.

  2. If the C Code tab is not already open, in the Apps gallery, under Code Generation, click Simulink Coder.

  3. Open the Simulink Coder Quick Start tool. On the C Code tab, click Quick Start.

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

  5. In the Generate Code step, apply the proposed changes and generate code from the model by clicking Next.

  6. Click Finish. In the Simulink Editor, return to the C Code tab.

Inspect the Generated Code

The code generator creates folder SecondOrderSystem_grt_rtw in your current working folder and places source code files in that folder. The generated code is in two primary files: SecondOrderSystem.c and SecondOrderSystem.h. The file SecondOrderSystem.c contains the algorithm code, including the ODE solver code. Model data and entry-point functions are accessible to a caller by including SecondOrderSystem.h. The 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.

  1. Open the code generation report. In the C Code tab, click Open Report.

  2. 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:

    • SecondOrderSystem_initialize

    • SecondOrderSystem_step

    • SecondOrderSystem_terminate

    The functions have a void-void interface, 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.

  3. Review the entry-point functions in the generated code. In the left pane of the code generation report, under Code, click file name 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 SecondOrderSystem.h. Then, review code for the initialize and terminate functions.

You can configure code generation customizations, regenerate code, and check results in the code generation report.

Next, verify whether model simulation results match generated executable program results.