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() {
|
2021-03-11 19:12:02 +01:00
|
|
|
// Generate the marker counter used to track for `cancel` and `reschedule`
|
2021-08-19 21:32:12 +02:00
|
|
|
let tq_marker = util::timer_queue_marker_ident();
|
2021-03-11 19:12:02 +01:00
|
|
|
items.push(quote!(
|
|
|
|
// #[doc = #doc]
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(non_camel_case_types)]
|
2021-08-19 08:21:18 +02:00
|
|
|
#[allow(non_upper_case_globals)]
|
2021-04-08 18:25:09 +02:00
|
|
|
static #tq_marker: rtic::RacyCell<u32> = rtic::RacyCell::new(0);
|
2021-03-11 19:12:02 +01:00
|
|
|
));
|
|
|
|
|
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<_>>();
|
|
|
|
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-02-25 19:05:39 +01:00
|
|
|
// let doc = "Tasks that can be scheduled".to_string();
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
2021-02-25 19:05:39 +01:00
|
|
|
// #[doc = #doc]
|
|
|
|
#[doc(hidden)]
|
2019-06-13 23:56:59 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Clone, Copy)]
|
2021-03-11 19:12:02 +01:00
|
|
|
pub 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();
|
2021-02-25 19:05:39 +01:00
|
|
|
let mono_type = &monotonic.ty;
|
2021-02-18 19:30:59 +01:00
|
|
|
let m_ident = util::monotonic_ident(&monotonic_name);
|
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
|
|
|
{
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-02-25 19:05:39 +01:00
|
|
|
// let doc = &format!("Timer queue for {}", monotonic_name);
|
2021-08-16 15:37:39 +02:00
|
|
|
let cap: usize = app
|
2020-10-11 18:38:38 +02:00
|
|
|
.software_tasks
|
|
|
|
.iter()
|
2021-08-16 15:37:39 +02:00
|
|
|
.map(|(_name, task)| task.args.capacity as usize)
|
2020-10-11 18:38:38 +02:00
|
|
|
.sum();
|
2021-08-16 15:37:39 +02:00
|
|
|
let n = util::capacity_literal(cap);
|
|
|
|
let tq_ty = quote!(rtic::export::TimerQueue<#mono_type, #t, #n>);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-04-08 18:25:09 +02:00
|
|
|
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
2021-02-25 19:05:39 +01:00
|
|
|
#[doc(hidden)]
|
2021-08-19 08:21:18 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[allow(non_upper_case_globals)]
|
2021-04-08 18:25:09 +02:00
|
|
|
static #tq: rtic::RacyCell<#tq_ty> =
|
2021-08-16 15:37:39 +02:00
|
|
|
rtic::RacyCell::new(rtic::export::TimerQueue(rtic::export::SortedLinkedList::new_u16()));
|
2019-06-13 23:56:59 +02:00
|
|
|
));
|
2021-02-18 19:30:59 +01:00
|
|
|
|
|
|
|
let mono = util::monotonic_ident(&monotonic_name);
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-02-25 19:05:39 +01:00
|
|
|
// let doc = &format!("Storage for {}", monotonic_name);
|
2021-02-18 19:30:59 +01:00
|
|
|
|
|
|
|
items.push(quote!(
|
2021-02-25 19:05:39 +01:00
|
|
|
#[doc(hidden)]
|
2021-08-19 08:21:18 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[allow(non_upper_case_globals)]
|
2021-04-08 18:25:09 +02:00
|
|
|
static #mono: rtic::RacyCell<Option<#mono_type>> = rtic::RacyCell::new(None);
|
2021-02-18 19:30:59 +01:00
|
|
|
));
|
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-12-13 14:52:16 +01:00
|
|
|
let rt_err = util::rt_err_ident();
|
2020-12-12 23:24:54 +01:00
|
|
|
|
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-12-13 14:52:16 +01:00
|
|
|
rtic::pend(#rt_err::#enum_::#interrupt);
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#t::#name => {
|
2021-11-02 13:41:12 +01:00
|
|
|
rtic::export::interrupt::free(|_| (&mut *#rq.get_mut()).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;
|
2021-02-18 19:30:59 +01:00
|
|
|
let disable_isr = if &*bound_interrupt.to_string() == "SysTick" {
|
2021-09-28 10:18:43 +02:00
|
|
|
quote!(core::mem::transmute::<_, rtic::export::SYST>(()).disable_interrupt())
|
2020-12-13 14:52:16 +01:00
|
|
|
} else {
|
|
|
|
quote!(rtic::export::NVIC::mask(#rt_err::#enum_::#bound_interrupt))
|
|
|
|
};
|
2020-12-12 23:24:54 +01:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
|
|
|
#[no_mangle]
|
2021-02-04 20:22:02 +01:00
|
|
|
#[allow(non_snake_case)]
|
2020-12-08 20:49:13 +01:00
|
|
|
unsafe fn #bound_interrupt() {
|
2021-02-21 16:15:34 +01:00
|
|
|
while let Some((task, index)) = rtic::export::interrupt::free(|_|
|
2021-11-02 13:41:12 +01:00
|
|
|
if let Some(mono) = (&mut *#m_ident.get_mut()).as_mut() {
|
|
|
|
(&mut *#tq.get_mut()).dequeue(|| #disable_isr, mono)
|
2021-02-21 16:15:34 +01:00
|
|
|
} else {
|
|
|
|
// We can only use the timer queue if `init` has returned, and it
|
|
|
|
// writes the `Some(monotonic)` we are accessing here.
|
|
|
|
core::hint::unreachable_unchecked()
|
|
|
|
})
|
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
|
|
|
}
|
2021-02-21 21:57:18 +01:00
|
|
|
|
2021-11-02 13:41:12 +01:00
|
|
|
rtic::export::interrupt::free(|_| if let Some(mono) = (&mut *#m_ident.get_mut()).as_mut() {
|
2021-02-21 21:57:18 +01:00
|
|
|
mono.on_interrupt();
|
|
|
|
});
|
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
|
|
|
|
}
|