Skip to content

Commit

Permalink
Updates to timer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
David O'Connor committed May 22, 2024
1 parent caa4c2f commit c4d144d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
28 changes: 10 additions & 18 deletions src/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -54,8 +49,7 @@ impl Add<Duration> 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,
}
}
}
Expand All @@ -65,8 +59,7 @@ impl Sub<Duration> 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,
}
}
}
Expand All @@ -75,8 +68,7 @@ impl Sub<Self> 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)
}
}
27 changes: 6 additions & 21 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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!(),
}
};
Expand Down

0 comments on commit c4d144d

Please sign in to comment.