svd-generator 0.4.4

Converts device information from flattened device tree into an SVD description
Documentation
use crate::svd::create_peripheral;
use crate::{Error, Result};

pub mod registers;

/// Represents the offsets of Cadence USB3 register cluster groups.
pub struct CdnsUsb3Offsets {
    /// Dual-role OTG (on-the-go) registers.
    pub otg: u32,
    /// XHCI registers.
    pub xhci: u32,
    /// Device registers.
    pub dev: u32,
}

impl TryFrom<&device_tree::Node> for CdnsUsb3Offsets {
    type Error = Error;

    fn try_from(val: &device_tree::Node) -> Result<Self> {
        // FIXME: write parser that takes #address-cells and #size-cells into account
        let regs = val
            .prop_u32_list("reg")?
            .chunks_exact(2)
            .map(|c| c[0])
            .collect::<Vec<u32>>();

        let reg_names = val.prop_str_list("reg-names")?;

        let regs_len = regs.len();
        let reg_names_len = reg_names.len();

        if regs_len != reg_names_len {
            Err(Error::DeviceTree(format!("CdnsUsb3 `regs` and `reg-names` mismatch, `regs` len: {regs_len}, `reg-names` len: {reg_names_len}")))
        } else {
            let otg_idx = reg_names
                .iter()
                .position(|r| r.contains("otg"))
                .ok_or(Error::Svd("no CdnsUsb3 OTG register offset".into()))?;

            let xhci_idx = reg_names
                .iter()
                .position(|r| r.contains("xhci"))
                .ok_or(Error::Svd("no CdnsUsb3 XHCI register offset".into()))?;

            let dev_idx = reg_names
                .iter()
                .position(|r| r.contains("dev"))
                .ok_or(Error::Svd("no CdnsUsb3 device register offset".into()))?;

            Ok(Self {
                otg: regs[otg_idx],
                xhci: regs[xhci_idx],
                dev: regs[dev_idx],
            })
        }
    }
}

/// Represents a Cadence USB3 compatible peripheral definition.
pub struct CdnsUsb3 {
    peripheral: svd::Peripheral,
}

impl CdnsUsb3 {
    /// Creates a new [CdnsUsb3] peripheral.
    pub fn create(
        name: &str,
        base_address: u64,
        size: u32,
        interrupt: Option<Vec<svd::Interrupt>>,
        offsets: CdnsUsb3Offsets,
        dim: u32,
    ) -> Result<Self> {
        let dim_element = if dim > 0 {
            Some(
                svd::DimElement::builder()
                    .dim(dim)
                    .dim_increment(size)
                    .build(svd::ValidateLevel::Strict)?,
            )
        } else {
            None
        };

        let peripheral = create_peripheral(
            name,
            format!("Cadence USB3: {name}").as_str(),
            base_address,
            size,
            interrupt,
            Some(registers::create(offsets)?),
            dim_element,
        )?;

        Ok(Self { peripheral })
    }

    /// Gets a reference to the SVD [`Peripheral`](svd::Peripheral).
    pub const fn peripheral(&self) -> &svd::Peripheral {
        &self.peripheral
    }

    /// Gets a mutable reference to the SVD [`Peripheral`](svd::Peripheral).
    pub fn peripheral_mut(&mut self) -> &mut svd::Peripheral {
        &mut self.peripheral
    }

    /// Converts the [CdnsUsb3] into the inner SVD [`Peripheral`](svd::Peripheral).
    pub fn to_inner(self) -> svd::Peripheral {
        self.peripheral
    }
}