aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/pre_init.rs
blob: bc87b1df48d7a4e9ec04a0565f40b815bfbb9042 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::ast::App;

use crate::{analyze::Analysis, check::Extra, codegen::util};

/// Generates code that runs before `#[init]`
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
    let mut stmts = vec![];

    // Disable interrupts -- `init` must run with interrupts disabled
    stmts.push(quote!(rtic::export::interrupt::disable();));

    // Populate the FreeQueue
    for fq in &app.software_tasks {
        // Get the task name
        let name = fq.0;
        let task = fq.1;
        let cap = task.args.capacity;

        let fq_ident = util::fq_ident(name);

        stmts.push(quote!(
            (0..#cap).for_each(|i| #fq_ident.enqueue_unchecked(i));
        ));
    }

    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()
        .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
        let interrupt = util::interrupt_ident();
        stmts.push(quote!(
            core.NVIC.set_priority(
                #device::#interrupt::#name,
                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
        stmts.push(quote!(rtic::export::NVIC::unmask(#device::#interrupt::#name);));
    }

    // Set exception priorities
    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(
            rtic::export::SystemHandler::#name,
            rtic::export::logical2hw(#priority, #nvic_prio_bits),
        );));
    }

    // Initialize the SysTick if there exist a TimerQueue
    if extra.monotonic.is_some() {
        let priority = analysis.channels.keys().max().unwrap();

        // 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(
            rtic::export::SystemHandler::SysTick,
            rtic::export::logical2hw(#priority, #nvic_prio_bits),
        );));

        stmts.push(quote!(
            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
    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);));
    }

    stmts
}