cube2rust/
i2c.rs

1use crate::*;
2use regex::Regex;
3
4#[derive(Debug)]
5pub struct I2C {
6    pub name_lower: String,
7    pub name_upper: String,
8    pub mode: Option<Mode>,
9}
10
11pub fn get_i2cs(config: &ConfigParams<'_>) -> anyhow::Result<Vec<I2C>> {
12    let mut i2cs = Vec::new();
13
14    // regex matches I2C1_SDA, I2C2_SDA, etc
15    let re = Regex::new(r"^(I2C\d)_SDA").unwrap();
16
17    // if the I2C is left at default values CubeMX will not generate an entry for it in the ioc file
18    // so we search for a I2C<i>_SDA signal set on a GPIO
19    for v in config.values() {
20        if let Some(signal) = v.get("Signal") {
21            if let Some(captures) = re.captures(signal) {
22                let name_upper = String::from(captures.get(1).unwrap().as_str());
23                let name_lower = name_upper.to_ascii_lowercase();
24                let mut mode = None;
25
26                if let Some(i2c_params) = config.get::<str>(&name_upper) {
27                    mode = parse_optional_param(i2c_params, "I2C_Speed_Mode")?;
28                }
29
30                i2cs.push(I2C {
31                    name_lower,
32                    name_upper,
33                    mode,
34                });
35            }
36        };
37    }
38
39    Ok(i2cs)
40}
41
42parameter!(
43    Mode,
44    [I2C_Standard, I2C_Fast, I2C_Fast_Plus],
45    default = I2C_Standard
46);