Skip to content

Commit

Permalink
Merge #695
Browse files Browse the repository at this point in the history
695: `defmt-rtt`: Refactor rtt [3/2] r=Urhengulas a=Urhengulas

This is the second attempt to land #573. This time I am not touching atomic variables, but only de-duplicate the almost copy pasted parts of `blocking_write` and `nonblocking_write`. Especially the first commit makes the duplication quite clear.

Co-authored-by: Urhengulas <johann.hemmann@code.berlin>
  • Loading branch information
bors[bot] and Urhengulas authored Oct 3, 2022
2 parents f379ff8 + b5c4917 commit 6be572e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
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

0 comments on commit 6be572e

Please sign in to comment.