From 95fc8944e3e6d975959924258890f239471c1984 Mon Sep 17 00:00:00 2001 From: alexmoon Date: Fri, 15 Jul 2022 12:16:18 -0400 Subject: Remove `nrf-softdevice-defmt-rtt` since `defmt-rtt` v0.3.2+ supports `critical-section` --- Cargo.lock | 62 ++++------------ Cargo.toml | 1 - nrf-softdevice-defmt-rtt/Cargo.toml | 17 ----- nrf-softdevice-defmt-rtt/README.md | 42 ----------- nrf-softdevice-defmt-rtt/src/channel.rs | 112 ---------------------------- nrf-softdevice-defmt-rtt/src/lib.rs | 125 -------------------------------- 6 files changed, 15 insertions(+), 344 deletions(-) delete mode 100644 nrf-softdevice-defmt-rtt/Cargo.toml delete mode 100644 nrf-softdevice-defmt-rtt/README.md delete mode 100644 nrf-softdevice-defmt-rtt/src/channel.rs delete mode 100644 nrf-softdevice-defmt-rtt/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8716a23..ebfcb7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,15 +21,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "aligned" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a785a543aea40f5e4e2e93bb2655d31bc21bb391fff65697150973e383f16bb" -dependencies = [ - "as-slice 0.1.5", -] - [[package]] name = "as-slice" version = "0.1.5" @@ -135,19 +126,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cortex-m" -version = "0.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9075300b07c6a56263b9b582c214d0ff037b00d45ec9fde1cc711490c56f1bb9" -dependencies = [ - "aligned", - "bare-metal 0.2.5", - "bitfield", - "cortex-m 0.7.5", - "volatile-register", -] - [[package]] name = "cortex-m" version = "0.7.5" @@ -188,7 +166,7 @@ checksum = "95da181745b56d4bd339530ec393508910c909c784e8962d15d722bacf0bcbcd" dependencies = [ "bare-metal 1.0.0", "cfg-if", - "cortex-m 0.7.5", + "cortex-m", "riscv", ] @@ -308,7 +286,7 @@ source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359 dependencies = [ "atomic-polyfill", "cfg-if", - "cortex-m 0.7.5", + "cortex-m", "critical-section", "defmt", "embassy-macros", @@ -326,7 +304,7 @@ source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359 dependencies = [ "atomic-polyfill", "cfg-if", - "cortex-m 0.7.5", + "cortex-m", "critical-section", "embassy", "embassy-hal-common", @@ -352,7 +330,7 @@ name = "embassy-hal-common" version = "0.1.0" source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "embassy", "num-traits", ] @@ -374,7 +352,7 @@ version = "0.1.0" source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "cfg-if", - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "critical-section", "defmt", @@ -649,7 +627,7 @@ checksum = "546c37ac5d9e56f55e73b677106873d9d9f5190605e41a856503623648488cae" name = "nrf-softdevice" version = "0.1.0" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "critical-section", "defmt", @@ -676,22 +654,12 @@ dependencies = [ "num_enum", ] -[[package]] -name = "nrf-softdevice-defmt-rtt" -version = "0.1.0" -dependencies = [ - "cortex-m 0.6.7", - "critical-section", - "defmt", - "nrf-softdevice", -] - [[package]] name = "nrf-softdevice-examples" version = "0.1.0" dependencies = [ "atomic-pool", - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "defmt", "defmt-rtt", @@ -749,7 +717,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0cd69f02212c43d0abf0f29b2b38657efe06f60719606398427e307ef2141da" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -760,7 +728,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c781b61666a97f249989d25c2997cefcf72a29721a2628cf666a4195566933b9" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -771,7 +739,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be89f9a72ea52c1fc1712db6cb9adebf95fb16598d5919e7d3dbd6e13b7913ce" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -782,7 +750,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccfc0d290dc3bf619206bd32d012778692c62401b14eda67091609feff1629e0" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -793,7 +761,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e649284d1cde4aef5c43ea32588553cfd31a9b7ce5fccdac98f012399382c33a" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -804,7 +772,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd2b96c950afd2887102113c5194cb5c985a13aff0eac5fb976a76cd6e62a21" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -815,7 +783,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9efa9d1e910e3b825fd7e28f369363a8e036b386b77e21035b9f0e510de199cf" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "cortex-m-rt", "vcell", ] @@ -855,7 +823,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ab1f00eac22bd18f8e5cae9555f2820b3a0c166b5b556ee3e203746ea6dcf3a" dependencies = [ - "cortex-m 0.7.5", + "cortex-m", "defmt", ] diff --git a/Cargo.toml b/Cargo.toml index 3debfa0..6a90ca9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ members = [ "nrf-softdevice-macro", "examples", - "nrf-softdevice-defmt-rtt", ] exclude = [ diff --git a/nrf-softdevice-defmt-rtt/Cargo.toml b/nrf-softdevice-defmt-rtt/Cargo.toml deleted file mode 100644 index df812b5..0000000 --- a/nrf-softdevice-defmt-rtt/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -authors = ["The Knurling-rs developers"] -categories = ["embedded", "no-std"] -description = "Transmit defmt log messages over the RTT (Real-Time Transfer) protocol" -edition = "2021" -keywords = ["knurling", "defmt", "defmt-transport"] -license = "MIT OR Apache-2.0" -name = "nrf-softdevice-defmt-rtt" -readme = "README.md" -repository = "https://github.com/knurling-rs/defmt" -version = "0.1.0" - -[dependencies] -defmt = { version = "0.3" } -nrf-softdevice = { path = "../nrf-softdevice", version = "0.1.0" } -cortex-m = "0.6.4" -critical-section = { version = "0.2.1" } diff --git a/nrf-softdevice-defmt-rtt/README.md b/nrf-softdevice-defmt-rtt/README.md deleted file mode 100644 index 7c69179..0000000 --- a/nrf-softdevice-defmt-rtt/README.md +++ /dev/null @@ -1,42 +0,0 @@ -# `defmt-rtt` - -> Transmit [`defmt`] log messages over the RTT (Real-Time Transfer) protocol - -[`defmt`]: https://github.com/knurling-rs/defmt - -`defmt` ("de format", short for "deferred formatting") is a highly efficient logging framework that targets resource-constrained devices, like microcontrollers. - -The fastest way to get started with `defmt` is to use our [app-template] to set up a new Cortex-M embedded project. - -[app-template]: https://github.com/knurling-rs/app-template - -For more details about the framework check the book at https://defmt.ferrous-systems.com - -## Support - -`defmt-rtt` is part of the [Knurling] project, [Ferrous Systems]' effort at -improving tooling used to develop for embedded systems. - -If you think that our work is useful, consider sponsoring it via [GitHub -Sponsors]. - -## License - -Licensed under either of - -- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or - http://www.apache.org/licenses/LICENSE-2.0) - -- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) - -at your option. - -### Contribution - -Unless you explicitly state otherwise, any contribution intentionally submitted -for inclusion in the work by you, as defined in the Apache-2.0 license, shall be -licensed as above, without any additional terms or conditions. - -[Knurling]: https://knurling.ferrous-systems.com/ -[Ferrous Systems]: https://ferrous-systems.com/ -[GitHub Sponsors]: https://github.com/sponsors/knurling-rs diff --git a/nrf-softdevice-defmt-rtt/src/channel.rs b/nrf-softdevice-defmt-rtt/src/channel.rs deleted file mode 100644 index d0d899f..0000000 --- a/nrf-softdevice-defmt-rtt/src/channel.rs +++ /dev/null @@ -1,112 +0,0 @@ -use core::ptr; -use core::sync::atomic::{AtomicUsize, Ordering}; - -use crate::{MODE_BLOCK_IF_FULL, MODE_MASK, SIZE}; - -#[repr(C)] -pub(crate) struct Channel { - pub name: *const u8, - pub buffer: *mut u8, - pub size: usize, - pub write: AtomicUsize, - 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 => Channel::blocking_write, - false => Channel::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; - } - - 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 { - SIZE - write - 1 - } else { - SIZE - write - }; - - if available == 0 { - return 0; - } - - let cursor = write; - let len = bytes.len().min(available); - - unsafe { - if cursor + len > SIZE { - // split memcpy - let pivot = 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) % SIZE, Ordering::Release); - - len - } - - fn nonblocking_write(&self, bytes: &[u8]) -> usize { - let write = self.write.load(Ordering::Acquire); - let cursor = write; - // NOTE truncate at SIZE to avoid more than one "wrap-around" in a single `write` call - let len = bytes.len().min(SIZE); - - unsafe { - if cursor + len > SIZE { - // split memcpy - let pivot = 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) % SIZE, Ordering::Release); - - 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 - } -} diff --git a/nrf-softdevice-defmt-rtt/src/lib.rs b/nrf-softdevice-defmt-rtt/src/lib.rs deleted file mode 100644 index 90d4d6f..0000000 --- a/nrf-softdevice-defmt-rtt/src/lib.rs +++ /dev/null @@ -1,125 +0,0 @@ -//! [`defmt`](https://github.com/knurling-rs/defmt) global logger over RTT. -//! -//! NOTE when using this crate it's not possible to use (link to) the `rtt-target` crate -//! -//! To use this crate, link to it by importing it somewhere in your project. -//! -//! ``` -//! // src/main.rs or src/bin/my-app.rs -//! use defmt_rtt as _; -//! ``` -//! -//! # Blocking/Non-blocking -//! -//! `probe-run` puts RTT into blocking-mode, to avoid losing data. -//! -//! As an effect this implementation may block forever if `probe-run` disconnects on runtime. This -//! is because the RTT buffer will fill up and writing will eventually halt the program execution. -//! -//! `defmt::flush` would also block forever in that case. - -#![no_std] - -mod channel; - -use core::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering}; - -use crate::channel::Channel; - -#[defmt::global_logger] -struct Logger; - -/// Global logger lock. -static TAKEN: AtomicBool = AtomicBool::new(false); -static INTERRUPTS_TOKEN: AtomicU8 = AtomicU8::new(0); -static mut ENCODER: defmt::Encoder = defmt::Encoder::new(); - -unsafe impl defmt::Logger for Logger { - fn acquire() { - let token = unsafe { critical_section::acquire() }; - - if !TAKEN.load(Ordering::Relaxed) { - // no need for CAS because interrupts are disabled - TAKEN.store(true, Ordering::Relaxed); - - INTERRUPTS_TOKEN.store(token, Ordering::Relaxed); - - // safety: accessing the `static mut` is OK because we have disabled interrupts. - unsafe { ENCODER.start_frame(do_write) } - } else { - unsafe { critical_section::release(token) }; - } - } - - unsafe fn flush() { - // SAFETY: if we get here, the global logger mutex is currently acquired - handle().flush(); - } - - unsafe fn release() { - // safety: accessing the `static mut` is OK because we have disabled interrupts. - ENCODER.end_frame(do_write); - TAKEN.store(false, Ordering::Relaxed); - critical_section::release(INTERRUPTS_TOKEN.load(Ordering::Relaxed)); - } - - unsafe fn write(bytes: &[u8]) { - // safety: accessing the `static mut` is OK because we have disabled interrupts. - ENCODER.write(bytes, do_write); - } -} - -fn do_write(bytes: &[u8]) { - unsafe { handle().write_all(bytes) } -} - -#[repr(C)] -struct Header { - id: [u8; 16], - max_up_channels: usize, - max_down_channels: usize, - up_channel: Channel, -} - -const MODE_MASK: usize = 0b11; -/// Block the application if the RTT buffer is full, wait for the host to read data. -const MODE_BLOCK_IF_FULL: usize = 2; -/// Don't block if the RTT buffer is full. Truncate data to output as much as fits. -const MODE_NON_BLOCKING_TRIM: usize = 1; - -// TODO make configurable -// NOTE use a power of 2 for best performance -const SIZE: usize = 1024; - -// make sure we only get shared references to the header/channel (avoid UB) -/// # Safety -/// `Channel` API is not re-entrant; this handle should not be held from different execution -/// contexts (e.g. thread-mode, interrupt context) -unsafe fn handle() -> &'static Channel { - // NOTE the `rtt-target` API is too permissive. It allows writing arbitrary data to any - // channel (`set_print_channel` + `rprint*`) and that can corrupt defmt log frames. - // So we declare the RTT control block here and make it impossible to use `rtt-target` together - // with this crate. - #[no_mangle] - static mut _SEGGER_RTT: Header = Header { - id: *b"SEGGER RTT\0\0\0\0\0\0", - max_up_channels: 1, - max_down_channels: 0, - up_channel: Channel { - name: NAME as *const _ as *const u8, - buffer: unsafe { &mut BUFFER as *mut _ as *mut u8 }, - size: SIZE, - write: AtomicUsize::new(0), - read: AtomicUsize::new(0), - flags: AtomicUsize::new(MODE_NON_BLOCKING_TRIM), - }, - }; - - #[cfg_attr(target_os = "macos", link_section = ".uninit,defmt-rtt.BUFFER")] - #[cfg_attr(not(target_os = "macos"), link_section = ".uninit.defmt-rtt.BUFFER")] - static mut BUFFER: [u8; SIZE] = [0; SIZE]; - - static NAME: &[u8] = b"defmt\0"; - - &_SEGGER_RTT.up_channel -} -- cgit v1.2.3