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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::sync::SpinLock;
use crate::io::Io;
use crate::i386::pio::Pio;
use crate::timer;
const OSCILLATOR_FREQ: usize = 1193182;
pub const CHAN_0_FREQUENCY: usize = 100;
const CHAN_0_DIVISOR: u16 = (OSCILLATOR_FREQ / CHAN_0_FREQUENCY) as u16;
lazy_static! {
static ref PIT_PORTS: SpinLock<PITPorts> = SpinLock::new(PITPorts {
port_chan_0: Pio::new(0x40),
port_chan_2: Pio::new(0x42),
port_cmd: Pio::new(0x43),
port_61: Pio::new(0x61)
});
}
#[derive(Debug, Clone, Copy)]
enum ChannelSelector {
Channel0,
Channel2
}
bitflags! {
struct Port61Flags: u8 {
const SPKR_CONTROL = 1 << 1;
const OUT2_STATUS = 1 << 5;
const GATE_2 = 1 << 0;
const OUT1_STATUS = 1 << 4;
const OTHER_2 = 1 << 2;
const OTHER_3 = 1 << 3;
const OTHER_6 = 1 << 6;
const OTHER_7 = 1 << 7;
}
}
#[allow(clippy::missing_docs_in_private_items)]
struct PITPorts {
port_chan_0: Pio<u8>,
port_chan_2: Pio<u8>,
port_cmd: Pio<u8>,
port_61: Pio<u8>
}
impl PITPorts {
fn write_reload_value(&mut self, channel_selector: ChannelSelector, value: u16) {
let port = match channel_selector {
ChannelSelector::Channel0 => &mut self.port_chan_0,
ChannelSelector::Channel2 => &mut self.port_chan_2
};
let lo: u8 = (value & 0xFF) as u8;
let hi: u8 = (value >> 8) as u8;
port.write(lo);
port.write(hi);
}
}
struct PITChannel2<'ports> {
ports: &'ports mut PITPorts
}
impl<'ports> PITChannel2<'ports> {
fn init(ports: &mut PITPorts) -> PITChannel2<'_> {
ports.port_cmd.write(
0b10110000
);
PITChannel2 { ports }
}
fn start_countdown(&mut self, value: u16) {
self.ports.write_reload_value(ChannelSelector::Channel2, value);
}
fn is_countdown_finished(&self) -> bool {
Port61Flags::from_bits(self.ports.port_61.read()).unwrap()
.contains(Port61Flags::OUT2_STATUS)
}
fn wait_countdown_is_finished(&self) {
while !(Port61Flags::from_bits(self.ports.port_61.read()).unwrap()
.contains(Port61Flags::OUT2_STATUS))
{
}
}
fn spin_wait_ms(&mut self, ms: usize) {
let ticks_to_wait: usize = (OSCILLATOR_FREQ / 1000) * ms;
if ticks_to_wait >= u16::max_value() as usize {
for _ in (0..=ticks_to_wait).step_by(u16::max_value() as usize) {
self.start_countdown(u16::max_value());
self.wait_countdown_is_finished();
}
}
let remaining_to_wait = ticks_to_wait as u16;
if remaining_to_wait > 0 {
self.start_countdown(remaining_to_wait);
self.wait_countdown_is_finished();
}
}
}
pub fn spin_wait_ms(ms: usize) {
let mut ports = PIT_PORTS.lock();
let mut chan2 = PITChannel2::init(&mut ports);
chan2.spin_wait_ms(ms);
}
pub unsafe fn init_channel_0() {
let mut ports = PIT_PORTS.lock();
ports.port_cmd.write(
0b00110100
);
ports.write_reload_value(ChannelSelector::Channel0, CHAN_0_DIVISOR);
timer::set_kernel_timer_info(0, OSCILLATOR_FREQ as u64, 1_000_000_000 / (CHAN_0_FREQUENCY as u64));
}
pub unsafe fn disable() {
let mut ports = PIT_PORTS.lock();
ports.port_cmd.write(0b00110010);
ports.write_reload_value(ChannelSelector::Channel0, 1);
}