Skip to content

Commit a24a245

Browse files
committed
Added function for converting svd registers into rust fields
1 parent f0e85fd commit a24a245

File tree

1 file changed

+54
-35
lines changed

1 file changed

+54
-35
lines changed

src/util.rs

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::borrow::Cow;
22

33
use regex::Regex;
44
use inflections::Inflect;
5+
use svd;
56
use svd::{Access, EnumeratedValues, Field, Peripheral, Register,
67
Usage};
8+
use syn;
79
use syn::{Ident, IntTy, Lit};
8-
use quote::Tokens;
10+
use quote::{Tokens, ToTokens};
911

1012
use errors::*;
1113

@@ -157,40 +159,11 @@ pub fn register_to_rust(register: &Register) -> Tokens {
157159
}
158160
},
159161
Register::Array(ref info, ref array_info) => {
160-
let has_brackets = info.name.contains("[%s]");
161-
162-
let comment = &format!(
163-
"0x{:02x} - {} [{}]",
164-
register.address_offset,
165-
respace(&register.description),
166-
array_info.dim,
167-
)
168-
[..];
169-
170-
let rty = Ident::from({
171-
let name = if has_brackets {
172-
info.name.replace("[%s]", "")
173-
} else {
174-
info.name.replace("%s", "")
175-
};
176-
name.to_sanitized_upper_case().into_owned()
177-
});
178-
179-
let reg_name = Ident::from({
180-
let name = if has_brackets {
181-
info.name.replace("[%s]", "")
182-
} else {
183-
info.name.replace("%s", "")
184-
};
185-
name.to_sanitized_snake_case().into_owned()
186-
});
187-
188-
let length = Ident::from(array_info.dim.to_string());
189-
190-
quote! {
191-
#[doc = #comment]
192-
pub #reg_name : [#rty; #length],
193-
}
162+
let field = convert_svd_register(register);
163+
let mut tokens = Tokens::new();
164+
field.to_tokens(&mut tokens);
165+
Ident::new(",").to_tokens(&mut tokens);
166+
tokens
194167
},
195168
}
196169
}
@@ -244,6 +217,52 @@ pub fn expand(register: &Register) -> Vec<Register> {
244217
out
245218
}
246219

220+
pub fn convert_svd_register(register: &svd::Register) -> syn::Field {
221+
let name_to_ty = |name: &String| -> syn::Ty {
222+
syn::Ty::Path(None, syn::Path{
223+
global: false,
224+
segments: vec![syn::PathSegment{
225+
ident: Ident::new(name.to_sanitized_upper_case()),
226+
parameters: syn::PathParameters::none(),
227+
}],
228+
})
229+
};
230+
231+
match *register {
232+
Register::Single(ref info) => {
233+
syn::Field{
234+
ident: Some(Ident::new(info.name.to_sanitized_snake_case())),
235+
vis: syn::Visibility::Public,
236+
attrs: vec![],
237+
ty: name_to_ty(&info.name),
238+
}
239+
},
240+
Register::Array(ref info, ref array_info) => {
241+
let has_brackets = info.name.contains("[%s]");
242+
243+
let name = if has_brackets {
244+
info.name.replace("[%s]", "")
245+
} else {
246+
info.name.replace("%s", "")
247+
};
248+
249+
let ident = Ident::new(name.to_sanitized_snake_case());
250+
251+
let ty = syn::Ty::Array(
252+
Box::new(name_to_ty(&name)),
253+
syn::ConstExpr::Lit(syn::Lit::Int(array_info.dim as u64, syn::IntTy::Unsuffixed)),
254+
);
255+
256+
syn::Field{
257+
ident: Some(ident),
258+
vis: syn::Visibility::Public,
259+
attrs: vec![],
260+
ty: ty,
261+
}
262+
},
263+
}
264+
}
265+
247266

248267
pub fn sort_by_offset(mut registers: Vec<Register>) -> Vec<Register> {
249268
registers.sort_by_key(|x| x.address_offset);

0 commit comments

Comments
 (0)