|
1 |
| -//! Delays |
2 |
| -use core::cmp; |
3 |
| -use cortex_m::peripheral::SYST; |
4 |
| -use hal::blocking::delay::{DelayMs, DelayUs}; |
5 |
| - |
6 |
| -use crate::prelude::*; |
7 | 1 | use crate::rcc::Clocks;
|
8 |
| -use crate::time::{Hertz, MicroSecond}; |
9 |
| - |
10 |
| -/// System timer (SysTick) as a delay provider |
11 |
| -pub struct Delay { |
12 |
| - clk: Hertz, |
13 |
| - syst: SYST, |
14 |
| -} |
15 |
| - |
16 |
| -impl Delay { |
17 |
| - /// Configures the system timer (SysTick) as a delay provider |
18 |
| - pub fn new(syst: SYST, clocks: &Clocks) -> Self { |
19 |
| - Delay { |
20 |
| - syst, |
21 |
| - clk: clocks.ahb_clk / 8, |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - pub fn delay<T>(&mut self, delay: T) |
26 |
| - where |
27 |
| - T: Into<MicroSecond>, |
28 |
| - { |
29 |
| - let mut cycles = delay.into().cycles(self.clk); |
30 |
| - while cycles > 0 { |
31 |
| - let reload = cmp::min(cycles, 0x00ff_ffff); |
32 |
| - cycles -= reload; |
33 |
| - self.syst.set_reload(reload); |
34 |
| - self.syst.clear_current(); |
35 |
| - self.syst.enable_counter(); |
36 |
| - while !self.syst.has_wrapped() {} |
37 |
| - self.syst.disable_counter(); |
38 |
| - } |
39 |
| - } |
40 |
| - |
41 |
| - /// Releases the system timer (SysTick) resource |
42 |
| - pub fn release(self) -> SYST { |
43 |
| - self.syst |
44 |
| - } |
45 |
| -} |
46 |
| - |
47 |
| -impl DelayUs<u32> for Delay { |
48 |
| - fn delay_us(&mut self, us: u32) { |
49 |
| - self.delay(us.us()) |
50 |
| - } |
51 |
| -} |
52 |
| - |
53 |
| -impl DelayUs<u16> for Delay { |
54 |
| - fn delay_us(&mut self, us: u16) { |
55 |
| - self.delay_us(us as u32) |
56 |
| - } |
57 |
| -} |
58 |
| - |
59 |
| -impl DelayUs<u8> for Delay { |
60 |
| - fn delay_us(&mut self, us: u8) { |
61 |
| - self.delay_us(us as u32) |
62 |
| - } |
63 |
| -} |
| 2 | +use crate::time::MicroSecond; |
| 3 | +pub use cortex_m::delay::*; |
| 4 | +use cortex_m::{peripheral::SYST, prelude::_embedded_hal_blocking_delay_DelayUs}; |
64 | 5 |
|
65 |
| -impl DelayMs<u32> for Delay { |
66 |
| - fn delay_ms(&mut self, ms: u32) { |
67 |
| - self.delay_us(ms.saturating_mul(1_000)); |
68 |
| - } |
69 |
| -} |
70 |
| - |
71 |
| -impl DelayMs<u16> for Delay { |
72 |
| - fn delay_ms(&mut self, ms: u16) { |
73 |
| - self.delay_ms(ms as u32); |
74 |
| - } |
| 6 | +pub trait SYSTDelayExt { |
| 7 | + fn delay(self, clocks: &Clocks) -> Delay; |
75 | 8 | }
|
76 | 9 |
|
77 |
| -impl DelayMs<u8> for Delay { |
78 |
| - fn delay_ms(&mut self, ms: u8) { |
79 |
| - self.delay_ms(ms as u32); |
| 10 | +impl SYSTDelayExt for SYST { |
| 11 | + fn delay(self, clocks: &Clocks) -> Delay { |
| 12 | + Delay::new(self, clocks.ahb_clk.0) |
80 | 13 | }
|
81 | 14 | }
|
82 | 15 |
|
83 | 16 | pub trait DelayExt {
|
84 |
| - fn delay(self, clocks: &Clocks) -> Delay; |
| 17 | + fn delay<T>(&mut self, delay: T) |
| 18 | + where |
| 19 | + T: Into<MicroSecond>; |
85 | 20 | }
|
86 | 21 |
|
87 |
| -impl DelayExt for SYST { |
88 |
| - fn delay(self, clocks: &Clocks) -> Delay { |
89 |
| - Delay::new(self, clocks) |
| 22 | +impl DelayExt for Delay { |
| 23 | + fn delay<T>(&mut self, delay: T) |
| 24 | + where |
| 25 | + T: Into<MicroSecond>, |
| 26 | + { |
| 27 | + self.delay_us(delay.into().0) |
90 | 28 | }
|
91 | 29 | }
|
0 commit comments