Measurement of Software Execution Time in Traveo II Family – KBA230552
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Author: ShusakuS_56 Version: **
Question:
I want to know the execution time of some software. Is there a method to measure the execution time without using a debugger?
Answer:
You can measure the approximate software execution time using the hardware timer (TCPWM). Here is the code snippet for "Hello World".
Code 1. Original “Hello World”
#include <stdio.h> int main() { printf("Hello World"); return 0; } |
To measure software execution time, make the following modifications to the “Hello World” code:
1. Add the TCPWM timer start command and the TCPWM timer stop command before and after printf ("Hello World"); to the ”.
Code 2. Adding TCPWM Timer Start and Timer Stop Commands
#include <stdio.h> int main() { // Initialize Clock Divider and TCPWM (For more details, please see Technical Reference Manual) : // If you need, please Enter Critical section to avoid any interrupt during measurement. uint32_t saveIntrEnabled = Cy_SysLib_EnterCriticalSection(); // Interrupt Disable & Save Current Status : // Timer Stop/Clear/Start (Please use INLINE function to reduce software offset like below) TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1STOP = 0x01; // Timer Stop Command (If Timer is Starting) TCPWM0_GRPx_CNTx_COUNTER->unCOUNTER.u32Register = 0UL; // Timer Counter Clear Command TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1START = 0x01; // Timer Start Command printf("Hello World"); // Timer Stop (Please use INLINE function to reduce software offset like below) TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1STOP = 0x01; // Timer Stop Command // Read and Save the counter value uint32_t TimerCount = Cy_Tcpwm_Counter_GetCounter(TCPWM0_GRPx_CNTx_COUNTER); : // If you need, restore interrupt status Cy_SysLib_ExitCriticalSection(saveIntrEnabled); // Interrupt Restore Previous Status return 0; } |
2. Then, add a command to read the timer value of the corresponding TCPWM. This timer value counts the number of clocks. Therefore, the software execution time can be calculated as:
[Example] Clock Frequency = 20 MHz, Value of TimerCount = 0xF000 (0d61440)
To make the measurement time more accurate, take the following measures.
- Increase clock speed.
- Measure the time from starting the counter to stopping it. Then, subtract from the count value. Here is an example:
#include <stdio.h> int main() { :
// Timer Stop/Clear/Start (Please use INLINE function to reduce software offset like below) TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1STOP = 0x01; // Timer Stop Command (If Timer is Starting) TCPWM0_GRPx_CNTx_COUNTER->unCOUNTER.u32Register = 0UL; // Timer Counter Clear Command TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1START = 0x01; // Timer Start Command printf("Hello World"); // Timer Stop (Please use INLINE function to reduce software offset like below) TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1STOP = 0x01; // Timer Stop Command // Read and Save the counter value uint32_t TimerCount = Cy_Tcpwm_Counter_GetCounter(TCPWM0_GRPx_CNTx_COUNTER); : // Measure the time from starting the counter to stopping TCPWM0_GRPx_CNTx_COUNTER->unCOUNTER.u32Register = 0UL; // Timer Counter Clear Command TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1START = 0x01; // Timer Start Command TCPWM0_GRPx_CNTx_COUNTER->unTR_CMD.stcField.u1STOP = 0x01; // Timer Stop Command uint32_t SwOffsetCount = Cy_Tcpwm_Counter_GetCounter(TCPWM0_GRPx_CNTx_COUNTER); uint32_t TimerCountDuringHelloWorld = TimerCount – SwOffsetCount; return 0; } |
Note: This KBA applies to the following series of Traveo II MCUs:
- CYT2B Series
- CYT4B Series
- CYT4D Series