1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::ctimer::CoreTimer;
use crate::rcu::Clocks;
use crate::unit::*;
use embedded_hal::blocking::delay::DelayMs;
pub struct Delay {
ctimer: CoreTimer,
clock_frequency: Hertz,
}
impl Delay {
pub fn new(clocks: Clocks, ctimer: CoreTimer) -> Self {
Delay {
ctimer,
clock_frequency: clocks.ck_sys(),
}
}
pub fn free(self) -> CoreTimer {
self.ctimer
}
}
impl DelayMs<u8> for Delay {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32::from(ms))
}
}
impl DelayMs<u16> for Delay {
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32::from(ms))
}
}
impl DelayMs<u32> for Delay {
fn delay_ms(&mut self, ms: u32) {
let count: u64 = ms as u64 * (self.clock_frequency.0 / 4000) as u64;
let tmp: u64 = self.ctimer.get_value();
let mut start: u64 = self.ctimer.get_value();
while start == tmp {
start = self.ctimer.get_value();
}
while u64::wrapping_sub(self.ctimer.get_value(), start) < count {}
}
}