Cycle Counter#

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)
{
  sl_cycle_counter_handle_t counter;

  // Enable the cycle counter; not needed if sl_system_init() is used.
  sl_cycle_counter_enable();

  status = sl_cycle_counter_init(&counter);
  if (status != SL_STATUS_OK) {
     // handle error
  }

  sl_cycle_counter_start(&counter);
  function_to_measure();
  sl_cycle_counter_stop(&counter);
  printf("Timing: %lu\r\n", sl_cycle_counter_get_cycles(&counter));

}

Modules#

sl_cycle_counter_handle_t

Functions#

void

Enable cycle counter.

void

Pause cycle counter.

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

Read the current value of the cycle counter.

Function Documentation#

sl_cycle_counter_enable#

void sl_cycle_counter_enable (void )

Enable cycle counter.

Parameters
N/A

Definition at line 115 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_pause#

void sl_cycle_counter_pause (void )

Pause cycle counter.

Parameters
N/A

Note

  • this function pauses the DWT cycle counter


Definition at line 122 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_resume#

void sl_cycle_counter_resume (void )

Resume cycle counter.

Parameters
N/A

Note

  • this function resumes the DWT cycle counter


Definition at line 132 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_init#

sl_status_t sl_cycle_counter_init (sl_cycle_counter_handle_t * handle)

Initialize the cycle counter handle.

Parameters
N/Ahandle

Pointer to counter handle to initialize.

Returns

  • SL_STATUS_OK if successful. Error code otherwise.

Initialize the cycle counter handle.


Definition at line 144 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_start#

void sl_cycle_counter_start (sl_cycle_counter_handle_t * handle)

Start recording.

Parameters
N/Ahandle

Pointer to initialized counter handle.

Start recording.


Definition at line 152 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_stop#

void sl_cycle_counter_stop (sl_cycle_counter_handle_t * handle)

Stop recording.

Parameters
N/Ahandle

Pointer to initialized counter handle.

Stop recording.


Definition at line 160 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_reset#

sl_status_t sl_cycle_counter_reset (sl_cycle_counter_handle_t * handle)

Reset the cycle counter handle.

Parameters
N/Ahandle

Pointer to initialized counter handle.

Returns

  • SL_STATUS_OK if successful. Error code otherwise.

Reset the cycle counter handle.


Definition at line 169 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_get_cycles#

uint32_t sl_cycle_counter_get_cycles (sl_cycle_counter_handle_t * handle)

Read the last recording duration.

Parameters
N/Ahandle

Pointer to initialized counter handle.

Returns

  • cycles elapsed in last recording.

Read the last recording duration.


Definition at line 178 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_get_max#

uint32_t sl_cycle_counter_get_max (sl_cycle_counter_handle_t * handle)

Read the max recording duration.

Parameters
N/Ahandle

Pointer to initialized counter handle.

Returns

  • max cycles elapsed in recordings.

Read the max recording duration.


Definition at line 187 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h

sl_cycle_counter_get_counter#

uint32_t sl_cycle_counter_get_counter (void )

Read the current value of the cycle counter.

Parameters
N/A

Returns

  • cycle counter value.


Definition at line 194 of file platform/driver/cycle_counter/inc/sl_cycle_counter.h