From 4713a76e37e6c0114d47941a31c069ed1813530e Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 7 Jun 2021 15:33:17 +0200 Subject: No alloc --- Cargo.lock | 112 ++++++++++++++++++++----------- examples/Cargo.toml | 10 +-- examples/src/bin/ble_bas_central.rs | 1 - examples/src/bin/ble_bas_peripheral.rs | 1 - examples/src/bin/ble_l2cap_central.rs | 64 ++++++++++++------ examples/src/bin/ble_l2cap_peripheral.rs | 48 ++++++++----- examples/src/bin/ble_peripheral_onoff.rs | 1 - examples/src/bin/ble_scan.rs | 1 - examples/src/bin/flash.rs | 1 - examples/src/example_common.rs | 29 +------- 10 files changed, 154 insertions(+), 114 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f823dee..2aadc20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,17 +27,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a785a543aea40f5e4e2e93bb2655d31bc21bb391fff65697150973e383f16bb" dependencies = [ - "as-slice", -] - -[[package]] -name = "alloc-cortex-m" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5069e21ec61f3b243a4a4c56d920d3206e6ee86022a67c23e9fe36eb60fc1efd" -dependencies = [ - "cortex-m 0.7.4", - "linked_list_allocator", + "as-slice 0.1.5", ] [[package]] @@ -52,6 +42,15 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "atomic-polyfill" version = "0.1.8" @@ -61,6 +60,18 @@ dependencies = [ "critical-section", ] +[[package]] +name = "atomic-pool" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b407ed6d721feca5f9432fcd24dec6857debbc8f5d86a75538d871ee85b8b1f" +dependencies = [ + "as-slice 0.1.5", + "as-slice 0.2.1", + "atomic-polyfill", + "stable_deref_trait", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -280,10 +291,20 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0db23d29972d99baa3de2ee2ae3f104c10564a6d05a346eb3f4c4f2c0525a06e" +[[package]] +name = "defmt-rtt" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d2cbbbd58847d508d97629b32cd9730a2d28532f71e219714614406029f18b1" +dependencies = [ + "critical-section", + "defmt", +] + [[package]] name = "embassy" version = "0.1.0" -source = "git+https://github.com/embassy-rs/embassy#604051777419591cf66f5d06196cf16d3e399752" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "atomic-polyfill", "cfg-if", @@ -298,10 +319,38 @@ dependencies = [ "pin-project", ] +[[package]] +name = "embassy-cortex-m" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" +dependencies = [ + "atomic-polyfill", + "cfg-if", + "cortex-m 0.7.4", + "critical-section", + "embassy", + "embassy-hal-common", + "embassy-macros", +] + +[[package]] +name = "embassy-embedded-hal" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" +dependencies = [ + "embassy", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0-alpha.8", + "embedded-hal-async", + "embedded-storage", + "embedded-storage-async", + "nb 1.0.0", +] + [[package]] name = "embassy-hal-common" version = "0.1.0" -source = "git+https://github.com/embassy-rs/embassy#604051777419591cf66f5d06196cf16d3e399752" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "cortex-m 0.7.4", "embassy", @@ -311,7 +360,7 @@ dependencies = [ [[package]] name = "embassy-macros" version = "0.1.0" -source = "git+https://github.com/embassy-rs/embassy#604051777419591cf66f5d06196cf16d3e399752" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "darling 0.13.4", "proc-macro2", @@ -322,7 +371,7 @@ dependencies = [ [[package]] name = "embassy-nrf" version = "0.1.0" -source = "git+https://github.com/embassy-rs/embassy#604051777419591cf66f5d06196cf16d3e399752" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "cfg-if", "cortex-m 0.7.4", @@ -330,10 +379,11 @@ dependencies = [ "critical-section", "defmt", "embassy", + "embassy-cortex-m", + "embassy-embedded-hal", "embassy-hal-common", "embassy-macros", "embassy-usb", - "embedded-dma", "embedded-hal 0.2.7", "embedded-hal 1.0.0-alpha.8", "embedded-hal-async", @@ -349,22 +399,13 @@ dependencies = [ [[package]] name = "embassy-usb" version = "0.1.0" -source = "git+https://github.com/embassy-rs/embassy#604051777419591cf66f5d06196cf16d3e399752" +source = "git+https://github.com/embassy-rs/embassy#5318fe404bed68a38a1d81eb7359ebc38e6fad15" dependencies = [ "defmt", "embassy", "heapless", ] -[[package]] -name = "embedded-dma" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c8c02e4347a0267ca60813c952017f4c5948c232474c6010a381a337f1bda4" -dependencies = [ - "stable_deref_trait", -] - [[package]] name = "embedded-hal" version = "0.2.7" @@ -386,21 +427,20 @@ dependencies = [ [[package]] name = "embedded-hal-async" -version = "0.1.0-alpha.0" +version = "0.1.0-alpha.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b9f684248ddba6d9f33c40386e7d8954ec13ad3c49705af3264cf995cce5abd" +checksum = "022249738afde9b2b755a28a367ae20c9367e8e9c24b9aaf60bcc9255a255ec5" dependencies = [ "embedded-hal 1.0.0-alpha.8", ] [[package]] name = "embedded-io" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe1572ca75ff2cbb520a8b0b5a9eb589073524748c82e457adb34c6cf0e6af4" +checksum = "36673b79844ff4ec0e3f00aeca0b2cfff564ff6739ab9801d13f45a8ec6cc1c7" dependencies = [ "defmt", - "futures", ] [[package]] @@ -565,12 +605,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -[[package]] -name = "linked_list_allocator" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "822add9edb1860698b79522510da17bef885171f75aa395cff099d770c609c24" - [[package]] name = "lock_api" version = "0.4.7" @@ -656,10 +690,11 @@ dependencies = [ name = "nrf-softdevice-examples" version = "0.1.0" dependencies = [ - "alloc-cortex-m", + "atomic-pool", "cortex-m 0.7.4", "cortex-m-rt", "defmt", + "defmt-rtt", "embassy", "embassy-nrf", "embedded-storage", @@ -668,7 +703,6 @@ dependencies = [ "futures", "heapless", "nrf-softdevice", - "nrf-softdevice-defmt-rtt", "nrf-softdevice-s140", "panic-probe", ] diff --git a/examples/Cargo.toml b/examples/Cargo.toml index e2a936e..5494c70 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -16,12 +16,12 @@ ble-gatt-server = ["nrf-softdevice/ble-gatt-server"] ble-gatt-client = ["nrf-softdevice/ble-gatt-client"] [dependencies] -embassy = { version = "0.1.0", features = ["nightly", "defmt"]} +embassy = { version = "0.1.0", features = ["nightly", "defmt", "defmt-timestamp-uptime"]} embassy-nrf = { version = "0.1.0", features = [ "nightly", "defmt", "nrf52840", "gpiote", "time-driver-rtc1" ]} -cortex-m = { version = "0.7.2" } +cortex-m = "0.7.2" cortex-m-rt = "0.7.0" -defmt = { version = "0.3", features = ["alloc"] } -nrf-softdevice-defmt-rtt = { path = "../nrf-softdevice-defmt-rtt", version = "0.1.0" } +defmt = "0.3" +defmt-rtt = "0.3.2" panic-probe = { version = "0.3", features= ["print-defmt"] } nrf-softdevice = { version = "0.1.0", path = "../nrf-softdevice", features = ["defmt", "nrf52840", "s140", "ble-peripheral", "ble-central", "critical-section-impl"] } nrf-softdevice-s140 = { version = "0.1.1", path = "../nrf-softdevice-s140" } @@ -30,7 +30,7 @@ embedded-storage-async = "0.3.0" futures = { version = "0.3.5", default-features = false } fixed = "1.2.0" heapless = "0.7.1" -alloc-cortex-m = "0.4.0" +atomic-pool = "0.2.1" [[bin]] name = "ble_bas_peripheral" diff --git a/examples/src/bin/ble_bas_central.rs b/examples/src/bin/ble_bas_central.rs index fb6bd09..15eb581 100644 --- a/examples/src/bin/ble_bas_central.rs +++ b/examples/src/bin/ble_bas_central.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] #[path = "../example_common.rs"] diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs index f685513..0e1ddac 100644 --- a/examples/src/bin/ble_bas_peripheral.rs +++ b/examples/src/bin/ble_bas_peripheral.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] #[path = "../example_common.rs"] diff --git a/examples/src/bin/ble_l2cap_central.rs b/examples/src/bin/ble_l2cap_central.rs index 53951e3..acc058d 100644 --- a/examples/src/bin/ble_l2cap_central.rs +++ b/examples/src/bin/ble_l2cap_central.rs @@ -1,9 +1,7 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] -extern crate alloc; #[path = "../example_common.rs"] mod example_common; @@ -82,39 +80,63 @@ async fn ble_central_task(sd: &'static Softdevice) { info!("l2cap connected"); for i in 0..10 { - let mut v = Vec::with_capacity(Packet::MTU); - v.extend(&[i; Packet::MTU]); - unwrap!(ch.tx(Packet(v)).await); + unwrap!(ch.tx(Packet::new(&[i; Packet::MTU])).await); info!("l2cap tx done"); } futures::future::pending::<()>().await; } -use alloc::vec::Vec; +use atomic_pool::{pool, Box}; + +pool!(PacketPool: [[u8; 512]; 10]); + +struct Packet { + len: usize, + buf: Box, +} + +impl Format for Packet { + fn format(&self, fmt: Formatter) { + defmt::write!(fmt, "Packet({:x})", &self.buf[..self.len]) + } +} + +impl Packet { + fn new(data: &[u8]) -> Self { + let mut buf = unwrap!(Box::::new([0; 512])); + buf[..data.len()].copy_from_slice(data); + Packet { + len: data.len(), + buf, + } + } +} -#[derive(defmt::Format)] -struct Packet(Vec); impl l2cap::Packet for Packet { const MTU: usize = 512; fn allocate() -> Option> { - let mut v = Vec::with_capacity(Self::MTU); - let ptr = v.as_mut_ptr(); - mem::forget(v); - info!("allocate {:x}", ptr as u32); - NonNull::new(ptr) + if let Some(buf) = Box::::new([0; 512]) { + let ptr = Box::into_raw(buf).cast::(); + info!("allocate {}", ptr.as_ptr() as u32); + Some(ptr) + } else { + None + } } - fn into_raw_parts(mut self) -> (NonNull, usize) { - let ptr = self.0.as_mut_ptr(); - let len = self.0.len(); - mem::forget(self); - info!("into_raw_parts {:x}", ptr as u32); - (unwrap!(NonNull::new(ptr)), len) + fn into_raw_parts(self) -> (NonNull, usize) { + let ptr = Box::into_raw(self.buf).cast::(); + let len = self.len; + info!("into_raw_parts {}", ptr.as_ptr() as u32); + (ptr, len) } unsafe fn from_raw_parts(ptr: NonNull, len: usize) -> Self { - info!("from_raw_parts {:x}", ptr.as_ptr() as u32); - Self(Vec::from_raw_parts(ptr.as_ptr(), len, Self::MTU)) + info!("from_raw_parts {}", ptr.as_ptr() as u32); + Self { + len, + buf: Box::from_raw(ptr.cast::<[u8; 512]>()), + } } } diff --git a/examples/src/bin/ble_l2cap_peripheral.rs b/examples/src/bin/ble_l2cap_peripheral.rs index 23ef03e..baf2389 100644 --- a/examples/src/bin/ble_l2cap_peripheral.rs +++ b/examples/src/bin/ble_l2cap_peripheral.rs @@ -1,9 +1,7 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] -extern crate alloc; #[path = "../example_common.rs"] mod example_common; @@ -59,35 +57,51 @@ async fn bluetooth_task(sd: &'static Softdevice) { loop { let pkt = unwrap!(ch.rx().await); - info!("rx: {:x}", pkt.0); + info!("rx: {:x}", pkt.as_bytes()); } } } -use alloc::vec::Vec; +use atomic_pool::{pool, Box}; + +pool!(PacketPool: [[u8; 512]; 10]); + +struct Packet { + len: usize, + buf: Box, +} + +impl Packet { + fn as_bytes(&self) -> &[u8] { + &self.buf[..self.len] + } +} -struct Packet(Vec); impl l2cap::Packet for Packet { const MTU: usize = 512; fn allocate() -> Option> { - let mut v = Vec::with_capacity(Self::MTU); - let ptr = v.as_mut_ptr(); - mem::forget(v); - info!("allocate {}", ptr as u32); - NonNull::new(ptr) + if let Some(buf) = Box::::new([0; 512]) { + let ptr = Box::into_raw(buf).cast::(); + info!("allocate {}", ptr.as_ptr() as u32); + Some(ptr) + } else { + None + } } - fn into_raw_parts(mut self) -> (NonNull, usize) { - let ptr = self.0.as_mut_ptr(); - let len = self.0.len(); - mem::forget(self); - info!("into_raw_parts {}", ptr as u32); - (unwrap!(NonNull::new(ptr)), len) + fn into_raw_parts(self) -> (NonNull, usize) { + let ptr = Box::into_raw(self.buf).cast::(); + let len = self.len; + info!("into_raw_parts {}", ptr.as_ptr() as u32); + (ptr, len) } unsafe fn from_raw_parts(ptr: NonNull, len: usize) -> Self { info!("from_raw_parts {}", ptr.as_ptr() as u32); - Self(Vec::from_raw_parts(ptr.as_ptr(), len, Self::MTU)) + Self { + len, + buf: Box::from_raw(ptr.cast::<[u8; 512]>()), + } } } diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs index 539b168..d428a69 100644 --- a/examples/src/bin/ble_peripheral_onoff.rs +++ b/examples/src/bin/ble_peripheral_onoff.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] #[path = "../example_common.rs"] diff --git a/examples/src/bin/ble_scan.rs b/examples/src/bin/ble_scan.rs index 7c8e7f6..ba9f9c5 100644 --- a/examples/src/bin/ble_scan.rs +++ b/examples/src/bin/ble_scan.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] #[path = "../example_common.rs"] diff --git a/examples/src/bin/flash.rs b/examples/src/bin/flash.rs index 27ea01b..99b37d2 100644 --- a/examples/src/bin/flash.rs +++ b/examples/src/bin/flash.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![feature(type_alias_impl_trait)] -#![feature(alloc_error_handler)] #![allow(incomplete_features)] #[path = "../example_common.rs"] diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs index f402f7f..4ebb100 100644 --- a/examples/src/example_common.rs +++ b/examples/src/example_common.rs @@ -1,30 +1,5 @@ #![macro_use] -use nrf_softdevice_defmt_rtt as _; // global logger +use defmt_rtt as _; // global logger +use embassy_nrf as _; // time driver use panic_probe as _; - -use embassy_nrf as _; - -use alloc_cortex_m::CortexMHeap; -use core::alloc::Layout; -use core::sync::atomic::{AtomicUsize, Ordering}; -use defmt::panic; - -// this is the allocator the application will use -#[global_allocator] -static ALLOCATOR: CortexMHeap = CortexMHeap::empty(); - -// define what happens in an Out Of Memory (OOM) condition -#[alloc_error_handler] -fn alloc_error(_layout: Layout) -> ! { - panic!("Alloc error"); -} - -defmt::timestamp! {"{=u64}", { - static COUNT: AtomicUsize = AtomicUsize::new(0); - // NOTE(no-CAS) `timestamps` runs with interrupts disabled - let n = COUNT.load(Ordering::Relaxed); - COUNT.store(n + 1, Ordering::Relaxed); - n as u64 - } -} -- cgit v1.2.3