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#
Functions#
Enable cycle counter.
Pause cycle counter.
Resume cycle counter.
Initialize the cycle counter handle.
Start recording.
Stop recording.
Reset the cycle counter handle.
Read the last recording duration.
Read the max recording duration.
Read the current value of the cycle counter.
Function Documentation#
sl_cycle_counter_enable#
void sl_cycle_counter_enable (void )
Enable cycle counter.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
sl_cycle_counter_pause#
void sl_cycle_counter_pause (void )
Pause cycle counter.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
this function pauses the DWT cycle counter
sl_cycle_counter_resume#
void sl_cycle_counter_resume (void )
Resume cycle counter.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
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.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer 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.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer to initialized counter handle. |
Start recording.
sl_cycle_counter_stop#
void sl_cycle_counter_stop (sl_cycle_counter_handle_t * handle)
Stop recording.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer 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.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer 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.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer 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.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_cycle_counter_handle_t * | N/A | handle | Pointer 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 )
Read the current value of the cycle counter.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
cycle counter value.