diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 7c44b07a..8eb7a5ff 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -26,5 +26,5 @@ panic-probe = "0.1.0" nrf52840-hal = { version = "0.11.0" } embassy = { version = "0.1.0", path = "../embassy" } embassy-nrf = { version = "0.1.0", path = "../embassy-nrf", features = ["defmt-trace", "52840"] } -futures = { version = "0.3.5", default-features = false } +futures = { version = "0.3.7", default-features = false, features = ["async-await"] } cortex-m-rtic = { git = "https://github.com/rtic-rs/cortex-m-rtic", branch = "master"} \ No newline at end of file diff --git a/examples/src/bin/gpiote.rs b/examples/src/bin/gpiote.rs index 5a6ae933..16b4f06d 100644 --- a/examples/src/bin/gpiote.rs +++ b/examples/src/bin/gpiote.rs @@ -6,15 +6,12 @@ mod example_common; use example_common::*; -use core::pin::Pin; use cortex_m_rt::entry; -use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; -use embassy_nrf::gpiote; -use futures::pin_mut; use nrf52840_hal::gpio; use embassy::executor::{task, Executor}; -static EXECUTOR: Executor = Executor::new(|| cortex_m::asm::sev()); +use embassy::util::Forever; +use embassy_nrf::gpiote; #[task] async fn run() { @@ -28,7 +25,7 @@ async fn run() { let pin1 = port0.p0_11.into_pullup_input().degrade(); let button1 = async { let ch = g - .new_input_channel(&pin1, gpiote::EventPolarity::HiToLo) + .new_input_channel(pin1, gpiote::EventPolarity::HiToLo) .dewrap(); loop { @@ -40,7 +37,7 @@ async fn run() { let pin2 = port0.p0_12.into_pullup_input().degrade(); let button2 = async { let ch = g - .new_input_channel(&pin2, gpiote::EventPolarity::LoToHi) + .new_input_channel(pin2, gpiote::EventPolarity::LoToHi) .dewrap(); loop { @@ -52,7 +49,7 @@ async fn run() { let pin3 = port0.p0_24.into_pullup_input().degrade(); let button3 = async { let ch = g - .new_input_channel(&pin3, gpiote::EventPolarity::Toggle) + .new_input_channel(pin3, gpiote::EventPolarity::Toggle) .dewrap(); loop { @@ -64,7 +61,7 @@ async fn run() { let pin4 = port0.p0_25.into_pullup_input().degrade(); let button4 = async { let ch = g - .new_input_channel(&pin4, gpiote::EventPolarity::Toggle) + .new_input_channel(pin4, gpiote::EventPolarity::Toggle) .dewrap(); loop { @@ -76,16 +73,17 @@ async fn run() { futures::join!(button1, button2, button3, button4); } +static EXECUTOR: Forever = Forever::new(); + #[entry] fn main() -> ! { info!("Hello World!"); - unsafe { - EXECUTOR.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(run()).dewrap(); - loop { - EXECUTOR.run(); - cortex_m::asm::wfe(); - } + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/multiprio.rs b/examples/src/bin/multiprio.rs index ed170c58..30a5e85e 100644 --- a/examples/src/bin/multiprio.rs +++ b/examples/src/bin/multiprio.rs @@ -86,8 +86,8 @@ async fn run_med() { cortex_m::asm::delay(32_000_000); // ~1 second let end = Instant::now(); - let ms = end.duration_since(start).into_ticks() / 33; - info!(" [med] done in {:u32} ms", ms); + let ms = end.duration_since(start).as_ticks() / 33; + info!(" [med] done in {:u64} ms", ms); Timer::after(Duration::from_ticks(23421)).await; } @@ -103,8 +103,8 @@ async fn run_low() { cortex_m::asm::delay(64_000_000); // ~2 seconds let end = Instant::now(); - let ms = end.duration_since(start).into_ticks() / 33; - info!("[low] done in {:u32} ms", ms); + let ms = end.duration_since(start).as_ticks() / 33; + info!("[low] done in {:u64} ms", ms); Timer::after(Duration::from_ticks(32983)).await; } diff --git a/examples/src/bin/qspi.rs b/examples/src/bin/qspi.rs index c60a666e..caabac8b 100644 --- a/examples/src/bin/qspi.rs +++ b/examples/src/bin/qspi.rs @@ -7,12 +7,12 @@ mod example_common; use example_common::*; use cortex_m_rt::entry; -use embassy::flash::Flash; -use embassy_nrf::qspi; use nrf52840_hal::gpio; use embassy::executor::{task, Executor}; -static EXECUTOR: Executor = Executor::new(|| cortex_m::asm::sev()); +use embassy::flash::Flash; +use embassy::util::Forever; +use embassy_nrf::qspi; const PAGE_SIZE: usize = 4096; @@ -115,16 +115,17 @@ async fn run() { info!("done!") } +static EXECUTOR: Forever = Forever::new(); + #[entry] fn main() -> ! { info!("Hello World!"); - unsafe { - EXECUTOR.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(run()).dewrap(); - loop { - EXECUTOR.run(); - cortex_m::asm::wfe(); - } + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/rtc_async.rs b/examples/src/bin/rtc_async.rs index 30b181a9..5126a2cc 100644 --- a/examples/src/bin/rtc_async.rs +++ b/examples/src/bin/rtc_async.rs @@ -8,11 +8,13 @@ use example_common::*; use core::mem::MaybeUninit; use cortex_m_rt::entry; -use embassy::executor::{task, Executor}; +use nrf52840_hal::clocks; + +use embassy::executor::{task, TimerExecutor}; use embassy::time::{Clock, Duration, Timer}; +use embassy::util::Forever; use embassy_nrf::pac; use embassy_nrf::rtc; -use nrf52840_hal::clocks; #[task] async fn run1() { @@ -30,8 +32,8 @@ async fn run2() { } } -static mut RTC: MaybeUninit> = MaybeUninit::uninit(); -static mut EXECUTOR: MaybeUninit>> = MaybeUninit::uninit(); +static RTC: Forever> = Forever::new(); +static EXECUTOR: Forever>> = Forever::new(); #[entry] fn main() -> ! { @@ -44,28 +46,18 @@ fn main() -> ! { .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) .start_lfclk(); - let rtc: &'static _ = unsafe { - let ptr = RTC.as_mut_ptr(); - ptr.write(rtc::RTC::new(p.RTC1)); - &*ptr - }; - + let rtc = RTC.put(rtc::RTC::new(p.RTC1)); rtc.start(); + unsafe { embassy::time::set_clock(rtc) }; - let executor: &'static _ = unsafe { - let ptr = EXECUTOR.as_mut_ptr(); - ptr.write(Executor::new(rtc.alarm0(), cortex_m::asm::sev)); - &*ptr - }; + let executor = EXECUTOR.put(TimerExecutor::new(rtc.alarm0(), cortex_m::asm::sev)); - unsafe { - executor.spawn(run1()).dewrap(); - executor.spawn(run2()).dewrap(); + executor.spawn(run1()).dewrap(); + executor.spawn(run2()).dewrap(); - loop { - executor.run(); - cortex_m::asm::wfe(); - } + loop { + executor.run(); + cortex_m::asm::wfe(); } } diff --git a/examples/src/bin/uart.rs b/examples/src/bin/uart.rs index 0eec2cd8..eeaf5fee 100644 --- a/examples/src/bin/uart.rs +++ b/examples/src/bin/uart.rs @@ -7,13 +7,13 @@ mod example_common; use example_common::*; use cortex_m_rt::entry; -use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; -use embassy_nrf::uarte; use futures::pin_mut; use nrf52840_hal::gpio; use embassy::executor::{task, Executor}; -static EXECUTOR: Executor = Executor::new(|| cortex_m::asm::sev()); +use embassy::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt}; +use embassy::util::Forever; +use embassy_nrf::uarte; #[task] async fn run() { @@ -64,16 +64,17 @@ async fn run() { } } +static EXECUTOR: Forever = Forever::new(); + #[entry] fn main() -> ! { info!("Hello World!"); - unsafe { - EXECUTOR.spawn(run()).dewrap(); + let executor = EXECUTOR.put(Executor::new(cortex_m::asm::wfi)); + executor.spawn(run()).dewrap(); - loop { - EXECUTOR.run(); - cortex_m::asm::wfe(); - } + loop { + executor.run(); + cortex_m::asm::wfe(); } }