Quellcode durchsuchen

concise implementation of e-h

Román Cárdenas vor 1 Jahr
Ursprung
Commit
147285e8a0
3 geänderte Dateien mit 22 neuen und 53 gelöschten Zeilen
  1. 4 0
      .github/workflows/clippy.yml
  2. 3 5
      src/hal/aclint.rs
  3. 15 48
      src/hal_async/aclint.rs

+ 4 - 0
.github/workflows/clippy.yml

@@ -19,6 +19,10 @@ jobs:
           # Nightly is only for reference and allowed to fail
           - toolchain: nightly
             experimental: true
+          # async traits are still not supported in stable
+          - toolchain: stable
+            cargo_flags: --all-features
+            experimental: true
     runs-on: ubuntu-latest
     continue-on-error: ${{ matrix.experimental || false }}
     steps:

+ 3 - 5
src/hal/aclint.rs

@@ -38,10 +38,8 @@ impl Delay {
 impl DelayUs for Delay {
     #[inline]
     fn delay_us(&mut self, us: u32) {
-        let time_from = self.mtime.read();
-        let time_to = time_from.wrapping_add(us as u64 * self.freq as u64 / 1_000_000);
-
-        while time_to < self.mtime.read() {} // wait for overflow
-        while time_to > self.mtime.read() {} // wait for time to pass
+        let t0 = self.mtime.read();
+        let n_ticks = us as u64 * self.freq as u64 / 1_000_000;
+        while self.mtime.read().wrapping_sub(t0) < n_ticks {}
     }
 }

+ 15 - 48
src/hal_async/aclint.rs

@@ -4,63 +4,32 @@ use crate::aclint::mtimer::MTIME;
 pub use crate::hal::aclint::Delay;
 pub use crate::hal_async::delay::DelayUs;
 use core::{
-    cmp::Ordering,
     future::Future,
     pin::Pin,
     task::{Context, Poll},
 };
 
-enum DelayAsyncState {
-    WaitOverflow(u64),
-    Wait(u64),
-    Ready,
-}
-
-struct FSMDelay {
+struct DelayAsync {
     mtime: MTIME,
-    state: DelayAsyncState,
+    t0: u64,
+    n_ticks: u64,
 }
 
-impl FSMDelay {
-    pub fn new(n_ticks: u64, mtime: MTIME) -> Self {
-        let t_from = mtime.read();
-        let t_to = t_from.wrapping_add(n_ticks);
-
-        let state = match t_to.cmp(&t_from) {
-            Ordering::Less => DelayAsyncState::WaitOverflow(t_to),
-            Ordering::Greater => DelayAsyncState::Wait(t_to),
-            Ordering::Equal => DelayAsyncState::Ready,
-        };
-
-        Self { mtime, state }
+impl DelayAsync {
+    pub fn new(mtime: MTIME, n_ticks: u64) -> Self {
+        let t0 = mtime.read();
+        Self { mtime, t0, n_ticks }
     }
 }
 
-impl Future for FSMDelay {
+impl Future for DelayAsync {
     type Output = ();
 
-    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
-        match self.state {
-            DelayAsyncState::WaitOverflow(t_to) => match t_to.cmp(&self.mtime.read()) {
-                Ordering::Less => Poll::Pending,
-                Ordering::Greater => {
-                    self.state = DelayAsyncState::Wait(t_to);
-                    Poll::Pending
-                }
-                Ordering::Equal => {
-                    self.state = DelayAsyncState::Ready;
-                    Poll::Ready(())
-                }
-            },
-            DelayAsyncState::Wait(t_to) => {
-                if self.mtime.read() < t_to {
-                    Poll::Pending
-                } else {
-                    self.state = DelayAsyncState::Ready;
-                    Poll::Ready(())
-                }
-            }
-            DelayAsyncState::Ready => Poll::Ready(()),
+    #[inline]
+    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
+        match self.mtime.read().wrapping_sub(self.t0) < self.n_ticks {
+            true => Poll::Pending,
+            false => Poll::Ready(()),
         }
     }
 }
@@ -69,14 +38,12 @@ impl DelayUs for Delay {
     #[inline]
     async fn delay_us(&mut self, us: u32) {
         let n_ticks = us as u64 * self.get_freq() as u64 / 1_000_000;
-        let state = FSMDelay::new(n_ticks, self.get_mtime());
-        state.await;
+        DelayAsync::new(self.get_mtime(), n_ticks).await;
     }
 
     #[inline]
     async fn delay_ms(&mut self, ms: u32) {
         let n_ticks = ms as u64 * self.get_freq() as u64 / 1_000;
-        let state = FSMDelay::new(n_ticks, self.get_mtime());
-        state.await;
+        DelayAsync::new(self.get_mtime(), n_ticks).await;
     }
 }