Skip to main content

kernel/hil/
time.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Hardware agnostic interfaces for time and timers within the Tock
6//! kernel.
7//!
8//! These traits are designed to be able encompass the wide
9//! variety of hardware counters in a general yet efficient way. They
10//! abstract the frequency of a counter through the `Frequency` trait
11//! and the width of a time value through the `Ticks`
12//! trait. Higher-level software abstractions should generally rely on
13//! standard and common implementations of these traits (e.g.. `u32`
14//! ticks and 16MHz frequency).  Hardware counter implementations and
15//! peripherals can represent the actual hardware units an translate
16//! into these more general ones.
17
18use crate::ErrorCode;
19use core::cmp::Ordering;
20use core::fmt;
21
22/// An integer type defining the width of a time value, which allows
23/// clients to know when wraparound will occur.
24pub trait Ticks: Clone + Copy + From<u32> + fmt::Debug + Ord + PartialOrd + Eq {
25    /// Width of the actual underlying timer in bits.
26    ///
27    /// The maximum value that *will* be attained by this timer should
28    /// be `(2 ** width) - 1`. In other words, the timer will wrap at
29    /// exactly `width` bits, and then continue counting at `0`.
30    ///
31    /// The return value is a `u32`, in accordance with the bit widths
32    /// specified using the BITS associated const on Rust integer
33    /// types.
34    fn width() -> u32;
35
36    /// Converts the type into a `usize`, stripping the higher bits
37    /// it if it is larger than `usize` and filling the higher bits
38    /// with 0 if it is smaller than `usize`.
39    fn into_usize(self) -> usize;
40
41    /// The amount of bits required to left-justify this ticks value
42    /// range (filling the lower bits with `0`) for it wrap at `(2 **
43    /// usize::BITS) - 1` bits. For timers with a `width` larger than
44    /// usize, this value will be `0` (i.e., they can simply be
45    /// truncated to usize::BITS bits).
46    fn usize_padding() -> u32 {
47        usize::BITS.saturating_sub(Self::width())
48    }
49
50    /// Converts the type into a `usize`, left-justified and
51    /// right-padded with `0` such that it is guaranteed to wrap at
52    /// `(2 ** usize::BITS) - 1`. If it is larger than usize::BITS
53    /// bits, any higher bits are stripped.
54    ///
55    /// The resulting tick rate will possibly be higher (multiplied by
56    /// `2 ** usize_padding()`). Use `usize_left_justified_scale_freq`
57    /// to convert the underlying timer's frequency into the padded
58    /// ticks frequency in Hertz.
59    fn into_usize_left_justified(self) -> usize {
60        self.into_usize() << Self::usize_padding()
61    }
62
63    /// Convert the generic [`Frequency`] argument into a frequency
64    /// (Hertz) describing a left-justified ticks value as returned by
65    /// [`Ticks::into_usize_left_justified`].
66    fn usize_left_justified_scale_freq<F: Frequency>() -> u32 {
67        F::frequency() << Self::usize_padding()
68    }
69
70    /// Converts the type into a `u32`, stripping the higher bits
71    /// it if it is larger than `u32` and filling the higher bits
72    /// with 0 if it is smaller than `u32`. Included as a simple
73    /// helper since Tock uses `u32` pervasively and most platforms
74    /// are 32 bits.
75    fn into_u32(self) -> u32;
76
77    /// The amount of bits required to left-justify this ticks value
78    /// range (filling the lower bits with `0`) for it wrap at `(2 **
79    /// 32) - 1` bits. For timers with a `width` larger than 32, this
80    /// value will be `0` (i.e., they can simply be truncated to
81    /// 32-bits).
82    ///
83    /// The return value is a `u32`, in accordance with the bit widths
84    /// specified using the BITS associated const on Rust integer
85    /// types.
86    fn u32_padding() -> u32 {
87        u32::BITS.saturating_sub(Self::width())
88    }
89
90    /// Converts the type into a `u32`, left-justified and
91    /// right-padded with `0` such that it is guaranteed to wrap at
92    /// `(2 ** 32) - 1`. If it is larger than 32-bits, any higher bits
93    /// are stripped.
94    ///
95    /// The resulting tick rate will possibly be higher (multiplied by
96    /// `2 ** u32_padding()`). Use `u32_left_justified_scale_freq` to
97    /// convert the underlying timer's frequency into the padded ticks
98    /// frequency in Hertz.
99    fn into_u32_left_justified(self) -> u32 {
100        self.into_u32() << Self::u32_padding()
101    }
102
103    /// Convert the generic [`Frequency`] argument into a frequency
104    /// (Hertz) describing a left-justified ticks value as returned by
105    /// [`Ticks::into_u32_left_justified`].
106    fn u32_left_justified_scale_freq<F: Frequency>() -> u32 {
107        F::frequency() << Self::u32_padding()
108    }
109
110    /// Add two values, wrapping around on overflow using standard
111    /// unsigned arithmetic.
112    fn wrapping_add(self, other: Self) -> Self;
113    /// Subtract two values, wrapping around on underflow using standard
114    /// unsigned arithmetic.
115    fn wrapping_sub(self, other: Self) -> Self;
116
117    /// Returns whether the value is in the range of [`start, `end`) using
118    /// unsigned arithmetic and considering wraparound. It returns `true`
119    /// if, incrementing from `start`, the value will be reached before `end`.
120    /// Put another way, it returns `(self - start) < (end - start)` in
121    /// unsigned arithmetic.
122    fn within_range(self, start: Self, end: Self) -> bool;
123
124    /// Returns the maximum value of this type, which should be (2^width)-1.
125    fn max_value() -> Self;
126
127    /// Returns the half the maximum value of this type, which should be (2^width-1).
128    fn half_max_value() -> Self;
129
130    /// Converts the specified val into this type if it fits otherwise the
131    /// `max_value()` is returned
132    fn from_or_max(val: u64) -> Self;
133
134    /// Scales the ticks by the specified numerator and denominator. If the resulting value would
135    /// be greater than u32,`u32::MAX` is returned instead
136    fn saturating_scale(self, numerator: u32, denominator: u32) -> u32;
137}
138
139/// Represents a clock's frequency in Hz, allowing code to transform
140/// between computer time units and wall clock time. It is typically
141/// an associated type for an implementation of the `Time` trait.
142pub trait Frequency {
143    /// Returns frequency in Hz.
144    fn frequency() -> u32;
145}
146
147/// Represents a moment in time, obtained by calling `now`.
148pub trait Time {
149    /// The number of ticks per second
150    type Frequency: Frequency;
151    /// The width of a time value
152    type Ticks: Ticks;
153
154    /// Returns a timestamp. Depending on the implementation of
155    /// Time, this could represent either a static timestamp or
156    /// a sample of a counter; if an implementation relies on
157    /// it being constant or changing it should use `Timestamp`
158    /// or `Counter`.
159    fn now(&self) -> Self::Ticks;
160}
161
162pub trait ConvertTicks<T: Ticks> {
163    /// Returns the number of ticks in the provided number of seconds,
164    /// rounding down any fractions. If the value overflows Ticks it
165    /// returns `Ticks::max_value()`.
166    fn ticks_from_seconds(&self, s: u32) -> T;
167
168    /// Returns the number of ticks in the provided number of milliseconds,
169    /// rounding down any fractions. If the value overflows Ticks it
170    /// returns `Ticks::max_value()`.
171    fn ticks_from_ms(&self, ms: u32) -> T;
172
173    /// Returns the number of ticks in the provided number of microseconds,
174    /// rounding down any fractions. If the value overflows Ticks it
175    /// returns `Ticks::max_value()`.
176    fn ticks_from_us(&self, us: u32) -> T;
177
178    /// Returns the number of seconds in the provided number of ticks,
179    /// rounding down any fractions. If the value overflows u32, `u32::MAX`
180    /// is returned,
181    fn ticks_to_seconds(&self, tick: T) -> u32;
182
183    /// Returns the number of milliseconds in the provided number of ticks,
184    /// rounding down any fractions. If the value overflows u32, `u32::MAX`
185    /// is returned,
186    fn ticks_to_ms(&self, tick: T) -> u32;
187
188    /// Returns the number of microseconds in the provided number of ticks,
189    /// rounding down any fractions. If the value overflows u32, `u32::MAX`
190    /// is returned,
191    fn ticks_to_us(&self, tick: T) -> u32;
192}
193
194impl<T: Time + ?Sized> ConvertTicks<<T as Time>::Ticks> for T {
195    #[inline]
196    fn ticks_from_seconds(&self, s: u32) -> <T as Time>::Ticks {
197        let val = <T as Time>::Frequency::frequency() as u64 * s as u64;
198        <T as Time>::Ticks::from_or_max(val)
199    }
200    #[inline]
201    fn ticks_from_ms(&self, ms: u32) -> <T as Time>::Ticks {
202        let val = <T as Time>::Frequency::frequency() as u64 * ms as u64;
203        <T as Time>::Ticks::from_or_max(val / 1_000)
204    }
205    #[inline]
206    fn ticks_from_us(&self, us: u32) -> <T as Time>::Ticks {
207        let val = <T as Time>::Frequency::frequency() as u64 * us as u64;
208        <T as Time>::Ticks::from_or_max(val / 1_000_000)
209    }
210
211    #[inline]
212    fn ticks_to_seconds(&self, tick: <T as Time>::Ticks) -> u32 {
213        tick.saturating_scale(1, <T as Time>::Frequency::frequency())
214    }
215    #[inline]
216    fn ticks_to_ms(&self, tick: <T as Time>::Ticks) -> u32 {
217        tick.saturating_scale(1_000, <T as Time>::Frequency::frequency())
218    }
219    #[inline]
220    fn ticks_to_us(&self, tick: <T as Time>::Ticks) -> u32 {
221        tick.saturating_scale(1_000_000, <T as Time>::Frequency::frequency())
222    }
223}
224
225/// Represents a static moment in time, that does not change over
226/// repeated calls to `Time::now`.
227pub trait Timestamp: Time {}
228
229/// Callback handler for when a counter has overflowed past its maximum
230/// value and returned to 0.
231pub trait OverflowClient {
232    fn overflow(&self);
233}
234
235/// Represents a free-running hardware counter that can be started and stopped.
236pub trait Counter<'a>: Time {
237    /// Starts the free-running hardware counter. Valid `Result<(), ErrorCode>` values are:
238    ///   - `Ok(())`: the counter is now running
239    ///   - `Err(ErrorCode::OFF)`: underlying clocks or other hardware resources
240    ///   are not on, such that the counter cannot start.
241    ///   - `Err(ErrorCode::FAIL)`: unidentified failure, counter is not running.
242    /// After a successful call to `start`, `is_running` MUST return true.
243    fn start(&self) -> Result<(), ErrorCode>;
244
245    /// Stops the free-running hardware counter. Valid `Result<(), ErrorCode>` values are:
246    ///   - `Ok(())`: the counter is now stopped. No further
247    ///   overflow callbacks will be invoked.
248    ///   - `Err(ErrorCode::BUSY)`: the counter is in use in a way that means it
249    ///   cannot be stopped and is busy.
250    ///   - `Err(ErrorCode::FAIL)`: unidentified failure, counter is running.
251    /// After a successful call to `stop`, `is_running` MUST return false.
252    fn stop(&self) -> Result<(), ErrorCode>;
253
254    /// Resets the counter to 0. This may introduce jitter on the counter.
255    /// Resetting the counter has no effect on any pending overflow callbacks.
256    /// If a client needs to reset and clear pending callbacks it should
257    /// call `stop` before `reset`.
258    /// Valid `Result<(), ErrorCode>` values are:
259    ///    - `Ok(())`: the counter was reset to 0.
260    ///    - `Err(ErrorCode::FAIL)`: the counter was not reset to 0.
261    fn reset(&self) -> Result<(), ErrorCode>;
262
263    /// Returns whether the counter is currently running.
264    fn is_running(&self) -> bool;
265}
266
267/// Extension trait for counters that support hardware overflow notifications.
268#[deprecated(
269    note = "set_overflow_client never used so moved to an independent trait. Set to be removed in future releases unless a concrete use case is raised."
270)]
271pub trait CounterOverflow<'a>: Counter<'a> {
272    fn set_overflow_client(&self, client: &'a dyn OverflowClient);
273}
274
275/// Callback handler for when an Alarm fires (a `Counter` reaches a specific
276/// value).
277pub trait AlarmClient {
278    /// Callback indicating the alarm time has been reached. The alarm
279    /// MUST be disabled when this is called. If a new alarm is needed,
280    /// the client can call `Alarm::set_alarm`.
281    fn alarm(&self);
282}
283
284/// Interface for receiving notification when a particular time
285/// (`Counter` value) is reached.
286///
287/// Clients use the [`AlarmClient`](trait.AlarmClient.html) trait to
288/// signal when the counter has reached a pre-specified value set in
289/// [`set_alarm`](#tymethod.set_alarm). Alarms are intended for
290/// low-level time needs that require precision (i.e., firing on a
291/// precise clock tick). Software that needs more functionality but
292/// can tolerate some jitter should use the `Timer` trait instead.
293pub trait Alarm<'a>: Time {
294    /// Specify the callback for when the counter reaches the alarm
295    /// value. If there was a previously installed callback this call
296    /// replaces it.
297    fn set_alarm_client(&self, client: &'a dyn AlarmClient);
298
299    /// Specify when the callback should be called and enable it. The
300    /// callback will be enqueued when `Time::now() == reference + dt`. The
301    /// callback itself may not run exactly at this time, due to delays.
302    /// However, it it assured to execute *after* `reference + dt`: it can
303    /// be delayed but will never fire early. The method takes `reference`
304    /// and `dt` rather than a single value denoting the counter value so it
305    /// can distinguish between alarms which have very recently already
306    /// passed and those in the far far future (see #1651).
307    fn set_alarm(&self, reference: Self::Ticks, dt: Self::Ticks);
308
309    /// Return the current alarm value. This is undefined at boot and
310    /// otherwise returns `now + dt` from the last call to `set_alarm`.
311    fn get_alarm(&self) -> Self::Ticks;
312
313    /// Disable the alarm and stop it from firing in the future.
314    /// Valid `Result<(), ErrorCode>` codes are:
315    ///   - `Ok(())` the alarm has been disarmed and will not invoke
316    ///   the callback in the future
317    ///   - `Err(ErrorCode::FAIL)` the alarm could not be disarmed and will invoke
318    ///   the callback in the future
319    fn disarm(&self) -> Result<(), ErrorCode>;
320
321    /// Returns whether the alarm is currently armed. Note that this
322    /// does not reliably indicate whether there will be a future
323    /// callback: it is possible that the alarm has triggered (and
324    /// disarmed) and a callback is pending and has not been called yet.
325    /// In this case it possible for `is_armed` to return false yet to
326    /// receive a callback.
327    fn is_armed(&self) -> bool;
328
329    /// Return the minimum dt value that is supported. Any dt smaller than
330    /// this will automatically be increased to this minimum value.
331    fn minimum_dt(&self) -> Self::Ticks;
332}
333
334/// Callback handler for when a timer fires.
335pub trait TimerClient {
336    fn timer(&self);
337}
338
339/// Interface for controlling callbacks when an interval has passed.
340///
341/// This interface is intended for software that requires repeated
342/// and/or one-shot timers and is willing to experience some jitter or
343/// imprecision in return for a simpler API that doesn't require
344/// actual calculation of counter values. Software that requires more
345/// precisely timed callbacks should use the `Alarm` trait instead.
346pub trait Timer<'a>: Time {
347    /// Specify the callback to invoke when the timer interval expires.
348    /// If there was a previously installed callback this call replaces it.
349    fn set_timer_client(&self, client: &'a dyn TimerClient);
350
351    /// Start a one-shot timer that will invoke the callback at least
352    /// `interval` ticks in the future. If there is a timer currently pending,
353    /// calling this cancels that previous timer. After a callback is invoked
354    /// for a one shot timer, the timer MUST NOT invoke the callback again
355    /// unless a new timer is started (either with repeating or one shot).
356    /// Returns the actual interval for the timer that was registered.
357    /// This MUST NOT be smaller than `interval` but MAY be larger.
358    fn oneshot(&self, interval: Self::Ticks) -> Self::Ticks;
359
360    /// Start a repeating timer that will invoke the callback every
361    /// `interval` ticks in the future. If there is a timer currently
362    /// pending, calling this cancels that previous timer.
363    /// Returns the actual interval for the timer that was registered.
364    /// This MUST NOT be smaller than `interval` but MAY be larger.
365    fn repeating(&self, interval: Self::Ticks) -> Self::Ticks;
366
367    /// Return the interval of the last requested timer.
368    fn interval(&self) -> Option<Self::Ticks>;
369
370    /// Return if the last requested timer is a one-shot timer.
371    fn is_oneshot(&self) -> bool;
372
373    /// Return if the last requested timer is a repeating timer.
374    fn is_repeating(&self) -> bool;
375
376    /// Return how many ticks are remaining until the next callback,
377    /// or None if the timer is disabled.  This call is useful because
378    /// there may be non-negligible delays between when a timer was
379    /// requested and it was actually scheduled. Therefore, since a
380    /// timer's start might be delayed slightly, the time remaining
381    /// might be slightly higher than one would expect if one
382    /// calculated it right before the call to start the timer.
383    fn time_remaining(&self) -> Option<Self::Ticks>;
384
385    /// Returns whether there is currently a timer enabled and so a callback
386    /// will be expected in the future. If `is_enabled` returns false then
387    /// the implementation MUST NOT invoke a callback until a call to `oneshot`
388    /// or `repeating` restarts the timer.
389    fn is_enabled(&self) -> bool;
390
391    /// Cancel the current timer, if any. Value `Result<(), ErrorCode>` values are:
392    ///  - `Ok(())`: no callback will be invoked in the future.
393    ///  - `Err(ErrorCode::FAIL)`: the timer could not be cancelled and a callback
394    ///  will be invoked in the future.
395    fn cancel(&self) -> Result<(), ErrorCode>;
396}
397
398// The following "frequencies" are represented as variant-less enums. Because
399// they can never be constructed, it forces them to be used purely as
400// type-markers which are guaranteed to be elided at runtime.
401
402/// 100MHz `Frequency`
403#[derive(Debug)]
404pub enum Freq100MHz {}
405impl Frequency for Freq100MHz {
406    fn frequency() -> u32 {
407        100_000_000
408    }
409}
410
411/// 16MHz `Frequency`
412#[derive(Debug)]
413pub enum Freq16MHz {}
414impl Frequency for Freq16MHz {
415    fn frequency() -> u32 {
416        16_000_000
417    }
418}
419
420/// 10MHz `Frequency`
421pub enum Freq10MHz {}
422impl Frequency for Freq10MHz {
423    fn frequency() -> u32 {
424        10_000_000
425    }
426}
427
428/// 1MHz `Frequency`
429#[derive(Debug)]
430pub enum Freq1MHz {}
431impl Frequency for Freq1MHz {
432    fn frequency() -> u32 {
433        1_000_000
434    }
435}
436
437/// 32.768KHz `Frequency`
438#[derive(Debug)]
439pub enum Freq32KHz {}
440impl Frequency for Freq32KHz {
441    fn frequency() -> u32 {
442        32_768
443    }
444}
445
446/// 16KHz `Frequency`
447#[derive(Debug)]
448pub enum Freq16KHz {}
449impl Frequency for Freq16KHz {
450    fn frequency() -> u32 {
451        16_000
452    }
453}
454
455/// 1KHz `Frequency`
456#[derive(Debug)]
457pub enum Freq1KHz {}
458impl Frequency for Freq1KHz {
459    fn frequency() -> u32 {
460        1_000
461    }
462}
463
464/// u32 `Ticks`
465#[derive(Clone, Copy, Debug)]
466pub struct Ticks32(u32);
467
468impl From<u32> for Ticks32 {
469    fn from(val: u32) -> Self {
470        Ticks32(val)
471    }
472}
473
474impl Ticks for Ticks32 {
475    fn width() -> u32 {
476        32
477    }
478
479    fn into_usize(self) -> usize {
480        self.0 as usize
481    }
482
483    fn into_u32(self) -> u32 {
484        self.0
485    }
486
487    fn wrapping_add(self, other: Self) -> Self {
488        Ticks32(self.0.wrapping_add(other.0))
489    }
490
491    fn wrapping_sub(self, other: Self) -> Self {
492        Ticks32(self.0.wrapping_sub(other.0))
493    }
494
495    fn within_range(self, start: Self, end: Self) -> bool {
496        self.wrapping_sub(start).0 < end.wrapping_sub(start).0
497    }
498
499    /// Returns the maximum value of this type, which should be (2^width)-1.
500    fn max_value() -> Self {
501        Ticks32(0xFFFFFFFF)
502    }
503
504    /// Returns the half the maximum value of this type, which should be (2^width-1).
505    fn half_max_value() -> Self {
506        Self(1 + (Self::max_value().0 / 2))
507    }
508
509    #[inline]
510    fn from_or_max(val: u64) -> Self {
511        if val < Self::max_value().0 as u64 {
512            Self::from(val as u32)
513        } else {
514            Self::max_value()
515        }
516    }
517
518    #[inline]
519    fn saturating_scale(self, numerator: u32, denominator: u32) -> u32 {
520        let scaled = self.0 as u64 * numerator as u64 / denominator as u64;
521        if scaled < u32::MAX as u64 {
522            scaled as u32
523        } else {
524            u32::MAX
525        }
526    }
527}
528
529impl PartialOrd for Ticks32 {
530    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
531        Some(self.cmp(other))
532    }
533}
534
535impl Ord for Ticks32 {
536    fn cmp(&self, other: &Self) -> Ordering {
537        self.0.cmp(&other.0)
538    }
539}
540
541impl PartialEq for Ticks32 {
542    fn eq(&self, other: &Self) -> bool {
543        self.0 == other.0
544    }
545}
546
547impl Eq for Ticks32 {}
548
549/// 24-bit `Ticks`
550#[derive(Clone, Copy, Debug)]
551pub struct Ticks24(u32);
552
553impl Ticks24 {
554    pub const MASK: u32 = 0x00FFFFFF;
555}
556
557impl From<u32> for Ticks24 {
558    fn from(val: u32) -> Self {
559        Ticks24(val & Self::MASK)
560    }
561}
562
563impl Ticks for Ticks24 {
564    fn width() -> u32 {
565        24
566    }
567
568    fn into_usize(self) -> usize {
569        self.0 as usize
570    }
571
572    fn into_u32(self) -> u32 {
573        self.0
574    }
575
576    fn wrapping_add(self, other: Self) -> Self {
577        Ticks24(self.0.wrapping_add(other.0) & Self::MASK)
578    }
579
580    fn wrapping_sub(self, other: Self) -> Self {
581        Ticks24(self.0.wrapping_sub(other.0) & Self::MASK)
582    }
583
584    fn within_range(self, start: Self, end: Self) -> bool {
585        self.wrapping_sub(start).0 < end.wrapping_sub(start).0
586    }
587
588    /// Returns the maximum value of this type, which should be (2^width)-1.
589    fn max_value() -> Self {
590        Ticks24(Self::MASK)
591    }
592
593    /// Returns the half the maximum value of this type, which should be (2^width-1).
594    fn half_max_value() -> Self {
595        Self(1 + (Self::max_value().0 / 2))
596    }
597
598    #[inline]
599    fn from_or_max(val: u64) -> Self {
600        if val < Self::max_value().0 as u64 {
601            Self::from(val as u32)
602        } else {
603            Self::max_value()
604        }
605    }
606
607    #[inline]
608    fn saturating_scale(self, numerator: u32, denominator: u32) -> u32 {
609        let scaled = self.0 as u64 * numerator as u64 / denominator as u64;
610        if scaled < u32::MAX as u64 {
611            scaled as u32
612        } else {
613            u32::MAX
614        }
615    }
616}
617
618impl PartialOrd for Ticks24 {
619    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
620        Some(self.cmp(other))
621    }
622}
623
624impl Ord for Ticks24 {
625    fn cmp(&self, other: &Self) -> Ordering {
626        self.0.cmp(&other.0)
627    }
628}
629
630impl PartialEq for Ticks24 {
631    fn eq(&self, other: &Self) -> bool {
632        self.0 == other.0
633    }
634}
635
636impl Eq for Ticks24 {}
637
638/// 16-bit `Ticks`
639#[derive(Clone, Copy, Debug)]
640pub struct Ticks16(u16);
641
642impl From<u16> for Ticks16 {
643    fn from(val: u16) -> Self {
644        Ticks16(val)
645    }
646}
647
648impl From<u32> for Ticks16 {
649    fn from(val: u32) -> Self {
650        Ticks16((val & 0xffff) as u16)
651    }
652}
653
654impl Ticks16 {
655    pub fn into_u16(self) -> u16 {
656        self.0
657    }
658}
659
660impl Ticks for Ticks16 {
661    fn width() -> u32 {
662        16
663    }
664
665    fn into_usize(self) -> usize {
666        self.0 as usize
667    }
668
669    fn into_u32(self) -> u32 {
670        self.0 as u32
671    }
672
673    fn wrapping_add(self, other: Self) -> Self {
674        Ticks16(self.0.wrapping_add(other.0))
675    }
676
677    fn wrapping_sub(self, other: Self) -> Self {
678        Ticks16(self.0.wrapping_sub(other.0))
679    }
680
681    fn within_range(self, start: Self, end: Self) -> bool {
682        self.wrapping_sub(start).0 < end.wrapping_sub(start).0
683    }
684
685    /// Returns the maximum value of this type, which should be (2^width)-1.
686    fn max_value() -> Self {
687        Ticks16(0xFFFF)
688    }
689
690    /// Returns the half the maximum value of this type, which should be (2^width-1).
691    fn half_max_value() -> Self {
692        Self(1 + (Self::max_value().0 / 2))
693    }
694
695    #[inline]
696    fn from_or_max(val: u64) -> Self {
697        if val < Self::max_value().0 as u64 {
698            Self::from(val as u32)
699        } else {
700            Self::max_value()
701        }
702    }
703
704    #[inline]
705    fn saturating_scale(self, numerator: u32, denominator: u32) -> u32 {
706        let scaled = self.0 as u64 * numerator as u64 / denominator as u64;
707        if scaled < u32::MAX as u64 {
708            scaled as u32
709        } else {
710            u32::MAX
711        }
712    }
713}
714
715impl PartialOrd for Ticks16 {
716    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
717        Some(self.cmp(other))
718    }
719}
720
721impl Ord for Ticks16 {
722    fn cmp(&self, other: &Self) -> Ordering {
723        self.0.cmp(&other.0)
724    }
725}
726
727impl PartialEq for Ticks16 {
728    fn eq(&self, other: &Self) -> bool {
729        self.0 == other.0
730    }
731}
732
733impl Eq for Ticks16 {}
734
735/// 64-bit `Ticks`
736#[derive(Clone, Copy, Debug)]
737pub struct Ticks64(u64);
738
739impl Ticks64 {
740    pub fn into_u64(self) -> u64 {
741        self.0
742    }
743}
744
745impl From<u32> for Ticks64 {
746    fn from(val: u32) -> Self {
747        Ticks64(val as u64)
748    }
749}
750
751impl From<u64> for Ticks64 {
752    fn from(val: u64) -> Self {
753        Ticks64(val)
754    }
755}
756
757impl Ticks for Ticks64 {
758    fn width() -> u32 {
759        64
760    }
761
762    fn into_usize(self) -> usize {
763        self.0 as usize
764    }
765
766    fn into_u32(self) -> u32 {
767        self.0 as u32
768    }
769
770    fn wrapping_add(self, other: Self) -> Self {
771        Ticks64(self.0.wrapping_add(other.0))
772    }
773
774    fn wrapping_sub(self, other: Self) -> Self {
775        Ticks64(self.0.wrapping_sub(other.0))
776    }
777
778    fn within_range(self, start: Self, end: Self) -> bool {
779        self.wrapping_sub(start).0 < end.wrapping_sub(start).0
780    }
781
782    /// Returns the maximum value of this type, which should be (2^width)-1.
783    fn max_value() -> Self {
784        Ticks64(!0u64)
785    }
786
787    /// Returns the half the maximum value of this type, which should be (2^width-1).
788    fn half_max_value() -> Self {
789        Self(1 + (Self::max_value().0 / 2))
790    }
791
792    #[inline]
793    fn from_or_max(val: u64) -> Self {
794        Self(val)
795    }
796
797    #[inline]
798    fn saturating_scale(self, num: u32, den: u32) -> u32 {
799        let scaled = self.0.saturating_mul(num as u64) / den as u64;
800        if scaled < u32::MAX as u64 {
801            scaled as u32
802        } else {
803            u32::MAX
804        }
805    }
806}
807
808impl PartialOrd for Ticks64 {
809    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
810        Some(self.cmp(other))
811    }
812}
813
814impl Ord for Ticks64 {
815    fn cmp(&self, other: &Self) -> Ordering {
816        self.0.cmp(&other.0)
817    }
818}
819
820impl PartialEq for Ticks64 {
821    fn eq(&self, other: &Self) -> bool {
822        self.0 == other.0
823    }
824}
825
826impl Eq for Ticks64 {}
827
828#[cfg(test)]
829mod tests {
830    use super::*;
831
832    struct Test1MHz64();
833    impl Time for Test1MHz64 {
834        type Frequency = Freq1MHz;
835        type Ticks = Ticks64;
836
837        fn now(&self) -> Self::Ticks {
838            0u32.into()
839        }
840    }
841
842    #[test]
843    fn test_from_ticks64() {
844        let s = Test1MHz64().ticks_to_seconds(1_000_000u32.into());
845        assert_eq!(s, 1);
846
847        let ms = Test1MHz64().ticks_to_ms(1_000_000u32.into());
848        assert_eq!(ms, 1_000);
849
850        let us = Test1MHz64().ticks_to_us(1_000_000u32.into());
851        assert_eq!(us, 1_000_000);
852
853        let s = Test1MHz64().ticks_to_seconds((1_000_000u64 << 31).into());
854        assert_eq!(s, 1 << 31);
855
856        let ms = Test1MHz64().ticks_to_ms((1_000_000u64 << 31).into());
857        assert_eq!(ms, !0u32);
858
859        let us = Test1MHz64().ticks_to_us((1_000_000u64 << 31).into());
860        assert_eq!(us, !0u32);
861    }
862
863    #[test]
864    fn test_to_ticks64() {
865        let t = Test1MHz64().ticks_from_seconds(1);
866        assert_eq!(t.into_u32(), 1_000_000);
867
868        let t = Test1MHz64().ticks_from_ms(1);
869        assert_eq!(t.into_u32(), 1_000);
870
871        let t = Test1MHz64().ticks_from_us(1);
872        assert_eq!(t.into_u32(), 1);
873
874        let t = Test1MHz64().ticks_from_seconds(1 << 31);
875        assert_eq!(t.into_u64(), 1_000_000u64 << 31);
876    }
877
878    struct Test1KHz16();
879    impl Time for Test1KHz16 {
880        type Frequency = Freq1KHz;
881        type Ticks = Ticks16;
882
883        fn now(&self) -> Self::Ticks {
884            0u32.into()
885        }
886    }
887
888    #[test]
889    fn test_from_ticks16() {
890        let s = Test1KHz16().ticks_to_seconds(1_000u32.into());
891        assert_eq!(s, 1);
892
893        let ms = Test1KHz16().ticks_to_ms(1_000u32.into());
894        assert_eq!(ms, 1_000);
895
896        let us = Test1KHz16().ticks_to_us(1_000u32.into());
897        assert_eq!(us, 1_000_000);
898    }
899
900    #[test]
901    fn test_to_ticks16() {
902        let t = Test1KHz16().ticks_from_seconds(1);
903        assert_eq!(t.into_u32(), 1_000);
904
905        let t = Test1KHz16().ticks_from_seconds(u32::MAX);
906        assert_eq!(t.into_u32(), u16::MAX as u32);
907
908        let t = Test1KHz16().ticks_from_seconds(66);
909        assert_eq!(t.into_u32(), u16::MAX as u32);
910
911        let t = Test1KHz16().ticks_from_seconds(65);
912        assert_eq!(t.into_u32(), 65_000);
913
914        let t = Test1KHz16().ticks_from_ms(1);
915        assert_eq!(t.into_u32(), 1);
916
917        let t = Test1KHz16().ticks_from_us(1);
918        assert_eq!(t.into_u32(), 0);
919    }
920
921    struct Test1KHz24();
922    impl Time for Test1KHz24 {
923        type Frequency = Freq1KHz;
924        type Ticks = Ticks24;
925
926        fn now(&self) -> Self::Ticks {
927            0u32.into()
928        }
929    }
930
931    #[test]
932    fn test_ticks24() {
933        let s = Test1KHz24().ticks_to_seconds(5_000_000u32.into());
934        assert_eq!(s, 5_000);
935
936        let ms = Test1KHz24().ticks_to_ms(5_000_000u32.into());
937        assert_eq!(ms, 5_000_000);
938
939        let us = Test1KHz24().ticks_to_us(5_000_000u32.into());
940        assert_eq!(us, u32::MAX);
941    }
942
943    #[test]
944    fn test_dyn_object() {
945        let time: &dyn Time<Frequency = Freq1KHz, Ticks = Ticks24> = &Test1KHz24();
946
947        let s = time.ticks_to_seconds(5_000_000u32.into());
948        assert_eq!(s, 5_000);
949
950        let ms = time.ticks_to_ms(5_000_000u32.into());
951        assert_eq!(ms, 5_000_000);
952
953        let us = time.ticks_to_us(5_000_000u32.into());
954        assert_eq!(us, u32::MAX);
955    }
956}