summaryrefslogtreecommitdiff
path: root/examples/src/bin
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2021-05-10 00:24:24 +0200
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2021-05-10 00:24:24 +0200
commit8201255a9d6dde4b29a5a539a1d6aeefcbd467df (patch)
tree3366939a2c4b121091eb1abb3605f82e750b5c2f /examples/src/bin
parent7e0bf163f35dca8a1daba11429035a7780f5f4b7 (diff)
downloadnrf-softdevice-8201255a9d6dde4b29a5a539a1d6aeefcbd467df.zip
Update embassy
Diffstat (limited to 'examples/src/bin')
-rw-r--r--examples/src/bin/ble_bas_central.rs11
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs13
-rw-r--r--examples/src/bin/ble_l2cap_central.rs10
-rw-r--r--examples/src/bin/ble_l2cap_peripheral.rs17
-rw-r--r--examples/src/bin/ble_peripheral_onoff.rs36
-rw-r--r--examples/src/bin/ble_scan.rs11
-rw-r--r--examples/src/bin/flash.rs15
-rw-r--r--examples/src/bin/rtic.rs168
8 files changed, 57 insertions, 224 deletions
diff --git a/examples/src/bin/ble_bas_central.rs b/examples/src/bin/ble_bas_central.rs
index 5e56d9d..8911ca8 100644
--- a/examples/src/bin/ble_bas_central.rs
+++ b/examples/src/bin/ble_bas_central.rs
@@ -4,16 +4,16 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use core::mem;
use cortex_m_rt::entry;
use defmt::info;
use defmt::*;
-use embassy::executor::{task, Executor};
+use embassy::executor::Executor;
use embassy::util::Forever;
use nrf_softdevice::ble::{central, gatt_client, Address, AddressType};
@@ -22,7 +22,7 @@ use nrf_softdevice::Softdevice;
static EXECUTOR: Forever<Executor> = Forever::new();
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
@@ -33,7 +33,7 @@ struct BatteryServiceClient {
battery_level: u8,
}
-#[task]
+#[embassy::task]
async fn ble_central_task(sd: &'static Softdevice) {
let addrs = &[&Address::new(
AddressType::RandomStatic,
@@ -97,8 +97,7 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index e2a7846..949a5cc 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -4,24 +4,24 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use core::mem;
use cortex_m_rt::entry;
use defmt::info;
use defmt::*;
+use embassy::executor::Executor;
+use embassy::util::Forever;
use nrf_softdevice::ble::{gatt_server, peripheral};
use nrf_softdevice::{raw, Softdevice};
-use embassy::executor::{task, Executor};
-use embassy::util::Forever;
static EXECUTOR: Forever<Executor> = Forever::new();
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
@@ -34,7 +34,7 @@ struct BatteryService {
foo: u16,
}
-#[task]
+#[embassy::task]
async fn bluetooth_task(sd: &'static Softdevice) {
let server: BatteryService = unwrap!(gatt_server::register(sd));
#[rustfmt::skip]
@@ -127,8 +127,7 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/ble_l2cap_central.rs b/examples/src/bin/ble_l2cap_central.rs
index 179fff7..1bb4e3f 100644
--- a/examples/src/bin/ble_l2cap_central.rs
+++ b/examples/src/bin/ble_l2cap_central.rs
@@ -4,6 +4,7 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
extern crate alloc;
#[path = "../example_common.rs"]
@@ -16,7 +17,7 @@ use core::slice;
use cortex_m_rt::entry;
use defmt::info;
use defmt::*;
-use embassy::executor::{task, Executor};
+use embassy::executor::Executor;
use embassy::util::Forever;
use nrf_softdevice::ble::l2cap::Packet as _;
@@ -28,12 +29,12 @@ static EXECUTOR: Forever<Executor> = Forever::new();
const PSM: u16 = 0x2349;
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[task]
+#[embassy::task]
async fn ble_central_task(sd: &'static Softdevice) {
info!("Scanning for peer...");
@@ -173,8 +174,7 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/ble_l2cap_peripheral.rs b/examples/src/bin/ble_l2cap_peripheral.rs
index c88869f..0ab7369 100644
--- a/examples/src/bin/ble_l2cap_peripheral.rs
+++ b/examples/src/bin/ble_l2cap_peripheral.rs
@@ -4,33 +4,33 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
extern crate alloc;
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use core::mem;
use core::ptr::NonNull;
use cortex_m_rt::entry;
use defmt::*;
+use embassy::executor::Executor;
+use embassy::util::Forever;
-use nrf_softdevice::ble;
use nrf_softdevice::ble::{l2cap, peripheral};
+use nrf_softdevice::{ble, RawError};
use nrf_softdevice::{raw, Softdevice};
-use embassy::executor::{task, Executor};
-use embassy::util::Forever;
static EXECUTOR: Forever<Executor> = Forever::new();
const PSM: u16 = 0x2349;
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[task]
+#[embassy::task]
async fn bluetooth_task(sd: &'static Softdevice) {
info!("My address: {:?}", ble::get_address(sd));
@@ -147,8 +147,9 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ unwrap!(RawError::convert(unsafe { raw::sd_clock_hfclk_request() }));
+
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/ble_peripheral_onoff.rs b/examples/src/bin/ble_peripheral_onoff.rs
index b8535a0..8e53c15 100644
--- a/examples/src/bin/ble_peripheral_onoff.rs
+++ b/examples/src/bin/ble_peripheral_onoff.rs
@@ -4,28 +4,28 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use core::mem;
use cortex_m_rt::entry;
use defmt::{panic, *};
-use embassy::executor::{task, Executor};
+use embassy::executor::Executor;
use embassy::traits::gpio::WaitForLow;
use embassy::util::Forever;
-use embassy_nrf::gpiote::{Gpiote, GpiotePin};
+use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull};
+use embassy_nrf::gpiote::{self, PortInput};
use embassy_nrf::interrupt;
use futures::pin_mut;
-use nrf52840_hal::gpio;
use nrf_softdevice::ble::{gatt_server, peripheral};
-use nrf_softdevice::{pac, raw, Softdevice};
+use nrf_softdevice::{raw, Softdevice};
static EXECUTOR: Forever<Executor> = Forever::new();
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
@@ -76,18 +76,20 @@ async fn run_bluetooth(sd: &'static Softdevice, server: &FooService) {
}
}
-#[task]
-async fn bluetooth_task(sd: &'static Softdevice, gpiote: pac::GPIOTE, p0: pac::P0) {
+#[embassy::task]
+async fn bluetooth_task(
+ sd: &'static Softdevice,
+ gpiote: gpiote::Initialized,
+ button1: AnyPin,
+ button2: AnyPin,
+) {
let server: FooService = unwrap!(gatt_server::register(sd));
- let port0 = gpio::p0::Parts::new(p0);
- let (gpiote, _) = Gpiote::new(gpiote, interrupt::take!(GPIOTE));
-
info!("Bluetooth is OFF");
info!("Press nrf52840-dk button 1 to enable, button 2 to disable");
- let button1 = GpiotePin::new(gpiote, port0.p0_11.into_pullup_input().degrade());
- let button2 = GpiotePin::new(gpiote, port0.p0_12.into_pullup_input().degrade());
+ let button1 = PortInput::new(gpiote, Input::new(button1, Pull::Up));
+ let button2 = PortInput::new(gpiote, Input::new(button2, Pull::Up));
pin_mut!(button1);
pin_mut!(button2);
loop {
@@ -163,12 +165,14 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
+ let p = embassy_nrf::Peripherals::take().unwrap();
+ let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE));
+
unwrap!(spawner.spawn(softdevice_task(sd)));
- unwrap!(spawner.spawn(bluetooth_task(sd, p.GPIOTE, p.P0,)));
+ unwrap!(spawner.spawn(bluetooth_task(sd, g, p.P0_11.degrade(), p.P0_12.degrade())));
});
}
diff --git a/examples/src/bin/ble_scan.rs b/examples/src/bin/ble_scan.rs
index b0b3421..df23162 100644
--- a/examples/src/bin/ble_scan.rs
+++ b/examples/src/bin/ble_scan.rs
@@ -4,16 +4,16 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use core::mem;
use core::slice;
use cortex_m_rt::entry;
use defmt::*;
-use embassy::executor::{task, Executor};
+use embassy::executor::Executor;
use embassy::util::Forever;
use nrf_softdevice::ble::central;
@@ -22,12 +22,12 @@ use nrf_softdevice::Softdevice;
static EXECUTOR: Forever<Executor> = Forever::new();
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[task]
+#[embassy::task]
async fn ble_task(sd: &'static Softdevice) {
let config = central::ScanConfig::default();
let res = central::scan(sd, &config, |params| unsafe {
@@ -108,8 +108,7 @@ fn main() -> ! {
..Default::default()
};
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &config);
+ let sd = Softdevice::enable(&config);
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/flash.rs b/examples/src/bin/flash.rs
index 174151d..3e5fe57 100644
--- a/examples/src/bin/flash.rs
+++ b/examples/src/bin/flash.rs
@@ -4,30 +4,30 @@
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(alloc_error_handler)]
+#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
-use example_common::*;
use cortex_m_rt::entry;
use defmt::*;
-use embassy::executor::{task, Executor};
+use embassy::executor::Executor;
use embassy::traits::flash::Flash as _;
use embassy::util::Forever;
-
use futures::pin_mut;
+
use nrf_softdevice::{Flash, Softdevice};
static EXECUTOR: Forever<Executor> = Forever::new();
-#[task]
+#[embassy::task]
async fn softdevice_task(sd: &'static Softdevice) {
sd.run().await;
}
-#[task]
+#[embassy::task]
async fn flash_task(sd: &'static Softdevice) {
- let mut f = Flash::take(sd);
+ let f = Flash::take(sd);
pin_mut!(f);
info!("starting erase");
@@ -43,8 +43,7 @@ async fn flash_task(sd: &'static Softdevice) {
fn main() -> ! {
info!("Hello World!");
- let (sdp, _p) = take_peripherals();
- let sd = Softdevice::enable(sdp, &Default::default());
+ let sd = Softdevice::enable(&Default::default());
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
diff --git a/examples/src/bin/rtic.rs b/examples/src/bin/rtic.rs
deleted file mode 100644
index f7511f6..0000000
--- a/examples/src/bin/rtic.rs
+++ /dev/null
@@ -1,168 +0,0 @@
-//! This example showcases how to use nrf-softdevice inside RTIC.
-//!
-//! It mixes RTIC's real-time interrupt-based multitasking with
-//! static-executor's cooperative async/await multitasking.
-//!
-//! static-executor is run in RTIC's idle task, at lowest priority, so all RTIC
-//! tasks will preempt async tasks if needed.
-//!
-//! Note that this is not fully safe: you must not use the softdevice's reserved
-//! priorities for RTIC tasks. There is no compile-time checking for that for now.
-
-#![no_main]
-#![no_std]
-#![feature(type_alias_impl_trait)]
-#![feature(min_type_alias_impl_trait)]
-#![feature(impl_trait_in_bindings)]
-#![feature(alloc_error_handler)]
-
-#[path = "../example_common.rs"]
-mod example_common;
-use example_common::*;
-
-use core::mem;
-use defmt::*;
-use embassy::executor::{task, Executor};
-use embassy::util::Forever;
-use nrf52840_hal::pac::TIMER1;
-use nrf52840_hal::prelude::*;
-use nrf52840_hal::timer::{Periodic, Timer};
-use rtic::app;
-
-use nrf_softdevice::ble::peripheral;
-use nrf_softdevice::{raw, temperature_celsius, 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) {
- #[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'T', b'I', b'C',
- ];
- #[rustfmt::skip]
- let scan_data = &[
- 0x03, 0x03, 0x09, 0x18,
- ];
-
- loop {
- let config = peripheral::Config::default();
- let adv = peripheral::ConnectableAdvertisement::ScannableUndirected {
- adv_data,
- scan_data,
- };
- let _conn = unwrap!(peripheral::advertise(sd, adv, &config).await);
-
- info!("advertising done!");
- }
-}
-
-#[app(device = nrf52840_hal::pac, peripherals = true)]
-const APP: () = {
- struct Resources {
- timer: Timer<TIMER1, Periodic>,
- sd_peripherals: Option<nrf_softdevice::Peripherals>,
- }
-
- #[init()]
- fn init(cx: init::Context) -> init::LateResources {
- info!("init");
-
- let mut timer = Timer::new(cx.device.TIMER1);
- timer.enable_interrupt();
- let mut timer = timer.into_periodic();
- timer.start(1_000_000u32); // 1Mhz, so once per second
-
- let sd_peripherals = nrf_softdevice::Peripherals {
- AAR: cx.device.AAR,
- ACL: cx.device.ACL,
- CCM: cx.device.CCM,
- CLOCK: cx.device.CLOCK,
- ECB: cx.device.ECB,
- EGU1: cx.device.EGU1,
- EGU2: cx.device.EGU2,
- EGU5: cx.device.EGU5,
- MWU: cx.device.MWU,
- NVMC: cx.device.NVMC,
- POWER: cx.device.POWER,
- RADIO: cx.device.RADIO,
- RNG: cx.device.RNG,
- RTC0: cx.device.RTC0,
- SWI1: cx.device.SWI1,
- SWI2: cx.device.SWI2,
- SWI5: cx.device.SWI5,
- TEMP: cx.device.TEMP,
- TIMER0: cx.device.TIMER0,
- };
-
- init::LateResources {
- timer,
- sd_peripherals: Some(sd_peripherals),
- }
- }
-
- #[idle(resources=[sd_peripherals])]
- fn idle(cx: idle::Context) -> ! {
- 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"HelloRTIC" 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()
- };
-
- // 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 sdp = cx.resources.sd_peripherals.take().unwrap();
- let sd = Softdevice::enable(sdp, &config);
-
- let temp = unwrap!(temperature_celsius(&sd));
- info!("{}°C", temp.to_num::<i32>());
-
- let executor = EXECUTOR.put(Executor::new());
- executor.run(|spawner| {
- unwrap!(spawner.spawn(softdevice_task(sd)));
- unwrap!(spawner.spawn(bluetooth_task(sd)));
- });
- }
-
- #[task(binds = TIMER1, resources = [timer], priority = 1)]
- fn exec(cx: exec::Context) {
- cx.resources.timer.wait().unwrap();
- info!("tick");
- }
-};