diff options
45 files changed, 139 insertions, 220 deletions
diff --git a/embassy-cortex-m/src/executor.rs b/embassy-cortex-m/src/executor.rs index 80c452f84..0d1745d8a 100644 --- a/embassy-cortex-m/src/executor.rs +++ b/embassy-cortex-m/src/executor.rs | |||
| @@ -71,7 +71,7 @@ impl<I: Interrupt> InterruptExecutor<I> { | |||
| 71 | /// Executor instance in a place where it'll live forever and grants you mutable | 71 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 72 | /// access. There's a few ways to do this: | 72 | /// access. There's a few ways to do this: |
| 73 | /// | 73 | /// |
| 74 | /// - a [Forever](embassy_util::Forever) (safe) | 74 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 75 | /// - a `static mut` (unsafe) | 75 | /// - a `static mut` (unsafe) |
| 76 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 76 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 77 | pub fn start(&'static mut self) -> SendSpawner { | 77 | pub fn start(&'static mut self) -> SendSpawner { |
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index bd023fb6a..dc483b826 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs | |||
| @@ -7,12 +7,12 @@ | |||
| 7 | //! use embassy_util::mutex::Mutex; | 7 | //! use embassy_util::mutex::Mutex; |
| 8 | //! use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; | 8 | //! use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; |
| 9 | //! | 9 | //! |
| 10 | //! static I2C_BUS: Forever<Mutex::<ThreadModeRawMutex, Twim<TWISPI0>>> = Forever::new(); | 10 | //! static I2C_BUS: StaticCell<Mutex::<ThreadModeRawMutex, Twim<TWISPI0>>> = StaticCell::new(); |
| 11 | //! let config = twim::Config::default(); | 11 | //! let config = twim::Config::default(); |
| 12 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 12 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); |
| 13 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); | 13 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, config); |
| 14 | //! let i2c_bus = Mutex::<ThreadModeRawMutex, _>::new(i2c); | 14 | //! let i2c_bus = Mutex::<ThreadModeRawMutex, _>::new(i2c); |
| 15 | //! let i2c_bus = I2C_BUS.put(i2c_bus); | 15 | //! let i2c_bus = I2C_BUS.init(i2c_bus); |
| 16 | //! | 16 | //! |
| 17 | //! // Device 1, using embedded-hal-async compatible driver for QMC5883L compass | 17 | //! // Device 1, using embedded-hal-async compatible driver for QMC5883L compass |
| 18 | //! let i2c_dev1 = I2cDevice::new(i2c_bus); | 18 | //! let i2c_dev1 = I2cDevice::new(i2c_bus); |
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index caa37f6f3..bb419d6a6 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs | |||
| @@ -7,13 +7,13 @@ | |||
| 7 | //! use embassy_util::mutex::Mutex; | 7 | //! use embassy_util::mutex::Mutex; |
| 8 | //! use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; | 8 | //! use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; |
| 9 | //! | 9 | //! |
| 10 | //! static SPI_BUS: Forever<Mutex<ThreadModeRawMutex, spim::Spim<SPI3>>> = Forever::new(); | 10 | //! static SPI_BUS: StaticCell<Mutex<ThreadModeRawMutex, spim::Spim<SPI3>>> = StaticCell::new(); |
| 11 | //! let mut config = spim::Config::default(); | 11 | //! let mut config = spim::Config::default(); |
| 12 | //! config.frequency = spim::Frequency::M32; | 12 | //! config.frequency = spim::Frequency::M32; |
| 13 | //! let irq = interrupt::take!(SPIM3); | 13 | //! let irq = interrupt::take!(SPIM3); |
| 14 | //! let spi = spim::Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, config); | 14 | //! let spi = spim::Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, config); |
| 15 | //! let spi_bus = Mutex::<ThreadModeRawMutex, _>::new(spi); | 15 | //! let spi_bus = Mutex::<ThreadModeRawMutex, _>::new(spi); |
| 16 | //! let spi_bus = SPI_BUS.put(spi_bus); | 16 | //! let spi_bus = SPI_BUS.init(spi_bus); |
| 17 | //! | 17 | //! |
| 18 | //! // Device 1, using embedded-hal-async compatible driver for ST7735 LCD display | 18 | //! // Device 1, using embedded-hal-async compatible driver for ST7735 LCD display |
| 19 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | 19 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); |
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 1fc343d15..837312e87 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | |||
| @@ -6,11 +6,11 @@ | |||
| 6 | //! use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice; | 6 | //! use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice; |
| 7 | //! use embassy_util::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; | 7 | //! use embassy_util::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; |
| 8 | //! | 8 | //! |
| 9 | //! static I2C_BUS: Forever<NoopMutex<RefCell<Twim<TWISPI0>>>> = Forever::new(); | 9 | //! static I2C_BUS: StaticCell<NoopMutex<RefCell<Twim<TWISPI0>>>> = StaticCell::new(); |
| 10 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 10 | //! let irq = interrupt::take!(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); |
| 11 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, Config::default()); | 11 | //! let i2c = Twim::new(p.TWISPI0, irq, p.P0_03, p.P0_04, Config::default()); |
| 12 | //! let i2c_bus = NoopMutex::new(RefCell::new(i2c)); | 12 | //! let i2c_bus = NoopMutex::new(RefCell::new(i2c)); |
| 13 | //! let i2c_bus = I2C_BUS.put(i2c_bus); | 13 | //! let i2c_bus = I2C_BUS.init(i2c_bus); |
| 14 | //! | 14 | //! |
| 15 | //! let i2c_dev1 = I2cDevice::new(i2c_bus); | 15 | //! let i2c_dev1 = I2cDevice::new(i2c_bus); |
| 16 | //! let mpu = Mpu6050::new(i2c_dev1); | 16 | //! let mpu = Mpu6050::new(i2c_dev1); |
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index a61326594..a48d9183c 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -6,11 +6,11 @@ | |||
| 6 | //! use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice; | 6 | //! use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice; |
| 7 | //! use embassy_util::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; | 7 | //! use embassy_util::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; |
| 8 | //! | 8 | //! |
| 9 | //! static SPI_BUS: Forever<NoopMutex<RefCell<Spim<SPI3>>>> = Forever::new(); | 9 | //! static SPI_BUS: StaticCell<NoopMutex<RefCell<Spim<SPI3>>>> = StaticCell::new(); |
| 10 | //! let irq = interrupt::take!(SPIM3); | 10 | //! let irq = interrupt::take!(SPIM3); |
| 11 | //! let spi = Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, Config::default()); | 11 | //! let spi = Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, Config::default()); |
| 12 | //! let spi_bus = NoopMutex::new(RefCell::new(spi)); | 12 | //! let spi_bus = NoopMutex::new(RefCell::new(spi)); |
| 13 | //! let spi_bus = SPI_BUS.put(spi_bus); | 13 | //! let spi_bus = SPI_BUS.init(spi_bus); |
| 14 | //! | 14 | //! |
| 15 | //! // Device 1, using embedded-hal compatible driver for ST7735 LCD display | 15 | //! // Device 1, using embedded-hal compatible driver for ST7735 LCD display |
| 16 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | 16 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); |
diff --git a/embassy-executor/Cargo.toml b/embassy-executor/Cargo.toml index 7d5c4a045..184e770cd 100644 --- a/embassy-executor/Cargo.toml +++ b/embassy-executor/Cargo.toml | |||
| @@ -44,6 +44,7 @@ embassy-time = { version = "0.1.0", path = "../embassy-time", optional = true} | |||
| 44 | atomic-polyfill = "1.0.1" | 44 | atomic-polyfill = "1.0.1" |
| 45 | critical-section = "1.1" | 45 | critical-section = "1.1" |
| 46 | cfg-if = "1.0.0" | 46 | cfg-if = "1.0.0" |
| 47 | static_cell = "1.0" | ||
| 47 | 48 | ||
| 48 | # WASM dependencies | 49 | # WASM dependencies |
| 49 | wasm-bindgen = { version = "0.2.76", features = ["nightly"], optional = true } | 50 | wasm-bindgen = { version = "0.2.76", features = ["nightly"], optional = true } |
diff --git a/embassy-executor/src/arch/cortex_m.rs b/embassy-executor/src/arch/cortex_m.rs index d6e758dfb..4b27a264e 100644 --- a/embassy-executor/src/arch/cortex_m.rs +++ b/embassy-executor/src/arch/cortex_m.rs | |||
| @@ -41,7 +41,7 @@ impl Executor { | |||
| 41 | /// Executor instance in a place where it'll live forever and grants you mutable | 41 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 42 | /// access. There's a few ways to do this: | 42 | /// access. There's a few ways to do this: |
| 43 | /// | 43 | /// |
| 44 | /// - a [Forever](crate::util::Forever) (safe) | 44 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 45 | /// - a `static mut` (unsafe) | 45 | /// - a `static mut` (unsafe) |
| 46 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 46 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 47 | /// | 47 | /// |
diff --git a/embassy-executor/src/arch/riscv32.rs b/embassy-executor/src/arch/riscv32.rs index 7a7d5698c..2a4b006da 100644 --- a/embassy-executor/src/arch/riscv32.rs +++ b/embassy-executor/src/arch/riscv32.rs | |||
| @@ -43,7 +43,7 @@ impl Executor { | |||
| 43 | /// Executor instance in a place where it'll live forever and grants you mutable | 43 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 44 | /// access. There's a few ways to do this: | 44 | /// access. There's a few ways to do this: |
| 45 | /// | 45 | /// |
| 46 | /// - a [Forever](crate::util::Forever) (safe) | 46 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 47 | /// - a `static mut` (unsafe) | 47 | /// - a `static mut` (unsafe) |
| 48 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 48 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 49 | /// | 49 | /// |
diff --git a/embassy-executor/src/arch/std.rs b/embassy-executor/src/arch/std.rs index b93ab8a79..701f0eb18 100644 --- a/embassy-executor/src/arch/std.rs +++ b/embassy-executor/src/arch/std.rs | |||
| @@ -40,7 +40,7 @@ impl Executor { | |||
| 40 | /// Executor instance in a place where it'll live forever and grants you mutable | 40 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 41 | /// access. There's a few ways to do this: | 41 | /// access. There's a few ways to do this: |
| 42 | /// | 42 | /// |
| 43 | /// - a [Forever](crate::util::Forever) (safe) | 43 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 44 | /// - a `static mut` (unsafe) | 44 | /// - a `static mut` (unsafe) |
| 45 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 45 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 46 | /// | 46 | /// |
diff --git a/embassy-executor/src/arch/wasm.rs b/embassy-executor/src/arch/wasm.rs index 9d5aa31ed..98091cfbb 100644 --- a/embassy-executor/src/arch/wasm.rs +++ b/embassy-executor/src/arch/wasm.rs | |||
| @@ -59,7 +59,7 @@ impl Executor { | |||
| 59 | /// Executor instance in a place where it'll live forever and grants you mutable | 59 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 60 | /// access. There's a few ways to do this: | 60 | /// access. There's a few ways to do this: |
| 61 | /// | 61 | /// |
| 62 | /// - a [Forever](crate::util::Forever) (safe) | 62 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 63 | /// - a `static mut` (unsafe) | 63 | /// - a `static mut` (unsafe) |
| 64 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 64 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 65 | pub fn start(&'static mut self, init: impl FnOnce(Spawner)) { | 65 | pub fn start(&'static mut self, init: impl FnOnce(Spawner)) { |
diff --git a/embassy-executor/src/arch/xtensa.rs b/embassy-executor/src/arch/xtensa.rs index 20bd7b8a5..f908aaa70 100644 --- a/embassy-executor/src/arch/xtensa.rs +++ b/embassy-executor/src/arch/xtensa.rs | |||
| @@ -43,7 +43,7 @@ impl Executor { | |||
| 43 | /// Executor instance in a place where it'll live forever and grants you mutable | 43 | /// Executor instance in a place where it'll live forever and grants you mutable |
| 44 | /// access. There's a few ways to do this: | 44 | /// access. There's a few ways to do this: |
| 45 | /// | 45 | /// |
| 46 | /// - a [Forever](crate::util::Forever) (safe) | 46 | /// - a [StaticCell](https://docs.rs/static_cell/latest/static_cell/) (safe) |
| 47 | /// - a `static mut` (unsafe) | 47 | /// - a `static mut` (unsafe) |
| 48 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) | 48 | /// - a local variable in a function you know never returns (like `fn main() -> !`), upgrading its lifetime with `transmute`. (unsafe) |
| 49 | /// | 49 | /// |
diff --git a/embassy-executor/src/lib.rs b/embassy-executor/src/lib.rs index 93f2eaa6d..e4cbd04b9 100644 --- a/embassy-executor/src/lib.rs +++ b/embassy-executor/src/lib.rs | |||
| @@ -67,3 +67,9 @@ pub mod raw; | |||
| 67 | 67 | ||
| 68 | mod spawner; | 68 | mod spawner; |
| 69 | pub use spawner::*; | 69 | pub use spawner::*; |
| 70 | |||
| 71 | /// Do not use. Used for macros and HALs only. Not covered by semver guarantees. | ||
| 72 | #[doc(hidden)] | ||
| 73 | pub mod _export { | ||
| 74 | pub use static_cell::StaticCell; | ||
| 75 | } | ||
diff --git a/embassy-macros/src/macros/main.rs b/embassy-macros/src/macros/main.rs index 52987d7d2..afe9bd3e2 100644 --- a/embassy-macros/src/macros/main.rs +++ b/embassy-macros/src/macros/main.rs | |||
| @@ -34,8 +34,8 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 34 | let main = quote! { | 34 | let main = quote! { |
| 35 | #[wasm_bindgen::prelude::wasm_bindgen(start)] | 35 | #[wasm_bindgen::prelude::wasm_bindgen(start)] |
| 36 | pub fn main() -> Result<(), wasm_bindgen::JsValue> { | 36 | pub fn main() -> Result<(), wasm_bindgen::JsValue> { |
| 37 | static EXECUTOR: ::embassy_util::Forever<::embassy_executor::Executor> = ::embassy_util::Forever::new(); | 37 | static EXECUTOR: ::embassy_executor::_export::StaticCell<::embassy_executor::Executor> = ::embassy_executor::_export::StaticCell::new(); |
| 38 | let executor = EXECUTOR.put(::embassy_executor::Executor::new()); | 38 | let executor = EXECUTOR.init(::embassy_executor::Executor::new()); |
| 39 | 39 | ||
| 40 | executor.start(|spawner| { | 40 | executor.start(|spawner| { |
| 41 | spawner.spawn(__embassy_main(spawner)).unwrap(); | 41 | spawner.spawn(__embassy_main(spawner)).unwrap(); |
diff --git a/embassy-util/Cargo.toml b/embassy-util/Cargo.toml index d4708d735..b54a58b46 100644 --- a/embassy-util/Cargo.toml +++ b/embassy-util/Cargo.toml | |||
| @@ -31,3 +31,4 @@ futures-util = { version = "0.3.17", features = [ "channel" ] } | |||
| 31 | 31 | ||
| 32 | # Enable critical-section implementation for std, for tests | 32 | # Enable critical-section implementation for std, for tests |
| 33 | critical-section = { version = "1.1", features = ["std"] } | 33 | critical-section = { version = "1.1", features = ["std"] } |
| 34 | static_cell = "1.0" | ||
diff --git a/embassy-util/src/channel/mpmc.rs b/embassy-util/src/channel/mpmc.rs index 535f77e6f..263f29bfc 100644 --- a/embassy-util/src/channel/mpmc.rs +++ b/embassy-util/src/channel/mpmc.rs | |||
| @@ -471,10 +471,10 @@ mod tests { | |||
| 471 | use futures_executor::ThreadPool; | 471 | use futures_executor::ThreadPool; |
| 472 | use futures_timer::Delay; | 472 | use futures_timer::Delay; |
| 473 | use futures_util::task::SpawnExt; | 473 | use futures_util::task::SpawnExt; |
| 474 | use static_cell::StaticCell; | ||
| 474 | 475 | ||
| 475 | use super::*; | 476 | use super::*; |
| 476 | use crate::blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}; | 477 | use crate::blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}; |
| 477 | use crate::Forever; | ||
| 478 | 478 | ||
| 479 | fn capacity<T, const N: usize>(c: &ChannelState<T, N>) -> usize { | 479 | fn capacity<T, const N: usize>(c: &ChannelState<T, N>) -> usize { |
| 480 | c.queue.capacity() - c.queue.len() | 480 | c.queue.capacity() - c.queue.len() |
| @@ -549,8 +549,8 @@ mod tests { | |||
| 549 | async fn receiver_receives_given_try_send_async() { | 549 | async fn receiver_receives_given_try_send_async() { |
| 550 | let executor = ThreadPool::new().unwrap(); | 550 | let executor = ThreadPool::new().unwrap(); |
| 551 | 551 | ||
| 552 | static CHANNEL: Forever<Channel<CriticalSectionRawMutex, u32, 3>> = Forever::new(); | 552 | static CHANNEL: StaticCell<Channel<CriticalSectionRawMutex, u32, 3>> = StaticCell::new(); |
| 553 | let c = &*CHANNEL.put(Channel::new()); | 553 | let c = &*CHANNEL.init(Channel::new()); |
| 554 | let c2 = c; | 554 | let c2 = c; |
| 555 | assert!(executor | 555 | assert!(executor |
| 556 | .spawn(async move { | 556 | .spawn(async move { |
| @@ -571,8 +571,8 @@ mod tests { | |||
| 571 | async fn senders_sends_wait_until_capacity() { | 571 | async fn senders_sends_wait_until_capacity() { |
| 572 | let executor = ThreadPool::new().unwrap(); | 572 | let executor = ThreadPool::new().unwrap(); |
| 573 | 573 | ||
| 574 | static CHANNEL: Forever<Channel<CriticalSectionRawMutex, u32, 1>> = Forever::new(); | 574 | static CHANNEL: StaticCell<Channel<CriticalSectionRawMutex, u32, 1>> = StaticCell::new(); |
| 575 | let c = &*CHANNEL.put(Channel::new()); | 575 | let c = &*CHANNEL.init(Channel::new()); |
| 576 | assert!(c.try_send(1).is_ok()); | 576 | assert!(c.try_send(1).is_ok()); |
| 577 | 577 | ||
| 578 | let c2 = c; | 578 | let c2 = c; |
diff --git a/embassy-util/src/forever.rs b/embassy-util/src/forever.rs deleted file mode 100644 index 4f3698211..000000000 --- a/embassy-util/src/forever.rs +++ /dev/null | |||
| @@ -1,95 +0,0 @@ | |||
| 1 | use core::cell::UnsafeCell; | ||
| 2 | use core::mem::MaybeUninit; | ||
| 3 | |||
| 4 | use atomic_polyfill::{AtomicBool, Ordering}; | ||
| 5 | |||
| 6 | /// Type with static lifetime that may be written to once at runtime. | ||
| 7 | /// | ||
| 8 | /// This may be used to initialize static objects at runtime, typically in the init routine. | ||
| 9 | /// This is useful for objects such as Embassy's RTC, which cannot be initialized in a const | ||
| 10 | /// context. | ||
| 11 | /// | ||
| 12 | /// Note: IF a global mutable variable is desired, use a CriticalSectionMutex or ThreadModeMutex instead. | ||
| 13 | /// | ||
| 14 | /// ``` | ||
| 15 | /// use embassy_util::Forever; | ||
| 16 | /// // Using an integer for the sake of keeping this example self-contained, | ||
| 17 | /// // see https://github.com/embassy-rs/embassy/wiki/Getting-Started for a more "proper" example. | ||
| 18 | /// static SOME_INT: Forever<u32> =Forever::new(); | ||
| 19 | /// | ||
| 20 | /// // put returns a mutable pointer to the object stored in the forever, which may then be passed | ||
| 21 | /// // around. | ||
| 22 | /// let mut x = SOME_INT.put(42); | ||
| 23 | /// assert_eq!(*x, 42); | ||
| 24 | /// ``` | ||
| 25 | pub struct Forever<T> { | ||
| 26 | used: AtomicBool, | ||
| 27 | t: UnsafeCell<MaybeUninit<T>>, | ||
| 28 | } | ||
| 29 | |||
| 30 | unsafe impl<T> Send for Forever<T> {} | ||
| 31 | unsafe impl<T> Sync for Forever<T> {} | ||
| 32 | |||
| 33 | impl<T> Forever<T> { | ||
| 34 | /// Create a new `Forever`. | ||
| 35 | #[inline(always)] | ||
| 36 | pub const fn new() -> Self { | ||
| 37 | Self { | ||
| 38 | used: AtomicBool::new(false), | ||
| 39 | t: UnsafeCell::new(MaybeUninit::uninit()), | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | /// Store a value in this `Forever`, returning a mutable reference to it. | ||
| 44 | /// | ||
| 45 | /// Using this method, the compiler usually constructs `val` in the stack and then moves | ||
| 46 | /// it into the `Forever`. If `T` is big, this is likely to cause stack overflows. | ||
| 47 | /// Considering using [`Signal::put_with`] instead, which will construct it in-place inside the `Forever`. | ||
| 48 | /// | ||
| 49 | /// # Panics | ||
| 50 | /// | ||
| 51 | /// Panics if this `Forever` already has a value stored in it. | ||
| 52 | #[inline(always)] | ||
| 53 | #[allow(clippy::mut_from_ref)] | ||
| 54 | pub fn put(&'static self, val: T) -> &'static mut T { | ||
| 55 | self.put_with(|| val) | ||
| 56 | } | ||
| 57 | |||
| 58 | /// Store the closure return value in this `Forever`, returning a mutable reference to it. | ||
| 59 | /// | ||
| 60 | /// The advantage over [`Forever::put`] is that this method allows the closure to construct | ||
| 61 | /// the `T` value in-place directly inside the `Forever`, saving stack space. | ||
| 62 | /// | ||
| 63 | /// # Panics | ||
| 64 | /// | ||
| 65 | /// Panics if this `Forever` already has a value stored in it. | ||
| 66 | #[inline(always)] | ||
| 67 | #[allow(clippy::mut_from_ref)] | ||
| 68 | pub fn put_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T { | ||
| 69 | if self | ||
| 70 | .used | ||
| 71 | .compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed) | ||
| 72 | .is_err() | ||
| 73 | { | ||
| 74 | panic!("Forever.put() called multiple times"); | ||
| 75 | } | ||
| 76 | |||
| 77 | let p: &'static mut MaybeUninit<T> = unsafe { &mut *self.t.get() }; | ||
| 78 | p.write(val()) | ||
| 79 | } | ||
| 80 | |||
| 81 | /// Unsafely get a mutable reference to the contents of this Forever. | ||
| 82 | /// | ||
| 83 | /// # Safety | ||
| 84 | /// | ||
| 85 | /// This is undefined behavior if: | ||
| 86 | /// | ||
| 87 | /// - The `Forever` has not been initialized yet (with `put' or `put_with`), or | ||
| 88 | /// - A reference to the contents (mutable or not) already exists. | ||
| 89 | #[inline(always)] | ||
| 90 | #[allow(clippy::mut_from_ref)] | ||
| 91 | pub unsafe fn steal(&self) -> &mut T { | ||
| 92 | let p: &mut MaybeUninit<T> = &mut *self.t.get(); | ||
| 93 | p.assume_init_mut() | ||
| 94 | } | ||
| 95 | } | ||
diff --git a/embassy-util/src/lib.rs b/embassy-util/src/lib.rs index 110c72811..8ec3300d9 100644 --- a/embassy-util/src/lib.rs +++ b/embassy-util/src/lib.rs | |||
| @@ -16,10 +16,8 @@ pub mod mutex; | |||
| 16 | pub mod pipe; | 16 | pub mod pipe; |
| 17 | pub mod waitqueue; | 17 | pub mod waitqueue; |
| 18 | 18 | ||
| 19 | mod forever; | ||
| 20 | mod select; | 19 | mod select; |
| 21 | mod yield_now; | 20 | mod yield_now; |
| 22 | 21 | ||
| 23 | pub use forever::*; | ||
| 24 | pub use select::*; | 22 | pub use select::*; |
| 25 | pub use yield_now::*; | 23 | pub use yield_now::*; |
diff --git a/embassy-util/src/pipe.rs b/embassy-util/src/pipe.rs index 9c20aeeff..d85b843e4 100644 --- a/embassy-util/src/pipe.rs +++ b/embassy-util/src/pipe.rs | |||
| @@ -461,10 +461,10 @@ mod io_impls { | |||
| 461 | mod tests { | 461 | mod tests { |
| 462 | use futures_executor::ThreadPool; | 462 | use futures_executor::ThreadPool; |
| 463 | use futures_util::task::SpawnExt; | 463 | use futures_util::task::SpawnExt; |
| 464 | use static_cell::StaticCell; | ||
| 464 | 465 | ||
| 465 | use super::*; | 466 | use super::*; |
| 466 | use crate::blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}; | 467 | use crate::blocking_mutex::raw::{CriticalSectionRawMutex, NoopRawMutex}; |
| 467 | use crate::Forever; | ||
| 468 | 468 | ||
| 469 | fn capacity<const N: usize>(c: &PipeState<N>) -> usize { | 469 | fn capacity<const N: usize>(c: &PipeState<N>) -> usize { |
| 470 | N - c.buffer.len() | 470 | N - c.buffer.len() |
| @@ -528,8 +528,8 @@ mod tests { | |||
| 528 | async fn receiver_receives_given_try_write_async() { | 528 | async fn receiver_receives_given_try_write_async() { |
| 529 | let executor = ThreadPool::new().unwrap(); | 529 | let executor = ThreadPool::new().unwrap(); |
| 530 | 530 | ||
| 531 | static CHANNEL: Forever<Pipe<CriticalSectionRawMutex, 3>> = Forever::new(); | 531 | static CHANNEL: StaticCell<Pipe<CriticalSectionRawMutex, 3>> = StaticCell::new(); |
| 532 | let c = &*CHANNEL.put(Pipe::new()); | 532 | let c = &*CHANNEL.init(Pipe::new()); |
| 533 | let c2 = c; | 533 | let c2 = c; |
| 534 | let f = async move { | 534 | let f = async move { |
| 535 | assert_eq!(c2.try_write(&[42]), Ok(1)); | 535 | assert_eq!(c2.try_write(&[42]), Ok(1)); |
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 2fcc31221..17f29b8fb 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -22,6 +22,7 @@ embedded-io = "0.3.0" | |||
| 22 | defmt = "0.3" | 22 | defmt = "0.3" |
| 23 | defmt-rtt = "0.3" | 23 | defmt-rtt = "0.3" |
| 24 | 24 | ||
| 25 | static_cell = "1.0" | ||
| 25 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | 26 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } |
| 26 | cortex-m-rt = "0.7.0" | 27 | cortex-m-rt = "0.7.0" |
| 27 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 28 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
diff --git a/examples/nrf/src/bin/channel_sender_receiver.rs b/examples/nrf/src/bin/channel_sender_receiver.rs index c9c458aec..d250b6a5c 100644 --- a/examples/nrf/src/bin/channel_sender_receiver.rs +++ b/examples/nrf/src/bin/channel_sender_receiver.rs | |||
| @@ -8,7 +8,7 @@ use embassy_nrf::gpio::{AnyPin, Level, Output, OutputDrive, Pin}; | |||
| 8 | use embassy_time::{Duration, Timer}; | 8 | use embassy_time::{Duration, Timer}; |
| 9 | use embassy_util::blocking_mutex::raw::NoopRawMutex; | 9 | use embassy_util::blocking_mutex::raw::NoopRawMutex; |
| 10 | use embassy_util::channel::mpmc::{Channel, Receiver, Sender}; | 10 | use embassy_util::channel::mpmc::{Channel, Receiver, Sender}; |
| 11 | use embassy_util::Forever; | 11 | use static_cell::StaticCell; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 12 | use {defmt_rtt as _, panic_probe as _}; |
| 13 | 13 | ||
| 14 | enum LedState { | 14 | enum LedState { |
| @@ -16,7 +16,7 @@ enum LedState { | |||
| 16 | Off, | 16 | Off, |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | static CHANNEL: Forever<Channel<NoopRawMutex, LedState, 1>> = Forever::new(); | 19 | static CHANNEL: StaticCell<Channel<NoopRawMutex, LedState, 1>> = StaticCell::new(); |
| 20 | 20 | ||
| 21 | #[embassy_executor::task] | 21 | #[embassy_executor::task] |
| 22 | async fn send_task(sender: Sender<'static, NoopRawMutex, LedState, 1>) { | 22 | async fn send_task(sender: Sender<'static, NoopRawMutex, LedState, 1>) { |
| @@ -43,7 +43,7 @@ async fn recv_task(led: AnyPin, receiver: Receiver<'static, NoopRawMutex, LedSta | |||
| 43 | #[embassy_executor::main] | 43 | #[embassy_executor::main] |
| 44 | async fn main(spawner: Spawner) { | 44 | async fn main(spawner: Spawner) { |
| 45 | let p = embassy_nrf::init(Default::default()); | 45 | let p = embassy_nrf::init(Default::default()); |
| 46 | let channel = CHANNEL.put(Channel::new()); | 46 | let channel = CHANNEL.init(Channel::new()); |
| 47 | 47 | ||
| 48 | unwrap!(spawner.spawn(send_task(channel.sender()))); | 48 | unwrap!(spawner.spawn(send_task(channel.sender()))); |
| 49 | unwrap!(spawner.spawn(recv_task(p.P0_13.degrade(), channel.receiver()))); | 49 | unwrap!(spawner.spawn(recv_task(p.P0_13.degrade(), channel.receiver()))); |
diff --git a/examples/nrf/src/bin/multiprio.rs b/examples/nrf/src/bin/multiprio.rs index b653689a7..25806ae48 100644 --- a/examples/nrf/src/bin/multiprio.rs +++ b/examples/nrf/src/bin/multiprio.rs | |||
| @@ -63,7 +63,7 @@ use embassy_nrf::executor::{Executor, InterruptExecutor}; | |||
| 63 | use embassy_nrf::interrupt; | 63 | use embassy_nrf::interrupt; |
| 64 | use embassy_nrf::interrupt::InterruptExt; | 64 | use embassy_nrf::interrupt::InterruptExt; |
| 65 | use embassy_time::{Duration, Instant, Timer}; | 65 | use embassy_time::{Duration, Instant, Timer}; |
| 66 | use embassy_util::Forever; | 66 | use static_cell::StaticCell; |
| 67 | use {defmt_rtt as _, panic_probe as _}; | 67 | use {defmt_rtt as _, panic_probe as _}; |
| 68 | 68 | ||
| 69 | #[embassy_executor::task] | 69 | #[embassy_executor::task] |
| @@ -108,9 +108,9 @@ async fn run_low() { | |||
| 108 | } | 108 | } |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | static EXECUTOR_HIGH: Forever<InterruptExecutor<interrupt::SWI1_EGU1>> = Forever::new(); | 111 | static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::SWI1_EGU1>> = StaticCell::new(); |
| 112 | static EXECUTOR_MED: Forever<InterruptExecutor<interrupt::SWI0_EGU0>> = Forever::new(); | 112 | static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::SWI0_EGU0>> = StaticCell::new(); |
| 113 | static EXECUTOR_LOW: Forever<Executor> = Forever::new(); | 113 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); |
| 114 | 114 | ||
| 115 | #[entry] | 115 | #[entry] |
| 116 | fn main() -> ! { | 116 | fn main() -> ! { |
| @@ -121,19 +121,19 @@ fn main() -> ! { | |||
| 121 | // High-priority executor: SWI1_EGU1, priority level 6 | 121 | // High-priority executor: SWI1_EGU1, priority level 6 |
| 122 | let irq = interrupt::take!(SWI1_EGU1); | 122 | let irq = interrupt::take!(SWI1_EGU1); |
| 123 | irq.set_priority(interrupt::Priority::P6); | 123 | irq.set_priority(interrupt::Priority::P6); |
| 124 | let executor = EXECUTOR_HIGH.put(InterruptExecutor::new(irq)); | 124 | let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq)); |
| 125 | let spawner = executor.start(); | 125 | let spawner = executor.start(); |
| 126 | unwrap!(spawner.spawn(run_high())); | 126 | unwrap!(spawner.spawn(run_high())); |
| 127 | 127 | ||
| 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 | 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 |
| 129 | let irq = interrupt::take!(SWI0_EGU0); | 129 | let irq = interrupt::take!(SWI0_EGU0); |
| 130 | irq.set_priority(interrupt::Priority::P7); | 130 | irq.set_priority(interrupt::Priority::P7); |
| 131 | let executor = EXECUTOR_MED.put(InterruptExecutor::new(irq)); | 131 | let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq)); |
| 132 | let spawner = executor.start(); | 132 | let spawner = executor.start(); |
| 133 | unwrap!(spawner.spawn(run_med())); | 133 | unwrap!(spawner.spawn(run_med())); |
| 134 | 134 | ||
| 135 | // Low priority executor: runs in thread mode, using WFE/SEV | 135 | // Low priority executor: runs in thread mode, using WFE/SEV |
| 136 | let executor = EXECUTOR_LOW.put(Executor::new()); | 136 | let executor = EXECUTOR_LOW.init(Executor::new()); |
| 137 | executor.run(|spawner| { | 137 | executor.run(|spawner| { |
| 138 | unwrap!(spawner.spawn(run_low())); | 138 | unwrap!(spawner.spawn(run_low())); |
| 139 | }); | 139 | }); |
diff --git a/examples/nrf/src/bin/raw_spawn.rs b/examples/nrf/src/bin/raw_spawn.rs index 415579be7..1b067f5e4 100644 --- a/examples/nrf/src/bin/raw_spawn.rs +++ b/examples/nrf/src/bin/raw_spawn.rs | |||
| @@ -8,7 +8,7 @@ use defmt::{info, unwrap}; | |||
| 8 | use embassy_executor::raw::TaskStorage; | 8 | use embassy_executor::raw::TaskStorage; |
| 9 | use embassy_executor::Executor; | 9 | use embassy_executor::Executor; |
| 10 | use embassy_time::{Duration, Timer}; | 10 | use embassy_time::{Duration, Timer}; |
| 11 | use embassy_util::Forever; | 11 | use static_cell::StaticCell; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 12 | use {defmt_rtt as _, panic_probe as _}; |
| 13 | 13 | ||
| 14 | async fn run1() { | 14 | async fn run1() { |
| @@ -25,14 +25,14 @@ async fn run2() { | |||
| 25 | } | 25 | } |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | static EXECUTOR: Forever<Executor> = Forever::new(); | 28 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 29 | 29 | ||
| 30 | #[entry] | 30 | #[entry] |
| 31 | fn main() -> ! { | 31 | fn main() -> ! { |
| 32 | info!("Hello World!"); | 32 | info!("Hello World!"); |
| 33 | 33 | ||
| 34 | let _p = embassy_nrf::init(Default::default()); | 34 | let _p = embassy_nrf::init(Default::default()); |
| 35 | let executor = EXECUTOR.put(Executor::new()); | 35 | let executor = EXECUTOR.init(Executor::new()); |
| 36 | 36 | ||
| 37 | let run1_task = TaskStorage::new(); | 37 | let run1_task = TaskStorage::new(); |
| 38 | let run2_task = TaskStorage::new(); | 38 | let run2_task = TaskStorage::new(); |
diff --git a/examples/nrf/src/bin/usb_ethernet.rs b/examples/nrf/src/bin/usb_ethernet.rs index f0a870317..d427f7563 100644 --- a/examples/nrf/src/bin/usb_ethernet.rs +++ b/examples/nrf/src/bin/usb_ethernet.rs | |||
| @@ -18,17 +18,17 @@ use embassy_usb::{Builder, Config, UsbDevice}; | |||
| 18 | use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; | 18 | use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; |
| 19 | use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; | 19 | use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; |
| 20 | use embassy_util::channel::mpmc::Channel; | 20 | use embassy_util::channel::mpmc::Channel; |
| 21 | use embassy_util::Forever; | ||
| 22 | use embedded_io::asynch::{Read, Write}; | 21 | use embedded_io::asynch::{Read, Write}; |
| 22 | use static_cell::StaticCell; | ||
| 23 | use {defmt_rtt as _, panic_probe as _}; | 23 | use {defmt_rtt as _, panic_probe as _}; |
| 24 | 24 | ||
| 25 | type MyDriver = Driver<'static, peripherals::USBD, PowerUsb>; | 25 | type MyDriver = Driver<'static, peripherals::USBD, PowerUsb>; |
| 26 | 26 | ||
| 27 | macro_rules! forever { | 27 | macro_rules! singleton { |
| 28 | ($val:expr) => {{ | 28 | ($val:expr) => {{ |
| 29 | type T = impl Sized; | 29 | type T = impl Sized; |
| 30 | static FOREVER: Forever<T> = Forever::new(); | 30 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 31 | FOREVER.put_with(move || $val) | 31 | STATIC_CELL.init_with(move || $val) |
| 32 | }}; | 32 | }}; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| @@ -116,7 +116,7 @@ async fn main(spawner: Spawner) { | |||
| 116 | control_buf: [u8; 128], | 116 | control_buf: [u8; 128], |
| 117 | serial_state: State<'static>, | 117 | serial_state: State<'static>, |
| 118 | } | 118 | } |
| 119 | let res: &mut Resources = forever!(Resources { | 119 | let res: &mut Resources = singleton!(Resources { |
| 120 | device_descriptor: [0; 256], | 120 | device_descriptor: [0; 256], |
| 121 | config_descriptor: [0; 256], | 121 | config_descriptor: [0; 256], |
| 122 | bos_descriptor: [0; 256], | 122 | bos_descriptor: [0; 256], |
| @@ -174,10 +174,10 @@ async fn main(spawner: Spawner) { | |||
| 174 | 174 | ||
| 175 | // Init network stack | 175 | // Init network stack |
| 176 | let device = Device { mac_addr: our_mac_addr }; | 176 | let device = Device { mac_addr: our_mac_addr }; |
| 177 | let stack = &*forever!(Stack::new( | 177 | let stack = &*singleton!(Stack::new( |
| 178 | device, | 178 | device, |
| 179 | config, | 179 | config, |
| 180 | forever!(StackResources::<1, 2, 8>::new()), | 180 | singleton!(StackResources::<1, 2, 8>::new()), |
| 181 | seed | 181 | seed |
| 182 | )); | 182 | )); |
| 183 | 183 | ||
diff --git a/examples/nrf/src/bin/usb_serial_multitask.rs b/examples/nrf/src/bin/usb_serial_multitask.rs index 4c1a93087..d62d7e520 100644 --- a/examples/nrf/src/bin/usb_serial_multitask.rs +++ b/examples/nrf/src/bin/usb_serial_multitask.rs | |||
| @@ -12,7 +12,7 @@ use embassy_nrf::{interrupt, pac, peripherals}; | |||
| 12 | use embassy_usb::driver::EndpointError; | 12 | use embassy_usb::driver::EndpointError; |
| 13 | use embassy_usb::{Builder, Config, UsbDevice}; | 13 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 14 | use embassy_usb_serial::{CdcAcmClass, State}; | 14 | use embassy_usb_serial::{CdcAcmClass, State}; |
| 15 | use embassy_util::Forever; | 15 | use static_cell::StaticCell; |
| 16 | use {defmt_rtt as _, panic_probe as _}; | 16 | use {defmt_rtt as _, panic_probe as _}; |
| 17 | 17 | ||
| 18 | type MyDriver = Driver<'static, peripherals::USBD, PowerUsb>; | 18 | type MyDriver = Driver<'static, peripherals::USBD, PowerUsb>; |
| @@ -67,8 +67,8 @@ async fn main(spawner: Spawner) { | |||
| 67 | control_buf: [u8; 64], | 67 | control_buf: [u8; 64], |
| 68 | serial_state: State<'static>, | 68 | serial_state: State<'static>, |
| 69 | } | 69 | } |
| 70 | static RESOURCES: Forever<Resources> = Forever::new(); | 70 | static RESOURCES: StaticCell<Resources> = StaticCell::new(); |
| 71 | let res = RESOURCES.put(Resources { | 71 | let res = RESOURCES.init(Resources { |
| 72 | device_descriptor: [0; 256], | 72 | device_descriptor: [0; 256], |
| 73 | config_descriptor: [0; 256], | 73 | config_descriptor: [0; 256], |
| 74 | bos_descriptor: [0; 256], | 74 | bos_descriptor: [0; 256], |
diff --git a/examples/std/Cargo.toml b/examples/std/Cargo.toml index b7009017c..164a2b42d 100644 --- a/examples/std/Cargo.toml +++ b/examples/std/Cargo.toml | |||
| @@ -20,3 +20,4 @@ libc = "0.2.101" | |||
| 20 | clap = { version = "3.0.0-beta.5", features = ["derive"] } | 20 | clap = { version = "3.0.0-beta.5", features = ["derive"] } |
| 21 | rand_core = { version = "0.6.3", features = ["std"] } | 21 | rand_core = { version = "0.6.3", features = ["std"] } |
| 22 | heapless = { version = "0.7.5", default-features = false } | 22 | heapless = { version = "0.7.5", default-features = false } |
| 23 | static_cell = "1.0" | ||
diff --git a/examples/std/src/bin/net.rs b/examples/std/src/bin/net.rs index 528609260..9b1450b72 100644 --- a/examples/std/src/bin/net.rs +++ b/examples/std/src/bin/net.rs | |||
| @@ -4,22 +4,22 @@ use clap::Parser; | |||
| 4 | use embassy_executor::{Executor, Spawner}; | 4 | use embassy_executor::{Executor, Spawner}; |
| 5 | use embassy_net::tcp::TcpSocket; | 5 | use embassy_net::tcp::TcpSocket; |
| 6 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, Stack, StackResources}; | 6 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, Stack, StackResources}; |
| 7 | use embassy_util::Forever; | ||
| 8 | use embedded_io::asynch::Write; | 7 | use embedded_io::asynch::Write; |
| 9 | use heapless::Vec; | 8 | use heapless::Vec; |
| 10 | use log::*; | 9 | use log::*; |
| 11 | use rand_core::{OsRng, RngCore}; | 10 | use rand_core::{OsRng, RngCore}; |
| 11 | use static_cell::StaticCell; | ||
| 12 | 12 | ||
| 13 | #[path = "../tuntap.rs"] | 13 | #[path = "../tuntap.rs"] |
| 14 | mod tuntap; | 14 | mod tuntap; |
| 15 | 15 | ||
| 16 | use crate::tuntap::TunTapDevice; | 16 | use crate::tuntap::TunTapDevice; |
| 17 | 17 | ||
| 18 | macro_rules! forever { | 18 | macro_rules! singleton { |
| 19 | ($val:expr) => {{ | 19 | ($val:expr) => {{ |
| 20 | type T = impl Sized; | 20 | type T = impl Sized; |
| 21 | static FOREVER: Forever<T> = Forever::new(); | 21 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 22 | FOREVER.put_with(move || $val) | 22 | STATIC_CELL.init_with(move || $val) |
| 23 | }}; | 23 | }}; |
| 24 | } | 24 | } |
| 25 | 25 | ||
| @@ -63,10 +63,10 @@ async fn main_task(spawner: Spawner) { | |||
| 63 | let seed = u64::from_le_bytes(seed); | 63 | let seed = u64::from_le_bytes(seed); |
| 64 | 64 | ||
| 65 | // Init network stack | 65 | // Init network stack |
| 66 | let stack = &*forever!(Stack::new( | 66 | let stack = &*singleton!(Stack::new( |
| 67 | device, | 67 | device, |
| 68 | config, | 68 | config, |
| 69 | forever!(StackResources::<1, 2, 8>::new()), | 69 | singleton!(StackResources::<1, 2, 8>::new()), |
| 70 | seed | 70 | seed |
| 71 | )); | 71 | )); |
| 72 | 72 | ||
| @@ -97,7 +97,7 @@ async fn main_task(spawner: Spawner) { | |||
| 97 | } | 97 | } |
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | static EXECUTOR: Forever<Executor> = Forever::new(); | 100 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 101 | 101 | ||
| 102 | fn main() { | 102 | fn main() { |
| 103 | env_logger::builder() | 103 | env_logger::builder() |
| @@ -106,7 +106,7 @@ fn main() { | |||
| 106 | .format_timestamp_nanos() | 106 | .format_timestamp_nanos() |
| 107 | .init(); | 107 | .init(); |
| 108 | 108 | ||
| 109 | let executor = EXECUTOR.put(Executor::new()); | 109 | let executor = EXECUTOR.init(Executor::new()); |
| 110 | executor.run(|spawner| { | 110 | executor.run(|spawner| { |
| 111 | spawner.spawn(main_task(spawner)).unwrap(); | 111 | spawner.spawn(main_task(spawner)).unwrap(); |
| 112 | }); | 112 | }); |
diff --git a/examples/std/src/bin/net_udp.rs b/examples/std/src/bin/net_udp.rs index 07e11c385..392a97f0d 100644 --- a/examples/std/src/bin/net_udp.rs +++ b/examples/std/src/bin/net_udp.rs | |||
| @@ -4,21 +4,21 @@ use clap::Parser; | |||
| 4 | use embassy_executor::{Executor, Spawner}; | 4 | use embassy_executor::{Executor, Spawner}; |
| 5 | use embassy_net::udp::UdpSocket; | 5 | use embassy_net::udp::UdpSocket; |
| 6 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, PacketMetadata, Stack, StackResources}; | 6 | use embassy_net::{ConfigStrategy, Ipv4Address, Ipv4Cidr, PacketMetadata, Stack, StackResources}; |
| 7 | use embassy_util::Forever; | ||
| 8 | use heapless::Vec; | 7 | use heapless::Vec; |
| 9 | use log::*; | 8 | use log::*; |
| 10 | use rand_core::{OsRng, RngCore}; | 9 | use rand_core::{OsRng, RngCore}; |
| 10 | use static_cell::StaticCell; | ||
| 11 | 11 | ||
| 12 | #[path = "../tuntap.rs"] | 12 | #[path = "../tuntap.rs"] |
| 13 | mod tuntap; | 13 | mod tuntap; |
| 14 | 14 | ||
| 15 | use crate::tuntap::TunTapDevice; | 15 | use crate::tuntap::TunTapDevice; |
| 16 | 16 | ||
| 17 | macro_rules! forever { | 17 | macro_rules! singleton { |
| 18 | ($val:expr) => {{ | 18 | ($val:expr) => {{ |
| 19 | type T = impl Sized; | 19 | type T = impl Sized; |
| 20 | static FOREVER: Forever<T> = Forever::new(); | 20 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 21 | FOREVER.put_with(move || $val) | 21 | STATIC_CELL.init_with(move || $val) |
| 22 | }}; | 22 | }}; |
| 23 | } | 23 | } |
| 24 | 24 | ||
| @@ -62,10 +62,10 @@ async fn main_task(spawner: Spawner) { | |||
| 62 | let seed = u64::from_le_bytes(seed); | 62 | let seed = u64::from_le_bytes(seed); |
| 63 | 63 | ||
| 64 | // Init network stack | 64 | // Init network stack |
| 65 | let stack = &*forever!(Stack::new( | 65 | let stack = &*singleton!(Stack::new( |
| 66 | device, | 66 | device, |
| 67 | config, | 67 | config, |
| 68 | forever!(StackResources::<1, 2, 8>::new()), | 68 | singleton!(StackResources::<1, 2, 8>::new()), |
| 69 | seed | 69 | seed |
| 70 | )); | 70 | )); |
| 71 | 71 | ||
| @@ -93,7 +93,7 @@ async fn main_task(spawner: Spawner) { | |||
| 93 | } | 93 | } |
| 94 | } | 94 | } |
| 95 | 95 | ||
| 96 | static EXECUTOR: Forever<Executor> = Forever::new(); | 96 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 97 | 97 | ||
| 98 | fn main() { | 98 | fn main() { |
| 99 | env_logger::builder() | 99 | env_logger::builder() |
| @@ -102,7 +102,7 @@ fn main() { | |||
| 102 | .format_timestamp_nanos() | 102 | .format_timestamp_nanos() |
| 103 | .init(); | 103 | .init(); |
| 104 | 104 | ||
| 105 | let executor = EXECUTOR.put(Executor::new()); | 105 | let executor = EXECUTOR.init(Executor::new()); |
| 106 | executor.run(|spawner| { | 106 | executor.run(|spawner| { |
| 107 | spawner.spawn(main_task(spawner)).unwrap(); | 107 | spawner.spawn(main_task(spawner)).unwrap(); |
| 108 | }); | 108 | }); |
diff --git a/examples/std/src/bin/serial.rs b/examples/std/src/bin/serial.rs index 35cba4cee..85ee54f70 100644 --- a/examples/std/src/bin/serial.rs +++ b/examples/std/src/bin/serial.rs | |||
| @@ -5,10 +5,10 @@ mod serial_port; | |||
| 5 | 5 | ||
| 6 | use async_io::Async; | 6 | use async_io::Async; |
| 7 | use embassy_executor::Executor; | 7 | use embassy_executor::Executor; |
| 8 | use embassy_util::Forever; | ||
| 9 | use embedded_io::asynch::Read; | 8 | use embedded_io::asynch::Read; |
| 10 | use log::*; | 9 | use log::*; |
| 11 | use nix::sys::termios; | 10 | use nix::sys::termios; |
| 11 | use static_cell::StaticCell; | ||
| 12 | 12 | ||
| 13 | use self::serial_port::SerialPort; | 13 | use self::serial_port::SerialPort; |
| 14 | 14 | ||
| @@ -40,7 +40,7 @@ async fn run() { | |||
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | static EXECUTOR: Forever<Executor> = Forever::new(); | 43 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 44 | 44 | ||
| 45 | fn main() { | 45 | fn main() { |
| 46 | env_logger::builder() | 46 | env_logger::builder() |
| @@ -49,7 +49,7 @@ fn main() { | |||
| 49 | .format_timestamp_nanos() | 49 | .format_timestamp_nanos() |
| 50 | .init(); | 50 | .init(); |
| 51 | 51 | ||
| 52 | let executor = EXECUTOR.put(Executor::new()); | 52 | let executor = EXECUTOR.init(Executor::new()); |
| 53 | executor.run(|spawner| { | 53 | executor.run(|spawner| { |
| 54 | spawner.spawn(run()).unwrap(); | 54 | spawner.spawn(run()).unwrap(); |
| 55 | }); | 55 | }); |
diff --git a/examples/stm32f3/Cargo.toml b/examples/stm32f3/Cargo.toml index b5ea28bb6..4e6b0ea1e 100644 --- a/examples/stm32f3/Cargo.toml +++ b/examples/stm32f3/Cargo.toml | |||
| @@ -23,3 +23,4 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 23 | heapless = { version = "0.7.5", default-features = false } | 23 | heapless = { version = "0.7.5", default-features = false } |
| 24 | nb = "1.0.0" | 24 | nb = "1.0.0" |
| 25 | embedded-storage = "0.3.0" | 25 | embedded-storage = "0.3.0" |
| 26 | static_cell = "1.0" | ||
diff --git a/examples/stm32f3/src/bin/multiprio.rs b/examples/stm32f3/src/bin/multiprio.rs index e96c31249..9e8228a4b 100644 --- a/examples/stm32f3/src/bin/multiprio.rs +++ b/examples/stm32f3/src/bin/multiprio.rs | |||
| @@ -63,7 +63,7 @@ use embassy_stm32::executor::{Executor, InterruptExecutor}; | |||
| 63 | use embassy_stm32::interrupt; | 63 | use embassy_stm32::interrupt; |
| 64 | use embassy_stm32::interrupt::InterruptExt; | 64 | use embassy_stm32::interrupt::InterruptExt; |
| 65 | use embassy_time::{Duration, Instant, Timer}; | 65 | use embassy_time::{Duration, Instant, Timer}; |
| 66 | use embassy_util::Forever; | 66 | use static_cell::StaticCell; |
| 67 | use {defmt_rtt as _, panic_probe as _}; | 67 | use {defmt_rtt as _, panic_probe as _}; |
| 68 | 68 | ||
| 69 | #[embassy_executor::task] | 69 | #[embassy_executor::task] |
| @@ -108,9 +108,9 @@ async fn run_low() { | |||
| 108 | } | 108 | } |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | static EXECUTOR_HIGH: Forever<InterruptExecutor<interrupt::UART4>> = Forever::new(); | 111 | static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::UART4>> = StaticCell::new(); |
| 112 | static EXECUTOR_MED: Forever<InterruptExecutor<interrupt::UART5>> = Forever::new(); | 112 | static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::UART5>> = StaticCell::new(); |
| 113 | static EXECUTOR_LOW: Forever<Executor> = Forever::new(); | 113 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); |
| 114 | 114 | ||
| 115 | #[entry] | 115 | #[entry] |
| 116 | fn main() -> ! { | 116 | fn main() -> ! { |
| @@ -121,19 +121,19 @@ fn main() -> ! { | |||
| 121 | // High-priority executor: SWI1_EGU1, priority level 6 | 121 | // High-priority executor: SWI1_EGU1, priority level 6 |
| 122 | let irq = interrupt::take!(UART4); | 122 | let irq = interrupt::take!(UART4); |
| 123 | irq.set_priority(interrupt::Priority::P6); | 123 | irq.set_priority(interrupt::Priority::P6); |
| 124 | let executor = EXECUTOR_HIGH.put(InterruptExecutor::new(irq)); | 124 | let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq)); |
| 125 | let spawner = executor.start(); | 125 | let spawner = executor.start(); |
| 126 | unwrap!(spawner.spawn(run_high())); | 126 | unwrap!(spawner.spawn(run_high())); |
| 127 | 127 | ||
| 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 | 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 |
| 129 | let irq = interrupt::take!(UART5); | 129 | let irq = interrupt::take!(UART5); |
| 130 | irq.set_priority(interrupt::Priority::P7); | 130 | irq.set_priority(interrupt::Priority::P7); |
| 131 | let executor = EXECUTOR_MED.put(InterruptExecutor::new(irq)); | 131 | let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq)); |
| 132 | let spawner = executor.start(); | 132 | let spawner = executor.start(); |
| 133 | unwrap!(spawner.spawn(run_med())); | 133 | unwrap!(spawner.spawn(run_med())); |
| 134 | 134 | ||
| 135 | // Low priority executor: runs in thread mode, using WFE/SEV | 135 | // Low priority executor: runs in thread mode, using WFE/SEV |
| 136 | let executor = EXECUTOR_LOW.put(Executor::new()); | 136 | let executor = EXECUTOR_LOW.init(Executor::new()); |
| 137 | executor.run(|spawner| { | 137 | executor.run(|spawner| { |
| 138 | unwrap!(spawner.spawn(run_low())); | 138 | unwrap!(spawner.spawn(run_low())); |
| 139 | }); | 139 | }); |
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 04a217aff..f93a1d0f9 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -23,6 +23,7 @@ heapless = { version = "0.7.5", default-features = false } | |||
| 23 | nb = "1.0.0" | 23 | nb = "1.0.0" |
| 24 | embedded-storage = "0.3.0" | 24 | embedded-storage = "0.3.0" |
| 25 | micromath = "2.0.0" | 25 | micromath = "2.0.0" |
| 26 | static_cell = "1.0" | ||
| 26 | 27 | ||
| 27 | usb-device = "0.2" | 28 | usb-device = "0.2" |
| 28 | usbd-serial = "0.1.1" | 29 | usbd-serial = "0.1.1" |
diff --git a/examples/stm32f4/src/bin/multiprio.rs b/examples/stm32f4/src/bin/multiprio.rs index e96c31249..9e8228a4b 100644 --- a/examples/stm32f4/src/bin/multiprio.rs +++ b/examples/stm32f4/src/bin/multiprio.rs | |||
| @@ -63,7 +63,7 @@ use embassy_stm32::executor::{Executor, InterruptExecutor}; | |||
| 63 | use embassy_stm32::interrupt; | 63 | use embassy_stm32::interrupt; |
| 64 | use embassy_stm32::interrupt::InterruptExt; | 64 | use embassy_stm32::interrupt::InterruptExt; |
| 65 | use embassy_time::{Duration, Instant, Timer}; | 65 | use embassy_time::{Duration, Instant, Timer}; |
| 66 | use embassy_util::Forever; | 66 | use static_cell::StaticCell; |
| 67 | use {defmt_rtt as _, panic_probe as _}; | 67 | use {defmt_rtt as _, panic_probe as _}; |
| 68 | 68 | ||
| 69 | #[embassy_executor::task] | 69 | #[embassy_executor::task] |
| @@ -108,9 +108,9 @@ async fn run_low() { | |||
| 108 | } | 108 | } |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | static EXECUTOR_HIGH: Forever<InterruptExecutor<interrupt::UART4>> = Forever::new(); | 111 | static EXECUTOR_HIGH: StaticCell<InterruptExecutor<interrupt::UART4>> = StaticCell::new(); |
| 112 | static EXECUTOR_MED: Forever<InterruptExecutor<interrupt::UART5>> = Forever::new(); | 112 | static EXECUTOR_MED: StaticCell<InterruptExecutor<interrupt::UART5>> = StaticCell::new(); |
| 113 | static EXECUTOR_LOW: Forever<Executor> = Forever::new(); | 113 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); |
| 114 | 114 | ||
| 115 | #[entry] | 115 | #[entry] |
| 116 | fn main() -> ! { | 116 | fn main() -> ! { |
| @@ -121,19 +121,19 @@ fn main() -> ! { | |||
| 121 | // High-priority executor: SWI1_EGU1, priority level 6 | 121 | // High-priority executor: SWI1_EGU1, priority level 6 |
| 122 | let irq = interrupt::take!(UART4); | 122 | let irq = interrupt::take!(UART4); |
| 123 | irq.set_priority(interrupt::Priority::P6); | 123 | irq.set_priority(interrupt::Priority::P6); |
| 124 | let executor = EXECUTOR_HIGH.put(InterruptExecutor::new(irq)); | 124 | let executor = EXECUTOR_HIGH.init(InterruptExecutor::new(irq)); |
| 125 | let spawner = executor.start(); | 125 | let spawner = executor.start(); |
| 126 | unwrap!(spawner.spawn(run_high())); | 126 | unwrap!(spawner.spawn(run_high())); |
| 127 | 127 | ||
| 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 | 128 | // Medium-priority executor: SWI0_EGU0, priority level 7 |
| 129 | let irq = interrupt::take!(UART5); | 129 | let irq = interrupt::take!(UART5); |
| 130 | irq.set_priority(interrupt::Priority::P7); | 130 | irq.set_priority(interrupt::Priority::P7); |
| 131 | let executor = EXECUTOR_MED.put(InterruptExecutor::new(irq)); | 131 | let executor = EXECUTOR_MED.init(InterruptExecutor::new(irq)); |
| 132 | let spawner = executor.start(); | 132 | let spawner = executor.start(); |
| 133 | unwrap!(spawner.spawn(run_med())); | 133 | unwrap!(spawner.spawn(run_med())); |
| 134 | 134 | ||
| 135 | // Low priority executor: runs in thread mode, using WFE/SEV | 135 | // Low priority executor: runs in thread mode, using WFE/SEV |
| 136 | let executor = EXECUTOR_LOW.put(Executor::new()); | 136 | let executor = EXECUTOR_LOW.init(Executor::new()); |
| 137 | executor.run(|spawner| { | 137 | executor.run(|spawner| { |
| 138 | unwrap!(spawner.spawn(run_low())); | 138 | unwrap!(spawner.spawn(run_low())); |
| 139 | }); | 139 | }); |
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index 29d6da4d8..e286d2310 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml | |||
| @@ -24,3 +24,4 @@ nb = "1.0.0" | |||
| 24 | rand_core = "0.6.3" | 24 | rand_core = "0.6.3" |
| 25 | critical-section = "1.1" | 25 | critical-section = "1.1" |
| 26 | embedded-storage = "0.3.0" | 26 | embedded-storage = "0.3.0" |
| 27 | static_cell = "1.0" | ||
diff --git a/examples/stm32f7/src/bin/eth.rs b/examples/stm32f7/src/bin/eth.rs index bdffabcb3..5202edf62 100644 --- a/examples/stm32f7/src/bin/eth.rs +++ b/examples/stm32f7/src/bin/eth.rs | |||
| @@ -13,16 +13,16 @@ use embassy_stm32::rng::Rng; | |||
| 13 | use embassy_stm32::time::mhz; | 13 | use embassy_stm32::time::mhz; |
| 14 | use embassy_stm32::{interrupt, Config}; | 14 | use embassy_stm32::{interrupt, Config}; |
| 15 | use embassy_time::{Duration, Timer}; | 15 | use embassy_time::{Duration, Timer}; |
| 16 | use embassy_util::Forever; | ||
| 17 | use embedded_io::asynch::Write; | 16 | use embedded_io::asynch::Write; |
| 18 | use rand_core::RngCore; | 17 | use rand_core::RngCore; |
| 18 | use static_cell::StaticCell; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | 19 | use {defmt_rtt as _, panic_probe as _}; |
| 20 | 20 | ||
| 21 | macro_rules! forever { | 21 | macro_rules! singleton { |
| 22 | ($val:expr) => {{ | 22 | ($val:expr) => {{ |
| 23 | type T = impl Sized; | 23 | type T = impl Sized; |
| 24 | static FOREVER: Forever<T> = Forever::new(); | 24 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 25 | FOREVER.put_with(move || $val) | 25 | STATIC_CELL.init_with(move || $val) |
| 26 | }}; | 26 | }}; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| @@ -52,7 +52,7 @@ async fn main(spawner: Spawner) -> ! { | |||
| 52 | 52 | ||
| 53 | let device = unsafe { | 53 | let device = unsafe { |
| 54 | Ethernet::new( | 54 | Ethernet::new( |
| 55 | forever!(State::new()), | 55 | singleton!(State::new()), |
| 56 | p.ETH, | 56 | p.ETH, |
| 57 | eth_int, | 57 | eth_int, |
| 58 | p.PA1, | 58 | p.PA1, |
| @@ -78,10 +78,10 @@ async fn main(spawner: Spawner) -> ! { | |||
| 78 | //}); | 78 | //}); |
| 79 | 79 | ||
| 80 | // Init network stack | 80 | // Init network stack |
| 81 | let stack = &*forever!(Stack::new( | 81 | let stack = &*singleton!(Stack::new( |
| 82 | device, | 82 | device, |
| 83 | config, | 83 | config, |
| 84 | forever!(StackResources::<1, 2, 8>::new()), | 84 | singleton!(StackResources::<1, 2, 8>::new()), |
| 85 | seed | 85 | seed |
| 86 | )); | 86 | )); |
| 87 | 87 | ||
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index a416796ea..fc5f74f99 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -28,6 +28,7 @@ critical-section = "1.1" | |||
| 28 | micromath = "2.0.0" | 28 | micromath = "2.0.0" |
| 29 | stm32-fmc = "0.2.4" | 29 | stm32-fmc = "0.2.4" |
| 30 | embedded-storage = "0.3.0" | 30 | embedded-storage = "0.3.0" |
| 31 | static_cell = "1.0" | ||
| 31 | 32 | ||
| 32 | # cargo build/run | 33 | # cargo build/run |
| 33 | [profile.dev] | 34 | [profile.dev] |
diff --git a/examples/stm32h7/src/bin/eth.rs b/examples/stm32h7/src/bin/eth.rs index 83210bcb5..4ccc0b5ef 100644 --- a/examples/stm32h7/src/bin/eth.rs +++ b/examples/stm32h7/src/bin/eth.rs | |||
| @@ -13,16 +13,16 @@ use embassy_stm32::rng::Rng; | |||
| 13 | use embassy_stm32::time::mhz; | 13 | use embassy_stm32::time::mhz; |
| 14 | use embassy_stm32::{interrupt, Config}; | 14 | use embassy_stm32::{interrupt, Config}; |
| 15 | use embassy_time::{Duration, Timer}; | 15 | use embassy_time::{Duration, Timer}; |
| 16 | use embassy_util::Forever; | ||
| 17 | use embedded_io::asynch::Write; | 16 | use embedded_io::asynch::Write; |
| 18 | use rand_core::RngCore; | 17 | use rand_core::RngCore; |
| 18 | use static_cell::StaticCell; | ||
| 19 | use {defmt_rtt as _, panic_probe as _}; | 19 | use {defmt_rtt as _, panic_probe as _}; |
| 20 | 20 | ||
| 21 | macro_rules! forever { | 21 | macro_rules! singleton { |
| 22 | ($val:expr) => {{ | 22 | ($val:expr) => {{ |
| 23 | type T = impl Sized; | 23 | type T = impl Sized; |
| 24 | static FOREVER: Forever<T> = Forever::new(); | 24 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 25 | FOREVER.put_with(move || $val) | 25 | STATIC_CELL.init_with(move || $val) |
| 26 | }}; | 26 | }}; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| @@ -53,7 +53,7 @@ async fn main(spawner: Spawner) -> ! { | |||
| 53 | 53 | ||
| 54 | let device = unsafe { | 54 | let device = unsafe { |
| 55 | Ethernet::new( | 55 | Ethernet::new( |
| 56 | forever!(State::new()), | 56 | singleton!(State::new()), |
| 57 | p.ETH, | 57 | p.ETH, |
| 58 | eth_int, | 58 | eth_int, |
| 59 | p.PA1, | 59 | p.PA1, |
| @@ -79,10 +79,10 @@ async fn main(spawner: Spawner) -> ! { | |||
| 79 | //}); | 79 | //}); |
| 80 | 80 | ||
| 81 | // Init network stack | 81 | // Init network stack |
| 82 | let stack = &*forever!(Stack::new( | 82 | let stack = &*singleton!(Stack::new( |
| 83 | device, | 83 | device, |
| 84 | config, | 84 | config, |
| 85 | forever!(StackResources::<1, 2, 8>::new()), | 85 | singleton!(StackResources::<1, 2, 8>::new()), |
| 86 | seed | 86 | seed |
| 87 | )); | 87 | )); |
| 88 | 88 | ||
diff --git a/examples/stm32h7/src/bin/eth_client.rs b/examples/stm32h7/src/bin/eth_client.rs index 99946f504..64fd84141 100644 --- a/examples/stm32h7/src/bin/eth_client.rs +++ b/examples/stm32h7/src/bin/eth_client.rs | |||
| @@ -13,17 +13,17 @@ use embassy_stm32::rng::Rng; | |||
| 13 | use embassy_stm32::time::mhz; | 13 | use embassy_stm32::time::mhz; |
| 14 | use embassy_stm32::{interrupt, Config}; | 14 | use embassy_stm32::{interrupt, Config}; |
| 15 | use embassy_time::{Duration, Timer}; | 15 | use embassy_time::{Duration, Timer}; |
| 16 | use embassy_util::Forever; | ||
| 17 | use embedded_io::asynch::Write; | 16 | use embedded_io::asynch::Write; |
| 18 | use embedded_nal_async::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpConnect}; | 17 | use embedded_nal_async::{Ipv4Addr, SocketAddr, SocketAddrV4, TcpConnect}; |
| 19 | use rand_core::RngCore; | 18 | use rand_core::RngCore; |
| 19 | use static_cell::StaticCell; | ||
| 20 | use {defmt_rtt as _, panic_probe as _}; | 20 | use {defmt_rtt as _, panic_probe as _}; |
| 21 | 21 | ||
| 22 | macro_rules! forever { | 22 | macro_rules! singleton { |
| 23 | ($val:expr) => {{ | 23 | ($val:expr) => {{ |
| 24 | type T = impl Sized; | 24 | type T = impl Sized; |
| 25 | static FOREVER: Forever<T> = Forever::new(); | 25 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 26 | FOREVER.put_with(move || $val) | 26 | STATIC_CELL.init_with(move || $val) |
| 27 | }}; | 27 | }}; |
| 28 | } | 28 | } |
| 29 | 29 | ||
| @@ -54,7 +54,7 @@ async fn main(spawner: Spawner) -> ! { | |||
| 54 | 54 | ||
| 55 | let device = unsafe { | 55 | let device = unsafe { |
| 56 | Ethernet::new( | 56 | Ethernet::new( |
| 57 | forever!(State::new()), | 57 | singleton!(State::new()), |
| 58 | p.ETH, | 58 | p.ETH, |
| 59 | eth_int, | 59 | eth_int, |
| 60 | p.PA1, | 60 | p.PA1, |
| @@ -80,10 +80,10 @@ async fn main(spawner: Spawner) -> ! { | |||
| 80 | //}); | 80 | //}); |
| 81 | 81 | ||
| 82 | // Init network stack | 82 | // Init network stack |
| 83 | let stack = &*forever!(Stack::new( | 83 | let stack = &*singleton!(Stack::new( |
| 84 | device, | 84 | device, |
| 85 | config, | 85 | config, |
| 86 | forever!(StackResources::<1, 2, 8>::new()), | 86 | singleton!(StackResources::<1, 2, 8>::new()), |
| 87 | seed | 87 | seed |
| 88 | )); | 88 | )); |
| 89 | 89 | ||
diff --git a/examples/stm32h7/src/bin/spi.rs b/examples/stm32h7/src/bin/spi.rs index c28f937a8..1f407f002 100644 --- a/examples/stm32h7/src/bin/spi.rs +++ b/examples/stm32h7/src/bin/spi.rs | |||
| @@ -12,8 +12,8 @@ use embassy_stm32::dma::NoDma; | |||
| 12 | use embassy_stm32::peripherals::SPI3; | 12 | use embassy_stm32::peripherals::SPI3; |
| 13 | use embassy_stm32::time::mhz; | 13 | use embassy_stm32::time::mhz; |
| 14 | use embassy_stm32::{spi, Config}; | 14 | use embassy_stm32::{spi, Config}; |
| 15 | use embassy_util::Forever; | ||
| 16 | use heapless::String; | 15 | use heapless::String; |
| 16 | use static_cell::StaticCell; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | 17 | use {defmt_rtt as _, panic_probe as _}; |
| 18 | 18 | ||
| 19 | #[embassy_executor::task] | 19 | #[embassy_executor::task] |
| @@ -31,7 +31,7 @@ async fn main_task(mut spi: spi::Spi<'static, SPI3, NoDma, NoDma>) { | |||
| 31 | } | 31 | } |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | static EXECUTOR: Forever<Executor> = Forever::new(); | 34 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 35 | 35 | ||
| 36 | #[entry] | 36 | #[entry] |
| 37 | fn main() -> ! { | 37 | fn main() -> ! { |
| @@ -54,7 +54,7 @@ fn main() -> ! { | |||
| 54 | spi::Config::default(), | 54 | spi::Config::default(), |
| 55 | ); | 55 | ); |
| 56 | 56 | ||
| 57 | let executor = EXECUTOR.put(Executor::new()); | 57 | let executor = EXECUTOR.init(Executor::new()); |
| 58 | 58 | ||
| 59 | executor.run(|spawner| { | 59 | executor.run(|spawner| { |
| 60 | unwrap!(spawner.spawn(main_task(spi))); | 60 | unwrap!(spawner.spawn(main_task(spi))); |
diff --git a/examples/stm32h7/src/bin/spi_dma.rs b/examples/stm32h7/src/bin/spi_dma.rs index 6c78c194f..53004fc9b 100644 --- a/examples/stm32h7/src/bin/spi_dma.rs +++ b/examples/stm32h7/src/bin/spi_dma.rs | |||
| @@ -11,8 +11,8 @@ use embassy_executor::Executor; | |||
| 11 | use embassy_stm32::peripherals::{DMA1_CH3, DMA1_CH4, SPI3}; | 11 | use embassy_stm32::peripherals::{DMA1_CH3, DMA1_CH4, SPI3}; |
| 12 | use embassy_stm32::time::mhz; | 12 | use embassy_stm32::time::mhz; |
| 13 | use embassy_stm32::{spi, Config}; | 13 | use embassy_stm32::{spi, Config}; |
| 14 | use embassy_util::Forever; | ||
| 15 | use heapless::String; | 14 | use heapless::String; |
| 15 | use static_cell::StaticCell; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | 16 | use {defmt_rtt as _, panic_probe as _}; |
| 17 | 17 | ||
| 18 | #[embassy_executor::task] | 18 | #[embassy_executor::task] |
| @@ -27,7 +27,7 @@ async fn main_task(mut spi: spi::Spi<'static, SPI3, DMA1_CH3, DMA1_CH4>) { | |||
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | static EXECUTOR: Forever<Executor> = Forever::new(); | 30 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 31 | 31 | ||
| 32 | #[entry] | 32 | #[entry] |
| 33 | fn main() -> ! { | 33 | fn main() -> ! { |
| @@ -50,7 +50,7 @@ fn main() -> ! { | |||
| 50 | spi::Config::default(), | 50 | spi::Config::default(), |
| 51 | ); | 51 | ); |
| 52 | 52 | ||
| 53 | let executor = EXECUTOR.put(Executor::new()); | 53 | let executor = EXECUTOR.init(Executor::new()); |
| 54 | 54 | ||
| 55 | executor.run(|spawner| { | 55 | executor.run(|spawner| { |
| 56 | unwrap!(spawner.spawn(main_task(spi))); | 56 | unwrap!(spawner.spawn(main_task(spi))); |
diff --git a/examples/stm32h7/src/bin/usart.rs b/examples/stm32h7/src/bin/usart.rs index 1384d54c6..87c2b1253 100644 --- a/examples/stm32h7/src/bin/usart.rs +++ b/examples/stm32h7/src/bin/usart.rs | |||
| @@ -7,7 +7,7 @@ use defmt::*; | |||
| 7 | use embassy_executor::Executor; | 7 | use embassy_executor::Executor; |
| 8 | use embassy_stm32::dma::NoDma; | 8 | use embassy_stm32::dma::NoDma; |
| 9 | use embassy_stm32::usart::{Config, Uart}; | 9 | use embassy_stm32::usart::{Config, Uart}; |
| 10 | use embassy_util::Forever; | 10 | use static_cell::StaticCell; |
| 11 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| 12 | 12 | ||
| 13 | #[embassy_executor::task] | 13 | #[embassy_executor::task] |
| @@ -27,13 +27,13 @@ async fn main_task() { | |||
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | static EXECUTOR: Forever<Executor> = Forever::new(); | 30 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 31 | 31 | ||
| 32 | #[entry] | 32 | #[entry] |
| 33 | fn main() -> ! { | 33 | fn main() -> ! { |
| 34 | info!("Hello World!"); | 34 | info!("Hello World!"); |
| 35 | 35 | ||
| 36 | let executor = EXECUTOR.put(Executor::new()); | 36 | let executor = EXECUTOR.init(Executor::new()); |
| 37 | 37 | ||
| 38 | executor.run(|spawner| { | 38 | executor.run(|spawner| { |
| 39 | unwrap!(spawner.spawn(main_task())); | 39 | unwrap!(spawner.spawn(main_task())); |
diff --git a/examples/stm32h7/src/bin/usart_dma.rs b/examples/stm32h7/src/bin/usart_dma.rs index f8d58bb84..3adffcbeb 100644 --- a/examples/stm32h7/src/bin/usart_dma.rs +++ b/examples/stm32h7/src/bin/usart_dma.rs | |||
| @@ -9,8 +9,8 @@ use defmt::*; | |||
| 9 | use embassy_executor::Executor; | 9 | use embassy_executor::Executor; |
| 10 | use embassy_stm32::dma::NoDma; | 10 | use embassy_stm32::dma::NoDma; |
| 11 | use embassy_stm32::usart::{Config, Uart}; | 11 | use embassy_stm32::usart::{Config, Uart}; |
| 12 | use embassy_util::Forever; | ||
| 13 | use heapless::String; | 12 | use heapless::String; |
| 13 | use static_cell::StaticCell; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | 14 | use {defmt_rtt as _, panic_probe as _}; |
| 15 | 15 | ||
| 16 | #[embassy_executor::task] | 16 | #[embassy_executor::task] |
| @@ -30,13 +30,13 @@ async fn main_task() { | |||
| 30 | } | 30 | } |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | static EXECUTOR: Forever<Executor> = Forever::new(); | 33 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 34 | 34 | ||
| 35 | #[entry] | 35 | #[entry] |
| 36 | fn main() -> ! { | 36 | fn main() -> ! { |
| 37 | info!("Hello World!"); | 37 | info!("Hello World!"); |
| 38 | 38 | ||
| 39 | let executor = EXECUTOR.put(Executor::new()); | 39 | let executor = EXECUTOR.init(Executor::new()); |
| 40 | 40 | ||
| 41 | executor.run(|spawner| { | 41 | executor.run(|spawner| { |
| 42 | unwrap!(spawner.spawn(main_task())); | 42 | unwrap!(spawner.spawn(main_task())); |
diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index fdb716d15..72365a640 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml | |||
| @@ -29,3 +29,4 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 29 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 29 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 30 | heapless = { version = "0.7.5", default-features = false } | 30 | heapless = { version = "0.7.5", default-features = false } |
| 31 | embedded-hal = "0.2.6" | 31 | embedded-hal = "0.2.6" |
| 32 | static_cell = "1.0" | ||
diff --git a/examples/stm32l0/src/bin/raw_spawn.rs b/examples/stm32l0/src/bin/raw_spawn.rs index bd87e62a4..edc17304a 100644 --- a/examples/stm32l0/src/bin/raw_spawn.rs +++ b/examples/stm32l0/src/bin/raw_spawn.rs | |||
| @@ -8,7 +8,7 @@ use defmt::*; | |||
| 8 | use embassy_executor::raw::TaskStorage; | 8 | use embassy_executor::raw::TaskStorage; |
| 9 | use embassy_executor::Executor; | 9 | use embassy_executor::Executor; |
| 10 | use embassy_time::{Duration, Timer}; | 10 | use embassy_time::{Duration, Timer}; |
| 11 | use embassy_util::Forever; | 11 | use static_cell::StaticCell; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 12 | use {defmt_rtt as _, panic_probe as _}; |
| 13 | 13 | ||
| 14 | async fn run1() { | 14 | async fn run1() { |
| @@ -25,14 +25,14 @@ async fn run2() { | |||
| 25 | } | 25 | } |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | static EXECUTOR: Forever<Executor> = Forever::new(); | 28 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); |
| 29 | 29 | ||
| 30 | #[entry] | 30 | #[entry] |
| 31 | fn main() -> ! { | 31 | fn main() -> ! { |
| 32 | info!("Hello World!"); | 32 | info!("Hello World!"); |
| 33 | 33 | ||
| 34 | let _p = embassy_stm32::init(Default::default()); | 34 | let _p = embassy_stm32::init(Default::default()); |
| 35 | let executor = EXECUTOR.put(Executor::new()); | 35 | let executor = EXECUTOR.init(Executor::new()); |
| 36 | 36 | ||
| 37 | let run1_task = TaskStorage::new(); | 37 | let run1_task = TaskStorage::new(); |
| 38 | let run2_task = TaskStorage::new(); | 38 | let run2_task = TaskStorage::new(); |
diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml index 4d96d31fc..d8e78088a 100644 --- a/examples/stm32l5/Cargo.toml +++ b/examples/stm32l5/Cargo.toml | |||
| @@ -28,3 +28,4 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 28 | heapless = { version = "0.7.5", default-features = false } | 28 | heapless = { version = "0.7.5", default-features = false } |
| 29 | rand_core = { version = "0.6.3", default-features = false } | 29 | rand_core = { version = "0.6.3", default-features = false } |
| 30 | embedded-io = { version = "0.3.0", features = ["async"] } | 30 | embedded-io = { version = "0.3.0", features = ["async"] } |
| 31 | static_cell = "1.0" | ||
diff --git a/examples/stm32l5/src/bin/usb_ethernet.rs b/examples/stm32l5/src/bin/usb_ethernet.rs index 7c53d03cc..959195518 100644 --- a/examples/stm32l5/src/bin/usb_ethernet.rs +++ b/examples/stm32l5/src/bin/usb_ethernet.rs | |||
| @@ -19,18 +19,18 @@ use embassy_usb::{Builder, UsbDevice}; | |||
| 19 | use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; | 19 | use embassy_usb_ncm::{CdcNcmClass, Receiver, Sender, State}; |
| 20 | use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; | 20 | use embassy_util::blocking_mutex::raw::ThreadModeRawMutex; |
| 21 | use embassy_util::channel::mpmc::Channel; | 21 | use embassy_util::channel::mpmc::Channel; |
| 22 | use embassy_util::Forever; | ||
| 23 | use embedded_io::asynch::{Read, Write}; | 22 | use embedded_io::asynch::{Read, Write}; |
| 24 | use rand_core::RngCore; | 23 | use rand_core::RngCore; |
| 24 | use static_cell::StaticCell; | ||
| 25 | use {defmt_rtt as _, panic_probe as _}; | 25 | use {defmt_rtt as _, panic_probe as _}; |
| 26 | 26 | ||
| 27 | type MyDriver = Driver<'static, embassy_stm32::peripherals::USB>; | 27 | type MyDriver = Driver<'static, embassy_stm32::peripherals::USB>; |
| 28 | 28 | ||
| 29 | macro_rules! forever { | 29 | macro_rules! singleton { |
| 30 | ($val:expr) => {{ | 30 | ($val:expr) => {{ |
| 31 | type T = impl Sized; | 31 | type T = impl Sized; |
| 32 | static FOREVER: Forever<T> = Forever::new(); | 32 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 33 | FOREVER.put_with(move || $val) | 33 | STATIC_CELL.init_with(move || $val) |
| 34 | }}; | 34 | }}; |
| 35 | } | 35 | } |
| 36 | 36 | ||
| @@ -115,7 +115,7 @@ async fn main(spawner: Spawner) { | |||
| 115 | control_buf: [u8; 128], | 115 | control_buf: [u8; 128], |
| 116 | serial_state: State<'static>, | 116 | serial_state: State<'static>, |
| 117 | } | 117 | } |
| 118 | let res: &mut Resources = forever!(Resources { | 118 | let res: &mut Resources = singleton!(Resources { |
| 119 | device_descriptor: [0; 256], | 119 | device_descriptor: [0; 256], |
| 120 | config_descriptor: [0; 256], | 120 | config_descriptor: [0; 256], |
| 121 | bos_descriptor: [0; 256], | 121 | bos_descriptor: [0; 256], |
| @@ -171,10 +171,10 @@ async fn main(spawner: Spawner) { | |||
| 171 | 171 | ||
| 172 | // Init network stack | 172 | // Init network stack |
| 173 | let device = Device { mac_addr: our_mac_addr }; | 173 | let device = Device { mac_addr: our_mac_addr }; |
| 174 | let stack = &*forever!(Stack::new( | 174 | let stack = &*singleton!(Stack::new( |
| 175 | device, | 175 | device, |
| 176 | config, | 176 | config, |
| 177 | forever!(StackResources::<1, 2, 8>::new()), | 177 | singleton!(StackResources::<1, 2, 8>::new()), |
| 178 | seed | 178 | seed |
| 179 | )); | 179 | )); |
| 180 | 180 | ||
