AM263x MCU+ SDK  08.02.01
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;
}
else
{
cpuCycles = (0xFFFFFFFFU - cycleCountBefore) + cycleCountAfter;
}

API

APIs for Counting CPU Cycles

CycleCounterP_getCount32
uint32_t CycleCounterP_getCount32()
Get 32b CPU cycle counter value.
CycleCounterP.h
CycleCounterP_reset
void CycleCounterP_reset()
Enable, reset, clear overflow for CPU cycle counter.