Documentation Center

  • Trial Software
  • Product Updates

Getting Started with Application Development

This example shows how to generate code from a Simulink® model using IDE Link component for Texas Instruments™ (TI) processors. A model is used to show you how to generate a working project specific to ADI DSPs. This example requires basic familiarity with the IDE Link "Automation Interface" and "Project Generator" components.

Supported IDE/tool chain:

  • Texas Instruments™ Code Composer Studio™

Supported processors:

Recommended tutorials:

Models used in this example:

Introduction

IDE Link provides tools that help you verify your code during development by enabling you to run portions of simulations on your hardware and profiling the executing code. Using the Automation Interface and Project Generator components, it offers verification tools including Processor-in-the-Loop (PIL), Real-Time Execution Profiler, Code Replacement Library (CRL) optimization and Stack Profiler. Processor-in-the-Loop simulation helps you evaluate how your process runs on a processor. The Real-Time Execution Profiler lets you see how the tasks in your process will run in real-time on the processor. CRL optimization lets you improve the performance of your application by using compiler-specific intrinsics in the generated code. The Stack Profiler lets you measure the actual stack usage of your application.

This example suggests a development workflow starting with a model created in Simulink and proceeding through generating verified and profiled code for the processor. The example guides you through intermediate stages of verification and profiling by showing the use of PIL, Real-Time Execution Profiling and Stack Profiling.

The tasks are arranged in the suggested order, but are presented in an independent manner that allows you to skip sections as you wish.

NOTE: Before continuing, verify that you have configured at least one board in Code Composer Studio™ (CCS). If not, please go to Code Composer Studio Setup and select a board based on a supported TI processor.

Task 1 - Design and Simulate Algorithm

The following figure shows a model that calculates the sum and difference of two random signal vectors.

Simulate the algorithm

1. Open the SumDiff ModelSumDiff Model and click the "Run" button to run the simulation.

2. Double-click "View Scopes" and observe the SUM and DIFF outputs.

3. Stop the simulation by clicking the "Stop" button.

Task 2 - Generate and Verify Code

After simulating the algorithm and confirming the correctness of the simulation results, the next step is to verify that the generated code behaves the same when run on the embedded processor. PIL simulation can be used to run part of the application on hardware while the rest is run in Simulink.

In this task you will verify the code generated for the "Compute_Sum_and_Diff" subsystem by making sure that there is no difference between the outputs of the original subsystem and the PIL block representing it.

The following figure shows the previous Simulink model with additional blocks used to compare the "Compute_Sum_and_Diff" algorithm simulation results against a processor-based implementation.

PIL builds and uses a MEX function to run the PIL simulation block. Before continuing, set up a compiler that MATLAB® will use to build the MEX function. Run the command mex -setup to select a compiler configuration. For more information, read Building MEX-FilesBuilding MEX-Files.

Set up the "Compute_Sum_and_Diff" subsystem to enable code-generation

1. Open the PIL ModelPIL Model.

2. Open the Model Configuration Parameters dialog, set the "System target file" parameter under "Code Generation" to "idelink_ert.tlc" and click "Apply".

3. Next, select "Coder Target" under "Code Generation". Click "Target Hardware Resources" and set the parameters to match your target hardware. Click "OK".

For more information on configuring the model for your target hardware see this sectionthis section in the IDE Link documentation.

Configure the model to perform PIL

In the following steps, you will configure the model so the build process does the following when you generate code for the "Compute_Sum_and_Diff" subsystem:

  • Creates a PIL block in a new model

  • Generates the PIL algorithm code and project in the CCS window

  • Builds the generated project

  • Loads the program onto the processor

1. In your model, select "Simulation" > "Model Configuration Parameters".

2. Under Code Generation > Coder Target > Tool Chain Automation, set Build action to "Create_Processor_In_the_Loop_project".

3. Click OK.

Create a PIL block for the "Compute_Sum_and_Diff" subsystem

