Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

risc-v support for defmt-test #796

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

- [#789]: `defmt`: Add support for new time-related display hints
- [#796]: `panic-probe`: Add riscv32 support, `print-log` feature and employ critical-section on panic.

[#789]: https://github.com/knurling-rs/defmt/pull/789
[#796]: https://github.com/knurling-rs/defmt/pull/796

## defmt-decoder v0.3.9, defmt-print v0.3.10 - 2023-10-04

Expand Down
4 changes: 4 additions & 0 deletions firmware/defmt-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# [Unreleased]

- [#796] Add support for risc-v, but do no longer import the cortex-m exception handlers

[#796]: https://github.com/knurling-rs/defmt/pull/796

# [v0.3.1]

- [#786]: `defmt-test`: Exit with semihosting exit
Expand Down
3 changes: 1 addition & 2 deletions firmware/defmt-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ repository = "https://github.com/knurling-rs/defmt"
version = "0.3.1"

[dependencies]
cortex-m-rt = "0.7"
cortex-m-semihosting = "0.5"
semihosting = "0.1.4"
defmt = { version = "0.3", path = "../../defmt" }
defmt-test-macros = { version = "=0.3.0", path = "macros" }
6 changes: 1 addition & 5 deletions firmware/defmt-test/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
use cortex_m_rt as _;
use cortex_m_semihosting::debug;
pub use defmt::info;

use crate::TestOutcome;

/// Terminates the application and makes a semihosting-capable debug tool exit
/// with status code 0.
pub fn exit() -> ! {
loop {
debug::exit(debug::EXIT_SUCCESS);
}
semihosting::process::exit(0);
}

pub fn check_outcome<T: TestOutcome>(outcome: T, should_error: bool) {
Expand Down
10 changes: 7 additions & 3 deletions firmware/panic-probe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
[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"

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


[features]
# Print the panic message using `rtt-target`.
print-rtt = ["rtt-target"]
# Print the panic message using `defmt`.
print-defmt = ["defmt", "defmt-error"]
# Print the panic message using `log`
print-log = ["log"]

defmt-error = [] # internal feature, do not use

Expand Down
12 changes: 6 additions & 6 deletions firmware/panic-probe/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# `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.

[`defmt`]: https://github.com/knurling-rs/defmt
`panic-probe` can optionally log the panic message. Enable one of the following features for that:
* `print-defmt` to print via `defmt::error!(..)`
* `print-log` to print via `log::error!(..)`
* `print-rtt` to print via `rtt_target::rprintln(..)`

## Support

Expand Down
67 changes: 32 additions & 35 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 @@ -35,27 +35,29 @@ mod imp {
#[cfg(feature = "print-defmt")]
use crate::print_defmt::print;

#[cfg(not(any(feature = "print-rtt", feature = "print-defmt")))]
#[cfg(feature = "print-log")]
use crate::print_log::print;

#[cfg(not(any(feature = "print-rtt", feature = "print-defmt", feature = "print-log")))]
fn print(_: &core::panic::PanicInfo) {}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
static PANICKED: AtomicBool = AtomicBool::new(false);

cortex_m::interrupt::disable();

// Guard against infinite recursion, just in case.
if !PANICKED.load(Ordering::Relaxed) {
PANICKED.store(true, Ordering::Relaxed);
critical_section::with(|_| {
// Guard against infinite recursion, just in case.
static PANICKED: AtomicBool = AtomicBool::new(false);
if !PANICKED.load(Ordering::Relaxed) {
PANICKED.store(true, Ordering::Relaxed);

print(info);
}
print(info);
}

crate::hard_fault();
crate::abort() // this call will never return, therefore we stay in the critical section forever.
})
}
}

/// 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 +66,12 @@ 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);
}
}

cortex_m::asm::udf();
pub fn abort() -> ! {
semihosting::process::abort();
}

#[cfg(feature = "print-rtt")]
Expand All @@ -104,3 +92,12 @@ mod print_defmt {
defmt::error!("{}", defmt::Display2Format(info));
}
}

#[cfg(feature = "print-log")]
mod print_log {
use core::panic::PanicInfo;

pub fn print(info: &PanicInfo) {
log::error!("{}", info);
}
}
1 change: 1 addition & 0 deletions firmware/qemu/tests/defmt-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use core::sync::atomic::{AtomicBool, Ordering};

use cortex_m_rt as _; // exception handlers
use defmt_semihosting as _; // global logger

static MAY_PANIC: AtomicBool = AtomicBool::new(false);
Expand Down
Loading