aboutsummaryrefslogtreecommitdiff
path: root/src/exception.rs
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <japaricious@gmail.com> 2017-01-22 14:15:09 -0500
committerGravatar Jorge Aparicio <japaricious@gmail.com> 2017-01-22 14:15:09 -0500
commit50779f23d2bbe9ed5168c044ed63901bd2dd3ce7 (patch)
tree79bd849eb0602f93e4e9e05b253190c8b0e4871a /src/exception.rs
parent5e19a55f9e3b37dd5d1fa029f0a0814531fde1af (diff)
downloadcortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.tar.gz
cortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.tar.zst
cortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.zip
add Exception
Diffstat (limited to 'src/exception.rs')
-rw-r--r--src/exception.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/exception.rs b/src/exception.rs
new file mode 100644
index 0000000..7046080
--- /dev/null
+++ b/src/exception.rs
@@ -0,0 +1,47 @@
+/// Kind of exception
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum Exception {
+ /// i.e. currently not servicing an exception
+ ThreadMode,
+ /// Non-maskable interrupt.
+ Nmi,
+ /// All class of fault.
+ HardFault,
+ /// Memory management.
+ MemoryManagementFault,
+ /// Pre-fetch fault, memory access fault.
+ BusFault,
+ /// Undefined instruction or illegal state.
+ UsageFault,
+ /// System service call via SWI instruction
+ SVCall,
+ /// Pendable request for system service
+ PendSV,
+ /// System tick timer
+ Systick,
+ /// An interrupt
+ Interrupt(u8),
+ // Unreachable variant
+ #[doc(hidden)]
+ Reserved,
+}
+
+impl Exception {
+ /// Returns the kind of exception that's currently being serviced
+ pub fn current() -> Exception {
+ match ::peripheral::scb().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,
+
+ }
+ }
+}