diff options
Diffstat (limited to 'examples/src')
-rw-r--r-- | examples/src/bin/ble_bas_central.rs | 1 | ||||
-rw-r--r-- | examples/src/bin/ble_bas_peripheral.rs | 1 | ||||
-rw-r--r-- | examples/src/bin/ble_l2cap_central.rs | 64 | ||||
-rw-r--r-- | examples/src/bin/ble_l2cap_peripheral.rs | 48 | ||||
-rw-r--r-- | examples/src/bin/ble_peripheral_onoff.rs | 1 | ||||
-rw-r--r-- | examples/src/bin/ble_scan.rs | 1 | ||||
-rw-r--r-- | examples/src/bin/flash.rs | 1 | ||||
-rw-r--r-- | examples/src/example_common.rs | 29 |
8 files changed, 76 insertions, 70 deletions
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<PacketPool>, +} + +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::<PacketPool>::new([0; 512])); + buf[..data.len()].copy_from_slice(data); + Packet { + len: data.len(), + buf, + } + } +} -#[derive(defmt::Format)] -struct Packet(Vec<u8>); impl l2cap::Packet for Packet { const MTU: usize = 512; fn allocate() -> Option<NonNull<u8>> { - 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::<PacketPool>::new([0; 512]) { + let ptr = Box::into_raw(buf).cast::<u8>(); + info!("allocate {}", ptr.as_ptr() as u32); + Some(ptr) + } else { + None + } } - fn into_raw_parts(mut self) -> (NonNull<u8>, 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<u8>, usize) { + let ptr = Box::into_raw(self.buf).cast::<u8>(); + let len = self.len; + info!("into_raw_parts {}", ptr.as_ptr() as u32); + (ptr, len) } unsafe fn from_raw_parts(ptr: NonNull<u8>, 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<PacketPool>, +} + +impl Packet { + fn as_bytes(&self) -> &[u8] { + &self.buf[..self.len] + } +} -struct Packet(Vec<u8>); impl l2cap::Packet for Packet { const MTU: usize = 512; fn allocate() -> Option<NonNull<u8>> { - 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::<PacketPool>::new([0; 512]) { + let ptr = Box::into_raw(buf).cast::<u8>(); + info!("allocate {}", ptr.as_ptr() as u32); + Some(ptr) + } else { + None + } } - fn into_raw_parts(mut self) -> (NonNull<u8>, 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<u8>, usize) { + let ptr = Box::into_raw(self.buf).cast::<u8>(); + let len = self.len; + info!("into_raw_parts {}", ptr.as_ptr() as u32); + (ptr, len) } unsafe fn from_raw_parts(ptr: NonNull<u8>, 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 - } -} |