Skip to content

Commit 0532305

Browse files
Merge #562
562: feat: Add keep_list arg to allow not to generate arrays r=burrbull a=duskmoon314 ## New feature Add a `keep_list` arg to allow not to generate arrays. ```shell svd2rust --help # --keep_list Keep lists when generating code of dimElement, instead of trying to generate arrays svd2rust --target riscv --keep_list -i soc.svd ``` Use `[%s]` to create arrays and use `%s` to create lists ref: https://www.keil.com/pack/doc/CMSIS/SVD/html/elem_special.html ## Motivation Issue #561 Might be useful if someone needs lists. Co-authored-by: Campbell He <[email protected]>
2 parents ad58ed3 + 619d2d0 commit 0532305

File tree

4 files changed

+115
-86
lines changed

4 files changed

+115
-86
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- Fix ValidateLevel usage in lib.rs
2323
- Parenthesizing `#offset_calc` to avoid clippy's warning of operator precedence
2424

25+
### Added
26+
27+
- `keep_list` option
28+
2529
## [v0.20.0] - 2021-12-07
2630

2731
### Fixed

src/generate/peripheral.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,15 @@ fn expand_cluster(
712712
.eq((0..array_info.dim).map(Ok))
713713
});
714714

715-
let array_convertible = sequential_indexes && sequential_addresses;
715+
let convert_list = match config.keep_list {
716+
true => match &array_info.dim_name {
717+
Some(dim_name) => dim_name.contains("[%s]"),
718+
None => info.name.contains("[%s]"),
719+
},
720+
false => true,
721+
};
722+
723+
let array_convertible = sequential_indexes && sequential_addresses && convert_list;
716724

