Skip to content

Commit f855b6d

Browse files
committed
move CountDownTimer to separate module
1 parent 2d67884 commit f855b6d

File tree

2 files changed

+254
-246
lines changed

2 files changed

+254
-246
lines changed

src/timer.rs

Lines changed: 3 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
//! Pins can be used for PWM output in both push-pull mode (`Alternate`) and open-drain mode
44
//! (`AlternateOD`).
55
6-
use cast::u16;
76
use cortex_m::peripheral::syst::SystClkSource;
87
use cortex_m::peripheral::{DCB, DWT, SYST};
9-
use embedded_hal::timer::{Cancel, CountDown, Periodic};
10-
use fugit::{MicrosDurationU32, TimerDurationU32};
11-
use void::Void;
128

139
use crate::pac::RCC;
1410

@@ -19,47 +15,14 @@ use crate::time::Hertz;
1915
#[cfg(not(feature = "stm32f410"))]
2016
pub mod monotonic;
2117

18+
mod count_down;
19+
pub use count_down::*;
20+
2221
/// Timer wrapper
2322
pub struct Timer<TIM> {
2423
pub(crate) tim: TIM,
2524
pub(crate) clk: Hertz,
2625
}
27-
/// Timer that waits given time
28-
pub struct CountDownTimer<TIM, const FREQ: u32> {
29-
tim: TIM,
30-
}
31-
32-
/// `CountDownTimer` with sampling of 1 MHz
33-
pub type CountDownTimerUs<TIM> = CountDownTimer<TIM, 1_000_000>;
34-
35-
/// `CountDownTimer` with sampling of 1 kHz
36-
///
37-
/// NOTE: don't use this if your system frequency more than 65 MHz
38-
pub type CountDownTimerMs<TIM> = CountDownTimer<TIM, 1_000>;
39-
40-
impl<TIM> Timer<TIM>
41-
where
42-
TIM: Instance,
43-
{
44-
/// Creates CountDownTimer with custom sampling
45-
pub fn count_down<const FREQ: u32>(self) -> CountDownTimer<TIM, FREQ> {
46-
let Self { tim, clk } = self;
47-
CountDownTimer::<TIM, FREQ>::new(tim, clk)
48-
}
49-
/// Creates CountDownTimer with sampling of 1 MHz
50-
pub fn count_down_us(self) -> CountDownTimerUs<TIM> {
51-
self.count_down::<1_000_000>()
52-
}
53-
54-
/// Creates CountDownTimer with sampling of 1 kHz
55-
///
56-
/// NOTE: don't use this if your system frequency more than 65 MHz
57-
pub fn count_down_ms(self) -> CountDownTimerMs<TIM> {
58-
self.count_down::<1_000>()
59-
}
60-
}
61-
62-
impl<TIM, const FREQ: u32> Periodic for CountDownTimer<TIM, FREQ> {}
6326

6427
/// Interrupt events
6528
pub enum Event {
@@ -84,92 +47,11 @@ impl Timer<SYST> {
8447
}
8548
}
8649

87-
/// Creates SysCountDownTimer
88-
pub fn count_down(self) -> SysCountDownTimer {
89-
let Self { tim, clk } = self;
90-
SysCountDownTimer { tim, clk }
91-
}
92-
9350
pub fn release(self) -> SYST {
9451
self.tim
9552
}
9653
}
9754

98-
pub struct SysCountDownTimer {
99-
tim: SYST,
100-
clk: Hertz,
101-
}
102-
103-
impl SysCountDownTimer {
104-
/// Starts listening for an `event`
105-
pub fn listen(&mut self, event: Event) {
106-
match event {
107-
Event::TimeOut => self.tim.enable_interrupt(),
108-
}
109-
}
110-
111-
/// Stops listening for an `event`
112-
pub fn unlisten(&mut self, event: Event) {
113-
match event {
114-
Event::TimeOut => self.tim.disable_interrupt(),
115-
}
116-
}
117-
}
118-
119-
impl SysCountDownTimer {
120-
pub fn start(&mut self, timeout: MicrosDurationU32) -> Result<(), Error> {
121-
let mul = self.clk.0 / 1_000_000;
122-
let rvr = timeout.ticks() * mul - 1;
123-
124-
assert!(rvr < (1 << 24));
125-
126-
self.tim.set_reload(rvr);
127-
self.tim.clear_current();
128-
self.tim.enable_counter();
129-
Ok(())
130-
}
131-
132-
pub fn wait(&mut self) -> nb::Result<(), Void> {
133-
if self.tim.has_wrapped() {
134-
Ok(())
135-
} else {
136-
Err(nb::Error::WouldBlock)
137-
}
138-
}
139-
140-
pub fn cancel(&mut self) -> Result<(), Error> {
141-
if !self.tim.is_counter_enabled() {
142-
return Err(Error::Disabled);
143-
}
144-
145-
self.tim.disable_counter();
146-
Ok(())
147-
}
148-
}
149-
150-
impl CountDown for SysCountDownTimer {
151-
type Time = MicrosDurationU32;
152-
153-
fn start<T>(&mut self, timeout: T)
154-
where
155-
T: Into<Self::Time>,
156-
{
157-
self.start(timeout.into()).unwrap()
158-
}
159-
160-
fn wait(&mut self) -> nb::Result<(), Void> {
161-
self.wait()
162-
}
163-
}
164-
165-
impl Cancel for SysCountDownTimer {
166-
type Error = Error;
167-
168-
fn cancel(&mut self) -> Result<(), Self::Error> {
169-
self.cancel()
170-
}
171-
}
172-
17355
/// A monotonic non-decreasing timer
17456
///
17557
/// This uses the timer in the debug watch trace peripheral. This means, that if the
@@ -320,131 +202,6 @@ macro_rules! hal {
320202
}
321203
}
322204

