aboutsummaryrefslogtreecommitdiff
path: root/panic-itm/src
diff options
context:
space:
mode:
authorGravatar Jonas Schievink <jonasschievink@gmail.com> 2020-11-11 00:35:40 +0100
committerGravatar Jonas Schievink <jonasschievink@gmail.com> 2020-11-11 00:35:40 +0100
commit5b65e95f816dfc96a1921c8837127581215aafd6 (patch)
treeb45e99d9f8e7368b6920afab5b5420e4c49feed5 /panic-itm/src
parentbc8c562cf110d64d20461ff19052559fba5e500b (diff)
downloadcortex-m-5b65e95f816dfc96a1921c8837127581215aafd6.tar.gz
cortex-m-5b65e95f816dfc96a1921c8837127581215aafd6.tar.zst
cortex-m-5b65e95f816dfc96a1921c8837127581215aafd6.zip
Import panic-itm
Diffstat (limited to 'panic-itm/src')
-rw-r--r--panic-itm/src/lib.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/panic-itm/src/lib.rs b/panic-itm/src/lib.rs
new file mode 100644
index 0000000..eedc38c
--- /dev/null
+++ b/panic-itm/src/lib.rs
@@ -0,0 +1,58 @@
+//! Log panic messages using the ITM (Instrumentation Trace Macrocell)
+//!
+//! This crate contains an implementation of `panic_fmt` that logs panic messages to the ITM
+//! stimulus port 0. Before printing the message the panic handler disables (masks) all the device
+//! specific interrupts. After printing the message the panic handler goes into an infinite loop.
+//!
+//! # Usage
+//!
+//! ``` ignore
+//! #![no_std]
+//!
+//! extern crate panic_itm;
+//!
+//! fn main() {
+//! panic!("FOO")
+//! }
+//! ```
+//!
+//! ``` text
+//! (gdb) monitor tpiu config external uart off 8000000 2000000
+//! (gdb) monitor itm port 0 on
+//! (gdb) continue
+//! (..)
+//! ```
+//!
+//! ``` text
+//! $ itmdump -f /dev/ttyUSB0
+//! panicked at 'FOO', src/main.rs:6:5
+//! ```
+
+#![deny(missing_docs)]
+#![deny(warnings)]
+#![no_std]
+
+extern crate cortex_m;
+
+use core::panic::PanicInfo;
+use core::sync::atomic::{self, Ordering};
+
+use cortex_m::interrupt;
+use cortex_m::iprintln;
+use cortex_m::peripheral::ITM;
+
+#[panic_handler]
+fn panic(info: &PanicInfo) -> ! {
+ interrupt::disable();
+
+ let itm = unsafe { &mut *ITM::ptr() };
+ let stim = &mut itm.stim[0];
+
+ iprintln!(stim, "{}", info);
+
+ 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);
+ }
+}