#error-logging #panic-message #rtt

no-std dev panic-rtt-target

Logs panic messages over RTT using rtt-target

4 releases

0.1.3 Jan 4, 2024
0.1.2 Apr 27, 2021
0.1.1 Aug 21, 2020
0.1.0 Apr 19, 2020

#3 in #panic-message

Download history 2192/week @ 2024-06-13 1336/week @ 2024-06-20 2266/week @ 2024-06-27 1986/week @ 2024-07-04 2369/week @ 2024-07-11 1225/week @ 2024-07-18 1494/week @ 2024-07-25 1453/week @ 2024-08-01 1414/week @ 2024-08-08 1577/week @ 2024-08-15 1148/week @ 2024-08-22 1290/week @ 2024-08-29 1723/week @ 2024-09-05 1407/week @ 2024-09-12 1259/week @ 2024-09-19 1078/week @ 2024-09-26

5,696 downloads per month
Used in 29 crates

MIT license

45KB
623 lines

panic-rtt-target

crates.io documentation

Logs panic messages over RTT. A companion crate for rtt-target.

Documentation

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 on channel 0. 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 channel 0 doesn't exist, nothing is logged.

The panic handler runs in a non-returning critical_section which implementation should be provided by the user.

Usage

Cargo.toml:

[dependencies]
cortex-m = { version = "0.7.6", features = ["critical-section-single-core"]}
panic-rtt-target = { version = "x.y.z" }

main.rs:

#![no_std]

use panic_rtt_target as _;
use rtt_target::rtt_init_default;

fn main() -> ! {
    // you can use any init macro as long as it creates channel 0
    rtt_init_default!();

    panic!("Something has gone terribly wrong");
}

Implementation details

The provided interrupt handler checks if RTT channel 0 is configured, writes the info and enters an infinite loop. If RTT channel 0 is not configured, the panic handler enters the failed to get channel infinite loop. The final state can be observed by breaking/halting the target.

fn panic(info: &PanicInfo) -> ! {
    critical_section::with(|_| {
        if let Some(mut channel) = unsafe { UpChannel::conjure(0) } {
            channel.set_mode(ChannelMode::BlockIfFull);

            writeln!(channel, "{}", info).ok();
        } else {
            // failed to get channel, but not much else we can do but spin
            loop {
                compiler_fence(SeqCst);
            }
        }

        // we should never leave critical section
        loop {
            compiler_fence(SeqCst);
        }
    })
}

Dependencies

~36KB