717725
if array_convertible {
718726
cluster_expanded.push(RegisterBlockField {
@@ -776,7 +784,15 @@ fn expand_register(
776784
.eq((0..array_info.dim).map(Ok))
777785
});
778786

779-
let array_convertible = sequential_indexes && sequential_addresses;
787+
let convert_list = match config.keep_list {
788+
true => match &array_info.dim_name {
789+
Some(dim_name) => dim_name.contains("[%s]"),
790+
None => info.name.contains("[%s]"),
791+
},
792+
false => true,
793+
};
794+
795+
let array_convertible = sequential_indexes && sequential_addresses && convert_list;
780796

781797
if array_convertible {
782798
register_expanded.push(RegisterBlockField {

src/main.rs

Lines changed: 91 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,91 +19,95 @@ fn run() -> Result<()> {
1919
use clap_conf::prelude::*;
2020
use std::io::Read;
2121

22-
let matches =
23-
App::new("svd2rust")
24-
.about("Generate a Rust API from SVD files")
25-
.arg(
26-
Arg::with_name("input")
27-
.help("Input SVD file")
28-
.short("i")
29-
.takes_value(true)
30-
.value_name("FILE"),
31-
)
32-
.arg(
33-
Arg::with_name("output")
34-
.long("output-dir")
35-
.help("Directory to place generated files")
36-
.short("o")
37-
.takes_value(true)
38-
.value_name("PATH"),
39-
)
40-
.arg(
41-
Arg::with_name("config")
42-
.long("config")
43-
.help("Config TOML file")
44-
.short("c")
45-
.takes_value(true)
46-
.value_name("TOML_FILE"),
47-
)
48-
.arg(
49-
Arg::with_name("target")
50-
.long("target")
51-
.help("Target architecture")
52-
.takes_value(true)
53-
.value_name("ARCH"),
54-
)
55-
.arg(
56-
Arg::with_name("nightly_features")
57-
.long("nightly")
58-
.help("Enable features only available to nightly rustc"),
59-
)
60-
.arg(Arg::with_name("const_generic").long("const_generic").help(
22+
let matches = App::new("svd2rust")
23+
.about("Generate a Rust API from SVD files")
24+
.arg(
25+
Arg::with_name("input")
26+
.help("Input SVD file")
27+
.short("i")
28+
.takes_value(true)
29+
.value_name("FILE"),
30+
)
31+
.arg(
32+
Arg::with_name("output")
33+
.long("output-dir")
34+
.help("Directory to place generated files")
35+
.short("o")
36+
.takes_value(true)
37+
.value_name("PATH"),
38+
)
39+
.arg(
40+
Arg::with_name("config")
41+
.long("config")
42+
.help("Config TOML file")
43+
.short("c")
44+
.takes_value(true)
45+
.value_name("TOML_FILE"),
46+
)
47+
.arg(
48+
Arg::with_name("target")
49+
.long("target")
50+
.help("Target architecture")
51+
.takes_value(true)
52+
.value_name("ARCH"),
53+
)
54+
.arg(
55+
Arg::with_name("nightly_features")
56+
.long("nightly")
57+
.help("Enable features only available to nightly rustc"),
58+
)
59+
.arg(
60+
Arg::with_name("const_generic").long("const_generic").help(
6161
"Use const generics to generate writers for same fields with different offsets",
62-
))
63-
.arg(
64-
Arg::with_name("ignore_groups")
65-
.long("ignore_groups")
66-
.help("Don't add alternateGroup name as prefix to register name"),
67-
)
68-
.arg(
69-
Arg::with_name("generic_mod")
70-
.long("generic_mod")
71-
.short("g")
72-
.help("Push generic mod in separate file"),
73-
)
74-
.arg(
75-
Arg::with_name("make_mod")
76-
.long("make_mod")
77-
.short("m")
78-
.help("Create mod.rs instead of lib.rs, without inner attributes"),
79-
)
80-
.arg(
81-
Arg::with_name("strict")
82-
.long("strict")
83-
.short("s")
84-
.help("Make advanced checks due to parsing SVD"),
85-
)
86-
.arg(
87-
Arg::with_name("source_type")
88-
.long("source_type")
89-
.help("Specify file/stream format"),
90-
)
91-
.arg(
92-
Arg::with_name("log_level")
93-
.long("log")
94-
.short("l")
95-
.help(&format!(
96-
"Choose which messages to log (overrides {})",
97-
env_logger::DEFAULT_FILTER_ENV
98-
))
99-
.takes_value(true)
100-
.possible_values(&["off", "error", "warn", "info", "debug", "trace"]),
101-
)
102-
.version(concat!(
103-
env!("CARGO_PKG_VERSION"),
104-
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
105-
))
106-
.get_matches();
62+
),
63+
)
64+
.arg(
65+
Arg::with_name("ignore_groups")
66+
.long("ignore_groups")
67+
.help("Don't add alternateGroup name as prefix to register name"),
68+
)
69+
.arg(Arg::with_name("keep_list").long("keep_list").help(
70+
"Keep lists when generating code of dimElement, instead of trying to generate arrays",
71+
))
72+
.arg(
73+
Arg::with_name("generic_mod")
74+
.long("generic_mod")
75+
.short("g")
76+
.help("Push generic mod in separate file"),
77+
)
78+
.arg(
79+
Arg::with_name("make_mod")
80+
.long("make_mod")
81+
.short("m")
82+
.help("Create mod.rs instead of lib.rs, without inner attributes"),
83+
)
84+
.arg(
85+
Arg::with_name("strict")
86+
.long("strict")
87+
.short("s")
88+
.help("Make advanced checks due to parsing SVD"),
89+
)
90+
.arg(
91+
Arg::with_name("source_type")
92+
.long("source_type")
93+
.help("Specify file/stream format"),
94+
)
95+
.arg(
96+
Arg::with_name("log_level")
97+
.long("log")
98+
.short("l")
99+
.help(&format!(
100+
"Choose which messages to log (overrides {})",
101+
env_logger::DEFAULT_FILTER_ENV
102+
))
103+
.takes_value(true)
104+
.possible_values(&["off", "error", "warn", "info", "debug", "trace"]),
105+
)
106+
.version(concat!(
107+
env!("CARGO_PKG_VERSION"),
108+
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
109+
))
110+
.get_matches();
107111

108112
let input = &mut String::new();
109113
match matches.value_of("input") {
@@ -148,6 +152,8 @@ fn run() -> Result<()> {
148152
cfg.bool_flag("const_generic", Filter::Arg) || cfg.bool_flag("const_generic", Filter::Conf);
149153
let ignore_groups =
150154
cfg.bool_flag("ignore_groups", Filter::Arg) || cfg.bool_flag("ignore_groups", Filter::Conf);
155+
let keep_list =
156+
cfg.bool_flag("keep_list", Filter::Arg) || cfg.bool_flag("keep_list", Filter::Conf);
151157
let strict = cfg.bool_flag("strict", Filter::Arg) || cfg.bool_flag("strict", Filter::Conf);
152158

153159
let mut source_type = cfg
@@ -169,6 +175,7 @@ fn run() -> Result<()> {
169175
make_mod,
170176
const_generic,
171177
ignore_groups,
178+
keep_list,
172179
strict,
173180
output_dir: path.clone(),
174181
source_type,

src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub struct Config {
2424
pub make_mod: bool,
2525
pub const_generic: bool,
2626
pub ignore_groups: bool,
27+
pub keep_list: bool,
2728
pub strict: bool,
2829
pub output_dir: PathBuf,
2930
pub source_type: SourceType,
@@ -38,6 +39,7 @@ impl Default for Config {
3839
make_mod: false,
3940
const_generic: false,
4041
ignore_groups: false,
42+
keep_list: false,
4143
strict: false,
4244
output_dir: PathBuf::from("."),
4345
source_type: SourceType::default(),

0 commit comments

Comments
 (0)