timer.rs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #[cfg(feature = "singleton")]
  2. use crate::util::AmoOnceRef;
  3. /// Timer programmer support
  4. pub trait Timer: Send + Sync {
  5. /// Programs the clock for next event after `stime_value` time.
  6. ///
  7. /// `stime_value` is in absolute time. This function must clear the pending timer interrupt bit as well.
  8. ///
  9. /// If the supervisor wishes to clear the timer interrupt without scheduling the next timer event,
  10. /// it can either request a timer interrupt infinitely far into the future (i.e., (uint64_t)-1),
  11. /// or it can instead mask the timer interrupt by clearing `sie.STIE` CSR bit.
  12. fn set_timer(&self, stime_value: u64);
  13. }
  14. #[cfg(feature = "singleton")]
  15. static TIMER: AmoOnceRef<dyn Timer> = AmoOnceRef::new();
  16. #[cfg(feature = "singleton")]
  17. /// Init TIMER module
  18. pub fn init_timer(timer: &'static dyn Timer) {
  19. if !TIMER.try_call_once(timer) {
  20. panic!("load sbi module when already loaded")
  21. }
  22. }
  23. #[cfg(feature = "singleton")]
  24. #[inline]
  25. pub(crate) fn probe_timer() -> bool {
  26. TIMER.get().is_some()
  27. }
  28. #[cfg(feature = "singleton")]
  29. #[inline]
  30. pub(crate) fn set_timer(time_value: u64) -> bool {
  31. if let Some(timer) = TIMER.get() {
  32. timer.set_timer(time_value);
  33. true
  34. } else {
  35. false
  36. }
  37. }