Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes durations in the time module to use u64 instead of u32 #820

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions boards/feather_m0/examples/async_timer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]

use atsamd_hal::time::Milliseconds;
#[cfg(not(feature = "use_semihosting"))]
use panic_halt as _;
#[cfg(feature = "use_semihosting")]
Expand All @@ -11,7 +12,6 @@ use feather_m0 as bsp;
use hal::{
clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController},
ehal::digital::StatefulOutputPin,
fugit::MillisDurationU32,
pac::Tc4,
timer::TimerCounter,
};
Expand Down Expand Up @@ -48,9 +48,7 @@ async fn main(_s: embassy_executor::Spawner) {
let mut timer = timer.into_future(Irqs);

loop {
timer
.delay(MillisDurationU32::from_ticks(500).convert())
.await;
timer.delay(Milliseconds::from_ticks(500).convert()).await;
red_led.toggle().unwrap();
}
}
6 changes: 2 additions & 4 deletions boards/metro_m4/examples/async_timer.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![no_std]
#![no_main]

use atsamd_hal::time::Milliseconds;
#[cfg(not(feature = "use_semihosting"))]
use panic_halt as _;
#[cfg(feature = "use_semihosting")]
use panic_semihosting as _;

use bsp::{hal, pac, pin_alias};
use hal::fugit::MillisDurationU32;
use hal::{
clock::GenericClockController, ehal::digital::StatefulOutputPin, pac::Tc4, timer::TimerCounter,
};
Expand Down Expand Up @@ -41,9 +41,7 @@ async fn main(_s: embassy_executor::Spawner) {
let mut timer = timer.into_future(Irqs);

loop {
timer
.delay(MillisDurationU32::from_ticks(500).convert())
.await;
timer.delay(Milliseconds::from_ticks(500).convert()).await;
red_led.toggle().unwrap();
}
}
7 changes: 4 additions & 3 deletions hal/src/peripherals/timer/async_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use crate::{
async_hal::interrupts::{Binding, Handler, Interrupt},
pac,
time::Nanoseconds,
timer_traits::InterruptDrivenTimer,
typelevel::Sealed,
};
Expand All @@ -17,7 +18,6 @@ use core::{
task::{Poll, Waker},
};
use embassy_sync::waitqueue::AtomicWaker;
use fugit::NanosDurationU32;
use portable_atomic::AtomicBool;

