summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2021-06-07 15:33:17 +0200
committeralexmoon <alex.r.moon@gmail.com>2022-07-14 12:01:41 -0400
commit4713a76e37e6c0114d47941a31c069ed1813530e (patch)
treebae48f74865972acb8fb96066212930ffddcf427 /examples
parentd54aceb4fce464f23dcedbff94f659b558124b9c (diff)
downloadnrf-softdevice-4713a76e37e6c0114d47941a31c069ed1813530e.zip
No alloc
Diffstat (limited to 'examples')
-rw-r--r--examples/Cargo.toml10
-rw-r--r--examples/src/bin/ble_bas_central.rs1
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs1
-rw-r--r--examples/src/bin/ble_l2cap_central.rs64
-rw-r--r--examples/src/bin/ble_l2cap_peripheral.rs48
-rw-r--r--examples/src/bin/ble_peripheral_onoff.rs1
-rw-r--r--examples/src/bin/ble_scan.rs1
-rw-r--r--examples/src/bin/flash.rs1
-rw-r--r--examples/src/example_common.rs29
9 files changed, 81 insertions, 75 deletions
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<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
- }
-}