metapac: allow runtime inspection of ir types
This commit is contained in:
parent
d63a20e69b
commit
23f9d9b236
@ -1,10 +1,12 @@
|
||||
pub mod ir {
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct IR {
|
||||
pub blocks: &'static [Block],
|
||||
pub fieldsets: &'static [FieldSet],
|
||||
pub enums: &'static [Enum],
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct Block {
|
||||
pub name: &'static str,
|
||||
pub extends: Option<&'static str>,
|
||||
@ -13,6 +15,7 @@ pub mod ir {
|
||||
pub items: &'static [BlockItem],
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct BlockItem {
|
||||
pub name: &'static str,
|
||||
pub description: Option<&'static str>,
|
||||
@ -23,27 +26,32 @@ pub mod ir {
|
||||
pub inner: BlockItemInner,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub enum BlockItemInner {
|
||||
Block(BlockItemBlock),
|
||||
Register(Register),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct Register {
|
||||
pub access: Access,
|
||||
pub bit_size: u32,
|
||||
pub fieldset: Option<&'static str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct BlockItemBlock {
|
||||
pub block: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub enum Access {
|
||||
ReadWrite,
|
||||
Read,
|
||||
Write,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct FieldSet {
|
||||
pub name: &'static str,
|
||||
pub extends: Option<&'static str>,
|
||||
@ -53,6 +61,7 @@ pub mod ir {
|
||||
pub fields: &'static [Field],
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct Field {
|
||||
pub name: &'static str,
|
||||
pub description: Option<&'static str>,
|
||||
@ -63,20 +72,24 @@ pub mod ir {
|
||||
pub enumm: Option<&'static str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub enum Array {
|
||||
Regular(RegularArray),
|
||||
Cursed(CursedArray),
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct RegularArray {
|
||||
pub len: u32,
|
||||
pub stride: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct CursedArray {
|
||||
pub offsets: &'static [u32],
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct Enum {
|
||||
pub name: &'static str,
|
||||
pub description: Option<&'static str>,
|
||||
@ -84,6 +97,7 @@ pub mod ir {
|
||||
pub variants: &'static [EnumVariant],
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
pub struct EnumVariant {
|
||||
pub name: &'static str,
|
||||
pub description: Option<&'static str>,
|
||||
@ -153,6 +167,7 @@ pub struct PeripheralRegisters {
|
||||
pub kind: &'static str,
|
||||
pub version: &'static str,
|
||||
pub block: &'static str,
|
||||
pub ir: &'static ir::IR,
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||
|
@ -22,7 +22,15 @@ pub mod ir {
|
||||
.map(|item| BlockItem {
|
||||
name: item.name.clone(),
|
||||
description: item.description.clone(),
|
||||
array: None,
|
||||
array: item.array.as_ref().map(|array| match &array {
|
||||
chiptool::ir::Array::Regular(regular_array) => Array::Regular(RegularArray {
|
||||
len: regular_array.len,
|
||||
stride: regular_array.stride,
|
||||
}),
|
||||
chiptool::ir::Array::Cursed(cursed_array) => Array::Cursed(CursedArray {
|
||||
offsets: cursed_array.offsets.clone(),
|
||||
}),
|
||||
}),
|
||||
byte_offset: item.byte_offset,
|
||||
inner: match &item.inner {
|
||||
chiptool::ir::BlockItemInner::Block(block) => BlockItemInner::Block(BlockItemBlock {
|
||||
@ -346,4 +354,6 @@ pub struct PeripheralRegisters {
|
||||
pub kind: String,
|
||||
pub version: String,
|
||||
pub block: String,
|
||||
#[serde(default)]
|
||||
pub ir: String,
|
||||
}
|
||||
|
@ -186,9 +186,9 @@ impl Gen {
|
||||
write!(
|
||||
&mut data,
|
||||
"
|
||||
const PERIPHERALS: &'static [Peripheral] = {};
|
||||
const INTERRUPTS: &'static [Interrupt] = {};
|
||||
const DMA_CHANNELS: &'static [DmaChannel] = {};
|
||||
pub(crate) static PERIPHERALS: &'static [Peripheral] = {};
|
||||
pub(crate) static INTERRUPTS: &'static [Interrupt] = {};
|
||||
pub(crate) static DMA_CHANNELS: &'static [DmaChannel] = {};
|
||||
",
|
||||
stringify(&core.peripherals),
|
||||
stringify(&core.interrupts),
|
||||
@ -199,6 +199,18 @@ impl Gen {
|
||||
let out_dir = self.opts.out_dir.clone();
|
||||
let n = self.metadata_dedup.len();
|
||||
let deduped_file = self.metadata_dedup.entry(data.clone()).or_insert_with(|| {
|
||||
let ir_regex = Regex::new("\":ir_for:([a-z0-9]+):\"").unwrap();
|
||||
let mut data = ir_regex.replace_all(&data, "&$1::REGISTERS").to_string();
|
||||
|
||||
for (module, version) in &peripheral_versions {
|
||||
writeln!(
|
||||
&mut data,
|
||||
"#[path=\"../registers/{}_{}.rs\"] pub mod {};",
|
||||
module, version, module
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let file = format!("metadata_{:04}.rs", n);
|
||||
let path = out_dir.join("src/chips").join(&file);
|
||||
fs::write(path, data).unwrap();
|
||||
@ -206,9 +218,9 @@ impl Gen {
|
||||
file
|
||||
});
|
||||
|
||||
let mut data = format!(
|
||||
let data = format!(
|
||||
"include!(\"../{}\");
|
||||
pub const METADATA: Metadata = Metadata {{
|
||||
pub static METADATA: Metadata = Metadata {{
|
||||
name: {:?},
|
||||
family: {:?},
|
||||
line: {:?},
|
||||
@ -226,15 +238,6 @@ impl Gen {
|
||||
&core.nvic_priority_bits,
|
||||
);
|
||||
|
||||
for (module, version) in &peripheral_versions {
|
||||
writeln!(
|
||||
&mut data,
|
||||
"#[path=\"../../registers/{}_{}.rs\"] pub mod {};",
|
||||
module, version, module
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let mut file = File::create(chip_dir.join("metadata.rs")).unwrap();
|
||||
file.write_all(data.as_bytes()).unwrap();
|
||||
|
||||
@ -278,6 +281,10 @@ impl Gen {
|
||||
for irq in &mut p.interrupts {
|
||||
irq.interrupt = irq.interrupt.to_ascii_uppercase();
|
||||
}
|
||||
|
||||
if let Some(registers) = &mut p.registers {
|
||||
registers.ir = format!(":ir_for:{}:", registers.kind);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -336,7 +343,7 @@ impl Gen {
|
||||
&mut data,
|
||||
"
|
||||
use crate::metadata::ir::*;
|
||||
static REGISTERS: IR = {};
|
||||
pub(crate) static REGISTERS: IR = {};
|
||||
",
|
||||
stringify(&ir)
|
||||
.replace("Register(", "BlockItemInner::Register(")
|
||||
|
Loading…
x
Reference in New Issue
Block a user