AM261x MCU+ SDK: Cycle Counter

| AM261x MCU+ SDK 26.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,

#include <kernel/dpl/CycleCounterP.h>
#include <kernel/dpl/DebugP.h>

Example usage for counting CPU cycles,

uint32_t cycleCountBefore, cycleCountAfter, cpuCycles;
/* enable and reset CPU cycle coutner */
CycleCounterP_reset();
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;
}
DebugP_log("CPU cycles:%u\n", cpuCycles);

# API

APIs for Counting CPU Cycles

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