diff options
author | 2017-06-24 22:10:02 -0500 | |
---|---|---|
committer | 2017-06-30 18:08:28 -0500 | |
commit | cb7a60a2b4059bd483df2841aab9db74305cd35e (patch) | |
tree | 21e5fdcd3fcdcff9346b7c05140b74cc121ba9ee /src | |
parent | 85f78eb40a432801e7f1a2633d8e825235b1aca4 (diff) | |
download | cortex-m-cb7a60a2b4059bd483df2841aab9db74305cd35e.tar.gz cortex-m-cb7a60a2b4059bd483df2841aab9db74305cd35e.tar.zst cortex-m-cb7a60a2b4059bd483df2841aab9db74305cd35e.zip |
move default_handler! and exception! macros to cortex-m-rt
Diffstat (limited to 'src')
-rw-r--r-- | src/exception.rs | 150 |
1 files changed, 29 insertions, 121 deletions
diff --git a/src/exception.rs b/src/exception.rs index ceb0f2a..36bf320 100644 --- a/src/exception.rs +++ b/src/exception.rs @@ -4,9 +4,23 @@ /// Enumeration of all exceptions #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum Vector { - /// Fault or system exception - Exception(Exception), +pub enum Exception { + /// Non-maskable interrupt + Nmi, + /// All class of fault. + HardFault, + /// Memory management. + MenManage, + /// 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 @@ -14,9 +28,9 @@ pub enum Vector { Reserved, } -impl Vector { +impl Exception { /// Returns the kind of exception that's currently being serviced - pub fn active() -> Option<Vector> { + pub fn active() -> Option<Exception> { // NOTE(safe) atomic read let icsr = unsafe { (*::peripheral::SCB.get()).icsr.read() }; if icsr == 0 { @@ -24,16 +38,16 @@ impl Vector { } 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, + 2 => Exception::Nmi, + 3 => Exception::HardFault, + 4 => Exception::MenManage, + 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, }) } } @@ -59,109 +73,3 @@ pub struct StackedRegisters { /// 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(); - } - } -} |