diff options
| -rw-r--r-- | embassy-macros/src/macros/task.rs | 18 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/random.rs | 33 |
2 files changed, 46 insertions, 5 deletions
diff --git a/embassy-macros/src/macros/task.rs b/embassy-macros/src/macros/task.rs index 573776f8c..90d2cd893 100644 --- a/embassy-macros/src/macros/task.rs +++ b/embassy-macros/src/macros/task.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | use darling::FromMeta; | 1 | use darling::FromMeta; |
| 2 | use proc_macro2::TokenStream; | 2 | use proc_macro2::TokenStream; |
| 3 | use quote::{format_ident, quote}; | 3 | use quote::{format_ident, quote}; |
| 4 | use syn::{parse_quote, ItemFn}; | ||
| 4 | 5 | ||
| 5 | use crate::util::ctxt::Ctxt; | 6 | use crate::util::ctxt::Ctxt; |
| 6 | 7 | ||
| @@ -57,18 +58,25 @@ pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, Toke | |||
| 57 | task_inner.vis = syn::Visibility::Inherited; | 58 | task_inner.vis = syn::Visibility::Inherited; |
| 58 | task_inner.sig.ident = task_inner_ident.clone(); | 59 | task_inner.sig.ident = task_inner_ident.clone(); |
| 59 | 60 | ||
| 61 | let mut task_outer: ItemFn = parse_quote! { | ||
| 62 | #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { | ||
| 63 | type Fut = impl ::core::future::Future + 'static; | ||
| 64 | static POOL: ::embassy_executor::raw::TaskPool<Fut, #pool_size> = ::embassy_executor::raw::TaskPool::new(); | ||
| 65 | unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } | ||
| 66 | } | ||
| 67 | }; | ||
| 68 | |||
| 69 | task_outer.attrs.append(&mut task_inner.attrs.clone()); | ||
| 70 | |||
| 60 | let result = quote! { | 71 | let result = quote! { |
| 61 | // This is the user's task function, renamed. | 72 | // This is the user's task function, renamed. |
| 62 | // We put it outside the #task_ident fn below, because otherwise | 73 | // We put it outside the #task_ident fn below, because otherwise |
| 63 | // the items defined there (such as POOL) would be in scope | 74 | // the items defined there (such as POOL) would be in scope |
| 64 | // in the user's code. | 75 | // in the user's code. |
| 76 | #[doc(hidden)] | ||
| 65 | #task_inner | 77 | #task_inner |
| 66 | 78 | ||
| 67 | #visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> { | 79 | #task_outer |
| 68 | type Fut = impl ::core::future::Future + 'static; | ||
| 69 | static POOL: ::embassy_executor::raw::TaskPool<Fut, #pool_size> = ::embassy_executor::raw::TaskPool::new(); | ||
| 70 | unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) } | ||
| 71 | } | ||
| 72 | }; | 80 | }; |
| 73 | 81 | ||
| 74 | Ok(result) | 82 | Ok(result) |
diff --git a/examples/stm32wl/src/bin/random.rs b/examples/stm32wl/src/bin/random.rs new file mode 100644 index 000000000..182c607f9 --- /dev/null +++ b/examples/stm32wl/src/bin/random.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::pac; | ||
| 8 | use embassy_stm32::rng::Rng; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | #[embassy_executor::main] | ||
| 12 | async fn main(_spawner: Spawner) { | ||
| 13 | let mut config = embassy_stm32::Config::default(); | ||
| 14 | config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32; | ||
| 15 | config.rcc.enable_lsi = true; //Needed for RNG to work | ||
| 16 | |||
| 17 | let p = embassy_stm32::init(config); | ||
| 18 | unsafe { | ||
| 19 | pac::RCC.ccipr().modify(|w| { | ||
| 20 | w.set_rngsel(0b01); | ||
| 21 | }); | ||
| 22 | } | ||
| 23 | |||
| 24 | info!("Hello World!"); | ||
| 25 | |||
| 26 | let mut rng = Rng::new(p.RNG); | ||
| 27 | |||
| 28 | let mut buf = [0u8; 16]; | ||
| 29 | unwrap!(rng.async_fill_bytes(&mut buf).await); | ||
| 30 | info!("random bytes: {:02x}", buf); | ||
| 31 | |||
| 32 | loop {} | ||
| 33 | } | ||
