aboutsummaryrefslogtreecommitdiff
path: root/rtic-monotonics/src
diff options
context:
space:
mode:
authorNils Fitinghoff <nils.fitinghoff@mobilaris.se>2023-08-24 16:29:20 +0200
committerEmil Fresk <emil.fresk@gmail.com>2023-08-25 15:16:18 +0000
commit4ea73021d62bda15ce9a7b36123e4a237c8e4255 (patch)
tree69249b8e623460da9eddfad25ce50271476c92e5 /rtic-monotonics/src
parent609f14b1e4f169eb639025a5edefbb64afebf816 (diff)
rtic-monotonics: Add 64-bit SysTick monotonic
Counting at 1 kHz, 32 bits for counting ticks is not enough to ensure monotonicity for more than 50 days. Add a feature to change the backing storage to 64 bits.
Diffstat (limited to 'rtic-monotonics/src')
-rw-r--r--rtic-monotonics/src/systick.rs34
1 files changed, 27 insertions, 7 deletions
diff --git a/rtic-monotonics/src/systick.rs b/rtic-monotonics/src/systick.rs
index fe66a20..9b8fca8 100644
--- a/rtic-monotonics/src/systick.rs
+++ b/rtic-monotonics/src/systick.rs
@@ -34,10 +34,22 @@
use super::Monotonic;
pub use super::{TimeoutError, TimerQueue};
-use atomic_polyfill::{AtomicU32, Ordering};
+use atomic_polyfill::Ordering;
use core::future::Future;
use cortex_m::peripheral::SYST;
-pub use fugit::{self, ExtU32};
+pub use fugit;
+cfg_if::cfg_if! {
+ if #[cfg(feature = "systick-64bit")] {
+ pub use fugit::ExtU64;
+ use atomic_polyfill::AtomicU64;
+ static SYSTICK_CNT: AtomicU64 = AtomicU64::new(0);
+ } else {
+ pub use fugit::ExtU32;
+ use atomic_polyfill::AtomicU32;
+ static SYSTICK_CNT: AtomicU32 = AtomicU32::new(0);
+ }
+}
+static SYSTICK_TIMER_QUEUE: TimerQueue<Systick> = TimerQueue::new();
// Features should be additive, here systick-100hz gets picked if both
// `systick-100hz` and `systick-10khz` are enabled.
@@ -94,9 +106,6 @@ impl Systick {
}
}
-static SYSTICK_CNT: AtomicU32 = AtomicU32::new(0);
-static SYSTICK_TIMER_QUEUE: TimerQueue<Systick> = TimerQueue::new();
-
// Forward timerqueue interface
impl Systick {
/// Used to access the underlying timer queue
@@ -136,8 +145,15 @@ impl Systick {
}
impl Monotonic for Systick {
- type Instant = fugit::TimerInstantU32<TIMER_HZ>;
- type Duration = fugit::TimerDurationU32<TIMER_HZ>;
+ cfg_if::cfg_if! {
+ if #[cfg(feature = "systick-64bit")] {
+ type Instant = fugit::TimerInstantU64<TIMER_HZ>;
+ type Duration = fugit::TimerDurationU64<TIMER_HZ>;
+ } else {
+ type Instant = fugit::TimerInstantU32<TIMER_HZ>;
+ type Duration = fugit::TimerDurationU32<TIMER_HZ>;
+ }
+ }
const ZERO: Self::Instant = Self::Instant::from_ticks(0);
@@ -175,10 +191,14 @@ impl Monotonic for Systick {
#[cfg(feature = "embedded-hal-async")]
impl embedded_hal_async::delay::DelayUs for Systick {
async fn delay_us(&mut self, us: u32) {
+ #[cfg(feature = "systick-64bit")]
+ let us = u64::from(us);
Self::delay(us.micros()).await;
}
async fn delay_ms(&mut self, ms: u32) {
+ #[cfg(feature = "systick-64bit")]
+ let ms = u64::from(ms);
Self::delay(ms.millis()).await;
}
}