summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2022-08-29 20:32:07 +0200
committercos <cos>2022-08-30 15:59:19 +0200
commit890d2616d21c5abd99a15cd5b9811c96440f8193 (patch)
tree33d83d950660aee5fa6d38faedd9fca8cb81bd0f
parent9dc3d2b10156ca64650e35226efc79de9f7ce5d7 (diff)
downloadnrf-softdevice-wip/timer-test.zip
Trivial timer testwip/timer-test
-rw-r--r--examples/Cargo.toml5
-rw-r--r--examples/src/bin/ble_dis_bas_peripheral_builder.rs53
2 files changed, 53 insertions, 5 deletions
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index b3f03a9..904ab0e 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -16,8 +16,8 @@ ble-gatt-server = ["nrf-softdevice/ble-gatt-server"]
ble-gatt-client = ["nrf-softdevice/ble-gatt-client"]
[dependencies]
-embassy-executor = { version = "0.1.0", features = ["nightly", "defmt"]}
-embassy-time = { version = "0.1.0", features = ["nightly", "defmt", "defmt-timestamp-uptime"]}
+embassy-executor = { version = "0.1.0", features = ["nightly", "defmt", "integrated-timers"]}
+embassy-time = { version = "0.1.0", features = ["nightly", "defmt", "defmt-timestamp-uptime", "tick-32768hz"]}
embassy-sync = { version = "0.1.0" }
embassy-nrf = { version = "0.1.0", features = [ "nightly", "defmt", "nrf52840", "gpiote", "time-driver-rtc1" ]}
cortex-m = "0.7.2"
@@ -32,6 +32,7 @@ embedded-storage-async = "0.3.0"
futures = { version = "0.3.5", default-features = false }
fixed = "1.2.0"
heapless = "0.7.1"
+static_cell = "1.0.0"
atomic-pool = "0.2.1"
[[bin]]
diff --git a/examples/src/bin/ble_dis_bas_peripheral_builder.rs b/examples/src/bin/ble_dis_bas_peripheral_builder.rs
index b73b6d8..0415e78 100644
--- a/examples/src/bin/ble_dis_bas_peripheral_builder.rs
+++ b/examples/src/bin/ble_dis_bas_peripheral_builder.rs
@@ -9,12 +9,28 @@ use core::mem;
use defmt::{info, *};
use embassy_executor::Spawner;
+use static_cell::StaticCell;
+
+use embassy_time::{
+ Duration,
+ Timer,
+};
+use embassy_sync::channel::{
+ Channel,
+ Sender,
+ Receiver,
+};
+use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use nrf_softdevice::ble::gatt_server::builder::ServiceBuilder;
use nrf_softdevice::ble::gatt_server::characteristic::{Attribute, Metadata, Properties};
use nrf_softdevice::ble::gatt_server::{CharacteristicHandles, RegisterError};
use nrf_softdevice::ble::{gatt_server, peripheral, Connection, Uuid};
use nrf_softdevice::{raw, Softdevice};
+type TickChannel = Channel<NoopRawMutex, (), 1>;
+static CHANNEL: StaticCell<TickChannel> = StaticCell::new();
+static SERVER: StaticCell<Server> = StaticCell::new();
+
const DEVICE_INFORMATION: Uuid = Uuid::new_16(0x180a);
const BATTERY_SERVICE: Uuid = Uuid::new_16(0x180f);
@@ -32,6 +48,16 @@ async fn softdevice_task(sd: &'static Softdevice) -> ! {
sd.run().await
}
+#[embassy_executor::task]
+async fn tick_task(sender: Sender<'static, NoopRawMutex, (), 1>) -> ! {
+ defmt::println!("Tick task started");
+ loop {
+ Timer::after(Duration::from_millis(250)).await;
+ defmt::println!("Tick");
+ sender.send(()).await;
+ }
+}
+
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum VidSource {
@@ -102,6 +128,19 @@ impl DeviceInformationService {
}
}
+#[embassy_executor::task]
+async fn battery_task(bas: &'static BatteryService, sd: &'static Softdevice, reciever: Receiver<'static, NoopRawMutex, (), 1>) -> ! {
+ let mut value = 0;
+ loop {
+ reciever.recv().await;
+ defmt::println!("Tock");
+ value += 1;
+ if let Err(e) = bas.battery_level_set(sd, value) {
+ defmt::error!("battery_level_set() failed with: {}", e);
+ }
+ }
+}
+
pub struct BatteryService {
value_handle: u16,
cccd_handle: u16,
@@ -138,6 +177,7 @@ impl BatteryService {
}
pub fn on_write(&self, handle: u16, data: &[u8]) {
+ defmt::println!("BatteryService::on_write()");
if handle == self.cccd_handle && !data.is_empty() {
info!("battery notifications: {}", (data[0] & 0x01) != 0);
}
@@ -177,6 +217,7 @@ impl gatt_server::Server for Server {
type Event = ();
fn on_write(&self, handle: u16, data: &[u8]) -> Option<Self::Event> {
+ defmt::debug!("gatt_server::Server::on_write()");
self.bas.on_write(handle, data);
None
}
@@ -216,8 +257,8 @@ async fn main(spawner: Spawner) {
..Default::default()
};
- let sd = Softdevice::enable(&config);
- let server = unwrap!(Server::new(sd, "12345678"));
+ let sd: &'static mut Softdevice = Softdevice::enable(&config);
+ let server: &'static mut Server = SERVER.init(unwrap!(Server::new(sd, "12345678")));
unwrap!(spawner.spawn(softdevice_task(sd)));
#[rustfmt::skip]
@@ -231,6 +272,12 @@ async fn main(spawner: Spawner) {
0x03, 0x03, 0x09, 0x18,
];
+ let channel: &'static mut TickChannel = CHANNEL.init(TickChannel::new());
+ let sender = channel.sender();
+ let receiver = channel.receiver();
+ unwrap!(spawner.spawn(tick_task(sender)));
+ unwrap!(spawner.spawn(battery_task(&server.bas, sd, receiver)));
+
loop {
let config = peripheral::Config::default();
let adv = peripheral::ConnectableAdvertisement::ScannableUndirected { adv_data, scan_data };
@@ -239,7 +286,7 @@ async fn main(spawner: Spawner) {
info!("advertising done!");
// Run the GATT server on the connection. This returns when the connection gets disconnected.
- let res = gatt_server::run(&conn, &server, |_| {}).await;
+ let res = gatt_server::run(&conn, server, |_| {}).await;
if let Err(e) = res {
info!("gatt_server run exited with error: {:?}", e);