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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")]
#![no_std]
use core::sync::atomic::{AtomicBool, Ordering};
use cortex_m::{
asm, interrupt, itm,
peripheral::{itm::Stim, ITM},
register,
};
#[cfg(armv6m)]
compile_error!(
"`defmt-itm` cannot be used on Cortex-M0(+) chips, because it requires an ITM peripheral"
);
static ENABLED: AtomicBool = AtomicBool::new(false);
pub fn enable(itm: ITM) {
unsafe { itm.ter[0].write(1) }
drop(itm);
ENABLED.store(true, Ordering::Relaxed);
}
#[defmt::global_logger]
struct Logger;
static TAKEN: AtomicBool = AtomicBool::new(false);
static INTERRUPTS_ACTIVE: AtomicBool = AtomicBool::new(false);
static mut ENCODER: defmt::Encoder = defmt::Encoder::new();
unsafe impl defmt::Logger for Logger {
fn acquire() {
if !ENABLED.load(Ordering::Relaxed) {
panic!("defmt ITM logger is not enabled")
}
let primask = register::primask::read();
interrupt::disable();
if TAKEN.load(Ordering::Relaxed) {
panic!("defmt logger taken reentrantly")
}
TAKEN.store(true, Ordering::Relaxed);
INTERRUPTS_ACTIVE.store(primask.is_active(), Ordering::Relaxed);
unsafe { ENCODER.start_frame(do_write) }
}
unsafe fn flush() {
while !stim_0().is_fifo_ready() {}
asm::delay(100);
}
unsafe fn release() {
ENCODER.end_frame(do_write);
TAKEN.store(false, Ordering::Relaxed);
if INTERRUPTS_ACTIVE.load(Ordering::Relaxed) {
interrupt::enable()
}
}
unsafe fn write(bytes: &[u8]) {
ENCODER.write(bytes, do_write);
}
}
fn do_write(bytes: &[u8]) {
unsafe { itm::write_all(stim_0(), bytes) }
}
unsafe fn stim_0<'a>() -> &'a mut Stim {
&mut (*ITM::PTR).stim[0]
}