diff options
author | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2020-10-31 23:24:31 +0100 |
---|---|---|
committer | Dario Nieuwenhuis <dirbaio@dirbaio.net> | 2020-10-31 23:24:31 +0100 |
commit | c621ed316cf6f49805a4db7b1f8e220bfc8fe86e (patch) | |
tree | 7950b43e8ea8d4de7cee8b9954807d2e2b9b0848 | |
parent | 908eb3faedfd029704b068ed92bfd2c8680798a7 (diff) | |
download | nrf-softdevice-c621ed316cf6f49805a4db7b1f8e220bfc8fe86e.zip |
Migrate to embassy (removes static-executor and async-flash)
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | async-flash/Cargo.toml | 10 | ||||
-rw-r--r-- | async-flash/src/lib.rs | 56 | ||||
-rw-r--r-- | async-flash/src/writer.rs | 107 | ||||
-rw-r--r-- | examples/Cargo.toml | 5 | ||||
-rw-r--r-- | examples/src/bin/ble_bas_central.rs | 18 | ||||
-rw-r--r-- | examples/src/bin/ble_bas_peripheral.rs | 18 | ||||
-rw-r--r-- | examples/src/bin/ble_peripheral_gattspam.rs | 18 | ||||
-rw-r--r-- | examples/src/bin/flash.rs | 21 | ||||
-rw-r--r-- | examples/src/bin/interrupts.rs | 19 | ||||
-rw-r--r-- | examples/src/bin/rtic.rs | 21 | ||||
-rw-r--r-- | examples/src/example_common.rs | 1 | ||||
-rw-r--r-- | nrf-softdevice/Cargo.toml | 2 | ||||
-rw-r--r-- | nrf-softdevice/src/flash.rs | 23 |
14 files changed, 95 insertions, 231 deletions
@@ -10,8 +10,6 @@ members = [ "nrf-softdevice-s140", "nrf-softdevice-macro", - "async-flash", - "examples", ] @@ -23,8 +21,9 @@ 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" } -static-executor = { git = "https://github.com/Dirbaio/static-executor" } -static-executor-cortex-m = { git = "https://github.com/Dirbaio/static-executor" } +embassy = { git = "https://github.com/akiles/embassy" } +embassy-nrf = { git = "https://github.com/akiles/embassy" } +embassy-macros = { git = "https://github.com/akiles/embassy" } [profile.dev] codegen-units = 1 diff --git a/async-flash/Cargo.toml b/async-flash/Cargo.toml deleted file mode 100644 index bd5f45e..0000000 --- a/async-flash/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "async-flash" -version = "0.1.0" -authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -defmt = "0.1.0"
\ No newline at end of file diff --git a/async-flash/src/lib.rs b/async-flash/src/lib.rs deleted file mode 100644 index beedaec..0000000 --- a/async-flash/src/lib.rs +++ /dev/null @@ -1,56 +0,0 @@ -#![no_std] -#![feature(slice_fill)] -#![feature(generic_associated_types)] - -use core::future::Future; - -#[derive(defmt::Format, Copy, Clone, Debug, Eq, PartialEq)] -pub enum Error { - Failed, - AddressMisaligned, - BufferMisaligned, - - _NonExhaustive, -} - -pub trait Flash { - type ReadFuture<'a>: Future<Output = Result<(), Error>>; - type WriteFuture<'a>: Future<Output = Result<(), Error>>; - type ErasePageFuture<'a>: Future<Output = Result<(), Error>>; - - /// Reads data from the flash device. - /// - /// address must be a multiple of self.read_size(). - /// buf.len() must be a multiple of self.read_size(). - fn read<'a>(&'a mut self, address: usize, buf: &'a mut [u8]) -> Self::ReadFuture<'a>; - - /// Writes data to the flash device. - /// - /// address must be a multiple of self.write_size(). - /// buf.len() must be a multiple of self.write_size(). - fn write<'a>(&'a mut self, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>; - - /// Erases a single page from the flash device. - /// - /// address must be a multiple of self.erase_size(). - fn erase<'a>(&'a mut self, address: usize) -> Self::ErasePageFuture<'a>; - - /// Returns the total size, in bytes. - /// This is not guaranteed to be a power of 2. - fn size(&self) -> usize; - - /// Returns the read size in bytes. - /// This is guaranteed to be a power of 2. - fn read_size(&self) -> usize; - - /// Returns the write size in bytes. - /// This is guaranteed to be a power of 2. - fn write_size(&self) -> usize; - - /// Returns the erase size in bytes. - /// This is guaranteed to be a power of 2. - fn erase_size(&self) -> usize; -} - -mod writer; -pub use writer::{Writer, WriterError}; diff --git a/async-flash/src/writer.rs b/async-flash/src/writer.rs deleted file mode 100644 index 86a02b3..0000000 --- a/async-flash/src/writer.rs +++ /dev/null @@ -1,107 +0,0 @@ -use crate::{Error, Flash}; - -#[derive(Copy, Clone, Debug)] -pub enum WriterError { - Flash(Error), - OutOfBounds, -} - -impl From<Error> for WriterError { - fn from(e: Error) -> Self { - Self::Flash(e) - } -} - -#[repr(align(4))] -struct AlignedBuf([u8; 256]); - -pub struct Writer<'a, F: Flash> { - flash: &'a mut F, - address: usize, - length: usize, - - write_cur: usize, - erase_cur: usize, - - buf: AlignedBuf, - buf_have: usize, -} - -impl<'a, F: Flash> Writer<'a, F> { - pub fn new(flash: &'a mut F, address: usize, length: usize) -> Self { - assert_eq!(256 & (flash.write_size() - 1), 0); - assert_eq!(address & (flash.erase_size() - 1), 0); - assert_eq!(length & (flash.erase_size() - 1), 0); - - Self { - flash, - address, - length, - - write_cur: address, - erase_cur: address, - - buf: AlignedBuf([0; 256]), - buf_have: 0, - } - } - - async fn do_write(&mut self, len: usize) -> Result<(), WriterError> { - if self.write_cur + len > self.address + self.length { - return Err(WriterError::OutOfBounds); - } - - while self.write_cur + len > self.erase_cur { - self.flash.erase(self.erase_cur).await?; - self.erase_cur += self.flash.erase_size(); - } - - self.flash.write(self.write_cur, &self.buf.0[..len]).await?; - self.write_cur += len; - - Ok(()) - } - - pub async fn write(&mut self, mut data: &[u8]) -> Result<(), WriterError> { - // This code is HORRIBLE. - // - // Calls to flash write must have data aligned to 4 bytes. - // We can't guarantee `data` is, so we're forced to buffer it - // somewhere we can make aligned. - - while data.len() != 0 { - let left = self.buf.0.len() - self.buf_have; - let n = core::cmp::min(left, data.len()); - - self.buf.0[self.buf_have..][..n].copy_from_slice(&data[..n]); - self.buf_have += n; - data = &data[n..]; - - // When buffer is full, write it out - if self.buf_have == self.buf.0.len() { - self.do_write(self.buf.0.len()).await?; - self.buf_have = 0; - } - } - - // Whatever's left in the buffer stays there. - // It will be written in subsequent calls, or in flush. - - Ok(()) - } - - pub async fn flush(mut self) -> Result<(), WriterError> { - if self.buf_have != 0 { - let write_size = self.flash.write_size(); - - // round up amount - let have = (self.buf_have + write_size - 1) & (!(write_size - 1)); - - // fill the leftover bytes (if any) with 0xFF - self.buf.0[self.buf_have..have].fill(0xFF); - - self.do_write(have).await?; - } - Ok(()) - } -} diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 8a46c73..a9eedc5 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -23,7 +23,8 @@ ble-gatt-server = ["nrf-softdevice/ble-gatt-server"] ble-gatt-client = ["nrf-softdevice/ble-gatt-client"] [dependencies] -async-flash = { version = "0.1.0", path = "../async-flash" } +embassy = { version = "0.1.0" } +embassy-nrf = { version = "0.1.0", features = [ "52840" ]} cortex-m = { version = "0.6.3" } cortex-m-rt = "0.6.12" cortex-m-rtic = { version = "0.5.5", optional = true } @@ -33,8 +34,6 @@ panic-probe = "0.1.0" nrf52840-hal = { version = "0.11.0" } nrf-softdevice = { version = "0.1.0", path = "../nrf-softdevice", features = ["defmt-trace", "nrf52840", "s140", "ble-peripheral", "ble-central"] } nrf-softdevice-s140 = { version = "0.1.1", path = "../nrf-softdevice-s140" } -static-executor = { version = "0.1.0", features=["defmt"]} -static-executor-cortex-m = { version = "0.1.0" } futures = { version = "0.3.5", default-features = false } fixed = "1.2.0" diff --git a/examples/src/bin/ble_bas_central.rs b/examples/src/bin/ble_bas_central.rs index 184dda3..a1cb0d9 100644 --- a/examples/src/bin/ble_bas_central.rs +++ b/examples/src/bin/ble_bas_central.rs @@ -9,12 +9,16 @@ 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::{central, gatt_client, Address, Connection, Uuid}; use nrf_softdevice::raw; use nrf_softdevice::Softdevice; -#[static_executor::task] +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } @@ -72,7 +76,7 @@ impl gatt_client::Client for BatteryServiceClient { } } -#[static_executor::task] +#[task] async fn ble_central_task(sd: &'static Softdevice) { let addrs = &[Address::new_random_static([ 0x59, 0xf9, 0xb1, 0x9c, 0x01, 0xf5, @@ -154,10 +158,12 @@ fn main() -> ! { let sd = Softdevice::enable(&config); - unsafe { - softdevice_task.spawn(sd).dewrap(); - ble_central_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(ble_central_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs index 131f9c1..ac87c46 100644 --- a/examples/src/bin/ble_bas_peripheral.rs +++ b/examples/src/bin/ble_bas_peripheral.rs @@ -14,7 +14,11 @@ use nrf_softdevice::ble::gatt_server::{Characteristic, CharacteristicHandles, Re use nrf_softdevice::ble::{gatt_server, peripheral, Connection, Uuid}; use nrf_softdevice::{raw, RawError, Softdevice}; -#[static_executor::task] +use embassy::executor::{task, Executor}; +use embassy::util::Forever; +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } @@ -28,7 +32,7 @@ struct BatteryService { battery_level: u8, } -#[static_executor::task] +#[task] async fn bluetooth_task(sd: &'static Softdevice) { let server: BatteryService = gatt_server::register(sd).dewrap(); @@ -119,10 +123,12 @@ fn main() -> ! { let sd = Softdevice::enable(&config); - unsafe { - softdevice_task.spawn(sd).dewrap(); - bluetooth_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(bluetooth_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/ble_peripheral_gattspam.rs b/examples/src/bin/ble_peripheral_gattspam.rs index 0f6b75b..c67dc84 100644 --- a/examples/src/bin/ble_peripheral_gattspam.rs +++ b/examples/src/bin/ble_peripheral_gattspam.rs @@ -9,16 +9,20 @@ 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::task] +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } -#[static_executor::task] +#[task] async fn bluetooth_task(sd: &'static Softdevice) { for i in 0..24 { let service_uuid = Uuid::new_16(0x4200 + i); @@ -153,10 +157,12 @@ fn main() -> ! { let sd = Softdevice::enable(&config); - unsafe { - softdevice_task.spawn(sd).dewrap(); - bluetooth_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(bluetooth_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/flash.rs b/examples/src/bin/flash.rs index e4570c2..54bde9a 100644 --- a/examples/src/bin/flash.rs +++ b/examples/src/bin/flash.rs @@ -6,16 +6,21 @@ mod example_common; use example_common::*; -use async_flash::Flash as _; use cortex_m_rt::entry; +use embassy::executor::{task, Executor}; +use embassy::flash::Flash as _; +use embassy::util::Forever; + use nrf_softdevice::{Flash, Softdevice}; -#[static_executor::task] +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } -#[static_executor::task] +#[task] async fn flash_task(sd: &'static Softdevice) { let mut f = Flash::take(sd); @@ -38,10 +43,12 @@ fn main() -> ! { let sd = Softdevice::enable(&Default::default()); - unsafe { - softdevice_task.spawn(sd).dewrap(); - flash_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(flash_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/interrupts.rs b/examples/src/bin/interrupts.rs index de9feae..574ecf2 100644 --- a/examples/src/bin/interrupts.rs +++ b/examples/src/bin/interrupts.rs @@ -7,15 +7,20 @@ mod example_common; use example_common::*; use cortex_m_rt::entry; +use embassy::executor::{task, Executor}; +use embassy::util::Forever; + use nrf_softdevice::interrupt; use nrf_softdevice::Softdevice; -#[static_executor::task] +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } -#[static_executor::task] +#[task] async fn interrupt_task(sd: &'static Softdevice) { let enabled = interrupt::is_enabled(interrupt::SWI0_EGU0); info!("enabled: {:?}", enabled); @@ -84,10 +89,12 @@ fn main() -> ! { let sd = Softdevice::enable(&Default::default()); - unsafe { - softdevice_task.spawn(sd).dewrap(); - interrupt_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(interrupt_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/rtic.rs b/examples/src/bin/rtic.rs index eb3bd4b..8e3de03 100644 --- a/examples/src/bin/rtic.rs +++ b/examples/src/bin/rtic.rs @@ -18,19 +18,24 @@ mod example_common; use example_common::*; use core::mem; +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}; -use rtic::app; -#[static_executor::task] +static EXECUTOR: Forever<Executor> = Forever::new(); + +#[task] async fn softdevice_task(sd: &'static Softdevice) { sd.run().await; } -#[static_executor::task] +#[task] async fn bluetooth_task(sd: &'static Softdevice) { #[rustfmt::skip] let adv_data = &[ @@ -123,11 +128,13 @@ const APP: () = { let temp = temperature_celsius(&sd).dewrap(); info!("{:i32}°C", temp.to_num::<i32>()); - unsafe { - softdevice_task.spawn(sd).dewrap(); - bluetooth_task.spawn(sd).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(softdevice_task(sd)).dewrap(); + executor.spawn(bluetooth_task(sd)).dewrap(); - static_executor::run(); + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/example_common.rs b/examples/src/example_common.rs index e991915..65bfe6b 100644 --- a/examples/src/example_common.rs +++ b/examples/src/example_common.rs @@ -3,7 +3,6 @@ use defmt_rtt as _; // global logger use nrf52840_hal as _; use panic_probe as _; -use static_executor_cortex_m as _; pub use defmt::{info, intern}; diff --git a/nrf-softdevice/Cargo.toml b/nrf-softdevice/Cargo.toml index e0d96a6..c01a816 100644 --- a/nrf-softdevice/Cargo.toml +++ b/nrf-softdevice/Cargo.toml @@ -32,7 +32,7 @@ ble-gatt-client = [] [dependencies] num_enum = { version = "0.5.1", default-features = false } -async-flash = { version = "0.1.0", path = "../async-flash" } +embassy = { version = "0.1.0" } cortex-m = "0.6.2" cortex-m-rt = "0.6.12" bare-metal = { version = "0.2.0", features = ["const-fn"] } diff --git a/nrf-softdevice/src/flash.rs b/nrf-softdevice/src/flash.rs index d7af2cb..1d6cc1e 100644 --- a/nrf-softdevice/src/flash.rs +++ b/nrf-softdevice/src/flash.rs @@ -1,6 +1,7 @@ use core::future::Future; use core::marker::PhantomData; use core::sync::atomic::{AtomicBool, Ordering}; +use embassy::flash::Error as FlashError; use crate::raw; use crate::util::*; @@ -33,20 +34,20 @@ impl Flash { } } -static SIGNAL: Signal<Result<(), async_flash::Error>> = Signal::new(); +static SIGNAL: Signal<Result<(), FlashError>> = Signal::new(); pub(crate) fn on_flash_success() { SIGNAL.signal(Ok(())) } pub(crate) fn on_flash_error() { - SIGNAL.signal(Err(async_flash::Error::Failed)) + SIGNAL.signal(Err(FlashError::Failed)) } -impl async_flash::Flash for Flash { - type ReadFuture<'a> = impl Future<Output = Result<(), async_flash::Error>> + 'a; - type WriteFuture<'a> = impl Future<Output = Result<(), async_flash::Error>> + 'a; - type ErasePageFuture<'a> = impl Future<Output = Result<(), async_flash::Error>> + 'a; +impl embassy::flash::Flash for Flash { + type ReadFuture<'a> = impl Future<Output = Result<(), FlashError>> + 'a; + type WriteFuture<'a> = impl Future<Output = Result<(), FlashError>> + 'a; + type ErasePageFuture<'a> = impl Future<Output = Result<(), FlashError>> + 'a; fn read<'a>(&'a mut self, address: usize, data: &'a mut [u8]) -> Self::ReadFuture<'a> { async move { @@ -67,10 +68,10 @@ impl async_flash::Flash for Flash { let data_len = data.len() as u32; if address % 4 != 0 { - return Err(async_flash::Error::AddressMisaligned); + return Err(FlashError::AddressMisaligned); } if (data_ptr as u32) % 4 != 0 || data_len % 4 != 0 { - return Err(async_flash::Error::BufferMisaligned); + return Err(FlashError::BufferMisaligned); } // This is safe because we've checked ptr and len is aligned above @@ -83,7 +84,7 @@ impl async_flash::Flash for Flash { Ok(()) => SIGNAL.wait().await, Err(e) => { warn!("sd_flash_write err {:?}", e); - Err(async_flash::Error::Failed) + Err(FlashError::Failed) } }; @@ -95,7 +96,7 @@ impl async_flash::Flash for Flash { fn erase<'a>(&'a mut self, address: usize) -> Self::ErasePageFuture<'a> { async move { if address % Self::PAGE_SIZE != 0 { - return Err(async_flash::Error::AddressMisaligned); + return Err(FlashError::AddressMisaligned); } let page_number = address / Self::PAGE_SIZE; @@ -106,7 +107,7 @@ impl async_flash::Flash for Flash { Ok(()) => SIGNAL.wait().await, Err(e) => { warn!("sd_flash_page_erase err {:?}", e); - Err(async_flash::Error::Failed) + Err(FlashError::Failed) } }; |