Skip to content

Commit 94feecc

Browse files
committed
defmt-rtt: Update to critical-section 1.0
1 parent 0209947 commit 94feecc

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

firmware/defmt-rtt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ version = "0.3.2"
1212

1313
[dependencies]
1414
defmt = { version = "0.3", path = "../../defmt" }
15-
critical-section = "0.2.5"
15+
critical-section = "1.0.0-alpha.2"

firmware/defmt-rtt/src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
mod channel;
2424

25-
use core::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
25+
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
2626

2727
use crate::channel::Channel;
2828

@@ -37,13 +37,13 @@ struct Logger;
3737

3838
/// Global logger lock.
3939
static TAKEN: AtomicBool = AtomicBool::new(false);
40-
static INTERRUPTS_ACTIVE: AtomicU8 = AtomicU8::new(0);
40+
static mut CS_RESTORE: critical_section::RestoreState = critical_section::RestoreState::invalid();
4141
static mut ENCODER: defmt::Encoder = defmt::Encoder::new();
4242

4343
unsafe impl defmt::Logger for Logger {
4444
fn acquire() {
4545
// safety: Must be paired with corresponding call to release(), see below
46-
let token = unsafe { critical_section::acquire() };
46+
let restore = unsafe { critical_section::acquire() };
4747

4848
if TAKEN.load(Ordering::Relaxed) {
4949
panic!("defmt logger taken reentrantly")
@@ -52,9 +52,10 @@ unsafe impl defmt::Logger for Logger {
5252
// no need for CAS because interrupts are disabled
5353
TAKEN.store(true, Ordering::Relaxed);
5454

55-
INTERRUPTS_ACTIVE.store(token, Ordering::Relaxed);
55+
// safety: accessing the `static mut` is OK because we have acquired a critical section.
56+
unsafe { CS_RESTORE = restore };
5657

57-
// safety: accessing the `static mut` is OK because we have disabled interrupts.
58+
// safety: accessing the `static mut` is OK because we have acquired a critical section.
5859
unsafe { ENCODER.start_frame(do_write) }
5960
}
6061

@@ -64,16 +65,20 @@ unsafe impl defmt::Logger for Logger {
6465
}
6566

6667
unsafe fn release() {
67-
// safety: accessing the `static mut` is OK because we have disabled interrupts.
68+
// safety: accessing the `static mut` is OK because we have acquired a critical section.
6869
ENCODER.end_frame(do_write);
6970

7071
TAKEN.store(false, Ordering::Relaxed);
72+
73+
// safety: accessing the `static mut` is OK because we have acquired a critical section.
74+
let restore = CS_RESTORE;
75+
7176
// safety: Must be paired with corresponding call to acquire(), see above
72-
critical_section::release(INTERRUPTS_ACTIVE.load(Ordering::Relaxed));
77+
critical_section::release(restore);
7378
}
7479

7580
unsafe fn write(bytes: &[u8]) {
76-
// safety: accessing the `static mut` is OK because we have disabled interrupts.
81+
// safety: accessing the `static mut` is OK because we have acquired a critical section.
7782
ENCODER.write(bytes, do_write);
7883
}
7984
}

0 commit comments

Comments
 (0)