Skip to content

Commit 147285e

Browse files
committed
concise implementation of e-h
1 parent 012aa79 commit 147285e

File tree

3 files changed

+22
-53
lines changed

3 files changed

+22
-53
lines changed

.github/workflows/clippy.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ jobs:
1919
# Nightly is only for reference and allowed to fail
2020
- toolchain: nightly
2121
experimental: true
22+
# async traits are still not supported in stable
23+
- toolchain: stable
24+
cargo_flags: --all-features
25+
experimental: true
2226
runs-on: ubuntu-latest
2327
continue-on-error: ${{ matrix.experimental || false }}
2428
steps:

src/hal/aclint.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ impl Delay {
3838
impl DelayUs for Delay {
3939
#[inline]
4040
fn delay_us(&mut self, us: u32) {
41-
let time_from = self.mtime.read();
42-
let time_to = time_from.wrapping_add(us as u64 * self.freq as u64 / 1_000_000);
43-
44-
while time_to < self.mtime.read() {} // wait for overflow
45-
while time_to > self.mtime.read() {} // wait for time to pass
41+
let t0 = self.mtime.read();
42+
let n_ticks = us as u64 * self.freq as u64 / 1_000_000;
43+
while self.mtime.read().wrapping_sub(t0) < n_ticks {}
4644
}
4745
}

src/hal_async/aclint.rs

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,32 @@ use crate::aclint::mtimer::MTIME;
44
pub use crate::hal::aclint::Delay;
55
pub use crate::hal_async::delay::DelayUs;
66
use core::{
7-
cmp::Ordering,
87
future::Future,
98
pin::Pin,
109
task::{Context, Poll},
1110
};
1211

13-
enum DelayAsyncState {
14-
WaitOverflow(u64),
15-
Wait(u64),
16-
Ready,
17-
}
18-
19-
struct FSMDelay {
12+
struct DelayAsync {
2013
mtime: MTIME,
21-
state: DelayAsyncState,
14+
t0: u64,
15+
n_ticks: u64,
2216
}
2317

24-
impl FSMDelay {
25-
pub fn new(n_ticks: u64, mtime: MTIME) -> Self {
26-
let t_from = mtime.read();
27-
let t_to = t_from.wrapping_add(n_ticks);
28-
29-
let state = match t_to.cmp(&t_from) {
30-
Ordering::Less => DelayAsyncState::WaitOverflow(t_to),
31-
Ordering::Greater => DelayAsyncState::Wait(t_to),
32-
Ordering::Equal => DelayAsyncState::Ready,
33-
};
34-
35-
Self { mtime, state }
18+
impl DelayAsync {
19+
pub fn new(mtime: MTIME, n_ticks: u64) -> Self {
20+
let t0 = mtime.read();
21+
Self { mtime, t0, n_ticks }
3622
}
3723
}
3824

39-
impl Future for FSMDelay {
25+
impl Future for DelayAsync {
4026
type Output = ();
4127

42-
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
43-
match self.state {
44-
DelayAsyncState::WaitOverflow(t_to) => match t_to.cmp(&self.mtime.read()) {
45-
Ordering::Less => Poll::Pending,
46-
Ordering::Greater => {
47-
self.state = DelayAsyncState::Wait(t_to);
48-
Poll::Pending
49-
}
50-
Ordering::Equal => {
51-
self.state = DelayAsyncState::Ready;
52-
Poll::Ready(())
53-
}
54-
},
55-
DelayAsyncState::Wait(t_to) => {
56-
if self.mtime.read() < t_to {
57-
Poll::Pending
58-
} else {
59-
self.state = DelayAsyncState::Ready;
60-
Poll::Ready(())
61-
}
62-
}
63-
DelayAsyncState::Ready => Poll::Ready(()),
28+
#[inline]
29+
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
30+
match self.mtime.read().wrapping_sub(self.t0) < self.n_ticks {
31+
true => Poll::Pending,
32+
false => Poll::Ready(()),
6433
}
6534
}
6635
}
@@ -69,14 +38,12 @@ impl DelayUs for Delay {
6938
#[inline]
7039
async fn delay_us(&mut self, us: u32) {
7140
let n_ticks = us as u64 * self.get_freq() as u64 / 1_000_000;
72-
let state = FSMDelay::new(n_ticks, self.get_mtime());
73-
state.await;
41+
DelayAsync::new(self.get_mtime(), n_ticks).await;
7442
}
7543

7644
#[inline]
7745
async fn delay_ms(&mut self, ms: u32) {
7846
let n_ticks = ms as u64 * self.get_freq() as u64 / 1_000;
79-
let state = FSMDelay::new(n_ticks, self.get_mtime());
80-
state.await;
47+
DelayAsync::new(self.get_mtime(), n_ticks).await;
8148
}
8249
}

0 commit comments

Comments
 (0)