nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions
nrfcxx::periph::TIMER Class Reference

Wrapper around the nRF5 TIMER peripheral. More...

#include <nrfcxx/periph.hpp>

Data Structures

class  timestamp_type
 Class supporting (high-resolution) timing. More...
 

Public Member Functions

int configure (unsigned int freq_Hz, unsigned int bitmode=TIMER_BITMODE_BITMODE_16Bit, bool use_constlat=true)
 Configure the TIMER to run as a timer. More...
 
void deconfigure ()
 Deconfigure the TIMER.
 
unsigned int frequency_Hz () const
 Get the configured clock frequency in Hz.
 
void start ()
 Invoke TASKS_START.
 
void clear ()
 Invoke TASKS_CLEAR.
 
void stop ()
 Invoke TASKS_STOP.
 
void shutdown ()
 Invoke TASKS_SHUTDOWN.
 
void capture (unsigned int ccidx) const
 Capture the current counter using CC ccidx. More...
 
unsigned int captured (unsigned int ccidx) const
 Return the captured counter in CC ccidx.
 
unsigned int counter (unsigned int ccidx=0) const
 Read the current timer value. More...
 
unsigned int delta (unsigned int a, unsigned int b) const
 Calculate the count distance from a to b. More...
 
timestamp_type timestamp (unsigned int ccidx=0) const
 Create a timestamp instance using this timer. More...
 
const nrf5::TIMER_Typeperipheral () const
 Reference the nRF5 TIMER peripheral instance used by the abstraction.
 
const unsigned int ccr_count () const
 The number of capture/compare registers on the device.
 

Static Public Member Functions

constexpr static bool bitmode_supported (const nrf5::TIMER_Type &timer, unsigned int bitmode)
 Verify a specific timer can support the requested bitmode. More...
 
static TIMERinstance (int idx)
 Reference the abstraction instance for a specific peripheral instance. More...
 

Static Public Attributes

constexpr static unsigned int cc_mask = 7
 Mask to create a valid CC index from a non-negative integer. More...
 

Protected Member Functions

constexpr TIMER (const nrf5::TIMER_Type &timer)
 
void capture_ (unsigned int ccidx) const
 Capture the current counter into CC ccidx (unvalidated).
 
unsigned int captured_ (unsigned int ccidx) const
 Return the captured value in CC ccidx (unvalidated).
 
unsigned int counter_ (unsigned int ccidx) const
 Capture and return the current counter using ccidx (unvalidated).
 

Detailed Description

Wrapper around the nRF5 TIMER peripheral.

This abstraction supports using specific timers in timer mode. Timer IRQ handler definitions and interrupt configuration, if necessary, are the responsibility of the application, as is allocation of capture/compare registers to specific roles.

Note
When a soft device is used TIMER0 is restricted to the soft device. Other TIMER instances are available to applications.

Member Function Documentation

◆ bitmode_supported()

constexpr static bool nrfcxx::periph::TIMER::bitmode_supported ( const nrf5::TIMER_Type timer,
unsigned int  bitmode 
)
inlinestaticconstexpr

Verify a specific timer can support the requested bitmode.

Note
The user is responsible for ensuring that timer references an existing nRF5x TIMER peripheral and that bitmode is a valid setting for the timer BITMODE register.

◆ capture()

void nrfcxx::periph::TIMER::capture ( unsigned int  ccidx) const
inline

Capture the current counter using CC ccidx.

Note
This function intentionally does not return the captured value, as reading it from the peripheral takes a couple clock cycles which can negatively impact instrumentation accuracy. If the overhead impact on subsequent operations is not a concern, use counter() to capture and return the current timer value.

◆ configure()

int nrfcxx::periph::TIMER::configure ( unsigned int  freq_Hz,
unsigned int  bitmode = TIMER_BITMODE_BITMODE_16Bit,
bool  use_constlat = true 
)

Configure the TIMER to run as a timer.

All interrupts are cleared and disabled, and the timer counter is cleared. Configuration is performed as specified by the parameters.

Parameters
freq_Hzthe desired frequency of the timer. The value is limited by clock::hfclk::Frequency_Hz, and the actual value is the largest power-of-2 divisor of that frequency that does not exceed freq_Hz. Zero may be passed to deconfigure the TIMER.
bitmodethe NRF_TIMER_Type::BITMODE.BITMODE setting controlling the range for timer counter values.
use_constlatpass true to ensure that NRF_POWER->TASKS_CONSTLAT is in force as long as the timer is configured; pass false if the timer can tolerate running in NRF_POWER->TASKS_LOWPWR mode. If freq_Hz is zero any previously-specified constant latency dependency is removed.
Returns
0 if configuration is successful, or -1 if a parameter is invalid.
Note
The timer must be started before it is useful. Startup time may be reduced if use_constlat is true.

◆ counter()

unsigned int nrfcxx::periph::TIMER::counter ( unsigned int  ccidx = 0) const
inline

Read the current timer value.

Since the timer counter is not exposed a capture/compare index must be provided. The caller is responsible for ensuring that ccidx is not used for other purposes that would be disrupted by invocations of counter().

Returns
the full-precision counter value.

◆ delta()

unsigned int nrfcxx::periph::TIMER::delta ( unsigned int  a,
unsigned int  b 
) const
inline

Calculate the count distance from a to b.

The value is truncated based on the bitmode passed to configure().

◆ instance()

static TIMER& nrfcxx::periph::TIMER::instance ( int  idx)
static

Reference the abstraction instance for a specific peripheral instance.

Parameters
idxthe peripheral instance desired.
Warning
If idx specifies a peripheral instance that does not exist on the device the system will reset into failsafe mode.

◆ timestamp()

timestamp_type nrfcxx::periph::TIMER::timestamp ( unsigned int  ccidx = 0) const
inline

Create a timestamp instance using this timer.

Parameters
ccidxthe capture/compare index to be used for counter captures.
Returns
a #timestamp_type object initialized to time activity relative to the current counter value.

Field Documentation

◆ cc_mask

constexpr static unsigned int nrfcxx::periph::TIMER::cc_mask = 7
staticconstexpr

Mask to create a valid CC index from a non-negative integer.

This allows up to 8 capture/compare registers. Not all registers are available on all timers.


The documentation for this class was generated from the following file: