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
|
//! Exceptions
#![allow(non_camel_case_types)]
/// Enumeration of all exceptions
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Vector {
/// Fault or system exception
Exception(Exception),
/// An interrupt
Interrupt(u8),
// Unreachable variant
#[doc(hidden)]
Reserved,
}
impl Vector {
/// Returns the kind of exception that's currently being serviced
pub fn active() -> Option<Vector> {
// NOTE(safe) atomic read
let icsr = unsafe { (*::peripheral::SCB.get()).icsr.read() };
if icsr == 0 {
return None;
}
Some(match icsr as u8 {
2 => Vector::Exception(Exception::NMI),
3 => Vector::Exception(Exception::HARD_FAULT),
4 => Vector::Exception(Exception::MEN_MANAGE),
5 => Vector::Exception(Exception::BUS_FAULT),
6 => Vector::Exception(Exception::USAGE_FAULT),
11 => Vector::Exception(Exception::SVCALL),
14 => Vector::Exception(Exception::PENDSV),
15 => Vector::Exception(Exception::SYS_TICK),
n if n >= 16 => Vector::Interrupt(n - 16),
_ => Vector::Reserved,
})
}
}
/// Registers stacked during an exception
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct StackedRegisters {
/// (General purpose) Register 0
pub r0: u32,
/// (General purpose) Register 1
pub r1: u32,
/// (General purpose) Register 2
pub r2: u32,
/// (General purpose) Register 3
pub r3: u32,
/// (General purpose) Register 12
pub r12: u32,
/// Linker Register
pub lr: u32,
/// Program Counter
pub pc: u32,
/// Program Status Register
pub xpsr: u32,
}
#[macro_export]
macro_rules! default_handler {
($f:ident, local: {
$($lvar:ident:$lty:ident = $lval:expr;)*
}) => {
#[allow(non_snake_case)]
mod DEFAULT_HANDLER {
pub struct Locals {
$(
pub $lvar: $lty,
)*
}
}
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn DEFAULT_HANDLER() {
static mut LOCALS: self::DEFAULT_HANDLER::Locals =
self::DEFAULT_HANDLER::Locals {
$(
$lvar: $lval,
)*
};
// type checking
let f: fn(&mut self::DEFAULT_HANDLER::Locals) = $f;
f(unsafe { &mut LOCALS });
}
};
($f:ident) => {
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn DEFAULT_HANDLER() {
// type checking
let f: fn() = $f;
f();
}
}
}
/// Fault and system exceptions
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Exception {
/// Non-maskable interrupt
NMI,
/// All class of fault.
HARD_FAULT,
/// Memory management.
MEN_MANAGE,
/// Pre-fetch fault, memory access fault.
BUS_FAULT,
/// Undefined instruction or illegal state.
USAGE_FAULT,
/// System service call via SWI instruction
SVCALL,
/// Pendable request for system service
PENDSV,
/// System tick timer
SYS_TICK,
}
#[macro_export]
macro_rules! exception {
($NAME:ident, $f:path, local: {
$($lvar:ident:$lty:ident = $lval:expr;)*
}) => {
#[allow(non_snake_case)]
mod $NAME {
pub struct Locals {
$(
pub $lvar: $lty,
)*
}
}
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn $NAME() {
// check that the handler exists
let _ = $crate::exception::Exception::$NAME;
static mut LOCALS: self::$NAME::Locals = self::$NAME::Locals {
$(
$lvar: $lval,
)*
};
// type checking
let f: fn(&mut self::$NAME::Locals) = $f;
f(unsafe { &mut LOCALS });
}
};
($NAME:ident, $f:path) => {
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn $NAME() {
// check that the handler exists
let _ = $crate::exception::Exception::$NAME;
// type checking
let f: fn() = $f;
f();
}
}
}
|