In the following steps, you will create the PIL algorithm object code and a corresponding PIL block in a new model.

1. Right-click the "Compute_Sum_and_Diff" subsystem, and select "C/C++ Code" > "Build This Subsystem" from the context menu.

2. Click "Build" button on the opened dialog box. Observe the progress of the build process in the MATLAB® command window.

Copy the PIL block to the model

1. Copy the PIL block to "sumdiff_pil" model, to the location where "Place PIL block here" is written.

2. Connect the signal lines to the corresponding input and output ports of the PIL block

Run PIL simulation

1. Click "Run" button in the toolbar.

2. Double-click "View Scopes", observe and compare the simulation and processor implementation results. The difference should be equal to zero.

3. Stop the simulation.

NOTE: You can save this model by renaming it and saving it to a writable location.

Task 3 - Generate and Profile Code

After verifying the processor implementation results, the next step is to profile the generated code to look for run-time performance improvements.

This task guides you through configuring the model to use the Real-Time Execution Profiler. The profiler generates a graphical display that shows when tasks are activated, preempted, resumed, and completed. In addition, the profiler generates an HTML report with statistical information on each synchronous task.

NOTE: Real-Time Execution Profiler uses the on-chip general purpose timer. The timer might not be available on some processor chip simulators, so you cannot use this feature on those simulators. If you can use a simulator, the model needs to run for a sufficient amount of time to ensure valid profiling results.

The following figure shows a Simulink model with additional blocks added to facilitate profiling real-time execution of the generated code.

Configure the model to perform real-time task execution profiling

In the following steps, you will configure the model to generate code that is instrumented to store real-time task profiling statistics that can be analyzed later.

1. Open the Profiling ModelProfiling Model.

2. Open the Model Configuration Parameters dialog, set the "System target parameter" under "Code Generation" to either "idelink_ert.tlc" or "idelink_grt.tlc" and click "Apply".

3. Next, select "Coder Target" under "Code Generation". Click "Target Hardware Resources" and set the parameters to match your target hardware. Click "OK".

For more information on configuring the model for your target hardware see this sectionthis section in the IDE Link documentation.

3. Under "Tool Chain Automation", check "Profile real-time execution" option. Make sure "Profiled by" is set to "Tasks".

4. Click "OK".

Generate HTML report and graphical display of profiling statistics

1. In your model, click "Build Model" button to generate, build, load, and execute code on the processor.

2. Let the application run for a few seconds, and then halt the processor by double-clicking the 'Halt Processor' button in the model.

Note: If you are running on a simulator, allow the application to run longer, as much as 10 minutes, to collect the necessary profiling samples.

3. View profiling results by clicking the "View Profiling Results" button in the model.

4. Note the "Average execution time" of the model.

NOTE: You can save this model by renaming it and saving it to a writable location.

Task 4 - Optimize Code Using Compiler Options

After profiling the generated code, the next step is to apply compiler-specific optimizations. In this task, you will apply compiler-level optimizations by specifying a compiler optimization switch calling for Function-level optimization. You will again use the Real-Time Execution Profiler used in previous task to assess the change in the real-time execution performance of the generated code.

The following figure shows the Simulink model that is set for profiling after you turn on compiler optimizations.

Turn on compiler optimizations

1. Open the Optimization ModelOptimization Model.

2. Open the Model Configuration Parameters dialog and select "Coder Target" under "Code Generation". Click "Target Hardware Resources" and verify that the parameters settings match your processor.

3. In your model, select "Simulation" > "Model Configuration Parameters".

4. Under "Code Generation" > "Coder Target" > "Tool Chain Automation", enter -o2 in the "Compiler options string" edit box to apply Function optimizations.

5. Click "OK" to save the configuration and close the dialog box.

Generate HTML report and graphical display of profiling statistics

1. In your model, click the "Build Model" button to generate, build, load, and execute code on the processor.

2. After a while, halt the process by clicking the "Halt Processor" button in the model.

