defmt_rtt/channel.rs
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 110 111 112 113 114 115 116 117 118
use core::{
ptr,
sync::atomic::{AtomicUsize, Ordering},
};
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.
///
/// Currently, only the lowest 2 bits are used to set the channel mode (see constants below).
pub flags: AtomicUsize,
}
impl Channel {
pub fn write_all(&self, mut bytes: &[u8]) {
// 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 => Self::blocking_write,
false => Self::nonblocking_write,
};
while !bytes.is_empty() {
let consumed = write(self, bytes);
if consumed != 0 {
bytes = &bytes[consumed..];
}
}
}
fn blocking_write(&self, bytes: &[u8]) -> usize {
if bytes.is_empty() {
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 = available_buffer_size(read, write);
// abort if buffer is full
if available == 0 {
return 0;
}
self.write_impl(bytes, write, available)
}
fn nonblocking_write(&self, bytes: &[u8]) -> usize {
let write = self.write.load(Ordering::Acquire);
// NOTE truncate at BUF_SIZE to avoid more than one "wrap-around" in a single `write` call
self.write_impl(bytes, write, BUF_SIZE)
}
fn write_impl(&self, bytes: &[u8], cursor: usize, available: usize) -> usize {
let len = bytes.len().min(available);
// copy `bytes[..len]` to the RTT buffer
unsafe {
if cursor + len > BUF_SIZE {
// split memcpy
let pivot = BUF_SIZE - cursor;
ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), pivot);
ptr::copy_nonoverlapping(bytes.as_ptr().add(pivot), self.buffer, len - pivot);
} else {
// single memcpy
ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), len);
}
}
// adjust the write pointer, so the host knows that there is new data
self.write
.store(cursor.wrapping_add(len) % BUF_SIZE, Ordering::Release);
// return the number of bytes written
len
}
pub fn flush(&self) {
// return early, if host is disconnected
if !self.host_is_connected() {
return;
}
// busy wait, until the read- catches up with the write-pointer
let read = || self.read.load(Ordering::Relaxed);
let write = || self.write.load(Ordering::Relaxed);
while read() != write() {}
}
fn host_is_connected(&self) -> bool {
// we assume that a host is connected if we are in blocking-mode. this is what probe-run does.
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
}
}