aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-macros/src/macros/main.rs57
-rw-r--r--embassy-macros/src/macros/task.rs10
-rw-r--r--embassy-macros/src/util/mod.rs1
-rw-r--r--embassy-macros/src/util/path.rs41
4 files changed, 26 insertions, 83 deletions
diff --git a/embassy-macros/src/macros/main.rs b/embassy-macros/src/macros/main.rs
index a8c8bb0d7..b040edc5a 100644
--- a/embassy-macros/src/macros/main.rs
+++ b/embassy-macros/src/macros/main.rs
@@ -3,28 +3,16 @@ use proc_macro2::TokenStream;
3use quote::quote; 3use quote::quote;
4 4
5use crate::util::ctxt::Ctxt; 5use crate::util::ctxt::Ctxt;
6use crate::util::path::ModulePrefix;
7
8#[cfg(feature = "stm32")]
9const HAL: Option<&str> = Some("embassy_stm32");
10#[cfg(feature = "nrf")]
11const HAL: Option<&str> = Some("embassy_nrf");
12#[cfg(feature = "rp")]
13const HAL: Option<&str> = Some("embassy_rp");
14#[cfg(not(any(feature = "stm32", feature = "nrf", feature = "rp")))]
15const HAL: Option<&str> = None;
16 6
17#[derive(Debug, FromMeta)] 7#[derive(Debug, FromMeta)]
18struct Args { 8struct Args {
19 #[darling(default)]
20 embassy_prefix: ModulePrefix,
21
22 #[allow(unused)] 9 #[allow(unused)]
23 #[darling(default)] 10 #[darling(default)]
24 config: Option<syn::LitStr>, 11 config: Option<syn::LitStr>,
25} 12}
26 13
27pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> { 14pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
15 #[allow(unused_variables)]
28 let args = Args::from_list(&args).map_err(|e| e.write_errors())?; 16 let args = Args::from_list(&args).map_err(|e| e.write_errors())?;
29 17
30 let fargs = f.sig.inputs.clone(); 18 let fargs = f.sig.inputs.clone();
@@ -38,26 +26,32 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
38 ctxt.error_spanned_by(&f.sig, "task functions must not be generic"); 26 ctxt.error_spanned_by(&f.sig, "task functions must not be generic");
39 } 27 }
40 28
41 if HAL.is_some() && fargs.len() != 2 { 29 #[cfg(feature = "stm32")]
30 let hal = Some(quote!(::embassy_stm32));
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 {
42 ctxt.error_spanned_by(&f.sig, "main function must have 2 arguments"); 39 ctxt.error_spanned_by(&f.sig, "main function must have 2 arguments");
43 } 40 }
44 if HAL.is_none() && fargs.len() != 1 { 41 if hal.is_none() && fargs.len() != 1 {
45 ctxt.error_spanned_by(&f.sig, "main function must have 1 argument"); 42 ctxt.error_spanned_by(&f.sig, "main function must have 1 argument");
46 } 43 }
47 44
48 ctxt.check()?; 45 ctxt.check()?;
49 46
50 let embassy_prefix = args.embassy_prefix;
51 let embassy_prefix_lit = embassy_prefix.literal();
52 let embassy_path = embassy_prefix.append("embassy_executor").path();
53 let f_body = f.block; 47 let f_body = f.block;
54 48
55 #[cfg(feature = "wasm")] 49 #[cfg(feature = "wasm")]
56 let main = quote! { 50 let main = quote! {
57 #[wasm_bindgen::prelude::wasm_bindgen(start)] 51 #[wasm_bindgen::prelude::wasm_bindgen(start)]
58 pub fn main() -> Result<(), wasm_bindgen::JsValue> { 52 pub fn main() -> Result<(), wasm_bindgen::JsValue> {
59 static EXECUTOR: ::embassy_util::Forever<#embassy_path::executor::Executor> = ::embassy_util::Forever::new(); 53 static EXECUTOR: ::embassy_util::Forever<::embassy_executor::executor::Executor> = ::embassy_util::Forever::new();
60 let executor = EXECUTOR.put(#embassy_path::executor::Executor::new()); 54 let executor = EXECUTOR.put(::embassy_executor::executor::Executor::new());
61 55
62 executor.start(|spawner| { 56 executor.start(|spawner| {
63 spawner.spawn(__embassy_main(spawner)).unwrap(); 57 spawner.spawn(__embassy_main(spawner)).unwrap();
@@ -70,7 +64,7 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
70 #[cfg(all(feature = "std", not(feature = "wasm")))] 64 #[cfg(all(feature = "std", not(feature = "wasm")))]
71 let main = quote! { 65 let main = quote! {
72 fn main() -> ! { 66 fn main() -> ! {
73 let mut executor = #embassy_path::executor::Executor::new(); 67 let mut executor = ::embassy_executor::executor::Executor::new();
74 let executor = unsafe { __make_static(&mut executor) }; 68 let executor = unsafe { __make_static(&mut executor) };
75 69
76 executor.run(|spawner| { 70 executor.run(|spawner| {
@@ -87,16 +81,13 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
87 }) 81 })
88 }); 82 });
89 83
90 let (hal_setup, peris_arg) = match HAL { 84 let (hal_setup, peris_arg) = match hal {
91 Some(hal) => { 85 Some(hal) => (
92 let embassy_hal_path = embassy_prefix.append(hal).path(); 86 quote!(
93 ( 87 let p = #hal::init(#config);
94 quote!( 88 ),
95 let p = #embassy_hal_path::init(#config); 89 quote!(p),
96 ), 90 ),
97 quote!(p),
98 )
99 }
100 None => (quote!(), quote!()), 91 None => (quote!(), quote!()),
101 }; 92 };
102 93
@@ -105,7 +96,7 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
105 fn main() -> ! { 96 fn main() -> ! {
106 #hal_setup 97 #hal_setup
107 98
108 let mut executor = #embassy_path::executor::Executor::new(); 99 let mut executor = ::embassy_executor::executor::Executor::new();
109 let executor = unsafe { __make_static(&mut executor) }; 100 let executor = unsafe { __make_static(&mut executor) };
110 101
111 executor.run(|spawner| { 102 executor.run(|spawner| {
@@ -116,7 +107,7 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
116 }; 107 };
117 108
118 let result = quote! { 109 let result = quote! {
119 #[#embassy_path::task(embassy_prefix = #embassy_prefix_lit)] 110 #[::embassy_executor::task()]
120 async fn __embassy_main(#fargs) { 111 async fn __embassy_main(#fargs) {
121 #f_body 112 #f_body
122 } 113 }
diff --git a/embassy-macros/src/macros/task.rs b/embassy-macros/src/macros/task.rs
index 57087c81c..414e5cb09 100644
--- a/embassy-macros/src/macros/task.rs
+++ b/embassy-macros/src/macros/task.rs
@@ -3,22 +3,16 @@ use proc_macro2::TokenStream;
3use quote::{format_ident, quote}; 3use quote::{format_ident, quote};
4 4
5use crate::util::ctxt::Ctxt; 5use crate::util::ctxt::Ctxt;
6use crate::util::path::ModulePrefix;
7 6
8#[derive(Debug, FromMeta)] 7#[derive(Debug, FromMeta)]
9struct Args { 8struct Args {
10 #[darling(default)] 9 #[darling(default)]
11 pool_size: Option<usize>, 10 pool_size: Option<usize>,
12 #[darling(default)]
13 embassy_prefix: ModulePrefix,
14} 11}
15 12
16pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> { 13pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
17 let args = Args::from_list(&args).map_err(|e| e.write_errors())?; 14 let args = Args::from_list(&args).map_err(|e| e.write_errors())?;
18 15
19 let embassy_prefix = args.embassy_prefix.append("embassy_executor");
20 let embassy_path = embassy_prefix.path();
21
22 let pool_size: usize = args.pool_size.unwrap_or(1); 16 let pool_size: usize = args.pool_size.unwrap_or(1);
23 17
24 let ctxt = Ctxt::new(); 18 let ctxt = Ctxt::new();
@@ -70,9 +64,9 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke
70 // in the user's code. 64 // in the user's code.
71 #task_inner 65 #task_inner
72 66
73 #visibility fn #task_ident(#fargs) -> #embassy_path::executor::SpawnToken<impl Sized> { 67 #visibility fn #task_ident(#fargs) -> ::embassy_executor::executor::SpawnToken<impl Sized> {
74 type Fut = impl ::core::future::Future + 'static; 68 type Fut = impl ::core::future::Future + 'static;
75 static POOL: #embassy_path::executor::raw::TaskPool<Fut, #pool_size> = #embassy_path::executor::raw::TaskPool::new(); 69 static POOL: ::embassy_executor::executor::raw::TaskPool<Fut, #pool_size> = ::embassy_executor::executor::raw::TaskPool::new();
76 unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } 70 unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) }
77 } 71 }
78 }; 72 };
diff --git a/embassy-macros/src/util/mod.rs b/embassy-macros/src/util/mod.rs
index c2f2dfd65..28702809e 100644
--- a/embassy-macros/src/util/mod.rs
+++ b/embassy-macros/src/util/mod.rs
@@ -1,2 +1 @@
1pub mod ctxt; pub mod ctxt;
2pub mod path;
diff --git a/embassy-macros/src/util/path.rs b/embassy-macros/src/util/path.rs
deleted file mode 100644
index 00fca7bdb..000000000
--- a/embassy-macros/src/util/path.rs
+++ /dev/null
@@ -1,41 +0,0 @@
1use darling::{FromMeta, Result};
2use proc_macro2::Span;
3use syn::{LitStr, Path};
4
5#[derive(Debug)]
6pub struct ModulePrefix {
7 literal: LitStr,
8}
9
10impl ModulePrefix {
11 pub fn new(path: &str) -> Self {
12 let literal = LitStr::new(path, Span::call_site());
13 Self { literal }
14 }
15
16 pub fn append(&self, component: &str) -> ModulePrefix {
17 let mut lit = self.literal().value();
18 lit.push_str(component);
19 Self::new(lit.as_str())
20 }
21
22 pub fn path(&self) -> Path {
23 self.literal.parse().unwrap()
24 }
25
26 pub fn literal(&self) -> &LitStr {
27 &self.literal
28 }
29}
30
31impl FromMeta for ModulePrefix {
32 fn from_string(value: &str) -> Result<Self> {
33 Ok(ModulePrefix::new(value))
34 }
35}
36
37impl Default for ModulePrefix {
38 fn default() -> ModulePrefix {
39 ModulePrefix::new("::")
40 }
41}