3. View profiling results by clicking the "View Profiling Results" button in the model.

4. Note the "Average execution time" and compare it with the one obtained in the previous task.

NOTE: You can save this model by renaming it and saving it to a writable location.

Task 5 - Optimize Code Using Processor-Specific Intrinsics

After verifying and profiling the generated code, the next step is to generate the production code that uses compiler-specific intrinsics to improve the performance of the generated code even further. This is accomplished by automatically replacing standard C constructs for certain numerical operations with the ones using processor and compiler-specific instructions. You will use "Code Replacement Library" feature to specify the replacement scheme that matches your processor family.

The following figure shows the Simulink model that is set for profiling after you turn on CRL optimizations.

Configure and build the model to use processor-specific intrinsics

1. Open the Processor-Specific ModelProcessor-Specific Model.

2. Open the Model Configuration Parameters dialog and select "Coder Target" under "Code Generation". Click "Target Hardware Resources" and verify that the parameters settings match your processor.

3. Under "Code Generation" > "Interface", select the "Code Replacement Library" that matches your processor family.

4. Click OK.

Since you are using the same model is in previous step, it is already configured to store real-time task profiling statistics.

Generate HTML report and graphical display of profiling statistics

1. In your model, click "Build Model" button to generate, build, load, and execute code on the processor.

2. After a while, halt the process by clicking the "Halt Processor" button in the model.

3. View profiling results by clicking the "View Profiling Results" button in the model.

4. Note the "Average execution time" and compare it with the one obtained in the previous task.

NOTE: You can save this model by renaming it and saving it to a writable location.

Task 6 - Profile Stack Usage

To further optimize the generated code, you can run the stack profiler to determine your application's stack usage. This is useful for determining the optimum stack size your application needs.

For this task, you will use the handle CCS_Obj saved in the base MATLAB workspace, and the CCS program created from the previous task.

1. Load the CCS program generated from the previous task on the processor.

2. Set up the stack profiler by entering the following command on the MATLAB prompt:

profile(CCS_Obj,'stack','setup')

This fills the processor's stack with a known repeating value, and displays the initial stack usage which is 0%.

3. Run the application. Wait for the application to end, or if it runs indefinitely, stop it after a considerable time has passed.

4. Get a profile of the stack usage by entering the following command on the MATLAB prompt:

profile(CCS_Obj,'stack','report')

Given the results, you either decrease or increase the size you allocated for the stack. You can update the stack size by going to "Simulation" > "Model Configuration Parameters" > Code Generation" > "Coder Target" > "Tool Chain Automation". Adjust the value written under System stack size (MAUs).

Task 7 - Generate Code for Production

After verifying and profiling the generated code, the next step is to generate the production code that does not have any instrumented code for verification and profiling.

Configure and build the model

1. Open the CodeGen ModelCodeGen Model.

2. Open the Model Configuration Parameters dialog and select "Coder Target" under "Code Generation". Click "Target Hardware Resources" and verify that the parameters settings match your processor.

3. Under "Code Generation" > "Interface", select the "Code Replacement Library" that matches your processor family. Click OK.

4. In your model, click "Build Model" button to build the model.

NOTE: You may save this model by renaming it and saving it to a writable location.

Task 8 - Test Final Implementation

After generating the code for production, you can the use "Automation Interface" debug methods to create test-benches for testing the final implementation.

Run testbench script

1. Click "Test Final Implementation" button in the model to execute the script.

2. Look at the result printed at the MATLAB prompt. The message should tell you whether or not the simulation and final implementations are the same.

NOTE: To view the script, click on "View Script" button in the model.

Summary

This example showed a workflow to generate, verify, profile and optimize code for production. It used Processor-in-the-Loop, Real-Time Execution Profiler, Code Replacement Library and Stack Profiler features of IDE Link component to accomplish these tasks. Repeat this workflow to achieve the desired results and the level of performance you seek.

Was this topic helpful?