diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-08-17 18:49:55 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-08-17 22:16:46 +0200 |
| commit | fc6e1e06b305d302d1b7ad17e8ef3a9be986c358 (patch) | |
| tree | 545ad829660f6053d29a01da286d03ec3d49f5ca /embassy-macros | |
| parent | d881f3ad9186cf3279aa1ba27093bad94035c186 (diff) | |
Remove HAL initialization from #[embassy::main] macro.
Diffstat (limited to 'embassy-macros')
| -rw-r--r-- | embassy-macros/Cargo.toml | 3 | ||||
| -rw-r--r-- | embassy-macros/src/macros/main.rs | 62 |
2 files changed, 13 insertions, 52 deletions
diff --git a/embassy-macros/Cargo.toml b/embassy-macros/Cargo.toml index 6abafa581..19a3e9de2 100644 --- a/embassy-macros/Cargo.toml +++ b/embassy-macros/Cargo.toml | |||
| @@ -13,8 +13,5 @@ proc-macro2 = "1.0.29" | |||
| 13 | proc-macro = true | 13 | proc-macro = true |
| 14 | 14 | ||
| 15 | [features] | 15 | [features] |
| 16 | nrf = [] | ||
| 17 | stm32 = [] | ||
| 18 | rp = [] | ||
| 19 | std = [] | 16 | std = [] |
| 20 | wasm = [] | 17 | wasm = [] |
diff --git a/embassy-macros/src/macros/main.rs b/embassy-macros/src/macros/main.rs index b040edc5a..a0cb0f0b3 100644 --- a/embassy-macros/src/macros/main.rs +++ b/embassy-macros/src/macros/main.rs | |||
| @@ -5,11 +5,7 @@ use quote::quote; | |||
| 5 | use crate::util::ctxt::Ctxt; | 5 | use crate::util::ctxt::Ctxt; |
| 6 | 6 | ||
| 7 | #[derive(Debug, FromMeta)] | 7 | #[derive(Debug, FromMeta)] |
| 8 | struct Args { | 8 | struct Args {} |
| 9 | #[allow(unused)] | ||
| 10 | #[darling(default)] | ||
| 11 | config: Option<syn::LitStr>, | ||
| 12 | } | ||
| 13 | 9 | ||
| 14 | pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> { | 10 | pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> { |
| 15 | #[allow(unused_variables)] | 11 | #[allow(unused_variables)] |
| @@ -20,26 +16,14 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 20 | let ctxt = Ctxt::new(); | 16 | let ctxt = Ctxt::new(); |
| 21 | 17 | ||
| 22 | if f.sig.asyncness.is_none() { | 18 | if f.sig.asyncness.is_none() { |
| 23 | ctxt.error_spanned_by(&f.sig, "task functions must be async"); | 19 | ctxt.error_spanned_by(&f.sig, "main function must be async"); |
| 24 | } | 20 | } |
| 25 | if !f.sig.generics.params.is_empty() { | 21 | if !f.sig.generics.params.is_empty() { |
| 26 | ctxt.error_spanned_by(&f.sig, "task functions must not be generic"); | 22 | ctxt.error_spanned_by(&f.sig, "main function must not be generic"); |
| 27 | } | 23 | } |
| 28 | 24 | ||
| 29 | #[cfg(feature = "stm32")] | 25 | if fargs.len() != 1 { |
| 30 | let hal = Some(quote!(::embassy_stm32)); | 26 | ctxt.error_spanned_by(&f.sig, "main function must have 1 argument: the spawner."); |
| 31 | #[cfg(feature = "nrf")] | ||
| 32 | let hal = Some(quote!(::embassy_nrf)); | ||
| 33 | #[cfg(feature = "rp")] | ||
| 34 | let hal = Some(quote!(::embassy_rp)); | ||
| 35 | #[cfg(not(any(feature = "stm32", feature = "nrf", feature = "rp")))] | ||
| 36 | let hal: Option<TokenStream> = None; | ||
| 37 | |||
| 38 | if hal.is_some() && fargs.len() != 2 { | ||
| 39 | ctxt.error_spanned_by(&f.sig, "main function must have 2 arguments"); | ||
| 40 | } | ||
| 41 | if hal.is_none() && fargs.len() != 1 { | ||
| 42 | ctxt.error_spanned_by(&f.sig, "main function must have 1 argument"); | ||
| 43 | } | 27 | } |
| 44 | 28 | ||
| 45 | ctxt.check()?; | 29 | ctxt.check()?; |
| @@ -74,35 +58,15 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 74 | }; | 58 | }; |
| 75 | 59 | ||
| 76 | #[cfg(all(not(feature = "std"), not(feature = "wasm")))] | 60 | #[cfg(all(not(feature = "std"), not(feature = "wasm")))] |
| 77 | let main = { | 61 | let main = quote! { |
| 78 | let config = args.config.map(|s| s.parse::<syn::Expr>().unwrap()).unwrap_or_else(|| { | 62 | #[cortex_m_rt::entry] |
| 79 | syn::Expr::Verbatim(quote! { | 63 | fn main() -> ! { |
| 80 | Default::default() | 64 | let mut executor = ::embassy_executor::executor::Executor::new(); |
| 65 | let executor = unsafe { __make_static(&mut executor) }; | ||
| 66 | |||
| 67 | executor.run(|spawner| { | ||
| 68 | spawner.must_spawn(__embassy_main(spawner)); | ||
| 81 | }) | 69 | }) |
| 82 | }); | ||
| 83 | |||
| 84 | let (hal_setup, peris_arg) = match hal { | ||
| 85 | Some(hal) => ( | ||
| 86 | quote!( | ||
| 87 | let p = #hal::init(#config); | ||
| 88 | ), | ||
| 89 | quote!(p), | ||
| 90 | ), | ||
| 91 | None => (quote!(), quote!()), | ||
| 92 | }; | ||
| 93 | |||
| 94 | quote! { | ||
| 95 | #[cortex_m_rt::entry] | ||
| 96 | fn main() -> ! { | ||
| 97 | #hal_setup | ||
| 98 | |||
| 99 | let mut executor = ::embassy_executor::executor::Executor::new(); | ||
| 100 | let executor = unsafe { __make_static(&mut executor) }; | ||
| 101 | |||
| 102 | executor.run(|spawner| { | ||
| 103 | spawner.must_spawn(__embassy_main(spawner, #peris_arg)); | ||
| 104 | }) | ||
| 105 | } | ||
| 106 | } | 70 | } |
| 107 | }; | 71 | }; |
| 108 | 72 | ||
