diff --git a/examples/crash.rs b/examples/crash.rs index bd3d6097c669038b372b779f31b0ffb99f1c7bbe..511f530be683e5eb29f331f8a45e919ad46da1f4 100644 --- a/examples/crash.rs +++ b/examples/crash.rs @@ -1,9 +1,13 @@ //! Debugging a crash (exception) +// #![deny(unsafe_code)] // this example is using unsafe +#![deny(warnings)] #![no_main] #![no_std] -extern crate panic_halt; +//use panic_halt as _; +use panic_semihosting as _; + use core::ptr; use cortex_m_rt::{entry, exception}; @@ -24,82 +28,5 @@ fn main() -> ! { #[exception] #[inline(never)] fn HardFault(ef: &cortex_m_rt::ExceptionFrame) -> ! { - // to inline never and read_volatile required to - // avoid `ef` being optimized out in release mode - unsafe { - ptr::read_volatile(ef); - } - - loop { - continue; - } + panic!("Exception frame {:?}", ef); } - -// -// Most crash conditions trigger a hard fault exception, whose handler is defined via -// ``` rust -// #[exception] -// fn HardFault(ef: &cortex_m_rt::ExceptionFrame) -> ! { -// ... -// ``` -// -// `cortex-m-rt` generates a trampoline, that calls into your user defined ` -// HardFault` handler. We look at the generated trampoline: -// -// ``` rust -// #[doc(hidden)] -// #[export_name = "HardFault"] -// #[link_section = ".HardFault.user"] -// pub unsafe extern "C" fn __cortex_m_rt_HardFault_trampoline(frame: &::cortex_m_rt::ExceptionFrame) { -// __cortex_m_rt_HardFault(frame) -// } -// ``` -// -// The `HardFault` handler has access to the exception `frame`, a -// snapshot of the CPU registers at the moment of the exception. -// -// To better see what is happening we make a `--release` build -// (It reduces the amount of redundant code.) -// -// ``` text -// $ cargo run --example crash --release -// ... -// Breakpoint 1, main () at examples/crash.rs:69 -// (gdb) continue -// Breakpoint 3, HardFault (frame=0x20007fe0) at examples/crash.rs:82 -// 82 #[exception] -// (gdb) p/x *frame -// $1 = cortex_m_rt::ExceptionFrame {r0: 0x2fffffff, r1: 0xf00000, r2: 0x0, r3: 0x0, r12: 0x0, lr: 0x8000405, pc: 0x800040a, xpsr: 0x61000000} -// (gdb) disassemble frame.pc -// Dump of assembler code for function crash::__cortex_m_rt_main: -// 0x08000406 <+0>: mvn.w r0, #3489660928 ; 0xd0000000 -// 0x0800040a <+4>: ldr r0, [r0, #0] -// 0x0800040c <+6>: b.n 0x800040c <crash::__cortex_m_rt_main+6> -// End of assembler dump. -// ``` -// -// The program counter (frame.pc) contains the address of the instruction that caused the exception. In GDB one can -// disassemble the program around this address to observe the instruction that caused the -// exception. In our case its the `ldr r0, [r0, #0]` caused the exception. This instruction tried to load (read) a 32-bit word -// from the address stored in the register `r0`. Looking again at the contents of `ExceptionFrame` -// we find that `r0` contained the address `0x2FFF_FFFF` when this instruction was executed. -// -// Looking at the assembly `mvn.w r0, #3489660928 ; 0xd0000000`. -// This is a *move* and *not*, so the resulting value here is actually -// 0x2fffffff. Why did it not do it straight up then as 0x2FFF_FFFF? -// -// Well a 32 bit constant cannot be stored in a 32 bit instruction. -// So under the hood it stores 0xd0, bit shifts it and bit wise inversion. -// This is the level of optimization Rust + LLVM is capable of. -// -// We can further backtrace the calls leading up to the fault. -// ``` text -// ((gdb) bt -// #0 HardFault (frame=0x20007fe0) at examples/crash.rs:79 -// #1 <signal handler called> -// #2 core::ptr::read_volatile (src=0x2fffffff) -// at /rustc/73528e339aae0f17a15ffa49a8ac608f50c6cf14/src/libcore/ptr/mod.rs:948 -// #3 crash::__cortex_m_rt_main () at examples/crash.rs:71 -// #4 0x08000404 in main () at examples/crash.rs:66 -// ``` -// Here we see that on frame #2 we are doing the read causing havoc. diff --git a/examples/device.rs b/examples/device.rs index 9efaa46cde0ce0af99c23165a124691b8e07139c..687979501ec7781488c460b5a615ae70ddcc40cd 100644 --- a/examples/device.rs +++ b/examples/device.rs @@ -5,7 +5,7 @@ #![no_std] #[allow(unused_extern_crates)] -extern crate panic_halt; +use panic_halt as _; use cortex_m::{iprint, peripheral::syst::SystClkSource}; use cortex_m_rt::entry; diff --git a/examples/exception.rs b/examples/exception.rs index b91ab9c8e84808f658425983c83b85214fb25355..1416ec4a7ca37ded4243ecb0ebd0d2844be7ab49 100644 --- a/examples/exception.rs +++ b/examples/exception.rs @@ -7,10 +7,11 @@ //! --- #![deny(unsafe_code)] +#![deny(warnings)] #![no_main] #![no_std] -extern crate panic_halt; +use panic_halt as _; use cortex_m::peripheral::syst::SystClkSource; use cortex_m::Peripherals; diff --git a/examples/exception_itm.rs b/examples/exception_itm.rs index 6460d71452756bd58e05eccb2e62da22d3d51818..bd78683296c587d33f48ee3994e14a1ff0df5d68 100644 --- a/examples/exception_itm.rs +++ b/examples/exception_itm.rs @@ -4,13 +4,12 @@ //! //! [1]: https://rust-embedded.github.io/cortex-m-rt/0.6.1/cortex_m_rt_macros/fn.exception.html //! -//! Notice, steal will panic! in debug mode, due to a `debug_assert` (ignored in release). //! --- #![no_main] #![no_std] -extern crate panic_halt; +use panic_halt as _; use cortex_m::peripheral::syst::SystClkSource; use cortex_m::{iprint, iprintln, Peripherals}; diff --git a/examples/exception_itm_raw.rs b/examples/exception_itm_raw.rs index f1ee7741c8fbd81093e398fc013ae843a7bfad8b..e346ff69c749821c332301b80a575e30b65f5716 100644 --- a/examples/exception_itm_raw.rs +++ b/examples/exception_itm_raw.rs @@ -8,7 +8,7 @@ #![no_main] #![no_std] -extern crate panic_halt; +use panic_halt as _; use cortex_m::peripheral::{syst::SystClkSource, ITM}; use cortex_m::{iprint, iprintln, Peripherals}; diff --git a/examples/hello.rs b/examples/hello.rs index aa4a095baae75995c420ad8ffa831e0d4cfda16c..c10c1ba341f3f2e40947825863e890af114dad3c 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,9 +1,10 @@ //! Prints "Hello, world!" on the host console using semihosting - +#![deny(unsafe_code)] +#![deny(warnings)] #![no_main] #![no_std] -extern crate panic_halt; +use panic_halt as _; use cortex_m_rt::entry; use cortex_m_semihosting::hprintln; diff --git a/examples/itm.rs b/examples/itm.rs index bb50f0ff349ed7599ba0c5753a349151ab4a0395..2dd8467ad5d7f64cfc9ba24154e6db33ba5df8f2 100644 --- a/examples/itm.rs +++ b/examples/itm.rs @@ -7,11 +7,12 @@ //! [`itmdump`]: https://docs.rs/itm/0.3.1/itm/ //! //! --- - +#![deny(unsafe_code)] +#![deny(warnings)] #![no_main] #![no_std] -extern crate panic_halt; +use panic_halt as _; use cortex_m::{iprintln, Peripherals}; use cortex_m_rt::entry; diff --git a/examples/panic.rs b/examples/panic.rs index 27ad3336db9e385fe37f3c690280e4f417cc86be..762cd7f85c195ab7a617257ae3bddf9db8a9505b 100644 --- a/examples/panic.rs +++ b/examples/panic.rs @@ -4,21 +4,21 @@ //! //! [0]: https://crates.io/keywords/panic-impl +#![deny(unsafe_code)] +#![deny(warnings)] #![no_main] #![no_std] // Pick one of these panic handlers: // `panic!` halts execution; the panic message is ignored -extern crate panic_halt; +use panic_halt as _; // Reports panic messages to the host stderr using semihosting -// NOTE to use this you need to uncomment the `panic-semihosting` dependency in Cargo.toml -// extern crate panic_semihosting; +//use panic_semihosting as _; // Logs panic messages using the ITM (Instrumentation Trace Macrocell) -// NOTE to use this you need to uncomment the `panic-itm` dependency in Cargo.toml -//extern crate panic_itm; +//use panic_itm as _; use cortex_m_rt::entry;