From 6929b56b4a5da4b41fddf0a51ef4d1786805c1db Mon Sep 17 00:00:00 2001 From: 30Wedge Date: Wed, 8 May 2024 17:23:49 -0400 Subject: [PATCH] Add a user-defined HardFault handler to print ExceptionFrame in crash example --- examples/crash.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/crash.rs b/examples/crash.rs index ab2d47b..8357806 100644 --- a/examples/crash.rs +++ b/examples/crash.rs @@ -82,8 +82,10 @@ use panic_halt as _; use core::ptr; +use core::fmt::Write; -use cortex_m_rt::entry; +use cortex_m_rt::{entry, exception, ExceptionFrame}; +use cortex_m_semihosting::hio; #[entry] fn main() -> ! { @@ -94,3 +96,12 @@ fn main() -> ! { loop {} } + +#[exception] +fn HardFault(ef: &ExceptionFrame) -> ! { + if let Ok(mut hstdout) = hio::hstdout() { + writeln!(hstdout, "{:#?}", ef).ok(); + } + + loop {} +}