Skip to content

Commit dcf626d

Browse files
committed
codegen update
1 parent 893fb64 commit dcf626d

File tree

3 files changed

+56
-89
lines changed

3 files changed

+56
-89
lines changed

codegen/src/codegen/gpio.rs

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fn gen_gpio_ip(ip: &gpio::Ip) -> Result<()> {
2121
let feature = ip_version_to_feature(&ip.version)?;
2222
let ports = merge_pins_by_port(&ip.pins)?;
2323

24-
println!(r#"#[cfg(feature = "{}")]"#, feature);
2524
gen_gpio_macro_call(&ports, &feature)?;
2625
Ok(())
2726
}
@@ -31,7 +30,7 @@ fn ip_version_to_feature(ip_version: &str) -> Result<String> {
3130
Lazy::new(|| Regex::new(r"^STM32(?P<version>\w+)_gpio_v1_0$").unwrap());
3231

3332
let captures = VERSION
34-
.captures(&ip_version)
33+
.captures(ip_version)
3534
.with_context(|| format!("invalid GPIO IP version: {}", ip_version))?;
3635

3736
let version = captures.name("version").unwrap().as_str();
@@ -60,94 +59,58 @@ fn merge_pins_by_port(pins: &[gpio::Pin]) -> Result<Vec<Port>> {
6059
}
6160

6261
fn gen_gpio_macro_call(ports: &[Port], feature: &str) -> Result<()> {
63-
println!("gpio!({{");
64-
65-
gen_pac_list(ports, feature);
66-
67-
println!(" ports: [");
6862
for port in ports {
69-
gen_port(port, feature)?;
63+
println!(r#"#[cfg(feature = "{}")]"#, feature);
64+
gen_port(port)?;
65+
println!();
7066
}
71-
println!(" ],");
7267

73-
println!("}});");
7468
Ok(())
7569
}
7670

77-
fn gen_pac_list(ports: &[Port], feature: &str) {
78-
let mut pac_modules: Vec<_> = ports
79-
.iter()
80-
.map(|port| get_port_pac_module(port, feature))
81-
.collect();
82-
pac_modules.sort_unstable();
83-
pac_modules.dedup();
84-
println!(" pacs: [{}],", pac_modules.join(", "));
85-
}
86-
87-
fn gen_port(port: &Port, feature: &str) -> Result<()> {
88-
let pac_module = get_port_pac_module(port, feature);
89-
let port_index = match port.id {
90-
'A' => 0,
91-
'B' => 1,
92-
'C' => 2,
93-
'D' => 3,
94-
'E' => 4,
95-
'F' => 5,
96-
'G' => 6,
97-
'H' => 7,
98-
_ => unreachable!(),
99-
};
100-
101-
println!(" {{");
71+
fn gen_port(port: &Port) -> Result<()> {
72+
let port_upper = port.id;
73+
let port_lower = port.id.to_ascii_lowercase();
10274
println!(
103-
" port: ({}/{}, {}, {}),",
104-
port.id,
105-
port.id.to_lowercase(),
106-
port_index,
107-
pac_module,
75+
"gpio!(GPIO{0}, gpio{1}, P{0}, '{0}', P{0}n, [",
76+
port_upper, port_lower
10877
);
109-
println!(" pins: [");
11078

11179
for pin in &port.pins {
112-
gen_pin(pin)?;
80+
gen_pin(port_upper, port_lower, pin)?;
11381
}
11482

115-
println!(" ],");
116-
println!(" }},");
83+
println!("]);");
11784
Ok(())
11885
}
11986

120-
fn get_port_pac_module(port: &Port, feature: &str) -> &'static str {
121-
// The registers in ports A and B have different reset values due to the
122-
// presence of debug pins, so they get dedicated PAC modules.
123-
match port.id {
124-
'A' => "gpioa",
125-
'B' => "gpiob",
126-
'D' if feature == "gpio-f373" => "gpiod",
127-
_ => "gpioc",
128-
}
129-
}
130-
131-
fn gen_pin(pin: &gpio::Pin) -> Result<()> {
87+
fn gen_pin(port_upper: char, port_lower: char, pin: &gpio::Pin) -> Result<()> {
13288
let nr = pin.number()?;
13389
let reset_mode = get_pin_reset_mode(pin)?;
134-
let afr = if nr < 8 { 'L' } else { 'H' };
13590
let af_numbers = get_pin_af_numbers(pin)?;
13691

13792
println!(
138-
" {} => {{ reset: {}, afr: {}, af: {:?} }},",
139-
nr, reset_mode, afr, af_numbers,
93+
" P{0}{2}: (p{1}{2}, {2}, {3:?}{4}),",
94+
port_upper,
95+
port_lower,
96+
nr,
97+
af_numbers,
98+
if let Some(rst) = reset_mode {
99+
format!(", {}", rst)
100+
} else {
101+
String::new()
102+
}
140103
);
141104

142105
Ok(())
143106
}
144107

145-
fn get_pin_reset_mode(pin: &gpio::Pin) -> Result<&'static str> {
108+
fn get_pin_reset_mode(pin: &gpio::Pin) -> Result<Option<&'static str>> {
146109
// Debug pins default to their debug function (AF0), everything else
147-
// defaults to input.
110+
// defaults to floating input or analog.
148111
let mode = match (pin.port()?, pin.number()?) {
149-
('A', 13) | ('A', 14) | ('A', 15) | ('B', 3) | ('B', 4) => "AF0<PushPull>",
150-
_ => "Input",
112+
('A', 13) | ('A', 14) | ('A', 15) | ('B', 3) | ('B', 4) => Some("super::Debugger"),
113+
_ => None,
151114
};
152115
Ok(mode)
153116
}

codegen/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn handle_gpio(db_path: PathBuf) -> Result<()> {
3434
}
3535

3636
fn emit_autogen_comment(db: &Db) -> Result<()> {
37-
let package = cubemx::package::load(&db)?;
37+
let package = cubemx::package::load(db)?;
3838
codegen::gen_autogen_comment(&package);
3939

4040
Ok(())

src/gpio.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -666,18 +666,18 @@ gpio!(GPIOA, gpioa, PA, 'A', PAn, [
666666
PA10: (pa10, 10, [1, 3, 4, 5, 6, 7, 8, 10, 15]),
667667
PA11: (pa11, 11, [5, 6, 7, 9, 11, 12, 15]),
668668
PA12: (pa12, 12, [1, 5, 6, 7, 8, 9, 11, 15]),
669-
PA13: (pa13, 13, [0, 1, 3, 5, 7, 15], super::Debugger), // SWDIO, PullUp VeryHigh speed
670-
PA14: (pa14, 14, [0, 3, 4, 6, 7, 15], super::Debugger), // SWCLK, PullDown
671-
PA15: (pa15, 15, [0, 1, 3, 4, 6, 7, 9, 15], super::Debugger), // JTDI, PullUp
669+
PA13: (pa13, 13, [0, 1, 3, 5, 7, 15], super::Debugger),
670+
PA14: (pa14, 14, [0, 3, 4, 6, 7, 15], super::Debugger),
671+
PA15: (pa15, 15, [0, 1, 3, 4, 6, 7, 9, 15], super::Debugger),
672672
]);
673673

674674
#[cfg(feature = "gpio-f302")]
675675
gpio!(GPIOB, gpiob, PB, 'B', PBn, [
676676
PB0: (pb0, 0, [3, 6, 15]),
677677
PB1: (pb1, 1, [3, 6, 8, 15]),
678678
PB2: (pb2, 2, [3, 15]),
679-
PB3: (pb3, 3, [0, 1, 3, 6, 7, 15], super::Debugger), // SWO, VeryHigh speed
680-
PB4: (pb4, 4, [0, 1, 3, 6, 7, 10, 15], super::Debugger), // JTRST, PullUp
679+
PB3: (pb3, 3, [0, 1, 3, 6, 7, 15], super::Debugger),
680+
PB4: (pb4, 4, [0, 1, 3, 6, 7, 10, 15], super::Debugger),
681681
PB5: (pb5, 5, [1, 4, 6, 7, 8, 10, 15]),
682682
PB6: (pb6, 6, [1, 3, 4, 7, 15]),
683683
PB7: (pb7, 7, [1, 3, 4, 7, 15]),
@@ -722,6 +722,7 @@ gpio!(GPIOF, gpiof, PF, 'F', PFn, [
722722
PF1: (pf1, 1, [4, 5]),
723723
]);
724724

725+
725726
#[cfg(feature = "gpio-f303e")]
726727
gpio!(GPIOA, gpioa, PA, 'A', PAn, [
727728
PA0: (pa0, 0, [1, 3, 7, 8, 9, 10, 15]),
@@ -737,18 +738,18 @@ gpio!(GPIOA, gpioa, PA, 'A', PAn, [
737738
PA10: (pa10, 10, [1, 3, 4, 5, 6, 7, 8, 10, 11, 15]),
738739
PA11: (pa11, 11, [5, 6, 7, 8, 9, 10, 11, 12, 15]),
739740
PA12: (pa12, 12, [1, 5, 6, 7, 8, 9, 10, 11, 15]),
740-
PA13: (pa13, 13, [0, 1, 3, 5, 7, 10, 15], super::Debugger), // SWDIO, PullUp VeryHigh speed
741-
PA14: (pa14, 14, [0, 3, 4, 5, 6, 7, 15], super::Debugger), // SWCLK, PullDown
742-
PA15: (pa15, 15, [0, 1, 2, 3, 4, 5, 6, 7, 9, 15], super::Debugger), // JTDI, PullUp
741+
PA13: (pa13, 13, [0, 1, 3, 5, 7, 10, 15], super::Debugger),
742+
PA14: (pa14, 14, [0, 3, 4, 5, 6, 7, 15], super::Debugger),
743+
PA15: (pa15, 15, [0, 1, 2, 3, 4, 5, 6, 7, 9, 15], super::Debugger),
743744
]);
744745

745746
#[cfg(feature = "gpio-f303e")]
746747
gpio!(GPIOB, gpiob, PB, 'B', PBn, [
747748
PB0: (pb0, 0, [2, 3, 4, 6, 15]),
748749
PB1: (pb1, 1, [2, 3, 4, 6, 8, 15]),
749750
PB2: (pb2, 2, [3, 15]),
750-
PB3: (pb3, 3, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger), // SWO, VeryHigh speed
751-
PB4: (pb4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger), // JTRST, PullUp
751+
PB3: (pb3, 3, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger),
752+
PB4: (pb4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger),
752753
PB5: (pb5, 5, [1, 2, 3, 4, 5, 6, 7, 8, 10, 15]),
753754
PB6: (pb6, 6, [1, 2, 3, 4, 5, 6, 7, 10, 15]),
754755
PB7: (pb7, 7, [1, 2, 3, 4, 5, 7, 10, 12, 15]),
@@ -869,6 +870,7 @@ gpio!(GPIOH, gpioh, PH, 'H', PHn, [
869870
PH2: (ph2, 2, [1]),
870871
]);
871872

873+
872874
#[cfg(feature = "gpio-f303")]
873875
gpio!(GPIOA, gpioa, PA, 'A', PAn, [
874876
PA0: (pa0, 0, [1, 3, 7, 8, 9, 10, 15]),
@@ -884,18 +886,18 @@ gpio!(GPIOA, gpioa, PA, 'A', PAn, [
884886
PA10: (pa10, 10, [1, 3, 4, 6, 7, 8, 10, 11, 15]),
885887
PA11: (pa11, 11, [6, 7, 8, 9, 10, 11, 12, 14, 15]),
886888
PA12: (pa12, 12, [1, 6, 7, 8, 9, 10, 11, 14, 15]),
887-
PA13: (pa13, 13, [0, 1, 3, 5, 7, 10, 15], super::Debugger), // SWDIO, PullUp VeryHigh speed
888-
PA14: (pa14, 14, [0, 3, 4, 5, 6, 7, 15], super::Debugger), // SWCLK, PullDown
889-
PA15: (pa15, 15, [0, 1, 2, 4, 5, 6, 7, 9, 15], super::Debugger), // JTDI, PullUp
889+
PA13: (pa13, 13, [0, 1, 3, 5, 7, 10, 15], super::Debugger),
890+
PA14: (pa14, 14, [0, 3, 4, 5, 6, 7, 15], super::Debugger),
891+
PA15: (pa15, 15, [0, 1, 2, 4, 5, 6, 7, 9, 15], super::Debugger),
890892
]);
891893

892894
#[cfg(feature = "gpio-f303")]
893895
gpio!(GPIOB, gpiob, PB, 'B', PBn, [
894896
PB0: (pb0, 0, [2, 3, 4, 6, 15]),
895897
PB1: (pb1, 1, [2, 3, 4, 6, 8, 15]),
896898
PB2: (pb2, 2, [3, 15]),
897-
PB3: (pb3, 3, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger), // SWO, VeryHigh speed
898-
PB4: (pb4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger), // JTRST, PullUp
899+
PB3: (pb3, 3, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger),
900+
PB4: (pb4, 4, [0, 1, 2, 3, 4, 5, 6, 7, 10, 15], super::Debugger),
899901
PB5: (pb5, 5, [1, 2, 3, 4, 5, 6, 7, 10, 15]),
900902
PB6: (pb6, 6, [1, 2, 3, 4, 5, 6, 7, 10, 15]),
901903
PB7: (pb7, 7, [1, 2, 3, 4, 5, 7, 10, 15]),
@@ -980,6 +982,7 @@ gpio!(GPIOF, gpiof, PF, 'F', PFn, [
980982
PF10: (pf10, 10, [1, 3, 5]),
981983
]);
982984

985+
983986
#[cfg(feature = "gpio-f333")]
984987
gpio!(GPIOA, gpioa, PA, 'A', PAn, [
985988
PA0: (pa0, 0, [1, 3, 7, 15]),
@@ -995,18 +998,18 @@ gpio!(GPIOA, gpioa, PA, 'A', PAn, [
995998
PA10: (pa10, 10, [1, 3, 6, 7, 8, 10, 13, 15]),
996999
PA11: (pa11, 11, [6, 7, 9, 11, 12, 13, 15]),
9971000
PA12: (pa12, 12, [1, 6, 7, 8, 9, 11, 13, 15]),
998-
PA13: (pa13, 13, [0, 1, 3, 5, 7, 15], super::Debugger), // SWDIO, PullUp VeryHigh speed
999-
PA14: (pa14, 14, [0, 3, 4, 6, 7, 15], super::Debugger), // SWCLK, PullDown
1000-
PA15: (pa15, 15, [0, 1, 3, 4, 5, 7, 9, 13, 15], super::Debugger), // JTDI, PullUp
1001+
PA13: (pa13, 13, [0, 1, 3, 5, 7, 15], super::Debugger),
1002+
PA14: (pa14, 14, [0, 3, 4, 6, 7, 15], super::Debugger),
1003+
PA15: (pa15, 15, [0, 1, 3, 4, 5, 7, 9, 13, 15], super::Debugger),
10011004
]);
10021005

10031006
#[cfg(feature = "gpio-f333")]
10041007
gpio!(GPIOB, gpiob, PB, 'B', PBn, [
10051008
PB0: (pb0, 0, [2, 3, 6, 15]),
10061009
PB1: (pb1, 1, [2, 3, 6, 8, 13, 15]),
10071010
PB2: (pb2, 2, [3, 13, 15]),
1008-
PB3: (pb3, 3, [0, 1, 3, 5, 7, 10, 12, 13, 15], super::Debugger), // SWO, VeryHigh speed
1009-
PB4: (pb4, 4, [0, 1, 2, 3, 5, 7, 10, 13, 15], super::Debugger), // JTRST, PullUp
1011+
PB3: (pb3, 3, [0, 1, 3, 5, 7, 10, 12, 13, 15], super::Debugger),
1012+
PB4: (pb4, 4, [0, 1, 2, 3, 5, 7, 10, 13, 15], super::Debugger),
10101013
PB5: (pb5, 5, [1, 2, 4, 5, 7, 10, 13, 15]),
10111014
PB6: (pb6, 6, [1, 3, 4, 7, 12, 13, 15]),
10121015
PB7: (pb7, 7, [1, 3, 4, 7, 10, 13, 15]),
@@ -1051,6 +1054,7 @@ gpio!(GPIOF, gpiof, PF, 'F', PFn, [
10511054
PF1: (pf1, 1, []),
10521055
]);
10531056

1057+
10541058
#[cfg(feature = "gpio-f373")]
10551059
gpio!(GPIOA, gpioa, PA, 'A', PAn, [
10561060
PA0: (pa0, 0, [1, 2, 3, 7, 8, 11, 15]),
@@ -1066,18 +1070,18 @@ gpio!(GPIOA, gpioa, PA, 'A', PAn, [
10661070
PA10: (pa10, 10, [1, 3, 4, 5, 7, 9, 10, 15]),
10671071
PA11: (pa11, 11, [2, 5, 6, 7, 8, 9, 10, 14, 15]),
10681072
PA12: (pa12, 12, [1, 2, 6, 7, 8, 9, 10, 14, 15]),
1069-
PA13: (pa13, 13, [0, 1, 2, 3, 5, 6, 7, 10, 15], super::Debugger), // SWDIO, PullUp VeryHigh speed
1070-
PA14: (pa14, 14, [0, 3, 4, 10, 15], super::Debugger), // SWCLK, PullDown
1071-
PA15: (pa15, 15, [0, 1, 3, 4, 5, 6, 10, 15], super::Debugger), // JTDI, PullUp
1073+
PA13: (pa13, 13, [0, 1, 2, 3, 5, 6, 7, 10, 15], super::Debugger),
1074+
PA14: (pa14, 14, [0, 3, 4, 10, 15], super::Debugger),
1075+
PA15: (pa15, 15, [0, 1, 3, 4, 5, 6, 10, 15], super::Debugger),
10721076
]);
10731077

10741078
#[cfg(feature = "gpio-f373")]
10751079
gpio!(GPIOB, gpiob, PB, 'B', PBn, [
10761080
PB0: (pb0, 0, [2, 3, 5, 10, 15]),
10771081
PB1: (pb1, 1, [2, 3, 15]),
10781082
PB2: (pb2, 2, [15]),
1079-
PB3: (pb3, 3, [0, 1, 2, 3, 5, 6, 7, 9, 10, 15], super::Debugger), // SWO, VeryHigh speed
1080-
PB4: (pb4, 4, [0, 1, 2, 3, 5, 6, 7, 9, 10, 15], super::Debugger), // JTRST, PullUp
1083+
PB3: (pb3, 3, [0, 1, 2, 3, 5, 6, 7, 9, 10, 15], super::Debugger),
1084+
PB4: (pb4, 4, [0, 1, 2, 3, 5, 6, 7, 9, 10, 15], super::Debugger),
10811085
PB5: (pb5, 5, [1, 2, 4, 5, 6, 7, 10, 11, 15]),
10821086
PB6: (pb6, 6, [1, 2, 3, 4, 7, 9, 10, 11, 15]),
10831087
PB7: (pb7, 7, [1, 2, 3, 4, 7, 9, 10, 11, 15]),

0 commit comments

Comments
 (0)