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
|
|
|
|
|
|
|
use crate::{analyze::Analysis, check::Extra, codegen::util};
|
|
|
|
|
|
|
|
/// Generates task dispatchers
|
2020-12-10 20:33:13 +01:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStream2> {
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut items = vec![];
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let interrupts = &analysis.interrupts;
|
|
|
|
|
|
|
|
for (&level, channel) in &analysis.channels {
|
|
|
|
let mut stmts = vec![];
|
|
|
|
|
|
|
|
let variants = channel
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|name| {
|
|
|
|
let cfgs = &app.software_tasks[name].cfgs;
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-02-25 19:05:39 +01:00
|
|
|
// let doc = format!(
|
|
|
|
// "Software tasks to be dispatched at priority level {}",
|
|
|
|
// level,
|
|
|
|
// );
|
2020-08-27 13:21:56 +02:00
|
|
|
let t = util::spawn_t_ident(level);
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Clone, Copy)]
|
2021-02-25 19:05:39 +01:00
|
|
|
// #[doc = #doc]
|
|
|
|
#[doc(hidden)]
|
2020-10-05 21:57:44 +02:00
|
|
|
pub enum #t {
|
2020-08-27 13:21:56 +02:00
|
|
|
#(#variants,)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
let n = util::capacity_typenum(channel.capacity, true);
|
|
|
|
let rq = util::rq_ident(level);
|
2021-02-25 19:05:39 +01:00
|
|
|
let rq = util::mark_internal_ident(&rq);
|
2020-09-01 19:04:55 +02:00
|
|
|
let (rq_ty, rq_expr) = {
|
2020-08-27 13:21:56 +02:00
|
|
|
(
|
|
|
|
quote!(rtic::export::SCRQ<#t, #n>),
|
|
|
|
quote!(rtic::export::Queue(unsafe {
|
|
|
|
rtic::export::iQueue::u8_sc()
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-02-25 19:05:39 +01:00
|
|
|
// let doc = format!(
|
|
|
|
// "Queue of tasks ready to be dispatched at priority level {}",
|
|
|
|
// level
|
|
|
|
// );
|
2020-08-27 13:21:56 +02:00
|
|
|
items.push(quote!(
|
2021-02-25 19:05:39 +01:00
|
|
|
#[doc(hidden)]
|
2020-10-21 20:20:26 +02:00
|
|
|
static mut #rq: #rq_ty = #rq_expr;
|
2020-08-27 13:21:56 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
let arms = channel
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|name| {
|
|
|
|
let task = &app.software_tasks[name];
|
|
|
|
let cfgs = &task.cfgs;
|
|
|
|
let fq = util::fq_ident(name);
|
2021-02-25 19:05:39 +01:00
|
|
|
let fq = util::mark_internal_ident(&fq);
|
2020-08-27 13:21:56 +02:00
|
|
|
let inputs = util::inputs_ident(name);
|
2021-02-25 19:05:39 +01:00
|
|
|
let inputs = util::mark_internal_ident(&inputs);
|
2020-08-27 13:21:56 +02:00
|
|
|
let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let locals_new = if task.locals.is_empty() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
quote!(#name::Locals::new(),)
|
|
|
|
};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-10-15 18:50:17 +02:00
|
|
|
let app_name = &app.name;
|
|
|
|
let app_path = quote! {crate::#app_name};
|
2020-08-27 13:21:56 +02:00
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#t::#name => {
|
|
|
|
let #tupled =
|
|
|
|
#inputs.get_unchecked(usize::from(index)).as_ptr().read();
|
|
|
|
#fq.split().0.enqueue_unchecked(index);
|
|
|
|
let priority = &rtic::export::Priority::new(PRIORITY);
|
2020-10-15 18:50:17 +02:00
|
|
|
#app_path::#name(
|
2020-08-27 13:21:56 +02:00
|
|
|
#locals_new
|
2020-12-12 23:24:54 +01:00
|
|
|
#name::Context::new(priority)
|
2020-08-27 13:21:56 +02:00
|
|
|
#(,#pats)*
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
}
|
2020-08-27 13:21:56 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
stmts.push(quote!(
|
|
|
|
while let Some((task, index)) = #rq.split().1.dequeue() {
|
|
|
|
match task {
|
|
|
|
#(#arms)*
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
2020-08-27 13:21:56 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
let doc = format!("Interrupt handler to dispatch tasks at priority {}", level);
|
2020-10-23 10:35:56 +02:00
|
|
|
let interrupt = util::suffixed(&interrupts[&level].0.to_string());
|
|
|
|
let attribute = &interrupts[&level].1.attrs;
|
2020-08-27 13:21:56 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
#[doc = #doc]
|
|
|
|
#[no_mangle]
|
2020-10-23 10:35:56 +02:00
|
|
|
#(#attribute)*
|
2020-08-27 13:21:56 +02:00
|
|
|
unsafe fn #interrupt() {
|
|
|
|
/// The priority of this interrupt handler
|
|
|
|
const PRIORITY: u8 = #level;
|
|
|
|
|
|
|
|
rtic::export::run(PRIORITY, || {
|
|
|
|
#(#stmts)*
|
|
|
|
});
|
|
|
|
}
|
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
items
|
|
|
|
}
|