Skip to content

Commit

Permalink
Merge #622 #661
Browse files Browse the repository at this point in the history
622: Refactor rtt [2/2] r=Urhengulas a=Urhengulas

This PR contains the not-error-prone changes from #573 and addresses the review given to #573.

661: `snapshot-tests`: Add tests for cell types r=Urhengulas a=Urhengulas

Follow-up to #656, which add snapshot tests for the new `defmt::Format` implementations.

Co-authored-by: Johann Hemmann <johann.hemmann@code.berlin>
  • Loading branch information
bors[bot] and Urhengulas authored Feb 15, 2022
3 parents 6f89322 + 28e8aba + 5a0729c commit 8b34b86
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
29 changes: 20 additions & 9 deletions firmware/defmt-rtt/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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() {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
}
}
5 changes: 5 additions & 0 deletions firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -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: <borrowed> }
INFO BorrowMutError: BorrowMutError
INFO BorrowError: BorrowError
INFO QEMU test finished!
16 changes: 16 additions & 0 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8b34b86

Please sign in to comment.