rtic/macros/src/codegen/timer_queue.rs

111 lines
3.4 KiB
Rust
Raw Normal View History

use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
2020-06-11 19:18:29 +02:00
use rtic_syntax::ast::App;
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![];
if let Some(m) = extra.monotonic {
2020-08-27 13:21:56 +02:00
let t = util::schedule_t_ident();
// Enumeration of `schedule`-able tasks
{
2020-10-11 18:38:38 +02:00
let variants = app
.software_tasks
.iter()
2020-10-11 18:38:38 +02:00
.map(|(name, task)| {
let cfgs = &task.cfgs;
quote!(
#(#cfgs)*
#name
)
})
.collect::<Vec<_>>();
2020-08-27 13:21:56 +02:00
let doc = format!("Tasks that can be scheduled");
items.push(quote!(
#[doc = #doc]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
2020-10-11 18:38:38 +02:00
pub enum #t {
#(#variants,)*
}
));
}
2020-08-27 13:21:56 +02:00
let tq = util::tq_ident();
// Static variable and resource proxy
{
2020-08-27 13:21:56 +02:00
let doc = format!("Timer queue");
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>);
items.push(quote!(
#[doc = #doc]
2020-10-11 18:38:38 +02:00
pub static mut #tq: #tq_ty = rtic::export::TimerQueue(
2020-06-11 19:18:29 +02:00
rtic::export::BinaryHeap(
rtic::export::iBinaryHeap::new()
)
);
));
}
// Timer queue handler
{
2020-10-11 18:38:38 +02:00
let arms = app
.software_tasks
.iter()
2020-10-11 18:38:38 +02:00
.map(|(name, task)| {
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);
2020-08-27 13:21:56 +02:00
let pend = {
quote!(
rtic::pend(you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#interrupt);
)
};
quote!(
#(#cfgs)*
#t::#name => {
2020-10-11 18:38:38 +02:00
rtic::export::interrupt::free(|_| #rq.split().0.enqueue_unchecked((#rqt::#name, index)));
#pend
}
)
})
.collect::<Vec<_>>();
2020-08-27 13:21:56 +02:00
let sys_tick = util::suffixed("SysTick");
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 _;
2020-10-11 18:38:38 +02:00
while let Some((task, index)) = rtic::export::interrupt::free(|_| #tq.dequeue())
{
match task {
#(#arms)*
}
2020-10-11 18:38:38 +02:00
}
}
));
}
}
items
}