pub fn new<R: Rate>(
        dev: D,
        xosc_frequency: Generic<u32>,
        config: PLLConfig<R>,
    ) -> Result<PhaseLockedLoop<Disabled, D>, Error>
    where
        R: Into<Hertz<u64>>,
    {
        const VCO_FREQ_RANGE: RangeInclusive<Hertz<u32>> =
            Hertz(400_000_000)..=Hertz(1_600_000_000);
        const POSTDIV_RANGE: Range<u8> = 1..7;
        const FBDIV_RANGE: Range<u16> = 16..320;

        //First we convert our rate to Hertz<u64> as all other rates can be converted to that.
        let vco_freq: Hertz<u64> = config.vco_freq.into();

        //Then we try to downscale to u32.
        let vco_freq: Hertz<u32> = vco_freq.try_into().map_err(|_| Error::BadArgument)?;

        if !VCO_FREQ_RANGE.contains(&vco_freq) {
            return Err(Error::VcoFreqOutOfRange);
        }

        if !POSTDIV_RANGE.contains(&config.post_div1) || !POSTDIV_RANGE.contains(&config.post_div2)
        {
            return Err(Error::PostDivOutOfRage);
        }

        let ref_freq_range: Range<Hertz<u32>> = Hertz(5_000_000)..vco_freq.div(16);

        let ref_freq_hz = Hertz::<u32>::try_from(xosc_frequency)
            .map_err(|_| Error::BadArgument)?
            .checked_div(&(config.refdiv as u32))
            .ok_or(Error::BadArgument)?;

        if !ref_freq_range.contains(&ref_freq_hz) {
            return Err(Error::RefFreqOutOfRange);
        }

        let fbdiv = vco_freq
            .checked_div(ref_freq_hz.integer())
            .ok_or(Error::BadArgument)?;

        let fbdiv: u16 = (*fbdiv.integer())
            .try_into()
            .map_err(|_| Error::BadArgument)?;

        if !FBDIV_RANGE.contains(&fbdiv) {
            return Err(Error::FeedbackDivOutOfRange);
        }

        let refdiv = config.refdiv;
        let post_div1 = config.post_div1;
        let post_div2 = config.post_div2;

        Ok(PhaseLockedLoop {
            state: Disabled {
                refdiv,
                fbdiv,
                post_div1,
                post_div2,
            },
            device: dev,
        })
    }
/// Instantiates a new Phase-Locked-Loop device.
Instantiates a new Phase-Locked-Loop device.
Instantiates a new Phase - Locked - Loop device .
pub fn new<R: Rate>(
        dev: D,
        xosc_frequency: Generic<u32>,
        config: PLLConfig<R>,
    ) -> Result<PhaseLockedLoop<Disabled, D>, Error>
    where
        R: Into<Hertz<u64>>,
    {
        const VCO_FREQ_RANGE: RangeInclusive<Hertz<u32>> =
            Hertz(400_000_000)..=Hertz(1_600_000_000);
        const POSTDIV_RANGE: Range<u8> = 1..7;
        const FBDIV_RANGE: Range<u16> = 16..320;
        //First we convert our rate to Hertz<u64> as all other rates can be converted to that.
        let vco_freq: Hertz<u64> = config.vco_freq.into();
        //Then we try to downscale to u32.
        let vco_freq: Hertz<u32> = vco_freq.try_into().map_err(|_| Error::BadArgument)?;
        if !VCO_FREQ_RANGE.contains(&vco_freq) {
            return Err(Error::VcoFreqOutOfRange);
        }
        if !POSTDIV_RANGE.contains(&config.post_div1) || !POSTDIV_RANGE.contains(&config.post_div2)
        {
            return Err(Error::PostDivOutOfRage);
        }
        let ref_freq_range: Range<Hertz<u32>> = Hertz(5_000_000)..vco_freq.div(16);
        let ref_freq_hz = Hertz::<u32>::try_from(xosc_frequency)
            .map_err(|_| Error::BadArgument)?
            .checked_div(&(config.refdiv as u32))
            .ok_or(Error::BadArgument)?;
        if !ref_freq_range.contains(&ref_freq_hz) {
            return Err(Error::RefFreqOutOfRange);
        }
        let fbdiv = vco_freq
            .checked_div(ref_freq_hz.integer())
            .ok_or(Error::BadArgument)?;
        let fbdiv: u16 = (*fbdiv.integer())
            .try_into()
            .map_err(|_| Error::BadArgument)?;
        if !FBDIV_RANGE.contains(&fbdiv) {
            return Err(Error::FeedbackDivOutOfRange);
        }
        let refdiv = config.refdiv;
        let post_div1 = config.post_div1;
        let post_div2 = config.post_div2;
        Ok(PhaseLockedLoop {
            state: Disabled {
                refdiv,
                fbdiv,
                post_div1,
                post_div2,
            },
            device: dev,
        })
    }
pub fn new < R : Rate > ( dev : D , xosc_frequency : Generic < u32 > , config : PLLConfig < R > , ) -> Result < PhaseLockedLoop < Disabled , D > , Error > where R : Into < Hertz < u64 > > , { const VCO_FREQ_RANGE : RangeInclusive < Hertz < u32 > > = Hertz ( 400_000_000 ) ..= Hertz ( 1_600_000_000 ) ; const POSTDIV_RANGE : Range < u8 > = 1 .. 7 ; const FBDIV_RANGE : Range < u16 > = 16 .. 320 ; //First we convert our rate to Hertz<u64> as all other rates can be converted to that. let vco_freq : Hertz < u64 > = config . vco_freq . into ( ) ; //Then we try to downscale to u32. let vco_freq : Hertz < u32 > = vco_freq . try_into ( ) . map_err ( | _ | Error :: BadArgument ) ? ; if ! VCO_FREQ_RANGE . contains ( & vco_freq ) { return Err ( Error :: VcoFreqOutOfRange ) ; } if ! POSTDIV_RANGE . contains ( & config . post_div1 ) || ! POSTDIV_RANGE . contains ( & config . post_div2 ) { return Err ( Error :: PostDivOutOfRage ) ; } let ref_freq_range : Range < Hertz < u32 > > = Hertz ( 5_000_000 ) .. vco_freq . div ( 16 ) ; let ref_freq_hz = Hertz :: < u32 > :: try_from ( xosc_frequency ) . map_err ( | _ | Error :: BadArgument ) ? . checked_div ( & ( config . refdiv as u32 ) ) . ok_or ( Error :: BadArgument ) ? ; if ! ref_freq_range . contains ( & ref_freq_hz ) { return Err ( Error :: RefFreqOutOfRange ) ; } let fbdiv = vco_freq . checked_div ( ref_freq_hz . integer ( ) ) . ok_or ( Error :: BadArgument ) ? ; let fbdiv : u16 = ( * fbdiv . integer ( ) ) . try_into ( ) . map_err ( | _ | Error :: BadArgument ) ? ; if ! FBDIV_RANGE . contains ( & fbdiv ) { return Err ( Error :: FeedbackDivOutOfRange ) ; } let refdiv = config . refdiv ; let post_div1 = config . post_div1 ; let post_div2 = config . post_div2 ; Ok ( PhaseLockedLoop { state : Disabled { refdiv , fbdiv , post_div1 , post_div2 , } , device : dev , } ) }
Instantiates a new Phase-Locked-Loop device.
Instantiates a new Phase - Locked - Loop device .
dev
D
xosc_frequency
Generic<u32>
config
PLLConfig<R>
dev
D
xosc_frequency
Generic<u32>
config
PLLConfig<R>