aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/pre_init.rs
blob: 19fc6461ec90eb27a7a5478721824f0606900970 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtfm_syntax::ast::{App, HardwareTaskKind};

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

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

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

    // populate this core `FreeQueue`s
    for (name, senders) in &analysis.free_queues {
        let task = &app.software_tasks[name];
        let cap = task.args.capacity;

        for &sender in senders.keys() {
            if sender == core {
                let fq = util::fq_ident(name, sender);

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

    stmts.push(quote!(
        // NOTE(transmute) to avoid debug_assertion in multi-core mode
        let mut core: rtfm::export::Peripherals = core::mem::transmute(());
    ));

    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
        .get(&core)
        .iter()
        .flat_map(|interrupts| *interrupts)
        .chain(app.hardware_tasks.iter().flat_map(|(name, task)| {
            if task.kind == HardwareTaskKind::Interrupt {
                Some((&task.args.priority, task.args.binds(name)))
            } 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(core, app.args.cores);
        stmts.push(quote!(
            core.NVIC.set_priority(
                #device::#interrupt::#name,
                rtfm::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!(core.NVIC.enable(#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
    if analysis.spawn_barriers.contains_key(&core) {
        let sb = util::spawn_barrier(core);
        let shared = if cfg!(feature = "heterogeneous") {
            Some(quote!(
                #[rtfm::export::shared]
            ))
        } else {
            None
        };

        const_app.push(quote!(
            #shared
            static #sb: rtfm::export::Barrier = rtfm::export::Barrier::new();
        ));

        // unblock cores that may send us a message
        stmts.push(quote!(
            #sb.release();
        ));
    }

    // set exception priorities
    for (name, priority) in app.hardware_tasks.iter().filter_map(|(name, task)| {
        if task.kind == HardwareTaskKind::Exception {
            Some((task.args.binds(name), 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(
            rtfm::export::SystemHandler::#name,
            rtfm::export::logical2hw(#priority, #nvic_prio_bits),
        );));
    }

    // initialize the SysTick
    if let Some(tq) = analysis.timer_queues.get(&core) {
        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(
            rtfm::export::SystemHandler::SysTick,
            rtfm::export::logical2hw(#priority, #nvic_prio_bits),
        );));

        stmts.push(quote!(
            core.SYST.set_clock_source(rtfm::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.get(&core).is_none() {
        // Set SLEEPONEXIT bit to enter sleep mode when returning from ISR
        stmts.push(quote!(core.SCB.scr.modify(|r| r | 1 << 1);));
    }

    // 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();
            ));
        }
    }

    (const_app, stmts)
}