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
16 changes: 8 additions & 8 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cast::u64;
use quote::{Tokens, ToTokens};
use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field,
Peripheral, Register, Usage, WriteConstraint};
use syn::{self, Ident, Lit};
use syn::{self, Ident};

use errors::*;
use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, U32Ext, BITS_PER_BYTE};
Expand Down Expand Up @@ -424,7 +424,7 @@ pub fn peripheral(
) -> Result<()> {
let name = Ident::new(&*p.name.to_uppercase());
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
let address = util::unsuffixed(u64(p.base_address));
let address = util::hex(p.base_address);
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));

items.push(quote! {
Expand Down Expand Up @@ -756,7 +756,7 @@ pub fn register(
let rv = register
.reset_value
.or(defs.reset_value)
.map(|rv| util::unsuffixed(u64(rv)))
.map(|rv| util::hex(rv))
.ok_or_else(|| {
format!("Register {} has no reset value",
register.name)
Expand Down Expand Up @@ -868,9 +868,9 @@ pub fn fields(
access: Option<Access>,
description: String,
evs: &'a [EnumeratedValues],
mask: Lit,
mask: Tokens,
name: &'a str,
offset: Lit,
offset: Tokens,
pc_r: Ident,
pc_w: Ident,
sc: Ident,
Expand Down Expand Up @@ -914,9 +914,9 @@ pub fn fields(
access: f.access,
evs: &f.enumerated_values,
sc: Ident::new(&*sc),
mask: util::unsuffixed_or_bool((1 << width) - 1, width),
mask: util::hex_or_bool((((1 as u64) << width) - 1) as u32, width),
name: &f.name,
offset: util::unsuffixed(u64(f.bit_range.offset)),
offset: util::unsuffixed(u64::from(f.bit_range.offset)),
ty: width.to_ty()?,
write_constraint: f.write_constraint.as_ref(),
})
Expand Down Expand Up @@ -1077,7 +1077,7 @@ pub fn fields(
.iter()
.map(|v| {
let value =
util::unsuffixed_or_bool(v.value, f.width);
util::hex_or_bool(v.value as u32, f.width);
let pc = &v.pc;

quote! {
Expand Down
47 changes: 37 additions & 10 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::borrow::Cow;
use inflections::Inflect;
use svd::{self, Access, EnumeratedValues, Field, Peripheral, Register,
Usage};
use syn::{self, Ident, IntTy, Lit};
use syn::{self, Ident};
use quote::Tokens;

use errors::*;

Expand Down Expand Up @@ -279,18 +280,44 @@ pub fn access_of(register: &Register) -> Access {
)
}

/// Turns `n` into an unsuffixed literal
pub fn unsuffixed(n: u64) -> Lit {
Lit::Int(n, IntTy::Unsuffixed)
/// Turns `n` into an unsuffixed separated hex token
pub fn hex(n: u32) -> Tokens {
let mut t = Tokens::new();
let (h2, h1) = ((n >> 16) & 0xffff, n & 0xffff);
t.append(if h2 != 0 {
format!("0x{:04x}_{:04x}", h2, h1)
} else if h1 & 0xff00 != 0 {
format!("0x{:04x}", h1)
} else if h1 != 0 {
format!("0x{:02x}", h1 & 0xff)
} else {
String::from("0")
});
t
}

pub fn unsuffixed_or_bool(n: u64, width: u32) -> Lit {
pub fn hex_or_bool(n: u32, width: u32) -> Tokens {
if width == 1 {
if n == 0 {
Lit::Bool(false)
} else {
Lit::Bool(true)
}
let mut t = Tokens::new();
t.append(if n == 0 { "false" } else { "true" });
t
} else {
hex(n)
}
}

/// Turns `n` into an unsuffixed token
pub fn unsuffixed(n: u64) -> Tokens {
let mut t = Tokens::new();
t.append(format!("{}", n));
t
}

pub fn unsuffixed_or_bool(n: u64, width: u32) -> Tokens {
if width == 1 {
let mut t = Tokens::new();
t.append(if n == 0 { "false" } else { "true" });
t
} else {
unsuffixed(n)
}
Expand Down