323-
impl<TIM, const FREQ: u32> CountDownTimer<TIM, FREQ>
324-
where
325-
TIM: General,
326-
{
327-
fn new(mut tim: TIM, clk: Hertz) -> Self {
328-
let psc = clk.0 / FREQ - 1;
329-
tim.set_prescaler(u16(psc).unwrap());
330-
Self { tim }
331-
}
332-
333-
/// Starts listening for an `event`
334-
///
335-
/// Note, you will also have to enable the TIM2 interrupt in the NVIC to start
336-
/// receiving events.
337-
pub fn listen(&mut self, event: Event) {
338-
match event {
339-
Event::TimeOut => {
340-
// Enable update event interrupt
341-
self.tim.listen_update_interrupt(true);
342-
}
343-
}
344-
}
345-
346-
/// Clears interrupt associated with `event`.
347-
///
348-
/// If the interrupt is not cleared, it will immediately retrigger after
349-
/// the ISR has finished.
350-
pub fn clear_interrupt(&mut self, event: Event) {
351-
match event {
352-
Event::TimeOut => {
353-
// Clear interrupt flag
354-
self.tim.clear_update_interrupt_flag();
355-
}
356-
}
357-
}
358-
359-
/// Stops listening for an `event`
360-
pub fn unlisten(&mut self, event: Event) {
361-
match event {
362-
Event::TimeOut => {
363-
// Disable update event interrupt
364-
self.tim.listen_update_interrupt(false);
365-
}
366-
}
367-
}
368-
369-
/// Releases the TIM peripheral
370-
pub fn release(mut self) -> TIM {
371-
// pause counter
372-
self.tim.disable_counter();
373-
self.tim
374-
}
375-
}
376-
377-
impl<TIM, const FREQ: u32> CountDownTimer<TIM, FREQ>
378-
where
379-
TIM: General,
380-
{
381-
pub fn start(&mut self, timeout: TimerDurationU32<FREQ>) -> Result<(), Error> {
382-
// pause
383-
self.tim.disable_counter();
384-
// reset counter
385-
self.tim.reset_counter();
386-
387-
let arr = timeout.ticks() - 1;
388-
self.tim.set_auto_reload(arr)?;
389-
390-
// Trigger update event to load the registers
391-
self.tim.trigger_update();
392-
393-
// start counter
394-
self.tim.enable_counter();
395-
396-
Ok(())
397-
}
398-
399-
pub fn wait(&mut self) -> nb::Result<(), Void> {
400-
if self.tim.get_update_interrupt_flag() {
401-
Err(nb::Error::WouldBlock)
402-
} else {
403-
self.tim.clear_update_interrupt_flag();
404-
Ok(())
405-
}
406-
}
407-
408-
pub fn cancel(&mut self) -> Result<(), Error> {
409-
if !self.tim.is_counter_enabled() {
410-
return Err(Error::Disabled);
411-
}
412-
413-
// disable counter
414-
self.tim.disable_counter();
415-
Ok(())
416-
}
417-
}
418-
419-
impl<TIM, const FREQ: u32> CountDown for CountDownTimer<TIM, FREQ>
420-
where
421-
TIM: General,
422-
{
423-
type Time = TimerDurationU32<FREQ>;
424-
425-
fn start<T>(&mut self, timeout: T)
426-
where
427-
T: Into<Self::Time>,
428-
{
429-
self.start(timeout.into()).unwrap()
430-
}
431-
432-
fn wait(&mut self) -> nb::Result<(), Void> {
433-
self.wait()
434-
}
435-
}
436-
437-
impl<TIM, const FREQ: u32> Cancel for CountDownTimer<TIM, FREQ>
438-
where
439-
TIM: General,
440-
{
441-
type Error = Error;
442-
443-
fn cancel(&mut self) -> Result<(), Self::Error> {
444-
self.cancel()
445-
}
446-
}
447-
448205
// All F4xx parts have these timers.
449206
hal!(
450207
crate::pac::TIM1: u16,

0 commit comments

Comments
 (0)