Measurement of software execution time in XMC7000 family- KBA234432
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Community Translation: XMC7000ファミリにおけるソフトウェア実行時間を測定 - KBA234432
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:
- Add the TCPWM timer start command and the TCPWM timer stop command before and after printf ("Hello World").
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) Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Stop Command (If Timer is Starting) Cy_TCPWM_PWM_SetCounter(TCPWM0, MY_TCPWM_CNT_NUM,0); // Timer Counter Clear Command Cy_TCPWM_TriggerStart_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Start Command printf("Hello World"); // Timer Stop (Please use INLINE function to reduce software offset like below) Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Stop Command // Read and Save the counter value uint32_t TimerCount = Cy_TCPWM_PWM_GetCounter(TCPWM0, MY_TCPWM_CNT_NUM); : // 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 = 25 MHz, Value of TimerCount = 0x0F98 (0d3992)
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)
Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Stop Command (If Timer is Starting)
Cy_TCPWM_PWM_SetCounter(TCPWM0, MY_TCPWM_CNT_NUM,0); // Timer Counter Clear Command
Cy_TCPWM_TriggerStart_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Start Command
printf("Hello World");
// Timer Stop (Please use INLINE function to reduce software offset like below)
Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Stop Command
// Read and Save the counter value
uint32_t TimerCount = Cy_TCPWM_PWM_GetCounter(TCPWM0, MY_TCPWM_CNT_NUM);
:
// Measure the time from starting the counter to stopping
Cy_TCPWM_TriggerStopOrKill_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Stop Command (If Timer is Starting)
Cy_TCPWM_PWM_SetCounter(TCPWM0, MY_TCPWM_CNT_NUM,0); // Timer Counter Clear Command
Cy_TCPWM_TriggerStart_Single(TCPWM0, MY_TCPWM_CNT_NUM); // Timer Start Command
uint32_t SwOffsetCount = Cy_TCPWM_PWM_GetCounter(TCPWM0, MY_TCPWM_CNT_NUM);
uint32_t TimerCountDuringHelloWorld = TimerCount – SwOffsetCount;
return 0;
}