Skip to content

Commit 1b94b42

Browse files
careyk007hannobraun
authored andcommitted
Adding support for periodic timers
1 parent b230244 commit 1b94b42

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

nrf52-hal-common/src/timer.rs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ use void::{unreachable, Void};
1717
#[cfg(any(feature = "52832", feature = "52840"))]
1818
use crate::target::{TIMER3, TIMER4};
1919

20+
use core::marker::PhantomData;
21+
22+
pub struct OneShotTimer;
23+
pub struct PeriodicTimer;
24+
2025

2126
/// Interface to a TIMER instance
2227
///
@@ -25,15 +30,13 @@ use crate::target::{TIMER3, TIMER4};
2530
///
2631
/// CC[0] is used for the current/most-recent delay period and CC[1] is used
2732
/// to grab the current value of the counter at a given instant.
28-
pub struct Timer<T>(T);
33+
pub struct Timer<T, U>(T, PhantomData<U>);
2934

30-
impl<T> Timer<T>
35+
impl<T> Timer<T, OneShotTimer>
3136
where
3237
T: Instance,
3338
{
34-
pub const TICKS_PER_SECOND: u32 = 1_000_000;
35-
36-
pub fn new(timer: T) -> Self {
39+
pub fn new(timer: T) -> Timer<T, OneShotTimer> {
3740
timer
3841
.shorts
3942
.write(|w| w.compare0_clear().enabled().compare0_stop().enabled());
@@ -42,9 +45,47 @@ where
4245
);
4346
timer.bitmode.write(|w| w.bitmode()._32bit());
4447

45-
Timer(timer)
48+
Timer::<T, OneShotTimer>(timer, PhantomData)
4649
}
4750

51+
pub fn into_periodic(self) -> Timer<T, PeriodicTimer> {
52+
self.0
53+
.shorts
54+
.write(|w| w.compare0_clear().enabled().compare0_stop().disabled());
55+
Timer::<T, PeriodicTimer>(self.free(), PhantomData)
56+
}
57+
}
58+
59+
impl<T> Timer<T, PeriodicTimer>
60+
where
61+
T: Instance,
62+
{
63+
pub fn new_periodic(timer: T) -> Timer<T, PeriodicTimer> {
64+
timer
65+
.shorts
66+
.write(|w| w.compare0_clear().enabled().compare0_stop().disabled());
67+
timer.prescaler.write(
68+
|w| unsafe { w.prescaler().bits(4) }, // 1 MHz
69+
);
70+
timer.bitmode.write(|w| w.bitmode()._32bit());
71+
72+
Timer::<T, PeriodicTimer>(timer, PhantomData)
73+
}
74+
75+
pub fn into_oneshot(self) -> Timer<T, OneShotTimer> {
76+
self.0
77+
.shorts
78+
.write(|w| w.compare0_clear().enabled().compare0_stop().enabled());
79+
Timer::<T, OneShotTimer>(self.free(), PhantomData)
80+
}
81+
}
82+
83+
impl<T, U> Timer<T, U>
84+
where
85+
T: Instance,
86+
{
87+
pub const TICKS_PER_SECOND: u32 = 1_000_000;
88+
4889
/// Return the raw interface to the underlying timer peripheral
4990
pub fn free(self) -> T {
5091
self.0
@@ -103,7 +144,7 @@ where
103144
}
104145
}
105146

106-
impl<T> timer::CountDown for Timer<T>
147+
impl<T, U> timer::CountDown for Timer<T, U>
107148
where
108149
T: Instance,
109150
{
@@ -164,7 +205,7 @@ where
164205
}
165206
}
166207

167-
impl<T> timer::Cancel for Timer<T>
208+
impl<T, U> timer::Cancel for Timer<T, U>
168209
where
169210
T: Instance,
170211
{
@@ -178,6 +219,11 @@ where
178219
}
179220
}
180221

222+
impl<T> timer::Periodic for Timer<T, PeriodicTimer>
223+
where
224+
T: Instance,
225+
{}
226+
181227

182228
/// Implemented by all `TIMER` instances
183229
pub trait Instance: Deref<Target = timer0::RegisterBlock> {

nrf52-hal-common/src/uarte.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::gpio::{
3333
Input,
3434
Floating,
3535
};
36-
use crate::timer::{self, Timer};
36+
use crate::timer::{self, Timer, OneShotTimer};
3737

3838
// Re-export SVD variants to allow user to directly set values
3939
pub use uarte0::{
@@ -236,7 +236,7 @@ impl<T> Uarte<T> where T: Instance {
236236
pub fn read_timeout<I>(
237237
&mut self,
238238
rx_buffer: &mut [u8],
239-
timer: &mut Timer<I>,
239+
timer: &mut Timer<I, OneShotTimer>,
240240
cycles: u32
241241
) -> Result<(), Error> where I: timer::Instance
242242
{

0 commit comments

Comments
 (0)