From a3b8e42d1fe35d7f9a5936a53bce1e12df91977b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 12 Apr 2017 09:37:22 -0500 Subject: [PATCH] add an example of overriding an exception closes #2 --- examples/exception-override.rs | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/exception-override.rs diff --git a/examples/exception-override.rs b/examples/exception-override.rs new file mode 100644 index 0000000..2206329 --- /dev/null +++ b/examples/exception-override.rs @@ -0,0 +1,42 @@ +//! Overriding an exception +//! +//! **NOTE** You have to disable the `cortex-m-rt` crate "exceptions" feature to +//! make this work. + +#![feature(used)] +#![no_std] + +extern crate cortex_m; +extern crate cortex_m_rt; +extern crate {{name}}; + +use core::ptr; + +use cortex_m::{asm, exception}; +use {{name}}::interrupt; + +fn main() { + unsafe { + // Invalid memory access + ptr::read_volatile(0x2FFF_FFFF as *const u32); + } +} + +extern "C" fn hard_fault(_: exception::HardFault) { + // You'll hit this breakpoint rather than the one in cortex-m-rt + asm::bkpt() +} + +#[allow(dead_code)] +#[used] +#[link_section = ".rodata.exceptions"] +static EXCEPTIONS: exception::Handlers = exception::Handlers { + hard_fault: hard_fault, + ..exception::DEFAULT_HANDLERS +}; + +#[allow(dead_code)] +#[used] +#[link_section = ".rodata.interrupts"] +static INTERRUPTS: interrupt::Handlers = + interrupt::Handlers { ..interrupt::DEFAULT_HANDLERS };