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())); - }); -}