From 1b48da7be0cb31ac2e09ee14944271e52366eb6d Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 15 May 2019 08:11:26 +0000 Subject: [PATCH] rust/k210-shared: More helpful panic handler Try sending the panic message to UARTHS. --- rust/accelerometer/Cargo.toml | 1 - rust/accelerometer/src/main.rs | 2 -- rust/game-of-life/Cargo.toml | 1 - rust/game-of-life/src/main.rs | 2 -- rust/k210-console/Cargo.toml | 1 - rust/k210-console/src/main.rs | 2 -- rust/k210-shared/src/lib.rs | 1 + rust/k210-shared/src/panic.rs | 25 +++++++++++++++++++++++++ rust/mandelbrot/Cargo.toml | 1 - rust/mandelbrot/src/main.rs | 2 -- 10 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 rust/k210-shared/src/panic.rs diff --git a/rust/accelerometer/Cargo.toml b/rust/accelerometer/Cargo.toml index dcbe96a..cc0b03c 100644 --- a/rust/accelerometer/Cargo.toml +++ b/rust/accelerometer/Cargo.toml @@ -5,7 +5,6 @@ authors = ["W.J. van der Laan "] edition = "2018" [dependencies] -panic-halt = "0.2.0" riscv-rt = "0.5.0" k210-hal = "0.1.0" riscv = "0.5" diff --git a/rust/accelerometer/src/main.rs b/rust/accelerometer/src/main.rs index 6c14c50..0e174f2 100644 --- a/rust/accelerometer/src/main.rs +++ b/rust/accelerometer/src/main.rs @@ -4,8 +4,6 @@ #![no_std] #![no_main] -extern crate panic_halt; - use k210_hal::pac; use k210_hal::prelude::*; use k210_hal::stdout::Stdout; diff --git a/rust/game-of-life/Cargo.toml b/rust/game-of-life/Cargo.toml index f8f47f0..25bbf2d 100644 --- a/rust/game-of-life/Cargo.toml +++ b/rust/game-of-life/Cargo.toml @@ -5,7 +5,6 @@ authors = ["W.J. van der Laan "] edition = "2018" [dependencies] -panic-halt = "0.2.0" riscv-rt = "0.5.0" k210-hal = "0.1.0" riscv = "0.5" diff --git a/rust/game-of-life/src/main.rs b/rust/game-of-life/src/main.rs index 52d751a..202e7bc 100644 --- a/rust/game-of-life/src/main.rs +++ b/rust/game-of-life/src/main.rs @@ -4,8 +4,6 @@ #![no_std] #![no_main] -extern crate panic_halt; - use k210_hal::pac; use k210_hal::prelude::*; use k210_hal::stdout::Stdout; diff --git a/rust/k210-console/Cargo.toml b/rust/k210-console/Cargo.toml index 24b599e..8c2e02e 100644 --- a/rust/k210-console/Cargo.toml +++ b/rust/k210-console/Cargo.toml @@ -5,7 +5,6 @@ authors = ["W.J. van der Laan "] edition = "2018" [dependencies] -panic-halt = "0.2.0" riscv-rt = "0.5.0" k210-hal = "0.1.0" riscv = "0.5" diff --git a/rust/k210-console/src/main.rs b/rust/k210-console/src/main.rs index 437d9b4..970a492 100644 --- a/rust/k210-console/src/main.rs +++ b/rust/k210-console/src/main.rs @@ -4,8 +4,6 @@ #![no_std] #![no_main] -extern crate panic_halt; - mod console; mod cp437; mod cp437_8x8; diff --git a/rust/k210-shared/src/lib.rs b/rust/k210-shared/src/lib.rs index b499278..2411eb3 100644 --- a/rust/k210-shared/src/lib.rs +++ b/rust/k210-shared/src/lib.rs @@ -4,5 +4,6 @@ #![no_std] pub mod board; +pub mod panic; pub mod soc; mod util; diff --git a/rust/k210-shared/src/panic.rs b/rust/k210-shared/src/panic.rs new file mode 100644 index 0000000..9ed5aa9 --- /dev/null +++ b/rust/k210-shared/src/panic.rs @@ -0,0 +1,25 @@ +/** Panic handler: based on ARM panic-itm */ +use k210_hal::pac; +use k210_hal::prelude::*; +use k210_hal::stdout::Stdout; +use core::panic::PanicInfo; +use core::sync::atomic::{self, Ordering}; + +/** Send panic messages to UARTHS at 115200 baud */ +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + // Stealing all peripherals, re-initializing the clocks and serial seems overkill here, but + // also, can we really know the state? + let p = unsafe { pac::Peripherals::steal() }; + let clocks = k210_hal::clock::Clocks::new(); + let serial = p.UARTHS.constrain(115_200.bps(), &clocks); + let (mut tx, _) = serial.split(); + let mut stdout = Stdout(&mut tx); + writeln!(stdout, "{}", info).unwrap(); + + loop { + // add some side effect to prevent this from turning into a UDF instruction + // see rust-lang/rust#28728 for details + atomic::compiler_fence(Ordering::SeqCst) + } +} diff --git a/rust/mandelbrot/Cargo.toml b/rust/mandelbrot/Cargo.toml index 470156c..9717bc6 100644 --- a/rust/mandelbrot/Cargo.toml +++ b/rust/mandelbrot/Cargo.toml @@ -5,7 +5,6 @@ authors = ["W.J. van der Laan "] edition = "2018" [dependencies] -panic-halt = "0.2.0" riscv-rt = "0.5.0" k210-hal = "0.1.0" riscv = "0.5" diff --git a/rust/mandelbrot/src/main.rs b/rust/mandelbrot/src/main.rs index 77255ba..8ba212a 100644 --- a/rust/mandelbrot/src/main.rs +++ b/rust/mandelbrot/src/main.rs @@ -4,8 +4,6 @@ #![no_std] #![no_main] -extern crate panic_halt; - mod palette; use k210_hal::pac;