aboutsummaryrefslogtreecommitdiff
path: root/examples-runner/src/lib.rs
blob: 13b8c95435e197f7e9b7d4aab8641036d624e536 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![no_std]


pub use embedded_ci_pac as pac; // memory layout

#[cfg(feature = "embedded-ci")]
use core::sync::atomic::{AtomicUsize, Ordering};

#[cfg(feature = "qemu")]
use panic_semihosting as _;

#[cfg(feature = "embedded-ci")]
use defmt_rtt as _; // global logger
#[cfg(feature = "embedded-ci")]
use panic_probe as _;

#[cfg(feature = "embedded-ci")]
defmt::timestamp! {"{=u64}", {
    static COUNT: AtomicUsize = AtomicUsize::new(0);
    // NOTE(no-CAS) `timestamps` runs with interrupts disabled
    let n = COUNT.load(Ordering::Relaxed);
    COUNT.store(n + 1, Ordering::Relaxed);
    n as u64
}
}

// same panicking *behavior* as `panic-probe` but doesn't print a panic message
// this prevents the panic message being printed *twice* when `defmt::panic` is invoked
#[cfg(feature = "embedded-ci")]
#[defmt::panic_handler]
fn panic() -> ! {
    cortex_m::asm::udf()
}

/// Generalize println for QEMU and defmt
#[cfg(feature = "embedded-ci")]
#[macro_export]
macro_rules! println {
    ($($l:tt)*) => {
        defmt::println!($($l)*);
    }
}

#[cfg(feature = "qemu")]
#[macro_export]
macro_rules! println {
    ($($l:tt)*) => {
        cortex_m_semihosting::hprintln!($($l)*).ok();
    }
}

/// Terminates the application and makes `probe-run` exit with exit-code = 0
pub fn exit() -> ! {
    #[cfg(feature = "qemu")]
    cortex_m_semihosting::debug::exit(cortex_m_semihosting::debug::EXIT_SUCCESS);

    loop {
        cortex_m::asm::bkpt();
    }
}