Skip to content

Commit

Permalink
Also print panic to the defmt channel
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 20, 2024
1 parent e84181c commit c310d05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions panic-rtt-target/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ repository = "https://github.com/probe-rs/rtt-target"
rtt-target = {version = "0.6.0", path = "../rtt-target" }
critical-section = "1.1.1"
portable-atomic = { version = "1.6.0", default-features = false }

defmt = { version = "0.3.0", optional = true }

[features]
default = []
defmt = ["dep:defmt"]

[package.metadata.docs.rs]
features = ["defmt"]
7 changes: 7 additions & 0 deletions panic-rtt-target/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ fn main() -> ! {
panic!("Something has gone terribly wrong");
}
```

# Defmt support

You can enable the `defmt` feature so that panics are printed to the defmt channel. If you do this
and have configured both a print and a defmt channel, the panic message will be printed to both.
The `defmt` feature doesn't automatically enable `rtt-target/defmt`. This allows you to use a
different defmt backend if needed.
8 changes: 6 additions & 2 deletions panic-rtt-target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
//! RTT must have been initialized by using one of the `rtt_init` macros. Otherwise you will get a
//! linker error at compile time.
//!
//! Panics are always logged to the print channel. Upon panicking the channel mode is also
//! automatically set to `BlockIfFull`, so that the full message will always be logged.
//! Panics are always logged to the print and defmt channels, if they are configured. Upon panicking
//! the channel mode is also automatically set to `BlockIfFull`, so that the full message will
//! always be logged.
//! If the code somehow manages to panic at runtime before RTT is initialized (quite unlikely),
//! or if the print channel doesn't exist, nothing is logged.
//!
Expand Down Expand Up @@ -48,6 +49,9 @@ use rtt_target::{with_terminal_channel, ChannelMode};
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
critical_section::with(|_| {
#[cfg(feature = "defmt")]
defmt::error!("{}", defmt::Display2Format(info));

with_terminal_channel(|term| {
term.set_mode(ChannelMode::BlockIfFull);
let mut channel = term.write(0);
Expand Down

0 comments on commit c310d05

Please sign in to comment.