Skip to content

Commit 50779f2

Browse files
author
Jorge Aparicio
committed
add Exception
1 parent 5e19a55 commit 50779f2

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- `Exception` a enumeration of the kind of exceptions the processor can service.
13+
There's also a `Exception::current` constructor that returns the `Exception`
14+
that's currently being serviced.
15+
1016
## [v0.1.5]
1117

1218
### Added

src/exception.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// Kind of exception
2+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3+
pub enum Exception {
4+
/// i.e. currently not servicing an exception
5+
ThreadMode,
6+
/// Non-maskable interrupt.
7+
Nmi,
8+
/// All class of fault.
9+
HardFault,
10+
/// Memory management.
11+
MemoryManagementFault,
12+
/// Pre-fetch fault, memory access fault.
13+
BusFault,
14+
/// Undefined instruction or illegal state.
15+
UsageFault,
16+
/// System service call via SWI instruction
17+
SVCall,
18+
/// Pendable request for system service
19+
PendSV,
20+
/// System tick timer
21+
Systick,
22+
/// An interrupt
23+
Interrupt(u8),
24+
// Unreachable variant
25+
#[doc(hidden)]
26+
Reserved,
27+
}
28+
29+
impl Exception {
30+
/// Returns the kind of exception that's currently being serviced
31+
pub fn current() -> Exception {
32+
match ::peripheral::scb().icsr.read() as u8 {
33+
0 => Exception::ThreadMode,
34+
2 => Exception::Nmi,
35+
3 => Exception::HardFault,
36+
4 => Exception::MemoryManagementFault,
37+
5 => Exception::BusFault,
38+
6 => Exception::UsageFault,
39+
11 => Exception::SVCall,
40+
14 => Exception::PendSV,
41+
15 => Exception::Systick,
42+
n if n >= 16 => Exception::Interrupt(n - 16),
43+
_ => Exception::Reserved,
44+
45+
}
46+
}
47+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ pub mod interrupt;
2222
pub mod peripheral;
2323
pub mod register;
2424

25+
mod exception;
26+
27+
pub use exception::Exception;
28+
2529
/// Stack frame
2630
#[repr(C)]
2731
pub struct StackFrame {

0 commit comments

Comments
 (0)