Skip to content

Commit 07344d8

Browse files
careyk007hannobraun
authored andcommitted
reverting to typestate approach
1 parent 6293d35 commit 07344d8

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

nrf52-hal-common/src/timer.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,13 @@ pub struct Periodic;
3030
///
3131
/// CC[0] is used for the current/most-recent delay period and CC[1] is used
3232
/// to grab the current value of the counter at a given instant.
33-
pub struct BaseTimer<T, U>(T, PhantomData<U>);
33+
pub struct Timer<T, U = OneShot>(T, PhantomData<U>);
3434

35-
pub type Timer<T> = BaseTimer<T, OneShot>;
36-
pub type PeriodicTimer<T> = BaseTimer<T, Periodic>;
37-
pub type OneShotTimer<T> = BaseTimer<T, OneShot>;
38-
39-
impl<T> OneShotTimer<T>
40-
where
35+
impl<T> Timer<T, OneShot>
36+
where
4137
T: Instance,
4238
{
43-
pub fn new(timer: T) -> OneShotTimer<T> {
39+
pub fn one_shot(timer: T) -> Timer<T, OneShot> {
4440
timer
4541
.shorts
4642
.write(|w| w.compare0_clear().enabled().compare0_stop().enabled());
@@ -49,23 +45,20 @@ where
4945
);
5046
timer.bitmode.write(|w| w.bitmode()._32bit());
5147

52-
BaseTimer::<T, OneShot>(timer, PhantomData)
48+
Timer::<T, OneShot>(timer, PhantomData)
5349
}
5450

55-
pub fn into_periodic(self) -> PeriodicTimer<T> {
56-
self.0
57-
.shorts
58-
.write(|w| w.compare0_clear().enabled().compare0_stop().disabled());
59-
60-
BaseTimer::<T, Periodic>(self.free(), PhantomData)
51+
pub fn new(timer: T) -> Timer<T, OneShot> {
52+
Timer::<T, OneShot>::one_shot(timer)
6153
}
54+
6255
}
6356

64-
impl<T> PeriodicTimer<T>
57+
impl<T> Timer<T, Periodic>
6558
where
6659
T: Instance,
6760
{
68-
pub fn new(timer: T) -> PeriodicTimer<T> {
61+
pub fn periodic(timer: T) -> Timer<T, Periodic> {
6962
timer
7063
.shorts
7164
.write(|w| w.compare0_clear().enabled().compare0_stop().disabled());
@@ -74,24 +67,33 @@ where
7467
);
7568
timer.bitmode.write(|w| w.bitmode()._32bit());
7669

77-
BaseTimer::<T, Periodic>(timer, PhantomData)
70+
Timer::<T, Periodic>(timer, PhantomData)
7871
}
7972

80-
pub fn into_oneshot(self) -> OneShotTimer<T> {
81-
self.0
82-
.shorts
83-
.write(|w| w.compare0_clear().enabled().compare0_stop().enabled());
84-
85-
BaseTimer::<T, OneShot>(self.free(), PhantomData)
86-
}
8773
}
8874

89-
impl<T, U> BaseTimer<T, U>
75+
impl<T, U> Timer<T, U>
9076
where
9177
T: Instance,
9278
{
9379
pub const TICKS_PER_SECOND: u32 = 1_000_000;
9480

81+
pub fn into_periodic(self) -> Timer<T, Periodic> {
82+
self.0
83+
.shorts
84+
.write(|w| w.compare0_clear().enabled().compare0_stop().disabled());
85+
86+
Timer::<T, Periodic>(self.free(), PhantomData)
87+
}
88+
89+
pub fn into_oneshot(self) -> Timer<T, OneShot> {
90+
self.0
91+
.shorts
92+
.write(|w| w.compare0_clear().enabled().compare0_stop().enabled());
93+
94+
Timer::<T, OneShot>(self.free(), PhantomData)
95+
}
96+
9597
/// Return the raw interface to the underlying timer peripheral
9698
pub fn free(self) -> T {
9799
self.0
@@ -150,7 +152,7 @@ where
150152
}
151153
}
152154

153-
impl<T, U> timer::CountDown for BaseTimer<T, U>
155+
impl<T, U> timer::CountDown for Timer<T, U>
154156
where
155157
T: Instance,
156158
{
@@ -211,7 +213,7 @@ where
211213
}
212214
}
213215

214-
impl<T, U> timer::Cancel for BaseTimer<T, U>
216+
impl<T, U> timer::Cancel for Timer<T, U>
215217
where
216218
T: Instance,
217219
{
@@ -225,7 +227,7 @@ where
225227
}
226228
}
227229

228-
impl<T> timer::Periodic for PeriodicTimer<T>
230+
impl<T> timer::Periodic for Timer<T, Periodic>
229231
where
230232
T: Instance,
231233
{}

0 commit comments

Comments
 (0)