Cycle Counter

Description

Provides an interface to the cycle counter of the DWT unit.

Initialization

The cycle counter must be enabled prior to any call to its API. If sl_system is used, only sl_system_init() must be called, otherwise sl_cycle_counter_enable() must be called manually.

Limitations

The cycle counter is stored in an unsigned 32-bit integer. As the counter frequently rolls over, timing durations should be kept short.

Usage Notes

When entering sleep in Energy Mode 1 (EM1), the DWT cycle counter continues to increment. The cycle counter is used by the emlib CORE component to measure the interrupt maximum disable time, when this feature is enabled. Since the power manager must enter sleep with PRIMASK set to 1, it will pause the cycle counter before entering EM1 and resume it on wakeup, in order to provide a realistic interrupt disable time. If this behaviour is not desired, the maximum interrupt disable feature must be disabled in the CORE component.

Usage example

#include "sl_cycle_counter.h"
void main(void)
{
// Enable the cycle counter; not needed if sl_system_init() is used.
status = sl_cycle_counter_init(&counter);
if (status != SL_STATUS_OK) {
// handle error
}
function_to_measure();
printf("Timing: %lu\r\n", sl_cycle_counter_get_cycles(&counter));
}

Data Structures

struct  sl_cycle_counter_handle_t
 A Cycle Counter Instance.
 

Functions

void sl_cycle_counter_enable (void)
 Enable cycle counter.
 
void sl_cycle_counter_pause (void)
 Pause cycle counter.
 
void sl_cycle_counter_resume (void)
 Resume cycle counter.
 
sl_status_t sl_cycle_counter_init (sl_cycle_counter_handle_t *handle)
 Initialize the cycle counter handle.
 
void sl_cycle_counter_start (sl_cycle_counter_handle_t *handle)
 Start recording.
 
void sl_cycle_counter_stop (sl_cycle_counter_handle_t *handle)
 Stop recording.
 
sl_status_t sl_cycle_counter_reset (sl_cycle_counter_handle_t *handle)
 Reset the cycle counter handle.
 
uint32_t sl_cycle_counter_get_cycles (sl_cycle_counter_handle_t *handle)
 Read the last recording duration.
 
uint32_t sl_cycle_counter_get_max (sl_cycle_counter_handle_t *handle)
 Read the max recording duration.
 
uint32_t sl_cycle_counter_get_counter (void)
 Read the current value of the cycle counter.
 

Function Documentation

◆ sl_cycle_counter_enable()

void sl_cycle_counter_enable ( void  )

Enable cycle counter.

◆ sl_cycle_counter_pause()

void sl_cycle_counter_pause ( void  )
inline

Pause cycle counter.

Note
this function pauses the DWT cycle counter

◆ sl_cycle_counter_resume()

void sl_cycle_counter_resume ( void  )
inline

Resume cycle counter.

Note
this function resumes the DWT cycle counter

◆ sl_cycle_counter_init()

sl_status_t sl_cycle_counter_init ( sl_cycle_counter_handle_t handle)

Initialize the cycle counter handle.

Parameters
handlePointer to counter handle to initialize.
Returns
SL_STATUS_OK if successful. Error code otherwise.

Initialize the cycle counter handle.

◆ sl_cycle_counter_start()

void sl_cycle_counter_start ( sl_cycle_counter_handle_t handle)

Start recording.

Parameters
handlePointer to initialized counter handle.

Start recording.

◆ sl_cycle_counter_stop()

void sl_cycle_counter_stop ( sl_cycle_counter_handle_t handle)

Stop recording.

Parameters
handlePointer to initialized counter handle.

Stop recording.

◆ sl_cycle_counter_reset()

sl_status_t sl_cycle_counter_reset ( sl_cycle_counter_handle_t handle)

Reset the cycle counter handle.

Parameters
handlePointer to initialized counter handle.
Returns
SL_STATUS_OK if successful. Error code otherwise.

Reset the cycle counter handle.

◆ sl_cycle_counter_get_cycles()

uint32_t sl_cycle_counter_get_cycles ( sl_cycle_counter_handle_t handle)

Read the last recording duration.

Parameters
handlePointer to initialized counter handle.
Returns
cycles elapsed in last recording.

Read the last recording duration.

◆ sl_cycle_counter_get_max()

uint32_t sl_cycle_counter_get_max ( sl_cycle_counter_handle_t handle)

Read the max recording duration.

Parameters
handlePointer to initialized counter handle.
Returns
max cycles elapsed in recordings.

Read the max recording duration.

◆ sl_cycle_counter_get_counter()

uint32_t sl_cycle_counter_get_counter ( void  )
inline

Read the current value of the cycle counter.

Returns
cycle counter value.