2017-07-29 08:16:11 +02:00
|
|
|
//! Procedural macros of the `cortex-m-rtfm` crate
|
2018-04-08 18:23:27 +02:00
|
|
|
// #![deny(warnings)]
|
2017-07-04 18:26:11 +02:00
|
|
|
#![recursion_limit = "128"]
|
|
|
|
|
2017-07-15 01:54:54 +02:00
|
|
|
#[macro_use]
|
2018-04-08 18:23:27 +02:00
|
|
|
extern crate failure;
|
2017-07-04 18:26:11 +02:00
|
|
|
extern crate proc_macro;
|
2018-04-08 18:23:27 +02:00
|
|
|
extern crate proc_macro2;
|
|
|
|
extern crate syn;
|
2017-07-04 18:26:11 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
2017-07-15 03:47:06 +02:00
|
|
|
extern crate rtfm_syntax as syntax;
|
2017-07-04 18:26:11 +02:00
|
|
|
|
2017-07-15 01:54:54 +02:00
|
|
|
use proc_macro::TokenStream;
|
2018-04-08 18:23:27 +02:00
|
|
|
use syntax::{App, Result};
|
2017-07-15 01:54:54 +02:00
|
|
|
|
|
|
|
mod analyze;
|
2017-07-04 18:26:11 +02:00
|
|
|
mod check;
|
|
|
|
mod trans;
|
2017-07-12 06:44:54 +02:00
|
|
|
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The `app!` macro, a macro used to specify the tasks and resources of a RTFM application.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The contents of this macro uses a `key: value` syntax. All the possible keys are shown below:
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// app! {
|
|
|
|
/// device: ..,
|
|
|
|
///
|
|
|
|
/// resources: { .. },
|
|
|
|
///
|
|
|
|
/// init: { .. },
|
|
|
|
///
|
|
|
|
/// idle: { .. },
|
|
|
|
///
|
|
|
|
/// tasks: { .. },
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # `device`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The value of this key is a Rust path, like `foo::bar::baz`, that must point to a *device crate*,
|
|
|
|
/// a crate generated using `svd2rust`.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// # `resources`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a list of `static` variables. These variables are the data
|
|
|
|
/// that can be safely accessed, modified and shared by tasks.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// resources: {
|
|
|
|
/// static A: bool = false;
|
|
|
|
/// static B: i32 = 0;
|
|
|
|
/// static C: [u8; 16] = [0; 16];
|
|
|
|
/// static D: Thing = Thing::new(..);
|
2017-11-22 09:27:14 +01:00
|
|
|
/// static E: Thing;
|
2017-07-29 07:34:00 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The initial value of a resource can be omitted. This means that the resource will be runtime
|
2018-01-15 23:26:07 +01:00
|
|
|
/// initialized; these runtime initialized resources are also known as *late resources*.
|
2017-11-22 09:27:14 +01:00
|
|
|
///
|
2017-07-29 07:34:00 +02:00
|
|
|
/// If this key is omitted its value defaults to an empty list.
|
|
|
|
///
|
|
|
|
/// # `init`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a set of key values. All the possible keys are shown below:
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// init: {
|
|
|
|
/// path: ..,
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ## `init.path`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a Rust path, like `foo::bar::baz`, that points to the
|
|
|
|
/// initialization function.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If the key is omitted its value defaults to `init`.
|
|
|
|
///
|
2018-01-15 23:26:07 +01:00
|
|
|
/// ## `init.resources`
|
|
|
|
///
|
|
|
|
/// This key is optional. Its value is a set of resources the `init` function *owns*. The resources
|
|
|
|
/// in this list must be a subset of the resources listed in the top `resources` key. Note that some
|
|
|
|
/// restrictions apply:
|
|
|
|
///
|
|
|
|
/// - The resources in this list can't be late resources.
|
|
|
|
/// - The resources that appear in this list can't appear in other list like `idle.resources` or
|
|
|
|
/// `tasks.$TASK.resources`
|
|
|
|
///
|
|
|
|
/// If this key is omitted its value is assumed to be an empty list.
|
|
|
|
///
|
2017-07-29 07:34:00 +02:00
|
|
|
/// # `idle`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a set of key values. All the possible keys are shown below:
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// idle: {
|
|
|
|
/// path: ..,
|
|
|
|
/// resources: [..],
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ## `idle.path`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a Rust path, like `foo::bar::baz`, that points to the idle
|
|
|
|
/// loop function.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If the key is omitted its value defaults to `idle`.
|
|
|
|
///
|
|
|
|
/// ## `idle.resources`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a list of resources the `idle` loop has access to. The
|
2018-01-15 23:26:07 +01:00
|
|
|
/// resources in this list must be a subset of the resources listed in the top `resources` key.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If omitted its value defaults to an empty list.
|
|
|
|
///
|
|
|
|
/// # `tasks`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a list of tasks. Each task itself is a set of key value pair.
|
|
|
|
/// The full syntax is shown below:
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ``` text
|
|
|
|
/// tasks: {
|
|
|
|
/// $TASK: {
|
|
|
|
/// enabled: ..,
|
|
|
|
/// path: ..,
|
|
|
|
/// priority: ..,
|
|
|
|
/// resources: [..],
|
|
|
|
/// },
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If this key is omitted its value is assumed to be an empty list.
|
|
|
|
///
|
|
|
|
/// ## `tasks.$TASK`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The key must be either a Cortex-M exception or a device specific interrupt. `PENDSV`, `SVCALL`,
|
|
|
|
/// `SYS_TICK` are considered as exceptions. All other names are assumed to be interrupts.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ## `tasks.$TASK.enabled`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional for interrupts and forbidden for exceptions. Its value must be a boolean
|
|
|
|
/// and indicates whether the interrupt will be enabled (`true`) or disabled (`false`) after `init`
|
|
|
|
/// ends and before `idle` starts.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If this key is omitted its value defaults to `true`.
|
|
|
|
///
|
|
|
|
/// ## `tasks.$TASK.path`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// The value of this key is a Rust path, like `foo::bar::baz`, that points to the handler of this
|
|
|
|
/// task.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// ## `tasks.$TASK.priority`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is an integer with type `u8` that specifies the priority of this
|
|
|
|
/// task. The minimum valid priority is 1. The maximum valid priority depends on the number of the
|
|
|
|
/// NVIC priority bits the device has; if the device has 4 priority bits the maximum allowed value
|
|
|
|
/// would be 16.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If this key is omitted its value defaults to `1`.
|
|
|
|
///
|
|
|
|
/// ## `tasks.$TASK.resources`
|
|
|
|
///
|
2017-11-22 09:27:14 +01:00
|
|
|
/// This key is optional. Its value is a list of resources this task has access to. The resources in
|
2018-01-15 23:26:07 +01:00
|
|
|
/// this list must be a subset of the resources listed in the top `resources` key.
|
2017-07-29 07:34:00 +02:00
|
|
|
///
|
|
|
|
/// If omitted its value defaults to an empty list.
|
2017-07-12 06:44:54 +02:00
|
|
|
#[proc_macro]
|
2017-07-15 03:47:06 +02:00
|
|
|
pub fn app(ts: TokenStream) -> TokenStream {
|
2017-07-15 01:54:54 +02:00
|
|
|
match run(ts) {
|
2018-04-08 18:23:27 +02:00
|
|
|
Err(e) => panic!("error: {}", e),
|
2017-07-15 01:54:54 +02:00
|
|
|
Ok(ts) => ts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(ts: TokenStream) -> Result<TokenStream> {
|
2018-04-08 18:23:27 +02:00
|
|
|
let app = App::parse(ts)?.check()?;
|
2017-07-15 03:47:06 +02:00
|
|
|
let app = check::app(app)?;
|
2017-07-15 01:54:54 +02:00
|
|
|
|
|
|
|
let ownerships = analyze::app(&app);
|
|
|
|
let tokens = trans::app(&app, &ownerships);
|
2017-07-04 18:26:11 +02:00
|
|
|
|
2018-04-08 18:23:27 +02:00
|
|
|
Ok(tokens.into())
|
2017-07-04 18:26:11 +02:00
|
|
|
}
|