From 9e445b3583c15c7701f3167eaa8dfe4afd541691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Sat, 4 Feb 2023 16:47:17 +0100 Subject: Move rtic macros to repo root, tune xtask --- rtic-macros/src/codegen/post_init.rs | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 rtic-macros/src/codegen/post_init.rs (limited to 'rtic-macros/src/codegen/post_init.rs') diff --git a/rtic-macros/src/codegen/post_init.rs b/rtic-macros/src/codegen/post_init.rs new file mode 100644 index 0000000..c4e5383 --- /dev/null +++ b/rtic-macros/src/codegen/post_init.rs @@ -0,0 +1,47 @@ +use crate::{analyze::Analysis, codegen::util, syntax::ast::App}; +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; + +/// Generates code that runs after `#[init]` returns +pub fn codegen(app: &App, analysis: &Analysis) -> Vec { + let mut stmts = vec![]; + + // Initialize shared resources + for (name, res) in &app.shared_resources { + let mangled_name = util::static_shared_resource_ident(name); + // If it's live + let cfgs = res.cfgs.clone(); + if analysis.shared_resources.get(name).is_some() { + stmts.push(quote!( + // We include the cfgs + #(#cfgs)* + // Resource is a RacyCell> + // - `get_mut` to obtain a raw pointer to `MaybeUninit` + // - `write` the defined value for the late resource T + #mangled_name.get_mut().write(core::mem::MaybeUninit::new(shared_resources.#name)); + )); + } + } + + // Initialize local resources + for (name, res) in &app.local_resources { + let mangled_name = util::static_local_resource_ident(name); + // If it's live + let cfgs = res.cfgs.clone(); + if analysis.local_resources.get(name).is_some() { + stmts.push(quote!( + // We include the cfgs + #(#cfgs)* + // Resource is a RacyCell> + // - `get_mut` to obtain a raw pointer to `MaybeUninit` + // - `write` the defined value for the late resource T + #mangled_name.get_mut().write(core::mem::MaybeUninit::new(local_resources.#name)); + )); + } + } + + // Enable the interrupts -- this completes the `init`-ialization phase + stmts.push(quote!(rtic::export::interrupt::enable();)); + + stmts +} -- cgit v1.2.3