diff --git a/examples/rtc-demo/src/main.rs b/examples/rtc-demo/src/main.rs index 20d94053..a462b658 100644 --- a/examples/rtc-demo/src/main.rs +++ b/examples/rtc-demo/src/main.rs @@ -24,19 +24,19 @@ fn main() -> ! { let clocks = clocks.start_lfclk(); // Run RTC for 1 second - let mut rtc = Rtc::new(p.RTC0); + let mut rtc = Rtc::new(p.RTC0, 0).unwrap(); rtc.set_compare(RtcCompareReg::Compare0, 32_768).unwrap(); rtc.enable_event(RtcInterrupt::Compare0); rprintln!("Starting RTC"); - let rtc = rtc.enable_counter(); + rtc.enable_counter(); rprintln!("Waiting for compare match"); while !rtc.is_event_triggered(RtcInterrupt::Compare0) {} rtc.reset_event(RtcInterrupt::Compare0); rprintln!("Compare match, stopping RTC"); - let rtc = rtc.disable_counter(); + rtc.disable_counter(); rprintln!("Counter stopped at {} ticks", rtc.get_counter()); diff --git a/nrf-hal-common/src/rtc.rs b/nrf-hal-common/src/rtc.rs index b09b8de1..a9096caa 100644 --- a/nrf-hal-common/src/rtc.rs +++ b/nrf-hal-common/src/rtc.rs @@ -11,29 +11,9 @@ use crate::pac::{rtc0, Interrupt, NVIC, RTC0, RTC1}; #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] use crate::pac::RTC2; -// Zero Size Type State structs - -/// The RTC has been stopped. -pub struct Stopped; -/// The RTC has been started. -pub struct Started; - /// An opaque high level interface to an RTC peripheral. -pub struct Rtc { +pub struct Rtc { periph: T, - _mode: M, -} - -impl Rtc -where - T: Instance, -{ - pub fn new(rtc: T) -> Self { - Rtc { - periph: rtc, - _mode: Stopped, - } - } } /// Interrupts/Events that can be generated by the RTCn peripheral. @@ -54,30 +34,34 @@ pub enum RtcCompareReg { Compare3, } -impl Rtc +impl Rtc where T: Instance, { + /// Creates a new RTC peripheral instance with a 12 bits prescaler. + /// fRTC = 32_768 / (`prescaler` + 1 ) + pub fn new(rtc: T, prescaler: u32) -> Result { + if prescaler >= (1 << 12) { + return Err(Error::PrescalerOutOfRange); + } + + unsafe { rtc.prescaler.write(|w| w.bits(prescaler)) }; + + Ok(Rtc { periph: rtc }) + } + /// Enable/start the Real Time Counter. - pub fn enable_counter(self) -> Rtc { + pub fn enable_counter(&self) { unsafe { self.periph.tasks_start.write(|w| w.bits(1)); } - Rtc { - periph: self.periph, - _mode: Started, - } } /// Disable/stop the Real Time Counter. - pub fn disable_counter(self) -> Rtc { + pub fn disable_counter(&self) { unsafe { self.periph.tasks_stop.write(|w| w.bits(1)); } - Rtc { - periph: self.periph, - _mode: Stopped, - } } /// Enable the generation of a hardware interrupt from a given stimulus. @@ -234,23 +218,6 @@ pub enum Error { CompareOutOfRange, } -impl Rtc -where - T: Instance, -{ - /// Set the prescaler for the RTC peripheral. 12 bits of range. - /// fRTC = 32_768 / (`prescaler` + 1 ) - pub fn set_prescaler(&mut self, prescaler: u32) -> Result<(), Error> { - if prescaler >= (1 << 12) { - return Err(Error::PrescalerOutOfRange); - } - - unsafe { self.periph.prescaler.write(|w| w.bits(prescaler)) }; - - Ok(()) - } -} - /// Implemented by all RTC instances. pub trait Instance: Deref { /// The interrupt associated with this RTC instance.