CPU Architecture Counter API
Module: CPU Architecture API
Functions
Name |
|
|---|---|
void |
Arch_Counter_Init(void ) |
void |
Arch_Counter_Reset(void ) |
uint32_t |
Arch_Counter_Read32(void ) |
uint64_t |
Arch_Counter_Read64(void ) |
LOCAL_INLINE uint32_t |
Arch_Counter_CalcDiff32(uint32_t startCycles, uint32_t endCycles) |
LOCAL_INLINE uint32_t |
Arch_Counter_UsecToCycles(uint32_t usec) |
LOCAL_INLINE uint32_t |
Arch_Counter_MsecToCycles(uint32_t msec) |
LOCAL_INLINE uint32_t |
Arch_Counter_CyclesToNsec(uint32_t cycles) |
LOCAL_INLINE uint32_t |
Arch_Counter_CyclesToUsec(uint32_t cycles) |
LOCAL_INLINE uint32_t |
Arch_Counter_CyclesToMsec(uint32_t cycles) |
Detailed Description
This includes APIs for initializing, reading, and managing a CPU cycle counter. The cycle counter is typically used for timing and profiling purposes.
Notes,
Arch_Counter_Init must be called once before using other APIs in this module. This enables and start the CPU cycle counter (free-running).
The cycle counter is a free-running counter that increments with each CPU clock cycle.
The counter is typically a 32-bit register that wraps around to zero upon reaching its maximum value.
For a CPU running at 200MHz, the 32-bit counter will overflow in about 21.5 seconds, whereas for a CPU running at 1GHz, it will overflow in about 4.3 seconds.
This should be good enough for most timing and profiling use cases.
Arch_Counter_Read32 reads the current value of the cycle counter.
Arch_Counter_CalcDiff32 computes the difference between two counter values, correctly handling counter rollover.
For most use cases, simply reading the counter with Arch_Counter_Read32 and using the utility function Arch_Counter_CalcDiff32 is recommended, as it correctly handles a single overflow. Reset API Arch_Counter_Reset should be used only if absolutely necessary.
For examples, a typical profiling use-case would look like:-
uint32_t startCycles, endCycles, diffCycles;
Arch_Counter_Init(); // Call once at the start of application, typically part of Device_init()
startCycles = Arch_Counter_Read32();
do_something(); // Code section to be profiled
endCycles = Arch_Counter_Read32();
diffCycles = Arch_Counter_CalcDiff32(startCycles, endCycles);
printf("do_something() took %u cycles\n", diffCycles);
CPU architecture specific notes,
C29,
Uses Embedded Real-time Analysis and Diagnostic (ERAD) - System Event Counter (SEC).
Arch_Counter_Init claims SEC unit ownership and enables the cycle counter in free-running mode.
Arch_Counter_Read64 is not supported.
R52,
Uses Performance Monitor Unit (PMU) - Cycle Counter
Arch_Counter_Init enables user mode access to PMU registers, enables the PMU and the cycle counter.
Following APIs should be used only from a privileged mode. This is due to access restrictions in User mode to read/modify various system registers.
M33,
Uses Data Watchpoint and Trace (DWT) unit - Cycle Count.
Arch_Counter_Init enables trace (global enable for DWT) and the DWT cycle counter.
Arch_Counter_Read64 is not supported.
Usage of the APIs will cause linking errors.
All APIs should be used only from privileged mode.
This is due to DWT and CoreDebug registers usage constraints - Privileged access only.
Functions Documentation
function Arch_Counter_Init
void Arch_Counter_Init(
void
)
Initialize the cycle counter.
Note: Some CPUs will have access restrictions with the execution mode. Refer CPU specific notes for details.
function Arch_Counter_Reset
void Arch_Counter_Reset(
void
)
Reset the cycle counter value.
Note: Some CPUs will have access restrictions with the execution mode. Refer CPU specific notes for details.
function Arch_Counter_Read32
uint32_t Arch_Counter_Read32(
void
)
Read the current cycle counter value.
Return: Current cycle count
Note: Some CPUs will have access restrictions with the execution mode. Refer CPU specific notes for details.
Precondition: Arch_Counter_Init must be called before this API.
function Arch_Counter_Read64
uint64_t Arch_Counter_Read64(
void
)
Read the 64-bit current cycle counter value.
Return: Current cycle count
Note: Some CPUs may not support this. Refer CPU specific notes for details.
Precondition: Arch_Counter_Init must be called before this API.
function Arch_Counter_CalcDiff32
LOCAL_INLINE uint32_t Arch_Counter_CalcDiff32(
uint32_t startCycles,
uint32_t endCycles
)
Calculate the difference between two counter values.
Parameters:
startCycles Start counter value
endCycles End counter value
Return: Difference between start and end counter values
This computes difference between two counter values, handling counter rollover.
function Arch_Counter_UsecToCycles
LOCAL_INLINE uint32_t Arch_Counter_UsecToCycles(
uint32_t usec
)
Convert microseconds to CPU cycles.
Parameters:
usec Time value in microseconds
Return: Number of CPU cycles equivalent to the given microseconds. Returns 0 if ARCH_CPU_CFG_FREQ_HZ is not defined.
This function converts a time value in microseconds to the equivalent number of CPU cycles based on the CPU frequency value set in arch_cpu_cfg.h.
function Arch_Counter_MsecToCycles
LOCAL_INLINE uint32_t Arch_Counter_MsecToCycles(
uint32_t msec
)
Convert milliseconds to CPU cycles.
Parameters:
msec Time value in milliseconds
Return: Number of CPU cycles equivalent to the given milliseconds. Returns 0 if ARCH_CPU_CFG_FREQ_HZ is not defined.
This function converts a time value in milliseconds to the equivalent number of CPU cycles based on the CPU frequency value set in arch_cpu_cfg.h.
function Arch_Counter_CyclesToNsec
LOCAL_INLINE uint32_t Arch_Counter_CyclesToNsec(
uint32_t cycles
)
Convert CPU cycles to nanoseconds.
Parameters:
cycles CPU cycle count
Return: Time in nanoseconds equivalent to the given number of CPU cycles.
This function converts a cycle count to the equivalent time in nanoseconds based on the CPU frequency value set in arch_cpu_cfg.h.
function Arch_Counter_CyclesToUsec
LOCAL_INLINE uint32_t Arch_Counter_CyclesToUsec(
uint32_t cycles
)
Convert CPU cycles to microseconds.
Parameters:
cycles CPU cycle count
Return: Time in microseconds equivalent to the given number of CPU cycles.
This function converts a cycle count to the equivalent time in microseconds based on the CPU frequency value set in arch_cpu_cfg.h.
function Arch_Counter_CyclesToMsec
LOCAL_INLINE uint32_t Arch_Counter_CyclesToMsec(
uint32_t cycles
)
Convert CPU cycles to milliseconds.
Parameters:
cycles CPU cycle count
Return: Time in milliseconds equivalent to the given number of CPU cycles.
This function converts a cycle count to the equivalent time in milliseconds based on the CPU frequency value set in arch_cpu_cfg.h.