1use proc_macro2::TokenStream as TokenStream2;
2use quote::quote;
3use rtic_syntax::ast::App;
45use crate::{analyze::Analysis, check::Extra, codegen::util};
67/// Generates code that runs before `#[init]`
8pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
9let mut stmts = vec![];
1011let rt_err = util::rt_err_ident();
1213// Disable interrupts -- `init` must run with interrupts disabled
14stmts.push(quote!(rtic::export::interrupt::disable();));
1516// Populate the FreeQueue
17for (name, task) in &app.software_tasks {
18let cap = task.args.capacity;
19let cfgs = &task.cfgs;
20let fq_ident = util::fq_ident(name);
2122 stmts.push(quote!(
23 #(#cfgs)*
24 (0..#cap).for_each(|i| (&mut *#fq_ident.get_mut()).enqueue_unchecked(i));
25 ));
26 }
2728 stmts.push(quote!(
29// To set the variable in cortex_m so the peripherals cannot be taken multiple times
30let mut core: rtic::export::Peripherals = rtic::export::Peripherals::steal().into();
31 ));
3233let device = &extra.device;
34let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
3536// check that all dispatchers exists in the `Interrupt` enumeration regardless of whether
37 // they are used or not
38let interrupt = util::interrupt_ident();
39for name in app.args.extern_interrupts.keys() {
40 stmts.push(quote!(let _ = #rt_err::#interrupt::#name;));
41 }
4243let interrupt_ids = analysis.interrupts.iter().map(|(p, (id, _))| (p, id));
4445// Unmask interrupts and set their priorities
46for (&priority, name) in interrupt_ids.chain(app.hardware_tasks.values().filter_map(|task| {
47if util::is_exception(&task.args.binds) {
48// We do exceptions in another pass
49None
50} else {
51Some((&task.args.priority, &task.args.binds))
52 }
53 })) {
54let es = format!(
55"Maximum priority used by interrupt vector '{}' is more than supported by hardware",
56 name
57 );
58// Compile time assert that this priority is supported by the device
59stmts.push(quote!(
60const _: () = if (1 << #nvic_prio_bits) < #priority as usize { ::core::panic!(#es); };
61 ));
6263 stmts.push(quote!(
64 core.NVIC.set_priority(
65 #rt_err::#interrupt::#name,
66 rtic::export::logical2hw(#priority, #nvic_prio_bits),
67 );
68 ));
6970// NOTE unmask the interrupt *after* setting its priority: changing the priority of a pended
71 // interrupt is implementation defined
72stmts.push(quote!(rtic::export::NVIC::unmask(#rt_err::#interrupt::#name);));
73 }
7475// Set exception priorities
76for (name, priority) in app.hardware_tasks.values().filter_map(|task| {
77if util::is_exception(&task.args.binds) {
78Some((&task.args.binds, task.args.priority))
79 } else {
80None
81}
82 }) {
83let es = format!(
84"Maximum priority used by interrupt vector '{}' is more than supported by hardware",
85 name
86 );
87// Compile time assert that this priority is supported by the device
88stmts.push(quote!(
89const _: () = if (1 << #nvic_prio_bits) < #priority as usize { ::core::panic!(#es); };
90 ));
9192 stmts.push(quote!(core.SCB.set_priority(
93 rtic::export::SystemHandler::#name,
94 rtic::export::logical2hw(#priority, #nvic_prio_bits),
95 );));
96 }
9798// Initialize monotonic's interrupts
99for (_, monotonic) in &app.monotonics {
100let priority = if let Some(prio) = monotonic.args.priority {
101quote! { #prio }
102 } else {
103quote! { (1 << #nvic_prio_bits) }
104 };
105let binds = &monotonic.args.binds;
106107let name = &monotonic.ident;
108let es = format!(
109"Maximum priority used by monotonic '{}' is more than supported by hardware",
110 name
111 );
112// Compile time assert that this priority is supported by the device
113stmts.push(quote!(
114const _: () = if (1 << #nvic_prio_bits) < #priority as usize { ::core::panic!(#es); };
115 ));
116117let mono_type = &monotonic.ty;
118119if &*binds.to_string() == "SysTick" {
120 stmts.push(quote!(
121 core.SCB.set_priority(
122 rtic::export::SystemHandler::SysTick,
123 rtic::export::logical2hw(#priority, #nvic_prio_bits),
124 );
125126// Always enable monotonic interrupts if they should never be off
127if !<#mono_type as rtic::Monotonic>::DISABLE_INTERRUPT_ON_EMPTY_QUEUE {
128 core::mem::transmute::<_, rtic::export::SYST>(())
129 .enable_interrupt();
130 }
131 ));
132 } else {
133 stmts.push(quote!(
134 core.NVIC.set_priority(
135 #rt_err::#interrupt::#binds,
136 rtic::export::logical2hw(#priority, #nvic_prio_bits),
137 );
138139// Always enable monotonic interrupts if they should never be off
140if !<#mono_type as rtic::Monotonic>::DISABLE_INTERRUPT_ON_EMPTY_QUEUE {
141 rtic::export::NVIC::unmask(#rt_err::#interrupt::#binds);
142 }
143 ));
144 }
145 }
146 stmts
147}