aclint.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! Asynchronous delay implementation for the (A)CLINT peripheral.
  2. use crate::aclint::mtimer::MTIME;
  3. pub use crate::hal::aclint::Delay;
  4. pub use crate::hal_async::delay::DelayUs;
  5. use core::{
  6. future::Future,
  7. pin::Pin,
  8. task::{Context, Poll},
  9. };
  10. struct DelayAsync {
  11. mtime: MTIME,
  12. t0: u64,
  13. n_ticks: u64,
  14. }
  15. impl DelayAsync {
  16. pub fn new(mtime: MTIME, n_ticks: u64) -> Self {
  17. let t0 = mtime.read();
  18. Self { mtime, t0, n_ticks }
  19. }
  20. }
  21. impl Future for DelayAsync {
  22. type Output = ();
  23. #[inline]
  24. fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
  25. match self.mtime.read().wrapping_sub(self.t0) < self.n_ticks {
  26. true => Poll::Pending,
  27. false => Poll::Ready(()),
  28. }
  29. }
  30. }
  31. impl DelayUs for Delay {
  32. #[inline]
  33. async fn delay_us(&mut self, us: u32) {
  34. let n_ticks = us as u64 * self.get_freq() as u64 / 1_000_000;
  35. DelayAsync::new(self.get_mtime(), n_ticks).await;
  36. }
  37. #[inline]
  38. async fn delay_ms(&mut self, ms: u32) {
  39. let n_ticks = ms as u64 * self.get_freq() as u64 / 1_000;
  40. DelayAsync::new(self.get_mtime(), n_ticks).await;
  41. }
  42. }