2021-07-29 13:14:18 +02:00
|
|
|
use chiptool::generate::CommonModule;
|
2021-11-24 00:42:14 +01:00
|
|
|
use proc_macro2::TokenStream;
|
2021-06-07 05:10:11 +02:00
|
|
|
use regex::Regex;
|
2021-07-30 17:06:58 +02:00
|
|
|
use std::collections::{BTreeMap, HashMap, HashSet};
|
2021-06-07 05:10:11 +02:00
|
|
|
use std::fmt::Write as _;
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2021-11-24 00:42:14 +01:00
|
|
|
use std::str::FromStr;
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-07-22 20:18:48 +02:00
|
|
|
use chiptool::util::ToSanitizedSnakeCase;
|
2021-07-29 13:14:18 +02:00
|
|
|
use chiptool::{generate, ir, transform};
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-22 02:37:46 +01:00
|
|
|
mod data;
|
|
|
|
use data::*;
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2022-02-09 00:28:05 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
|
|
|
struct Metadata<'a> {
|
|
|
|
name: &'a str,
|
|
|
|
family: &'a str,
|
|
|
|
line: &'a str,
|
|
|
|
memory: &'a [MemoryRegion],
|
|
|
|
peripherals: &'a [Peripheral],
|
|
|
|
interrupts: &'a [Interrupt],
|
|
|
|
dma_channels: &'a [DmaChannel],
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:06:58 +02:00
|
|
|
fn make_peripheral_counts(out: &mut String, data: &BTreeMap<String, u8>) {
|
2021-07-13 05:47:10 +02:00
|
|
|
write!(
|
|
|
|
out,
|
|
|
|
"#[macro_export]
|
2021-06-25 20:00:11 +02:00
|
|
|
macro_rules! peripheral_count {{
|
2021-07-13 05:47:10 +02:00
|
|
|
"
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-25 20:00:11 +02:00
|
|
|
for (name, count) in data {
|
2021-07-13 05:47:10 +02:00
|
|
|
write!(out, "({}) => ({});\n", name, count,).unwrap();
|
2021-06-25 20:00:11 +02:00
|
|
|
}
|
2021-07-13 05:47:10 +02:00
|
|
|
write!(out, " }}\n").unwrap();
|
2021-06-25 20:00:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:06:58 +02:00
|
|
|
fn make_dma_channel_counts(out: &mut String, data: &BTreeMap<String, u8>) {
|
2021-11-02 17:03:56 +01:00
|
|
|
if data.len() == 0 {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-15 18:25:51 +02:00
|
|
|
write!(
|
|
|
|
out,
|
|
|
|
"#[macro_export]
|
2021-07-12 21:48:26 +02:00
|
|
|
macro_rules! dma_channels_count {{
|
2021-07-15 18:25:51 +02:00
|
|
|
"
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-12 21:48:26 +02:00
|
|
|
for (name, count) in data {
|
2021-07-15 18:25:51 +02:00
|
|
|
write!(out, "({}) => ({});\n", name, count,).unwrap();
|
2021-07-12 21:48:26 +02:00
|
|
|
}
|
2021-07-15 18:25:51 +02:00
|
|
|
write!(out, " }}\n").unwrap();
|
2021-07-12 21:48:26 +02:00
|
|
|
}
|
|
|
|
|
2021-06-07 05:10:11 +02:00
|
|
|
fn make_table(out: &mut String, name: &str, data: &Vec<Vec<String>>) {
|
|
|
|
write!(
|
|
|
|
out,
|
|
|
|
"#[macro_export]
|
|
|
|
macro_rules! {} {{
|
|
|
|
($($pat:tt => $code:tt;)*) => {{
|
|
|
|
macro_rules! __{}_inner {{
|
|
|
|
$(($pat) => $code;)*
|
|
|
|
($_:tt) => {{}}
|
|
|
|
}}
|
|
|
|
",
|
|
|
|
name, name
|
|
|
|
)
|
2021-07-13 05:47:10 +02:00
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
|
|
|
for row in data {
|
|
|
|
write!(out, " __{}_inner!(({}));\n", name, row.join(",")).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(
|
|
|
|
out,
|
|
|
|
" }};
|
|
|
|
}}"
|
|
|
|
)
|
2021-07-13 05:47:10 +02:00
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Options {
|
|
|
|
pub chips: Vec<String>,
|
|
|
|
pub out_dir: PathBuf,
|
|
|
|
pub data_dir: PathBuf,
|
|
|
|
}
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
pub fn gen_chip(
|
|
|
|
options: &Options,
|
|
|
|
chip_core_name: &str,
|
|
|
|
chip: &Chip,
|
|
|
|
core: &Core,
|
|
|
|
core_index: usize,
|
|
|
|
all_peripheral_versions: &mut HashSet<(String, String)>,
|
|
|
|
) {
|
|
|
|
let mut ir = ir::IR::new();
|
|
|
|
|
|
|
|
let mut dev = ir::Device {
|
|
|
|
interrupts: Vec::new(),
|
|
|
|
peripherals: Vec::new(),
|
2021-06-07 05:10:11 +02:00
|
|
|
};
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
// Load DBGMCU register for chip
|
2022-02-07 02:10:12 +01:00
|
|
|
let mut dbgmcu: Option<ir::IR> = core.peripherals.iter().find_map(|p| {
|
|
|
|
if p.name == "DBGMCU" {
|
2022-02-07 22:48:53 +01:00
|
|
|
p.registers.as_ref().map(|bi| {
|
2021-11-23 23:49:06 +01:00
|
|
|
let dbgmcu_reg_path = options
|
|
|
|
.data_dir
|
|
|
|
.join("registers")
|
2022-02-07 22:48:53 +01:00
|
|
|
.join(&format!("{}_{}.yaml", bi.kind, bi.version));
|
2021-11-23 23:49:06 +01:00
|
|
|
serde_yaml::from_reader(File::open(dbgmcu_reg_path).unwrap()).unwrap()
|
|
|
|
})
|
2021-09-15 13:35:00 +02:00
|
|
|
} else {
|
|
|
|
None
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut peripheral_versions: BTreeMap<String, String> = BTreeMap::new();
|
|
|
|
let mut pin_table: Vec<Vec<String>> = Vec::new();
|
|
|
|
let mut interrupt_table: Vec<Vec<String>> = Vec::new();
|
|
|
|
let mut peripherals_table: Vec<Vec<String>> = Vec::new();
|
|
|
|
let mut dma_channels_table: Vec<Vec<String>> = Vec::new();
|
|
|
|
let mut peripheral_counts: BTreeMap<String, u8> = BTreeMap::new();
|
|
|
|
let mut dma_channel_counts: BTreeMap<String, u8> = BTreeMap::new();
|
|
|
|
let mut dbgmcu_table: Vec<Vec<String>> = Vec::new();
|
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
let gpio_base = core
|
|
|
|
.peripherals
|
|
|
|
.iter()
|
|
|
|
.find(|p| p.name == "GPIOA")
|
|
|
|
.unwrap()
|
|
|
|
.address as u32;
|
2021-11-23 23:49:06 +01:00
|
|
|
let gpio_stride = 0x400;
|
|
|
|
|
|
|
|
let number_suffix_re = Regex::new("^(.*?)[0-9]*$").unwrap();
|
|
|
|
|
|
|
|
if let Some(ref mut reg) = dbgmcu {
|
|
|
|
if let Some(ref cr) = reg.fieldsets.get("CR") {
|
|
|
|
for field in cr.fields.iter().filter(|e| e.name.contains("DBG")) {
|
|
|
|
let mut fn_name = String::new();
|
|
|
|
fn_name.push_str("set_");
|
|
|
|
fn_name.push_str(&field.name.to_sanitized_snake_case());
|
|
|
|
dbgmcu_table.push(vec!["cr".into(), fn_name]);
|
2021-06-16 15:12:07 +02:00
|
|
|
}
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for p in &core.peripherals {
|
|
|
|
let captures = number_suffix_re.captures(&p.name).unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
let root_peri_name = captures.get(1).unwrap().as_str().to_string();
|
|
|
|
peripheral_counts.insert(
|
|
|
|
root_peri_name.clone(),
|
|
|
|
peripheral_counts.get(&root_peri_name).map_or(1, |v| v + 1),
|
|
|
|
);
|
|
|
|
let mut ir_peri = ir::Peripheral {
|
2022-02-07 02:10:12 +01:00
|
|
|
name: p.name.clone(),
|
2021-11-23 23:49:06 +01:00
|
|
|
array: None,
|
|
|
|
base_address: p.address,
|
|
|
|
block: None,
|
|
|
|
description: None,
|
|
|
|
interrupts: HashMap::new(),
|
2021-06-07 05:10:11 +02:00
|
|
|
};
|
|
|
|
|
2022-02-07 22:48:53 +01:00
|
|
|
if let Some(bi) = &p.registers {
|
2021-06-25 20:00:11 +02:00
|
|
|
peripheral_counts.insert(
|
2022-02-07 22:48:53 +01:00
|
|
|
bi.kind.clone(),
|
|
|
|
peripheral_counts.get(&bi.kind).map_or(1, |v| v + 1),
|
2021-06-25 20:00:11 +02:00
|
|
|
);
|
2021-07-13 15:50:42 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for irq in &p.interrupts {
|
2021-11-23 23:49:06 +01:00
|
|
|
let mut row = Vec::new();
|
2022-02-07 02:10:12 +01:00
|
|
|
row.push(p.name.clone());
|
2022-02-07 22:48:53 +01:00
|
|
|
row.push(bi.kind.clone());
|
2021-11-23 23:49:06 +01:00
|
|
|
row.push(bi.block.clone());
|
2022-02-07 02:10:12 +01:00
|
|
|
row.push(irq.signal.clone());
|
|
|
|
row.push(irq.interrupt.to_ascii_uppercase());
|
2021-11-23 23:49:06 +01:00
|
|
|
interrupt_table.push(row)
|
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
let mut peripheral_row = Vec::new();
|
2022-02-07 22:48:53 +01:00
|
|
|
peripheral_row.push(bi.kind.clone());
|
2022-02-07 02:10:12 +01:00
|
|
|
peripheral_row.push(p.name.clone());
|
2021-11-23 23:49:06 +01:00
|
|
|
peripherals_table.push(peripheral_row);
|
|
|
|
|
|
|
|
if let Some(old_version) =
|
2022-02-07 22:48:53 +01:00
|
|
|
peripheral_versions.insert(bi.kind.clone(), bi.version.clone())
|
2021-11-23 23:49:06 +01:00
|
|
|
{
|
|
|
|
if old_version != bi.version {
|
|
|
|
panic!(
|
|
|
|
"Peripheral {} has multiple versions: {} and {}",
|
2022-02-07 22:48:53 +01:00
|
|
|
bi.kind, old_version, bi.version
|
2021-11-23 23:49:06 +01:00
|
|
|
);
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
2022-02-07 22:48:53 +01:00
|
|
|
ir_peri.block = Some(format!("{}::{}", bi.kind, bi.block));
|
2021-11-23 23:49:06 +01:00
|
|
|
|
2022-02-07 22:48:53 +01:00
|
|
|
match bi.kind.as_str() {
|
2021-11-23 23:49:06 +01:00
|
|
|
"gpio" => {
|
2022-02-07 02:10:12 +01:00
|
|
|
let port_letter = p.name.chars().skip(4).next().unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
assert_eq!(0, (p.address as u32 - gpio_base) % gpio_stride);
|
|
|
|
let port_num = (p.address as u32 - gpio_base) / gpio_stride;
|
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for pin_num in 0u32..16 {
|
2021-11-23 23:49:06 +01:00
|
|
|
let pin_name = format!("P{}{}", port_letter, pin_num);
|
|
|
|
pin_table.push(vec![
|
|
|
|
pin_name.clone(),
|
2022-02-07 02:10:12 +01:00
|
|
|
p.name.clone(),
|
2021-11-23 23:49:06 +01:00
|
|
|
port_num.to_string(),
|
|
|
|
pin_num.to_string(),
|
|
|
|
format!("EXTI{}", pin_num),
|
|
|
|
]);
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 23:49:06 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
dev.peripherals.push(ir_peri);
|
|
|
|
}
|
2021-07-12 21:48:26 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for ch in &core.dma_channels {
|
2021-11-23 23:49:06 +01:00
|
|
|
let mut row = Vec::new();
|
2022-02-07 02:10:12 +01:00
|
|
|
let dma_peri = core.peripherals.iter().find(|p| p.name == ch.dma).unwrap();
|
2022-02-07 22:48:53 +01:00
|
|
|
let bi = dma_peri.registers.as_ref().unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
row.push(ch.name.clone());
|
|
|
|
row.push(ch.dma.clone());
|
2022-02-07 22:48:53 +01:00
|
|
|
row.push(bi.kind.clone());
|
2022-02-07 02:10:12 +01:00
|
|
|
row.push(ch.channel.to_string());
|
|
|
|
if let Some(dmamux) = &ch.dmamux {
|
|
|
|
let dmamux_channel = ch.dmamux_channel.unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
row.push(format!(
|
|
|
|
"{{dmamux: {}, dmamux_channel: {}}}",
|
|
|
|
dmamux, dmamux_channel
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
row.push("{}".to_string());
|
2021-07-12 16:32:57 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
dma_channels_table.push(row);
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
let dma_peri_name = ch.dma.clone();
|
2021-11-23 23:49:06 +01:00
|
|
|
dma_channel_counts.insert(
|
|
|
|
dma_peri_name.clone(),
|
|
|
|
dma_channel_counts.get(&dma_peri_name).map_or(1, |v| v + 1),
|
|
|
|
);
|
|
|
|
}
|
2021-06-25 20:00:11 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for irq in &core.interrupts {
|
2021-11-23 23:49:06 +01:00
|
|
|
dev.interrupts.push(ir::Interrupt {
|
2022-02-07 02:10:12 +01:00
|
|
|
name: irq.name.clone(),
|
2021-11-23 23:49:06 +01:00
|
|
|
description: None,
|
2022-02-07 02:10:12 +01:00
|
|
|
value: irq.number,
|
2021-11-23 23:49:06 +01:00
|
|
|
});
|
2021-06-25 20:00:11 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
let name = irq.name.to_ascii_uppercase();
|
2021-11-23 23:49:06 +01:00
|
|
|
|
|
|
|
interrupt_table.push(vec![name.clone()]);
|
|
|
|
|
|
|
|
if name.contains("EXTI") {
|
|
|
|
interrupt_table.push(vec!["EXTI".to_string(), name.clone()]);
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
ir.devices.insert("".to_string(), dev);
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
let mut extra = format!(
|
|
|
|
"pub fn GPIO(n: usize) -> gpio::Gpio {{
|
2021-06-07 05:10:11 +02:00
|
|
|
gpio::Gpio(({} + {}*n) as _)
|
|
|
|
}}",
|
2021-11-23 23:49:06 +01:00
|
|
|
gpio_base, gpio_stride,
|
|
|
|
);
|
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
for (module, version) in &peripheral_versions {
|
2021-11-23 23:49:06 +01:00
|
|
|
all_peripheral_versions.insert((module.clone(), version.clone()));
|
2021-08-17 14:25:18 +02:00
|
|
|
write!(
|
|
|
|
&mut extra,
|
2021-11-23 23:49:06 +01:00
|
|
|
"#[path=\"../../peripherals/{}_{}.rs\"] pub mod {};\n",
|
|
|
|
module, version, module
|
2021-08-17 14:25:18 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
|
|
|
write!(
|
|
|
|
&mut extra,
|
|
|
|
"pub const CORE_INDEX: usize = {};\n",
|
|
|
|
core_index
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
// Cleanups!
|
|
|
|
transform::sort::Sort {}.run(&mut ir).unwrap();
|
|
|
|
transform::Sanitize {}.run(&mut ir).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
// ==============================
|
|
|
|
// Setup chip dir
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
let chip_dir = options
|
|
|
|
.out_dir
|
|
|
|
.join("src/chips")
|
|
|
|
.join(chip_core_name.to_ascii_lowercase());
|
|
|
|
fs::create_dir_all(&chip_dir).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
// ==============================
|
|
|
|
// generate pac.rs
|
|
|
|
|
|
|
|
let data = generate::render(&ir, &gen_opts()).unwrap().to_string();
|
|
|
|
let data = data.replace("] ", "]\n");
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
// Remove inner attributes like #![no_std]
|
2021-11-24 01:04:23 +01:00
|
|
|
let data = Regex::new("# *! *\\[.*\\]").unwrap().replace_all(&data, "");
|
|
|
|
|
|
|
|
let mut file = File::create(chip_dir.join("pac.rs")).unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
file.write_all(data.as_bytes()).unwrap();
|
|
|
|
file.write_all(extra.as_bytes()).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
let mut device_x = String::new();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2022-02-07 02:10:12 +01:00
|
|
|
for irq in &core.interrupts {
|
2022-02-09 00:28:05 +01:00
|
|
|
write!(&mut device_x, "PROVIDE({} = DefaultHandler);\n", irq.name).unwrap();
|
2021-11-23 23:49:06 +01:00
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
// ==============================
|
|
|
|
// generate mod.rs
|
|
|
|
|
|
|
|
let mut data = String::new();
|
|
|
|
|
2022-02-09 00:28:05 +01:00
|
|
|
write!(&mut data, "#[cfg(feature=\"metadata\")] pub mod metadata;").unwrap();
|
2021-11-24 01:04:23 +01:00
|
|
|
write!(&mut data, "#[cfg(feature=\"pac\")] mod pac;").unwrap();
|
|
|
|
write!(&mut data, "#[cfg(feature=\"pac\")] pub use pac::*; ").unwrap();
|
|
|
|
|
|
|
|
let peripheral_version_table = peripheral_versions
|
|
|
|
.iter()
|
|
|
|
.map(|(kind, version)| vec![kind.clone(), version.clone()])
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
make_table(&mut data, "pins", &pin_table);
|
|
|
|
make_table(&mut data, "interrupts", &interrupt_table);
|
|
|
|
make_table(&mut data, "peripherals", &peripherals_table);
|
|
|
|
make_table(&mut data, "peripheral_versions", &peripheral_version_table);
|
|
|
|
make_table(&mut data, "dma_channels", &dma_channels_table);
|
|
|
|
make_table(&mut data, "dbgmcu", &dbgmcu_table);
|
|
|
|
make_peripheral_counts(&mut data, &peripheral_counts);
|
|
|
|
make_dma_channel_counts(&mut data, &dma_channel_counts);
|
|
|
|
|
|
|
|
let mut file = File::create(chip_dir.join("mod.rs")).unwrap();
|
|
|
|
file.write_all(data.as_bytes()).unwrap();
|
|
|
|
|
2022-02-09 00:28:05 +01:00
|
|
|
// ==============================
|
|
|
|
// generate metadata.rs
|
|
|
|
|
|
|
|
let metadata = Metadata {
|
|
|
|
name: &chip.name,
|
|
|
|
family: &chip.family,
|
|
|
|
line: &chip.line,
|
|
|
|
memory: &chip.memory,
|
|
|
|
peripherals: &core.peripherals,
|
|
|
|
interrupts: &core.interrupts,
|
|
|
|
dma_channels: &core.dma_channels,
|
|
|
|
};
|
|
|
|
let metadata = format!("{:#?}", metadata);
|
|
|
|
let metadata = metadata.replace("[\n", "&[\n");
|
|
|
|
let metadata = metadata.replace("[],\n", "&[],\n");
|
|
|
|
|
|
|
|
let mut data = String::new();
|
|
|
|
|
|
|
|
write!(
|
|
|
|
&mut data,
|
|
|
|
"
|
|
|
|
include!(\"../../metadata.rs\");
|
|
|
|
use MemoryRegionKind::*;
|
|
|
|
pub const METADATA: Metadata = {};
|
|
|
|
",
|
|
|
|
metadata
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut file = File::create(chip_dir.join("metadata.rs")).unwrap();
|
|
|
|
file.write_all(data.as_bytes()).unwrap();
|
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
// ==============================
|
|
|
|
// generate device.x
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
File::create(chip_dir.join("device.x"))
|
|
|
|
.unwrap()
|
|
|
|
.write_all(device_x.as_bytes())
|
|
|
|
.unwrap();
|
|
|
|
|
2021-11-24 01:04:23 +01:00
|
|
|
// ==============================
|
2021-11-23 23:49:06 +01:00
|
|
|
// generate default memory.x
|
|
|
|
gen_memory_x(&chip_dir, &chip);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn load_chip(options: &Options, name: &str) -> Chip {
|
|
|
|
let chip_path = options
|
|
|
|
.data_dir
|
|
|
|
.join("chips")
|
2022-02-07 02:10:12 +01:00
|
|
|
.join(&format!("{}.json", name));
|
2021-11-23 23:49:06 +01:00
|
|
|
let chip = fs::read(chip_path).expect(&format!("Could not load chip {}", name));
|
|
|
|
serde_yaml::from_slice(&chip).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-11-24 00:42:14 +01:00
|
|
|
fn gen_opts() -> generate::Options {
|
|
|
|
generate::Options {
|
|
|
|
common_module: CommonModule::External(TokenStream::from_str("crate::common").unwrap()),
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 23:49:06 +01:00
|
|
|
|
2021-11-24 00:42:14 +01:00
|
|
|
pub fn gen(options: Options) {
|
2021-11-23 23:49:06 +01:00
|
|
|
fs::create_dir_all(options.out_dir.join("src/peripherals")).unwrap();
|
|
|
|
fs::create_dir_all(options.out_dir.join("src/chips")).unwrap();
|
|
|
|
|
|
|
|
let mut all_peripheral_versions: HashSet<(String, String)> = HashSet::new();
|
|
|
|
let mut chip_core_names: Vec<String> = Vec::new();
|
2021-07-30 20:06:10 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
for chip_name in &options.chips {
|
2021-11-29 02:10:06 +01:00
|
|
|
println!("Generating {}...", chip_name);
|
|
|
|
|
2022-02-09 00:28:05 +01:00
|
|
|
let mut chip = load_chip(&options, chip_name);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
for core in &mut chip.cores {
|
|
|
|
for irq in &mut core.interrupts {
|
|
|
|
irq.name = irq.name.to_ascii_uppercase();
|
|
|
|
}
|
|
|
|
for p in &mut core.peripherals {
|
|
|
|
for irq in &mut p.interrupts {
|
|
|
|
irq.interrupt = irq.interrupt.to_ascii_uppercase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate
|
2021-11-23 23:49:06 +01:00
|
|
|
for (core_index, core) in chip.cores.iter().enumerate() {
|
|
|
|
let chip_core_name = match chip.cores.len() {
|
|
|
|
1 => chip_name.clone(),
|
|
|
|
_ => format!("{}-{}", chip_name, core.name),
|
|
|
|
};
|
|
|
|
|
|
|
|
chip_core_names.push(chip_core_name.clone());
|
|
|
|
gen_chip(
|
|
|
|
&options,
|
|
|
|
&chip_core_name,
|
|
|
|
&chip,
|
|
|
|
core,
|
|
|
|
core_index,
|
|
|
|
&mut all_peripheral_versions,
|
|
|
|
)
|
|
|
|
}
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (module, version) in all_peripheral_versions {
|
|
|
|
println!("loading {} {}", module, version);
|
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
let regs_path = Path::new(&options.data_dir)
|
2021-06-07 05:10:11 +02:00
|
|
|
.join("registers")
|
|
|
|
.join(&format!("{}_{}.yaml", module, version));
|
|
|
|
|
|
|
|
let mut ir: ir::IR = serde_yaml::from_reader(File::open(regs_path).unwrap()).unwrap();
|
|
|
|
|
|
|
|
transform::expand_extends::ExpandExtends {}
|
|
|
|
.run(&mut ir)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-07-29 13:14:18 +02:00
|
|
|
transform::map_names(&mut ir, |k, s| match k {
|
|
|
|
transform::NameKind::Block => *s = format!("{}", s),
|
|
|
|
transform::NameKind::Fieldset => *s = format!("regs::{}", s),
|
|
|
|
transform::NameKind::Enum => *s = format!("vals::{}", s),
|
|
|
|
_ => {}
|
|
|
|
});
|
2021-06-07 05:10:11 +02:00
|
|
|
|
|
|
|
transform::sort::Sort {}.run(&mut ir).unwrap();
|
|
|
|
transform::Sanitize {}.run(&mut ir).unwrap();
|
|
|
|
|
2021-11-24 00:42:14 +01:00
|
|
|
let items = generate::render(&ir, &gen_opts()).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
let mut file = File::create(
|
2021-11-23 23:49:06 +01:00
|
|
|
options
|
|
|
|
.out_dir
|
2021-06-07 05:10:11 +02:00
|
|
|
.join("src/peripherals")
|
|
|
|
.join(format!("{}_{}.rs", module, version)),
|
|
|
|
)
|
2021-07-13 05:47:10 +02:00
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
let data = items.to_string().replace("] ", "]\n");
|
|
|
|
|
|
|
|
// Remove inner attributes like #![no_std]
|
|
|
|
let re = Regex::new("# *! *\\[.*\\]").unwrap();
|
|
|
|
let data = re.replace_all(&data, "");
|
|
|
|
file.write_all(data.as_bytes()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate src/lib_inner.rs
|
|
|
|
const PATHS_MARKER: &[u8] = b"// GEN PATHS HERE";
|
|
|
|
let librs = include_bytes!("assets/lib_inner.rs");
|
|
|
|
let i = bytes_find(librs, PATHS_MARKER).unwrap();
|
|
|
|
let mut paths = String::new();
|
2021-06-16 15:12:07 +02:00
|
|
|
|
2021-11-23 23:49:06 +01:00
|
|
|
for name in chip_core_names {
|
|
|
|
let x = name.to_ascii_lowercase();
|
|
|
|
write!(
|
|
|
|
&mut paths,
|
2021-11-24 01:04:23 +01:00
|
|
|
"#[cfg_attr(feature=\"{}\", path = \"chips/{}/mod.rs\")]",
|
2021-11-23 23:49:06 +01:00
|
|
|
x, x
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
let mut contents: Vec<u8> = Vec::new();
|
|
|
|
contents.extend(&librs[..i]);
|
|
|
|
contents.extend(paths.as_bytes());
|
|
|
|
contents.extend(&librs[i + PATHS_MARKER.len()..]);
|
2021-11-23 23:49:06 +01:00
|
|
|
fs::write(options.out_dir.join("src").join("lib_inner.rs"), &contents).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
|
|
|
// Generate src/lib.rs
|
|
|
|
const CUT_MARKER: &[u8] = b"// GEN CUT HERE";
|
2021-07-13 05:47:10 +02:00
|
|
|
let librs = include_bytes!("../../stm32-metapac/src/lib.rs");
|
2021-06-07 05:10:11 +02:00
|
|
|
let i = bytes_find(librs, CUT_MARKER).unwrap();
|
|
|
|
let mut contents: Vec<u8> = Vec::new();
|
|
|
|
contents.extend(&librs[..i]);
|
|
|
|
contents.extend(b"include!(\"lib_inner.rs\");\n");
|
2021-11-23 23:49:06 +01:00
|
|
|
fs::write(options.out_dir.join("src").join("lib.rs"), contents).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
|
|
|
// Generate src/common.rs
|
|
|
|
fs::write(
|
2021-11-23 23:49:06 +01:00
|
|
|
options.out_dir.join("src").join("common.rs"),
|
2021-06-07 05:10:11 +02:00
|
|
|
generate::COMMON_MODULE,
|
|
|
|
)
|
2021-07-13 05:47:10 +02:00
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
2022-02-09 00:28:05 +01:00
|
|
|
// Generate src/metadata.rs
|
|
|
|
fs::write(
|
|
|
|
options.out_dir.join("src").join("metadata.rs"),
|
|
|
|
include_bytes!("assets/metadata.rs"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-06-07 05:10:11 +02:00
|
|
|
// Generate Cargo.toml
|
|
|
|
const BUILDDEP_BEGIN: &[u8] = b"# BEGIN BUILD DEPENDENCIES";
|
|
|
|
const BUILDDEP_END: &[u8] = b"# END BUILD DEPENDENCIES";
|
|
|
|
|
2021-07-13 05:47:10 +02:00
|
|
|
let mut contents = include_bytes!("../../stm32-metapac/Cargo.toml").to_vec();
|
2021-06-07 05:10:11 +02:00
|
|
|
let begin = bytes_find(&contents, BUILDDEP_BEGIN).unwrap();
|
|
|
|
let end = bytes_find(&contents, BUILDDEP_END).unwrap() + BUILDDEP_END.len();
|
|
|
|
contents.drain(begin..end);
|
2021-11-23 23:49:06 +01:00
|
|
|
fs::write(options.out_dir.join("Cargo.toml"), contents).unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
|
|
|
|
// Generate build.rs
|
2021-11-23 23:49:06 +01:00
|
|
|
fs::write(
|
|
|
|
options.out_dir.join("build.rs"),
|
|
|
|
include_bytes!("assets/build.rs"),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-06-07 05:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
|
|
|
|
haystack
|
|
|
|
.windows(needle.len())
|
|
|
|
.position(|window| window == needle)
|
|
|
|
}
|
2021-07-30 20:06:10 +02:00
|
|
|
|
|
|
|
fn gen_memory_x(out_dir: &PathBuf, chip: &Chip) {
|
|
|
|
let mut memory_x = String::new();
|
|
|
|
|
2022-02-07 20:40:35 +01:00
|
|
|
let flash = chip.memory.iter().find(|r| r.name == "BANK_1").unwrap();
|
|
|
|
let ram = chip.memory.iter().find(|r| r.name == "SRAM").unwrap();
|
2021-07-30 20:06:10 +02:00
|
|
|
|
|
|
|
write!(memory_x, "MEMORY\n{{\n").unwrap();
|
2021-08-04 12:43:51 +02:00
|
|
|
write!(
|
|
|
|
memory_x,
|
|
|
|
" FLASH : ORIGIN = 0x{:x}, LENGTH = {}\n",
|
2022-02-07 20:40:35 +01:00
|
|
|
flash.address, flash.size,
|
2021-08-04 12:43:51 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
write!(
|
|
|
|
memory_x,
|
|
|
|
" RAM : ORIGIN = 0x{:x}, LENGTH = {}\n",
|
2022-02-07 20:40:35 +01:00
|
|
|
ram.address, ram.size,
|
2021-08-04 12:43:51 +02:00
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-30 20:06:10 +02:00
|
|
|
write!(memory_x, "}}").unwrap();
|
|
|
|
|
2021-08-02 19:21:30 +02:00
|
|
|
fs::create_dir_all(out_dir.join("memory_x")).unwrap();
|
|
|
|
let mut file = File::create(out_dir.join("memory_x").join("memory.x")).unwrap();
|
2021-08-04 12:29:20 +02:00
|
|
|
file.write_all(memory_x.as_bytes()).unwrap();
|
2021-07-30 20:06:10 +02:00
|
|
|
}
|