2019-06-13 23:56:59 +02:00
|
|
|
use std::collections::HashSet;
|
2017-07-15 01:54:54 +02:00
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
use proc_macro2::Span;
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic_syntax::{
|
2019-06-13 23:56:59 +02:00
|
|
|
analyze::Analysis,
|
2019-06-20 06:19:59 +02:00
|
|
|
ast::{App, CustomArg},
|
2019-06-13 23:56:59 +02:00
|
|
|
};
|
|
|
|
use syn::{parse, Path};
|
|
|
|
|
|
|
|
pub struct Extra<'a> {
|
|
|
|
pub device: &'a Path,
|
|
|
|
pub monotonic: Option<&'a Path>,
|
2020-08-27 13:21:56 +02:00
|
|
|
pub peripherals: bool,
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Extra<'a> {
|
|
|
|
pub fn monotonic(&self) -> &'a Path {
|
|
|
|
self.monotonic.expect("UNREACHABLE")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn app<'a>(app: &'a App, analysis: &Analysis) -> parse::Result<Extra<'a>> {
|
2020-09-01 19:04:55 +02:00
|
|
|
// Check that all exceptions are valid; only exceptions with configurable priorities are
|
2019-06-13 23:56:59 +02:00
|
|
|
// accepted
|
2019-06-20 06:19:59 +02:00
|
|
|
for (name, task) in &app.hardware_tasks {
|
|
|
|
let name_s = task.args.binds.to_string();
|
2019-06-13 23:56:59 +02:00
|
|
|
match &*name_s {
|
|
|
|
"SysTick" => {
|
2020-08-27 13:21:56 +02:00
|
|
|
// If the timer queue is used, then SysTick is unavailable
|
|
|
|
if !analysis.timer_queues.is_empty() {
|
2019-06-13 23:56:59 +02:00
|
|
|
return Err(parse::Error::new(
|
|
|
|
name.span(),
|
|
|
|
"this exception can't be used because it's being used by the runtime",
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// OK
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 06:19:59 +02:00
|
|
|
"NonMaskableInt" | "HardFault" => {
|
2019-06-13 23:56:59 +02:00
|
|
|
return Err(parse::Error::new(
|
|
|
|
name.span(),
|
|
|
|
"only exceptions with configurable priority can be used as hardware tasks",
|
|
|
|
));
|
|
|
|
}
|
2019-06-20 06:19:59 +02:00
|
|
|
|
|
|
|
_ => {}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2017-07-28 00:08:42 +02:00
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Check that external (device-specific) interrupts are not named after known (Cortex-M)
|
2019-06-13 23:56:59 +02:00
|
|
|
// exceptions
|
2020-09-01 16:39:05 +02:00
|
|
|
for name in app.extern_interrupts.keys() {
|
2019-06-13 23:56:59 +02:00
|
|
|
let name_s = name.to_string();
|
|
|
|
|
|
|
|
match &*name_s {
|
|
|
|
"NonMaskableInt" | "HardFault" | "MemoryManagement" | "BusFault" | "UsageFault"
|
|
|
|
| "SecureFault" | "SVCall" | "DebugMonitor" | "PendSV" | "SysTick" => {
|
|
|
|
return Err(parse::Error::new(
|
|
|
|
name.span(),
|
|
|
|
"Cortex-M exceptions can't be used as `extern` interrupts",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
2017-07-28 00:08:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Check that there are enough external interrupts to dispatch the software tasks and the timer
|
2019-06-13 23:56:59 +02:00
|
|
|
// queue handler
|
2020-08-27 13:21:56 +02:00
|
|
|
let mut first = None;
|
|
|
|
let priorities = app
|
|
|
|
.software_tasks
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(name, task)| {
|
|
|
|
first = Some(name);
|
|
|
|
Some(task.args.priority)
|
|
|
|
})
|
|
|
|
.chain(analysis.timer_queues.first().map(|tq| tq.priority))
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
let need = priorities.len();
|
2020-09-01 16:39:05 +02:00
|
|
|
let given = app.extern_interrupts.len();
|
2020-08-27 13:21:56 +02:00
|
|
|
if need > given {
|
|
|
|
let s = {
|
|
|
|
format!(
|
|
|
|
"not enough `extern` interrupts to dispatch \
|
|
|
|
all software tasks (need: {}; given: {})",
|
|
|
|
need, given
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// If not enough tasks and first still is None, may cause
|
2020-09-01 19:04:55 +02:00
|
|
|
// "custom attribute panicked" due to unwrap on None
|
2020-08-27 13:21:56 +02:00
|
|
|
return Err(parse::Error::new(first.unwrap().span(), &s));
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2017-07-15 01:54:54 +02:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut device = None;
|
|
|
|
let mut monotonic = None;
|
2020-08-27 13:21:56 +02:00
|
|
|
let mut peripherals = false;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
for (k, v) in &app.args.custom {
|
|
|
|
let ks = k.to_string();
|
|
|
|
|
|
|
|
match &*ks {
|
|
|
|
"device" => match v {
|
|
|
|
CustomArg::Path(p) => device = Some(p),
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return Err(parse::Error::new(
|
|
|
|
k.span(),
|
|
|
|
"unexpected argument value; this should be a path",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"monotonic" => match v {
|
|
|
|
CustomArg::Path(p) => monotonic = Some(p),
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return Err(parse::Error::new(
|
|
|
|
k.span(),
|
|
|
|
"unexpected argument value; this should be a path",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"peripherals" => match v {
|
2020-09-01 16:39:05 +02:00
|
|
|
CustomArg::Bool(x) => peripherals = if *x { true } else { false },
|
2019-06-13 23:56:59 +02:00
|
|
|
_ => {
|
|
|
|
return Err(parse::Error::new(
|
|
|
|
k.span(),
|
2020-09-01 16:39:05 +02:00
|
|
|
"unexpected argument value; this should be a boolean",
|
2019-06-13 23:56:59 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
return Err(parse::Error::new(k.span(), "unexpected argument"));
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-15 01:54:54 +02:00
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
if !&analysis.timer_queues.is_empty() && monotonic.is_none() {
|
2018-11-03 17:02:41 +01:00
|
|
|
return Err(parse::Error::new(
|
|
|
|
Span::call_site(),
|
2019-06-13 23:56:59 +02:00
|
|
|
"a `monotonic` timer must be specified to use the `schedule` API",
|
2018-11-03 17:02:41 +01:00
|
|
|
));
|
|
|
|
}
|
2017-07-28 00:08:42 +02:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
if let Some(device) = device {
|
|
|
|
Ok(Extra {
|
|
|
|
device,
|
|
|
|
monotonic,
|
|
|
|
peripherals,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(parse::Error::new(
|
|
|
|
Span::call_site(),
|
2020-06-11 19:18:29 +02:00
|
|
|
"a `device` argument must be specified in `#[rtic::app]`",
|
2019-06-13 23:56:59 +02:00
|
|
|
))
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2017-07-04 18:26:11 +02:00
|
|
|
}
|