Remove `Started`/`Stopped` type state from `Rtc`
This made it really difficult to enable and disable the RTC on demand with RTIC.
Pass the prescaler as argument to the costructor.
@@ -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.
@@ -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.
@@ -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.
