diff --git a/firmware/defmt-rtt/src/channel.rs b/firmware/defmt-rtt/src/channel.rs index 3293bfe1..6625d313 100644 --- a/firmware/defmt-rtt/src/channel.rs +++ b/firmware/defmt-rtt/src/channel.rs @@ -5,12 +5,16 @@ use core::{ use crate::{consts::BUF_SIZE, MODE_BLOCK_IF_FULL, MODE_MASK}; +/// RTT Up channel #[repr(C)] pub(crate) struct Channel { pub name: *const u8, + /// Pointer to the RTT buffer. pub buffer: *mut u8, pub size: usize, + /// Written by the target. pub write: AtomicUsize, + /// Written by the host. pub read: AtomicUsize, /// Channel properties. /// @@ -23,8 +27,8 @@ impl Channel { // the host-connection-status is only modified after RAM initialization while the device is // halted, so we only need to check it once before the write-loop let write = match self.host_is_connected() { - true => Channel::blocking_write, - false => Channel::nonblocking_write, + true => Self::blocking_write, + false => Self::nonblocking_write, }; while !bytes.is_empty() { @@ -40,16 +44,12 @@ impl Channel { return 0; } + // calculate how much space is left in the buffer let read = self.read.load(Ordering::Relaxed); let write = self.write.load(Ordering::Acquire); - let available = if read > write { - read - write - 1 - } else if read == 0 { - BUF_SIZE - write - 1 - } else { - BUF_SIZE - write - }; + let available = available_buffer_size(read, write); + // abort if buffer is full if available == 0 { return 0; } @@ -114,3 +114,14 @@ impl Channel { self.flags.load(Ordering::Relaxed) & MODE_MASK == MODE_BLOCK_IF_FULL } } + +/// How much space is left in the buffer? +fn available_buffer_size(read_cursor: usize, write_cursor: usize) -> usize { + if read_cursor > write_cursor { + read_cursor - write_cursor - 1 + } else if read_cursor == 0 { + BUF_SIZE - write_cursor - 1 + } else { + BUF_SIZE - write_cursor + } +} diff --git a/firmware/qemu/src/bin/log.out b/firmware/qemu/src/bin/log.out index 2cf2e2ac..36b7b409 100644 --- a/firmware/qemu/src/bin/log.out +++ b/firmware/qemu/src/bin/log.out @@ -150,4 +150,9 @@ INFO ccbbaadd INFO log data: 43981 INFO flush! 🚽 INFO log more data! 🎉 +INFO Cell: Cell { value: 43981 }) +INFO RefCell: RefCell { value: 43981 } +INFO borrowed RefCell: RefCell { value: } +INFO BorrowMutError: BorrowMutError +INFO BorrowError: BorrowError INFO QEMU test finished! diff --git a/firmware/qemu/src/bin/log.rs b/firmware/qemu/src/bin/log.rs index 146ff79d..f829ecf1 100644 --- a/firmware/qemu/src/bin/log.rs +++ b/firmware/qemu/src/bin/log.rs @@ -716,6 +716,22 @@ fn main() -> ! { defmt::flush(); defmt::info!("log more data! 🎉"); + { + use core::cell; + + let a = cell::Cell::new(0xABCD); + let b = cell::RefCell::new(0xABCD); + defmt::info!("Cell: {}", a); + defmt::info!("RefCell: {}", b); + + let _c = b.try_borrow_mut().unwrap(); + let d = b.try_borrow_mut().unwrap_err(); + let e = b.try_borrow().unwrap_err(); + defmt::info!("borrowed RefCell: {}", b); + defmt::info!("BorrowMutError: {}", d); + defmt::info!("BorrowError: {}", e); + } + defmt::info!("QEMU test finished!"); loop {