diff options
| author | Quentin Smith <[email protected]> | 2022-08-19 00:53:06 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2022-08-19 00:53:41 -0400 |
| commit | 71e468681b1c074e06276d34ff00e36169c632f5 (patch) | |
| tree | ce86220caabbdc044720df8efb23de7bc4c36ea0 /embassy-macros | |
| parent | 2edf532f8d8ce048137990bf74b07759428ed7c1 (diff) | |
| parent | aefa5275a2ab2cac6caef599e7adb76ce1beeddd (diff) | |
Merge branch 'master' of https://github.com/embassy-rs/embassy into rtos-trace
Diffstat (limited to 'embassy-macros')
| -rw-r--r-- | embassy-macros/Cargo.toml | 3 | ||||
| -rw-r--r-- | embassy-macros/src/macros/cortex_m_interrupt_take.rs | 8 | ||||
| -rw-r--r-- | embassy-macros/src/macros/main.rs | 81 | ||||
| -rw-r--r-- | embassy-macros/src/macros/task.rs | 10 | ||||
| -rw-r--r-- | embassy-macros/src/util/mod.rs | 1 | ||||
| -rw-r--r-- | embassy-macros/src/util/path.rs | 41 |
6 files changed, 24 insertions, 120 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/cortex_m_interrupt_take.rs b/embassy-macros/src/macros/cortex_m_interrupt_take.rs index 62249807e..f6e41bcb4 100644 --- a/embassy-macros/src/macros/cortex_m_interrupt_take.rs +++ b/embassy-macros/src/macros/cortex_m_interrupt_take.rs | |||
| @@ -16,8 +16,8 @@ pub fn run(name: syn::Ident) -> Result<TokenStream, TokenStream> { | |||
| 16 | static HANDLER: interrupt::Handler; | 16 | static HANDLER: interrupt::Handler; |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | let func = HANDLER.func.load(::embassy_executor::export::atomic::Ordering::Relaxed); | 19 | let func = HANDLER.func.load(interrupt::_export::atomic::Ordering::Relaxed); |
| 20 | let ctx = HANDLER.ctx.load(::embassy_executor::export::atomic::Ordering::Relaxed); | 20 | let ctx = HANDLER.ctx.load(interrupt::_export::atomic::Ordering::Relaxed); |
| 21 | let func: fn(*mut ()) = ::core::mem::transmute(func); | 21 | let func: fn(*mut ()) = ::core::mem::transmute(func); |
| 22 | ::embassy_executor::rtos_trace_interrupt! { | 22 | ::embassy_executor::rtos_trace_interrupt! { |
| 23 | ::embassy_executor::export::trace::isr_enter(); | 23 | ::embassy_executor::export::trace::isr_enter(); |
| @@ -28,9 +28,9 @@ pub fn run(name: syn::Ident) -> Result<TokenStream, TokenStream> { | |||
| 28 | } | 28 | } |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | static TAKEN: ::embassy_executor::export::atomic::AtomicBool = ::embassy_executor::export::atomic::AtomicBool::new(false); | 31 | static TAKEN: interrupt::_export::atomic::AtomicBool = interrupt::_export::atomic::AtomicBool::new(false); |
| 32 | 32 | ||
| 33 | if TAKEN.compare_exchange(false, true, ::embassy_executor::export::atomic::Ordering::AcqRel, ::embassy_executor::export::atomic::Ordering::Acquire).is_err() { | 33 | if TAKEN.compare_exchange(false, true, interrupt::_export::atomic::Ordering::AcqRel, interrupt::_export::atomic::Ordering::Acquire).is_err() { |
| 34 | core::panic!("IRQ Already taken"); | 34 | core::panic!("IRQ Already taken"); |
| 35 | } | 35 | } |
| 36 | 36 | ||
diff --git a/embassy-macros/src/macros/main.rs b/embassy-macros/src/macros/main.rs index a8c8bb0d7..52987d7d2 100644 --- a/embassy-macros/src/macros/main.rs +++ b/embassy-macros/src/macros/main.rs | |||
| @@ -3,28 +3,12 @@ use proc_macro2::TokenStream; | |||
| 3 | use quote::quote; | 3 | use quote::quote; |
| 4 | 4 | ||
| 5 | use crate::util::ctxt::Ctxt; | 5 | use crate::util::ctxt::Ctxt; |
| 6 | use crate::util::path::ModulePrefix; | ||
| 7 | |||
| 8 | #[cfg(feature = "stm32")] | ||
| 9 | const HAL: Option<&str> = Some("embassy_stm32"); | ||
| 10 | #[cfg(feature = "nrf")] | ||
| 11 | const HAL: Option<&str> = Some("embassy_nrf"); | ||
| 12 | #[cfg(feature = "rp")] | ||
| 13 | const HAL: Option<&str> = Some("embassy_rp"); | ||
| 14 | #[cfg(not(any(feature = "stm32", feature = "nrf", feature = "rp")))] | ||
| 15 | const HAL: Option<&str> = None; | ||
| 16 | 6 | ||
| 17 | #[derive(Debug, FromMeta)] | 7 | #[derive(Debug, FromMeta)] |
| 18 | struct Args { | 8 | struct Args {} |
| 19 | #[darling(default)] | ||
| 20 | embassy_prefix: ModulePrefix, | ||
| 21 | |||
| 22 | #[allow(unused)] | ||
| 23 | #[darling(default)] | ||
| 24 | config: Option<syn::LitStr>, | ||
| 25 | } | ||
| 26 | 9 | ||
| 27 | 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> { |
| 11 | #[allow(unused_variables)] | ||
| 28 | let args = Args::from_list(&args).map_err(|e| e.write_errors())?; | 12 | let args = Args::from_list(&args).map_err(|e| e.write_errors())?; |
| 29 | 13 | ||
| 30 | let fargs = f.sig.inputs.clone(); | 14 | let fargs = f.sig.inputs.clone(); |
| @@ -32,32 +16,26 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 32 | let ctxt = Ctxt::new(); | 16 | let ctxt = Ctxt::new(); |
| 33 | 17 | ||
| 34 | if f.sig.asyncness.is_none() { | 18 | if f.sig.asyncness.is_none() { |
| 35 | ctxt.error_spanned_by(&f.sig, "task functions must be async"); | 19 | ctxt.error_spanned_by(&f.sig, "main function must be async"); |
| 36 | } | 20 | } |
| 37 | if !f.sig.generics.params.is_empty() { | 21 | if !f.sig.generics.params.is_empty() { |
| 38 | 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"); |
| 39 | } | 23 | } |
| 40 | 24 | ||
| 41 | if HAL.is_some() && fargs.len() != 2 { | 25 | if fargs.len() != 1 { |
| 42 | ctxt.error_spanned_by(&f.sig, "main function must have 2 arguments"); | 26 | ctxt.error_spanned_by(&f.sig, "main function must have 1 argument: the spawner."); |
| 43 | } | ||
| 44 | if HAL.is_none() && fargs.len() != 1 { | ||
| 45 | ctxt.error_spanned_by(&f.sig, "main function must have 1 argument"); | ||
| 46 | } | 27 | } |
| 47 | 28 | ||
| 48 | ctxt.check()?; | 29 | ctxt.check()?; |
| 49 | 30 | ||
| 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; | 31 | let f_body = f.block; |
| 54 | 32 | ||
| 55 | #[cfg(feature = "wasm")] | 33 | #[cfg(feature = "wasm")] |
| 56 | let main = quote! { | 34 | let main = quote! { |
| 57 | #[wasm_bindgen::prelude::wasm_bindgen(start)] | 35 | #[wasm_bindgen::prelude::wasm_bindgen(start)] |
| 58 | pub fn main() -> Result<(), wasm_bindgen::JsValue> { | 36 | pub fn main() -> Result<(), wasm_bindgen::JsValue> { |
| 59 | static EXECUTOR: ::embassy_util::Forever<#embassy_path::executor::Executor> = ::embassy_util::Forever::new(); | 37 | static EXECUTOR: ::embassy_util::Forever<::embassy_executor::Executor> = ::embassy_util::Forever::new(); |
| 60 | let executor = EXECUTOR.put(#embassy_path::executor::Executor::new()); | 38 | let executor = EXECUTOR.put(::embassy_executor::Executor::new()); |
| 61 | 39 | ||
| 62 | executor.start(|spawner| { | 40 | executor.start(|spawner| { |
| 63 | spawner.spawn(__embassy_main(spawner)).unwrap(); | 41 | spawner.spawn(__embassy_main(spawner)).unwrap(); |
| @@ -70,7 +48,7 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 70 | #[cfg(all(feature = "std", not(feature = "wasm")))] | 48 | #[cfg(all(feature = "std", not(feature = "wasm")))] |
| 71 | let main = quote! { | 49 | let main = quote! { |
| 72 | fn main() -> ! { | 50 | fn main() -> ! { |
| 73 | let mut executor = #embassy_path::executor::Executor::new(); | 51 | let mut executor = ::embassy_executor::Executor::new(); |
| 74 | let executor = unsafe { __make_static(&mut executor) }; | 52 | let executor = unsafe { __make_static(&mut executor) }; |
| 75 | 53 | ||
| 76 | executor.run(|spawner| { | 54 | executor.run(|spawner| { |
| @@ -80,43 +58,20 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 80 | }; | 58 | }; |
| 81 | 59 | ||
| 82 | #[cfg(all(not(feature = "std"), not(feature = "wasm")))] | 60 | #[cfg(all(not(feature = "std"), not(feature = "wasm")))] |
| 83 | let main = { | 61 | let main = quote! { |
| 84 | let config = args.config.map(|s| s.parse::<syn::Expr>().unwrap()).unwrap_or_else(|| { | 62 | #[cortex_m_rt::entry] |
| 85 | syn::Expr::Verbatim(quote! { | 63 | fn main() -> ! { |
| 86 | Default::default() | 64 | let mut executor = ::embassy_executor::Executor::new(); |
| 65 | let executor = unsafe { __make_static(&mut executor) }; | ||
| 66 | |||
| 67 | executor.run(|spawner| { | ||
| 68 | spawner.must_spawn(__embassy_main(spawner)); | ||
| 87 | }) | 69 | }) |
| 88 | }); | ||
| 89 | |||
| 90 | let (hal_setup, peris_arg) = match HAL { | ||
| 91 | Some(hal) => { | ||
| 92 | let embassy_hal_path = embassy_prefix.append(hal).path(); | ||
| 93 | ( | ||
| 94 | quote!( | ||
| 95 | let p = #embassy_hal_path::init(#config); | ||
| 96 | ), | ||
| 97 | quote!(p), | ||
| 98 | ) | ||
| 99 | } | ||
| 100 | None => (quote!(), quote!()), | ||
| 101 | }; | ||
| 102 | |||
| 103 | quote! { | ||
| 104 | #[cortex_m_rt::entry] | ||
| 105 | fn main() -> ! { | ||
| 106 | #hal_setup | ||
| 107 | |||
| 108 | let mut executor = #embassy_path::executor::Executor::new(); | ||
| 109 | let executor = unsafe { __make_static(&mut executor) }; | ||
| 110 | |||
| 111 | executor.run(|spawner| { | ||
| 112 | spawner.must_spawn(__embassy_main(spawner, #peris_arg)); | ||
| 113 | }) | ||
| 114 | } | ||
| 115 | } | 70 | } |
| 116 | }; | 71 | }; |
| 117 | 72 | ||
| 118 | let result = quote! { | 73 | let result = quote! { |
| 119 | #[#embassy_path::task(embassy_prefix = #embassy_prefix_lit)] | 74 | #[::embassy_executor::task()] |
| 120 | async fn __embassy_main(#fargs) { | 75 | async fn __embassy_main(#fargs) { |
| 121 | #f_body | 76 | #f_body |
| 122 | } | 77 | } |
diff --git a/embassy-macros/src/macros/task.rs b/embassy-macros/src/macros/task.rs index 57087c81c..573776f8c 100644 --- a/embassy-macros/src/macros/task.rs +++ b/embassy-macros/src/macros/task.rs | |||
| @@ -3,22 +3,16 @@ use proc_macro2::TokenStream; | |||
| 3 | use quote::{format_ident, quote}; | 3 | use quote::{format_ident, quote}; |
| 4 | 4 | ||
| 5 | use crate::util::ctxt::Ctxt; | 5 | use crate::util::ctxt::Ctxt; |
| 6 | use crate::util::path::ModulePrefix; | ||
| 7 | 6 | ||
| 8 | #[derive(Debug, FromMeta)] | 7 | #[derive(Debug, FromMeta)] |
| 9 | struct Args { | 8 | struct 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 | ||
| 16 | pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> { | 13 | pub 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::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::raw::TaskPool<Fut, #pool_size> = ::embassy_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 @@ | |||
| 1 | pub mod ctxt; | pub mod ctxt; | |
| 2 | pub 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 @@ | |||
| 1 | use darling::{FromMeta, Result}; | ||
| 2 | use proc_macro2::Span; | ||
| 3 | use syn::{LitStr, Path}; | ||
| 4 | |||
| 5 | #[derive(Debug)] | ||
| 6 | pub struct ModulePrefix { | ||
| 7 | literal: LitStr, | ||
| 8 | } | ||
| 9 | |||
| 10 | impl 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 | |||
| 31 | impl FromMeta for ModulePrefix { | ||
| 32 | fn from_string(value: &str) -> Result<Self> { | ||
| 33 | Ok(ModulePrefix::new(value)) | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | impl Default for ModulePrefix { | ||
| 38 | fn default() -> ModulePrefix { | ||
| 39 | ModulePrefix::new("::") | ||
| 40 | } | ||
| 41 | } | ||
