This example generates C code from the ex_codegen_dsp model and builds an executable from the generated code. You can run the executable inside the MATLAB environment. In addition, you can package and relocate the code to another development environment that does not have the MATLAB and Simulink products installed.
Prepare the model for code generation by specifying code generation settings in the Configuration Parameters dialog box. Choose the appropriate solver and code generation target, and check the model configuration for execution efficiency. For more details on each of these steps, see Generate C Code for a Model (Simulink Coder).
In the Modeling tab of the model toolstrip, click Model Settings. The Configuration Parameters dialog opens. Navigate to the Code Generation tab, select the Generate code only parameter, and click Apply.
After the model finishes generating code, the Code Generation Report appears, allowing you to inspect the generated code. Note that the build process creates a new subfolder called ex_codegen_dsp_grt_rtw in your current MATLAB working folder. This subfolder contains all the files created by the code generation process, including those that contain the generated C source code. For more information on viewing the generated code, see Generate C Code for a Model (Simulink Coder).
After your compiler is setup, you can build and run the compiled code. The ex_codegen_dsp model is currently configured to generate code only. To build the generated code, you must first make the following changes:
In the Modeling tab of the model toolstrip, click Model Settings. The Configuration Parameters dialog opens. Navigate to the Code Generation tab, clear the Generate code only parameter, and click Apply.
The code generator builds the executable and generates the Code Generation Report. The code generator places the executable in the working folder. On Windows, the executable is ex_codegen_dsp.exe. On Linux, the executable is ex_codegen_dsp.
Running the generated code creates a MAT-file that contains the same variables as those generated by simulating the model. The variables in the MAT-file are named with a prefix of rt_. After you run the generated code, you can load the variables from the MAT-file by typing the following command at the MATLAB prompt:
You can now compare the variables from the generated code with the variables from the model simulation. To access the last set of coefficients from the generated code, enter the following in the MATLAB prompt:
Once you generate code from your Simulink model, you can relocate the code to another development environment using the pack-and-go utility. Use this utility when the development environment does not have the MATLAB and Simulink products.
The pack-and-go utility uses the tools for customizing the build process after code generation and a packNGo (Simulink Coder) function to find and package files for building an executable image. The files are packaged in a compressed file that you can relocate and unpack using a standard zip utility.
You can package the code by either using the user interface or by using the command-line interface. The command-line interface provides more control over the details of code packaging. For more information on each of these methods, see Relocate or Share Generated Code (Simulink Coder).
You can use the Model Advisor checks to assess model readinessto generate code. To check and configure your model for code generationapplication objectives such as traceability or debugging, use theCode Generation Advisor.
4. Expand By Task > Code Generation Efficiency. To check your model for code generation efficiency, use the checks in the folder. By default, checks that do not trigger an Update Diagram are selected. The checks available for code generation efficiency depend on whether you have a Simulink Coder or Embedded Coder license.
The results shows a Run Summary that flags check warnings. The warnings identify issues that impact code efficiency. For more information about the report, see Save and View Model Advisor Check Reports.
You can include one model inside another model by using a Model block. Each instance of a Model block is a model reference. For simulation and code generation, blocks within a referenced model execute together as a unit. The model that contains a referenced model is a parent model. A collection of parent and referenced models constitutes a model hierarchy.
The code generator places the code for the top model in a hierarchy in the code generation folder and places the code for referenced models in an slprj folder. Subfolders in slprj are separate places for different types of files. For folder information, see Manage Build Process Folders.
By default, the Simulink Coder uses incremental code generation. When generating code, the software compares structural checksums of referenced model files to the generated code files to determine whether to regenerate model reference targets. To control when rebuilds occur, use the configuration parameter Model Referencing > Rebuild. For details, see Rebuild.
The Simulink Coder software also uses incremental loading. The code for a referenced model is not loaded into memory until the code for its parent model executes and requires the outputs of the referenced model. Simulink Coder then loads the referenced model target and executes the code. Once loaded, the target remains in memory until it is no longer used.
2. For the top model, open the C Code tab by selecting either Simulink Coder app or Embedded Coder app. To generate code for the model reference hierarchy, click the Build button.
To open the code generation report for a referenced model, at the top of the report, click ex_model_arg. In the drop-down, click ex_model_arg_ref. The source files for the referenced model are displayed along with the generated code for the referenced model.
You can store the models referenced by Model blocks anywhere on the MATLAB path. A top model can include models stored on different file systems or in different folders. The simulation targets and generated code derived from these models cannot be stored on different file systems or in different folders. To allow code reuse, you must set up models referenced by a top model to simulate and generate model reference target code in a single code generation folder.
The second approach requires maintenance of several instances of the model reference code. It is possible for generated code to become redundant, for example, when you make changes to the referenced model. To minimize code regeneration of referenced models, choose a specific code generation folder for all sessions.
Open the Configuration Parameters dialog box, Solver pane. To generate code for a model, you must configure the model to use a fixed-step solver. The following table shows the solver configuration for this example.
The system target file defines an environment for generating and building code for execution on a certain hardware or operating system platform. For example, one property of a system target file is the value for the CodeFormat TLC variable. The GRT configuration requires a fixed step solver and the rsim.tlc supports variable step code generation.
Before generating code for a model, use the Code Generation Advisor to check the model conditions and configuration settings. The check identify issues that can result in inaccurate or inefficient code.
Messages appear in the Diagnostics Viewer. The code generator produces standard C and header files, and an HTML code generation report. The code generator places the files in a build folder, a subfolder named ThrottleControl_grt_rtw under your current working folder.
Build the model. Watch the messages in the Diagnostics Viewer. The code generator uses a template make file associated with your system target file selection to create an executable file. You can run this program on your workstation, independent of external timing and events.
To generate code, change the model configuration to specify a fixed-step solver then select a system target file. Using the grt.tlc file requires a fixed-step solver. If the model contains continuous time blocks, you can use a variable-step solver with the rsim.tlc system target file.
This example shows how you can generate HDL code for a simple counter model in Simulink. This model is compatible for HDL code generation. To create this counter model, see Create HDL-Compatible Simulink Model.
HDL Coder compiles the model before generating code. Depending on model display options such as port data types, the model can change in appearance after code generation. As code generation proceeds, HDL Coder displays progress messages in the MATLAB command line with links to the configuration set and the generated files. To view the files in the MATLAB Editor, click the links.
A folder icon for the hdlsrc folder appears in the current folder. To view the generated code and script files, double-click the hdlsrc folder, and then double-click the folder that has the same name as the model for which you generated HDL code.
HDL_DUT.vhd: VHDL code that contains the entity definition and RTL architecture implementing the counter that you designed. If you generated Verilog or SystemVerilog code, you get a HDL_DUT.v or HDL_DUT.sv file.
HDL Coder creates a behavioral model of the HDL code called the generated model. The generated model name is the same as the original model and has the prefix gm_. The generated model is bit-true and cycle-accurate to the generated HDL code. This model shows the effect of block implementations and speed and area optimizations that you specified. See also Speed and Area Optimizations in HDL Coder.
To view your generated HDL code alongside your model, you can use the Code view. After you generate HDL code for your model, the Code view displays the generated code to the right of your model. To manually open the Code view, open the HDL Coder app. On the Simulink toolstrip click the View Code button. Select the file that you want to display by using the drop-down list at the top of the Code view. You can dock or undock the Code view from the editor and minimize or expand the Code view using the down arrow in the upper right corner of the Code view.
aa06259810