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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use core::marker::PhantomData;
use riscv::register::{mie, mip};
use riscv::interrupt::Nr;
use e310x::PLIC;
use e310x::Interrupt;
#[derive(Clone, Copy)]
pub enum Priority {
P0,
P1,
P2,
P3,
P4,
P5,
P6,
P7,
}
impl Priority {
fn from(prio: u32) -> Option<Priority> {
match prio {
0 => Some(Priority::P0),
1 => Some(Priority::P1),
2 => Some(Priority::P2),
3 => Some(Priority::P3),
4 => Some(Priority::P4),
5 => Some(Priority::P5),
6 => Some(Priority::P6),
7 => Some(Priority::P7),
_ => None,
}
}
}
impl Into<u32> for Priority {
fn into(self) -> u32 {
match self {
Priority::P0 => 0,
Priority::P1 => 1,
Priority::P2 => 2,
Priority::P3 => 3,
Priority::P4 => 4,
Priority::P5 => 5,
Priority::P6 => 6,
Priority::P7 => 7,
}
}
}
pub struct IrqWatchdog;
pub struct IrqRtc;
pub struct IrqUart0;
pub struct Plic {
pub mext: MEXT,
pub threshold: THRESHOLD,
pub claim: CLAIM,
pub wdog: INTERRUPT<IrqWatchdog>,
pub rtc: INTERRUPT<IrqRtc>,
pub uart0: INTERRUPT<IrqUart0>,
}
impl From<PLIC> for Plic {
fn from(_: PLIC) -> Self {
Plic {
mext: MEXT { _0: () },
threshold: THRESHOLD { _0: () },
claim: CLAIM { _0: () },
wdog: INTERRUPT {
offset: 0,
mask: 1 << (Interrupt::WATCHDOG as u8),
priority_offset: Interrupt::WATCHDOG as usize,
_marker: PhantomData,
},
rtc: INTERRUPT {
offset: 0,
mask: 1 << (Interrupt::RTC as u8),
priority_offset: Interrupt::RTC as usize,
_marker: PhantomData,
},
uart0: INTERRUPT {
offset: 0,
mask: 1 << (Interrupt::UART0 as u8),
priority_offset: Interrupt::UART0 as usize,
_marker: PhantomData,
}
}
}
}
pub struct MEXT {
_0: (),
}
impl MEXT {
#[inline]
pub fn enable(&mut self) {
unsafe { mie::set_mext() };
}
#[inline]
pub fn disable(&mut self) {
unsafe { mie::clear_mext() };
}
#[inline]
pub fn is_pending(&self) -> bool {
mip::read().mext()
}
}
pub struct THRESHOLD {
_0: (),
}
impl THRESHOLD {
pub fn get(&self) -> Priority {
let threshold = unsafe { (*PLIC::ptr()).threshold.read() };
Priority::from(threshold.bits()).unwrap()
}
pub fn set(&mut self, priority: Priority) {
unsafe {
(*PLIC::ptr()).threshold.write(|w| w.bits(priority.into()));
}
}
}
pub struct CLAIM {
_0: (),
}
impl CLAIM {
pub fn claim(&mut self) -> Option<Interrupt> {
let intr = unsafe { (*PLIC::ptr()).claim.read().bits() };
if intr == 0 {
None
} else {
Some(Interrupt::try_from(intr as u8).unwrap())
}
}
pub fn complete(&mut self, intr: Interrupt) {
unsafe {
(*PLIC::ptr()).claim.write(|w| w.bits(intr.nr() as u32));
}
}
}
pub struct INTERRUPT<IRQ> {
offset: usize,
mask: u32,
priority_offset: usize,
_marker: PhantomData<IRQ>,
}
impl<IRQ> INTERRUPT<IRQ> {
#[inline]
pub fn enable(&mut self) {
unsafe {
(*PLIC::ptr()).enable[self.offset]
.modify(|r, w| w.bits(r.bits() | self.mask));
}
}
#[inline]
pub fn disable(&mut self) {
unsafe {
(*PLIC::ptr()).enable[self.offset]
.modify(|r, w| w.bits(r.bits() & !self.mask));
}
}
pub fn is_pending(&self) -> bool {
let pending = unsafe {
(*PLIC::ptr()).pending[self.offset].read()
};
pending.bits() & self.mask == self.mask
}
pub fn is_enabled(&self) -> bool {
let enabled = unsafe {
(*PLIC::ptr()).enable[self.offset].read()
};
enabled.bits() & self.mask == self.mask
}
pub fn priority(&self) -> Priority {
let priority = unsafe {
(*PLIC::ptr()).priority[self.priority_offset].read()
};
Priority::from(priority.bits()).unwrap()
}
pub fn set_priority(&mut self, priority: Priority) {
unsafe {
(*PLIC::ptr()).priority[self.priority_offset]
.write(|w| w.bits(priority as u32));
}
}
}