2018-12-02 17:35:26 +01:00
|
|
|
#![deny(warnings)]
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use std::{
|
2019-01-16 07:42:50 +01:00
|
|
|
collections::{BTreeMap, HashMap},
|
2019-02-11 00:25:33 +01:00
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
2018-11-03 17:02:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
use proc_macro2::Span;
|
|
|
|
use quote::quote;
|
|
|
|
use rand::{Rng, SeedableRng};
|
2018-12-16 18:37:36 +01:00
|
|
|
use syn::{parse_quote, ArgCaptured, Attribute, Ident, IntSuffix, LitInt};
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-16 19:13:22 +01:00
|
|
|
use crate::{
|
|
|
|
analyze::{Analysis, Ownership},
|
|
|
|
syntax::{App, Idents, Static},
|
|
|
|
};
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
// NOTE to avoid polluting the user namespaces we map some identifiers to pseudo-hygienic names.
|
|
|
|
// In some instances we also use the pseudo-hygienic names for safety, for example the user should
|
|
|
|
// not modify the priority field of resources.
|
2019-01-16 07:42:50 +01:00
|
|
|
type Aliases = BTreeMap<Ident, Ident>;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
struct Context {
|
|
|
|
// Alias
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
baseline: Ident,
|
2019-01-16 07:42:50 +01:00
|
|
|
dispatchers: BTreeMap<u8, Dispatcher>,
|
2018-11-03 17:02:41 +01:00
|
|
|
// Alias (`fn`)
|
|
|
|
idle: Ident,
|
|
|
|
// Alias (`fn`)
|
|
|
|
init: Ident,
|
|
|
|
// Alias
|
|
|
|
priority: Ident,
|
|
|
|
// For non-singletons this maps the resource name to its `static mut` variable name
|
|
|
|
statics: Aliases,
|
|
|
|
/// Task -> Alias (`struct`)
|
|
|
|
resources: HashMap<Kind, Resources>,
|
|
|
|
// Alias (`enum`)
|
|
|
|
schedule_enum: Ident,
|
|
|
|
// Task -> Alias (`fn`)
|
|
|
|
schedule_fn: Aliases,
|
2019-01-16 07:42:50 +01:00
|
|
|
tasks: BTreeMap<Ident, Task>,
|
2018-11-03 17:02:41 +01:00
|
|
|
// Alias (`struct` / `static mut`)
|
|
|
|
timer_queue: Ident,
|
2019-01-17 02:36:55 +01:00
|
|
|
// Generator of Ident names or suffixes
|
|
|
|
ident_gen: IdentGenerator,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2018-12-15 19:17:08 +01:00
|
|
|
struct Dispatcher {
|
|
|
|
enum_: Ident,
|
|
|
|
ready_queue: Ident,
|
|
|
|
}
|
|
|
|
|
2018-12-15 19:07:09 +01:00
|
|
|
struct Task {
|
|
|
|
alias: Ident,
|
|
|
|
free_queue: Ident,
|
|
|
|
inputs: Ident,
|
|
|
|
spawn_fn: Ident,
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
scheduleds: Ident,
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
impl Default for Context {
|
|
|
|
fn default() -> Self {
|
2019-01-17 02:36:55 +01:00
|
|
|
let mut ident_gen = IdentGenerator::new();
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
Context {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
2019-02-16 00:22:00 +01:00
|
|
|
baseline: ident_gen.mk_ident(None, false),
|
2019-01-16 07:42:50 +01:00
|
|
|
dispatchers: BTreeMap::new(),
|
2019-02-16 00:22:00 +01:00
|
|
|
idle: ident_gen.mk_ident(Some("idle"), false),
|
|
|
|
init: ident_gen.mk_ident(Some("init"), false),
|
|
|
|
priority: ident_gen.mk_ident(None, false),
|
2018-11-03 17:02:41 +01:00
|
|
|
statics: Aliases::new(),
|
|
|
|
resources: HashMap::new(),
|
2019-02-16 00:22:00 +01:00
|
|
|
schedule_enum: ident_gen.mk_ident(None, false),
|
2018-11-03 17:02:41 +01:00
|
|
|
schedule_fn: Aliases::new(),
|
2019-01-16 07:42:50 +01:00
|
|
|
tasks: BTreeMap::new(),
|
2019-02-16 00:22:00 +01:00
|
|
|
timer_queue: ident_gen.mk_ident(None, false),
|
2019-01-17 02:36:55 +01:00
|
|
|
ident_gen,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Resources {
|
|
|
|
alias: Ident,
|
|
|
|
decl: proc_macro2::TokenStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn app(app: &App, analysis: &Analysis) -> TokenStream {
|
|
|
|
let mut ctxt = Context::default();
|
|
|
|
|
|
|
|
let resources = resources(&mut ctxt, &app, analysis);
|
|
|
|
|
|
|
|
let tasks = tasks(&mut ctxt, &app, analysis);
|
|
|
|
|
|
|
|
let (dispatchers_data, dispatchers) = dispatchers(&mut ctxt, &app, analysis);
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
let (init_fn, has_late_resources) = init(&mut ctxt, &app, analysis);
|
2018-11-03 17:02:41 +01:00
|
|
|
let init_arg = if cfg!(feature = "timer-queue") {
|
|
|
|
quote!(rtfm::Peripherals {
|
|
|
|
CBP: p.CBP,
|
|
|
|
CPUID: p.CPUID,
|
|
|
|
DCB: &mut p.DCB,
|
|
|
|
FPB: p.FPB,
|
|
|
|
FPU: p.FPU,
|
|
|
|
ITM: p.ITM,
|
|
|
|
MPU: p.MPU,
|
|
|
|
SCB: &mut p.SCB,
|
|
|
|
TPIU: p.TPIU,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
quote!(rtfm::Peripherals {
|
|
|
|
CBP: p.CBP,
|
|
|
|
CPUID: p.CPUID,
|
|
|
|
DCB: p.DCB,
|
|
|
|
DWT: p.DWT,
|
|
|
|
FPB: p.FPB,
|
|
|
|
FPU: p.FPU,
|
|
|
|
ITM: p.ITM,
|
|
|
|
MPU: p.MPU,
|
|
|
|
SCB: &mut p.SCB,
|
|
|
|
SYST: p.SYST,
|
|
|
|
TPIU: p.TPIU,
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
let init = &ctxt.init;
|
|
|
|
let init_phase = if has_late_resources {
|
|
|
|
let assigns = app
|
|
|
|
.resources
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(name, res)| {
|
|
|
|
if res.expr.is_none() {
|
|
|
|
let alias = &ctxt.statics[name];
|
|
|
|
|
|
|
|
Some(quote!(#alias.set(res.#name);))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
let res = #init(#init_arg);
|
|
|
|
#(#assigns)*
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
quote!(#init(#init_arg);)
|
|
|
|
};
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
let post_init = post_init(&ctxt, &app, analysis);
|
|
|
|
|
|
|
|
let (idle_fn, idle_expr) = idle(&mut ctxt, &app, analysis);
|
|
|
|
|
|
|
|
let exceptions = exceptions(&mut ctxt, app, analysis);
|
|
|
|
|
|
|
|
let (root_interrupts, scoped_interrupts) = interrupts(&mut ctxt, app, analysis);
|
|
|
|
|
|
|
|
let spawn = spawn(&mut ctxt, app, analysis);
|
|
|
|
|
|
|
|
let schedule = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => schedule(&ctxt, app),
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
2019-01-17 02:36:55 +01:00
|
|
|
let timer_queue = timer_queue(&mut ctxt, app, analysis);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-15 20:34:33 +01:00
|
|
|
let pre_init = pre_init(&ctxt, &app, analysis);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
let assertions = assertions(app, analysis);
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let main = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
quote!(
|
|
|
|
#resources
|
|
|
|
|
|
|
|
#spawn
|
|
|
|
|
|
|
|
#timer_queue
|
|
|
|
|
|
|
|
#schedule
|
|
|
|
|
|
|
|
#dispatchers_data
|
|
|
|
|
|
|
|
#(#exceptions)*
|
|
|
|
|
|
|
|
#root_interrupts
|
|
|
|
|
|
|
|
const APP: () = {
|
|
|
|
#scoped_interrupts
|
|
|
|
|
|
|
|
#(#dispatchers)*
|
|
|
|
};
|
|
|
|
|
|
|
|
#(#tasks)*
|
|
|
|
|
|
|
|
#init_fn
|
|
|
|
|
|
|
|
#idle_fn
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
#[export_name = "main"]
|
2018-11-03 17:02:41 +01:00
|
|
|
#[allow(unsafe_code)]
|
|
|
|
#[doc(hidden)]
|
2018-12-16 18:37:36 +01:00
|
|
|
unsafe fn #main() -> ! {
|
2018-11-03 17:02:41 +01:00
|
|
|
#assertions
|
|
|
|
|
|
|
|
rtfm::export::interrupt::disable();
|
|
|
|
|
|
|
|
#pre_init
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
#init_phase
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#post_init
|
|
|
|
|
|
|
|
rtfm::export::interrupt::enable();
|
|
|
|
|
|
|
|
#idle_expr
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resources(ctxt: &mut Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
let mut module = vec![];
|
|
|
|
for (name, res) in &app.resources {
|
2018-12-16 18:37:36 +01:00
|
|
|
let cfgs = &res.cfgs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let attrs = &res.attrs;
|
|
|
|
let mut_ = &res.mutability;
|
|
|
|
let ty = &res.ty;
|
|
|
|
let expr = &res.expr;
|
|
|
|
|
|
|
|
if res.singleton {
|
|
|
|
items.push(quote!(
|
|
|
|
#(#attrs)*
|
|
|
|
pub static #mut_ #name: #ty = #expr;
|
|
|
|
));
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, true); // XXX is randomness required?
|
2018-11-03 17:02:41 +01:00
|
|
|
if let Some(Ownership::Shared { ceiling }) = analysis.ownerships.get(name) {
|
|
|
|
items.push(mk_resource(
|
|
|
|
ctxt,
|
2018-12-16 18:37:36 +01:00
|
|
|
cfgs,
|
2018-11-03 17:02:41 +01:00
|
|
|
name,
|
|
|
|
quote!(#name),
|
|
|
|
*ceiling,
|
|
|
|
quote!(&mut <#name as owned_singleton::Singleton>::new()),
|
|
|
|
app,
|
|
|
|
Some(&mut module),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt.statics.insert(name.clone(), alias);
|
|
|
|
} else {
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
let symbol = format!("{}::{}", name, alias);
|
|
|
|
|
|
|
|
items.push(
|
|
|
|
expr.as_ref()
|
|
|
|
.map(|expr| {
|
|
|
|
quote!(
|
|
|
|
#(#attrs)*
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-12-02 17:35:26 +01:00
|
|
|
#[doc = #symbol]
|
2018-11-03 17:02:41 +01:00
|
|
|
static mut #alias: #ty = #expr;
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
quote!(
|
|
|
|
#(#attrs)*
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-12-02 17:35:26 +01:00
|
|
|
#[doc = #symbol]
|
2018-11-03 17:02:41 +01:00
|
|
|
static mut #alias: rtfm::export::MaybeUninit<#ty> =
|
|
|
|
rtfm::export::MaybeUninit::uninitialized();
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(Ownership::Shared { ceiling }) = analysis.ownerships.get(name) {
|
|
|
|
if res.mutability.is_some() {
|
|
|
|
let ptr = if res.expr.is_none() {
|
2019-02-15 19:52:25 +01:00
|
|
|
quote!(unsafe { &mut *#alias.as_mut_ptr() })
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
|
|
|
quote!(unsafe { &mut #alias })
|
|
|
|
};
|
|
|
|
|
|
|
|
items.push(mk_resource(
|
|
|
|
ctxt,
|
2018-12-16 18:37:36 +01:00
|
|
|
cfgs,
|
2018-11-03 17:02:41 +01:00
|
|
|
name,
|
|
|
|
quote!(#ty),
|
|
|
|
*ceiling,
|
|
|
|
ptr,
|
|
|
|
app,
|
|
|
|
Some(&mut module),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt.statics.insert(name.clone(), alias);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !module.is_empty() {
|
|
|
|
items.push(quote!(
|
|
|
|
/// Resource proxies
|
|
|
|
pub mod resources {
|
|
|
|
#(#module)*
|
2018-12-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
fn init(ctxt: &mut Context, app: &App, analysis: &Analysis) -> (proc_macro2::TokenStream, bool) {
|
2018-11-03 17:02:41 +01:00
|
|
|
let attrs = &app.init.attrs;
|
|
|
|
let locals = mk_locals(&app.init.statics, true);
|
|
|
|
let stmts = &app.init.stmts;
|
2019-02-12 14:53:49 +01:00
|
|
|
// TODO remove in v0.5.x
|
2018-11-03 17:02:41 +01:00
|
|
|
let assigns = app
|
|
|
|
.init
|
|
|
|
.assigns
|
|
|
|
.iter()
|
|
|
|
.map(|assign| {
|
2018-12-16 18:37:36 +01:00
|
|
|
let attrs = &assign.attrs;
|
2018-11-03 17:02:41 +01:00
|
|
|
if app
|
|
|
|
.resources
|
|
|
|
.get(&assign.left)
|
|
|
|
.map(|r| r.expr.is_none())
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
let alias = &ctxt.statics[&assign.left];
|
|
|
|
let expr = &assign.right;
|
2018-12-16 18:37:36 +01:00
|
|
|
quote!(
|
|
|
|
#(#attrs)*
|
|
|
|
unsafe { #alias.set(#expr); }
|
|
|
|
)
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
|
|
|
let left = &assign.left;
|
|
|
|
let right = &assign.right;
|
2018-12-16 18:37:36 +01:00
|
|
|
quote!(
|
|
|
|
#(#attrs)*
|
|
|
|
#left = #right;
|
|
|
|
)
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let prelude = prelude(
|
|
|
|
ctxt,
|
|
|
|
Kind::Init,
|
|
|
|
&app.init.args.resources,
|
|
|
|
&app.init.args.spawn,
|
|
|
|
&app.init.args.schedule,
|
|
|
|
app,
|
|
|
|
255,
|
|
|
|
analysis,
|
|
|
|
);
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
let (late_resources, late_resources_ident, ret) = if app.init.returns_late_resources {
|
|
|
|
// create `LateResources` struct in the root of the crate
|
2019-02-16 00:28:12 +01:00
|
|
|
let ident = ctxt.ident_gen.mk_ident(None, false);
|
2019-02-12 14:53:49 +01:00
|
|
|
|
|
|
|
let fields = app
|
|
|
|
.resources
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(name, res)| {
|
|
|
|
if res.expr.is_none() {
|
|
|
|
let ty = &res.ty;
|
|
|
|
Some(quote!(pub #name: #ty))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let late_resources = quote!(
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub struct #ident {
|
|
|
|
#(#fields),*
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
(
|
|
|
|
Some(late_resources),
|
|
|
|
Some(ident),
|
|
|
|
Some(quote!(-> init::LateResources)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(None, None, None)
|
|
|
|
};
|
|
|
|
let has_late_resources = late_resources.is_some();
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
let module = module(
|
|
|
|
ctxt,
|
|
|
|
Kind::Init,
|
|
|
|
!app.init.args.schedule.is_empty(),
|
|
|
|
!app.init.args.spawn.is_empty(),
|
|
|
|
app,
|
2019-02-12 14:53:49 +01:00
|
|
|
late_resources_ident,
|
2018-11-03 17:02:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
let baseline = &ctxt.baseline;
|
|
|
|
let baseline_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
2019-02-16 00:22:00 +01:00
|
|
|
() => quote!(let ref #baseline = rtfm::Instant::artificial(0);),
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let start_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => quote!(
|
|
|
|
#[allow(unused_variables)]
|
2019-02-16 00:22:00 +01:00
|
|
|
let start = *#baseline;
|
2018-11-03 17:02:41 +01:00
|
|
|
),
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let unsafety = &app.init.unsafety;
|
|
|
|
let device = &app.args.device;
|
|
|
|
let init = &ctxt.init;
|
2019-02-12 14:53:49 +01:00
|
|
|
(
|
|
|
|
quote!(
|
|
|
|
#late_resources
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
#module
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
// unsafe trampoline to deter end-users from calling this non-reentrant function
|
2019-02-12 14:53:49 +01:00
|
|
|
#(#attrs)*
|
2019-02-16 00:22:00 +01:00
|
|
|
unsafe fn #init(core: rtfm::Peripherals) #ret {
|
|
|
|
#[inline(always)]
|
|
|
|
#unsafety fn init(mut core: rtfm::Peripherals) #ret {
|
|
|
|
#(#locals)*
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#baseline_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#prelude
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let mut device = unsafe { #device::Peripherals::steal() };
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#start_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#(#stmts)*
|
|
|
|
|
|
|
|
#(#assigns)*
|
|
|
|
}
|
2019-02-12 14:53:49 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
init(core)
|
2019-02-12 14:53:49 +01:00
|
|
|
}
|
|
|
|
),
|
|
|
|
has_late_resources,
|
2018-11-03 17:02:41 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn post_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
|
|
|
let mut exprs = vec![];
|
|
|
|
|
|
|
|
// TODO turn the assertions that check that the priority is not larger than what's supported by
|
|
|
|
// the device into compile errors
|
|
|
|
let device = &app.args.device;
|
|
|
|
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
|
|
|
|
for (name, exception) in &app.exceptions {
|
|
|
|
let priority = exception.args.priority;
|
|
|
|
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits))));
|
|
|
|
exprs.push(quote!(p.SCB.set_priority(
|
|
|
|
rtfm::export::SystemHandler::#name,
|
|
|
|
((1 << #nvic_prio_bits) - #priority) << (8 - #nvic_prio_bits),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !analysis.timer_queue.tasks.is_empty() {
|
|
|
|
let priority = analysis.timer_queue.priority;
|
|
|
|
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits))));
|
|
|
|
exprs.push(quote!(p.SCB.set_priority(
|
|
|
|
rtfm::export::SystemHandler::SysTick,
|
|
|
|
((1 << #nvic_prio_bits) - #priority) << (8 - #nvic_prio_bits),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.idle.is_none() {
|
|
|
|
// Set SLEEPONEXIT bit to enter sleep mode when returning from ISR
|
|
|
|
exprs.push(quote!(p.SCB.scr.modify(|r| r | 1 << 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enable and start the system timer
|
|
|
|
if !analysis.timer_queue.tasks.is_empty() {
|
|
|
|
let tq = &ctxt.timer_queue;
|
2019-02-15 19:52:25 +01:00
|
|
|
exprs.push(
|
|
|
|
quote!((*#tq.as_mut_ptr()).syst.set_clock_source(rtfm::export::SystClkSource::Core)),
|
|
|
|
);
|
|
|
|
exprs.push(quote!((*#tq.as_mut_ptr()).syst.enable_counter()));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable cycle counter
|
|
|
|
if cfg!(feature = "timer-queue") {
|
|
|
|
exprs.push(quote!(p.DCB.enable_trace()));
|
|
|
|
exprs.push(quote!(p.DWT.enable_cycle_counter()));
|
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#exprs;)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function creates creates a module for `init` / `idle` / a `task` (see `kind` argument)
|
|
|
|
fn module(
|
|
|
|
ctxt: &mut Context,
|
|
|
|
kind: Kind,
|
|
|
|
schedule: bool,
|
|
|
|
spawn: bool,
|
|
|
|
app: &App,
|
2019-02-12 14:53:49 +01:00
|
|
|
late_resources: Option<Ident>,
|
2018-11-03 17:02:41 +01:00
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
let mut fields = vec![];
|
|
|
|
|
|
|
|
let name = kind.ident();
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
let device = &app.args.device;
|
|
|
|
|
|
|
|
let mut lt = None;
|
|
|
|
match kind {
|
|
|
|
Kind::Init => {
|
|
|
|
if cfg!(feature = "timer-queue") {
|
|
|
|
fields.push(quote!(
|
|
|
|
/// System start time = `Instant(0 /* cycles */)`
|
|
|
|
pub start: rtfm::Instant,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
fields.push(quote!(
|
|
|
|
/// Core (Cortex-M) peripherals
|
|
|
|
pub core: rtfm::Peripherals<'a>,
|
|
|
|
/// Device specific peripherals
|
|
|
|
pub device: #device::Peripherals,
|
|
|
|
));
|
|
|
|
lt = Some(quote!('a));
|
|
|
|
}
|
|
|
|
Kind::Idle => {}
|
|
|
|
Kind::Exception(_) | Kind::Interrupt(_) => {
|
|
|
|
if cfg!(feature = "timer-queue") {
|
|
|
|
fields.push(quote!(
|
|
|
|
/// Time at which this handler started executing
|
|
|
|
pub start: rtfm::Instant,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Task(_) => {
|
|
|
|
if cfg!(feature = "timer-queue") {
|
|
|
|
fields.push(quote!(
|
|
|
|
/// The time at which this task was scheduled to run
|
|
|
|
pub scheduled: rtfm::Instant,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if schedule {
|
|
|
|
lt = Some(quote!('a));
|
|
|
|
|
|
|
|
fields.push(quote!(
|
|
|
|
/// Tasks that can be scheduled from this context
|
|
|
|
pub schedule: Schedule<'a>,
|
|
|
|
));
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
/// Tasks that can be scheduled from this context
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Schedule<'a> {
|
|
|
|
#[doc(hidden)]
|
2019-02-16 00:22:00 +01:00
|
|
|
pub #priority: &'a rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if spawn {
|
|
|
|
lt = Some(quote!('a));
|
|
|
|
|
|
|
|
fields.push(quote!(
|
|
|
|
/// Tasks that can be spawned from this context
|
|
|
|
pub spawn: Spawn<'a>,
|
|
|
|
));
|
|
|
|
|
|
|
|
if kind.is_idle() {
|
|
|
|
items.push(quote!(
|
|
|
|
/// Tasks that can be spawned from this context
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Spawn<'a> {
|
|
|
|
#[doc(hidden)]
|
2019-02-16 00:22:00 +01:00
|
|
|
pub #priority: &'a rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
let baseline_field = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
let baseline = &ctxt.baseline;
|
|
|
|
quote!(
|
2019-02-16 00:22:00 +01:00
|
|
|
// NOTE this field is visible so we use a shared reference to make it
|
|
|
|
// immutable
|
2018-11-03 17:02:41 +01:00
|
|
|
#[doc(hidden)]
|
2019-02-16 00:22:00 +01:00
|
|
|
pub #baseline: &'a rtfm::Instant,
|
2018-11-03 17:02:41 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
/// Tasks that can be spawned from this context
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Spawn<'a> {
|
|
|
|
#baseline_field
|
|
|
|
#[doc(hidden)]
|
2019-02-16 00:22:00 +01:00
|
|
|
pub #priority: &'a rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut root = None;
|
|
|
|
if let Some(resources) = ctxt.resources.get(&kind) {
|
|
|
|
lt = Some(quote!('a));
|
|
|
|
|
|
|
|
root = Some(resources.decl.clone());
|
|
|
|
|
|
|
|
let alias = &resources.alias;
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use super::#alias as Resources;
|
|
|
|
));
|
|
|
|
|
|
|
|
fields.push(quote!(
|
|
|
|
/// Resources available in this context
|
|
|
|
pub resources: Resources<'a>,
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
let doc = match kind {
|
|
|
|
Kind::Exception(_) => "Exception handler",
|
|
|
|
Kind::Idle => "Idle loop",
|
|
|
|
Kind::Init => "Initialization function",
|
|
|
|
Kind::Interrupt(_) => "Interrupt handler",
|
|
|
|
Kind::Task(_) => "Software task",
|
|
|
|
};
|
|
|
|
|
2019-02-12 14:53:49 +01:00
|
|
|
if let Some(late_resources) = late_resources {
|
|
|
|
items.push(quote!(
|
|
|
|
pub use super::#late_resources as LateResources;
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
quote!(
|
|
|
|
#root
|
|
|
|
|
|
|
|
#[doc = #doc]
|
2019-01-17 19:55:21 +01:00
|
|
|
#[allow(non_snake_case)]
|
2018-11-03 17:02:41 +01:00
|
|
|
pub mod #name {
|
|
|
|
/// Variables injected into this context by the `app` attribute
|
|
|
|
pub struct Context<#lt> {
|
|
|
|
#(#fields)*
|
|
|
|
}
|
|
|
|
|
|
|
|
#(#items)*
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The prelude injects `resources`, `spawn`, `schedule` and `start` / `scheduled` (all values) into
|
|
|
|
/// a function scope
|
|
|
|
fn prelude(
|
|
|
|
ctxt: &mut Context,
|
|
|
|
kind: Kind,
|
|
|
|
resources: &Idents,
|
|
|
|
spawn: &Idents,
|
|
|
|
schedule: &Idents,
|
|
|
|
app: &App,
|
|
|
|
logical_prio: u8,
|
|
|
|
analysis: &Analysis,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
let lt = if kind.runs_once() {
|
|
|
|
quote!('static)
|
|
|
|
} else {
|
|
|
|
quote!('a)
|
|
|
|
};
|
|
|
|
|
|
|
|
let module = kind.ident();
|
|
|
|
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
if !resources.is_empty() {
|
|
|
|
let mut defs = vec![];
|
|
|
|
let mut exprs = vec![];
|
|
|
|
|
|
|
|
// NOTE This field is just to avoid unused type parameter errors around `'a`
|
2019-02-16 00:22:00 +01:00
|
|
|
defs.push(quote!(#[allow(dead_code)] pub #priority: &'a rtfm::export::Priority));
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(parse_quote!(#priority));
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
let mut may_call_lock = false;
|
|
|
|
let mut needs_unsafe = false;
|
|
|
|
for name in resources {
|
|
|
|
let res = &app.resources[name];
|
2018-12-16 18:37:36 +01:00
|
|
|
let cfgs = &res.cfgs;
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
let initialized = res.expr.is_some();
|
|
|
|
let singleton = res.singleton;
|
|
|
|
let mut_ = res.mutability;
|
|
|
|
let ty = &res.ty;
|
|
|
|
|
|
|
|
if kind.is_init() {
|
|
|
|
let mut force_mut = false;
|
|
|
|
if !analysis.ownerships.contains_key(name) {
|
|
|
|
// owned by Init
|
|
|
|
if singleton {
|
|
|
|
needs_unsafe = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: #name
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: <#name as owned_singleton::Singleton>::new()
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'static #mut_ #ty
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// owned by someone else
|
|
|
|
if singleton {
|
|
|
|
needs_unsafe = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'a mut #name
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &mut <#name as owned_singleton::Singleton>::new()
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
force_mut = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'a mut #ty
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let alias = &ctxt.statics[name];
|
|
|
|
// Resources assigned to init are always const initialized
|
|
|
|
needs_unsafe = true;
|
|
|
|
if force_mut {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &mut #alias
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &#mut_ #alias
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let ownership = &analysis.ownerships[name];
|
2018-11-04 18:50:42 +01:00
|
|
|
let mut exclusive = false;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
if ownership.needs_lock(logical_prio) {
|
|
|
|
may_call_lock = true;
|
|
|
|
if singleton {
|
|
|
|
if mut_.is_none() {
|
|
|
|
needs_unsafe = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'a #name
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &<#name as owned_singleton::Singleton>::new()
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// Generate a resource proxy
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: resources::#name<'a>
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: resources::#name { #priority }
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if mut_.is_none() {
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'a #ty
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
|
|
|
// Generate a resource proxy
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: resources::#name<'a>
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: resources::#name { #priority }
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if singleton {
|
|
|
|
if kind.runs_once() {
|
|
|
|
needs_unsafe = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: #name
|
|
|
|
));
|
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: <#name as owned_singleton::Singleton>::new()
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
|
|
|
needs_unsafe = true;
|
2018-11-04 19:46:49 +01:00
|
|
|
if ownership.is_owned() || mut_.is_none() {
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &'a #mut_ #name
|
|
|
|
));
|
2019-02-16 00:22:00 +01:00
|
|
|
// XXX is randomness required?
|
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, true);
|
2018-11-04 19:46:49 +01:00
|
|
|
items.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-04 19:46:49 +01:00
|
|
|
let #mut_ #alias = unsafe {
|
|
|
|
<#name as owned_singleton::Singleton>::new()
|
|
|
|
};
|
2018-11-04 18:50:42 +01:00
|
|
|
));
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &#mut_ #alias
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
} else {
|
|
|
|
may_call_lock = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: rtfm::Exclusive<'a, #name>
|
|
|
|
));
|
2019-02-16 00:22:00 +01:00
|
|
|
// XXX is randomness required?
|
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, true);
|
2018-11-04 19:46:49 +01:00
|
|
|
items.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-04 19:46:49 +01:00
|
|
|
let #mut_ #alias = unsafe {
|
|
|
|
<#name as owned_singleton::Singleton>::new()
|
|
|
|
};
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
exprs.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-04 19:46:49 +01:00
|
|
|
#name: rtfm::Exclusive(&mut #alias)
|
2018-11-04 18:50:42 +01:00
|
|
|
));
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
2018-11-04 18:50:42 +01:00
|
|
|
if ownership.is_owned() || mut_.is_none() {
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: &#lt #mut_ #ty
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
} else {
|
|
|
|
exclusive = true;
|
|
|
|
may_call_lock = true;
|
2018-12-16 18:37:36 +01:00
|
|
|
defs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
pub #name: rtfm::Exclusive<#lt, #ty>
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let alias = &ctxt.statics[name];
|
|
|
|
needs_unsafe = true;
|
|
|
|
if initialized {
|
2018-11-04 18:50:42 +01:00
|
|
|
if exclusive {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: rtfm::Exclusive(&mut #alias)
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
} else {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#name: &#mut_ #alias
|
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
2019-02-15 19:52:25 +01:00
|
|
|
let expr = if mut_.is_some() {
|
|
|
|
quote!(&mut *#alias.as_mut_ptr())
|
2018-11-03 17:02:41 +01:00
|
|
|
} else {
|
2019-02-15 19:52:25 +01:00
|
|
|
quote!(&*#alias.as_ptr())
|
2018-11-03 17:02:41 +01:00
|
|
|
};
|
2018-11-04 18:50:42 +01:00
|
|
|
|
|
|
|
if exclusive {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
2019-02-15 19:52:25 +01:00
|
|
|
#name: rtfm::Exclusive(#expr)
|
2018-12-16 18:37:36 +01:00
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
} else {
|
2018-12-16 18:37:36 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
#(#cfgs)*
|
2019-02-15 19:52:25 +01:00
|
|
|
#name: #expr
|
2018-12-16 18:37:36 +01:00
|
|
|
));
|
2018-11-04 18:50:42 +01:00
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
let unsafety = if needs_unsafe {
|
|
|
|
Some(quote!(unsafe))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
let defs = &defs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let doc = format!("`{}::Resources`", kind.ident().to_string());
|
|
|
|
let decl = quote!(
|
|
|
|
#[doc = #doc]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub struct #alias<'a> { #(#defs,)* }
|
|
|
|
);
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut resources = #unsafety { #alias { #(#exprs,)* } };
|
|
|
|
));
|
|
|
|
|
|
|
|
ctxt.resources
|
|
|
|
.insert(kind.clone(), Resources { alias, decl });
|
|
|
|
|
|
|
|
if may_call_lock {
|
|
|
|
items.push(quote!(
|
|
|
|
use rtfm::Mutex;
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !spawn.is_empty() {
|
|
|
|
if kind.is_idle() {
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
let spawn = #module::Spawn { #priority };
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
let baseline_expr = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
let baseline = &ctxt.baseline;
|
|
|
|
quote!(#baseline)
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
let spawn = #module::Spawn { #priority, #baseline_expr };
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !schedule.is_empty() {
|
|
|
|
// Populate `schedule_fn`
|
|
|
|
for task in schedule {
|
|
|
|
if ctxt.schedule_fn.contains_key(task) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
ctxt.schedule_fn
|
|
|
|
.insert(task.clone(), ctxt.ident_gen.mk_ident(None, false));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use rtfm::U32Ext;
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
let schedule = #module::Schedule { #priority };
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if items.is_empty() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
quote!(
|
2019-02-16 00:22:00 +01:00
|
|
|
let ref #priority = unsafe { rtfm::export::Priority::new(#logical_prio) };
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#(#items)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn idle(
|
|
|
|
ctxt: &mut Context,
|
|
|
|
app: &App,
|
|
|
|
analysis: &Analysis,
|
|
|
|
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
|
|
|
|
if let Some(idle) = app.idle.as_ref() {
|
|
|
|
let attrs = &idle.attrs;
|
|
|
|
let locals = mk_locals(&idle.statics, true);
|
|
|
|
let stmts = &idle.stmts;
|
|
|
|
|
|
|
|
let prelude = prelude(
|
|
|
|
ctxt,
|
|
|
|
Kind::Idle,
|
|
|
|
&idle.args.resources,
|
|
|
|
&idle.args.spawn,
|
|
|
|
&idle.args.schedule,
|
|
|
|
app,
|
|
|
|
0,
|
|
|
|
analysis,
|
|
|
|
);
|
|
|
|
|
|
|
|
let module = module(
|
|
|
|
ctxt,
|
|
|
|
Kind::Idle,
|
|
|
|
!idle.args.schedule.is_empty(),
|
|
|
|
!idle.args.spawn.is_empty(),
|
|
|
|
app,
|
2019-02-12 14:53:49 +01:00
|
|
|
None,
|
2018-11-03 17:02:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let unsafety = &idle.unsafety;
|
|
|
|
let idle = &ctxt.idle;
|
|
|
|
|
|
|
|
(
|
|
|
|
quote!(
|
|
|
|
#module
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
// unsafe trampoline to deter end-users from calling this non-reentrant function
|
2018-11-03 17:02:41 +01:00
|
|
|
#(#attrs)*
|
2019-02-16 00:22:00 +01:00
|
|
|
unsafe fn #idle() -> ! {
|
|
|
|
#[inline(always)]
|
|
|
|
#unsafety fn idle() -> ! {
|
|
|
|
#(#locals)*
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#prelude
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#(#stmts)*
|
|
|
|
}
|
|
|
|
|
|
|
|
idle()
|
2018-12-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
),
|
2018-11-03 17:02:41 +01:00
|
|
|
quote!(#idle()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
quote!(),
|
|
|
|
quote!(loop {
|
|
|
|
rtfm::export::wfi();
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec<proc_macro2::TokenStream> {
|
|
|
|
app.exceptions
|
|
|
|
.iter()
|
|
|
|
.map(|(ident, exception)| {
|
|
|
|
let attrs = &exception.attrs;
|
|
|
|
let stmts = &exception.stmts;
|
|
|
|
|
|
|
|
let prelude = prelude(
|
|
|
|
ctxt,
|
|
|
|
Kind::Exception(ident.clone()),
|
|
|
|
&exception.args.resources,
|
|
|
|
&exception.args.spawn,
|
|
|
|
&exception.args.schedule,
|
|
|
|
app,
|
|
|
|
exception.args.priority,
|
|
|
|
analysis,
|
|
|
|
);
|
|
|
|
|
|
|
|
let module = module(
|
|
|
|
ctxt,
|
|
|
|
Kind::Exception(ident.clone()),
|
|
|
|
!exception.args.schedule.is_empty(),
|
|
|
|
!exception.args.spawn.is_empty(),
|
|
|
|
app,
|
2019-02-12 14:53:49 +01:00
|
|
|
None,
|
2018-11-03 17:02:41 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
let baseline = &ctxt.baseline;
|
|
|
|
let baseline_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
2019-02-16 00:22:00 +01:00
|
|
|
() => quote!(let ref #baseline = rtfm::Instant::now();),
|
2018-11-03 17:02:41 +01:00
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let start_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => quote!(
|
2018-12-02 17:35:26 +01:00
|
|
|
#[allow(unused_variables)]
|
2019-02-16 00:22:00 +01:00
|
|
|
let start = *#baseline;
|
2018-12-02 17:35:26 +01:00
|
|
|
),
|
2018-11-03 17:02:41 +01:00
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
let locals = mk_locals(&exception.statics, false);
|
|
|
|
let symbol = ident.to_string();
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
let unsafety = &exception.unsafety;
|
|
|
|
quote!(
|
|
|
|
#module
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
// unsafe trampoline to deter end-users from calling this non-reentrant function
|
2018-12-16 18:37:36 +01:00
|
|
|
#[export_name = #symbol]
|
2018-11-03 17:02:41 +01:00
|
|
|
#(#attrs)*
|
2019-02-16 00:22:00 +01:00
|
|
|
unsafe fn #alias() {
|
|
|
|
#[inline(always)]
|
|
|
|
#unsafety fn exception() {
|
|
|
|
#(#locals)*
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#baseline_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#prelude
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#start_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
rtfm::export::run(move || {
|
|
|
|
#(#stmts)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
exception()
|
2018-12-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
)
|
2018-11-03 17:02:41 +01:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn interrupts(
|
|
|
|
ctxt: &mut Context,
|
|
|
|
app: &App,
|
|
|
|
analysis: &Analysis,
|
|
|
|
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
|
|
|
|
let mut root = vec![];
|
|
|
|
let mut scoped = vec![];
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
let device = &app.args.device;
|
2018-11-03 17:02:41 +01:00
|
|
|
for (ident, interrupt) in &app.interrupts {
|
|
|
|
let attrs = &interrupt.attrs;
|
|
|
|
let stmts = &interrupt.stmts;
|
|
|
|
|
|
|
|
let prelude = prelude(
|
|
|
|
ctxt,
|
|
|
|
Kind::Interrupt(ident.clone()),
|
|
|
|
&interrupt.args.resources,
|
|
|
|
&interrupt.args.spawn,
|
|
|
|
&interrupt.args.schedule,
|
|
|
|
app,
|
|
|
|
interrupt.args.priority,
|
|
|
|
analysis,
|
|
|
|
);
|
|
|
|
|
|
|
|
root.push(module(
|
|
|
|
ctxt,
|
|
|
|
Kind::Interrupt(ident.clone()),
|
|
|
|
!interrupt.args.schedule.is_empty(),
|
|
|
|
!interrupt.args.spawn.is_empty(),
|
|
|
|
app,
|
2019-02-12 14:53:49 +01:00
|
|
|
None,
|
2018-11-03 17:02:41 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
let baseline = &ctxt.baseline;
|
|
|
|
let baseline_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
2019-02-16 00:22:00 +01:00
|
|
|
() => quote!(let ref #baseline = rtfm::Instant::now();),
|
2018-11-03 17:02:41 +01:00
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let start_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => quote!(
|
|
|
|
#[allow(unused_variables)]
|
2019-02-16 00:22:00 +01:00
|
|
|
let start = *#baseline;
|
2018-11-03 17:02:41 +01:00
|
|
|
),
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
let locals = mk_locals(&interrupt.statics, false);
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-12-16 18:37:36 +01:00
|
|
|
let symbol = ident.to_string();
|
2018-11-03 17:02:41 +01:00
|
|
|
let unsafety = &interrupt.unsafety;
|
|
|
|
scoped.push(quote!(
|
2019-02-16 00:22:00 +01:00
|
|
|
// unsafe trampoline to deter end-users from calling this non-reentrant function
|
2018-11-03 17:02:41 +01:00
|
|
|
#(#attrs)*
|
2018-12-16 18:37:36 +01:00
|
|
|
#[export_name = #symbol]
|
2019-02-16 00:22:00 +01:00
|
|
|
unsafe fn #alias() {
|
|
|
|
#[inline(always)]
|
|
|
|
#unsafety fn interrupt() {
|
|
|
|
// check that this interrupt exists
|
|
|
|
let _ = #device::interrupt::#ident;
|
2018-12-16 18:37:36 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#(#locals)*
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#baseline_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#prelude
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#start_let
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
rtfm::export::run(move || {
|
|
|
|
#(#stmts)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
interrupt()
|
2018-12-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(quote!(#(#root)*), quote!(#(#scoped)*))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tasks(ctxt: &mut Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
2018-12-15 19:07:09 +01:00
|
|
|
|
|
|
|
// first pass to generate buffers (statics and resources) and spawn aliases
|
2018-11-03 17:02:41 +01:00
|
|
|
for (name, task) in &app.tasks {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
2019-02-16 00:22:00 +01:00
|
|
|
let scheduleds_alias = ctxt.ident_gen.mk_ident(None, false);
|
|
|
|
let free_alias = ctxt.ident_gen.mk_ident(None, false);
|
|
|
|
let inputs_alias = ctxt.ident_gen.mk_ident(None, false);
|
|
|
|
let task_alias = ctxt.ident_gen.mk_ident(Some(&name.to_string()), false);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
let inputs = &task.inputs;
|
|
|
|
|
|
|
|
let ty = tuple_ty(inputs);
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
let capacity = analysis.capacities[name];
|
|
|
|
let capacity_lit = mk_capacity_literal(capacity);
|
|
|
|
let capacity_ty = mk_typenum_capacity(capacity, true);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
let resource = mk_resource(
|
|
|
|
ctxt,
|
2018-12-16 18:37:36 +01:00
|
|
|
&[],
|
2018-11-03 17:02:41 +01:00
|
|
|
&free_alias,
|
|
|
|
quote!(rtfm::export::FreeQueue<#capacity_ty>),
|
|
|
|
*analysis.free_queues.get(name).unwrap_or(&0),
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
quote!(&mut #free_alias)
|
|
|
|
} else {
|
|
|
|
quote!(#free_alias.get_mut())
|
|
|
|
},
|
2018-11-03 17:02:41 +01:00
|
|
|
app,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let scheduleds_static = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
let scheduleds_symbol = format!("{}::SCHEDULED_TIMES::{}", name, scheduleds_alias);
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
let inits =
|
|
|
|
(0..capacity).map(|_| quote!(rtfm::export::MaybeUninit::uninitialized()));
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#[doc = #scheduleds_symbol]
|
|
|
|
static mut #scheduleds_alias:
|
|
|
|
[rtfm::export::MaybeUninit<rtfm::Instant>; #capacity_lit] =
|
|
|
|
[#(#inits),*];
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
quote!(
|
|
|
|
#[doc = #scheduleds_symbol]
|
|
|
|
static mut #scheduleds_alias:
|
|
|
|
rtfm::export::MaybeUninit<[rtfm::Instant; #capacity_lit]> =
|
|
|
|
rtfm::export::MaybeUninit::uninitialized();
|
|
|
|
)
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let inputs_symbol = format!("{}::INPUTS::{}", name, inputs_alias);
|
|
|
|
let free_symbol = format!("{}::FREE_QUEUE::{}", name, free_alias);
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
let inits = (0..capacity).map(|_| quote!(rtfm::export::MaybeUninit::uninitialized()));
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #free_symbol]
|
|
|
|
static mut #free_alias: rtfm::export::FreeQueue<#capacity_ty> = unsafe {
|
|
|
|
rtfm::export::FreeQueue::new_sc()
|
|
|
|
};
|
|
|
|
|
|
|
|
#[doc = #inputs_symbol]
|
|
|
|
static mut #inputs_alias: [rtfm::export::MaybeUninit<#ty>; #capacity_lit] =
|
|
|
|
[#(#inits),*];
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #free_symbol]
|
|
|
|
static mut #free_alias: rtfm::export::MaybeUninit<
|
2018-11-03 17:02:41 +01:00
|
|
|
rtfm::export::FreeQueue<#capacity_ty>
|
2019-02-15 19:52:25 +01:00
|
|
|
> = rtfm::export::MaybeUninit::uninitialized();
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
#[doc = #inputs_symbol]
|
|
|
|
static mut #inputs_alias: rtfm::export::MaybeUninit<[#ty; #capacity_lit]> =
|
|
|
|
rtfm::export::MaybeUninit::uninitialized();
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
#resource
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#scheduleds_static
|
2018-12-15 19:07:09 +01:00
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-15 19:07:09 +01:00
|
|
|
ctxt.tasks.insert(
|
|
|
|
name.clone(),
|
|
|
|
Task {
|
|
|
|
alias: task_alias,
|
|
|
|
free_queue: free_alias,
|
|
|
|
inputs: inputs_alias,
|
2019-02-16 00:22:00 +01:00
|
|
|
spawn_fn: ctxt.ident_gen.mk_ident(None, false),
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-15 19:07:09 +01:00
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
scheduleds: scheduleds_alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-15 19:07:09 +01:00
|
|
|
// second pass to generate the actual task function
|
|
|
|
for (name, task) in &app.tasks {
|
|
|
|
let inputs = &task.inputs;
|
|
|
|
let locals = mk_locals(&task.statics, false);
|
|
|
|
let stmts = &task.stmts;
|
|
|
|
let unsafety = &task.unsafety;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-15 19:07:09 +01:00
|
|
|
let scheduled_let = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
let baseline = &ctxt.baseline;
|
2019-02-16 00:22:00 +01:00
|
|
|
quote!(let scheduled = *#baseline;)
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2018-12-15 19:07:09 +01:00
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let prelude = prelude(
|
|
|
|
ctxt,
|
|
|
|
Kind::Task(name.clone()),
|
|
|
|
&task.args.resources,
|
|
|
|
&task.args.spawn,
|
|
|
|
&task.args.schedule,
|
|
|
|
app,
|
|
|
|
task.args.priority,
|
|
|
|
analysis,
|
|
|
|
);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
items.push(module(
|
|
|
|
ctxt,
|
|
|
|
Kind::Task(name.clone()),
|
|
|
|
!task.args.schedule.is_empty(),
|
|
|
|
!task.args.spawn.is_empty(),
|
|
|
|
app,
|
2019-02-12 14:53:49 +01:00
|
|
|
None,
|
2018-11-03 17:02:41 +01:00
|
|
|
));
|
|
|
|
|
2018-12-16 19:10:36 +01:00
|
|
|
let attrs = &task.attrs;
|
|
|
|
let cfgs = &task.cfgs;
|
2018-12-15 19:07:09 +01:00
|
|
|
let task_alias = &ctxt.tasks[name].alias;
|
2019-02-16 00:22:00 +01:00
|
|
|
let (baseline, baseline_arg) = match () {
|
2018-12-15 19:07:09 +01:00
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
let baseline = &ctxt.baseline;
|
2019-02-16 00:22:00 +01:00
|
|
|
(quote!(#baseline,), quote!(#baseline: &rtfm::Instant,))
|
2018-12-15 19:07:09 +01:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
2019-02-16 00:22:00 +01:00
|
|
|
() => (quote!(), quote!()),
|
2018-12-15 19:07:09 +01:00
|
|
|
};
|
2019-02-16 00:22:00 +01:00
|
|
|
let pats = tuple_pat(inputs);
|
2018-12-15 19:07:09 +01:00
|
|
|
items.push(quote!(
|
2019-02-16 00:22:00 +01:00
|
|
|
// unsafe trampoline to deter end-users from calling this non-reentrant function
|
2018-12-15 19:07:09 +01:00
|
|
|
#(#attrs)*
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2019-02-16 00:22:00 +01:00
|
|
|
unsafe fn #task_alias(#baseline_arg #(#inputs,)*) {
|
|
|
|
#[inline(always)]
|
|
|
|
#unsafety fn task(#baseline_arg #(#inputs,)*) {
|
|
|
|
#(#locals)*
|
2018-12-15 19:07:09 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#prelude
|
|
|
|
|
|
|
|
#scheduled_let
|
2018-12-15 19:07:09 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
#(#stmts)*
|
|
|
|
}
|
2018-12-15 19:07:09 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
task(#baseline #pats)
|
2018-12-15 19:07:09 +01:00
|
|
|
}
|
|
|
|
));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatchers(
|
|
|
|
ctxt: &mut Context,
|
|
|
|
app: &App,
|
|
|
|
analysis: &Analysis,
|
|
|
|
) -> (proc_macro2::TokenStream, proc_macro2::TokenStream) {
|
|
|
|
let mut data = vec![];
|
|
|
|
let mut dispatchers = vec![];
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
let device = &app.args.device;
|
2018-11-03 17:02:41 +01:00
|
|
|
for (level, dispatcher) in &analysis.dispatchers {
|
2019-02-16 00:22:00 +01:00
|
|
|
let ready_alias = ctxt.ident_gen.mk_ident(None, false);
|
|
|
|
let enum_alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
let capacity = mk_typenum_capacity(dispatcher.capacity, true);
|
|
|
|
|
2018-12-16 19:10:36 +01:00
|
|
|
let variants = dispatcher
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|task| {
|
|
|
|
let task_ = &app.tasks[task];
|
|
|
|
let cfgs = &task_.cfgs;
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#task
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-11-03 17:02:41 +01:00
|
|
|
let symbol = format!("P{}::READY_QUEUE::{}", level, ready_alias);
|
|
|
|
let e = quote!(rtfm::export);
|
|
|
|
let ty = quote!(#e::ReadyQueue<#enum_alias, #capacity>);
|
|
|
|
let ceiling = *analysis.ready_queues.get(&level).unwrap_or(&0);
|
|
|
|
let resource = mk_resource(
|
|
|
|
ctxt,
|
2018-12-16 18:37:36 +01:00
|
|
|
&[],
|
2018-11-03 17:02:41 +01:00
|
|
|
&ready_alias,
|
|
|
|
ty.clone(),
|
|
|
|
ceiling,
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
quote!(&mut #ready_alias)
|
|
|
|
} else {
|
|
|
|
quote!(#ready_alias.get_mut())
|
|
|
|
},
|
2018-11-03 17:02:41 +01:00
|
|
|
app,
|
|
|
|
None,
|
|
|
|
);
|
2019-02-15 19:52:25 +01:00
|
|
|
|
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
data.push(quote!(
|
|
|
|
#[doc = #symbol]
|
|
|
|
static mut #ready_alias: #ty = unsafe { #e::ReadyQueue::new_sc() };
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
data.push(quote!(
|
|
|
|
#[doc = #symbol]
|
|
|
|
static mut #ready_alias: #e::MaybeUninit<#ty> = #e::MaybeUninit::uninitialized();
|
|
|
|
));
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
data.push(quote!(
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
2018-12-16 19:10:36 +01:00
|
|
|
enum #enum_alias { #(#variants,)* }
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#resource
|
|
|
|
));
|
|
|
|
|
|
|
|
let arms = dispatcher
|
|
|
|
.tasks
|
|
|
|
.iter()
|
|
|
|
.map(|task| {
|
2018-12-15 19:07:09 +01:00
|
|
|
let task_ = &ctxt.tasks[task];
|
|
|
|
let inputs = &task_.inputs;
|
|
|
|
let free = &task_.free_queue;
|
|
|
|
let alias = &task_.alias;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-12-16 19:10:36 +01:00
|
|
|
let task__ = &app.tasks[task];
|
|
|
|
let pats = tuple_pat(&task__.inputs);
|
|
|
|
let cfgs = &task__.cfgs;
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
let baseline_let;
|
|
|
|
let call;
|
|
|
|
match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
2018-12-15 19:07:09 +01:00
|
|
|
let scheduleds = &task_.scheduleds;
|
2019-02-15 19:52:25 +01:00
|
|
|
let scheduled = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#scheduleds.get_unchecked(usize::from(index)).as_ptr())
|
|
|
|
} else {
|
|
|
|
quote!(#scheduleds.get_ref().get_unchecked(usize::from(index)))
|
|
|
|
};
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
baseline_let = quote!(
|
2019-02-15 19:52:25 +01:00
|
|
|
let baseline = ptr::read(#scheduled);
|
2018-11-03 17:02:41 +01:00
|
|
|
);
|
2019-02-16 00:22:00 +01:00
|
|
|
call = quote!(#alias(&baseline, #pats));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => {
|
|
|
|
baseline_let = quote!();
|
|
|
|
call = quote!(#alias(#pats));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
let (free_, input) = if cfg!(feature = "nightly") {
|
|
|
|
(
|
|
|
|
quote!(#free),
|
|
|
|
quote!(#inputs.get_unchecked(usize::from(index)).as_ptr()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(
|
|
|
|
quote!(#free.get_mut()),
|
|
|
|
quote!(#inputs.get_ref().get_unchecked(usize::from(index))),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2018-12-16 19:10:36 +01:00
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#enum_alias::#task => {
|
|
|
|
#baseline_let
|
2019-02-15 19:52:25 +01:00
|
|
|
let input = ptr::read(#input);
|
|
|
|
#free_.split().0.enqueue_unchecked(index);
|
2018-12-16 19:10:36 +01:00
|
|
|
let (#pats) = input;
|
|
|
|
#call
|
|
|
|
}
|
|
|
|
)
|
2018-11-03 17:02:41 +01:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let attrs = &dispatcher.attrs;
|
2018-12-16 18:37:36 +01:00
|
|
|
let interrupt = &dispatcher.interrupt;
|
|
|
|
let symbol = interrupt.to_string();
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2019-02-15 19:52:25 +01:00
|
|
|
let ready_alias_ = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#ready_alias)
|
|
|
|
} else {
|
|
|
|
quote!(#ready_alias.get_mut())
|
|
|
|
};
|
2018-11-03 17:02:41 +01:00
|
|
|
dispatchers.push(quote!(
|
|
|
|
#(#attrs)*
|
2018-12-16 18:37:36 +01:00
|
|
|
#[export_name = #symbol]
|
|
|
|
unsafe fn #alias() {
|
2018-11-03 17:02:41 +01:00
|
|
|
use core::ptr;
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
// check that this interrupt exists
|
|
|
|
let _ = #device::interrupt::#interrupt;
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
rtfm::export::run(|| {
|
2019-02-15 19:52:25 +01:00
|
|
|
while let Some((task, index)) = #ready_alias_.split().1.dequeue() {
|
2018-11-03 17:02:41 +01:00
|
|
|
match task {
|
|
|
|
#(#arms)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2018-12-15 19:17:08 +01:00
|
|
|
ctxt.dispatchers.insert(
|
|
|
|
*level,
|
|
|
|
Dispatcher {
|
|
|
|
ready_queue: ready_alias,
|
|
|
|
enum_: enum_alias,
|
|
|
|
},
|
|
|
|
);
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(quote!(#(#data)*), quote!(#(#dispatchers)*))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
// Generate `spawn` functions
|
|
|
|
let device = &app.args.device;
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
let baseline = &ctxt.baseline;
|
2018-12-15 19:07:09 +01:00
|
|
|
for (name, task) in &ctxt.tasks {
|
|
|
|
let alias = &task.spawn_fn;
|
|
|
|
let task_ = &app.tasks[name];
|
2018-12-16 19:10:36 +01:00
|
|
|
let cfgs = &task_.cfgs;
|
2018-12-15 19:07:09 +01:00
|
|
|
let free = &task.free_queue;
|
|
|
|
let level = task_.args.priority;
|
2018-12-15 19:17:08 +01:00
|
|
|
let dispatcher = &ctxt.dispatchers[&level];
|
|
|
|
let ready = &dispatcher.ready_queue;
|
|
|
|
let enum_ = &dispatcher.enum_;
|
2018-11-03 17:02:41 +01:00
|
|
|
let dispatcher = &analysis.dispatchers[&level].interrupt;
|
2018-12-15 19:07:09 +01:00
|
|
|
let inputs = &task.inputs;
|
|
|
|
let args = &task_.inputs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let ty = tuple_ty(args);
|
|
|
|
let pats = tuple_pat(args);
|
|
|
|
|
|
|
|
let scheduleds_write = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
2018-12-15 19:07:09 +01:00
|
|
|
let scheduleds = &ctxt.tasks[name].scheduleds;
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
quote!(
|
|
|
|
ptr::write(
|
|
|
|
#scheduleds.get_unchecked_mut(usize::from(index)).as_mut_ptr(),
|
|
|
|
#baseline,
|
|
|
|
);
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
quote!(
|
|
|
|
ptr::write(
|
|
|
|
#scheduleds.get_mut().get_unchecked_mut(usize::from(index)),
|
|
|
|
#baseline,
|
|
|
|
);
|
|
|
|
)
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let baseline_arg = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => quote!(#baseline: rtfm::Instant,),
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
let input = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#inputs.get_unchecked_mut(usize::from(index)).as_mut_ptr())
|
|
|
|
} else {
|
|
|
|
quote!(#inputs.get_mut().get_unchecked_mut(usize::from(index)))
|
|
|
|
};
|
2018-11-03 17:02:41 +01:00
|
|
|
items.push(quote!(
|
|
|
|
#[inline(always)]
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
unsafe fn #alias(
|
|
|
|
#baseline_arg
|
2019-02-16 00:22:00 +01:00
|
|
|
#priority: &rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
#(#args,)*
|
|
|
|
) -> Result<(), #ty> {
|
|
|
|
use core::ptr;
|
|
|
|
|
|
|
|
use rtfm::Mutex;
|
|
|
|
|
|
|
|
if let Some(index) = (#free { #priority }).lock(|f| f.split().1.dequeue()) {
|
2019-02-15 19:52:25 +01:00
|
|
|
ptr::write(#input, (#pats));
|
2018-11-03 17:02:41 +01:00
|
|
|
#scheduleds_write
|
|
|
|
|
|
|
|
#ready { #priority }.lock(|rq| {
|
2018-12-15 19:07:09 +01:00
|
|
|
rq.split().0.enqueue_unchecked((#enum_::#name, index))
|
2018-11-03 17:02:41 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
rtfm::pend(#device::Interrupt::#dispatcher);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err((#pats))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate `spawn` structs; these call the `spawn` functions generated above
|
|
|
|
for (name, spawn) in app.spawn_callers() {
|
|
|
|
if spawn.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
let is_idle = name.to_string() == "idle";
|
|
|
|
|
|
|
|
let mut methods = vec![];
|
|
|
|
for task in spawn {
|
2018-12-16 19:10:36 +01:00
|
|
|
let task_ = &app.tasks[task];
|
2018-12-15 19:07:09 +01:00
|
|
|
let alias = &ctxt.tasks[task].spawn_fn;
|
2018-12-16 19:10:36 +01:00
|
|
|
let inputs = &task_.inputs;
|
|
|
|
let cfgs = &task_.cfgs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let ty = tuple_ty(inputs);
|
|
|
|
let pats = tuple_pat(inputs);
|
|
|
|
|
|
|
|
let instant = match () {
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
() => {
|
|
|
|
if is_idle {
|
|
|
|
quote!(rtfm::Instant::now(),)
|
|
|
|
} else {
|
2019-02-16 00:22:00 +01:00
|
|
|
quote!(*self.#baseline,)
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "timer-queue"))]
|
|
|
|
() => quote!(),
|
|
|
|
};
|
|
|
|
methods.push(quote!(
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
#[inline]
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
pub fn #task(&self, #(#inputs,)*) -> Result<(), #ty> {
|
|
|
|
unsafe { #alias(#instant &self.#priority, #pats) }
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
impl<'a> #name::Spawn<'a> {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
fn schedule(ctxt: &Context, app: &App) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
// Generate `schedule` functions
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
let timer_queue = &ctxt.timer_queue;
|
|
|
|
for (task, alias) in &ctxt.schedule_fn {
|
2018-12-15 19:07:09 +01:00
|
|
|
let task_ = &ctxt.tasks[task];
|
|
|
|
let free = &task_.free_queue;
|
2018-11-03 17:02:41 +01:00
|
|
|
let enum_ = &ctxt.schedule_enum;
|
2018-12-15 19:07:09 +01:00
|
|
|
let inputs = &task_.inputs;
|
|
|
|
let scheduleds = &task_.scheduleds;
|
2018-12-16 19:10:36 +01:00
|
|
|
let task__ = &app.tasks[task];
|
|
|
|
let args = &task__.inputs;
|
|
|
|
let cfgs = &task__.cfgs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let ty = tuple_ty(args);
|
|
|
|
let pats = tuple_pat(args);
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
let input = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#inputs.get_unchecked_mut(usize::from(index)).as_mut_ptr())
|
|
|
|
} else {
|
|
|
|
quote!(#inputs.get_mut().get_unchecked_mut(usize::from(index)))
|
|
|
|
};
|
|
|
|
|
|
|
|
let scheduled = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#scheduleds.get_unchecked_mut(usize::from(index)).as_mut_ptr())
|
|
|
|
} else {
|
|
|
|
quote!(#scheduleds.get_mut().get_unchecked_mut(usize::from(index)))
|
|
|
|
};
|
2018-11-03 17:02:41 +01:00
|
|
|
items.push(quote!(
|
|
|
|
#[inline(always)]
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
unsafe fn #alias(
|
2019-02-16 00:22:00 +01:00
|
|
|
#priority: &rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
instant: rtfm::Instant,
|
|
|
|
#(#args,)*
|
|
|
|
) -> Result<(), #ty> {
|
|
|
|
use core::ptr;
|
|
|
|
|
|
|
|
use rtfm::Mutex;
|
|
|
|
|
|
|
|
if let Some(index) = (#free { #priority }).lock(|f| f.split().1.dequeue()) {
|
2019-02-15 19:52:25 +01:00
|
|
|
ptr::write(#input, (#pats));
|
|
|
|
ptr::write(#scheduled, instant);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
let nr = rtfm::export::NotReady {
|
|
|
|
instant,
|
|
|
|
index,
|
|
|
|
task: #enum_::#task,
|
|
|
|
};
|
|
|
|
|
|
|
|
({#timer_queue { #priority }}).lock(|tq| tq.enqueue_unchecked(nr));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err((#pats))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate `Schedule` structs; these call the `schedule` functions generated above
|
|
|
|
for (name, schedule) in app.schedule_callers() {
|
|
|
|
if schedule.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(!schedule.is_empty());
|
|
|
|
|
|
|
|
let mut methods = vec![];
|
|
|
|
for task in schedule {
|
|
|
|
let alias = &ctxt.schedule_fn[task];
|
2018-12-16 19:10:36 +01:00
|
|
|
let task_ = &app.tasks[task];
|
|
|
|
let inputs = &task_.inputs;
|
|
|
|
let cfgs = &task_.cfgs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let ty = tuple_ty(inputs);
|
|
|
|
let pats = tuple_pat(inputs);
|
|
|
|
|
|
|
|
methods.push(quote!(
|
|
|
|
#[inline]
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
pub fn #task(
|
|
|
|
&self,
|
|
|
|
instant: rtfm::Instant,
|
|
|
|
#(#inputs,)*
|
|
|
|
) -> Result<(), #ty> {
|
|
|
|
unsafe { #alias(&self.#priority, instant, #pats) }
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
items.push(quote!(
|
|
|
|
impl<'a> #name::Schedule<'a> {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:36:55 +01:00
|
|
|
fn timer_queue(ctxt: &mut Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
2018-11-03 17:02:41 +01:00
|
|
|
let tasks = &analysis.timer_queue.tasks;
|
|
|
|
|
|
|
|
if tasks.is_empty() {
|
|
|
|
return quote!();
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut items = vec![];
|
|
|
|
|
2018-12-16 19:10:36 +01:00
|
|
|
let variants = tasks
|
|
|
|
.iter()
|
|
|
|
.map(|task| {
|
|
|
|
let cfgs = &app.tasks[task].cfgs;
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#task
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-11-03 17:02:41 +01:00
|
|
|
let enum_ = &ctxt.schedule_enum;
|
|
|
|
items.push(quote!(
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[derive(Clone, Copy)]
|
2018-12-16 19:10:36 +01:00
|
|
|
enum #enum_ { #(#variants,)* }
|
2018-11-03 17:02:41 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
let cap = mk_typenum_capacity(analysis.timer_queue.capacity, false);
|
|
|
|
let tq = &ctxt.timer_queue;
|
|
|
|
let symbol = format!("TIMER_QUEUE::{}", tq);
|
2019-02-15 19:52:25 +01:00
|
|
|
if cfg!(feature = "nightly") {
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #symbol]
|
|
|
|
static mut #tq: rtfm::export::MaybeUninit<rtfm::export::TimerQueue<#enum_, #cap>> =
|
2018-11-03 17:02:41 +01:00
|
|
|
rtfm::export::MaybeUninit::uninitialized();
|
2019-02-15 19:52:25 +01:00
|
|
|
));
|
|
|
|
} else {
|
|
|
|
items.push(quote!(
|
|
|
|
#[doc = #symbol]
|
|
|
|
static mut #tq:
|
|
|
|
rtfm::export::MaybeUninit<rtfm::export::TimerQueue<#enum_, #cap>> =
|
|
|
|
rtfm::export::MaybeUninit::uninitialized();
|
|
|
|
));
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
items.push(mk_resource(
|
|
|
|
ctxt,
|
2018-12-16 18:37:36 +01:00
|
|
|
&[],
|
2018-11-03 17:02:41 +01:00
|
|
|
tq,
|
|
|
|
quote!(rtfm::export::TimerQueue<#enum_, #cap>),
|
|
|
|
analysis.timer_queue.ceiling,
|
2019-02-15 19:52:25 +01:00
|
|
|
quote!(&mut *#tq.as_mut_ptr()),
|
2018-11-03 17:02:41 +01:00
|
|
|
app,
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
let device = &app.args.device;
|
|
|
|
let arms = tasks
|
|
|
|
.iter()
|
|
|
|
.map(|task| {
|
2018-12-16 19:10:36 +01:00
|
|
|
let task_ = &app.tasks[task];
|
|
|
|
let level = task_.args.priority;
|
|
|
|
let cfgs = &task_.cfgs;
|
2018-12-15 19:17:08 +01:00
|
|
|
let dispatcher_ = &ctxt.dispatchers[&level];
|
|
|
|
let tenum = &dispatcher_.enum_;
|
|
|
|
let ready = &dispatcher_.ready_queue;
|
2018-11-03 17:02:41 +01:00
|
|
|
let dispatcher = &analysis.dispatchers[&level].interrupt;
|
|
|
|
|
|
|
|
quote!(
|
2018-12-16 19:10:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
#enum_::#task => {
|
|
|
|
(#ready { #priority }).lock(|rq| {
|
|
|
|
rq.split().0.enqueue_unchecked((#tenum::#task, index))
|
|
|
|
});
|
|
|
|
|
|
|
|
rtfm::pend(#device::Interrupt::#dispatcher);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let logical_prio = analysis.timer_queue.priority;
|
2019-02-16 00:22:00 +01:00
|
|
|
let alias = ctxt.ident_gen.mk_ident(None, false);
|
2018-11-03 17:02:41 +01:00
|
|
|
items.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#[export_name = "SysTick"]
|
2018-11-03 17:02:41 +01:00
|
|
|
#[doc(hidden)]
|
2018-12-16 18:37:36 +01:00
|
|
|
unsafe fn #alias() {
|
2018-11-03 17:02:41 +01:00
|
|
|
use rtfm::Mutex;
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let ref #priority = rtfm::export::Priority::new(#logical_prio);
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
rtfm::export::run(|| {
|
|
|
|
rtfm::export::sys_tick(#tq { #priority }, |task, index| {
|
|
|
|
match task {
|
|
|
|
#(#arms)*
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
2018-12-15 20:34:33 +01:00
|
|
|
fn pre_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
2018-11-03 17:02:41 +01:00
|
|
|
let mut exprs = vec![];
|
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
if !cfg!(feature = "nightly") {
|
|
|
|
// these are `MaybeUninit` arrays
|
|
|
|
for task in ctxt.tasks.values() {
|
|
|
|
let inputs = &task.inputs;
|
|
|
|
exprs.push(quote!(#inputs.set(core::mem::uninitialized());))
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
#[cfg(feature = "timer-queue")]
|
|
|
|
for task in ctxt.tasks.values() {
|
|
|
|
let scheduleds = &task.scheduleds;
|
|
|
|
exprs.push(quote!(#scheduleds.set(core::mem::uninitialized());))
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
// these are `MaybeUninit` `ReadyQueue`s
|
|
|
|
for dispatcher in ctxt.dispatchers.values() {
|
|
|
|
let rq = &dispatcher.ready_queue;
|
|
|
|
exprs.push(quote!(#rq.set(rtfm::export::ReadyQueue::new_sc());))
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-02-15 19:52:25 +01:00
|
|
|
// these are `MaybeUninit` `FreeQueue`s
|
|
|
|
for task in ctxt.tasks.values() {
|
|
|
|
let fq = &task.free_queue;
|
|
|
|
exprs.push(quote!(#fq.set(rtfm::export::FreeQueue::new_sc());))
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the timer queue
|
|
|
|
if !analysis.timer_queue.tasks.is_empty() {
|
|
|
|
let tq = &ctxt.timer_queue;
|
|
|
|
exprs.push(quote!(#tq.set(rtfm::export::TimerQueue::new(p.SYST));));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the `FreeQueue`s
|
2018-12-15 19:07:09 +01:00
|
|
|
for (name, task) in &ctxt.tasks {
|
|
|
|
let fq = &task.free_queue;
|
2019-02-15 19:52:25 +01:00
|
|
|
let fq_ = if cfg!(feature = "nightly") {
|
|
|
|
quote!(#fq)
|
|
|
|
} else {
|
|
|
|
quote!(#fq.get_mut())
|
|
|
|
};
|
2018-12-15 19:07:09 +01:00
|
|
|
let capacity = analysis.capacities[name];
|
2018-11-03 17:02:41 +01:00
|
|
|
exprs.push(quote!(
|
|
|
|
for i in 0..#capacity {
|
2019-02-15 19:52:25 +01:00
|
|
|
#fq_.enqueue_unchecked(i);
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-12-15 20:34:33 +01:00
|
|
|
// TODO turn the assertions that check that the priority is not larger than what's supported by
|
|
|
|
// the device into compile errors
|
|
|
|
let device = &app.args.device;
|
|
|
|
let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS);
|
|
|
|
for (name, interrupt) in &app.interrupts {
|
|
|
|
let priority = interrupt.args.priority;
|
|
|
|
exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);));
|
|
|
|
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));));
|
|
|
|
exprs.push(quote!(p.NVIC.set_priority(
|
|
|
|
#device::Interrupt::#name,
|
|
|
|
((1 << #nvic_prio_bits) - #priority) << (8 - #nvic_prio_bits),
|
|
|
|
);));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (priority, dispatcher) in &analysis.dispatchers {
|
|
|
|
let name = &dispatcher.interrupt;
|
|
|
|
exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);));
|
|
|
|
exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));));
|
|
|
|
exprs.push(quote!(p.NVIC.set_priority(
|
|
|
|
#device::Interrupt::#name,
|
|
|
|
((1 << #nvic_prio_bits) - #priority) << (8 - #nvic_prio_bits),
|
|
|
|
);));
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
// Set the cycle count to 0 and disable it while `init` executes
|
|
|
|
if cfg!(feature = "timer-queue") {
|
|
|
|
exprs.push(quote!(p.DWT.ctrl.modify(|r| r & !1);));
|
|
|
|
exprs.push(quote!(p.DWT.cyccnt.write(0);));
|
|
|
|
}
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
let mut p = rtfm::export::Peripherals::steal();
|
|
|
|
#(#exprs)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assertions(app: &App, analysis: &Analysis) -> proc_macro2::TokenStream {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
for ty in &analysis.assert_sync {
|
|
|
|
items.push(quote!(rtfm::export::assert_sync::<#ty>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for task in &analysis.tasks_assert_send {
|
|
|
|
let ty = tuple_ty(&app.tasks[task].inputs);
|
|
|
|
items.push(quote!(rtfm::export::assert_send::<#ty>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// all late resources need to be `Send`
|
|
|
|
for ty in &analysis.resources_assert_send {
|
|
|
|
items.push(quote!(rtfm::export::assert_send::<#ty>()));
|
|
|
|
}
|
|
|
|
|
|
|
|
quote!(#(#items;)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_resource(
|
|
|
|
ctxt: &Context,
|
2018-12-16 18:37:36 +01:00
|
|
|
cfgs: &[Attribute],
|
2018-11-03 17:02:41 +01:00
|
|
|
struct_: &Ident,
|
|
|
|
ty: proc_macro2::TokenStream,
|
|
|
|
ceiling: u8,
|
|
|
|
ptr: proc_macro2::TokenStream,
|
|
|
|
app: &App,
|
|
|
|
module: Option<&mut Vec<proc_macro2::TokenStream>>,
|
|
|
|
) -> proc_macro2::TokenStream {
|
|
|
|
let priority = &ctxt.priority;
|
|
|
|
let device = &app.args.device;
|
|
|
|
|
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
let path = if let Some(module) = module {
|
|
|
|
let doc = format!("`{}`", ty);
|
|
|
|
module.push(quote!(
|
2019-02-13 15:37:24 +01:00
|
|
|
#[allow(non_camel_case_types)]
|
2018-11-03 17:02:41 +01:00
|
|
|
#[doc = #doc]
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
pub struct #struct_<'a> {
|
|
|
|
#[doc(hidden)]
|
2019-02-16 00:22:00 +01:00
|
|
|
pub #priority: &'a rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
quote!(resources::#struct_)
|
|
|
|
} else {
|
|
|
|
items.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
struct #struct_<'a> {
|
2019-02-16 00:22:00 +01:00
|
|
|
#priority: &'a rtfm::export::Priority,
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
quote!(#struct_)
|
|
|
|
};
|
|
|
|
|
|
|
|
items.push(quote!(
|
2018-12-16 18:37:36 +01:00
|
|
|
#(#cfgs)*
|
2018-11-04 18:50:42 +01:00
|
|
|
impl<'a> rtfm::Mutex for #path<'a> {
|
|
|
|
type T = #ty;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2018-11-04 18:50:42 +01:00
|
|
|
#[inline]
|
|
|
|
fn lock<R, F>(&mut self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self::T) -> R,
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
rtfm::export::claim(
|
|
|
|
#ptr,
|
|
|
|
&self.#priority,
|
|
|
|
#ceiling,
|
|
|
|
#device::NVIC_PRIO_BITS,
|
|
|
|
f,
|
|
|
|
)
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
quote!(#(#items)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_capacity_literal(capacity: u8) -> LitInt {
|
|
|
|
LitInt::new(u64::from(capacity), IntSuffix::None, Span::call_site())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_typenum_capacity(capacity: u8, power_of_two: bool) -> proc_macro2::TokenStream {
|
|
|
|
let capacity = if power_of_two {
|
|
|
|
capacity
|
|
|
|
.checked_next_power_of_two()
|
|
|
|
.expect("capacity.next_power_of_two()")
|
|
|
|
} else {
|
|
|
|
capacity
|
|
|
|
};
|
|
|
|
|
|
|
|
let ident = Ident::new(&format!("U{}", capacity), Span::call_site());
|
|
|
|
|
|
|
|
quote!(rtfm::export::consts::#ident)
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:36:55 +01:00
|
|
|
struct IdentGenerator {
|
2019-02-11 00:25:33 +01:00
|
|
|
call_count: u32,
|
2019-01-17 02:36:55 +01:00
|
|
|
rng: rand::rngs::SmallRng,
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-01-17 02:36:55 +01:00
|
|
|
impl IdentGenerator {
|
|
|
|
fn new() -> IdentGenerator {
|
2019-02-11 00:25:33 +01:00
|
|
|
let elapsed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
|
|
|
|
|
|
|
let secs = elapsed.as_secs();
|
|
|
|
let nanos = elapsed.subsec_nanos();
|
|
|
|
|
|
|
|
let mut seed: [u8; 16] = [0; 16];
|
|
|
|
|
|
|
|
for (i, v) in seed.iter_mut().take(8).enumerate() {
|
|
|
|
*v = ((secs >> (i * 8)) & 0xFF) as u8
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, v) in seed.iter_mut().skip(8).take(4).enumerate() {
|
|
|
|
*v = ((nanos >> (i * 8)) & 0xFF) as u8
|
|
|
|
}
|
|
|
|
|
|
|
|
let rng = rand::rngs::SmallRng::from_seed(seed);
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
IdentGenerator { call_count: 0, rng }
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
fn mk_ident(&mut self, name: Option<&str>, random: bool) -> Ident {
|
2019-02-11 00:25:33 +01:00
|
|
|
let s = if let Some(name) = name {
|
2019-01-17 02:36:55 +01:00
|
|
|
format!("{}_", name)
|
2018-12-02 17:35:26 +01:00
|
|
|
} else {
|
2019-02-16 00:22:00 +01:00
|
|
|
"__rtfm_internal_".to_string()
|
2019-01-17 02:36:55 +01:00
|
|
|
};
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
let mut s = format!("{}{}", s, self.call_count);
|
|
|
|
self.call_count += 1;
|
|
|
|
|
|
|
|
if random {
|
|
|
|
s.push('_');
|
2019-02-11 00:25:33 +01:00
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
for i in 0..4 {
|
|
|
|
if i == 0 || self.rng.gen() {
|
|
|
|
s.push(('a' as u8 + self.rng.gen::<u8>() % 25) as char)
|
|
|
|
} else {
|
|
|
|
s.push(('0' as u8 + self.rng.gen::<u8>() % 10) as char)
|
|
|
|
}
|
2019-01-17 02:36:55 +01:00
|
|
|
}
|
2018-12-02 17:35:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:36:55 +01:00
|
|
|
Ident::new(&s, Span::call_site())
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// `once = true` means that these locals will be called from a function that will run *once*
|
2019-01-16 07:42:50 +01:00
|
|
|
fn mk_locals(locals: &BTreeMap<Ident, Static>, once: bool) -> proc_macro2::TokenStream {
|
2018-11-03 17:02:41 +01:00
|
|
|
let lt = if once { Some(quote!('static)) } else { None };
|
|
|
|
|
|
|
|
let locals = locals
|
|
|
|
.iter()
|
|
|
|
.map(|(name, static_)| {
|
|
|
|
let attrs = &static_.attrs;
|
2018-12-15 22:04:30 +01:00
|
|
|
let cfgs = &static_.cfgs;
|
2018-11-03 17:02:41 +01:00
|
|
|
let expr = &static_.expr;
|
|
|
|
let ident = name;
|
|
|
|
let ty = &static_.ty;
|
|
|
|
|
|
|
|
quote!(
|
|
|
|
#[allow(non_snake_case)]
|
2018-12-15 22:04:30 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
let #ident: &#lt mut #ty = {
|
|
|
|
#(#attrs)*
|
2018-12-15 22:04:30 +01:00
|
|
|
#(#cfgs)*
|
2018-11-03 17:02:41 +01:00
|
|
|
static mut #ident: #ty = #expr;
|
|
|
|
|
|
|
|
unsafe { &mut #ident }
|
|
|
|
};
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
quote!(#(#locals)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tuple_pat(inputs: &[ArgCaptured]) -> proc_macro2::TokenStream {
|
|
|
|
if inputs.len() == 1 {
|
|
|
|
let pat = &inputs[0].pat;
|
|
|
|
quote!(#pat)
|
|
|
|
} else {
|
|
|
|
let pats = inputs.iter().map(|i| &i.pat).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
quote!(#(#pats,)*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tuple_ty(inputs: &[ArgCaptured]) -> proc_macro2::TokenStream {
|
|
|
|
if inputs.len() == 1 {
|
|
|
|
let ty = &inputs[0].ty;
|
|
|
|
quote!(#ty)
|
|
|
|
} else {
|
|
|
|
let tys = inputs.iter().map(|i| &i.ty).collect::<Vec<_>>();
|
|
|
|
|
|
|
|
quote!((#(#tys,)*))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 18:37:36 +01:00
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
2018-11-03 17:02:41 +01:00
|
|
|
enum Kind {
|
|
|
|
Exception(Ident),
|
|
|
|
Idle,
|
|
|
|
Init,
|
|
|
|
Interrupt(Ident),
|
|
|
|
Task(Ident),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Kind {
|
|
|
|
fn ident(&self) -> Ident {
|
|
|
|
match self {
|
|
|
|
Kind::Init => Ident::new("init", Span::call_site()),
|
|
|
|
Kind::Idle => Ident::new("idle", Span::call_site()),
|
|
|
|
Kind::Task(name) | Kind::Interrupt(name) | Kind::Exception(name) => name.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_idle(&self) -> bool {
|
|
|
|
*self == Kind::Idle
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_init(&self) -> bool {
|
|
|
|
*self == Kind::Init
|
|
|
|
}
|
|
|
|
|
|
|
|
fn runs_once(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Kind::Init | Kind::Idle => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|