AM62Ax MCU+ SDK  09.00.00
Cycle Counter

Features Supported

  • API to return CPU cycles executed so far
  • API to reset the CPU cycle counter

Features NOT Supported

  • Additional event counters in the CPU are not supported via this API. Refer to CPU architecture specific technical and architecture reference manuals, to access additional CPU architecture specific counter features.

Important Usage Guidelines

  • Make sure to call CycleCounterP_reset() to enable and reset the CPU cycle counter before using it
  • CycleCounterP_getCount32() uses a CPU architecture specific counter to count every CPU cycle. Since this is a 32b counter, this will typically wraparound and overflow within few seconds. E.g. for 500Mhz CPU, the counter will overflow in ~ 8secs.
  • Application can have logic to handle one wraparound of the counter, as shown in the example usage below. i.e max time duration that can be measured will be limited to 0xFFFFFFFF CPU cycles, i.e few seconds only.
  • This API is meant to used for very fine, short duration measurements. To measure longer durations use ClockP_getTimeUsec() from Clock module.

Example Usage

Include the below file to access the APIs,

Example usage for counting CPU cycles,

uint32_t cycleCountBefore, cycleCountAfter, cpuCycles;
/* enable and reset CPU cycle coutner */
cycleCountBefore = CycleCounterP_getCount32();
/* call functions to profile */
cycleCountAfter = CycleCounterP_getCount32();
/* Check for overflow and wrap around.
* This logic will only work for one overflow.
* If multiple overflows happen during the profile period,
* then CPU cycles count will be wrong,
if(cycleCountAfter > cycleCountBefore)
cpuCycles = cycleCountAfter - cycleCountBefore;
cpuCycles = (0xFFFFFFFFU - cycleCountBefore) + cycleCountAfter;
DebugP_log("CPU cycles:%u\n", cpuCycles);


APIs for Counting CPU Cycles

#define DebugP_log(format,...)
Function to log a string to the enabled console.
Definition: DebugP.h:227
void CycleCounterP_reset(void)
Enable, reset, clear overflow for CPU cycle counter.
uint32_t CycleCounterP_getCount32(void)
Get 32b CPU cycle counter value.