peripherals as scoped singletons

This commit is contained in:
Jorge Aparicio 2017-11-20 05:11:25 +01:00
parent e620b1e57a
commit e97afa71ce
7 changed files with 68 additions and 30 deletions

View file

@ -63,16 +63,15 @@ pub fn app(app: check::App) -> Result<App> {
tasks: app.tasks
.into_iter()
.map(|(k, v)| {
let v = ::check::task(k.as_ref(), v)
.chain_err(|| format!("checking task `{}`", k))?;
let v =
::check::task(k.as_ref(), v).chain_err(|| format!("checking task `{}`", k))?;
Ok((k, v))
})
.collect::<Result<_>>()?,
};
::check::resources(&app)
.chain_err(|| "checking `resources`")?;
::check::resources(&app).chain_err(|| "checking `resources`")?;
Ok(app)
}
@ -93,6 +92,17 @@ fn resources(app: &App) -> Result<()> {
bail!("resource `{}` is unused", resource);
}
for (name, task) in &app.tasks {
for resource in &task.resources {
ensure!(
app.resources.contains_key(&resource),
"task {} contains an undeclared resource with name {}",
name,
resource
);
}
}
Ok(())
}

View file

@ -161,8 +161,13 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
let device = &app.device;
let krate = krate();
let mut tys = vec![quote!(#device::Peripherals)];
let mut exprs = vec![quote!(#device::Peripherals::all())];
let mut tys = vec![quote!(init::Peripherals)];
let mut exprs = vec![quote!{
init::Peripherals {
core: ::#device::CorePeripherals::steal(),
device: ::#device::Peripherals::steal(),
}
}];
let mut ret = None;
let mut mod_items = vec![];
@ -255,7 +260,10 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
root.push(quote! {
#[allow(unsafe_code)]
mod init {
pub use ::#device::Peripherals;
pub struct Peripherals {
pub core: ::#device::CorePeripherals,
pub device: ::#device::Peripherals,
}
#(#mod_items)*
}
@ -268,7 +276,7 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
Kind::Exception(ref e) => {
if exceptions.is_empty() {
exceptions.push(quote! {
let scb = &*#device::SCB.get();
let scb = &*#device::SCB::ptr();
});
}
@ -284,7 +292,7 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
// Interrupt. These can be enabled / disabled through the NVIC
if interrupts.is_empty() {
interrupts.push(quote! {
let nvic = &*#device::NVIC.get();
let nvic = &*#device::NVIC::ptr();
});
}