use crate::peripherals::timer;
Expand Down Expand Up @@ -186,7 +186,7 @@ where
{
/// Delay asynchronously
#[inline]
pub async fn delay(&mut self, count: NanosDurationU32) {
pub async fn delay(&mut self, count: Nanoseconds) {
self.timer.start(count);
self.timer.enable_interrupt();

Expand Down Expand Up @@ -217,7 +217,8 @@ where
T: AsyncCount16,
{
async fn delay_ns(&mut self, ns: u32) {
self.delay(NanosDurationU32::from_ticks(ns).convert()).await;
self.delay(Nanoseconds::from_ticks(ns.into()).convert())
.await;
}
}

Expand Down
3 changes: 1 addition & 2 deletions hal/src/peripherals/timer/d11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use core::convert::Infallible;

use atsamd_hal_macros::hal_cfg;
use fugit::NanosDurationU32;

use crate::ehal_02::timer::{CountDown, Periodic};
use crate::pac::Pm;
Expand Down Expand Up @@ -86,7 +85,7 @@ where
self.tc.count_16().intenset().write(|w| w.ovf().set_bit());
}

fn start<T: Into<NanosDurationU32>>(&mut self, timeout: T) {
fn start<T: Into<Nanoseconds>>(&mut self, timeout: T) {
let params = TimerParams::new_ns(timeout.into(), self.freq);
let divider = params.divider;
let cycles = params.cycles;
Expand Down
3 changes: 1 addition & 2 deletions hal/src/peripherals/timer/d5x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use core::convert::Infallible;

use atsamd_hal_macros::hal_cfg;
use fugit::NanosDurationU32;

use crate::ehal_02::timer::{CountDown, Periodic};
use crate::pac::tc0::Count16 as Count16Reg;
Expand Down Expand Up @@ -85,7 +84,7 @@ where

fn start<T>(&mut self, timeout: T)
where
T: Into<NanosDurationU32>,
T: Into<Nanoseconds>,
{
let params = TimerParams::new_ns(timeout.into(), self.freq);
let divider = params.divider;
Expand Down
6 changes: 2 additions & 4 deletions hal/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Import the prelude to gain convenient access to helper traits
pub use crate::eic::EicPin;
pub use crate::timer_traits::InterruptDrivenTimer;
pub use fugit::ExtU32 as _;
pub use fugit::ExtU64 as _;
pub use fugit::ExtU64Ceil as _;
pub use fugit::RateExtU32 as _;

// embedded-hal doesn’t yet have v2 in its prelude, so we need to
Expand All @@ -14,6 +15,3 @@ pub use crate::ehal_02::prelude::*;

#[cfg(feature = "rtic")]
pub use rtic_time::Monotonic as _;

#[cfg(feature = "rtic")]
pub use fugit::{ExtU64, ExtU64Ceil};
6 changes: 2 additions & 4 deletions hal/src/rtc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Real-time clock/counter
use atsamd_hal_macros::{hal_cfg, hal_macro_helper};
use fugit::NanosDurationU32;

use crate::ehal_02;
use crate::pac;
Expand Down Expand Up @@ -383,7 +382,7 @@ impl InterruptDrivenTimer for Rtc<Count32Mode> {

fn start<T>(&mut self, timeout: T)
where
T: Into<NanosDurationU32>,
T: Into<Nanoseconds>,
{
let params = TimerParams::new_us(timeout, self.rtc_clock_freq);
let divider = params.divider;
Expand Down Expand Up @@ -458,8 +457,7 @@ impl TimerParams {
pub fn new_us(timeout: impl Into<Nanoseconds>, src_freq: impl Into<Hertz>) -> Self {
let timeout = timeout.into();
let src_freq = src_freq.into();
let ticks: u32 =
(timeout.to_nanos() as u64 * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32;
let ticks: u32 = (timeout.to_nanos() * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32;
Self::new_from_ticks(ticks)
}

Expand Down
8 changes: 4 additions & 4 deletions hal/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub type MegaHertz = fugit::MegahertzU32;
// Period based

/// Seconds
pub type Seconds = fugit::SecsDurationU32;
pub type Seconds = fugit::SecsDurationU64;

/// Milliseconds
pub type Milliseconds = fugit::MillisDurationU32;
pub type Milliseconds = fugit::MillisDurationU64;

/// Microseconds
pub type Microseconds = fugit::MicrosDurationU32;
pub type Microseconds = fugit::MicrosDurationU64;

/// Nanoseconds
pub type Nanoseconds = fugit::NanosDurationU32;
pub type Nanoseconds = fugit::NanosDurationU64;
5 changes: 2 additions & 3 deletions hal/src/timer_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ impl TimerParams {

/// calculates TimerParams from a given period based timeout.
pub fn new_ns(timeout: Nanoseconds, src_freq: Hertz) -> Self {
let ticks: u32 =
(timeout.to_nanos() as u64 * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32;
let ticks: u32 = (timeout.to_nanos() * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32;
Self::new_from_ticks(ticks)
}

Expand Down Expand Up @@ -51,7 +50,7 @@ impl TimerParams {

#[cfg(test)]
mod tests {
use crate::fugit::{ExtU32, RateExtU32};
use crate::fugit::{ExtU64, RateExtU32};
use crate::timer_params::TimerParams;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions hal/src/timer_traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::convert::Infallible;

use fugit::NanosDurationU32;
use crate::time::Nanoseconds;

/// Specifies a timer that can enable & disable an interrupt that fires
/// when the timer expires
Expand All @@ -9,7 +9,7 @@ pub trait InterruptDrivenTimer {
fn enable_interrupt(&mut self);

/// Start the timer with a given timeout in nanoseconds
fn start<T: Into<NanosDurationU32>>(&mut self, timeout: T);
fn start<T: Into<Nanoseconds>>(&mut self, timeout: T);

/// Wait for the timer to finish counting down **without blocking**.
fn wait(&mut self) -> nb::Result<(), Infallible>;
Expand Down