aboutsummaryrefslogtreecommitdiff
path: root/rtic-macros/src/codegen/post_init.rs
diff options
context:
space:
mode:
authorGravatar bors[bot] <26634292+bors[bot]@users.noreply.github.com> 2023-03-04 21:10:24 +0000
committerGravatar GitHub <noreply@github.com> 2023-03-04 21:10:24 +0000
commit7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b (patch)
tree80a47f0dc40059014e9448c4c2eb34c54dff45fe /rtic-macros/src/codegen/post_init.rs
parent1c5db277e4161470136dbd2a11e914ff1d383581 (diff)
parent98c5490d94950608d31cd5ad9dd260f2f853735c (diff)
downloadrtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.tar.gz
rtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.tar.zst
rtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.zip
Merge #694
694: RTIC 2 r=AfoHT a=korken89 Co-authored-by: Emil Fresk <emil.fresk@gmail.com> Co-authored-by: Per Lindgren <per.lindgren@ltu.se>
Diffstat (limited to 'rtic-macros/src/codegen/post_init.rs')
-rw-r--r--rtic-macros/src/codegen/post_init.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/rtic-macros/src/codegen/post_init.rs b/rtic-macros/src/codegen/post_init.rs
new file mode 100644
index 00000000..c4e53837
--- /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<TokenStream2> {
+ 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<MaybeUninit<T>>
+ // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
+ // - `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<MaybeUninit<T>>
+ // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
+ // - `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
+}