This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[FAQ] FAQ: How do I use Simulink “Custom Code” blocks to measure code execution time on TI C2000?

MathWorks' Embedded Coder with the MathWorks TI C2000 Hardware Support Package allows automatic build and execution on C2000 hardware from models developed using MATLAB and Simulink. 

Simulink Coder’s Custom Code blocks are a key method to call custom code. Custom code blocks can be found under the Simulink Coder library. Start by navigating to the Simulink library by typing the command “slLibraryBrowser” at the MATLAB command prompt and locate the blocks as shown below. 

Suppose we want to measure the time taken to execute a block of code on F2837x device. We can configure and use the CPU timer to get the timing information. We read the free running timer value before code execution and read back timer value at the end. The difference of the timer values gives code execution time. 

We are assuming the algorithm whose execution time is to be computed is inside an atomic subsystem. When using custom code block with subsystems, it is important to note that the custom code blocks honor the boundary of a subsystem only if it is atomic. 

1. Add System Initialize block 

  • Begin with System Initialize block to Initialize, configure and start the CPU timer 2. 

Note: Timer0 is used for scheduling and Timer1 is used for profiling in C2000 HW support package. 

  • Add the block to the model and double click on the block to open the block mask. 

  • ConfigCpuTimer() function is used to configure CPU timers. 

  • 200MHz is the frequency at which CPUTimer counts and 10000 is the timer period in uSeconds. 

2. Add Model Header block 

  • Use Model Header block to extern variables in model header file. 

 

3. Add Model Source block 

  • Use Model Source block to define variables needed to store timer values. 

4. Add Subsystem block containing the algorithm 

  • As noted before make the subsystem as atomic. 

5. Add System Outputs block 

  • Add this block inside the atomic subsystem containing the algorithm to read the timer value before code execution. 

  • We also can read the timer value after code execution from the same block in the exit code and compute the difference. 

  • The computed difference can be sent over a serial link to host using the memory copy block.