Skip to content

Commit 82de921

Browse files
committed
store waker
1 parent 36a264c commit 82de921

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

.github/workflows/clippy.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ jobs:
2929
- name: Run clippy (no features)
3030
run: cargo clippy --all --no-default-features -- -D warnings
3131
- name: Run clippy (all features)
32-
run: cargo clippy --all --all-features -- -D warnings
32+
# We exclude riscv-peripheral because it's not yet stable-compliant
33+
run: cargo clippy --exclude riscv-peripheral --all --all-features -- -D warnings
3334

3435
# Additonal clippy checks for riscv-rt
3536
clippy-riscv-rt:

.github/workflows/riscv-peripheral.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ jobs:
3333
targets: ${{ matrix.target }}
3434
- name: Build (no features)
3535
run: cargo build --package riscv-peripheral --target ${{ matrix.target }}
36-
- name: Build (all features)
37-
run: cargo build --package riscv-peripheral --target ${{ matrix.target }} --all-features
36+
# not yet, let's wait for 1.75.0
37+
# - name: Build (all features)
38+
# run: cargo build --package riscv-peripheral --target ${{ matrix.target }} --all-features
3839

3940
# On MacOS, Ubuntu, and Windows, we run the tests.
4041
build-others:
@@ -47,8 +48,9 @@ jobs:
4748
- uses: dtolnay/rust-toolchain@stable
4849
- name: Build (no features)
4950
run: cargo test --package riscv-peripheral
50-
- name: Build (all features)
51-
run: cargo test --package riscv-peripheral --all-features
51+
# not yet, let's wait for 1.75.0
52+
# - name: Build (all features)
53+
# run: cargo test --package riscv-peripheral --all-features
5254

5355
# Job to check that all the builds succeeded
5456
build-check:

riscv-peripheral/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ edition = "2021"
77

88
[dependencies]
99
embedded-hal = "1.0.0-rc.2"
10-
# embedded-hal-async = { version = "1.0.0-rc.1", optional = true }
10+
embedded-hal-async = { version = "1.0.0-rc.2", optional = true }
1111
riscv = { path = "../riscv", version = "0.10" }
1212
riscv-pac = { path = "../riscv-pac", version = "0.1.0" }
1313

1414
[features]
15-
# hal-async = ["embedded-hal-async"]
15+
hal-async = ["embedded-hal-async"]
1616

1717
[package.metadata.docs.rs]
1818
default-target = "riscv64imac-unknown-none-elf"

riscv-peripheral/src/hal_async/aclint.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,62 @@
22
33
use crate::aclint::mtimer::MTIME;
44
pub use crate::hal::aclint::Delay;
5-
pub use crate::hal_async::delay::DelayUs;
5+
pub use crate::hal_async::delay::DelayNs;
66
use core::{
77
future::Future,
88
pin::Pin,
9-
task::{Context, Poll},
9+
task::{Context, Poll, Waker},
1010
};
1111

1212
struct DelayAsync {
1313
mtime: MTIME,
1414
t0: u64,
1515
n_ticks: u64,
16+
waker: Option<Waker>,
1617
}
1718

1819
impl DelayAsync {
1920
pub fn new(mtime: MTIME, n_ticks: u64) -> Self {
2021
let t0 = mtime.read();
21-
Self { mtime, t0, n_ticks }
22+
Self {
23+
mtime,
24+
t0,
25+
n_ticks,
26+
waker: None,
27+
}
2228
}
2329
}
2430

2531
impl Future for DelayAsync {
2632
type Output = ();
2733

2834
#[inline]
29-
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
35+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
3036
match self.mtime.read().wrapping_sub(self.t0) < self.n_ticks {
31-
true => Poll::Pending,
32-
false => Poll::Ready(()),
37+
true => {
38+
self.get_mut().waker = Some(cx.waker().clone());
39+
Poll::Pending
40+
}
41+
false => {
42+
if let Some(waker) = self.get_mut().waker.take() {
43+
waker.wake();
44+
} else {
45+
// corner case: delay expired before polling for the first time
46+
cx.waker().wake_by_ref();
47+
};
48+
Poll::Ready(())
49+
}
3350
}
3451
}
3552
}
3653

37-
impl DelayUs for Delay {
54+
impl DelayNs for Delay {
55+
#[inline]
56+
async fn delay_ns(&mut self, ns: u32) {
57+
let n_ticks = ns as u64 * self.get_freq() as u64 / 1_000_000_000;
58+
DelayAsync::new(self.get_mtime(), n_ticks).await;
59+
}
60+
3861
#[inline]
3962
async fn delay_us(&mut self, us: u32) {
4063
let n_ticks = us as u64 * self.get_freq() as u64 / 1_000_000;

riscv-peripheral/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub use riscv; // re-export riscv crate to allow macros to use it
77

88
pub mod common; // common definitions for all peripherals
99
pub mod hal; // trait implementations for embedded-hal
10-
// #[cfg(feature = "hal-async")]
11-
// pub mod hal_async; // async trait implementations for embedded-hal
10+
#[cfg(feature = "hal-async")]
11+
pub mod hal_async; // async trait implementations for embedded-hal
1212
pub mod macros; // macros for easing the definition of peripherals in PACs
1313

1414
pub mod aclint; // ACLINT and CLINT peripherals

0 commit comments

Comments
 (0)