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
use ctxt::Context;
use Reserved;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Exception {
ThreadMode,
Nmi,
HardFault,
MemoryManagementFault,
BusFault,
UsageFault,
SVCall,
PendSV,
Systick,
Interrupt(u8),
#[doc(hidden)]
Reserved,
}
impl Exception {
pub fn current() -> Exception {
match unsafe { (*::peripheral::SCB.get()).icsr.read() } as u8 {
0 => Exception::ThreadMode,
2 => Exception::Nmi,
3 => Exception::HardFault,
4 => Exception::MemoryManagementFault,
5 => Exception::BusFault,
6 => Exception::UsageFault,
11 => Exception::SVCall,
14 => Exception::PendSV,
15 => Exception::Systick,
n if n >= 16 => Exception::Interrupt(n - 16),
_ => Exception::Reserved,
}
}
}
#[repr(C)]
pub struct Handlers {
pub nmi: extern "C" fn(Nmi),
pub hard_fault: extern "C" fn(HardFault),
pub mem_manage: extern "C" fn(MemManage),
pub bus_fault: extern "C" fn(BusFault),
pub usage_fault: extern "C" fn(UsageFault),
pub _reserved0: [Reserved; 4],
pub svcall: extern "C" fn(Svcall),
pub _reserved1: [Reserved; 2],
pub pendsv: extern "C" fn(Pendsv),
pub sys_tick: extern "C" fn(SysTick),
}
pub struct Nmi {
_0: (),
}
pub struct HardFault {
_0: (),
}
pub struct MemManage {
_0: (),
}
pub struct BusFault {
_0: (),
}
pub struct UsageFault {
_0: (),
}
pub struct Svcall {
_0: (),
}
pub struct Pendsv {
_0: (),
}
pub struct SysTick {
_0: (),
}
unsafe impl Context for Nmi {}
unsafe impl Context for HardFault {}
unsafe impl Context for MemManage {}
unsafe impl Context for BusFault {}
unsafe impl Context for UsageFault {}
unsafe impl Context for Svcall {}
unsafe impl Context for Pendsv {}
unsafe impl Context for SysTick {}
pub const DEFAULT_HANDLERS: Handlers = Handlers {
_reserved0: [Reserved::Vector; 4],
_reserved1: [Reserved::Vector; 2],
bus_fault: default_handler,
hard_fault: default_handler,
mem_manage: default_handler,
nmi: default_handler,
pendsv: default_handler,
svcall: default_handler,
sys_tick: default_handler,
usage_fault: default_handler,
};
#[naked]
pub extern "C" fn default_handler<T>(_token: T)
where
T: Context,
{
#[cfg(target_arch = "arm")]
extern "C" fn handler(_sr: &StackedRegisters) -> ! {
let _e = Exception::current();
::asm::bkpt();
loop {}
}
match () {
#[cfg(target_arch = "arm")]
() => {
unsafe {
asm!("mrs r0, MSP
ldr r1, [r0, #20]
b $0"
:
: "i"(handler as extern "C" fn(&StackedRegisters) -> !)
:
: "volatile");
::core::intrinsics::unreachable()
}
}
#[cfg(not(target_arch = "arm"))]
() => {}
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub struct StackedRegisters {
pub r0: u32,
pub r1: u32,
pub r2: u32,
pub r3: u32,
pub r12: u32,
pub lr: u32,
pub pc: u32,
pub xpsr: u32,
}