Skip to content

Commit 8ef2415

Browse files
authored
Support TIM3 on more devices. (#308)
* Support TIM3 on more devices. * Allow cancelling timers.
1 parent 26e0917 commit 8ef2415

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

src/timer.rs

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! Timers
22
3-
use crate::hal::timer::{CountDown, Periodic};
4-
// missing PAC support
5-
/*
3+
use core::convert::Infallible;
4+
5+
use crate::hal::timer::{Cancel, CountDown, Periodic};
6+
67
#[cfg(any(
7-
feature = "stm32l451",
8+
// feature = "stm32l451",
89
feature = "stm32l452",
910
feature = "stm32l462",
10-
feature = "stm32l471",
11+
// feature = "stm32l471",
1112
feature = "stm32l475",
1213
feature = "stm32l476",
1314
feature = "stm32l485",
@@ -20,11 +21,11 @@ use crate::hal::timer::{CountDown, Periodic};
2021
// feature = "stm32l4s5",
2122
// feature = "stm32l4r7",
2223
// feature = "stm32l4s7",
23-
feature = "stm32l4r9",
24-
feature = "stm32l4s9",
24+
// feature = "stm32l4r9",
25+
// feature = "stm32l4s9",
2526
))]
2627
use crate::stm32::TIM3;
27-
*/
28+
2829
#[cfg(not(any(
2930
feature = "stm32l412",
3031
feature = "stm32l422",
@@ -88,17 +89,16 @@ macro_rules! hal {
8889
type Time = Hertz;
8990

9091
// NOTE(allow) `w.psc().bits()` is safe for TIM{6,7} but not for TIM{2,3,4} due to
91-
// some SVD omission
92+
// some SVD omission.
9293
#[allow(unused_unsafe)]
9394
fn start<T>(&mut self, timeout: T)
9495
where
9596
T: Into<Hertz>,
9697
{
97-
// pause
98-
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
98+
self.pause();
9999

100100
self.timeout = timeout.into();
101-
let ticks = self.clocks.pclk1() / self.timeout; // TODO check pclk that timer is on
101+
let ticks = self.clocks.pclk1() / self.timeout; // TODO: Check pclk that timer is on.
102102
let psc = u16((ticks - 1) / (1 << 16)).unwrap();
103103

104104
self.tim.psc.write(|w| unsafe { w.psc().bits(psc) });
@@ -107,14 +107,15 @@ macro_rules! hal {
107107

108108
self.tim.arr.write(|w| unsafe { w.bits(u32(arr)) });
109109

110-
// Trigger an update event to load the prescaler value to the clock
110+
// Trigger an update event to load the prescaler value to the clock.
111111
self.tim.egr.write(|w| w.ug().set_bit());
112+
112113
// The above line raises an update event which will indicate
113114
// that the timer is already finished. Since this is not the case,
114-
// it should be cleared
115+
// it should be cleared.
115116
self.clear_update_interrupt_flag();
116117

117-
// start counter
118+
// Start counter.
118119
self.tim.cr1.modify(|_, w| w.cen().set_bit());
119120
}
120121

@@ -128,6 +129,17 @@ macro_rules! hal {
128129
}
129130
}
130131

132+
impl Cancel for Timer<$TIM> {
133+
type Error = Infallible;
134+
135+
fn cancel(&mut self) -> Result<(), Self::Error> {
136+
self.pause();
137+
self.reset();
138+
139+
Ok(())
140+
}
141+
}
142+
131143
impl Timer<$TIM> {
132144
// XXX(why not name this `new`?) bummer: constructors need to have different names
133145
// even if the `$TIM` are non overlapping (compare to the `free` function below
@@ -171,7 +183,7 @@ macro_rules! hal {
171183
let max = core::$width::MAX;
172184
tim.arr.write(|w| unsafe { w.bits(max.into()) });
173185

174-
// Trigger an update event to load the prescaler value to the clock
186+
// Trigger an update event to load the prescaler value to the clock.
175187
tim.egr.write(|w| w.ug().set_bit());
176188

177189

@@ -180,7 +192,7 @@ macro_rules! hal {
180192
// it should be cleared
181193
tim.sr.modify(|_, w| w.uif().clear_bit());
182194

183-
// start counter
195+
// Start counter.
184196
tim.cr1.modify(|_, w| {
185197
w.cen().set_bit();
186198

@@ -204,17 +216,16 @@ macro_rules! hal {
204216
pub fn listen(&mut self, event: Event) {
205217
match event {
206218
Event::TimeOut => {
207-
// Enable update event interrupt
219+
// Enable update event interrupt.
208220
self.tim.dier.write(|w| w.uie().set_bit());
209221
}
210222
}
211223
}
212224

213-
214225
/// Clears interrupt associated with `event`.
215226
///
216-
/// If the interrupt is not cleared, it will immediately retrigger after
217-
/// the ISR has finished.
227+
/// If the interrupt is not cleared, it will immediately
228+
/// retrigger after the ISR has finished.
218229
pub fn clear_interrupt(&mut self, event: Event) {
219230
match event {
220231
Event::TimeOut => {
@@ -224,8 +235,7 @@ macro_rules! hal {
224235
}
225236
}
226237

227-
228-
/// Stops listening for an `event`
238+
/// Stops listening for an `event`.
229239
pub fn unlisten(&mut self, event: Event) {
230240
match event {
231241
Event::TimeOut => {
@@ -235,7 +245,7 @@ macro_rules! hal {
235245
}
236246
}
237247

238-
/// Clears Update Interrupt Flag
248+
/// Clear the update interrupt flag.
239249
pub fn clear_update_interrupt_flag(&mut self) {
240250
self.tim.sr.modify(|_, w| w.uif().clear_bit());
241251
}
@@ -246,10 +256,19 @@ macro_rules! hal {
246256
cnt.cnt().bits()
247257
}
248258

249-
/// Releases the TIM peripheral
250-
pub fn free(self) -> $TIM {
251-
// pause counter
259+
/// Pause the counter.
260+
pub fn pause(&mut self) {
252261
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
262+
}
263+
264+
/// Reset the counter.
265+
pub fn reset(&mut self) {
266+
self.tim.cnt.modify(|_, w| unsafe { w.bits(0) });
267+
}
268+
269+
/// Releases the TIM peripheral.
270+
pub fn free(mut self) -> $TIM {
271+
self.pause();
253272
self.tim
254273
}
255274
}
@@ -265,14 +284,11 @@ hal! {
265284
TIM16: (tim16, free_running_tim16, APB2, u16),
266285
}
267286

268-
// missing PAC support
269-
// RCC_APB1RSTR1->TIM3RST not defined
270-
/*
271287
#[cfg(any(
272-
feature = "stm32l451",
288+
// feature = "stm32l451",
273289
feature = "stm32l452",
274290
feature = "stm32l462",
275-
feature = "stm32l471",
291+
// feature = "stm32l471",
276292
feature = "stm32l475",
277293
feature = "stm32l476",
278294
feature = "stm32l485",
@@ -285,13 +301,12 @@ hal! {
285301
// feature = "stm32l4s5",
286302
// feature = "stm32l4r7",
287303
// feature = "stm32l4s7",
288-
feature = "stm32l4r9",
289-
feature = "stm32l4s9",
304+
// feature = "stm32l4r9",
305+
// feature = "stm32l4s9",
290306
))]
291307
hal! {
292-
TIM3: (tim3, free_running_tim3, tim3en, tim3rst, APB1R1, u32),
308+
TIM3: (tim3, free_running_tim3, APB1R1, u16),
293309
}
294-
*/
295310

296311
#[cfg(not(any(
297312
feature = "stm32l412",

0 commit comments

Comments
 (0)