rtic/macros/src/codegen/pre_init.rs

149 lines
4.9 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 code that runs before `#[init]`
2020-09-01 16:39:05 +02:00
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
let mut stmts = vec![];
// disable interrupts -- `init` must run with interrupts disabled
2020-06-11 19:18:29 +02:00
stmts.push(quote!(rtic::export::interrupt::disable();));
2020-08-27 13:21:56 +02:00
// populate the FreeQueue
for fq in &analysis.free_queues {
// Get the task name
let name = fq.0;
let task = &app.software_tasks[name];
let cap = task.args.capacity;
2020-08-27 13:21:56 +02:00
let fq_ident = util::fq_ident(name);
stmts.push(quote!(
2020-08-27 13:21:56 +02:00
(0..#cap).for_each(|i| #fq_ident.enqueue_unchecked(i));
));
}
2020-08-27 13:21:56 +02:00
stmts.push(quote!(
// To set the variable in cortex_m so the peripherals cannot be taken multiple times
let mut core: rtic::export::Peripherals = rtic::export::Peripherals::steal().into();
));
let device = extra.device;
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
// unmask interrupts and set their priorities
for (&priority, name) in analysis
.interrupts
.iter()
2019-06-20 06:19:59 +02:00
.chain(app.hardware_tasks.values().flat_map(|task| {
if !util::is_exception(&task.args.binds) {
Some((&task.args.priority, &task.args.binds))
} else {
// we do exceptions in another pass
None
}
}))
{
// compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
// NOTE this also checks that the interrupt exists in the `Interrupt` enumeration
2020-08-27 13:21:56 +02:00
let interrupt = util::interrupt_ident();
stmts.push(quote!(
core.NVIC.set_priority(
2019-06-18 10:31:31 +02:00
#device::#interrupt::#name,
2020-06-11 19:18:29 +02:00
rtic::export::logical2hw(#priority, #nvic_prio_bits),
);
));
// NOTE unmask the interrupt *after* setting its priority: changing the priority of a pended
// interrupt is implementation defined
2020-06-11 19:18:29 +02:00
stmts.push(quote!(rtic::export::NVIC::unmask(#device::#interrupt::#name);));
}
// cross-spawn barriers: now that priorities have been set and the interrupts have been unmasked
// we are ready to receive messages from *other* cores
2020-08-27 13:21:56 +02:00
/*
if analysis.spawn_barriers.contains_key(&core) {
let sb = util::spawn_barrier(core);
2019-06-18 10:31:31 +02:00
let shared = if cfg!(feature = "heterogeneous") {
Some(quote!(
2020-06-11 19:18:29 +02:00
#[rtic::export::shared]
2019-06-18 10:31:31 +02:00
))
} else {
None
};
const_app.push(quote!(
2019-06-18 10:31:31 +02:00
#shared
2020-06-11 19:18:29 +02:00
static #sb: rtic::export::Barrier = rtic::export::Barrier::new();
));
// unblock cores that may send us a message
stmts.push(quote!(
#sb.release();
));
}
2020-08-27 13:21:56 +02:00
*/
// set exception priorities
2019-06-20 06:19:59 +02:00
for (name, priority) in app.hardware_tasks.values().filter_map(|task| {
if util::is_exception(&task.args.binds) {
Some((&task.args.binds, task.args.priority))
} else {
None
}
}) {
// compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
stmts.push(quote!(core.SCB.set_priority(
2020-06-11 19:18:29 +02:00
rtic::export::SystemHandler::#name,
rtic::export::logical2hw(#priority, #nvic_prio_bits),
);));
}
2020-08-27 13:21:56 +02:00
// initialize the SysTick if there exist a TimerQueue
if let Some(tq) = analysis.timer_queues.first() {
let priority = tq.priority;
// compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
stmts.push(quote!(core.SCB.set_priority(
2020-06-11 19:18:29 +02:00
rtic::export::SystemHandler::SysTick,
rtic::export::logical2hw(#priority, #nvic_prio_bits),
);));
stmts.push(quote!(
2020-06-11 19:18:29 +02:00
core.SYST.set_clock_source(rtic::export::SystClkSource::Core);
core.SYST.enable_counter();
core.DCB.enable_trace();
));
}
// if there's no user `#[idle]` then optimize returning from interrupt handlers
2020-08-27 13:21:56 +02:00
if app.idles.is_empty() {
// Set SLEEPONEXIT bit to enter sleep mode when returning from ISR
stmts.push(quote!(core.SCB.scr.modify(|r| r | 1 << 1);));
}
2020-08-27 13:21:56 +02:00
/*
// cross-spawn barriers: wait until other cores are ready to receive messages
for (&receiver, senders) in &analysis.spawn_barriers {
// only block here if `init` can send messages to `receiver`
if senders.get(&core) == Some(&true) {
let sb = util::spawn_barrier(receiver);
stmts.push(quote!(
#sb.wait();
));
}
}
2020-08-27 13:21:56 +02:00
*/
2020-08-27 13:21:56 +02:00
stmts
}