Main Content

Code Execution Profiling for Arduino Hardware in External Mode

This example shows how to use the Simulink® Support Package for Arduino® Hardware to profile the real-time execution of generated code running as an executable on ARM® architecture-based Arduino hardware boards with XCP communication interface in External mode.

Introduction

Sample times specified in the Simulink models determine the time schedule for running generated code on target hardware. With enough computing power on the hardware, the code runs in real-time according to the specified sample times. With real-time execution profiling, you can check if the generated code meets your real-time performance requirements. This support package supports the code execution profiling on the following ARM architecture-based Arduino hardware boards:

  • Arduino MKR Wifi 1010

  • Arduino MKR 1000

  • Arduino MKR Zero

  • Arduino Nano 33 BLE Sense

  • Arduino Nano 33 IoT

  • Arduino Robot Control Board

  • Arduino Robot Motor Board

  • Arduino Due

  • Arduino Uno

  • Arduino Mega ADK

  • Arduino Mega 2560

  • Arduino Leonardo

At the end of the Simulink model code profile execution, you can:

  • View a report of code execution times.

  • Access and analyze execution time profiling data.

Profile Task Execution

This task shows you how to configure an Arduino Simulink model to profile its code execution.

1. Open any Arduino Simulink model. In this example, we are using the arduino_gettingstarted Simulink model.

For more information on the arduino_gettingstarted Simulink model, see Get Started with Arduino Hardware example.

3. On the Hardware Implementation tab, select the ARM architecture-based Arduino board from the Hardware board drop-down menu.

4. Go to Advanced Parameters and select Use Embedded Coder features.

5. Go to Code Generation > Verification on the left pane. If this option does not appear on the left pane, click Apply > OK on the Configuration Parameters dialog box and close it. Open the dialog box and continue with the steps mentioned below.

6. Select Measure task execution time to profile execution time for each rate in the Simulink model. The Workspace variable, executionProfile, holds the profiling data in the workspace.

7. Under Save Options, you can select any one of these options.

a. Metrics only - Use this option to reduces bandwidth usage for the communication channel between Simulink and the target application. During the simulation, the software does not stream execution times to the Simulation Data Inspector. On the target hardware, the software stores maximum execution times, average execution times, and number of calls. At the end of the simulation, Simulink uploads the data from the target hardware to your development computer.

b. Summary only data - Use this option to save only code profiling summary data in the base workspace.

c. All data - Use this option to save the code profiling measurement and analysis data in the base workspace. This option also enables the streaming of execution times to Simulation Data Inspector during simulations.

Note: Arduino hardware boards with SAM core architecture supports task execution measurement with the Metrics Only saving option.

8. Click Apply and OK.

9. On the Simulation tab of the Simulink Toolstrip, set the Stop time to 5 seconds.

10. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune to build, deploy, and run your model as an executable on the ARM Arduino board. At the start of execution, a window appears and shows that the executable is running on the ARM Arduino board. The code runs for 5 seconds and then stops.

11. Enter this code to obtain the profiling report for the session that you just ran. This report shows each task has a different expected turnaround and execution time.

executionProfile.report

Code Execution Profiling Report Details

This report provides the following details:

1. A detailed summary

2. Information about profiled code sections, which includes time measurements for:

  • The entry_point_fn_initialize function, for example, arduino_gettingstarted_initialize.

  • The entry point function, for example, arduino_gettingstarted_step[0.10].

  • The entry_point_fn_terminate function, for example, arduino_gettingstarted_terminate.

3. Average and maximum Arduino CPU core utilization.

4. Definition of metrics.

The report has these sections:

1. Section: Name of function from which code is generated.

2. Maximum Execution Time in ns: Longest time between start and end of code section.

3. Average Execution Time in ns: Average time between start and end of code section.

4. Calls: Number of calls to the code section.

5. MATLAB icon: Icon that you click to display the profiled code section.

Other Things to Try

Profile other Simulink models from the Simulink Support Package for Arduino Hardware. Observe the time taken for implementing steps in the Simulink model that helps to improve its efficiency.