Skip to content

Commit 37c7b68

Browse files
committed
merge counter & syscounter mods, rm old Delay
1 parent d2be502 commit 37c7b68

File tree

9 files changed

+242
-459
lines changed

9 files changed

+242
-459
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Rename `Delay<SYST>` to SysDelay, remove old `Delay<TIM2>`
1213
- Extend timers to 32bit on `Delay`
1314
- Move `MonoTimer` from `timer` to dwt mode [#448]
1415
- Unify serial trait impls for embedded-hal 0.2 & 1.0 [#447]

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ synopsys-usb-otg = { version = "0.2.0", features = ["cortex-m"], optional = true
3737
sdio-host = { version = "0.5.0", optional = true }
3838
embedded-dma = "0.2.0"
3939
bare-metal = { version = "1" }
40-
cast = { default-features = false, version = "0.3.0" }
4140
void = { default-features = false, version = "1.0.2" }
4241
embedded-hal = { features = ["unproven"], version = "0.2.7" }
4342
display-interface = { version = "0.4.1", optional = true }

src/timer.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use fugit::HertzU32 as Hertz;
1616

1717
pub mod counter;
1818
pub use counter::*;
19-
pub mod syscounter;
20-
pub use syscounter::*;
2119
pub mod delay;
2220
pub use delay::*;
2321
mod pins;
@@ -98,7 +96,7 @@ pub trait TimerExt: Sized {
9896
fn counter_hz(self, clocks: &Clocks) -> CounterHz<Self>;
9997

10098
/// Blocking [Delay] with custom fixed precision
101-
fn delay<const FREQ: u32>(self, clocks: &Clocks) -> FDelay<Self, FREQ>;
99+
fn delay<const FREQ: u32>(self, clocks: &Clocks) -> Delay<Self, FREQ>;
102100
/// Blocking [Delay] with fixed precision of 1 ms (1 kHz sampling)
103101
///
104102
/// Can wait from 2 ms to 49 days.
@@ -113,8 +111,6 @@ pub trait TimerExt: Sized {
113111
fn delay_us(self, clocks: &Clocks) -> DelayUs<Self> {
114112
self.delay::<1_000_000>(clocks)
115113
}
116-
/// Blocking [Delay]
117-
fn delay_dyn(self, clocks: &Clocks) -> Delay<Self>;
118114
}
119115

120116
impl<TIM: Instance> TimerExt for TIM {
@@ -124,12 +120,9 @@ impl<TIM: Instance> TimerExt for TIM {
124120
fn counter_hz(self, clocks: &Clocks) -> CounterHz<Self> {
125121
Timer::new(self, clocks).counter_hz()
126122
}
127-
fn delay<const FREQ: u32>(self, clocks: &Clocks) -> FDelay<Self, FREQ> {
123+
fn delay<const FREQ: u32>(self, clocks: &Clocks) -> Delay<Self, FREQ> {
128124
FTimer::new(self, clocks).delay()
129125
}
130-
fn delay_dyn(self, clocks: &Clocks) -> Delay<Self> {
131-
Timer::new(self, clocks).delay()
132-
}
133126
}
134127

135128
pub trait SysTimerExt: Sized {
@@ -143,7 +136,7 @@ pub trait SysTimerExt: Sized {
143136
self.counter::<1_000_000>(clocks)
144137
}
145138
/// Blocking [Delay] with custom precision
146-
fn delay(self, clocks: &Clocks) -> Delay<Self>;
139+
fn delay(self, clocks: &Clocks) -> SysDelay;
147140
}
148141

149142
impl SysTimerExt for SYST {
@@ -153,7 +146,7 @@ impl SysTimerExt for SYST {
153146
fn counter<const FREQ: u32>(self, clocks: &Clocks) -> SysCounter<FREQ> {
154147
Timer::syst(self, clocks).counter()
155148
}
156-
fn delay(self, clocks: &Clocks) -> Delay<Self> {
149+
fn delay(self, clocks: &Clocks) -> SysDelay {
157150
Timer::syst_external(self, clocks).delay()
158151
}
159152
}
@@ -688,8 +681,8 @@ impl<TIM: Instance, const FREQ: u32> FTimer<TIM, FREQ> {
688681
}
689682

690683
/// Creates `Delay` that imlements [embedded_hal::blocking::delay] traits
691-
pub fn delay(self) -> FDelay<TIM, FREQ> {
692-
FDelay(self)
684+
pub fn delay(self) -> Delay<TIM, FREQ> {
685+
Delay(self)
693686
}
694687

695688
/// Releases the TIM peripheral

src/timer/counter.rs

Lines changed: 158 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::{compute_arr_presc, Error, Event, FTimer, Instance, Timer};
1+
use super::{compute_arr_presc, Error, Event, FTimer, Instance, SysEvent, Timer};
2+
use crate::pac::SYST;
23
use core::ops::{Deref, DerefMut};
3-
use fugit::HertzU32 as Hertz;
4+
use fugit::{HertzU32 as Hertz, TimerDurationU32, TimerInstantU32};
45

56
/// Hardware timers
67
pub struct CounterHz<TIM>(pub(super) Timer<TIM>);
@@ -66,7 +67,6 @@ impl<TIM: Instance> CounterHz<TIM> {
6667
Ok(())
6768
}
6869
}
69-
use fugit::{TimerDurationU32, TimerInstantU32};
7070

7171
/// Periodic non-blocking timer that imlements [embedded_hal::timer::CountDown]
7272
pub struct Counter<TIM, const FREQ: u32>(pub(super) FTimer<TIM, FREQ>);
@@ -160,3 +160,158 @@ impl<TIM: Instance, const FREQ: u32> fugit_timer::Timer<FREQ> for Counter<TIM, F
160160
self.wait()
161161
}
162162
}
163+
164+
impl Timer<SYST> {
165+
/// Creates [SysCounterHz] which takes [Hertz] as Duration
166+
pub fn counter_hz(self) -> SysCounterHz {
167+
SysCounterHz(self)
168+
}
169+
170+
/// Creates [SysCounter] with custom precision (core frequency recommended is known)
171+
pub fn counter<const FREQ: u32>(self) -> SysCounter<FREQ> {
172+
SysCounter(self)
173+
}
174+
175+
/// Creates [SysCounter] 1 microsecond precision
176+
pub fn counter_us(self) -> SysCounterUs {
177+
SysCounter(self)
178+
}
179+
}
180+
181+
/// Hardware timers
182+
pub struct SysCounterHz(Timer<SYST>);
183+
184+
impl Deref for SysCounterHz {
185+
type Target = Timer<SYST>;
186+
fn deref(&self) -> &Self::Target {
187+
&self.0
188+
}
189+
}
190+
191+
impl DerefMut for SysCounterHz {
192+
fn deref_mut(&mut self) -> &mut Self::Target {
193+
&mut self.0
194+
}
195+
}
196+
197+
impl SysCounterHz {
198+
pub fn start(&mut self, timeout: Hertz) -> Result<(), Error> {
199+
let rvr = self.clk.raw() / timeout.raw() - 1;
200+
201+
if rvr >= (1 << 24) {
202+
return Err(Error::WrongAutoReload);
203+
}
204+
205+
self.tim.set_reload(rvr);
206+
self.tim.clear_current();
207+
self.tim.enable_counter();
208+
209+
Ok(())
210+
}
211+
212+
pub fn wait(&mut self) -> nb::Result<(), Error> {
213+
if self.tim.has_wrapped() {
214+
Ok(())
215+
} else {
216+
Err(nb::Error::WouldBlock)
217+
}
218+
}
219+
220+
pub fn cancel(&mut self) -> Result<(), Error> {
221+
if !self.tim.is_counter_enabled() {
222+
return Err(Error::Disabled);
223+
}
224+
225+
self.tim.disable_counter();
226+
Ok(())
227+
}
228+
}
229+
230+
pub type SysCounterUs = SysCounter<1_000_000>;
231+
232+
/// SysTick timer with precision of 1 μs (1 MHz sampling)
233+
pub struct SysCounter<const FREQ: u32>(Timer<SYST>);
234+
235+
impl<const FREQ: u32> Deref for SysCounter<FREQ> {
236+
type Target = Timer<SYST>;
237+
fn deref(&self) -> &Self::Target {
238+
&self.0
239+
}
240+
}
241+
242+
impl<const FREQ: u32> DerefMut for SysCounter<FREQ> {
243+
fn deref_mut(&mut self) -> &mut Self::Target {
244+
&mut self.0
245+
}
246+
}
247+
248+
impl<const FREQ: u32> SysCounter<FREQ> {
249+
/// Starts listening for an `event`
250+
pub fn listen(&mut self, event: SysEvent) {
251+
match event {
252+
SysEvent::Update => self.tim.enable_interrupt(),
253+
}
254+
}
255+
256+
/// Stops listening for an `event`
257+
pub fn unlisten(&mut self, event: SysEvent) {
258+
match event {
259+
SysEvent::Update => self.tim.disable_interrupt(),
260+
}
261+
}
262+
263+
pub fn now(&self) -> TimerInstantU32<FREQ> {
264+
TimerInstantU32::from_ticks(SYST::get_current() / (self.clk.raw() / FREQ))
265+
}
266+
267+
pub fn start(&mut self, timeout: TimerDurationU32<FREQ>) -> Result<(), Error> {
268+
let rvr = timeout.ticks() * (self.clk.raw() / FREQ) - 1;
269+
270+
if rvr >= (1 << 24) {
271+
return Err(Error::WrongAutoReload);
272+
}
273+
274+
self.tim.set_reload(rvr);
275+
self.tim.clear_current();
276+
self.tim.enable_counter();
277+
278+
Ok(())
279+
}
280+
281+
pub fn wait(&mut self) -> nb::Result<(), Error> {
282+
if self.tim.has_wrapped() {
283+
Ok(())
284+
} else {
285+
Err(nb::Error::WouldBlock)
286+
}
287+
}
288+
289+
pub fn cancel(&mut self) -> Result<(), Error> {
290+
if !self.tim.is_counter_enabled() {
291+
return Err(Error::Disabled);
292+
}
293+
294+
self.tim.disable_counter();
295+
Ok(())
296+
}
297+
}
298+
299+
impl<const FREQ: u32> fugit_timer::Timer<FREQ> for SysCounter<FREQ> {
300+
type Error = Error;
301+
302+
fn now(&mut self) -> TimerInstantU32<FREQ> {
303+
Self::now(self)
304+
}
305+
306+
fn start(&mut self, duration: TimerDurationU32<FREQ>) -> Result<(), Self::Error> {
307+
self.start(duration)
308+
}
309+
310+
fn wait(&mut self) -> nb::Result<(), Self::Error> {
311+
self.wait()
312+
}
313+
314+
fn cancel(&mut self) -> Result<(), Self::Error> {
315+
self.cancel()
316+
}
317+
}

0 commit comments

Comments
 (0)