nrfcxx
0.1.0
C++-17 Framework for Nordic nRF5 Devices
|
Go to the documentation of this file. 7 #ifndef NRFCXX_CLOCK_HPP 8 #define NRFCXX_CLOCK_HPP 70 return ((CLOCK_HFCLKSTAT_SRC_Xtal << CLOCK_HFCLKSTAT_SRC_Pos)
71 == (nrf5::CLOCK->HFCLKSTAT & CLOCK_HFCLKSTAT_SRC_Msk));
131 return ((CLOCK_LFCLKSTAT_STATE_Running << CLOCK_LFCLKSTAT_STATE_Pos)
132 == (nrf5::CLOCK->LFCLKSTAT & CLOCK_LFCLKSTAT_STATE_Msk));
160 return (nrf5::CLOCK->LFCLKSTAT & CLOCK_LFCLKSTAT_SRC_Msk) >> CLOCK_LFCLKSTAT_SRC_Pos;
259 using duration_type = std::chrono::duration<int64_t, std::ratio<1, Frequency_Hz>>;
294 bool with_msec =
false);
302 return nrf5::UPTIME_RTC->COUNTER;
313 constexpr
static unsigned int delta24 (
unsigned int a,
333 unsigned int capture_utt24;
337 capture_utt24{
now24()}
343 return capture_utt24;
349 capture_utt24 =
now24();
355 auto now_utt24 =
now24();
356 return delta24(capture_utt24, now_utt24);
360 unsigned int delta (
unsigned int tick)
const 362 return delta24(capture_utt24, tick);
369 auto now_utt24 =
now24();
370 auto rv =
delta24(capture_utt24, now_utt24);
371 capture_utt24 = now_utt24;
383 static uint64_t
now ();
386 template <
typename DurT>
393 template <
typename DurT>
396 return std::chrono::duration_cast<duration_type>(dur).count();
401 constexpr
static int64_t
to_us (int64_t utt)
403 return to_duration<std::chrono::microseconds>(utt).count();
415 constexpr
static int64_t
to_ms (int64_t utt)
417 return to_duration<std::chrono::milliseconds>(utt).count();
439 static int sleep (
int dur);
451 template <
typename DurT,
452 typename DurT::rep = 0>
472 operator"" _utt (
unsigned long long n)
555 auto const s =
state();
614 alarm{callback, 0, md}
648 bool reschedule =
false>
651 return {[&events](
auto&){
665 template <
typename DurT,
666 typename DurT::rep = 0>
717 template <
typename DurT>
757 template <
typename DurT,
758 typename DurT::rep = 0>
802 friend void ::RTC1_IRQHandler();
809 return deadline_ - now;
813 unsigned int deadline_ = 0;
814 unsigned int interval_;
816 alarm* next_ =
nullptr;
817 static alarm_queue queue_;
899 bool synchronized ()
const noexcept
901 return (0 != sync_ctt_);
922 unsigned int rate () const noexcept
931 return sync_ctt_ - sync_utt_;
time_type convert_ctt(time_type now_utt) noexcept
Best effort conversion from local time to civil time.
unsigned int delta_reset()
Return ticks since the captured time and reset captured time to now.
Definition: clock.hpp:367
int ordinal_(unsigned int now) const noexcept
Calculate an ordinal for the alarm using the distance from now until its deadline.
Definition: clock.hpp:807
constexpr static duration_type::rep from_duration(DurT dur)
Generic conversion from a std::chrono::duration type to uptime ticks.
Definition: clock.hpp:394
void schedule_offset(DurT offset)
Schedule an alarm using a std::chrono duration.
Definition: clock.hpp:718
alarm & set_deadline(unsigned int deadline)
Set the deadline for the alarm to fire.
Definition: clock.hpp:792
static int source_configure(int src, unsigned cal_interval_qs=DefaultRCOCalInterval_qs)
Enable, disable, or query the source of the low-frequency crystal.
~alarm()
On destruction the alarm is cancelled.
Definition: clock.hpp:674
state_type state() const
The current state of the alarm.
Definition: clock.hpp:544
constexpr static int64_t to_us(int64_t utt)
Convert uptime ticks to integral microseconds (rounding down).
Definition: clock.hpp:401
constexpr static unsigned int Frequency_Hz
The high-frequency clock always runs at nominal 16 MHz.
Definition: clock.hpp:62
static constexpr unsigned int default_lfclk_src()
Provide the LFCLKSRC value preferred by the board.
Definition: impl.hpp:76
constexpr static unsigned int Frequency_Hz
Frequency of the uptime clock as a symbolic constant.
Definition: clock.hpp:247
time_type offset() const noexcept
The nominal offset between the civil time and local clocks at the time of the last synchronization po...
Definition: clock.hpp:929
The alarm has been cancelled.
Definition: clock.hpp:540
Functions and data related to the high-frequency clock.
Definition: clock.hpp:31
Alarm is in the queue to execute at some point in the future.
Definition: clock.hpp:518
Alarm has been constructed but has either not yet been scheduled or has completed.
Definition: clock.hpp:511
std::make_signed< time_type >::type diff_type
The signed version of time_type.
Definition: clock.hpp:880
alarm(callback_type callback, unsigned int interval=0, void *md=0)
Standard alarm constructor.
Definition: clock.hpp:595
constexpr static uint32_t counter_mask
Mask for the 24-bit RTC counter.
Definition: periph.hpp:241
void set(event_type events)
Atomically record one or more events.
Definition: core.hpp:603
mutex_irq< static_cast< IRQn_Type >(nrf5::UPTIME_RTC.IRQn)> mutex_type
An RAII type for mutex access to state that might be changed during the uptime clock FLIH.
Definition: clock.hpp:251
nvic_BlockIRQ as a template type.
Definition: core.hpp:497
Support for converting between time domains with the same resolution but unstable clocks.
Definition: clock.hpp:864
Support for a persistent system clock with 32 KiHz resolution.
Definition: clock.hpp:243
static void constlat_request(const periph::TIMER &timer, bool enable)
Set or clear timer-based requests for constant latency wakeups.
std::function< bool(alarm &alarm)> callback_type
The signature of an alarm callback function.
Definition: clock.hpp:577
static void wakeup()
Cancel any in-progress sleep().
static bool active()
Return true iff the low-frequency clock is active.
Definition: clock.hpp:129
Wrapper around the nRF5 TIMER peripheral.
Definition: periph.hpp:301
void schedule()
Queue the alarm to execute at its current deadline().
static int sleep(int dur)
Sleep for a given number of uptime ticks.
alarm & set_interval(DurT interval)
Set the interval() using a given duration.
Definition: clock.hpp:759
constexpr static unsigned int Frequency_Hz
The low-frequency clock always runs at nominal 32 KiHz.
Definition: clock.hpp:119
void update_sync(time_type now_ctt, time_type now_utt) noexcept
Provide a new synchronization pair.
void *const metadata
Pointer to arbitrary data associated with the alarm.
Definition: clock.hpp:839
unsigned int interval() const
An offset added to deadline() prior to invoking the callback when the alarm fires.
Definition: clock.hpp:741
static int configure(int on)
Enable, disable, or query the state of the low-frequency clock.
Alarm deadline has been reached and the callback will soon be invoked.
Definition: clock.hpp:527
static DurT sleep(DurT dur)
Sleep for a specified duration.
Definition: clock.hpp:453
state_type
Constants identifying the alarm state.
Definition: clock.hpp:505
bool configure_pcirq(int on)
Query or control whether the POWER_CLOCK interrupt handler is enabled.
static const char * as_text(text_type buf, uint64_t dur_utt)
Convert an uptime duration to text format.
static bool hfxt_active()
Return true iff the high-frequency clock is being driven by a crystal oscillator.
Definition: clock.hpp:68
static constexpr unsigned int RATE_DENOM
The implicit denominator of rate().
Definition: clock.hpp:883
unsigned int rate() const noexcept
The differential rate of the civil time clock relative to the local clock.
Definition: clock.hpp:922
alarm(callback_type callback, void *md)
Overload for common case of configuring with static callback.
Definition: clock.hpp:612
static unsigned int now24()
Low 24 bits of the uptime counter.
Definition: clock.hpp:300
int initialize(bool enable_hfxt=false, int lfclk_src=board::default_lfclk_src(), unsigned int lfrc_cal_interval_qs=lfclk::DefaultRCOCalInterval_qs)
Initialize the clock system.
unsigned int delta(unsigned int tick) const
Return ticks between captured time and tick.
Definition: clock.hpp:360
Primary header for nrfcxx implementation dependencies.
std::chrono::duration< int64_t, std::ratio< 1, Frequency_Hz > > duration_type
Full precision duration type for uptime clock.
Definition: clock.hpp:259
char[13] text_type
A type containing a NUL-terminated text time representation: HH:MM:SS.mmm
Definition: clock.hpp:262
constexpr static DurT to_duration(int64_t utt)
Generic conversion from uptime ticks to a std::chrono::duration type.
Definition: clock.hpp:387
bool active() const
Return true if the alarm state is one of ST_scheduled, ST_ready, or ST_in_callback.
Definition: clock.hpp:553
static uint32_t source()
Return the actual source for the low-frequency clock.
Definition: clock.hpp:158
Class supporting an alarm with custom callback and repeatability.
Definition: clock.hpp:498
static bool hfxt_configure(int on)
Enable, disable, or query the state of application request for the high-frequency crystal.
Functions and data related to the low-frequency clock.
Definition: clock.hpp:107
static uint64_t now()
Full-range uptime counter.
unsigned int delta() const
Return ticks since the captured time.
Definition: clock.hpp:353
static const char * as_day_text(day_text_type buf, uint64_t dur_utt, bool with_msec=false)
Convert an uptime duration to text format.
char[18] day_text_type
A type encoding a NUL-terminated text day+time representation: DDDD HH:MM:SS.mmm
Definition: clock.hpp:265
unsigned int deadline() const
The value of (the low 32 bits of) uptime::now() at which the alarm should fire.
Definition: clock.hpp:767
static alarm for_event(event_set &events)
Factory producing an alarm with a callback that sets an event.
Definition: clock.hpp:649
void reset()
Capture the current time.
Definition: clock.hpp:347
constexpr static int64_t from_ms(int64_t ms)
Convert integral milliseconds to uptime ticks (rounding down).
Definition: clock.hpp:422
constexpr static int64_t from_us(int64_t us)
Convert integral microseconds to uptime ticks (rounding down).
Definition: clock.hpp:408
alarm & set_interval(unsigned int interval)
Set the interval() in uptime ticks.
Definition: clock.hpp:750
unsigned int event_type
The type used to represent a (set of) event(s).
Definition: core.hpp:547
constexpr alarm(callback_type callback, DurT interval, void *md=0)
Duration-specified alarm constructor.
Definition: clock.hpp:667
state_type cancel()
Cancel a potentially-scheduled alarm.
uint64_t time_type
Type for unsigned 32 KiHz ticks since the epoch of a time system.
Definition: clock.hpp:874
Class supporting short-term duration measurements.
Definition: clock.hpp:330
constexpr static unsigned int DefaultRCOCalInterval_qs
The default RC oscillator calibration interval, in quarter-seconds.
Definition: clock.hpp:126
void schedule_offset(int offset)
Queue the alarm to execute at offset ticks from the current time.
Abstraction of Nordic device peripherals.
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
A record of events that occur asynchonously.
Definition: core.hpp:541
constexpr static int64_t to_ms(int64_t utt)
Convert uptime ticks to integral milliseconds (rounding down).
Definition: clock.hpp:415
unsigned int captured() const
Return the tick corresponding to the captured time.
Definition: clock.hpp:341
The callback is being invoked.
Definition: clock.hpp:535
constexpr static unsigned int delta24(unsigned int a, unsigned int b)
Calculate the tick-count between two 24-bit counter values.
Definition: clock.hpp:313