Skip to content

Commit be74469

Browse files
committed
Enable at least masking out a Monotonic
Simplest case working, but leaves a lot to ask as shown by examples/cfg-monotonic.rs Current `rtic-syntax` is unable to validate and handle the `cfgs[]` which limits the usefulness of this.
1 parent 3240fb3 commit be74469

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

ci/expected/cfg-monotonic.run

Whitespace-only changes.

macros/src/codegen.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,18 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
108108
.map(|(_, monotonic)| {
109109
let name = &monotonic.ident;
110110
let name_str = &name.to_string();
111+
let cfgs = &monotonic.cfgs;
111112
let ident = util::monotonic_ident(name_str);
112113
let doc = &format!(
113114
"This module holds the static implementation for `{}::now()`",
114115
name_str
115116
);
116117

117118
let default_monotonic = if monotonic.args.default {
118-
quote!(pub use #name::now;)
119+
quote!(
120+
#(#cfgs)*
121+
pub use #name::now;
122+
)
119123
} else {
120124
quote!()
121125
};
@@ -125,6 +129,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
125129

126130
#[doc = #doc]
127131
#[allow(non_snake_case)]
132+
#(#cfgs)*
128133
pub mod #name {
129134

130135
/// Read the current time from this monotonic

macros/src/codegen/module.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ pub fn codegen(
116116
.monotonics
117117
.iter()
118118
.map(|(_, monotonic)| {
119+
let cfgs = &monotonic.cfgs;
119120
let mono = &monotonic.ty;
120-
quote! {#mono}
121+
quote! {
122+
#(#cfgs)*
123+
pub #mono
124+
}
121125
})
122126
.collect();
123127

@@ -128,7 +132,7 @@ pub fn codegen(
128132
#[allow(non_snake_case)]
129133
#[allow(non_camel_case_types)]
130134
pub struct #internal_monotonics_ident(
131-
#(pub #monotonic_types),*
135+
#(#monotonic_types),*
132136
);
133137
));
134138

@@ -226,8 +230,8 @@ pub fn codegen(
226230
// Spawn caller
227231
items.push(quote!(
228232

229-
#(#cfgs)*
230233
/// Spawns the task directly
234+
#(#cfgs)*
231235
pub fn #internal_spawn_ident(#(#args,)*) -> Result<(), #ty> {
232236
let input = #tupled;
233237

@@ -267,6 +271,7 @@ pub fn codegen(
267271
let tq = util::tq_ident(&monotonic.ident.to_string());
268272
let t = util::schedule_t_ident();
269273
let m = &monotonic.ident;
274+
let cfgs = &monotonic.cfgs;
270275
let m_ident = util::monotonic_ident(&monotonic_name);
271276
let m_isr = &monotonic.args.binds;
272277
let enum_ = util::interrupt_ident();
@@ -298,13 +303,17 @@ pub fn codegen(
298303

299304
if monotonic.args.default {
300305
module_items.push(quote!(
306+
#(#cfgs)*
301307
pub use #m::spawn_after;
308+
#(#cfgs)*
302309
pub use #m::spawn_at;
310+
#(#cfgs)*
303311
pub use #m::SpawnHandle;
304312
));
305313
}
306314
module_items.push(quote!(
307315
#[doc(hidden)]
316+
#(#cfgs)*
308317
pub mod #m {
309318
pub use super::super::#internal_spawn_after_ident as spawn_after;
310319
pub use super::super::#internal_spawn_at_ident as spawn_at;
@@ -322,6 +331,7 @@ pub fn codegen(
322331
marker: u32,
323332
}
324333

334+
#(#cfgs)*
325335
impl core::fmt::Debug for #internal_spawn_handle_ident {
326336
#[doc(hidden)]
327337
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
@@ -353,6 +363,7 @@ pub fn codegen(
353363

354364
/// Reschedule after
355365
#[inline]
366+
#(#cfgs)*
356367
pub fn reschedule_after(
357368
self,
358369
duration: <#m as rtic::Monotonic>::Duration
@@ -361,6 +372,7 @@ pub fn codegen(
361372
}
362373

363374
/// Reschedule at
375+
#(#cfgs)*
364376
pub fn reschedule_at(
365377
self,
366378
instant: <#m as rtic::Monotonic>::Instant
@@ -376,11 +388,11 @@ pub fn codegen(
376388
}
377389
}
378390

379-
#(#cfgs)*
380391
/// Spawns the task after a set duration relative to the current time
381392
///
382393
/// This will use the time `Instant::new(0)` as baseline if called in `#[init]`,
383394
/// so if you use a non-resetable timer use `spawn_at` when in `#[init]`
395+
#(#cfgs)*
384396
#[allow(non_snake_case)]
385397
pub fn #internal_spawn_after_ident(
386398
duration: <#m as rtic::Monotonic>::Duration

macros/src/codegen/post_init.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,28 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
4343
}
4444
}
4545

46-
for (i, (monotonic, _)) in app.monotonics.iter().enumerate() {
46+
for (i, (monotonic_ident, monotonic)) in app.monotonics.iter().enumerate() {
4747
// For future use
4848
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
4949
// stmts.push(quote!(#[doc = #doc]));
50+
let cfgs = &monotonic.cfgs;
5051

5152
#[allow(clippy::cast_possible_truncation)]
5253
let idx = Index {
5354
index: i as u32,
5455
span: Span::call_site(),
5556
};
56-
stmts.push(quote!(monotonics.#idx.reset();));
57+
stmts.push(quote!(
58+
#(#cfgs)*
59+
monotonics.#idx.reset();
60+
));
5761

5862
// Store the monotonic
59-
let name = util::monotonic_ident(&monotonic.to_string());
60-
stmts.push(quote!(#name.get_mut().write(Some(monotonics.#idx));));
63+
let name = util::monotonic_ident(&monotonic_ident.to_string());
64+
stmts.push(quote!(
65+
#(#cfgs)*
66+
#name.get_mut().write(Some(monotonics.#idx));
67+
));
6168
}
6269

6370
// Enable the interrupts -- this completes the `init`-ialization phase

macros/src/codegen/software_tasks.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub fn codegen(
6262
for (_, monotonic) in &app.monotonics {
6363
let instants = util::monotonic_instants_ident(name, &monotonic.ident);
6464
let mono_type = &monotonic.ty;
65+
let cfgs = &monotonic.cfgs;
6566

6667
let uninit = mk_uninit();
6768
// For future use
@@ -73,6 +74,7 @@ pub fn codegen(
7374
#[allow(non_camel_case_types)]
7475
#[allow(non_upper_case_globals)]
7576
#[doc(hidden)]
77+
#(#cfgs)*
7678
static #instants:
7779
rtic::RacyCell<[core::mem::MaybeUninit<<#mono_type as rtic::Monotonic>::Instant>; #cap_lit]> =
7880
rtic::RacyCell::new([#(#elems,)*]);

macros/src/codegen/timer_queue.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
1313
// Generate the marker counter used to track for `cancel` and `reschedule`
1414
let tq_marker = util::timer_queue_marker_ident();
1515
items.push(quote!(
16-
// #[doc = #doc]
1716
#[doc(hidden)]
1817
#[allow(non_camel_case_types)]
1918
#[allow(non_upper_case_globals)]
@@ -56,6 +55,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
5655
let tq = util::tq_ident(&monotonic_name);
5756
let t = util::schedule_t_ident();
5857
let mono_type = &monotonic.ty;
58+
let cfgs = &monotonic.cfgs;
5959
let m_ident = util::monotonic_ident(&monotonic_name);
6060

6161
// Static variables and resource proxy
@@ -76,6 +76,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
7676
#[doc(hidden)]
7777
#[allow(non_camel_case_types)]
7878
#[allow(non_upper_case_globals)]
79+
#(#cfgs)*
7980
static #tq: rtic::RacyCell<#tq_ty> =
8081
rtic::RacyCell::new(rtic::export::TimerQueue(rtic::export::SortedLinkedList::new_u16()));
8182
));
@@ -88,6 +89,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
8889
#[doc(hidden)]
8990
#[allow(non_camel_case_types)]
9091
#[allow(non_upper_case_globals)]
92+
#(#cfgs)*
9193
static #mono: rtic::RacyCell<Option<#mono_type>> = rtic::RacyCell::new(None);
9294
));
9395
}
@@ -126,6 +128,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
126128
})
127129
.collect::<Vec<_>>();
128130

131+
let cfgs = &monotonic.cfgs;
129132
let bound_interrupt = &monotonic.args.binds;
130133
let disable_isr = if &*bound_interrupt.to_string() == "SysTick" {
131134
quote!(core::mem::transmute::<_, rtic::export::SYST>(()).disable_interrupt())
@@ -136,6 +139,7 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
136139
items.push(quote!(
137140
#[no_mangle]
138141
#[allow(non_snake_case)]
142+
#(#cfgs)*
139143
unsafe fn #bound_interrupt() {
140144
while let Some((task, index)) = rtic::export::interrupt::free(|_|
141145
if let Some(mono) = (&mut *#m_ident.get_mut()).as_mut() {

0 commit comments

Comments
 (0)