Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ pub fn render(
]
};

let mut fields = vec![];
let mut exprs = vec![];
let mut fields = TokenStream::new();
let mut exprs = TokenStream::new();
if target == Target::CortexM {
out.extend(quote! {
pub use cortex_m::peripheral::Peripherals as CorePeripherals;
Expand Down Expand Up @@ -202,11 +202,11 @@ pub fn render(

let p = p.name.to_sanitized_upper_case();
let id = Ident::new(&p, Span::call_site());
fields.push(quote! {
fields.extend(quote! {
#[doc = #p]
pub #id: #id
pub #id: #id,
});
exprs.push(quote!(#id: #id { _marker: PhantomData }));
exprs.extend(quote!(#id: #id { _marker: PhantomData },));
}

let span = Span::call_site();
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn render(
///All the peripherals
#[allow(non_snake_case)]
pub struct Peripherals {
#(#fields,)*
#fields
}

impl Peripherals {
Expand All @@ -254,7 +254,7 @@ pub fn render(
DEVICE_PERIPHERALS = true;

Peripherals {
#(#exprs,)*
#exprs
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/generate/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub fn render(
interrupts.sort_by_key(|i| i.value);

let mut root = TokenStream::new();
let mut from_arms = vec![];
let mut elements = vec![];
let mut from_arms = TokenStream::new();
let mut elements = TokenStream::new();
let mut names = vec![];
let mut variants = vec![];
let mut variants = TokenStream::new();

// Current position in the vector table
let mut pos = 0;
let mut mod_items = vec![];
let mut mod_items = TokenStream::new();
for interrupt in &interrupts {
while pos < interrupt.value {
elements.push(quote!(Vector { _reserved: 0 }));
elements.extend(quote!(Vector { _reserved: 0 },));
pos += 1;
}
pos += 1;
Expand All @@ -55,16 +55,16 @@ pub fn render(

let value = util::unsuffixed(u64(interrupt.value));

variants.push(quote! {
variants.extend(quote! {
#[doc = #description]
#name_uc = #value,
});

from_arms.push(quote! {
from_arms.extend(quote! {
#value => Ok(Interrupt::#name_uc),
});

elements.push(quote!(Vector { _handler: #name_uc }));
elements.extend(quote!(Vector { _handler: #name_uc },));
names.push(name_uc);
}

Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn render(
#[link_section = ".vector_table.interrupts"]
#[no_mangle]
pub static __INTERRUPTS: [Vector; #n] = [
#(#elements,)*
#elements
];
});
}
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn render(
#[used]
pub static __INTERRUPTS:
[Vector; #n] = [
#(#elements,)*
#elements
];
});
}
Expand All @@ -140,7 +140,7 @@ pub fn render(
#[derive(Copy, Clone, Debug)]
#enum_repr
pub enum Interrupt {
#(#variants)*
#variants
}

unsafe impl bare_metal::Nr for Interrupt {
Expand All @@ -154,7 +154,7 @@ pub fn render(
if target == Target::CortexM || target == Target::Msp430 {
root.extend(interrupt_enum);
} else {
mod_items.push(quote! {
mod_items.extend(quote! {
#interrupt_enum

#[derive(Debug, Copy, Clone)]
Expand All @@ -164,7 +164,7 @@ pub fn render(
#[inline]
pub fn try_from(value: u8) -> Result<Self, TryFromInterruptError> {
match value {
#(#from_arms)*
#from_arms
_ => Err(TryFromInterruptError(())),
}
}
Expand All @@ -179,7 +179,7 @@ pub fn render(
};

if target != Target::CortexM && target != Target::Msp430 {
mod_items.push(quote! {
mod_items.extend(quote! {
#[cfg(feature = "rt")]
#[macro_export]
/// Assigns a handler to an interrupt
Expand Down Expand Up @@ -269,7 +269,7 @@ pub fn render(
root.extend(quote! {
#[doc(hidden)]
pub mod interrupt {
#(#mod_items)*
#mod_items
}
});

Expand Down
108 changes: 50 additions & 58 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,17 @@ pub fn fields(
}
};

r_impl_items.extend(quote! {
#[doc = #description_with_bits]
#[inline(always)]
pub fn #sc(&self) -> #_pc_r {
#_pc_r::new ( #value )
}
});

if let Some((evs, base)) = lookup_filter(&lookup_results, Usage::Read) {
evs_r = Some(evs.clone());

r_impl_items.extend(quote! {
#[doc = #description_with_bits]
#[inline(always)]
pub fn #sc(&self) -> #_pc_r {
#_pc_r::new( #value )
}
});

if let Some(base) = base {
let pc = base.field.to_sanitized_upper_case();
let base_pc_r = Ident::new(&(pc.clone() + "_A"), span);
Expand All @@ -309,50 +309,50 @@ pub fn fields(

add_from_variants(mod_items, &variants, &pc_r, &fty, &description, rv);

let mut enum_items = vec![];
let mut enum_items = TokenStream::new();

let mut arms = variants
.iter()
.map(|v| {
let i = util::unsuffixed_or_bool(v.value, width);
let pc = &v.pc;
let mut arms = TokenStream::new();
for v in variants.iter().map(|v| {
let i = util::unsuffixed_or_bool(v.value, width);
let pc = &v.pc;

if has_reserved_variant {
quote! { #i => Val(#pc_r::#pc) }
} else {
quote! { #i => #pc_r::#pc }
}
})
.collect::<Vec<_>>();
if has_reserved_variant {
quote! { #i => Val(#pc_r::#pc), }
} else {
quote! { #i => #pc_r::#pc, }
}
}) {
arms.extend(v);
}

if has_reserved_variant {
arms.push(quote! {
i => Res(i)
arms.extend(quote! {
i => Res(i),
});
} else if 1 << width.to_ty_width()? != variants.len() {
arms.push(quote! {
_ => unreachable!()
arms.extend(quote! {
_ => unreachable!(),
});
}

if has_reserved_variant {
enum_items.push(quote! {
enum_items.extend(quote! {
///Get enumerated values variant
#[inline(always)]
pub fn variant(&self) -> crate::Variant<#fty, #pc_r> {
use crate::Variant::*;
match self.bits {
#(#arms),*
#arms
}
}
});
} else {
enum_items.push(quote! {
enum_items.extend(quote! {
///Get enumerated values variant
#[inline(always)]
pub fn variant(&self) -> #pc_r {
match self.bits {
#(#arms),*
#arms
}
}
});
Expand All @@ -372,7 +372,7 @@ pub fn fields(
);

let doc = format!("Checks if the value of the field is `{}`", pc);
enum_items.push(quote! {
enum_items.extend(quote! {
#[doc = #doc]
#[inline(always)]
pub fn #is_variant(&self) -> bool {
Expand All @@ -386,19 +386,11 @@ pub fn fields(
#[doc = #doc]
pub type #_pc_r = crate::R<#fty, #pc_r>;
impl #_pc_r {
#(#enum_items)*
#enum_items
}
});
}
} else {
r_impl_items.extend(quote! {
#[doc = #description_with_bits]
#[inline(always)]
pub fn #sc(&self) -> #_pc_r {
#_pc_r::new ( #value )
}
});

let doc = format!("Reader of field `{}`", f.name);
mod_items.extend(quote! {
#[doc = #doc]
Expand All @@ -411,7 +403,7 @@ pub fn fields(
let new_pc_w = Ident::new(&(pc.clone() + "_AW"), span);
let _pc_w = Ident::new(&(pc.clone() + "_W"), span);

let mut proxy_items = vec![];
let mut proxy_items = TokenStream::new();
let mut unsafety = unsafety(f.write_constraint.as_ref(), width);

if let Some((evs, base)) = lookup_filter(&lookup_results, Usage::Write) {
Expand All @@ -432,7 +424,7 @@ pub fn fields(
}
}

proxy_items.push(quote! {
proxy_items.extend(quote! {
///Writes `variant` to the field
#[inline(always)]
pub fn variant(self, variant: #pc_w) -> &'a mut W {
Expand All @@ -447,7 +439,7 @@ pub fn fields(
let sc = &v.sc;

let doc = util::escape_brackets(util::respace(&v.doc).as_ref());
proxy_items.push(quote! {
proxy_items.extend(quote! {
#[doc = #doc]
#[inline(always)]
pub fn #sc(self) -> &'a mut W {
Expand All @@ -458,7 +450,7 @@ pub fn fields(
}

if width == 1 {
proxy_items.push(quote! {
proxy_items.extend(quote! {
///Sets the field bit
#[inline(always)]
pub #unsafety fn set_bit(self) -> &'a mut W {
Expand All @@ -473,7 +465,7 @@ pub fn fields(
});
}

proxy_items.push(if offset != 0 {
proxy_items.extend(if offset != 0 {
let offset = &util::unsuffixed(offset);
quote! {
///Writes raw bits to the field
Expand Down Expand Up @@ -502,7 +494,7 @@ pub fn fields(
}

impl<'a> #_pc_w<'a> {
#(#proxy_items)*
#proxy_items
}
});

Expand Down Expand Up @@ -587,18 +579,18 @@ fn add_from_variants(
(quote! { #[repr(#fty)] }, quote! { variant as _ })
};

let vars = variants
.iter()
.map(|v| {
let desc = util::escape_brackets(&format!("{}: {}", v.value, v.doc));
let pcv = &v.pc;
let pcval = &util::unsuffixed(v.value);
quote! {
#[doc = #desc]
#pcv = #pcval
}
})
.collect::<Vec<_>>();
let mut vars = TokenStream::new();
for v in variants.iter().map(|v| {
let desc = util::escape_brackets(&format!("{}: {}", v.value, v.doc));
let pcv = &v.pc;
let pcval = &util::unsuffixed(v.value);
quote! {
#[doc = #desc]
#pcv = #pcval,
}
}) {
vars.extend(v);
}

let desc = if let Some(rv) = reset_value {
format!("{}\n\nValue on reset: {}", desc, rv)
Expand All @@ -611,7 +603,7 @@ fn add_from_variants(
#[derive(Clone, Copy, Debug, PartialEq)]
#repr
pub enum #pc {
#(#vars),*
#vars
}
impl From<#pc> for #fty {
#[inline(always)]
Expand Down