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 code that runs before `#[init]`
|
2020-09-01 16:39:05 +02:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut stmts = vec![];
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Disable interrupts -- `init` must run with interrupts disabled
|
2020-06-11 19:18:29 +02:00
|
|
|
stmts.push(quote!(rtic::export::interrupt::disable();));
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Populate the FreeQueue
|
2020-10-11 19:41:57 +02:00
|
|
|
for (name, task) in &app.software_tasks {
|
2019-06-13 23:56:59 +02:00
|
|
|
let cap = task.args.capacity;
|
2020-08-27 13:21:56 +02:00
|
|
|
let fq_ident = util::fq_ident(name);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-06-30 21:23:35 +02:00
|
|
|
stmts.push(quote!(
|
2020-08-27 13:21:56 +02:00
|
|
|
(0..#cap).for_each(|i| #fq_ident.enqueue_unchecked(i));
|
2020-06-30 21:23:35 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
));
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
let device = extra.device;
|
|
|
|
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Unmask interrupts and set their priorities
|
2019-06-13 23:56:59 +02:00
|
|
|
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))
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
2020-09-01 19:04:55 +02:00
|
|
|
// We do exceptions in another pass
|
2019-06-13 23:56:59 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
{
|
2020-09-01 19:04:55 +02:00
|
|
|
// Compile time assert that this priority is supported by the device
|
2019-06-13 23:56:59 +02:00
|
|
|
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();
|
2019-06-13 23:56:59 +02:00
|
|
|
stmts.push(quote!(
|
|
|
|
core.NVIC.set_priority(
|
2020-10-14 20:27:43 +02:00
|
|
|
you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#interrupt::#name,
|
2020-06-11 19:18:29 +02:00
|
|
|
rtic::export::logical2hw(#priority, #nvic_prio_bits),
|
2019-06-13 23:56:59 +02:00
|
|
|
);
|
|
|
|
));
|
|
|
|
|
|
|
|
// NOTE unmask the interrupt *after* setting its priority: changing the priority of a pended
|
|
|
|
// interrupt is implementation defined
|
2020-10-14 20:27:43 +02:00
|
|
|
stmts.push(quote!(rtic::export::NVIC::unmask(you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#interrupt::#name);));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +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))
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
2020-09-01 19:04:55 +02:00
|
|
|
// Compile time assert that this priority is supported by the device
|
2019-06-13 23:56:59 +02:00
|
|
|
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),
|
2019-06-13 23:56:59 +02:00
|
|
|
);));
|
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Initialize the SysTick if there exist a TimerQueue
|
2020-10-11 18:38:38 +02:00
|
|
|
if extra.monotonic.is_some() {
|
|
|
|
let priority = analysis.channels.keys().max().unwrap();
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Compile time assert that this priority is supported by the device
|
2019-06-13 23:56:59 +02:00
|
|
|
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),
|
2019-06-13 23:56:59 +02:00
|
|
|
);));
|
|
|
|
|
|
|
|
stmts.push(quote!(
|
2020-06-11 19:18:29 +02:00
|
|
|
core.SYST.set_clock_source(rtic::export::SystClkSource::Core);
|
2019-06-13 23:56:59 +02:00
|
|
|
core.SYST.enable_counter();
|
|
|
|
core.DCB.enable_trace();
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// 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() {
|
2019-06-13 23:56:59 +02:00
|
|
|
// 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
|
|
|
stmts
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|