Merge pull request #475 from embassy-rs/adc-fix

Fix chips with multiple ADC_COMMONs.
This commit is contained in:
Dario Nieuwenhuis 2024-04-28 21:31:12 +00:00 committed by GitHub
commit 85a82da243
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 77 additions and 76 deletions

View File

@ -4,6 +4,8 @@ use std::collections::{HashMap, HashSet};
use stm32_data_serde::chip::core::peripheral::Pin; use stm32_data_serde::chip::core::peripheral::Pin;
use super::*; use super::*;
use crate::gpio_af::parse_signal_name;
use crate::normalize_peris::normalize_peri_name;
mod xml { mod xml {
use serde::Deserialize; use serde::Deserialize;
@ -213,24 +215,20 @@ impl PeriMatcher {
("STM32WL5.*:ADC:.*", ("adc", "g0", "ADC")), ("STM32WL5.*:ADC:.*", ("adc", "g0", "ADC")),
("STM32WLE.*:ADC:.*", ("adc", "g0", "ADC")), ("STM32WLE.*:ADC:.*", ("adc", "g0", "ADC")),
("STM32G0.*:ADC:.*", ("adc", "g0", "ADC")), ("STM32G0.*:ADC:.*", ("adc", "g0", "ADC")),
("STM32G0.*:ADC_COMMON:.*", ("adccommon", "v3", "ADC_COMMON")),
("STM32U0.*:ADC:.*", ("adc", "u0", "ADC")), ("STM32U0.*:ADC:.*", ("adc", "u0", "ADC")),
("STM32U0.*:ADC_COMMON:.*", ("adccommon", "v3", "ADC_COMMON")),
("STM32G4.*:ADC:.*", ("adc", "g4", "ADC")), ("STM32G4.*:ADC:.*", ("adc", "g4", "ADC")),
("STM32G4.*:ADC_COMMON:.*", ("adccommon", "v4", "ADC_COMMON")), ("STM32G0.*:ADC\\d*_COMMON:.*", ("adccommon", "v3", "ADC_COMMON")),
(".*:ADC_COMMON:aditf2_v1_1", ("adccommon", "v2", "ADC_COMMON")), ("STM32U0.*:ADC\\d*_COMMON:.*", ("adccommon", "v3", "ADC_COMMON")),
(".*:ADC_COMMON:aditf5_v2_0", ("adccommon", "v3", "ADC_COMMON")), ("STM32G4.*:ADC\\d*_COMMON:.*", ("adccommon", "v4", "ADC_COMMON")),
(".*:ADC_COMMON:aditf5_v2_2", ("adccommon", "v3", "ADC_COMMON")),
(".*:ADC_COMMON:aditf4_v3_0_WL", ("adccommon", "v3", "ADC_COMMON")),
(".*:ADC_COMMON:aditf5_v1_1", ("adccommon", "f3", "ADC_COMMON")),
(".*:ADC3_COMMON:aditf5_v1_1", ("adccommon", "f3", "ADC_COMMON")),
( (
"STM32H50.*:ADC_COMMON:aditf5_v3_0_H5", "STM32(L[45]|W[BL]).*:ADC\\d*_COMMON:.*",
("adccommon", "h50", "ADC_COMMON"), ("adccommon", "v3", "ADC_COMMON"),
), ),
("STM32H5.*:ADC_COMMON:aditf5_v3_0_H5", ("adccommon", "h5", "ADC_COMMON")), ("STM32F3.*:ADC\\d*_COMMON:.*", ("adccommon", "f3", "ADC_COMMON")),
("STM32H7.*:ADC_COMMON:.*", ("adccommon", "v4", "ADC_COMMON")), ("STM32F[247].*:ADC\\d*_COMMON:.*", ("adccommon", "v2", "ADC_COMMON")),
("STM32H7.*:ADC3_COMMON:.*", ("adccommon", "v4", "ADC_COMMON")), ("STM32H50.*:ADC\\d*_COMMON:.*", ("adccommon", "h50", "ADC_COMMON")),
("STM32H5.*:ADC\\d*_COMMON:.*", ("adccommon", "h5", "ADC_COMMON")),
("STM32H7.*:ADC\\d*_COMMON:.*", ("adccommon", "v4", "ADC_COMMON")),
("STM32G4.*:OPAMP:G4_tsmc90_fastOpamp", ("opamp", "g4", "OPAMP")), ("STM32G4.*:OPAMP:G4_tsmc90_fastOpamp", ("opamp", "g4", "OPAMP")),
("STM32F3.*:OPAMP:tsmc018_ull_opamp_v1_0", ("opamp", "f3", "OPAMP")), ("STM32F3.*:OPAMP:tsmc018_ull_opamp_v1_0", ("opamp", "f3", "OPAMP")),
("STM32H7.*:OPAMP:.*", ("opamp", "h_v1", "OPAMP")), ("STM32H7.*:OPAMP:.*", ("opamp", "h_v1", "OPAMP")),
@ -1007,7 +1005,7 @@ fn process_core(
let mut peri_kinds = HashMap::new(); let mut peri_kinds = HashMap::new();
for ip in group.ips.values() { for ip in group.ips.values() {
let pname = ip.instance_name.clone(); let pname = normalize_peri_name(&ip.instance_name);
let pkind = format!("{}:{}", ip.name, ip.version); let pkind = format!("{}:{}", ip.name, ip.version);
let pkind = pkind.strip_suffix("_Cube").unwrap_or(&pkind); let pkind = pkind.strip_suffix("_Cube").unwrap_or(&pkind);
@ -1020,6 +1018,14 @@ fn process_core(
"IRTIM", "IRTIM",
// We add this as ghost peri // We add this as ghost peri
"SYS", "SYS",
"ADC_COMMON",
"ADC1_COMMON",
"ADC12_COMMON",
"ADC123_COMMON",
"ADC3_COMMON",
"ADC4_COMMON",
"ADC34_COMMON",
"ADC345_COMMON",
// These are software libraries // These are software libraries
"FREERTOS", "FREERTOS",
"PDM2PCM", "PDM2PCM",
@ -1034,29 +1040,11 @@ fn process_core(
"TOUCHSENSING", "TOUCHSENSING",
]; ];
if FAKE_PERIPHERALS.contains(&pname.as_str()) { if FAKE_PERIPHERALS.contains(&pname) {
continue; continue;
} }
let pname = match pname.as_str() { peri_kinds.insert(pname.to_string(), pkind.to_string());
"HDMI_CEC" => "CEC".to_string(),
"SUBGHZ" => "SUBGHZSPI".to_string(),
// remove when https://github.com/stm32-rs/stm32-rs/pull/789 merges
"USB_DRD_FS" => "USB".to_string(),
_ => pname,
};
if pname.starts_with("ADC") {
if let Entry::Vacant(entry) = peri_kinds.entry("ADC_COMMON".to_string()) {
entry.insert(format!("ADC_COMMON:{}", ip.version.strip_suffix("_Cube").unwrap()));
}
}
if pname.starts_with("ADC3") && (chip_name.starts_with("STM32H7") || chip_name.starts_with("STM32F3")) {
if let Entry::Vacant(entry) = peri_kinds.entry("ADC3_COMMON".to_string()) {
entry.insert(format!("ADC3_COMMON:{}", ip.version.strip_suffix("_Cube").unwrap()));
}
}
peri_kinds.insert(pname, pkind.to_string());
} }
const GHOST_PERIS: &[&str] = &[ const GHOST_PERIS: &[&str] = &[
"GPIOA", "GPIOA",
@ -1098,9 +1086,17 @@ fn process_core(
"VREFINTCAL", "VREFINTCAL",
"UID", "UID",
"HSEM", "HSEM",
"ADC1_COMMON",
"ADC12_COMMON",
"ADC123_COMMON",
"ADC3_COMMON",
"ADC4_COMMON",
"ADC34_COMMON",
"ADC345_COMMON",
]; ];
for pname in GHOST_PERIS { for pname in GHOST_PERIS {
if let Entry::Vacant(entry) = peri_kinds.entry(pname.to_string()) { let normalized_pname = normalize_peri_name(pname);
if let Entry::Vacant(entry) = peri_kinds.entry(normalized_pname.to_string()) {
if defines.get_peri_addr(pname).is_some() { if defines.get_peri_addr(pname).is_some() {
entry.insert("unknown".to_string()); entry.insert("unknown".to_string());
} }
@ -1140,27 +1136,21 @@ fn process_core(
let mut periph_pins = HashMap::<_, Vec<_>>::new(); let mut periph_pins = HashMap::<_, Vec<_>>::new();
for (pin_name, pin) in &group.pins { for (pin_name, pin) in &group.pins {
for signal in &pin.signals { for signal in &pin.signals {
let mut signal = signal.name.clone(); let signal = &signal.name;
if signal.starts_with("DEBUG_SUBGHZSPI-") {
signal = format!("SUBGHZSPI_{}", &signal[16..(signal.len() - 3)]);
}
// TODO: What are those signals (well, GPIO is clear) Which peripheral do they belong to? // TODO: What are those signals (well, GPIO is clear) Which peripheral do they belong to?
if !["GPIO", "CEC", "AUDIOCLK", "VDDTCXO"].contains(&signal.as_str()) && !signal.contains("EXTI") { if ["GPIO", "CEC", "AUDIOCLK", "VDDTCXO"].contains(&signal.as_str()) || signal.contains("EXTI") {
// both peripherals and signals can have underscores in their names so there is no easy way to split continue;
// check if signal name starts with one of the peripheral names }
for periph in peri_kinds.keys() { let Some((signal_peri, signal_name)) = parse_signal_name(signal) else {
if let Some(signal) = signal.strip_prefix(&format!("{periph}_")) { continue;
periph_pins.entry(periph.to_string()).or_default().push( };
periph_pins.entry(signal_peri.to_string()).or_default().push(
stm32_data_serde::chip::core::peripheral::Pin { stm32_data_serde::chip::core::peripheral::Pin {
pin: pin_name.clone(), pin: pin_name.clone(),
signal: signal.to_string(), signal: signal_name.to_string(),
af: None, af: None,
}, },
); );
break;
}
}
}
} }
} }
for pins in periph_pins.values_mut() { for pins in periph_pins.values_mut() {
@ -1174,15 +1164,7 @@ fn process_core(
continue; continue;
} }
let addr = if (chip_name.starts_with("STM32F0") let addr = if let Some(cap) = regex!(r"^FDCANRAM(?P<idx>[0-9]+)$").captures(&pname) {
|| chip_name.starts_with("STM32L1")
|| chip_name.starts_with("STM32L0"))
&& pname == "ADC"
{
defines.get_peri_addr("ADC1")
} else if chip_name.starts_with("STM32H7") && pname == "HRTIM" {
defines.get_peri_addr("HRTIM1")
} else if let Some(cap) = regex!(r"^FDCANRAM(?P<idx>[0-9]+)$").captures(&pname) {
defines.get_peri_addr("FDCANRAM").map(|addr| { defines.get_peri_addr("FDCANRAM").map(|addr| {
if chip_name.starts_with("STM32H7") { if chip_name.starts_with("STM32H7") {
addr addr
@ -1197,10 +1179,7 @@ fn process_core(
defines.get_peri_addr(&pname) defines.get_peri_addr(&pname)
}; };
let addr = match addr { let Some(addr) = addr else { continue };
Some(addr) => addr,
None => continue,
};
let mut p = stm32_data_serde::chip::core::Peripheral { let mut p = stm32_data_serde::chip::core::Peripheral {
name: if pname == "SBS" { name: if pname == "SBS" {

View File

@ -2,6 +2,8 @@ use std::collections::HashMap;
use anyhow::Context; use anyhow::Context;
use crate::normalize_peris::normalize_peri_name;
mod xml { mod xml {
use serde::Deserialize; use serde::Deserialize;
@ -150,7 +152,7 @@ impl DmaChannels {
}; };
chip_dma chip_dma
.peripherals .peripherals
.entry(target_peri_name.to_string()) .entry(normalize_peri_name(target_peri_name).to_string())
.or_default() .or_default()
.push(stm32_data_serde::chip::core::peripheral::DmaChannel { .push(stm32_data_serde::chip::core::peripheral::DmaChannel {
signal: request.to_string(), signal: request.to_string(),
@ -270,7 +272,7 @@ impl DmaChannels {
}; };
chip_dma chip_dma
.peripherals .peripherals
.entry(target_peri_name.to_string()) .entry(normalize_peri_name(target_peri_name).to_string())
.or_default() .or_default()
.push(entry); .push(entry);
} }
@ -320,7 +322,7 @@ impl DmaChannels {
}; };
chip_dma chip_dma
.peripherals .peripherals
.entry(target_peri_name.to_string()) .entry(normalize_peri_name(target_peri_name).to_string())
.or_default() .or_default()
.push(stm32_data_serde::chip::core::peripheral::DmaChannel { .push(stm32_data_serde::chip::core::peripheral::DmaChannel {
signal: request.to_string(), signal: request.to_string(),

View File

@ -1,5 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::normalize_peris::normalize_peri_name;
use crate::regex; use crate::regex;
mod xml { mod xml {
@ -102,7 +103,7 @@ impl Af {
} }
} }
fn parse_signal_name(signal_name: &str) -> Option<(&str, &str)> { pub fn parse_signal_name(signal_name: &str) -> Option<(&str, &str)> {
let (peri_name, signal_name) = { let (peri_name, signal_name) = {
if let Some(signal_name) = signal_name.strip_prefix("USB_OTG_FS_") { if let Some(signal_name) = signal_name.strip_prefix("USB_OTG_FS_") {
("USB_OTG_FS", signal_name) ("USB_OTG_FS", signal_name)
@ -121,6 +122,6 @@ fn parse_signal_name(signal_name: &str) -> Option<(&str, &str)> {
Some((peri_name, signal_name.strip_suffix("OUT").unwrap_or(signal_name))) Some((peri_name, signal_name.strip_suffix("OUT").unwrap_or(signal_name)))
} else { } else {
Some((peri_name, signal_name)) Some((normalize_peri_name(peri_name), signal_name))
} }
} }

View File

@ -175,11 +175,12 @@ impl Defines {
&["OCTOSPI2_R_BASE", "OCTOSPI2_R_BASE_NS", "OCTOSPI2_REG_BASE"], &["OCTOSPI2_R_BASE", "OCTOSPI2_R_BASE_NS", "OCTOSPI2_REG_BASE"],
), ),
("FLASH", &["FLASH_R_BASE", "FLASH_REG_BASE"]), ("FLASH", &["FLASH_R_BASE", "FLASH_REG_BASE"]),
("ADC", &["ADC1_BASE", "ADC_BASE"]),
("ADC1", &["ADC1_BASE", "ADC_BASE"]),
( (
"ADC_COMMON", "ADC1_COMMON",
&["ADC_COMMON", "ADC1_COMMON", "ADC12_COMMON", "ADC123_COMMON"], &["ADC1_COMMON_BASE", "ADC_COMMON_BASE", "ADC1_COMMON", "ADC_COMMON"],
), ),
("ADC3_COMMON", &["ADC3_COMMON", "ADC4_COMMON", "ADC34_COMMON"]),
("CAN", &["CAN_BASE", "CAN1_BASE"]), ("CAN", &["CAN_BASE", "CAN1_BASE"]),
("FMC", &["FMC_BASE", "FMC_R_BASE"]), ("FMC", &["FMC_BASE", "FMC_R_BASE"]),
("FSMC", &["FSMC_R_BASE"]), ("FSMC", &["FSMC_R_BASE"]),

View File

@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
use log::*; use log::*;
use crate::chips::ChipGroup; use crate::chips::ChipGroup;
use crate::normalize_peris::normalize_peri_name;
use crate::regex; use crate::regex;
mod xml { mod xml {
@ -342,6 +343,7 @@ impl ChipInterrupts {
} }
for (p, s) in interrupt_signals { for (p, s) in interrupt_signals {
let p = normalize_peri_name(&p).to_string();
let signals = chip_signals.entry(p).or_default(); let signals = chip_signals.entry(p).or_default();
let irqs = signals.entry(s).or_default(); let irqs = signals.entry(s).or_default();
irqs.insert(header_name.clone()); irqs.insert(header_name.clone());

View File

@ -5,6 +5,7 @@ mod gpio_af;
mod header; mod header;
mod interrupts; mod interrupts;
mod memory; mod memory;
mod normalize_peris;
mod rcc; mod rcc;
mod registers; mod registers;

View File

@ -0,0 +1,15 @@
#[rustfmt::skip]
static NORMALIZE: &[(&str, &str)] = &[
("ADC", "ADC1"),
("HRTIM", "HRTIM1"),
("HDMI_CEC", "CEC"),
("SUBGHZ", "SUBGHZSPI"),
("USB_DRD_FS", "USB"),
];
pub fn normalize_peri_name(name: &str) -> &str {
if let Some((_, res)) = NORMALIZE.iter().find(|(n, _)| *n == name) {
return res;
}
return name;
}