-
Notifications
You must be signed in to change notification settings - Fork 190
/
timer_queue.rs
153 lines (133 loc) · 5.11 KB
/
timer_queue.rs
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![];
for (&sender, timer_queue) in &analysis.timer_queues {
let cfg_sender = util::cfg_core(sender, app.args.cores);
let t = util::schedule_t_ident(sender);
// Enumeration of `schedule`-able tasks
{
let variants = timer_queue
.tasks
.iter()
.map(|name| {
let cfgs = &app.software_tasks[name].cfgs;
quote!(
#(#cfgs)*
#name
)
})
.collect::<Vec<_>>();
let doc = format!("Tasks that can be scheduled from core #{}", sender);
items.push(quote!(
#cfg_sender
#[doc = #doc]
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
enum #t {
#(#variants,)*
}
));
}
let tq = util::tq_ident(sender);
// Static variable and resource proxy
{
let doc = format!("Core #{} timer queue", sender);
let m = extra.monotonic();
let n = util::capacity_typenum(timer_queue.capacity, false);
let tq_ty = quote!(rtic::export::TimerQueue<#m, #t, #n>);
let section = util::link_section("bss", sender);
items.push(quote!(
#cfg_sender
#[doc = #doc]
#section
static mut #tq: #tq_ty = rtic::export::TimerQueue(
rtic::export::BinaryHeap(
rtic::export::iBinaryHeap::new()
)
);
#cfg_sender
struct #tq<'a> {
priority: &'a rtic::export::Priority,
}
));
items.push(util::impl_mutex(
extra,
&[],
cfg_sender.as_ref(),
false,
&tq,
tq_ty,
timer_queue.ceiling,
quote!(&mut #tq),
));
}
// Timer queue handler
{
let rt_err = util::rt_err_ident();
let arms = timer_queue
.tasks
.iter()
.map(|name| {
let task = &app.software_tasks[name];
let cfgs = &task.cfgs;
let priority = task.args.priority;
let receiver = task.args.core;
let rq = util::rq_ident(receiver, priority, sender);
let rqt = util::spawn_t_ident(receiver, priority, sender);
let enum_ = util::interrupt_ident(receiver, app.args.cores);
let interrupt = &analysis.interrupts[&receiver][&priority];
let pend = if sender != receiver {
quote!(
#rt_err::xpend(#receiver, #rt_err::#enum_::#interrupt);
)
} else {
quote!(
rtic::pend(#rt_err::#enum_::#interrupt);
)
};
quote!(
#(#cfgs)*
#t::#name => {
(#rq { priority: &rtic::export::Priority::new(PRIORITY) }).lock(|rq| {
rq.split().0.enqueue_unchecked((#rqt::#name, index))
});
#pend
}
)
})
.collect::<Vec<_>>();
let priority = timer_queue.priority;
let sys_tick = util::suffixed("SysTick", sender);
let section = util::link_section("text", sender);
items.push(quote!(
#[no_mangle]
#cfg_sender
#section
unsafe fn #sys_tick() {
use rtic::Mutex as _;
/// The priority of this handler
const PRIORITY: u8 = #priority;
rtic::export::run(PRIORITY, || {
while let Some((task, index)) = (#tq {
// NOTE dynamic priority is always the static priority at this point
priority: &rtic::export::Priority::new(PRIORITY),
})
// NOTE `inline(always)` produces faster and smaller code
.lock(#[inline(always)]
|tq| tq.dequeue())
{
match task {
#(#arms)*
}
}
});
}
));
}
}
items
}