Skip to content

Commit

Permalink
riscv32 support for panic-probe
Browse files Browse the repository at this point in the history
  • Loading branch information
t-moe committed Nov 28, 2023
1 parent 7a31dfb commit 5719804
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
14 changes: 10 additions & 4 deletions firmware/panic-probe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
[package]
authors = ["The Knurling Authors"]
categories = ["embedded", "no-std"]
description = "Panic handler that exits `probe-run` with an error code"
description = "Panic handler that exits `probe-rs` with an error code"
edition = "2021"
keywords = ["knurling", "panic-impl", "defmt", "probe-run"]
keywords = ["knurling", "panic-impl", "defmt", "probe-rs"]
license = "MIT OR Apache-2.0"
name = "panic-probe"
readme = "README.md"
repository = "https://github.com/knurling-rs/defmt"
version = "0.3.1"
version = "0.4.0"

[dependencies]
cortex-m = "0.7"
defmt = { version = "0.3", path = "../../defmt", optional = true }
rtt-target = { version = "0.4", optional = true }
semihosting = "0.1.4"

[target.'cfg(target_arch = "arm")'.dependencies]
cortex-m = "0.7"

[target.'cfg(target_arch = "riscv32")'.dependencies]
riscv = "0.10.1"


[features]
Expand Down
4 changes: 2 additions & 2 deletions firmware/panic-probe/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `panic-probe`

> Panic handler that exits [`probe-run`] with an error code
> Panic handler that exits [`probe-rs`] with an error code using semihosting::process::abort.
[`probe-run`]: https://github.com/knurling-rs/probe-run
[`probe-rs`]: https://github.com/probe-rs/probe-rs

`panic-probe` can optionally log the panic message using the [`defmt`] logging framework.
This functionality can be enabled through the `print-defmt` Cargo feature.
Expand Down
46 changes: 21 additions & 25 deletions firmware/panic-probe/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Panic handler for `probe-run`.
//! Panic handler for `probe-rs`.
//!
//! When this panic handler is used, panics will make `probe-run` print a backtrace and exit with a
//! non-zero status code, indicating failure. This building block can be used to run on-device
//! tests.
//! When this panic handler is used, panics will make `probe-rs` print a backtrace (by triggering a semihosting::process::abort).
//! Probe-rs will then exit with a non-zero status code, indicating failure.
//! This building block can be used to run on-device tests.
//!
//! # Panic Messages
//!
Expand All @@ -20,8 +20,8 @@
#![cfg(target_os = "none")]
#![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")]

#[cfg(not(cortex_m))]
compile_error!("`panic-probe` only supports Cortex-M targets (thumbvN-none-eabi[hf])");
#[cfg(all(not(cortex_m), not(target_arch = "riscv32")))]
compile_error!("`panic-probe` only supports Cortex-M targets or riscv32");

// Functionality `cfg`d out on platforms with OS/libstd.
#[cfg(target_os = "none")]
Expand All @@ -42,7 +42,7 @@ mod imp {
fn panic(info: &PanicInfo) -> ! {
static PANICKED: AtomicBool = AtomicBool::new(false);

cortex_m::interrupt::disable();
crate::disable_isr();

// Guard against infinite recursion, just in case.
if !PANICKED.load(Ordering::Relaxed) {
Expand All @@ -51,11 +51,11 @@ mod imp {
print(info);
}

crate::hard_fault();
crate::abort();
}
}

/// Trigger a `HardFault` via `udf` instruction.
/// Triggers a semihosting::process::abort
///
/// This function may be used to as `defmt::panic_handler` to avoid double prints.
///
Expand All @@ -64,26 +64,22 @@ mod imp {
/// ```
/// #[defmt::panic_handler]
/// fn panic() -> ! {
/// panic_probe::hard_fault();
/// panic_probe::abort();
/// }
/// ```
#[cfg(target_os = "none")]
pub fn hard_fault() -> ! {
// If `UsageFault` is enabled, we disable that first, since otherwise `udf` will cause that
// exception instead of `HardFault`.
#[cfg(not(any(armv6m, armv8m_base)))]
{
const SHCSR: *mut u32 = 0xE000ED24usize as _;
const USGFAULTENA: usize = 18;

unsafe {
let mut shcsr = core::ptr::read_volatile(SHCSR);
shcsr &= !(1 << USGFAULTENA);
core::ptr::write_volatile(SHCSR, shcsr);
}
}
pub fn abort() -> ! {
semihosting::process::abort();
}

cortex_m::asm::udf();
#[cfg(target_os = "none")]
pub fn disable_isr() {
#[cfg(cortex_m)]
cortex_m::interrupt::disable();
#[cfg(target_arch = "riscv32")]
unsafe {
riscv::interrupt::disable()
};
}

#[cfg(feature = "print-rtt")]
Expand Down

0 comments on commit 5719804

Please sign in to comment.