Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Started/Stopped type state from Rtc #252

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/rtc-demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
65 changes: 16 additions & 49 deletions nrf-hal-common/src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, M> {
pub struct Rtc<T> {
periph: T,
_mode: M,
}

impl<T> Rtc<T, Stopped>
where
T: Instance,
{
pub fn new(rtc: T) -> Self {
Rtc {
periph: rtc,
_mode: Stopped,
}
}
}

/// Interrupts/Events that can be generated by the RTCn peripheral.
Expand All @@ -54,30 +34,34 @@ pub enum RtcCompareReg {
Compare3,
}

impl<T, M> Rtc<T, M>
impl<T> Rtc<T>
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<Self, Error> {
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<T, Started> {
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<T, Stopped> {
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.
Expand Down Expand Up @@ -234,23 +218,6 @@ pub enum Error {
CompareOutOfRange,
}

impl<T> Rtc<T, Stopped>
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<Target = rtc0::RegisterBlock> {
/// The interrupt associated with this RTC instance.
Expand Down