|
1 | 1 | //! Delay devices and providers |
2 | 2 | use crate::register::mcycle; |
3 | | -use embedded_hal::blocking::delay::{DelayMs, DelayUs}; |
| 3 | +use embedded_hal::delay::DelayUs; |
4 | 4 |
|
5 | 5 | /// Machine mode cycle counter (`mcycle`) as a delay provider |
6 | 6 | #[derive(Copy, Clone)] |
| 7 | +#[repr(transparent)] |
7 | 8 | pub struct McycleDelay { |
| 9 | + /// The clock speed of the core, in Hertz |
8 | 10 | ticks_second: u32, |
9 | 11 | } |
10 | 12 |
|
11 | 13 | impl McycleDelay { |
12 | 14 | /// Constructs the delay provider. |
13 | 15 | /// `ticks_second` should be the clock speed of the core, in Hertz |
14 | | - #[inline(always)] |
15 | | - pub fn new(ticks_second: u32) -> Self { |
| 16 | + #[inline] |
| 17 | + pub const fn new(ticks_second: u32) -> Self { |
16 | 18 | Self { ticks_second } |
17 | 19 | } |
18 | 20 | } |
19 | 21 |
|
20 | | -impl DelayUs<u64> for McycleDelay { |
| 22 | +impl DelayUs for McycleDelay { |
21 | 23 | #[inline] |
22 | | - fn delay_us(&mut self, us: u64) { |
| 24 | + fn delay_us(&mut self, us: u32) { |
23 | 25 | let t0 = mcycle::read64(); |
24 | | - let clock = (us * (self.ticks_second as u64)) / 1_000_000; |
| 26 | + let us_64: u64 = us.into(); |
| 27 | + let clock = (us_64 * (self.ticks_second as u64)) / 1_000_000u64; |
25 | 28 | while mcycle::read64().wrapping_sub(t0) <= clock {} |
26 | 29 | } |
27 | 30 | } |
28 | | - |
29 | | -impl DelayUs<u32> for McycleDelay { |
30 | | - #[inline(always)] |
31 | | - fn delay_us(&mut self, us: u32) { |
32 | | - self.delay_us(us as u64) |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -// Implemented for constructions like `delay.delay_us(50_000);` |
37 | | -impl DelayUs<i32> for McycleDelay { |
38 | | - #[inline(always)] |
39 | | - fn delay_us(&mut self, us: i32) { |
40 | | - assert!(us >= 0); |
41 | | - self.delay_us(us as u32); |
42 | | - } |
43 | | -} |
44 | | - |
45 | | -impl DelayUs<u16> for McycleDelay { |
46 | | - #[inline(always)] |
47 | | - fn delay_us(&mut self, us: u16) { |
48 | | - self.delay_us(us as u32) |
49 | | - } |
50 | | -} |
51 | | - |
52 | | -impl DelayUs<u8> for McycleDelay { |
53 | | - #[inline(always)] |
54 | | - fn delay_us(&mut self, us: u8) { |
55 | | - self.delay_us(us as u32) |
56 | | - } |
57 | | -} |
58 | | - |
59 | | -impl DelayMs<u32> for McycleDelay { |
60 | | - #[inline] |
61 | | - fn delay_ms(&mut self, ms: u32) { |
62 | | - self.delay_us((ms as u64) * 1000) |
63 | | - } |
64 | | -} |
65 | | - |
66 | | -// Implemented for constructions like `delay.delay_ms(50_000);` |
67 | | -impl DelayMs<i32> for McycleDelay { |
68 | | - #[inline(always)] |
69 | | - fn delay_ms(&mut self, ms: i32) { |
70 | | - assert!(ms >= 0); |
71 | | - self.delay_ms(ms as u32); |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -impl DelayMs<u16> for McycleDelay { |
76 | | - #[inline(always)] |
77 | | - fn delay_ms(&mut self, ms: u16) { |
78 | | - self.delay_ms(ms as u32) |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -impl DelayMs<u8> for McycleDelay { |
83 | | - #[inline(always)] |
84 | | - fn delay_ms(&mut self, ms: u8) { |
85 | | - self.delay_ms(ms as u32) |
86 | | - } |
87 | | -} |
0 commit comments