2019-06-13 23:56:59 +02:00
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic_syntax::ast::App;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-10-21 20:20:26 +02:00
|
|
|
use crate::{analyze::Analysis, codegen::util};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
/// Generates code that runs after `#[init]` returns
|
2020-09-01 19:04:55 +02:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut stmts = vec![];
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Initialize late resources
|
2020-10-13 16:16:33 +02:00
|
|
|
if !analysis.late_resources.is_empty() {
|
2020-08-27 13:21:56 +02:00
|
|
|
// BTreeSet wrapped in a vector
|
2020-09-01 19:04:55 +02:00
|
|
|
for name in analysis.late_resources.first().unwrap() {
|
2020-10-21 20:20:26 +02:00
|
|
|
let mangled_name = util::mangle_ident(&name);
|
2020-09-01 19:04:55 +02:00
|
|
|
// If it's live
|
2020-04-08 16:43:47 +02:00
|
|
|
let cfgs = app.late_resources[name].cfgs.clone();
|
2019-06-13 23:56:59 +02:00
|
|
|
if analysis.locations.get(name).is_some() {
|
2020-04-08 16:43:47 +02:00
|
|
|
// Need to also include the cfgs
|
|
|
|
stmts.push(quote!(
|
2020-06-03 12:34:01 +02:00
|
|
|
#(#cfgs)*
|
2020-10-21 20:20:26 +02:00
|
|
|
#mangled_name.as_mut_ptr().write(late.#name);
|
2020-06-03 12:34:01 +02:00
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Enable the interrupts -- this completes the `init`-ialization phase
|
2020-06-11 19:18:29 +02:00
|
|
|
stmts.push(quote!(rtic::export::interrupt::enable();));
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
stmts
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|