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
260
261
use core::{fmt, hash, marker::PhantomData};
use super::{
utils, ClearInterruptLineError, EnableInterruptLineError, Kernel, PendInterruptLineError, Port,
QueryInterruptLineError, SetInterruptLinePriorityError,
};
use crate::utils::Init;
pub type InterruptNum = usize;
pub type InterruptPriority = i16;
pub struct InterruptLine<System>(InterruptNum, PhantomData<System>);
impl<System> Clone for InterruptLine<System> {
fn clone(&self) -> Self {
Self(self.0, self.1)
}
}
impl<System> Copy for InterruptLine<System> {}
impl<System> PartialEq for InterruptLine<System> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl<System> Eq for InterruptLine<System> {}
impl<System> hash::Hash for InterruptLine<System> {
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
hash::Hash::hash(&self.0, state);
}
}
impl<System> fmt::Debug for InterruptLine<System> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("InterruptLine").field(&self.0).finish()
}
}
impl<System> InterruptLine<System> {
pub const fn from_num(num: InterruptNum) -> Self {
Self(num, PhantomData)
}
pub const fn num(self) -> InterruptNum {
self.0
}
}
impl<System: Kernel> InterruptLine<System> {
#[cfg_attr(not(feature = "inline_syscall"), inline(never))]
pub fn set_priority(
self,
value: InterruptPriority,
) -> Result<(), SetInterruptLinePriorityError> {
let mut lock = utils::lock_cpu::<System>()?;
if !System::is_task_context() {
return Err(SetInterruptLinePriorityError::BadContext);
}
if !System::MANAGED_INTERRUPT_PRIORITY_RANGE.contains(&value) {
return Err(SetInterruptLinePriorityError::BadParam);
}
unsafe { self.set_priority_unchecked_inner(value, lock.borrow_mut()) }
}
#[cfg_attr(not(feature = "inline_syscall"), inline(never))]
pub unsafe fn set_priority_unchecked(
self,
value: InterruptPriority,
) -> Result<(), SetInterruptLinePriorityError> {
let mut lock = utils::lock_cpu::<System>()?;
if !System::is_task_context() {
return Err(SetInterruptLinePriorityError::BadContext);
}
unsafe { self.set_priority_unchecked_inner(value, lock.borrow_mut()) }
}
#[inline]
unsafe fn set_priority_unchecked_inner(
self,
value: InterruptPriority,
_lock: utils::CpuLockGuardBorrowMut<System>,
) -> Result<(), SetInterruptLinePriorityError> {
unsafe { System::set_interrupt_line_priority(self.0, value) }
}
#[inline]
pub fn enable(self) -> Result<(), EnableInterruptLineError> {
unsafe { System::enable_interrupt_line(self.0) }
}
#[inline]
pub fn disable(self) -> Result<(), EnableInterruptLineError> {
unsafe { System::disable_interrupt_line(self.0) }
}
#[inline]
pub fn pend(self) -> Result<(), PendInterruptLineError> {
unsafe { System::pend_interrupt_line(self.0) }
}
#[inline]
pub fn clear(self) -> Result<(), ClearInterruptLineError> {
unsafe { System::clear_interrupt_line(self.0) }
}
#[inline]
pub fn is_pending(self) -> Result<bool, QueryInterruptLineError> {
unsafe { System::is_interrupt_line_pending(self.0) }
}
}
pub struct InterruptHandler<System>(PhantomData<System>);
impl<System> InterruptHandler<System> {
pub(super) const fn new() -> Self {
Self(PhantomData)
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct InterruptLineInit<System> {
pub(super) line: InterruptLine<System>,
pub(super) priority: InterruptPriority,
pub(super) flags: InterruptLineInitFlags,
}
impl<System> Init for InterruptLineInit<System> {
const INIT: Self = Self {
line: InterruptLine::from_num(0),
priority: Init::INIT,
flags: InterruptLineInitFlags::empty(),
};
}
bitflags::bitflags! {
#[doc(hidden)]
pub struct InterruptLineInitFlags: u32 {
const ENABLE = 1 << 0;
const SET_PRIORITY = 1 << 1;
}
}
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct InterruptAttr<System: Port> {
pub line_inits: &'static [InterruptLineInit<System>],
}
impl<System: Kernel> InterruptAttr<System> {
pub(super) unsafe fn init(&self, mut lock: utils::CpuLockGuardBorrowMut<System>) {
for line_init in self.line_inits {
if line_init
.flags
.contains(InterruptLineInitFlags::SET_PRIORITY)
{
unsafe {
line_init
.line
.set_priority_unchecked_inner(line_init.priority, lock.borrow_mut())
.unwrap()
};
}
if line_init.flags.contains(InterruptLineInitFlags::ENABLE) {
line_init.line.enable().unwrap();
}
}
}
}