aboutsummaryrefslogtreecommitdiff
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
parent5e19a55f9e3b37dd5d1fa029f0a0814531fde1af (diff)
downloadcortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.tar.gz
cortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.tar.zst
cortex-m-50779f23d2bbe9ed5168c044ed63901bd2dd3ce7.zip
add Exception
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/exception.rs47
-rw-r--r--src/lib.rs4
3 files changed, 57 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5e7ac7b..35a4523 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
+### Added
+
+- `Exception` a enumeration of the kind of exceptions the processor can service.
+ There's also a `Exception::current` constructor that returns the `Exception`
+ that's currently being serviced.
+
## [v0.1.5]
### Added
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,
+
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 7459c4e..09eeceb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,10 @@ pub mod interrupt;
pub mod peripheral;
pub mod register;
+mod exception;
+
+pub use exception::Exception;
+
/// Stack frame
#[repr(C)]
pub struct StackFrame {