xmc4-hal 0.5.7

Peripheral access API for XMC4 series microcontrollers
Documentation
/// Shameless reuse from https://github.com/stm32-rs

/// Bits per second
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct Bps(pub u32);

#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct Hertz(pub u32);

#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct KiloHertz(pub u32);

#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
pub struct MegaHertz(pub u32);

/// Extension trait that adds convenience methods to the `u32` type
pub trait U32Ext {
    /// Wrap in `Bps`
    fn bps(self) -> Bps;

    /// Wrap in `Hertz`
    fn hz(self) -> Hertz;

    /// Wrap in `KiloHertz`
    fn khz(self) -> KiloHertz;

    /// Wrap in `MegaHertz`
    fn mhz(self) -> MegaHertz;
}

impl U32Ext for u32 {
    fn bps(self) -> Bps {
        Bps(self)
    }

    fn hz(self) -> Hertz {
        Hertz(self)
    }

    fn khz(self) -> KiloHertz {
        KiloHertz(self)
    }

    fn mhz(self) -> MegaHertz {
        MegaHertz(self)
    }
}

impl From<KiloHertz> for Hertz {
    fn from(val: KiloHertz) -> Self {
        Hertz(val.0 * 1_000)
    }
}

impl From<Hertz> for KiloHertz {
    fn from(val: Hertz) -> Self {
        KiloHertz(val.0 / 1_000)
    }
}

impl From<MegaHertz> for Hertz {
    fn from(val: MegaHertz) -> Self {
        Hertz(val.0 * 1_000_000)
    }
}

impl From<Hertz> for MegaHertz {
    fn from(val: Hertz) -> Self {
        MegaHertz(val.0 / 1_000_000)
    }
}

impl From<MegaHertz> for KiloHertz {
    fn from(val: MegaHertz) -> Self {
        KiloHertz(val.0 * 1_000)
    }
}

impl From<KiloHertz> for MegaHertz {
    fn from(val: KiloHertz) -> Self {
        MegaHertz(val.0 / 1_000)
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn to_hertz() {
        let hz32: u32 = 64;
        let hz = Hertz(64);
        assert_eq!(hz, hz32.hz());
    }

    #[test]
    fn to_kilohertz() {
        let hz32: u32 = 64;
        let hz = KiloHertz(64);
        assert_eq!(hz, hz32.khz());
    }

    #[test]
    fn to_megahertz() {
        let hz32: u32 = 64;
        let hz = MegaHertz(64);
        assert_eq!(hz, hz32.mhz());
    }

    #[test]
    fn to_bps() {
        let hz32: u32 = 64;
        let hz = Bps(64);
        assert_eq!(hz, hz32.bps());
    }

    #[test]
    fn to_hz_from_khz() {
        let hz = Hertz(55000);
        let khz = KiloHertz(55);
        assert_eq!(hz, khz.into());
    }

    #[test]
    fn to_hz_from_mhz() {
        let hz = Hertz(55000000);
        let mhz = MegaHertz(55);
        assert_eq!(hz, mhz.into());
    }

    #[test]
    fn to_khz_from_hz() {
        let hz = Hertz(55000000);
        let khz = KiloHertz(55000);
        assert_eq!(khz, hz.into());
    }

    #[test]
    fn to_khz_from_mhz() {
        let mhz = MegaHertz(55);
        let khz = KiloHertz(55000);
        assert_eq!(khz, mhz.into());
    }
}