summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2020-11-10 14:15:39 +0100
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2020-11-10 14:15:39 +0100
commit1c15bd7677a77ba8a453ac3723d0153b0120d473 (patch)
tree100329b88906a9da4c232f592460c8cb2935a9e5
parent3dffe02f75a691ed56579c28f83cc9ff405f01bf (diff)
downloadnrf-softdevice-1c15bd7677a77ba8a453ac3723d0153b0120d473.zip
update examples to anyfmt
-rw-r--r--Cargo.lock5
-rw-r--r--Cargo.toml3
-rw-r--r--examples/Cargo.toml10
-rw-r--r--examples/src/bin/ble_bas_central.rs24
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs44
-rw-r--r--examples/src/bin/ble_peripheral_gattspam.rs172
-rw-r--r--examples/src/bin/flash.rs17
-rw-r--r--examples/src/bin/interrupts.rs5
-rw-r--r--examples/src/bin/rtic.rs31
-rw-r--r--examples/src/example_common.rs54
-rwxr-xr-xtest-build.sh2
11 files changed, 81 insertions, 286 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b133ac3..1803e17 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -32,6 +32,9 @@ dependencies = [
name = "anyfmt"
version = "0.1.0"
source = "git+https://github.com/akiles/embassy#2e062f562773f4f4ff978e7976c2d4b08b968a6c"
+dependencies = [
+ "defmt",
+]
[[package]]
name = "as-slice"
@@ -259,6 +262,7 @@ dependencies = [
"bare-metal",
"cortex-m",
"cortex-m-rt",
+ "defmt",
"embassy",
"embedded-hal",
"nrf52840-hal",
@@ -514,6 +518,7 @@ dependencies = [
name = "nrf-softdevice-examples"
version = "0.1.0"
dependencies = [
+ "anyfmt",
"cortex-m",
"cortex-m-rt",
"cortex-m-rtic",
diff --git a/Cargo.toml b/Cargo.toml
index 4706707..a6289ef 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,7 @@
+cargo-features = ["resolver"]
[workspace]
+resolver = "2"
members = [
"nrf-softdevice",
"nrf-softdevice-mbr",
@@ -21,6 +23,7 @@ exclude = [
panic-probe = { git = "https://github.com/knurling-rs/probe-run", branch="main" }
defmt-rtt = { git = "https://github.com/knurling-rs/defmt", branch="main" }
defmt = { git = "https://github.com/knurling-rs/defmt", branch="main" }
+anyfmt = { git = "https://github.com/akiles/embassy" }
embassy = { git = "https://github.com/akiles/embassy" }
embassy-nrf = { git = "https://github.com/akiles/embassy" }
embassy-macros = { git = "https://github.com/akiles/embassy" }
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index bb36eaf..584eeef 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -23,8 +23,9 @@ ble-gatt-server = ["nrf-softdevice/ble-gatt-server"]
ble-gatt-client = ["nrf-softdevice/ble-gatt-client"]
[dependencies]
+anyfmt = {version = "0.1.0", features = ["defmt"] }
embassy = { version = "0.1.0", features = ["defmt"]}
-embassy-nrf = { version = "0.1.0", features = [ "52840" ]}
+embassy-nrf = { version = "0.1.0", features = [ "defmt", "52840" ]}
cortex-m = { version = "0.6.3" }
cortex-m-rt = "0.6.12"
cortex-m-rtic = { version = "0.5.5", optional = true }
@@ -51,6 +52,9 @@ name = "ble_bas_central"
required-features = ["ble-gatt-client"]
[[bin]]
-name = "ble_peripheral_gattspam"
-required-features = ["ble-gatt-client"]
+name = "flash"
+required-features = []
+[[bin]]
+name = "interrupts"
+required-features = []
diff --git a/examples/src/bin/ble_bas_central.rs b/examples/src/bin/ble_bas_central.rs
index 6d1b83c..38ef609 100644
--- a/examples/src/bin/ble_bas_central.rs
+++ b/examples/src/bin/ble_bas_central.rs
@@ -6,6 +6,7 @@
mod example_common;
use example_common::*;
+use anyfmt::{panic, *};
use core::mem;
use cortex_m_rt::entry;
use defmt::info;
@@ -35,28 +36,21 @@ async fn ble_central_task(sd: &'static Softdevice, config: central::Config) {
0x06, 0x6b, 0x71, 0x2c, 0xf5, 0xc0,
])];
- let conn = central::connect(sd, addrs, config)
- .await
- .dexpect(intern!("connect"));
+ let conn = unwrap!(central::connect(sd, addrs, config).await);
info!("connected");
- let client: BatteryServiceClient = gatt_client::discover(&conn)
- .await
- .dexpect(intern!("discover"));
+ let client: BatteryServiceClient = unwrap!(gatt_client::discover(&conn).await);
// Read
- let val = client.battery_level_read().await.dexpect(intern!("read"));
+ let val = unwrap!(client.battery_level_read().await);
info!("read battery level: {:u8}", val);
// Write, set it to 42
- client
- .battery_level_write(42)
- .await
- .dexpect(intern!("write"));
+ unwrap!(client.battery_level_write(42).await);
info!("Wrote battery level!");
// Read to check it's changed
- let val = client.battery_level_read().await.dexpect(intern!("read"));
+ let val = unwrap!(client.battery_level_read().await);
info!("read battery level: {:u8}", val);
}
@@ -102,10 +96,8 @@ fn main() -> ! {
let sd = Softdevice::enable(sdp, &config);
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor
- .spawn(ble_central_task(sd, central::Config::default()))
- .dewrap();
+ unwrap!(executor.spawn(softdevice_task(sd)));
+ unwrap!(executor.spawn(ble_central_task(sd, central::Config::default())));
loop {
executor.run();
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index da80644..d734ce8 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -6,6 +6,7 @@
mod example_common;
use example_common::*;
+use anyfmt::{panic, *};
use core::mem;
use cortex_m_rt::entry;
use defmt::info;
@@ -26,11 +27,13 @@ async fn softdevice_task(sd: &'static Softdevice) {
struct BatteryService {
#[characteristic(uuid = "2a19", read, write, notify)]
battery_level: u8,
+ #[characteristic(uuid = "3a4a1f7e-22d8-11eb-a3aa-1b3b1d4e4a0d", read, write, notify)]
+ foo: u16,
}
#[task]
async fn bluetooth_task(sd: &'static Softdevice, config: peripheral::Config) {
- let server: BatteryService = gatt_server::register(sd).dewrap();
+ let server: BatteryService = unwrap!(gatt_server::register(sd));
#[rustfmt::skip]
let adv_data = &[
0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8,
@@ -43,16 +46,17 @@ async fn bluetooth_task(sd: &'static Softdevice, config: peripheral::Config) {
];
loop {
- let conn = peripheral::advertise(
- sd,
- peripheral::ConnectableAdvertisement::ScannableUndirected {
- adv_data,
- scan_data,
- },
- config,
- )
- .await
- .dewrap();
+ let conn = unwrap!(
+ peripheral::advertise(
+ sd,
+ peripheral::ConnectableAdvertisement::ScannableUndirected {
+ adv_data,
+ scan_data,
+ },
+ config,
+ )
+ .await
+ );
info!("advertising done!");
@@ -64,12 +68,20 @@ async fn bluetooth_task(sd: &'static Softdevice, config: peripheral::Config) {
info!("send notification error: {:?}", e);
}
}
+ BatteryServiceEvent::FooWrite(val) => {
+ info!("wrote battery level: {:u16}", val);
+ if let Err(e) = server.foo_notify(&conn, val + 1) {
+ info!("send notification error: {:?}", e);
+ }
+ }
BatteryServiceEvent::BatteryLevelNotificationsEnabled => {
info!("battery notifications enabled")
}
BatteryServiceEvent::BatteryLevelNotificationsDisabled => {
info!("battery notifications disabled")
}
+ BatteryServiceEvent::FooNotificationsEnabled => info!("foo notifications enabled"),
+ BatteryServiceEvent::FooNotificationsDisabled => info!("foo notifications disabled"),
})
.await;
@@ -92,9 +104,9 @@ fn main() -> ! {
}),
conn_gap: Some(raw::ble_gap_conn_cfg_t {
conn_count: 6,
- event_length: 6,
+ event_length: 24,
}),
- conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 128 }),
+ conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 256 }),
gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
attr_tab_size: 32768,
}),
@@ -121,10 +133,8 @@ fn main() -> ! {
let sd = Softdevice::enable(sdp, &config);
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor
- .spawn(bluetooth_task(sd, peripheral::Config::default()))
- .dewrap();
+ unwrap!(executor.spawn(softdevice_task(sd)));
+ unwrap!(executor.spawn(bluetooth_task(sd, peripheral::Config::default())));
loop {
executor.run();
diff --git a/examples/src/bin/ble_peripheral_gattspam.rs b/examples/src/bin/ble_peripheral_gattspam.rs
deleted file mode 100644
index 902ad23..0000000
--- a/examples/src/bin/ble_peripheral_gattspam.rs
+++ /dev/null
@@ -1,172 +0,0 @@
-#![no_std]
-#![no_main]
-#![feature(type_alias_impl_trait)]
-
-#[path = "../example_common.rs"]
-mod example_common;
-use example_common::*;
-
-use core::mem;
-use cortex_m_rt::entry;
-use defmt::info;
-use embassy::executor::{task, Executor};
-use embassy::util::Forever;
-
-use nrf_softdevice::ble::{peripheral, Uuid};
-use nrf_softdevice::{raw, RawError, Softdevice};
-
-static EXECUTOR: Forever<Executor> = Forever::new();
-
-#[task]
-async fn softdevice_task(sd: &'static Softdevice) {
- sd.run().await;
-}
-
-#[task]
-async fn bluetooth_task(sd: &'static Softdevice, config: peripheral::Config) {
- for i in 0..24 {
- let service_uuid = Uuid::new_16(0x4200 + i);
-
- let mut service_handle: u16 = 0;
- let ret = unsafe {
- raw::sd_ble_gatts_service_add(
- raw::BLE_GATTS_SRVC_TYPE_PRIMARY as u8,
- service_uuid.as_raw_ptr(),
- &mut service_handle as _,
- )
- };
- RawError::convert(ret).dewrap();
-
- let max = if i == 0 { 64 } else { 16 };
- for j in 0..max {
- let char_uuid = Uuid::new_16(0x6900 + j);
-
- let mut val: u8 = 123;
-
- let mut cccd_attr_md: raw::ble_gatts_attr_md_t = unsafe { mem::zeroed() };
- cccd_attr_md.read_perm = raw::ble_gap_conn_sec_mode_t {
- _bitfield_1: raw::ble_gap_conn_sec_mode_t::new_bitfield_1(1, 1),
- };
- cccd_attr_md.write_perm = raw::ble_gap_conn_sec_mode_t {
- _bitfield_1: raw::ble_gap_conn_sec_mode_t::new_bitfield_1(1, 1),
- };
- cccd_attr_md.set_vloc(raw::BLE_GATTS_VLOC_STACK as u8);
-
- let mut attr_md: raw::ble_gatts_attr_md_t = unsafe { mem::zeroed() };
- attr_md.read_perm = raw::ble_gap_conn_sec_mode_t {
- _bitfield_1: raw::ble_gap_conn_sec_mode_t::new_bitfield_1(1, 1),
- };
- attr_md.write_perm = raw::ble_gap_conn_sec_mode_t {
- _bitfield_1: raw::ble_gap_conn_sec_mode_t::new_bitfield_1(1, 1),
- };
- attr_md.set_vloc(raw::BLE_GATTS_VLOC_STACK as u8);
-
- let mut attr: raw::ble_gatts_attr_t = unsafe { mem::zeroed() };
- attr.p_uuid = unsafe { char_uuid.as_raw_ptr() };
- attr.p_attr_md = &attr_md as _;
- attr.init_len = 1;
- attr.max_len = 1;
- attr.p_value = &mut val;
-
- let mut char_md: raw::ble_gatts_char_md_t = unsafe { mem::zeroed() };
- char_md.char_props.set_read(1);
- char_md.char_props.set_write(1);
- char_md.char_props.set_notify(1);
- char_md.p_cccd_md = &mut cccd_attr_md;
-
- let mut char_handles: raw::ble_gatts_char_handles_t = unsafe { mem::zeroed() };
-
- let ret = unsafe {
- raw::sd_ble_gatts_characteristic_add(
- service_handle,
- &mut char_md as _,
- &mut attr as _,
- &mut char_handles as _,
- )
- };
- RawError::convert(ret).dewrap();
- }
- }
-
- #[rustfmt::skip]
- let adv_data = &[
- 0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8,
- 0x03, 0x03, 0x09, 0x18,
- 0x0a, 0x09, b'H', b'e', b'l', b'l', b'o', b'R', b'u', b's', b't',
- ];
- #[rustfmt::skip]
- let scan_data = &[
- 0x03, 0x03, 0x09, 0x18,
- ];
-
- loop {
- let conn = peripheral::advertise(
- sd,
- peripheral::ConnectableAdvertisement::ScannableUndirected {
- adv_data,
- scan_data,
- },
- config,
- )
- .await
- .dewrap();
-
- info!("advertising done!");
-
- // Detach the connection so it isn't disconnected when dropped.
- conn.detach();
- }
-}
-
-#[entry]
-fn main() -> ! {
- info!("Hello World!");
-
- let config = nrf_softdevice::Config {
- clock: Some(raw::nrf_clock_lf_cfg_t {
- source: raw::NRF_CLOCK_LF_SRC_XTAL as u8,
- rc_ctiv: 0,
- rc_temp_ctiv: 0,
- accuracy: 7,
- }),
- conn_gap: Some(raw::ble_gap_conn_cfg_t {
- conn_count: 6,
- event_length: 6,
- }),
- conn_gatt: Some(raw::ble_gatt_conn_cfg_t { att_mtu: 128 }),
- gatts_attr_tab_size: Some(raw::ble_gatts_cfg_attr_tab_size_t {
- attr_tab_size: 32768,
- }),
- gap_role_count: Some(raw::ble_gap_cfg_role_count_t {
- adv_set_count: 1,
- periph_role_count: 3,
- central_role_count: 3,
- central_sec_count: 0,
- _bitfield_1: raw::ble_gap_cfg_role_count_t::new_bitfield_1(0),
- }),
- gap_device_name: Some(raw::ble_gap_cfg_device_name_t {
- p_value: b"HelloRust" as *const u8 as _,
- current_len: 9,
- max_len: 9,
- write_perm: unsafe { mem::zeroed() },
- _bitfield_1: raw::ble_gap_cfg_device_name_t::new_bitfield_1(
- raw::BLE_GATTS_VLOC_STACK as u8,
- ),
- }),
- ..Default::default()
- };
-
- let (sdp, p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
-
- let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor
- .spawn(bluetooth_task(sd, peripheral::Config::default()))
- .dewrap();
-
- loop {
- executor.run();
- cortex_m::asm::wfe();
- }
-}
diff --git a/examples/src/bin/flash.rs b/examples/src/bin/flash.rs
index a7a7a87..ee3fb38 100644
--- a/examples/src/bin/flash.rs
+++ b/examples/src/bin/flash.rs
@@ -6,6 +6,7 @@
mod example_common;
use example_common::*;
+use anyfmt::{panic, *};
use cortex_m_rt::entry;
use embassy::executor::{task, Executor};
use embassy::flash::Flash as _;
@@ -25,16 +26,12 @@ async fn flash_task(sd: &'static Softdevice) {
let mut f = Flash::take(sd);
info!("starting erase");
- match f.erase(0x80000).await {
- Ok(()) => info!("erased!"),
- Err(e) => depanic!("erase failed: {:?}", e),
- }
+ unwrap!(f.erase(0x80000).await);
+ info!("erased!");
info!("starting write");
- match f.write(0x80000, &[1, 2, 3, 4]).await {
- Ok(()) => info!("write done!"),
- Err(e) => depanic!("write failed: {:?}", e),
- }
+ unwrap!(f.write(0x80000, &[1, 2, 3, 4]).await);
+ info!("write done!");
}
#[entry]
@@ -45,8 +42,8 @@ fn main() -> ! {
let sd = Softdevice::enable(sdp, &Default::default());
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor.spawn(flash_task(sd)).dewrap();
+ unwrap!(executor.spawn(softdevice_task(sd)));
+ unwrap!(executor.spawn(flash_task(sd)));
loop {
executor.run();
diff --git a/examples/src/bin/interrupts.rs b/examples/src/bin/interrupts.rs
index 91454a1..b1cea14 100644
--- a/examples/src/bin/interrupts.rs
+++ b/examples/src/bin/interrupts.rs
@@ -6,6 +6,7 @@
mod example_common;
use example_common::*;
+use anyfmt::{panic, *};
use cortex_m_rt::entry;
use embassy::executor::{task, Executor};
use embassy::util::Forever;
@@ -91,8 +92,8 @@ fn main() -> ! {
let sd = Softdevice::enable(sdp, &Default::default());
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor.spawn(interrupt_task(sd)).dewrap();
+ unwrap!(executor.spawn(softdevice_task(sd)));
+ unwrap!(executor.spawn(interrupt_task(sd)));
loop {
executor.run();
diff --git a/examples/src/bin/rtic.rs b/examples/src/bin/rtic.rs
index c8bde1c..885eea0 100644
--- a/examples/src/bin/rtic.rs
+++ b/examples/src/bin/rtic.rs
@@ -17,6 +17,7 @@
mod example_common;
use example_common::*;
+use anyfmt::{panic, *};
use core::mem;
use embassy::executor::{task, Executor};
use embassy::util::Forever;
@@ -49,15 +50,17 @@ async fn bluetooth_task(sd: &'static Softdevice) {
];
loop {
- let conn = peripheral::advertise(
- sd,
- peripheral::ConnectableAdvertisement::ScannableUndirected {
- adv_data,
- scan_data,
- },
- )
- .await
- .dewrap();
+ let conn = unwrap!(
+ peripheral::advertise(
+ sd,
+ peripheral::ConnectableAdvertisement::ScannableUndirected {
+ adv_data,
+ scan_data,
+ },
+ peripheral::Config::default()
+ )
+ .await
+ );
info!("advertising done!");
@@ -120,17 +123,19 @@ const APP: () = {
..Default::default()
};
+ let (sdp, p) = take_peripherals();
+
// Softdevice enable must not be done in RTIC init
// because RTIC runs init with interrupts disabled, and the
// softdevice crashes if it's enabled with interrupts disabled.
- let sd = Softdevice::enable(&config);
+ let sd = Softdevice::enable(sdp, &config);
- let temp = temperature_celsius(&sd).dewrap();
+ let temp = unwrap!(temperature_celsius(&sd));
info!("{:i32}°C", temp.to_num::<i32>());
let executor = EXECUTOR.put(Executor::new(cortex_m::asm::sev));
- executor.spawn(softdevice_task(sd)).dewrap();
- executor.spawn(bluetooth_task(sd)).dewrap();
+ unwrap!(executor.spawn(softdevice_task(sd)));
+ unwrap!(executor.spawn(bluetooth_task(sd)));
loop {
executor.run();
diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs
index da753bc..3f27a0f 100644
--- a/examples/src/example_common.rs
+++ b/examples/src/example_common.rs
@@ -5,8 +5,7 @@ use nrf52840_hal as _;
use nrf_softdevice::pac;
use panic_probe as _;
-pub use defmt::{info, intern};
-
+use anyfmt::{panic, *};
use core::sync::atomic::{AtomicUsize, Ordering};
#[defmt::timestamp]
@@ -20,7 +19,7 @@ fn timestamp() -> u64 {
// Take peripherals, split by softdevice and application
pub fn take_peripherals() -> (nrf_softdevice::Peripherals, Peripherals) {
- let p = pac::Peripherals::take().dewrap();
+ let p = unwrap!(pac::Peripherals::take());
(
nrf_softdevice::Peripherals {
@@ -156,52 +155,3 @@ pub struct Peripherals {
pub USBD: pac::USBD,
pub WDT: pac::WDT,
}
-
-macro_rules! depanic {
- ($( $i:expr ),*) => {
- {
- defmt::error!($( $i ),*);
- panic!();
- }
- }
-}
-
-pub trait Dewrap<T> {
- /// dewrap = defmt unwrap
- fn dewrap(self) -> T;
-
- /// dexpect = defmt expect
- fn dexpect<M: defmt::Format>(self, msg: M) -> T;
-}
-
-impl<T> Dewrap<T> for Option<T> {
- fn dewrap(self) -> T {
- match self {
- Some(t) => t,
- None => depanic!("Dewrap failed: enum is none"),
- }
- }
-
- fn dexpect<M: defmt::Format>(self, msg: M) -> T {
- match self {
- Some(t) => t,
- None => depanic!("Unexpected None: {:?}", msg),
- }
- }
-}
-
-impl<T, E: defmt::Format> Dewrap<T> for Result<T, E> {
- fn dewrap(self) -> T {
- match self {
- Ok(t) => t,
- Err(e) => depanic!("Dewrap failed: {:?}", e),
- }
- }
-
- fn dexpect<M: defmt::Format>(self, msg: M) -> T {
- match self {
- Ok(t) => t,
- Err(e) => depanic!("Unexpected error: {:?}: {:?}", msg, e),
- }
- }
-}
diff --git a/test-build.sh b/test-build.sh
index 9d38066..0f598da 100755
--- a/test-build.sh
+++ b/test-build.sh
@@ -5,7 +5,7 @@ set -euxo pipefail
# build examples
#==================
-cargo build --target thumbv7em-none-eabihf -p nrf-softdevice-examples --bins
+cargo build --target thumbv7em-none-eabihf -p nrf-softdevice-examples --features cortex-m-rtic --bins
# build all softdevice+chip combinations (with all supported features enabled)