Skip to content

Commit 9266b73

Browse files
committed
generate a Peripherals struct that grants access to all the peripherals
1 parent c7aed0a commit 9266b73

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

src/generate.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn device(
9292

9393
::generate::interrupt(target, &d.peripherals, items);
9494

95-
const CORE_PERIPHERALS: &'static [&'static str] = &[
95+
const CORE_PERIPHERALS: &[&str] = &[
9696
"CPUID",
9797
"DCB",
9898
"DWT",
@@ -106,13 +106,21 @@ pub fn device(
106106
"TPIU",
107107
];
108108

109+
let mut fields = vec![];
110+
let mut exprs = vec![];
109111
if *target == Target::CortexM {
110112
for p in CORE_PERIPHERALS {
111-
let p = Ident::new(*p);
113+
let id = Ident::new(*p);
112114

113115
items.push(quote! {
114-
pub use cortex_m::peripheral::#p;
116+
pub use cortex_m::peripheral::#id;
117+
});
118+
119+
fields.push(quote! {
120+
#[doc = #p]
121+
pub #id: &'a #id
115122
});
123+
exprs.push(quote!(#id: &*#id.get()));
116124
}
117125
}
118126

@@ -125,8 +133,32 @@ pub fn device(
125133
}
126134

127135
::generate::peripheral(p, &d.peripherals, items, &d.defaults)?;
136+
let p = &*p.name;
137+
let id = Ident::new(&*p);
138+
fields.push(quote! {
139+
#[doc = #p]
140+
pub #id: &'a #id
141+
});
142+
exprs.push(quote!(#id: &*#id.get()));
128143
}
129144

145+
items.push(quote! {
146+
/// All the peripherals
147+
#[allow(non_snake_case)]
148+
pub struct Peripherals<'a> {
149+
#(#fields,)*
150+
}
151+
152+
impl<'a> Peripherals<'a> {
153+
/// Grants access to all the peripherals
154+
pub unsafe fn all() -> Self {
155+
Peripherals {
156+
#(#exprs,)*
157+
}
158+
}
159+
}
160+
});
161+
130162
Ok(())
131163
}
132164

0 commit comments

Comments
 (0)