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
|
|
|
|
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
if let Some(timer_queue) = &analysis.timer_queues.first() {
|
|
|
|
let t = util::schedule_t_ident();
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
// Enumeration of `schedule`-able tasks
|
|
|
|
{
|
|
|
|
let variants = timer_queue
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|name| {
|
|
|
|
let cfgs = &app.software_tasks[name].cfgs;
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let doc = format!("Tasks that can be scheduled");
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #doc]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum #t {
|
|
|
|
#(#variants,)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let tq = util::tq_ident();
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
// Static variable and resource proxy
|
|
|
|
{
|
2020-08-27 13:21:56 +02:00
|
|
|
let doc = format!("Timer queue");
|
2019-06-13 23:56:59 +02:00
|
|
|
let m = extra.monotonic();
|
|
|
|
let n = util::capacity_typenum(timer_queue.capacity, 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-06-11 19:18:29 +02:00
|
|
|
static mut #tq: #tq_ty = rtic::export::TimerQueue(
|
|
|
|
rtic::export::BinaryHeap(
|
|
|
|
rtic::export::iBinaryHeap::new()
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
struct #tq<'a> {
|
2020-06-11 19:18:29 +02:00
|
|
|
priority: &'a rtic::export::Priority,
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
items.push(util::impl_mutex(
|
|
|
|
extra,
|
|
|
|
&[],
|
|
|
|
false,
|
|
|
|
&tq,
|
|
|
|
tq_ty,
|
|
|
|
timer_queue.ceiling,
|
|
|
|
quote!(&mut #tq),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer queue handler
|
|
|
|
{
|
|
|
|
let device = extra.device;
|
|
|
|
let arms = timer_queue
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|name| {
|
|
|
|
let task = &app.software_tasks[name];
|
|
|
|
|
|
|
|
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);
|
|
|
|
let enum_ = util::interrupt_ident();
|
|
|
|
let interrupt = &analysis.interrupts.get(&priority);
|
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-06-11 19:18:29 +02:00
|
|
|
rtic::pend(#device::#enum_::#interrupt);
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#t::#name => {
|
2020-06-11 19:18:29 +02:00
|
|
|
(#rq { priority: &rtic::export::Priority::new(PRIORITY) }).lock(|rq| {
|
2019-06-13 23:56:59 +02:00
|
|
|
rq.split().0.enqueue_unchecked((#rqt::#name, index))
|
|
|
|
});
|
|
|
|
|
|
|
|
#pend
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let priority = timer_queue.priority;
|
2020-08-27 13:21:56 +02:00
|
|
|
let sys_tick = util::suffixed("SysTick");
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[no_mangle]
|
2019-06-18 10:31:31 +02:00
|
|
|
unsafe fn #sys_tick() {
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic::Mutex as _;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
/// The priority of this handler
|
|
|
|
const PRIORITY: u8 = #priority;
|
|
|
|
|
2020-06-11 19:18:29 +02:00
|
|
|
rtic::export::run(PRIORITY, || {
|
2019-06-13 23:56:59 +02:00
|
|
|
while let Some((task, index)) = (#tq {
|
|
|
|
// NOTE dynamic priority is always the static priority at this point
|
2020-06-11 19:18:29 +02:00
|
|
|
priority: &rtic::export::Priority::new(PRIORITY),
|
2019-06-13 23:56:59 +02:00
|
|
|
})
|
|
|
|
// NOTE `inline(always)` produces faster and smaller code
|
|
|
|
.lock(#[inline(always)]
|
2020-08-27 13:21:56 +02:00
|
|
|
|tq| tq.dequeue())
|
2019-06-13 23:56:59 +02:00
|
|
|
{
|
|
|
|
match task {
|
|
|
|
#(#arms)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items
|
|
|
|
}
|