aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/hardware_tasks.rs
blob: c7f3e7d913df897d5e611994f72b47a8c2a4890f (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
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::{ast::App, Context};

use crate::{
    analyze::Analysis,
    check::Extra,
    codegen::{local_resources_struct, module, shared_resources_struct},
};

/// Generate support code for hardware tasks (`#[exception]`s and `#[interrupt]`s)
pub fn codegen(
    app: &App,
    analysis: &Analysis,
    extra: &Extra,
) -> (
    // mod_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors
    Vec<TokenStream2>,
    // root_hardware_tasks -- items that must be placed in the root of the crate:
    // - `${task}Locals` structs
    // - `${task}Resources` structs
    // - `${task}` modules
    Vec<TokenStream2>,
    // user_hardware_tasks -- the `#[task]` functions written by the user
    Vec<TokenStream2>,
) {
    let mut mod_app = vec![];
    let mut root = vec![];
    let mut user_tasks = vec![];

    for (name, task) in &app.hardware_tasks {
        // TODO: Fix locals
        // let locals_new = if task.args.local_resources.is_empty() {
        //     quote!()
        // } else {
        //     quote!(#name::Locals::new(),)
        // };

        let symbol = task.args.binds.clone();
        let priority = task.args.priority;
        let cfgs = &task.cfgs;
        let attrs = &task.attrs;

        mod_app.push(quote!(
            #[allow(non_snake_case)]
            #[no_mangle]
            #(#attrs)*
            #(#cfgs)*
            unsafe fn #symbol() {
                const PRIORITY: u8 = #priority;

                rtic::export::run(PRIORITY, || {
                    #name(
                        #name::Context::new(&rtic::export::Priority::new(PRIORITY))
                    )
                });
            }
        ));

        let mut shared_needs_lt = false;
        let mut local_needs_lt = false;

        // TODO: Fix locals
        // `${task}Locals`
        if !task.args.local_resources.is_empty() {
            let (item, constructor) = local_resources_struct::codegen(
                Context::HardwareTask(name),
                &mut local_needs_lt,
                app,
            );

            root.push(item);

            mod_app.push(constructor);
        }

        // `${task}Resources`
        if !task.args.shared_resources.is_empty() {
            let (item, constructor) = shared_resources_struct::codegen(
                Context::HardwareTask(name),
                &mut shared_needs_lt,
                app,
            );

            root.push(item);

            mod_app.push(constructor);
        }

        root.push(module::codegen(
            Context::HardwareTask(name),
            shared_needs_lt,
            local_needs_lt,
            app,
            analysis,
            extra,
        ));

        if !task.is_extern {
            let attrs = &task.attrs;
            let context = &task.context;
            let stmts = &task.stmts;
            user_tasks.push(quote!(
                #(#attrs)*
                #[allow(non_snake_case)]
                fn #name(#context: #name::Context) {
                    use rtic::Mutex as _;
                    use rtic::mutex_prelude::*;

                    #(#stmts)*
                }
            ));
        }
    }

    (mod_app, root, user_tasks)
}