-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathmodule.rs
208 lines (174 loc) · 6.24 KB
/
module.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::syntax::{ast::App, Context};
use crate::{analyze::Analysis, codegen::bindings::interrupt_mod, codegen::util};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
#[allow(clippy::too_many_lines)]
pub fn codegen(ctxt: Context, app: &App, analysis: &Analysis) -> TokenStream2 {
let mut items = vec![];
let mut module_items = vec![];
let mut fields = vec![];
let mut values = vec![];
// Used to copy task cfgs to the whole module
let mut task_cfgs = vec![];
let name = ctxt.ident(app);
match ctxt {
Context::Init => {
fields.push(quote!(
/// The space used to allocate async executors in bytes.
pub executors_size: usize
));
if app.args.core {
fields.push(quote!(
/// Core peripherals
pub core: rtic::export::Peripherals
));
values.push(quote!(core: core));
}
if app.args.peripherals {
let device = &app.args.device;
fields.push(quote!(
/// Device peripherals (PAC)
pub device: #device::Peripherals
));
values.push(quote!(device: #device::Peripherals::steal()));
}
fields.push(quote!(
/// Critical section token for init
pub cs: rtic::export::CriticalSection<'a>
));
values.push(quote!(cs: rtic::export::CriticalSection::new()));
values.push(quote!(executors_size));
}
Context::Idle | Context::HardwareTask(_) | Context::SoftwareTask(_) => {}
}
if ctxt.has_local_resources(app) {
let ident = util::local_resources_ident(ctxt, app);
module_items.push(quote!(
#[doc(inline)]
pub use super::#ident as LocalResources;
));
fields.push(quote!(
/// Local Resources this task has access to
pub local: #name::LocalResources<'a>
));
values.push(quote!(local: #name::LocalResources::new()));
}
if ctxt.has_shared_resources(app) {
let ident = util::shared_resources_ident(ctxt, app);
module_items.push(quote!(
#[doc(inline)]
pub use super::#ident as SharedResources;
));
fields.push(quote!(
/// Shared Resources this task has access to
pub shared: #name::SharedResources<'a>
));
values.push(quote!(shared: #name::SharedResources::new()));
}
let doc = match ctxt {
Context::Idle => "Idle loop",
Context::Init => "Initialization function",
Context::HardwareTask(_) => "Hardware task",
Context::SoftwareTask(_) => "Software task",
};
let v = Vec::new();
let cfgs = match ctxt {
Context::HardwareTask(t) => &app.hardware_tasks[t].cfgs,
Context::SoftwareTask(t) => &app.software_tasks[t].cfgs,
_ => &v,
};
let core = if ctxt.is_init() {
if app.args.core {
Some(quote!(core: rtic::export::Peripherals, executors_size: usize))
} else {
Some(quote!(executors_size: usize))
}
} else {
None
};
let internal_context_name = util::internal_task_ident(name, "Context");
let exec_name = util::internal_task_ident(name, "EXEC");
items.push(quote!(
#(#cfgs)*
/// Execution context
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
pub struct #internal_context_name<'a> {
#[doc(hidden)]
__rtic_internal_p: ::core::marker::PhantomData<&'a ()>,
#(#fields,)*
}
#(#cfgs)*
impl<'a> #internal_context_name<'a> {
#[inline(always)]
#[allow(missing_docs)]
pub unsafe fn new(#core) -> Self {
#internal_context_name {
__rtic_internal_p: ::core::marker::PhantomData,
#(#values,)*
}
}
}
));
module_items.push(quote!(
#(#cfgs)*
#[doc(inline)]
pub use super::#internal_context_name as Context;
));
if let Context::SoftwareTask(..) = ctxt {
let spawnee = &app.software_tasks[name];
let priority = spawnee.args.priority;
let cfgs = &spawnee.cfgs;
// Store a copy of the task cfgs
task_cfgs.clone_from(cfgs);
let pend_interrupt = if priority > 0 {
let int_mod = interrupt_mod(app);
let interrupt = &analysis.interrupts.get(&priority).expect("UREACHABLE").0;
quote!(rtic::export::pend(#int_mod::#interrupt);)
} else {
quote!()
};
let internal_spawn_ident = util::internal_task_ident(name, "spawn");
let from_ptr_n_args = util::from_ptr_n_args_ident(spawnee.inputs.len());
let (input_args, input_tupled, input_untupled, input_ty) =
util::regroup_inputs(&spawnee.inputs);
// Spawn caller
items.push(quote!(
#(#cfgs)*
/// Spawns the task directly
#[allow(non_snake_case)]
#[doc(hidden)]
pub fn #internal_spawn_ident(#(#input_args,)*) -> ::core::result::Result<(), #input_ty> {
// SAFETY: If `try_allocate` succeeds one must call `spawn`, which we do.
unsafe {
let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name);
if exec.try_allocate() {
exec.spawn(#name(unsafe { #name::Context::new() } #(,#input_untupled)*));
#pend_interrupt
Ok(())
} else {
Err(#input_tupled)
}
}
}
));
module_items.push(quote!(
#(#cfgs)*
#[doc(inline)]
pub use super::#internal_spawn_ident as spawn;
));
}
if items.is_empty() {
quote!()
} else {
quote!(
#(#items)*
#[allow(non_snake_case)]
#(#task_cfgs)*
#[doc = #doc]
pub mod #name {
#(#module_items)*
}
)
}
}