Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defmt-rtt: Refactor rtt [3/2] #695

Merged
merged 5 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- [#689]: defmt-rtt: Update to critical-section 1.0
- [#692]: Wrap const fn in const item to ensure compile-time-evaluation.
- [#695]: `defmt-rtt`: Refactor rtt [3/2]
- [#689]: `defmt-rtt`: Update to critical-section 1.0
- [#692]: `defmt-macros`: Wrap const fn in const item to ensure compile-time-evaluation.
- [#690]: Satisfy clippy
- [#688]: Release `defmt-decoder 0.3.3`
- [#687]: `CI`: Re-enable `qemu-snapshot (nightly)` tests
- [#686]: `CI`: Temporarily disable `qemu-snapshot (nightly)`
- [#684]: Fix `syn` dependency version in `defmt-macros`.
- [#683]: defmt-rtt: Make sure the whole RTT structure is in RAM
- [#682]: defmt-print: exit when stdin is closed
- [#683]: `defmt-rtt`: Make sure the whole RTT structure is in RAM
- [#682]: `defmt-print`: exit when stdin is closed
- [#681]: Make use of i/o locking being static since rust `1.61`.
- [#679]: Add changelog enforcer
- [#679]: `CI`: Add changelog enforcer
- [#678]: Satisfy clippy

[#695]: https://github.com/knurling-rs/defmt/pull/695
[#692]: https://github.com/knurling-rs/defmt/pull/692
[#690]: https://github.com/knurling-rs/defmt/pull/690
[#688]: https://github.com/knurling-rs/defmt/pull/688
Expand Down
35 changes: 13 additions & 22 deletions firmware/defmt-rtt/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,20 @@ impl Channel {
return 0;
}

let cursor = write;
let len = bytes.len().min(available);

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);
}
}
self.write
.store(write.wrapping_add(len) % BUF_SIZE, Ordering::Release);

len
self.write_impl(bytes, write, available)
}

fn nonblocking_write(&self, bytes: &[u8]) -> usize {
let write = self.write.load(Ordering::Acquire);
let cursor = write;
// NOTE truncate atBUF_SIZE to avoid more than one "wrap-around" in a single `write` call
let len = bytes.len().min(BUF_SIZE);

// 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
Expand All @@ -91,9 +79,12 @@ impl Channel {
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(write.wrapping_add(len) % BUF_SIZE, Ordering::Release);
.store(cursor.wrapping_add(len) % BUF_SIZE, Ordering::Release);

// return the number of bytes written
len
}

Expand Down