rtfm! macro

This commit is contained in:
Jorge Aparicio 2017-06-27 20:08:02 -05:00
parent 2bf5401439
commit 032bb0f0e2
3 changed files with 575 additions and 1038 deletions

View file

@ -10,24 +10,11 @@ keywords = ["arm", "cortex-m"]
license = "MIT OR Apache-2.0"
name = "cortex-m-rtfm"
repository = "https://github.com/japaric/cortex-m-rtfm"
version = "0.1.1"
[build-dependencies]
quote = "0.3.15"
syn = "0.11.10"
version = "0.2.0"
[dependencies]
cortex-m = "0.2.6"
static-ref = "0.1.0"
typenum = "1.7.0"
static-ref = "0.2.0"
[dev-dependencies]
compiletest_rs = "0.2.5"
[features]
# Number of priority bits
P2 = []
P3 = []
P4 = []
P5 = []
default = ["P4"]
[dependencies.cortex-m]
branch = "rfc"
git = "https://github.com/japaric/cortex-m"

126
build.rs
View file

@ -1,13 +1,4 @@
#[macro_use]
extern crate quote;
extern crate syn;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use syn::{Ident, IntTy, Lit};
fn main() {
let target = env::var("TARGET").unwrap();
@ -16,122 +7,5 @@ fn main() {
println!("cargo:rustc-cfg=thumbv6m");
}
let bits = if env::var_os("CARGO_FEATURE_P2").is_some() {
2
} else if env::var_os("CARGO_FEATURE_P3").is_some() {
3
} else if env::var_os("CARGO_FEATURE_P4").is_some() {
4
} else if env::var_os("CARGO_FEATURE_P5").is_some() {
5
} else {
panic!(
"Specify the number of priority bits through one of these Cargo \
features: P2, P3, P4 or P5"
);
};
let n = Lit::Int(bits, IntTy::Unsuffixed);
let mut tokens = vec![];
tokens.push(
quote! {
const PRIORITY_BITS: u8 = #n;
},
);
// Ceilings and thresholds
for i in 0..(1 << bits) + 1 {
let c = Ident::new(format!("C{}", i));
let t = Ident::new(format!("T{}", i));
let u = Ident::new(format!("U{}", i));
let doc = format!("A ceiling of {}", i);
tokens.push(
quote! {
#[doc = #doc]
pub type #c = ::typenum::#u;
},
);
let doc = format!("A preemption threshold of {}", i);
tokens.push(
quote! {
#[doc = #doc]
pub type #t = Threshold<::typenum::#u>;
},
);
}
// Priorities
for i in 0..(1 << bits) + 1 {
let p = Ident::new(format!("P{}", i));
let u = Ident::new(format!("U{}", i));
let doc = format!(
"A priority of {}{}",
i,
if i == 0 {
", the lowest priority"
} else if i == (1 << bits) {
", the highest priority"
} else {
""
}
);
tokens.push(
quote! {
#[doc = #doc]
pub type #p = Priority<::typenum::#u>;
},
);
}
// GreaterThanOrEqual & LessThanOrEqual
for i in 0..(1 << bits) + 1 {
for j in 0..(i + 1) {
let i = Ident::new(format!("U{}", i));
let j = Ident::new(format!("U{}", j));
tokens.push(
quote! {
unsafe impl GreaterThanOrEqual<::typenum::#j> for
::typenum::#i {}
unsafe impl LessThanOrEqual<::typenum::#i> for
::typenum::#j {}
},
);
}
}
let u = Ident::new(format!("U{}", (1 << bits)));
let c = Ident::new(format!("C{}", (1 << bits)));
let p = Ident::new(format!("P{}", (1 << bits)));
let t = Ident::new(format!("T{}", (1 << bits)));
tokens.push(
quote! {
/// Maximum ceiling
pub type CMax = #c;
/// Maximum priority
pub type PMax = #p;
/// Maximum preemption threshold
pub type TMax = #t;
/// Maximum priority level
pub type UMax = ::typenum::#u;
},
);
let tokens = quote! {
#(#tokens)*
};
let out_dir = env::var("OUT_DIR").unwrap();
let mut out = File::create(PathBuf::from(out_dir).join("prio.rs")).unwrap();
out.write_all(tokens.as_str().as_bytes()).unwrap();
println!("cargo:rerun-if-changed=build.rs");
}

1460
src/lib.rs

File diff suppressed because it is too large Load diff