stm32-metapac-gen: separate data structs

This commit is contained in:
Dario Nieuwenhuis 2021-11-22 02:37:46 +01:00
parent c8e69a14eb
commit 0db4da10f8
2 changed files with 107 additions and 103 deletions

View file

@ -0,0 +1,105 @@
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Chip {
pub name: String,
pub family: String,
pub line: String,
pub cores: Vec<Core>,
pub flash: Memory,
pub ram: Memory,
pub packages: Vec<Package>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Memory {
pub bytes: u32,
pub regions: HashMap<String, MemoryRegion>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct MemoryRegion {
pub base: u32,
pub bytes: Option<u32>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Core {
pub name: String,
pub peripherals: BTreeMap<String, Peripheral>,
pub interrupts: BTreeMap<String, u32>,
pub dma_channels: BTreeMap<String, DmaChannel>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Package {
pub name: String,
pub package: String,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Peripheral {
pub address: u64,
#[serde(default)]
pub kind: Option<String>,
#[serde(default)]
pub block: Option<String>,
#[serde(default)]
pub clock: Option<String>,
#[serde(default)]
pub pins: Vec<Pin>,
#[serde(default)]
pub dma_channels: BTreeMap<String, Vec<PeripheralDmaChannel>>,
#[serde(default)]
pub interrupts: BTreeMap<String, String>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Pin {
pub pin: String,
pub signal: String,
pub af: Option<String>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct DmaChannel {
pub dma: String,
pub channel: u32,
pub dmamux: Option<String>,
pub dmamux_channel: Option<u32>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Hash)]
pub struct PeripheralDmaChannel {
pub channel: Option<String>,
pub dmamux: Option<String>,
pub request: Option<u32>,
}
pub struct BlockInfo {
/// usart_v1/USART -> usart
pub module: String,
/// usart_v1/USART -> v1
pub version: String,
/// usart_v1/USART -> USART
pub block: String,
}
impl BlockInfo {
pub fn parse(s: &str) -> Self {
let mut s = s.split('/');
let module = s.next().unwrap();
let block = s.next().unwrap();
assert!(s.next().is_none());
let mut s = module.split('_');
let module = s.next().unwrap();
let version = s.next().unwrap();
assert!(s.next().is_none());
Self {
module: module.to_string(),
version: version.to_string(),
block: block.to_string(),
}
}
}

View file

@ -1,7 +1,6 @@
use chiptool::generate::CommonModule; use chiptool::generate::CommonModule;
use chiptool::ir::IR; use chiptool::ir::IR;
use regex::Regex; use regex::Regex;
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::env; use std::env;
use std::fmt::Write as _; use std::fmt::Write as _;
@ -14,108 +13,8 @@ use std::path::PathBuf;
use chiptool::util::ToSanitizedSnakeCase; use chiptool::util::ToSanitizedSnakeCase;
use chiptool::{generate, ir, transform}; use chiptool::{generate, ir, transform};
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)] mod data;
pub struct Chip { use data::*;
pub name: String,
pub family: String,
pub line: String,
pub cores: Vec<Core>,
pub flash: Memory,
pub ram: Memory,
pub packages: Vec<Package>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Memory {
pub bytes: u32,
pub regions: HashMap<String, MemoryRegion>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct MemoryRegion {
pub base: u32,
pub bytes: Option<u32>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Core {
pub name: String,
pub peripherals: BTreeMap<String, Peripheral>,
pub interrupts: BTreeMap<String, u32>,
pub dma_channels: BTreeMap<String, DmaChannel>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Package {
pub name: String,
pub package: String,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Peripheral {
pub address: u64,
#[serde(default)]
pub kind: Option<String>,
#[serde(default)]
pub block: Option<String>,
#[serde(default)]
pub clock: Option<String>,
#[serde(default)]
pub pins: Vec<Pin>,
#[serde(default)]
pub dma_channels: BTreeMap<String, Vec<PeripheralDmaChannel>>,
#[serde(default)]
pub interrupts: BTreeMap<String, String>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct Pin {
pub pin: String,
pub signal: String,
pub af: Option<String>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
pub struct DmaChannel {
pub dma: String,
pub channel: u32,
pub dmamux: Option<String>,
pub dmamux_channel: Option<u32>,
}
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Hash)]
pub struct PeripheralDmaChannel {
pub channel: Option<String>,
pub dmamux: Option<String>,
pub request: Option<u32>,
}
struct BlockInfo {
/// usart_v1/USART -> usart
module: String,
/// usart_v1/USART -> v1
version: String,
/// usart_v1/USART -> USART
block: String,
}
impl BlockInfo {
fn parse(s: &str) -> Self {
let mut s = s.split("/");
let module = s.next().unwrap();
let block = s.next().unwrap();
assert!(s.next().is_none());
let mut s = module.split("_");
let module = s.next().unwrap();
let version = s.next().unwrap();
assert!(s.next().is_none());
Self {
module: module.to_string(),
version: version.to_string(),
block: block.to_string(),
}
}
}
fn find_reg<'c>(rcc: &'c ir::IR, reg_regex: &str, field_name: &str) -> Option<(&'c str, &'c str)> { fn find_reg<'c>(rcc: &'c ir::IR, reg_regex: &str, field_name: &str) -> Option<(&'c str, &'c str)> {
let reg_regex = Regex::new(reg_regex).unwrap(); let reg_regex = Regex::new(reg_regex).unwrap();