embassy/time: refactor module structure

This commit is contained in:
Dario Nieuwenhuis 2021-07-12 03:10:01 +02:00
parent 40bc67bee0
commit 94bd4eb7d5
4 changed files with 75 additions and 81 deletions

View file

@ -4,7 +4,6 @@ use core::{mem, ptr};
pub mod raw; pub mod raw;
mod run_queue; mod run_queue;
pub(crate) mod timer;
mod timer_queue; mod timer_queue;
mod util; mod util;
mod waker; mod waker;

70
embassy/src/time/delay.rs Normal file
View file

@ -0,0 +1,70 @@
use core::future::Future;
use super::{Duration, Instant, Timer};
/// Async or blocking delay.
pub struct Delay;
impl crate::traits::delay::Delay for Delay {
type DelayFuture<'a> = impl Future<Output = ()> + 'a;
fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> {
Timer::after(Duration::from_millis(millis))
}
fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> {
Timer::after(Duration::from_micros(micros))
}
}
/// Type used for blocking delays through embedded-hal traits.
///
/// For this interface to work, the Executor's clock must be correctly initialized before using it.
/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
/// Make sure to use a suitable tick rate for your use case. The tick rate can be chosen through
/// features flags of this crate.
pub struct BlockingTimer;
impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer {
fn delay_ms(&mut self, ms: u8) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer {
fn delay_ms(&mut self, ms: u16) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer {
fn delay_ms(&mut self, ms: u32) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer {
fn delay_us(&mut self, us: u8) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer {
fn delay_us(&mut self, us: u16) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer {
fn delay_us(&mut self, us: u32) {
block_for(Duration::from_micros(us as u64))
}
}
/// Blocks the cpu for at least `duration`.
///
/// For this interface to work, the Executor's clock must be correctly initialized before using it.
pub fn block_for(duration: Duration) {
let expires_at = Instant::now() + duration;
while Instant::now() < expires_at {}
}

View file

@ -1,13 +1,16 @@
//! Time abstractions //! Time abstractions
//! To use these abstractions, first call `set_clock` with an instance of an [Clock](trait.Clock.html). //! To use these abstractions, first call `set_clock` with an instance of an [Clock](trait.Clock.html).
//! //!
mod delay;
mod duration; mod duration;
mod instant; mod instant;
mod timer;
mod traits; mod traits;
pub use crate::executor::timer::{with_timeout, Delay, Ticker, TimeoutError, Timer}; pub use delay::{block_for, Delay};
pub use duration::Duration; pub use duration::Duration;
pub use instant::Instant; pub use instant::Instant;
pub use timer::{with_timeout, Ticker, TimeoutError, Timer};
pub use traits::*; pub use traits::*;
#[cfg(any( #[cfg(any(
@ -42,56 +45,3 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) {
pub(crate) fn now() -> u64 { pub(crate) fn now() -> u64 {
unsafe { unwrap!(CLOCK, "No clock set").now() } unsafe { unwrap!(CLOCK, "No clock set").now() }
} }
/// Type used for blocking delays through embedded-hal traits.
///
/// For this interface to work, the Executor's clock must be correctly initialized before using it.
/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
/// Make sure to use a suitable tick rate for your use case. The tick rate can be chosen through
/// features flags of this crate.
pub struct BlockingTimer;
impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer {
fn delay_ms(&mut self, ms: u8) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer {
fn delay_ms(&mut self, ms: u16) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer {
fn delay_ms(&mut self, ms: u32) {
block_for(Duration::from_millis(ms as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer {
fn delay_us(&mut self, us: u8) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer {
fn delay_us(&mut self, us: u16) {
block_for(Duration::from_micros(us as u64))
}
}
impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer {
fn delay_us(&mut self, us: u32) {
block_for(Duration::from_micros(us as u64))
}
}
/// Blocks the cpu for at least `duration`.
///
/// For this interface to work, the Executor's clock must be correctly initialized before using it.
pub fn block_for(duration: Duration) {
let expires_at = Instant::now() + duration;
while Instant::now() < expires_at {}
}

View file

@ -1,36 +1,11 @@
use core::future::Future; use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin; use core::pin::Pin;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
use futures::{future::select, future::Either, pin_mut, Stream}; use futures::{future::select, future::Either, pin_mut, Stream};
use super::raw; use crate::executor::raw;
use crate::time::{Duration, Instant}; use crate::time::{Duration, Instant};
/// Delay abstraction using embassy's clock.
pub struct Delay {
_data: PhantomData<bool>,
}
impl Delay {
pub fn new() -> Self {
Delay {
_data: PhantomData {},
}
}
}
impl crate::traits::delay::Delay for Delay {
type DelayFuture<'a> = impl Future<Output = ()> + 'a;
fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> {
Timer::after(Duration::from_millis(millis))
}
fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> {
Timer::after(Duration::from_micros(micros))
}
}
pub struct TimeoutError; pub struct TimeoutError;
pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> {
let timeout_fut = Timer::after(timeout); let timeout_fut = Timer::after(timeout);