Skip to content

Commit

Permalink
feat(gen): dma signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Apr 27, 2024
1 parent 319b886 commit 7f87a24
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 33 deletions.
90 changes: 90 additions & 0 deletions ch32-data-gen/src/dma.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::{
collections::{HashMap, HashSet},
path::Path,
};

pub fn handle_chip_dma_include<P: AsRef<Path>>(
path: P,
chip: &mut ch32_data_serde::Chip,
) -> anyhow::Result<()> {
let meta_yaml_path = path.as_ref();

for core in &mut chip.cores {
// DMA, Signal, Channel
// e.g. DMA1, ADC1, 1
let mut all_dma_signals: HashSet<(String, String, u8)> = HashSet::new();

if let Some(dma_channels_inc) = core.include_dma_channels.take() {
for (dma, inc_path) in dma_channels_inc {
let dma_yaml_path = meta_yaml_path.parent().unwrap().join(&inc_path);
let content = std::fs::read_to_string(&dma_yaml_path)?;
let dma_map: HashMap<String, u8> = serde_yaml::from_str(&content)?;

for (signal, &channel) in &dma_map {
let dma_signal = (dma.clone(), signal.clone(), channel);
if all_dma_signals.contains(&dma_signal) {
anyhow::bail!("DMA signal {} already exists in core {}", dma, core.name);
}
all_dma_signals.insert(dma_signal);
}

let mut dma_map: Vec<(String, u8)> = dma_map.into_iter().collect();
dma_map.sort_by_key(|(_, number)| *number);

let max_ch = dma_map.iter().map(|(_, channel)| *channel).max().unwrap();

/* Insert Chip core level dma_channels
cores[0].dma_channels
{
"name": "DMA1_CH1",
"dma": "DMA1",
"channel": 0
},
*/
for i in 0..max_ch {
let name = format!("{}_CH{}", dma, i + 1);
let channel = i; // 0 based
core.dma_channels
.push(ch32_data_serde::chip::core::DmaChannels {
name,
dma: dma.clone(),
channel,
});
}
}

// channel is 1-based number
for (dma, signal, channel) in &all_dma_signals {
let periph = signal.split('_').next().unwrap();

let signal_name = if signal.contains('_') {
signal.split('_').skip(1).next().unwrap()
} else {
signal
};
// "signal": "M",
// "channel": "DMA1_CH2"
let channel_name = format!("{}_CH{}", dma, channel);

for p in &mut core.peripherals {
if p.name == periph {
//println!(
// "found DMA singal {} {} {}",
// periph, signal_name, channel_name
//);
p.dma_channels
.push(ch32_data_serde::chip::core::peripheral::DmaChannel {
signal: signal_name.to_string(),
channel: Some(channel_name.to_string()),
dma: None, // TODO: rm these unused fields, as they only exist in STM32
dmamux: None,
request: None,
})
}
}
}
}
}

Ok(())
}
37 changes: 4 additions & 33 deletions ch32-data-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{collections::HashMap, path::Path};

mod dma;
mod registers;

#[macro_export]
Expand Down Expand Up @@ -111,41 +112,11 @@ fn main() -> anyhow::Result<()> {
core.peripherals.extend(peripherals);
}
}
}

if let Some(dma_channels_inc) = core.include_dma_channels.take() {
for (dma, inc_path) in dma_channels_inc {
let dma_yaml_path = meta_yaml_path.parent().unwrap().join(&inc_path);
let content = std::fs::read_to_string(&dma_yaml_path)?;
let dma_map: HashMap<String, u8> = serde_yaml::from_str(&content)?;
let mut dma_map: Vec<(String, u8)> = dma_map.into_iter().collect();
dma_map.sort_by_key(|(_, number)| *number);

let max_ch = dma_map.iter().map(|(_, channel)| *channel).max().unwrap();

/* Format
cores[0].dma_channels
{
"name": "DMA1_CH1",
"dma": "DMA1",
"channel": 0
},
*/
for i in 0..max_ch {
let name = format!("{}_CH{}", dma, i + 1);
let channel = i; // 0 based
core.dma_channels
.push(ch32_data_serde::chip::core::DmaChannels {
name,
dma: dma.clone(),
channel,
});
}
}
// DMA includes and dma_channels
dma::handle_chip_dma_include(&meta_yaml_path, &mut chip)?;

//println!("dma: {:#?}", dma);
//core.dma_channels.extend(dma);
}
}
println!(
"chip: {}, peripherals: {}",
chip.name,
Expand Down

0 comments on commit 7f87a24

Please sign in to comment.