From d5ab02792c5489668bd399ac094900f5e26b3420 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 29 Mar 2021 02:47:10 +0200 Subject: [PATCH 1/2] nrf: add main macro support --- embassy-macros/Cargo.toml | 3 +- embassy-macros/src/chip/nrf.rs | 62 +++++++++++++++++++ embassy-macros/src/chip/stm32.rs | 51 +++++++++++++++ embassy-macros/src/lib.rs | 60 +++++------------- embassy-nrf-examples/src/bin/buffered_uart.rs | 36 ++--------- .../src/bin/executor_fairness_test.rs | 35 +++-------- .../src/bin/gpiote_channel.rs | 21 +------ embassy-nrf-examples/src/bin/gpiote_port.rs | 20 +----- embassy-nrf-examples/src/bin/ppi.rs | 25 ++------ embassy-nrf-examples/src/bin/qspi.rs | 20 +----- embassy-nrf-examples/src/bin/spim.rs | 38 ++---------- embassy-nrf-examples/src/bin/timer.rs | 31 ++-------- embassy-nrf-examples/src/bin/uart.rs | 35 ++--------- 13 files changed, 173 insertions(+), 264 deletions(-) create mode 100644 embassy-macros/src/chip/nrf.rs create mode 100644 embassy-macros/src/chip/stm32.rs diff --git a/embassy-macros/Cargo.toml b/embassy-macros/Cargo.toml index 295a5703..a3d2fd37 100644 --- a/embassy-macros/Cargo.toml +++ b/embassy-macros/Cargo.toml @@ -8,10 +8,11 @@ edition = "2018" syn = { version = "1.0.39", features = ["full", "extra-traits"] } quote = "1.0.7" darling = "0.10.2" +proc-macro2 = "1.0.24" [lib] proc-macro = true [features] stm32 = [] -nrf = [] \ No newline at end of file +nrf = [] diff --git a/embassy-macros/src/chip/nrf.rs b/embassy-macros/src/chip/nrf.rs new file mode 100644 index 00000000..d8922341 --- /dev/null +++ b/embassy-macros/src/chip/nrf.rs @@ -0,0 +1,62 @@ +use darling::FromMeta; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::spanned::Spanned; + +#[derive(Debug, FromMeta)] +pub enum HfclkSource { + Internal, + ExternalXtal, +} + +impl Default for HfclkSource { + fn default() -> Self { + Self::Internal + } +} + +#[derive(Debug, FromMeta)] +pub enum LfclkSource { + InternalRC, + Synthesized, + ExternalXtal, + ExternalLowSwing, + ExternalFullSwing, +} + +impl Default for LfclkSource { + fn default() -> Self { + Self::InternalRC + } +} + +#[derive(Debug, FromMeta)] +pub struct Args { + #[darling(default)] + pub hfclk_source: HfclkSource, + #[darling(default)] + pub lfclk_source: LfclkSource, +} + +pub fn generate(args: Args) -> TokenStream { + let hfclk_source = format_ident!("{}", format!("{:?}", args.hfclk_source)); + let lfclk_source = format_ident!("{}", format!("{:?}", args.lfclk_source)); + + quote!( + use embassy_nrf::{interrupt, peripherals, rtc}; + + let mut config = embassy_nrf::system::Config::default(); + config.hfclk_source = embassy_nrf::system::HfclkSource::#hfclk_source; + config.lfclk_source = embassy_nrf::system::LfclkSource::#lfclk_source; + unsafe { embassy_nrf::system::configure(config) }; + + let mut rtc = rtc::RTC::new(unsafe { ::steal() }, interrupt::take!(RTC1)); + let rtc = unsafe { make_static(&mut rtc) }; + rtc.start(); + let mut alarm = rtc.alarm0(); + + unsafe { embassy::time::set_clock(rtc) }; + + let alarm = unsafe { make_static(&mut alarm) }; + ) +} diff --git a/embassy-macros/src/chip/stm32.rs b/embassy-macros/src/chip/stm32.rs new file mode 100644 index 00000000..01c6a9cf --- /dev/null +++ b/embassy-macros/src/chip/stm32.rs @@ -0,0 +1,51 @@ +use darling::FromMeta; +use proc_macro2::TokenStream; +use quote::{format_ident, quote}; +use syn::spanned::Spanned; + +#[derive(Debug, FromMeta)] +pub struct Args { + #[darling(default)] + pub use_hse: Option, + #[darling(default)] + pub sysclk: Option, + #[darling(default)] + pub pclk1: Option, +} + +pub fn generate(args: Args) -> TokenStream { + let mut clock_cfg_args = quote! {}; + if args.use_hse.is_some() { + let mhz = args.use_hse.unwrap(); + clock_cfg_args = quote! { #clock_cfg_args.use_hse(#mhz.mhz()) }; + } + + if args.sysclk.is_some() { + let mhz = args.sysclk.unwrap(); + clock_cfg_args = quote! { #clock_cfg_args.sysclk(#mhz.mhz()) }; + } + + if args.pclk1.is_some() { + let mhz = args.pclk1.unwrap(); + clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) }; + } + + quote!( + use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext}; + + let dp = pac::Peripherals::take().unwrap(); + let rcc = dp.RCC.constrain(); + let clocks = rcc.cfgr#clock_cfg_args.freeze(); + + unsafe { Peripherals::set_peripherals(clocks) }; + + let mut rtc = rtc::RTC::new(dp.TIM3, interrupt::take!(TIM3), clocks); + let rtc = unsafe { make_static(&mut rtc) }; + rtc.start(); + let mut alarm = rtc.alarm1(); + + unsafe { embassy::time::set_clock(rtc) }; + + let alarm = unsafe { make_static(&mut alarm) }; + ) +} diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index a14c374f..26b49b2e 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs @@ -191,22 +191,21 @@ pub fn interrupt_take(item: TokenStream) -> TokenStream { result.into() } -#[derive(Debug, FromMeta)] -struct MainMacroArgs { - #[darling(default)] - use_hse: Option, - #[darling(default)] - sysclk: Option, - #[darling(default)] - pclk1: Option, -} +#[cfg(feature = "nrf")] +#[path = "chip/nrf.rs"] +mod chip; +#[cfg(feature = "stm32")] +#[path = "chip/stm32.rs"] +mod chip; + +#[cfg(any(feature = "nrf", feature = "stm32"))] #[proc_macro_attribute] pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { let macro_args = syn::parse_macro_input!(args as syn::AttributeArgs); let mut task_fn = syn::parse_macro_input!(item as syn::ItemFn); - let macro_args = match MainMacroArgs::from_list(¯o_args) { + let macro_args = match chip::Args::from_list(¯o_args) { Ok(v) => v, Err(e) => { return TokenStream::from(e.write_errors()); @@ -254,27 +253,9 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { let name = task_fn.sig.ident.clone(); let task_fn_body = task_fn.block.clone(); - let mut clock_cfg_args = quote! {}; - if macro_args.use_hse.is_some() { - let mhz = macro_args.use_hse.unwrap(); - clock_cfg_args = quote! { #clock_cfg_args.use_hse(#mhz.mhz()) }; - } - - if macro_args.sysclk.is_some() { - let mhz = macro_args.sysclk.unwrap(); - clock_cfg_args = quote! { #clock_cfg_args.sysclk(#mhz.mhz()) }; - } - - if macro_args.pclk1.is_some() { - let mhz = macro_args.pclk1.unwrap(); - clock_cfg_args = quote! { #clock_cfg_args.pclk1(#mhz.mhz()) }; - } + let chip_setup = chip::generate(macro_args); let result = quote! { - static __embassy_rtc: embassy::util::Forever> = embassy::util::Forever::new(); - static __embassy_alarm: embassy::util::Forever> = embassy::util::Forever::new(); - static __embassy_executor: embassy::util::Forever = embassy::util::Forever::new(); - #[embassy::executor::task] async fn __embassy_main(#args) { #task_fn_body @@ -282,27 +263,20 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { #[cortex_m_rt::entry] fn main() -> ! { - use embassy::executor::Executor; - use embassy_stm32::{rtc, interrupt, Peripherals, pac, hal::rcc::RccExt, hal::time::U32Ext}; + unsafe fn make_static(t: &mut T) -> &'static mut T { + ::core::mem::transmute(t) + } - let dp = pac::Peripherals::take().unwrap(); - let rcc = dp.RCC.constrain(); - let clocks = rcc.cfgr#clock_cfg_args.freeze(); + #chip_setup - unsafe { Peripherals::set_peripherals(clocks) }; - - let rtc = __embassy_rtc.put(rtc::RTC::new(dp.TIM3, interrupt::take!(TIM3), clocks)); - rtc.start(); - - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = __embassy_alarm.put(rtc.alarm1()); - let executor = __embassy_executor.put(Executor::new()); + let mut executor = ::embassy::executor::Executor::new(); + let executor = unsafe { make_static(&mut executor) }; executor.set_alarm(alarm); executor.run(|spawner| { spawner.spawn(__embassy_main(spawner)).unwrap(); }) + } }; result.into() diff --git a/embassy-nrf-examples/src/bin/buffered_uart.rs b/embassy-nrf-examples/src/bin/buffered_uart.rs index c7c82e84..8f5d9fb2 100644 --- a/embassy-nrf-examples/src/bin/buffered_uart.rs +++ b/embassy-nrf-examples/src/bin/buffered_uart.rs @@ -8,19 +8,17 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::Spawner; use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; -use embassy::util::{Forever, Steal}; use embassy_nrf::gpio::NoPin; -use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, peripherals, rtc, uarte, Peripherals}; +use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; use example_common::*; use futures::pin_mut; -#[task] -async fn run() { - let p = unsafe { Peripherals::steal() }; +#[embassy::main] +async fn main(spawner: Spawner) { + let p = Peripherals::take().unwrap(); let mut config = uarte::Config::default(); config.parity = uarte::Parity::EXCLUDED; @@ -70,27 +68,3 @@ async fn run() { info!("write done"); } } - -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::Peripherals::take()); - - unsafe { embassy_nrf::system::configure(Default::default()) }; - let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); - rtc.start(); - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm0()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); - - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/executor_fairness_test.rs b/embassy-nrf-examples/src/bin/executor_fairness_test.rs index 67610ef0..e82e5475 100644 --- a/embassy-nrf-examples/src/bin/executor_fairness_test.rs +++ b/embassy-nrf-examples/src/bin/executor_fairness_test.rs @@ -10,13 +10,10 @@ mod example_common; use example_common::*; use core::task::Poll; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::{task, Spawner}; use embassy::time::{Duration, Instant, Timer}; -use embassy::util::Forever; -use embassy_nrf::peripherals; -use embassy_nrf::{interrupt, rtc}; +use embassy_nrf::interrupt; #[task] async fn run1() { @@ -42,27 +39,9 @@ async fn run3() { .await; } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::Peripherals::take()); - - unsafe { embassy_nrf::system::configure(Default::default()) }; - let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); - rtc.start(); - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm0()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); - executor.run(|spawner| { - unwrap!(spawner.spawn(run1())); - unwrap!(spawner.spawn(run2())); - unwrap!(spawner.spawn(run3())); - }); +#[embassy::main] +async fn main(spawner: Spawner) { + unwrap!(spawner.spawn(run1())); + unwrap!(spawner.spawn(run2())); + unwrap!(spawner.spawn(run3())); } diff --git a/embassy-nrf-examples/src/bin/gpiote_channel.rs b/embassy-nrf-examples/src/bin/gpiote_channel.rs index d223df7a..9e653192 100644 --- a/embassy-nrf-examples/src/bin/gpiote_channel.rs +++ b/embassy-nrf-examples/src/bin/gpiote_channel.rs @@ -9,17 +9,14 @@ mod example_common; use example_common::*; -use cortex_m_rt::entry; use defmt::panic; - -use embassy::executor::{task, Executor}; -use embassy::util::Forever; +use embassy::executor::Spawner; use embassy_nrf::gpio::{Input, Pull}; use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity}; use embassy_nrf::{interrupt, Peripherals}; -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { let p = Peripherals::take().unwrap(); let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE)); @@ -80,15 +77,3 @@ async fn run() { futures::join!(button1, button2, button3, button4); } - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let executor = EXECUTOR.put(Executor::new()); - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs index 199a33da..204a307c 100644 --- a/embassy-nrf-examples/src/bin/gpiote_port.rs +++ b/embassy-nrf-examples/src/bin/gpiote_port.rs @@ -9,11 +9,9 @@ mod example_common; use core::pin::Pin; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::Spawner; use embassy::traits::gpio::{WaitForHigh, WaitForLow}; -use embassy::util::Forever; use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull}; use embassy_nrf::gpiote::{self, PortInput}; use embassy_nrf::interrupt; @@ -29,8 +27,8 @@ async fn button(n: usize, mut pin: PortInput<'static, AnyPin>) { } } -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { let p = Peripherals::take().unwrap(); let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE)); @@ -53,15 +51,3 @@ async fn run() { ); futures::join!(button1, button2, button3, button4); } - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let executor = EXECUTOR.put(Executor::new()); - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/ppi.rs b/embassy-nrf-examples/src/bin/ppi.rs index 382c18f8..aeda76f2 100644 --- a/embassy-nrf-examples/src/bin/ppi.rs +++ b/embassy-nrf-examples/src/bin/ppi.rs @@ -7,23 +7,19 @@ #[path = "../example_common.rs"] mod example_common; -use core::future::pending; - use example_common::*; -use cortex_m_rt::entry; +use core::future::pending; use defmt::panic; - -use embassy::executor::{task, Executor}; -use embassy::util::Forever; +use embassy::executor::Spawner; use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pull}; use embassy_nrf::gpiote::{self, InputChannel, InputChannelPolarity}; use embassy_nrf::ppi::Ppi; use embassy_nrf::{interrupt, Peripherals}; use gpiote::{OutputChannel, OutputChannelPolarity}; -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { let p = Peripherals::take().unwrap(); let g = gpiote::initialize(p.GPIOTE, interrupt::take!(GPIOTE)); @@ -94,18 +90,7 @@ async fn run() { info!("Press button 2 to turn on LED 1"); info!("Press button 3 to turn off LED 1"); info!("Press button 4 to toggle LEDs 1 and 2"); + // Block forever so the above drivers don't get dropped pending::<()>().await; } - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let executor = EXECUTOR.put(Executor::new()); - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/qspi.rs b/embassy-nrf-examples/src/bin/qspi.rs index 2005b997..27dd28d7 100644 --- a/embassy-nrf-examples/src/bin/qspi.rs +++ b/embassy-nrf-examples/src/bin/qspi.rs @@ -8,11 +8,9 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; use defmt::{assert_eq, panic}; -use embassy::executor::{task, Executor}; +use embassy::executor::Spawner; use embassy::traits::flash::Flash; -use embassy::util::Forever; use embassy_nrf::Peripherals; use embassy_nrf::{interrupt, qspi}; use example_common::*; @@ -25,8 +23,8 @@ const PAGE_SIZE: usize = 4096; #[repr(C, align(4))] struct AlignedBuf([u8; 4096]); -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { let p = Peripherals::take().unwrap(); let csn = p.P0_17; @@ -96,15 +94,3 @@ async fn run() { info!("done!") } - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let executor = EXECUTOR.put(Executor::new()); - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/spim.rs b/embassy-nrf-examples/src/bin/spim.rs index 5e2340ff..6d0fa93d 100644 --- a/embassy-nrf-examples/src/bin/spim.rs +++ b/embassy-nrf-examples/src/bin/spim.rs @@ -8,21 +8,19 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; -use embassy::util::Forever; +use embassy::executor::Spawner; use embassy::util::Steal; use embassy_nrf::gpio::{Level, Output, OutputDrive}; -use embassy_nrf::{interrupt, rtc, spim}; -use embassy_nrf::{peripherals, Peripherals}; +use embassy_nrf::Peripherals; +use embassy_nrf::{interrupt, spim}; use embassy_traits::spi::FullDuplex; use embedded_hal::digital::v2::*; use example_common::*; use futures::pin_mut; -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { info!("running!"); let p = unsafe { Peripherals::steal() }; @@ -84,29 +82,3 @@ async fn run() { info!("erevid: {=[?]}", rx); } - -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::Peripherals::take()); - - unsafe { embassy_nrf::system::configure(Default::default()) }; - let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); - rtc.start(); - unsafe { embassy::time::set_clock(rtc) }; - - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm0()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); - - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} diff --git a/embassy-nrf-examples/src/bin/timer.rs b/embassy-nrf-examples/src/bin/timer.rs index 47d5d677..6116aa60 100644 --- a/embassy-nrf-examples/src/bin/timer.rs +++ b/embassy-nrf-examples/src/bin/timer.rs @@ -9,12 +9,9 @@ mod example_common; use example_common::*; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::{task, Spawner}; use embassy::time::{Duration, Timer}; -use embassy::util::Forever; -use embassy_nrf::{interrupt, peripherals, rtc}; #[task] async fn run1() { @@ -32,26 +29,8 @@ async fn run2() { } } -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::Peripherals::take()); - - unsafe { embassy_nrf::system::configure(Default::default()) }; - let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); - rtc.start(); - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm0()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); - executor.run(|spawner| { - unwrap!(spawner.spawn(run1())); - unwrap!(spawner.spawn(run2())); - }); +#[embassy::main] +async fn main(spawner: Spawner) { + unwrap!(spawner.spawn(run1())); + unwrap!(spawner.spawn(run2())); } diff --git a/embassy-nrf-examples/src/bin/uart.rs b/embassy-nrf-examples/src/bin/uart.rs index 9bbcb3d4..b3d32814 100644 --- a/embassy-nrf-examples/src/bin/uart.rs +++ b/embassy-nrf-examples/src/bin/uart.rs @@ -9,17 +9,16 @@ mod example_common; use example_common::*; -use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::Spawner; use embassy::traits::uart::{Read, Write}; -use embassy::util::{Forever, Steal}; +use embassy::util::Steal; use embassy_nrf::gpio::NoPin; -use embassy_nrf::{interrupt, peripherals, rtc, uarte, Peripherals}; +use embassy_nrf::{interrupt, uarte, Peripherals}; use futures::pin_mut; -#[task] -async fn run() { +#[embassy::main] +async fn main(spawner: Spawner) { let p = unsafe { Peripherals::steal() }; let mut config = uarte::Config::default(); @@ -74,27 +73,3 @@ async fn run() { */ } } - -static RTC: Forever> = Forever::new(); -static ALARM: Forever> = Forever::new(); -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { - info!("Hello World!"); - - let p = unwrap!(embassy_nrf::Peripherals::take()); - - unsafe { embassy_nrf::system::configure(Default::default()) }; - let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); - rtc.start(); - unsafe { embassy::time::set_clock(rtc) }; - - let alarm = ALARM.put(rtc.alarm0()); - let executor = EXECUTOR.put(Executor::new()); - executor.set_alarm(alarm); - - executor.run(|spawner| { - unwrap!(spawner.spawn(run())); - }); -} From d7c3a38efceacbd5a326c84c6e0bbef44b06e97c Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 29 Mar 2021 03:00:48 +0200 Subject: [PATCH 2/2] Reexport macros at root, use eveywhere as `#[embassy::task]` --- embassy-macros/src/lib.rs | 2 +- embassy-nrf-examples/src/bin/executor_fairness_test.rs | 8 ++++---- embassy-nrf-examples/src/bin/multiprio.rs | 8 ++++---- embassy-nrf-examples/src/bin/timer.rs | 6 +++--- embassy-std-examples/src/bin/serial.rs | 3 +-- embassy-std-examples/src/bin/tick.rs | 3 +-- embassy-stm32f4-examples/src/bin/can.rs | 4 ++-- embassy-stm32f4-examples/src/bin/exti.rs | 4 ++-- embassy-stm32f4-examples/src/bin/rtc_async.rs | 5 ++--- embassy-stm32f4-examples/src/bin/serial.rs | 4 ++-- embassy-stm32f4-examples/src/bin/usb_serial.rs | 4 ++-- embassy/src/executor/mod.rs | 2 -- embassy/src/executor/timer.rs | 6 +++--- embassy/src/lib.rs | 2 +- embassy/src/util/signal.rs | 2 +- 15 files changed, 29 insertions(+), 34 deletions(-) diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index 26b49b2e..145714f2 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs @@ -256,7 +256,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { let chip_setup = chip::generate(macro_args); let result = quote! { - #[embassy::executor::task] + #[embassy::task] async fn __embassy_main(#args) { #task_fn_body } diff --git a/embassy-nrf-examples/src/bin/executor_fairness_test.rs b/embassy-nrf-examples/src/bin/executor_fairness_test.rs index e82e5475..3ea74316 100644 --- a/embassy-nrf-examples/src/bin/executor_fairness_test.rs +++ b/embassy-nrf-examples/src/bin/executor_fairness_test.rs @@ -11,11 +11,11 @@ use example_common::*; use core::task::Poll; use defmt::panic; -use embassy::executor::{task, Spawner}; +use embassy::executor::Spawner; use embassy::time::{Duration, Instant, Timer}; use embassy_nrf::interrupt; -#[task] +#[embassy::task] async fn run1() { loop { info!("DING DONG"); @@ -23,14 +23,14 @@ async fn run1() { } } -#[task] +#[embassy::task] async fn run2() { loop { Timer::at(Instant::from_ticks(0)).await; } } -#[task] +#[embassy::task] async fn run3() { futures::future::poll_fn(|cx| { cx.waker().wake_by_ref(); diff --git a/embassy-nrf-examples/src/bin/multiprio.rs b/embassy-nrf-examples/src/bin/multiprio.rs index aa2824f6..c6228a1e 100644 --- a/embassy-nrf-examples/src/bin/multiprio.rs +++ b/embassy-nrf-examples/src/bin/multiprio.rs @@ -66,13 +66,13 @@ use example_common::*; use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor, InterruptExecutor}; +use embassy::executor::{Executor, InterruptExecutor}; use embassy::interrupt::InterruptExt; use embassy::time::{Duration, Instant, Timer}; use embassy::util::Forever; use embassy_nrf::{interrupt, peripherals, rtc}; -#[task] +#[embassy::task] async fn run_high() { loop { info!(" [high] tick!"); @@ -80,7 +80,7 @@ async fn run_high() { } } -#[task] +#[embassy::task] async fn run_med() { loop { let start = Instant::now(); @@ -97,7 +97,7 @@ async fn run_med() { } } -#[task] +#[embassy::task] async fn run_low() { loop { let start = Instant::now(); diff --git a/embassy-nrf-examples/src/bin/timer.rs b/embassy-nrf-examples/src/bin/timer.rs index 6116aa60..a2b717bc 100644 --- a/embassy-nrf-examples/src/bin/timer.rs +++ b/embassy-nrf-examples/src/bin/timer.rs @@ -10,10 +10,10 @@ mod example_common; use example_common::*; use defmt::panic; -use embassy::executor::{task, Spawner}; +use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; -#[task] +#[embassy::task] async fn run1() { loop { info!("BIG INFREQUENT TICK"); @@ -21,7 +21,7 @@ async fn run1() { } } -#[task] +#[embassy::task] async fn run2() { loop { info!("tick"); diff --git a/embassy-std-examples/src/bin/serial.rs b/embassy-std-examples/src/bin/serial.rs index 6d628fe1..79f10c41 100644 --- a/embassy-std-examples/src/bin/serial.rs +++ b/embassy-std-examples/src/bin/serial.rs @@ -6,7 +6,6 @@ mod serial_port; use async_io::Async; -use embassy::executor::task; use embassy::io::AsyncBufReadExt; use embassy::util::Forever; use embassy_std::Executor; @@ -15,7 +14,7 @@ use nix::sys::termios; use self::serial_port::SerialPort; -#[task] +#[embassy::task] async fn run() { // Open the serial port. let baudrate = termios::BaudRate::B115200; diff --git a/embassy-std-examples/src/bin/tick.rs b/embassy-std-examples/src/bin/tick.rs index 96eef067..47713f7b 100644 --- a/embassy-std-examples/src/bin/tick.rs +++ b/embassy-std-examples/src/bin/tick.rs @@ -2,13 +2,12 @@ #![feature(impl_trait_in_bindings)] #![feature(type_alias_impl_trait)] -use embassy::executor::task; use embassy::time::{Duration, Timer}; use embassy::util::Forever; use embassy_std::Executor; use log::*; -#[task] +#[embassy::task] async fn run() { loop { info!("tick"); diff --git a/embassy-stm32f4-examples/src/bin/can.rs b/embassy-stm32f4-examples/src/bin/can.rs index 5ffde247..c34c0a2a 100644 --- a/embassy-stm32f4-examples/src/bin/can.rs +++ b/embassy-stm32f4-examples/src/bin/can.rs @@ -11,13 +11,13 @@ use example_common::{panic, *}; use bxcan::filter::Mask32; use cortex_m_rt::entry; -use embassy::executor::{task, Executor}; +use embassy::executor::Executor; use embassy::util::Forever; use embassy_stm32f4::{can, interrupt}; use stm32f4xx_hal::prelude::*; use stm32f4xx_hal::{can::Can, stm32}; -#[task] +#[embassy::task] async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { let gpioa = dp.GPIOA.split(); diff --git a/embassy-stm32f4-examples/src/bin/exti.rs b/embassy-stm32f4-examples/src/bin/exti.rs index 9b2535b1..9879de40 100644 --- a/embassy-stm32f4-examples/src/bin/exti.rs +++ b/embassy-stm32f4-examples/src/bin/exti.rs @@ -10,7 +10,7 @@ mod example_common; use example_common::{panic, *}; use cortex_m_rt::entry; -use embassy::executor::{task, Executor}; +use embassy::executor::Executor; use embassy::traits::gpio::*; use embassy::util::Forever; use embassy_stm32f4::exti::ExtiPin; @@ -19,7 +19,7 @@ use futures::pin_mut; use stm32f4xx_hal::prelude::*; use stm32f4xx_hal::stm32; -#[task] +#[embassy::task] async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { let gpioa = dp.GPIOA.split(); diff --git a/embassy-stm32f4-examples/src/bin/rtc_async.rs b/embassy-stm32f4-examples/src/bin/rtc_async.rs index 1927d01b..f2757294 100644 --- a/embassy-stm32f4-examples/src/bin/rtc_async.rs +++ b/embassy-stm32f4-examples/src/bin/rtc_async.rs @@ -11,12 +11,11 @@ use example_common::*; use defmt::panic; use embassy; use embassy::executor::Spawner; -use embassy::task; use embassy::time::{Duration, Timer}; use embassy_stm32f4; use embassy_stm32f4::hal; -#[task] +#[embassy::task] async fn run1() { loop { info!("BIG INFREQUENT TICK"); @@ -24,7 +23,7 @@ async fn run1() { } } -#[task] +#[embassy::task] async fn run2() { loop { info!("tick"); diff --git a/embassy-stm32f4-examples/src/bin/serial.rs b/embassy-stm32f4-examples/src/bin/serial.rs index c8e7f288..925fd68f 100644 --- a/embassy-stm32f4-examples/src/bin/serial.rs +++ b/embassy-stm32f4-examples/src/bin/serial.rs @@ -11,7 +11,7 @@ use example_common::{panic, *}; use cortex_m::singleton; use cortex_m_rt::entry; -use embassy::executor::{task, Executor}; +use embassy::executor::Executor; use embassy::traits::uart::{Read, Write}; use embassy::util::Forever; use embassy_stm32f4::interrupt; @@ -22,7 +22,7 @@ use stm32f4xx_hal::prelude::*; use stm32f4xx_hal::serial::config::Config; use stm32f4xx_hal::stm32; -#[task] +#[embassy::task] async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { dp.DBGMCU.cr.modify(|_, w| { w.dbg_sleep().set_bit(); diff --git a/embassy-stm32f4-examples/src/bin/usb_serial.rs b/embassy-stm32f4-examples/src/bin/usb_serial.rs index f1c4631d..6d3b2cc1 100644 --- a/embassy-stm32f4-examples/src/bin/usb_serial.rs +++ b/embassy-stm32f4-examples/src/bin/usb_serial.rs @@ -10,7 +10,7 @@ use example_common::*; use cortex_m_rt::entry; use defmt::panic; -use embassy::executor::{task, Executor}; +use embassy::executor::Executor; use embassy::interrupt::InterruptExt; use embassy::io::{AsyncBufReadExt, AsyncWriteExt}; use embassy::time::{Duration, Timer}; @@ -25,7 +25,7 @@ use stm32f4xx_hal::prelude::*; use usb_device::bus::UsbBusAllocator; use usb_device::prelude::*; -#[task] +#[embassy::task] async fn run1(bus: &'static mut UsbBusAllocator>) { info!("Async task"); diff --git a/embassy/src/executor/mod.rs b/embassy/src/executor/mod.rs index b2c42190..8a3a8991 100644 --- a/embassy/src/executor/mod.rs +++ b/embassy/src/executor/mod.rs @@ -1,5 +1,3 @@ -pub use embassy_macros::{main, task}; - use core::marker::PhantomData; use core::ptr::NonNull; use core::{mem, ptr}; diff --git a/embassy/src/executor/timer.rs b/embassy/src/executor/timer.rs index 541f96f5..774613eb 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/executor/timer.rs @@ -60,7 +60,7 @@ impl Timer { /// use embassy::executor::task; /// use embassy::time::{Duration, Timer}; /// - /// #[task] + /// #[embassy::task] /// async fn demo_sleep_seconds() { /// // suspend this task for one second. /// Timer::after(Duration::from_secs(1)).await; @@ -104,7 +104,7 @@ impl Future for Timer { /// use embassy::time::{Duration, Timer}; /// # fn foo() {} /// -/// #[task] +/// #[embassy::task] /// async fn ticker_example_0() { /// loop { /// foo(); @@ -129,7 +129,7 @@ impl Future for Timer { /// use futures::StreamExt; /// # fn foo(){} /// -/// #[task] +/// #[embassy::task] /// async fn ticker_example_1() { /// let mut ticker = Ticker::every(Duration::from_secs(1)); /// loop { diff --git a/embassy/src/lib.rs b/embassy/src/lib.rs index 482d5b36..af05a794 100644 --- a/embassy/src/lib.rs +++ b/embassy/src/lib.rs @@ -17,8 +17,8 @@ pub mod io; pub mod time; pub mod util; +pub use embassy_macros::*; pub use embassy_traits as traits; -pub use executor::{main, task}; #[doc(hidden)] /// Implementation details for embassy macros. DO NOT USE. diff --git a/embassy/src/util/signal.rs b/embassy/src/util/signal.rs index 749b3224..e4629bf9 100644 --- a/embassy/src/util/signal.rs +++ b/embassy/src/util/signal.rs @@ -122,7 +122,7 @@ unsafe impl cortex_m::interrupt::Nr for NrWrap { /// use embassy::util::InterruptFuture; /// use embassy::executor::task; /// use embassy_stm32f4::interrupt; // Adjust this to your MCU's embassy HAL. -/// #[task] +/// #[embassy::task] /// async fn demo_interrupt_future() { /// // Using STM32f446 interrupt names, adjust this to your application as necessary. /// // Wait for TIM2 to tick.