How do I use Simulink “Custom Code” blocks to measure code execution time on TI C2000?
37 views (last 30 days)
Edited: MathWorks Support Team on 2 Dec 2020
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 Browser by pressing the button, or by typing the command “slLibraryBrowser” at the MATLAB command prompt. 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, as shown in the screenshot below. The 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 specify 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, mark 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.