Skip to content

Commit 63cba63

Browse files
Merge pull request #252 from timokroeger/no-rtc-typestate
Remove `Started`/`Stopped` type state from `Rtc`
2 parents 36ac411 + b519f08 commit 63cba63

File tree

2 files changed

+19
-52
lines changed

2 files changed

+19
-52
lines changed

examples/rtc-demo/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ fn main() -> ! {
2424
let clocks = clocks.start_lfclk();
2525

2626
// Run RTC for 1 second
27-
let mut rtc = Rtc::new(p.RTC0);
27+
let mut rtc = Rtc::new(p.RTC0, 0).unwrap();
2828
rtc.set_compare(RtcCompareReg::Compare0, 32_768).unwrap();
2929
rtc.enable_event(RtcInterrupt::Compare0);
3030

3131
rprintln!("Starting RTC");
32-
let rtc = rtc.enable_counter();
32+
rtc.enable_counter();
3333

3434
rprintln!("Waiting for compare match");
3535
while !rtc.is_event_triggered(RtcInterrupt::Compare0) {}
3636
rtc.reset_event(RtcInterrupt::Compare0);
3737

3838
rprintln!("Compare match, stopping RTC");
39-
let rtc = rtc.disable_counter();
39+
rtc.disable_counter();
4040

4141
rprintln!("Counter stopped at {} ticks", rtc.get_counter());
4242

nrf-hal-common/src/rtc.rs

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,9 @@ use crate::pac::{rtc0, Interrupt, NVIC, RTC0, RTC1};
1111
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
1212
use crate::pac::RTC2;
1313

14-
// Zero Size Type State structs
15-
16-
/// The RTC has been stopped.
17-
pub struct Stopped;
18-
/// The RTC has been started.
19-
pub struct Started;
20-
2114
/// An opaque high level interface to an RTC peripheral.
22-
pub struct Rtc<T, M> {
15+
pub struct Rtc<T> {
2316
periph: T,
24-
_mode: M,
25-
}
26-
27-
impl<T> Rtc<T, Stopped>
28-
where
29-
T: Instance,
30-
{
31-
pub fn new(rtc: T) -> Self {
32-
Rtc {
33-
periph: rtc,
34-
_mode: Stopped,
35-
}
36-
}
3717
}
3818

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

57-
impl<T, M> Rtc<T, M>
37+
impl<T> Rtc<T>
5838
where
5939
T: Instance,
6040
{
41+
/// Creates a new RTC peripheral instance with a 12 bits prescaler.
42+
/// fRTC = 32_768 / (`prescaler` + 1 )
43+
pub fn new(rtc: T, prescaler: u32) -> Result<Self, Error> {
44+
if prescaler >= (1 << 12) {
45+
return Err(Error::PrescalerOutOfRange);
46+
}
47+
48+
unsafe { rtc.prescaler.write(|w| w.bits(prescaler)) };
49+
50+
Ok(Rtc { periph: rtc })
51+
}
52+
6153
/// Enable/start the Real Time Counter.
62-
pub fn enable_counter(self) -> Rtc<T, Started> {
54+
pub fn enable_counter(&self) {
6355
unsafe {
6456
self.periph.tasks_start.write(|w| w.bits(1));
6557
}
66-
Rtc {
67-
periph: self.periph,
68-
_mode: Started,
69-
}
7058
}
7159

7260
/// Disable/stop the Real Time Counter.
73-
pub fn disable_counter(self) -> Rtc<T, Stopped> {
61+
pub fn disable_counter(&self) {
7462
unsafe {
7563
self.periph.tasks_stop.write(|w| w.bits(1));
7664
}
77-
Rtc {
78-
periph: self.periph,
79-
_mode: Stopped,
80-
}
8165
}
8266

8367
/// Enable the generation of a hardware interrupt from a given stimulus.
@@ -234,23 +218,6 @@ pub enum Error {
234218
CompareOutOfRange,
235219
}
236220

237-
impl<T> Rtc<T, Stopped>
238-
where
239-
T: Instance,
240-
{
241-
/// Set the prescaler for the RTC peripheral. 12 bits of range.
242-
/// fRTC = 32_768 / (`prescaler` + 1 )
243-
pub fn set_prescaler(&mut self, prescaler: u32) -> Result<(), Error> {
244-
if prescaler >= (1 << 12) {
245-
return Err(Error::PrescalerOutOfRange);
246-
}
247-
248-
unsafe { self.periph.prescaler.write(|w| w.bits(prescaler)) };
249-
250-
Ok(())
251-
}
252-
}
253-
254221
/// Implemented by all RTC instances.
255222
pub trait Instance: Deref<Target = rtc0::RegisterBlock> {
256223
/// The interrupt associated with this RTC instance.

0 commit comments

Comments
 (0)