Skip to content

Commit

Permalink
defmt-rtt: Update to critical-section 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Aug 10, 2022
1 parent 0209947 commit 94feecc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion firmware/defmt-rtt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ version = "0.3.2"

[dependencies]
defmt = { version = "0.3", path = "../../defmt" }
critical-section = "0.2.5"
critical-section = "1.0.0-alpha.2"
21 changes: 13 additions & 8 deletions firmware/defmt-rtt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

mod channel;

use core::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use crate::channel::Channel;

Expand All @@ -37,13 +37,13 @@ struct Logger;

/// Global logger lock.
static TAKEN: AtomicBool = AtomicBool::new(false);
static INTERRUPTS_ACTIVE: AtomicU8 = AtomicU8::new(0);
static mut CS_RESTORE: critical_section::RestoreState = critical_section::RestoreState::invalid();
static mut ENCODER: defmt::Encoder = defmt::Encoder::new();

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

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

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

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

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

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

TAKEN.store(false, Ordering::Relaxed);

// safety: accessing the `static mut` is OK because we have acquired a critical section.
let restore = CS_RESTORE;

// safety: Must be paired with corresponding call to acquire(), see above
critical_section::release(INTERRUPTS_ACTIVE.load(Ordering::Relaxed));
critical_section::release(restore);
}

unsafe fn write(bytes: &[u8]) {
// safety: accessing the `static mut` is OK because we have disabled interrupts.
// safety: accessing the `static mut` is OK because we have acquired a critical section.
ENCODER.write(bytes, do_write);
}
}
Expand Down

0 comments on commit 94feecc

Please sign in to comment.