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 timer queues and timer queue handlers
|
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-12-08 20:49:13 +01:00
|
|
|
if !app.monotonics.is_empty() {
|
2020-08-27 13:21:56 +02:00
|
|
|
let t = util::schedule_t_ident();
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
// Enumeration of `schedule`-able tasks
|
|
|
|
{
|
2020-10-11 18:38:38 +02:00
|
|
|
let variants = app
|
|
|
|
.software_tasks
|
2019-06-13 23:56:59 +02:00
|
|
|
.iter()
|
2020-10-11 18:38:38 +02:00
|
|
|
.map(|(name, task)| {
|
|
|
|
let cfgs = &task.cfgs;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-10-13 16:16:33 +02:00
|
|
|
let doc = "Tasks that can be scheduled".to_string();
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #doc]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Clone, Copy)]
|
2020-10-21 20:20:26 +02:00
|
|
|
enum #t {
|
2019-06-13 23:56:59 +02:00
|
|
|
#(#variants,)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
2020-12-08 20:49:13 +01:00
|
|
|
}
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-12-08 20:49:13 +01:00
|
|
|
for (_, monotonic) in &app.monotonics {
|
|
|
|
let monotonic_name = monotonic.ident.to_string();
|
|
|
|
let tq = util::tq_ident(&monotonic_name);
|
|
|
|
let t = util::schedule_t_ident();
|
|
|
|
let m = &monotonic.ident;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-12-08 20:49:13 +01:00
|
|
|
// Static variables and resource proxy
|
2019-06-13 23:56:59 +02:00
|
|
|
{
|
2020-12-08 20:49:13 +01:00
|
|
|
let doc = &format!("Timer queue for {}", monotonic_name);
|
2020-10-11 18:38:38 +02:00
|
|
|
let cap = app
|
|
|
|
.software_tasks
|
|
|
|
.iter()
|
|
|
|
.map(|(_name, task)| task.args.capacity)
|
|
|
|
.sum();
|
|
|
|
let n = util::capacity_typenum(cap, false);
|
2020-06-11 19:18:29 +02:00
|
|
|
let tq_ty = quote!(rtic::export::TimerQueue<#m, #t, #n>);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #doc]
|
2020-10-21 20:20:26 +02:00
|
|
|
static mut #tq: #tq_ty = rtic::export::TimerQueue(
|
2020-06-11 19:18:29 +02:00
|
|
|
rtic::export::BinaryHeap(
|
|
|
|
rtic::export::iBinaryHeap::new()
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer queue handler
|
|
|
|
{
|
2020-12-12 23:24:54 +01:00
|
|
|
let enum_ = util::interrupt_ident();
|
|
|
|
|
2020-10-11 18:38:38 +02:00
|
|
|
let arms = app
|
|
|
|
.software_tasks
|
2019-06-13 23:56:59 +02:00
|
|
|
.iter()
|
2020-10-11 18:38:38 +02:00
|
|
|
.map(|(name, task)| {
|
2019-06-13 23:56:59 +02:00
|
|
|
let cfgs = &task.cfgs;
|
|
|
|
let priority = task.args.priority;
|
2020-08-27 13:21:56 +02:00
|
|
|
let rq = util::rq_ident(priority);
|
|
|
|
let rqt = util::spawn_t_ident(priority);
|
2020-12-08 20:49:13 +01:00
|
|
|
|
|
|
|
// The interrupt that runs the task dispatcher
|
2020-10-23 10:35:56 +02:00
|
|
|
let interrupt = &analysis.interrupts.get(&priority).expect("RTIC-ICE: interrupt not found").0;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let pend = {
|
2019-06-13 23:56:59 +02:00
|
|
|
quote!(
|
2020-10-14 20:27:43 +02:00
|
|
|
rtic::pend(you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#interrupt);
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#t::#name => {
|
2020-10-11 18:38:38 +02:00
|
|
|
rtic::export::interrupt::free(|_| #rq.split().0.enqueue_unchecked((#rqt::#name, index)));
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
#pend
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-12-08 20:49:13 +01:00
|
|
|
let bound_interrupt = &monotonic.args.binds;
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[no_mangle]
|
2020-12-08 20:49:13 +01:00
|
|
|
unsafe fn #bound_interrupt() {
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic::Mutex as _;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-12-12 23:24:54 +01:00
|
|
|
while let Some((task, index)) = rtic::export::interrupt::free(|_| #tq.dequeue(
|
|
|
|
|| rtic::export::NVIC::unmask(you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#bound_interrupt),
|
|
|
|
))
|
2020-10-11 18:38:38 +02:00
|
|
|
{
|
|
|
|
match task {
|
|
|
|
#(#arms)*
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
2020-10-11 18:38:38 +02:00
|
|
|
}
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 20:49:13 +01:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
items
|
|
|
|
}
|