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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{
hal::timer::{CountDown, Periodic},
sysctl::{self, Clocks},
};
use nb;
#[rustfmt::skip]
use tm4c123x::{
TIMER0, TIMER1, TIMER2, TIMER3, TIMER4, TIMER5,
WTIMER0, WTIMER1, WTIMER2, WTIMER3, WTIMER4, WTIMER5,
};
use tm4c_hal::time::Hertz;
use void::Void;
pub struct Timer<TIM> {
tim: TIM,
clocks: Clocks,
timeout: Hertz,
}
pub enum Event {
TimeOut,
}
macro_rules! hal {
($($TIM:ident: ($tim:ident, $powerDomain:ident),)+) => {
$(
impl Periodic for Timer<$TIM> {}
impl CountDown for Timer<$TIM> {
type Time = Hertz;
#[allow(unused_unsafe)]
fn start<T>(&mut self, timeout: T)
where
T: Into<Hertz>,
{
self.tim.ctl.modify(|_, w|
w.taen().clear_bit()
.tben().clear_bit()
);
self.timeout = timeout.into();
let frequency = self.timeout.0;
let ticks = self.clocks.sysclk.0 / frequency;
self.tim.tav.write(|w| unsafe { w.bits(ticks) });
self.tim.tailr.write(|w| unsafe { w.bits(ticks) });
self.tim.ctl.modify(|_, w|
w.taen().set_bit()
);
}
fn wait(&mut self) -> nb::Result<(), Void> {
if self.tim.ris.read().tatoris().bit_is_clear () {
Err(nb::Error::WouldBlock)
} else {
self.tim.icr.write(|w| w.tatocint().set_bit());
Ok(())
}
}
}
impl Timer<$TIM> {
pub fn $tim<T>(tim: $TIM, timeout: T,
pc: &sysctl::PowerControl,
clocks: &Clocks,
) -> Self
where
T: Into<Hertz>,
{
sysctl::control_power(
pc, sysctl::Domain::$powerDomain,
sysctl::RunMode::Run, sysctl::PowerState::On);
sysctl::reset(pc, sysctl::Domain::$powerDomain);
tim.ctl.write(|w|
w.taen().clear_bit()
.tben().clear_bit()
.tastall().set_bit()
);
tim.cfg.write(|w| w.cfg()._32_bit_timer());
tim.tamr.write(|w| w.tamr().period());
let mut timer = Timer {
tim:tim,
clocks: *clocks,
timeout: Hertz(0),
};
timer.start(timeout);
timer
}
pub fn listen(&mut self, event: Event) {
match event {
Event::TimeOut => {
self.tim.imr.modify(|_,w| w.tatoim().set_bit());
}
}
}
pub fn unlisten(&mut self, event: Event) {
match event {
Event::TimeOut => {
self.tim.imr.modify(|_,w| w.tatoim().clear_bit());
}
}
}
pub fn free(self) -> $TIM {
self.tim.ctl.write(|w|
w.taen().clear_bit()
.tben().clear_bit());
self.tim
}
}
)+
}
}
hal! {
TIMER0: (timer0, Timer0),
TIMER1: (timer1, Timer1),
TIMER2: (timer2, Timer2),
TIMER3: (timer3, Timer3),
TIMER4: (timer4, Timer4),
TIMER5: (timer5, Timer5),
WTIMER0: (wtimer0, WideTimer0),
WTIMER1: (wtimer1, WideTimer1),
WTIMER2: (wtimer2, WideTimer2),
WTIMER3: (wtimer3, WideTimer3),
WTIMER4: (wtimer4, WideTimer4),
WTIMER5: (wtimer5, WideTimer5),
}