|
| 1 | +use crate::cubemx::ip::gpio; |
| 2 | +use anyhow::{Context, Result}; |
| 3 | +use once_cell::sync::Lazy; |
| 4 | +use regex::Regex; |
| 5 | +use std::collections::HashMap; |
| 6 | + |
| 7 | +struct Port<'a> { |
| 8 | + id: char, |
| 9 | + pins: Vec<&'a gpio::Pin>, |
| 10 | +} |
| 11 | + |
| 12 | +pub fn gen_mappings(gpio_ips: &[gpio::Ip]) -> Result<()> { |
| 13 | + for ip in gpio_ips.iter() { |
| 14 | + println!(); |
| 15 | + gen_gpio_ip(ip)?; |
| 16 | + } |
| 17 | + Ok(()) |
| 18 | +} |
| 19 | + |
| 20 | +fn gen_gpio_ip(ip: &gpio::Ip) -> Result<()> { |
| 21 | + let feature = ip_version_to_feature(&ip.version)?; |
| 22 | + let ports = merge_pins_by_port(&ip.pins)?; |
| 23 | + |
| 24 | + println!(r#"#[cfg(feature = "{}")]"#, feature); |
| 25 | + gen_gpio_macro_call(&ports)?; |
| 26 | + Ok(()) |
| 27 | +} |
| 28 | + |
| 29 | +fn ip_version_to_feature(ip_version: &str) -> Result<String> { |
| 30 | + static VERSION: Lazy<Regex> = |
| 31 | + Lazy::new(|| Regex::new(r"^STM32(?P<version>\w+)_gpio_v1_0$").unwrap()); |
| 32 | + |
| 33 | + let captures = VERSION |
| 34 | + .captures(&ip_version) |
| 35 | + .with_context(|| format!("invalid GPIO IP version: {}", ip_version))?; |
| 36 | + |
| 37 | + let version = captures.name("version").unwrap().as_str(); |
| 38 | + let feature = format!("gpio-{}", version.to_lowercase()); |
| 39 | + Ok(feature) |
| 40 | +} |
| 41 | + |
| 42 | +fn merge_pins_by_port(pins: &[gpio::Pin]) -> Result<Vec<Port>> { |
| 43 | + let mut pins_by_port = HashMap::new(); |
| 44 | + for pin in pins.iter() { |
| 45 | + pins_by_port |
| 46 | + .entry(pin.port()?) |
| 47 | + .and_modify(|e: &mut Vec<_>| e.push(pin)) |
| 48 | + .or_insert_with(|| vec![pin]); |
| 49 | + } |
| 50 | + |
| 51 | + let mut ports = Vec::new(); |
| 52 | + for (id, mut pins) in pins_by_port { |
| 53 | + pins.sort_by_key(|p| p.number().unwrap_or_default()); |
| 54 | + pins.dedup_by_key(|p| p.number().unwrap_or_default()); |
| 55 | + ports.push(Port { id, pins }); |
| 56 | + } |
| 57 | + ports.sort_by_key(|p| p.id); |
| 58 | + |
| 59 | + Ok(ports) |
| 60 | +} |
| 61 | + |
| 62 | +fn gen_gpio_macro_call(ports: &[Port]) -> Result<()> { |
| 63 | + println!("gpio!(["); |
| 64 | + for port in ports { |
| 65 | + gen_port(port)?; |
| 66 | + } |
| 67 | + println!("]);"); |
| 68 | + Ok(()) |
| 69 | +} |
| 70 | + |
| 71 | +fn gen_port(port: &Port) -> Result<()> { |
| 72 | + let pac_module = get_port_pac_module(port); |
| 73 | + |
| 74 | + println!(" {{"); |
| 75 | + println!( |
| 76 | + " port: ({}/{}, pac: {}),", |
| 77 | + port.id, |
| 78 | + port.id.to_lowercase(), |
| 79 | + pac_module, |
| 80 | + ); |
| 81 | + println!(" pins: ["); |
| 82 | + |
| 83 | + for pin in port.pins.iter() { |
| 84 | + gen_pin(pin)?; |
| 85 | + } |
| 86 | + |
| 87 | + println!(" ],"); |
| 88 | + println!(" }},"); |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +fn get_port_pac_module(port: &Port) -> &'static str { |
| 93 | + // The registers in ports A and B have different reset values due to the |
| 94 | + // presence of debug pins, so they get dedicated PAC modules. |
| 95 | + match port.id { |
| 96 | + 'A' => "gpioa", |
| 97 | + 'B' => "gpiob", |
| 98 | + _ => "gpioc", |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +fn gen_pin(pin: &gpio::Pin) -> Result<()> { |
| 103 | + let nr = pin.number()?; |
| 104 | + let reset_mode = get_pin_reset_mode(pin)?; |
| 105 | + let afr = if nr < 8 { 'L' } else { 'H' }; |
| 106 | + let af_numbers = get_pin_af_numbers(pin)?; |
| 107 | + |
| 108 | + println!( |
| 109 | + " {} => {{ reset: {}, afr: {}/{}, af: {:?} }},", |
| 110 | + nr, |
| 111 | + reset_mode, |
| 112 | + afr, |
| 113 | + afr.to_lowercase(), |
| 114 | + af_numbers, |
| 115 | + ); |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | + |
| 120 | +fn get_pin_reset_mode(pin: &gpio::Pin) -> Result<&'static str> { |
| 121 | + // Debug pins default to their debug function (AF0), everything else |
| 122 | + // defaults to floating input. |
| 123 | + let mode = match (pin.port()?, pin.number()?) { |
| 124 | + ('A', 13) | ('A', 14) | ('A', 15) | ('B', 3) | ('B', 4) => "AF0", |
| 125 | + _ => "Input<Floating>", |
| 126 | + }; |
| 127 | + Ok(mode) |
| 128 | +} |
| 129 | + |
| 130 | +fn get_pin_af_numbers(pin: &gpio::Pin) -> Result<Vec<u8>> { |
| 131 | + let mut numbers = Vec::new(); |
| 132 | + for signal in pin.pin_signals.iter() { |
| 133 | + numbers.push(signal.af()?); |
| 134 | + } |
| 135 | + |
| 136 | + numbers.sort(); |
| 137 | + numbers.dedup(); |
| 138 | + |
| 139 | + Ok(numbers) |
| 140 | +} |
0 commit comments