aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/timer_queue.rs
blob: 82d0ac981e5e0963139638908ed781bd446edf6a (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
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::ast::App;

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

/// Generates timer queues and timer queue handlers
pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStream2> {
    let mut items = vec![];

    if !app.monotonics.is_empty() {
        let t = util::schedule_t_ident();

        // Enumeration of `schedule`-able tasks
        {
            let variants = app
                .software_tasks
                .iter()
                .map(|(name, task)| {
                    let cfgs = &task.cfgs;

                    quote!(
                        #(#cfgs)*
                        #name
                    )
                })
                .collect::<Vec<_>>();

            // let doc = "Tasks that can be scheduled".to_string();
            items.push(quote!(
                // #[doc = #doc]
                #[doc(hidden)]
                #[allow(non_camel_case_types)]
                #[derive(Clone, Copy)]
                enum #t {
                    #(#variants,)*
                }
            ));
        }
    }

    for (_, monotonic) in &app.monotonics {
        let monotonic_name = monotonic.ident.to_string();
        let tq = util::tq_ident(&monotonic_name);
        let tq = util::mark_internal_ident(&tq);
        let t = util::schedule_t_ident();
        let mono_type = &monotonic.ty;
        let m_ident = util::monotonic_ident(&monotonic_name);
        let m_ident = util::mark_internal_ident(&m_ident);
        let app_name = &app.name;
        let app_path = quote! {crate::#app_name};

        // Static variables and resource proxy
        {
            // let doc = &format!("Timer queue for {}", monotonic_name);
            let cap = app
                .software_tasks
                .iter()
                .map(|(_name, task)| task.args.capacity)
                .sum();
            let n = util::capacity_typenum(cap, false);
            let tq_ty = quote!(rtic::export::TimerQueue<#mono_type, #t, #n>);

            items.push(quote!(
                #[doc(hidden)]
                static mut #tq: #tq_ty = rtic::export::TimerQueue(
                    rtic::export::BinaryHeap(
                        rtic::export::iBinaryHeap::new()
                    )
                );
            ));

            let mono = util::monotonic_ident(&monotonic_name);
            let mono = util::mark_internal_ident(&mono);
            // let doc = &format!("Storage for {}", monotonic_name);

            items.push(quote!(
                #[doc(hidden)]
                static mut #mono: Option<#mono_type> = None;
            ));
        }

        // Timer queue handler
        {
            let enum_ = util::interrupt_ident();
            let rt_err = util::rt_err_ident();

            let arms = app
                .software_tasks
                .iter()
                .map(|(name, task)| {
                    let cfgs = &task.cfgs;
                    let priority = task.args.priority;
                    let rq = util::rq_ident(priority);
                    let rq = util::mark_internal_ident(&rq);
                    let rqt = util::spawn_t_ident(priority);

                    // The interrupt that runs the task dispatcher
                    let interrupt = &analysis.interrupts.get(&priority).expect("RTIC-ICE: interrupt not found").0;

                    let pend = {
                        quote!(
                            rtic::pend(#rt_err::#enum_::#interrupt);
                        )
                    };

                    quote!(
                        #(#cfgs)*
                        #t::#name => {
                            rtic::export::interrupt::free(|_| #rq.split().0.enqueue_unchecked((#rqt::#name, index)));

                            #pend
                        }
                    )
                })
                .collect::<Vec<_>>();

            let bound_interrupt = &monotonic.args.binds;
            let disable_isr = if &*bound_interrupt.to_string() == "SysTick" {
                quote!(core::mem::transmute::<_, cortex_m::peripheral::SYST>(()).disable_interrupt())
            } else {
                quote!(rtic::export::NVIC::mask(#rt_err::#enum_::#bound_interrupt))
            };

            items.push(quote!(
                #[no_mangle]
                #[allow(non_snake_case)]
                unsafe fn #bound_interrupt() {

                    while let Some((task, index)) = rtic::export::interrupt::free(|_|
                        if let Some(mono) = #app_path::#m_ident.as_mut() {
                            #tq.dequeue(|| #disable_isr, mono)
                        } else {
                            // We can only use the timer queue if `init` has returned, and it
                            // writes the `Some(monotonic)` we are accessing here.
                            core::hint::unreachable_unchecked()
                        })
                    {
                        match task {
                            #(#arms)*
                        }
                    }

                    rtic::export::interrupt::free(|_| if let Some(mono) = #app_path::#m_ident.as_mut() {
                        mono.on_interrupt();
                    });
                }
            ));
        }
    }

    items
}
5236744034dd5e1a3c0?s=13&d=retro' width='13' height='13' alt='Gravatar' /> Jarred Sumner 2-1/+6 2023-08-21Update README.md (#4232)Gravatar xxxhussein 1-1/+1 2023-08-21Add missing header changeGravatar Jarred Sumner 1-1/+1 2023-08-21Add LazyPropertyGravatar Jarred Sumner 1-0/+3 2023-08-21Fix BigIntStats generated classGravatar Jarred Sumner 1-1/+1 2023-08-21RegenerateGravatar Jarred Sumner 1-8/+15 2023-08-21Implement FileGravatar Jarred Sumner 12-12/+387 2023-08-20Fixes #1675 (#4230)Gravatar Jarred Sumner 8-70/+297 2023-08-20Implement `--inspect-brk` (#4222)Gravatar Jarred Sumner 17-41/+101 2023-08-20Fix test failures from 3a9a6c63a (#4231)Gravatar Jarred Sumner 4-32/+34 2023-08-20Fix(bundler): use different alias mappings based on the target. (#4163)Gravatar Ai Hoshino 8-18/+90 2023-08-19Update BunDebugger.cppGravatar Jarred Sumner 1-1/+3 2023-08-19Introduce `bun --inspect-wait`Gravatar Jarred Sumner 3-19/+47 2023-08-19misc non-posix fixesGravatar Jarred Sumner 2-3/+3 2023-08-19Update lockfile.mdGravatar Jarred Sumner 1-1/+8 2023-08-19Update lockfile.mdGravatar Jarred Sumner 1-4/+4 2023-08-19Update lockfile.mdGravatar Jarred Sumner 1-1/+29 2023-08-19Update Dockerfile-distroless (#4210)Gravatar Omar 1-0/+1 2023-08-19Fix symbol visibilityGravatar Jarred Sumner 1-0/+1 2023-08-19[napi] Implement `node_api_create_syntax_error`, `node_api_symbol_for`, `nod...Gravatar Jarred Sumner 5-1/+70 2023-08-19Fix crash impacting sharp & resvg (#4221)Gravatar Jarred Sumner 5-73/+73 2023-08-19Fixes #172 (#4220)Gravatar Jarred Sumner 7-9/+87 2023-08-19Add inline sourcemaps when `--inspect` is enabled (#4213)Gravatar Jarred Sumner 3-3/+64 2023-08-19tty `ReadStream`, `WriteStream`, and readline rawmode (#4179)Gravatar Dylan Conway 23-722/+821 2023-08-18Fix make headers (again)Gravatar Jarred Sumner 1-0/+2