diff --git a/esp-hal-common/src/systimer.rs b/esp-hal-common/src/systimer.rs index 5dbf46d71e..8745ba4bc6 100644 --- a/esp-hal-common/src/systimer.rs +++ b/esp-hal-common/src/systimer.rs @@ -16,59 +16,49 @@ use crate::pac::SYSTIMER; #[derive(Debug)] pub struct SystemTimer { - inner: SYSTIMER, - alrm0: Option>, - alrm1: Option>, - alrm2: Option>, + _inner: SYSTIMER, + pub alarm0: Alarm, + pub alarm1: Alarm, + pub alarm2: Alarm, } impl SystemTimer { pub fn new(p: SYSTIMER) -> Self { - // TODO enable Self { - inner: p, - alrm0: Some(Alarm::new()), - alrm1: Some(Alarm::new()), - alrm2: Some(Alarm::new()), + _inner: p, + alarm0: Alarm::new(), + alarm1: Alarm::new(), + alarm2: Alarm::new(), } } // TODO use fugit types - pub fn now(&self) -> u64 { - self.inner + pub fn now() -> u64 { + // This should be safe to access from multiple contexts + // worst case scenario the second accesor ends up reading + // an older time stamp + let systimer = unsafe { &*SYSTIMER::ptr() }; + systimer .unit0_op - .write(|w| w.timer_unit0_update().set_bit()); + .modify(|_, w| w.timer_unit0_update().set_bit()); - while !self - .inner + while !systimer .unit0_op .read() .timer_unit0_value_valid() .bit_is_set() {} - let value_lo = self.inner.unit0_value_lo.read().bits(); - let value_hi = self.inner.unit0_value_hi.read().bits(); + let value_lo = systimer.unit0_value_lo.read().bits(); + let value_hi = systimer.unit0_value_hi.read().bits(); ((value_hi as u64) << 32) | value_lo as u64 } - - pub fn alarm0(&mut self) -> Option> { - self.alrm0.take() - } - - pub fn alarm1(&mut self) -> Option> { - self.alrm1.take() - } - - pub fn alarm2(&mut self) -> Option> { - self.alrm2.take() - } } #[derive(Debug)] pub struct Target; -// pub struct Periodic; // TODO +// pub struct Periodic; // TODO, also impl e-h timer traits #[derive(Debug)] pub struct Alarm { diff --git a/esp32c3-hal/examples/systimer.rs b/esp32c3-hal/examples/systimer.rs index d124b14040..1453542987 100644 --- a/esp32c3-hal/examples/systimer.rs +++ b/esp32c3-hal/examples/systimer.rs @@ -42,19 +42,19 @@ fn main() -> ! { writeln!(serial0, "SYSTIMER Demo start!").ok(); - let mut syst = SystemTimer::new(peripherals.SYSTIMER); + let syst = SystemTimer::new(peripherals.SYSTIMER); - writeln!(serial0, "SYSTIMER Current value = {}", syst.now()).ok(); + writeln!(serial0, "SYSTIMER Current value = {}", SystemTimer::now()).ok(); - let alarm0 = syst.alarm0().unwrap(); + let alarm0 = syst.alarm0; alarm0.set_target(40_000_000); alarm0.enable_interrupt(); - let alarm1 = syst.alarm1().unwrap(); + let alarm1 = syst.alarm1; alarm1.set_target(41_111_111); alarm1.enable_interrupt(); - let alarm2 = syst.alarm2().unwrap(); + let alarm2 = syst.alarm2; alarm2.set_target(42_222_222 * 2); alarm2.enable_interrupt(); @@ -123,7 +123,7 @@ pub fn interrupt1() { riscv::interrupt::free(|cs| unsafe { let mut serial = SERIAL.borrow(*cs).borrow_mut(); let serial = serial.as_mut().unwrap(); - writeln!(serial, "Interrupt 1").ok(); + writeln!(serial, "Interrupt 1 = {}", SystemTimer::now()).ok(); let mut alarm = ALARM0.borrow(*cs).borrow_mut(); let alarm = alarm.as_mut().unwrap(); @@ -138,7 +138,7 @@ pub fn interrupt2() { riscv::interrupt::free(|cs| unsafe { let mut serial = SERIAL.borrow(*cs).borrow_mut(); let serial = serial.as_mut().unwrap(); - writeln!(serial, "Interrupt 2").ok(); + writeln!(serial, "Interrupt 2 = {}", SystemTimer::now()).ok(); let mut alarm = ALARM1.borrow(*cs).borrow_mut(); let alarm = alarm.as_mut().unwrap(); @@ -153,7 +153,7 @@ pub fn interrupt3() { riscv::interrupt::free(|cs| unsafe { let mut serial = SERIAL.borrow(*cs).borrow_mut(); let serial = serial.as_mut().unwrap(); - writeln!(serial, "Interrupt 3").ok(); + writeln!(serial, "Interrupt 3 = {}", SystemTimer::now()).ok(); let mut alarm = ALARM2.borrow(*cs).borrow_mut(); let alarm = alarm.as_mut().unwrap();