aclint.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //! Delay trait implementation for (A)CLINT peripherals
  2. use crate::aclint::mtimer::MTIME;
  3. pub use crate::hal::delay::DelayNs;
  4. /// Delay implementation for (A)CLINT peripherals.
  5. pub struct Delay {
  6. mtime: MTIME,
  7. freq: usize,
  8. }
  9. impl Delay {
  10. /// Creates a new `Delay` instance.
  11. #[inline]
  12. pub const fn new(mtime: MTIME, freq: usize) -> Self {
  13. Self { mtime, freq }
  14. }
  15. /// Returns the frequency of the `MTIME` register.
  16. #[inline]
  17. pub const fn get_freq(&self) -> usize {
  18. self.freq
  19. }
  20. /// Sets the frequency of the `MTIME` register.
  21. #[inline]
  22. pub fn set_freq(&mut self, freq: usize) {
  23. self.freq = freq;
  24. }
  25. /// Returns the `MTIME` register.
  26. #[inline]
  27. pub const fn get_mtime(&self) -> MTIME {
  28. self.mtime
  29. }
  30. }
  31. impl DelayNs for Delay {
  32. #[inline]
  33. fn delay_ns(&mut self, ns: u32) {
  34. let t0 = self.mtime.read();
  35. let ns_64: u64 = ns.into();
  36. let n_ticks = ns_64 * self.freq as u64 / 1_000_000_000;
  37. while self.mtime.read().wrapping_sub(t0) < n_ticks {}
  38. }
  39. }