From c4d144d49a0e760b89c9b19015e124c7de24051d Mon Sep 17 00:00:00 2001 From: David O'Connor Date: Wed, 22 May 2024 16:37:33 -0400 Subject: [PATCH] Updates to timer interface --- src/instant.rs | 28 ++++++++++------------------ src/timer.rs | 27 ++++++--------------------- 2 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/instant.rs b/src/instant.rs index 71f2099..bb6a15e 100644 --- a/src/instant.rs +++ b/src/instant.rs @@ -12,33 +12,28 @@ use core::{ #[derive(Eq, PartialEq, PartialOrd, Copy, Clone, Default)] pub struct Instant { /// Total count, in microseconds. - /// todo: Do you need ns resolution? - // pub count_us: i64, // todo: u64 or i64? x128? - pub count_ns: i64, // todo: u64 or i64? x128? + pub count_ns: i128, // todo: u128? } impl Instant { /// The time, in seconds. pub fn as_secs(&self) -> f32 { - // self.count_us as f32 / 1_000_000. self.count_ns as f32 / 1_000_000_000. } /// The time, in milliseconds. - pub fn as_ms(&self) -> f32 { - // self.count_us as f32 / 1_000. - self.count_ns as f32 / 1_000_000. + pub fn as_ms(&self) -> u64 { + (self.count_ns / 1_000_000) as u64 } /// The time, in microseconds - pub fn as_us(&self) -> f32 { - // self.count_us as f32 - self.count_ns as f32 / 1_000. + pub fn as_us(&self) -> u64 { + (self.count_ns / 1_000) as u64 } /// The time, in nanoseconds - pub fn as_ns(&self) -> f32 { - self.count_ns as f32 + pub fn as_ns(&self) -> i128 { + self.count_ns } } @@ -54,8 +49,7 @@ impl Add for Instant { fn add(self, rhs: Duration) -> Self::Output { Self { - // count_us: self.count_us + rhs.as_micros() as i64, - count_ns: self.count_ns + rhs.as_nanos() as i64, + count_ns: self.count_ns + rhs.as_nanos() as i128, } } } @@ -65,8 +59,7 @@ impl Sub for Instant { fn sub(self, rhs: Duration) -> Self::Output { Self { - // count_us: self.count_us - rhs.as_micros() as i64, - count_ns: self.count_ns - rhs.as_nanos() as i64, + count_ns: self.count_ns - rhs.as_nanos() as i128, } } } @@ -75,8 +68,7 @@ impl Sub for Instant { type Output = Duration; fn sub(self, rhs: Self) -> Self::Output { - // todo: Handle negative overflow! - // Duration::from_micros((self.count_us - rhs.count_us) as u64) + // todo: Handle negative overflow. Duration::from_nanos((self.count_ns - rhs.count_ns) as u64) } } diff --git a/src/timer.rs b/src/timer.rs index 7ff2c07..d5867b2 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -853,7 +853,7 @@ macro_rules! make_timer { // Instant { count_us } let count_ns = ((self.read_count() as u64 + self.wrap_count as u64 * - self.get_max_duty() as u64) * self.ns_per_tick) as i64; + self.get_max_duty() as u64) * self.ns_per_tick) as i128; Instant { count_ns } } @@ -2062,26 +2062,11 @@ pub fn clear_update_interrupt(tim_num: u8) { let bits = 0xffff_ffff; match tim_num { - 1 => periphs - .TIM1 - .sr - .write(|w| w.bits(bits).uif().clear_bit()), - 2 => periphs - .TIM2 - .sr - .write(|w| w.bits(bits).uif().clear_bit()), - 3 => periphs - .TIM3 - .sr - .write(|w| w.bits(bits).uif().clear_bit()), - 4 => periphs - .TIM4 - .sr - .write(|w| w.bits(bits).uif().clear_bit()), - 8 => periphs - .TIM8 - .sr - .write(|w| w.bits(bits).uif().clear_bit()), + 1 => periphs.TIM1.sr.write(|w| w.bits(bits).uif().clear_bit()), + 2 => periphs.TIM2.sr.write(|w| w.bits(bits).uif().clear_bit()), + 3 => periphs.TIM3.sr.write(|w| w.bits(bits).uif().clear_bit()), + 4 => periphs.TIM4.sr.write(|w| w.bits(bits).uif().clear_bit()), + // 8 => periphs.TIM8.sr.write(|w| w.bits(bits).uif().clear_bit()), _ => unimplemented!(), } };