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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::timer::Timer;
use crate::time::U32Ext;
use cast::u32;
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
use embedded_hal::timer::CountDown;
use gd32vf103_pac::{TIMER0, TIMER1, TIMER2, TIMER3, TIMER4, TIMER5, TIMER6};
pub struct Delay<TIMER> where Timer<TIMER>: CountDown {
timer: Timer<TIMER>,
}
macro_rules! delay {
($($TIMER:ident,)+) => {
$(
impl Delay<$TIMER> {
pub fn new(timer: Timer<$TIMER>) -> Self {
Delay { timer, }
}
pub fn free(self) -> Timer<$TIMER> {
self.timer
}
}
impl DelayMs<u32> for Delay<$TIMER> {
fn delay_ms(&mut self, ms: u32) {
self.delay_us(ms * 1_000);
}
}
impl DelayMs<u16> for Delay<$TIMER> {
fn delay_ms(&mut self, ms: u16) {
self.delay_ms(u32(ms));
}
}
impl DelayMs<u8> for Delay<$TIMER> {
fn delay_ms(&mut self, ms: u8) {
self.delay_ms(u32(ms));
}
}
impl DelayUs<u32> for Delay<$TIMER> {
fn delay_us(&mut self, us: u32) {
let freq = 1_000_000 / us;
self.timer.start(freq.hz());
while let Err(_) = self.timer.wait() { }
self.timer.tim.ctl0.modify(|_, w| w.cen().clear_bit());
}
}
impl DelayUs<u16> for Delay<$TIMER> {
fn delay_us(&mut self, us: u16) {
self.delay_us(u32(us))
}
}
impl DelayUs<u8> for Delay<$TIMER> {
fn delay_us(&mut self, us: u8) {
self.delay_us(u32(us))
}
}
)+
}
}
delay! {
TIMER0,
TIMER1,
TIMER2,
TIMER3,
TIMER4,
TIMER5,
TIMER6,
}