Skip to content

Commit e306adf

Browse files
committed
Implemented new functions to format literals as seperated hex tokens
The two main reasons behind this change are ensuring that the numbers (mostly addresses, masks and default values) line up with the datasheet for easier searching and verification since those are pretty much always expressed in hexadecimal. The other reason being clippy now complains about long numeric literals (without seperators) being hard to read -- I agree. Signed-off-by: Daniel Egger <[email protected]>
1 parent ed9ba7f commit e306adf

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/generate.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cast::u64;
55
use quote::{Tokens, ToTokens};
66
use svd::{Access, BitRange, Defaults, Device, EnumeratedValues, Field,
77
Peripheral, Register, Usage, WriteConstraint};
8-
use syn::{self, Ident, Lit};
8+
use syn::{self, Ident};
99

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

430430
items.push(quote! {
@@ -756,7 +756,7 @@ pub fn register(
756756
let rv = register
757757
.reset_value
758758
.or(defs.reset_value)
759-
.map(|rv| util::unsuffixed(u64(rv)))
759+
.map(|rv| util::hex(rv))
760760
.ok_or_else(|| {
761761
format!("Register {} has no reset value",
762762
register.name)
@@ -868,9 +868,9 @@ pub fn fields(
868868
access: Option<Access>,
869869
description: String,
870870
evs: &'a [EnumeratedValues],
871-
mask: Lit,
871+
mask: Tokens,
872872
name: &'a str,
873-
offset: Lit,
873+
offset: Tokens,
874874
pc_r: Ident,
875875
pc_w: Ident,
876876
sc: Ident,
@@ -914,9 +914,9 @@ pub fn fields(
914914
access: f.access,
915915
evs: &f.enumerated_values,
916916
sc: Ident::new(&*sc),
917-
mask: util::unsuffixed_or_bool((1 << width) - 1, width),
917+
mask: util::hex_or_bool((((1 as u64) << width) - 1) as u32, width),
918918
name: &f.name,
919-
offset: util::unsuffixed(u64(f.bit_range.offset)),
919+
offset: util::hex(f.bit_range.offset),
920920
ty: width.to_ty()?,
921921
write_constraint: f.write_constraint.as_ref(),
922922
})
@@ -1077,7 +1077,7 @@ pub fn fields(
10771077
.iter()
10781078
.map(|v| {
10791079
let value =
1080-
util::unsuffixed_or_bool(v.value, f.width);
1080+
util::hex_or_bool(v.value as u32, f.width);
10811081
let pc = &v.pc;
10821082

10831083
quote! {

src/util.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use inflections::Inflect;
44
use svd::{self, Access, EnumeratedValues, Field, Peripheral, Register,
55
Usage};
66
use syn::{self, Ident, IntTy, Lit};
7+
use quote::Tokens;
78

89
use errors::*;
910

@@ -279,6 +280,32 @@ pub fn access_of(register: &Register) -> Access {
279280
)
280281
}
281282

283+
/// Turns `n` into an unsuffixed separated hex tokens
284+
pub fn hex(n: u32) -> Tokens {
285+
let mut t = Tokens::new();
286+
let (h2, h1) = ((n >> 16) & 0xffff, n & 0xffff);
287+
t.append(if h2 != 0 {
288+
format!("0x{:04x}_{:04x}", h2, h1)
289+
} else if h1 & 0xff00 != 0 {
290+
format!("0x{:04x}", h1)
291+
} else if h1 != 0 {
292+
format!("0x{:02x}", h1 & 0xff)
293+
} else {
294+
String::from("0")
295+
});
296+
t
297+
}
298+
299+
pub fn hex_or_bool(n: u32, width: u32) -> Tokens {
300+
if width == 1 {
301+
let mut t = Tokens::new();
302+
t.append(if n == 0 { "false" } else { "true" });
303+
t
304+
} else {
305+
hex(n)
306+
}
307+
}
308+
282309
/// Turns `n` into an unsuffixed literal
283310
pub fn unsuffixed(n: u64) -> Lit {
284311
Lit::Int(n, IntTy::Unsuffixed)

0 commit comments

Comments
 (0)