Browse Source

Add custom panic handling

Kevin Lee 2 years ago
parent
commit
1b7873fed1
3 changed files with 18 additions and 7 deletions
  1. 1 1
      Nixie_Firmware_Rust/Cargo.toml
  2. 5 4
      Nixie_Firmware_Rust/openocd.gdb
  3. 12 2
      Nixie_Firmware_Rust/src/main.rs

+ 1 - 1
Nixie_Firmware_Rust/Cargo.toml

@@ -9,7 +9,7 @@ version = "0.1.0"
 cortex-m = "0.6.0"
 cortex-m-rt = "0.6.10"
 cortex-m-semihosting = "0.3.3"
-panic-halt = "0.2.0"
+# panic-halt = "0.2.0"
 
 # Uncomment for the panic example.
 # panic-itm = "0.4.1"

+ 5 - 4
Nixie_Firmware_Rust/openocd.gdb

@@ -7,9 +7,10 @@ set print asm-demangle on
 set backtrace limit 32
 
 # detect unhandled exceptions, hard faults and panics
-break DefaultHandler
-break HardFault
-break rust_begin_unwind
+# break DefaultHandler
+# break HardFault
+# break rust_begin_unwind
+
 # # run the next few lines so the panic message is printed immediately
 # # the number needs to be adjusted for your panic handler
 # commands $bpnum
@@ -17,7 +18,7 @@ break rust_begin_unwind
 # end
 
 # *try* to stop at the user entry point (it might be gone due to inlining)
-break main
+# break main
 
 monitor arm semihosting enable
 

+ 12 - 2
Nixie_Firmware_Rust/src/main.rs

@@ -4,11 +4,12 @@
 #![cfg_attr(not(test), no_main)]
 
 // pick a panicking behavior
-#[cfg(not(test))]
-use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
+// #[cfg(not(test))]
+// use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
 // use panic_abort as _; // requires nightly
 // use panic_itm as _; // logs messages over ITM; requires ITM support
 // use panic_semihosting as _; // logs messages to the host stderr; requires a debugger
+use core::panic::PanicInfo; // custom panic handler
 
 // use cortex_m::asm;
 use cortex_m_rt::entry;
@@ -30,11 +31,20 @@ fn main() -> ! {
     let gpioa = &peripherals.GPIOA;
     gpioa.odr.modify(|_, w| w.odr0().set_bit());
 
+    panic!();
+
     loop {
         // your code goes here
     }
 }
 
+#[panic_handler]
+#[cfg(not(test))]
+fn panic(_info: &PanicInfo) -> ! {
+    hprintln!("Panic!").unwrap();
+    loop {}
+}
+
 fn add(a: i32, b: i32) -> i32 {
     a + b
 }