use linked_hash_map::LinkedHashMap;
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::rc::Rc;
pub trait HasSameType {
fn has_same_type(&self, other: &Self) -> bool;
}
impl HasSameType for PeripheralMod {
fn has_same_type(&self, other: &Self) -> bool {
self.clusters == other.clusters
&& self.registers == other.registers
&& self.struct_id == other.struct_id
&& self.module_id == other.module_id
}
}
impl HasSameType for Cluster {
fn has_same_type(&self, other: &Self) -> bool {
self.clusters == other.clusters
&& self.registers == other.registers
&& self.struct_id == other.struct_id
&& self.struct_module_path == other.struct_module_path
}
}
impl HasSameType for Register {
fn has_same_type(&self, other: &Self) -> bool {
self.fields == other.fields
&& self.struct_id == other.struct_id
&& self.struct_module_path == other.struct_module_path
&& self.access == other.access
&& self.size == other.size
&& self.reset_value == other.reset_value
}
}
#[derive(Default, Clone, Debug, PartialEq, Serialize)]
pub struct Device {
pub name: String,
pub description: String,
pub peripheral_mod: LinkedHashMap<String, Rc<RefCell<PeripheralMod>>>,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct EnumeratedSingleValue {
pub name: String,
pub value: u64,
pub description: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
pub enum EnumeratedValueUsage {
Read,
Write,
#[default]
ReadWrite,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct EnumeratedValueType {
pub name: String,
pub usage: EnumeratedValueUsage,
pub size: BitSize, pub values: Vec<EnumeratedSingleValue>,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FieldGetterSetter {
pub name: String,
pub description: String,
pub offset: u32,
pub mask: u32,
pub size: BitSize,
pub enum_types: Vec<EnumeratedValueType>,
pub enum_type_write: Option<String>,
pub enum_type_read: Option<String>,
pub access: RegisterBitfieldAccess,
pub dim: u32,
pub dim_increment: u32,
pub dim_index: Vec<String>,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum RegisterAccess {
#[default]
R,
W,
RW,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum RegisterBitfieldAccess {
#[default]
R,
W,
RW,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum BitSize {
BIT64,
#[default]
BIT32,
BIT16,
BIT8,
}
impl BitSize {
pub fn val_2_bit_size(val: u64) -> BitSize {
if val <= u8::MAX.into() {
BitSize::BIT8
} else if val <= u16::MAX.into() {
BitSize::BIT16
} else if val <= u32::MAX.into() {
BitSize::BIT32
} else {
BitSize::BIT64
}
}
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Register {
pub name: String,
pub description: String,
pub offset: u32,
pub dim: u32,
pub dim_increment: u32,
pub dim_index: Vec<String>,
pub access: RegisterAccess,
pub fields: LinkedHashMap<String, Rc<RefCell<FieldGetterSetter>>>,
pub size: BitSize,
pub reset_value: u64,
pub has_enumerated_fields: bool,
pub is_derived_from: bool,
pub struct_module_path: Vec<String>,
pub struct_id: String,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Cluster {
pub name: String,
pub description: String,
pub offset: u32,
pub dim: u32,
pub dim_increment: u32,
pub dim_index: Vec<String>,
pub registers: LinkedHashMap<String, Rc<RefCell<Register>>>,
pub clusters: LinkedHashMap<String, Rc<RefCell<Cluster>>>,
pub is_derived_from: bool,
pub struct_module_path: Vec<String>,
pub struct_id: String,
pub module_id: String,
}
#[derive(Default, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PeripheralMod {
pub name: String,
pub description: String,
pub clusters: LinkedHashMap<String, Rc<RefCell<Cluster>>>,
pub registers: LinkedHashMap<String, Rc<RefCell<Register>>>,
pub base_addr: Vec<u64>,
pub interrupts: Vec<Interrupt>,
pub derived_from: Option<String>,
pub struct_id: String,
pub module_id: String,
}
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub struct PathChunk {
pub path: String,
pub index: Option<u32>,
}
#[derive(Clone, Serialize, Deserialize, Debug, Hash, PartialEq, Eq)]
pub struct Interrupt {
pub name: String,
pub value: u32,
pub description: String,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct IR {
pub device: Device,
pub register_addresses: LinkedHashMap<u64, Vec<Vec<PathChunk>>>,
pub license_text: String,
pub version: String,
pub interrupt_table: Vec<Option<Interrupt>>,
pub nvic_prio_bits: Option<u32>,
pub vendor_systick_config: Option<bool>,
pub fpu_present: Option<bool>,
pub mpu_present: Option<bool>,
}