Skip to content

Commit

Permalink
Simplifying changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Jul 7, 2023
1 parent b4008ec commit 3c18a7f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 50 deletions.
52 changes: 24 additions & 28 deletions ad9959/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,36 +97,33 @@ pub enum Error {
}

impl<I: Interface> Ad9959<I> {
/// Constructor.
/// Construct and initialize the DDS.
///
/// Args:
/// * `interface` - An interface to the DDS.
/// `clock_frequency` to generate the system clock.
pub fn new(interface: I, clock_frequency: f32) -> Self {
Self {
interface,
reference_clock_frequency: clock_frequency,
system_clock_multiplier: 1,
communication_mode: Mode::SingleBitTwoWire,
}
}

/// Initialize the DDS
///
/// # Args
/// * `reset_pin` - A pin connected to the DDS reset input.
/// * `io_update` - A pin connected to the DDS io_update input.
/// * `delay` - A delay implementation for blocking operation for specific amounts of time.
/// * `desired_mode` - The desired communication mode of the interface to the DDS.
/// * `clock_frequency` - The clock frequency of the reference clock input.
/// * `multiplier` - The desired clock multiplier for the system clock. This multiplies
pub fn initialize(
&mut self,
reset_pin: &mut impl OutputPin,
/// `clock_frequency` to generate the system clock.
pub fn new(
interface: I,
mut reset_pin: impl OutputPin,
io_update: &mut impl OutputPin,
delay: &mut impl DelayUs<u16>,
delay: &mut impl DelayUs<u8>,
desired_mode: Mode,
clock_frequency: f32,
multiplier: u8,
) -> Result<(), Error> {
) -> Result<Self, Error> {
let mut ad9959 = Ad9959 {
interface,
reference_clock_frequency: clock_frequency,
system_clock_multiplier: 1,
communication_mode: desired_mode,
};

io_update.set_low().or(Err(Error::Pin))?;

// Reset the AD9959
Expand All @@ -139,13 +136,14 @@ impl<I: Interface> Ad9959<I> {

reset_pin.set_low().or(Err(Error::Pin))?;

self.interface
ad9959
.interface
.configure_mode(Mode::SingleBitTwoWire)
.or(Err(Error::Interface))?;

// Program the interface configuration in the AD9959. Default to all channels enabled.
let csr = [Channel::ALL.bits() | desired_mode as u8];
self.write(Register::CSR, &csr)?;
ad9959.write(Register::CSR, &csr)?;

// Latch the new interface configuration.
io_update.set_high().or(Err(Error::Pin))?;
Expand All @@ -157,7 +155,8 @@ impl<I: Interface> Ad9959<I> {

io_update.set_low().or(Err(Error::Pin))?;

self.interface
ad9959
.interface
.configure_mode(desired_mode)
.or(Err(Error::Interface))?;

Expand All @@ -171,16 +170,13 @@ impl<I: Interface> Ad9959<I> {

// Read back the CSR to ensure it specifies the mode correctly.
let mut updated_csr: [u8; 1] = [0];
self.read(Register::CSR, &mut updated_csr)?;
ad9959.read(Register::CSR, &mut updated_csr)?;
if updated_csr[0] != csr[0] {
return Err(Error::Check);
}

// Set the clock frequency to configure the device as necessary.
self.configure_system_clock(
self.reference_clock_frequency,
multiplier,
)?;
ad9959.configure_system_clock(clock_frequency, multiplier)?;

// Latch the new clock configuration.
io_update.set_high().or(Err(Error::Pin))?;
Expand All @@ -192,7 +188,7 @@ impl<I: Interface> Ad9959<I> {

io_update.set_low().or(Err(Error::Pin))?;

Ok(())
Ok(ad9959)
}

fn read(&mut self, reg: Register, data: &mut [u8]) -> Result<(), Error> {
Expand Down
37 changes: 15 additions & 22 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,35 +870,28 @@ pub fn setup(
};

#[cfg(not(feature = "pounder_v1_0"))]
let mut reset_pin = gpiog.pg6.into_push_pull_output();
let reset_pin = gpiog.pg6.into_push_pull_output();
#[cfg(feature = "pounder_v1_0")]
let mut reset_pin = gpioa.pa0.into_push_pull_output();
let reset_pin = gpioa.pa0.into_push_pull_output();

let mut io_update = gpiog.pg7.into_push_pull_output();

// Delay to allow the pounder DDS reference clock to fully start up. The exact startup
// time is not specified, but bench testing indicates it usually comes up within
// 200-300uS. We do a larger delay to ensure that it comes up and is stable before
// using it.
delay.delay_ms(10u32);

let mut ad9959 = ad9959::Ad9959::new(
qspi_interface,
reset_pin,
&mut io_update,
&mut delay,
ad9959::Mode::FourBitSerial,
design_parameters::DDS_REF_CLK.to_Hz() as f32,
);

// The oscillator to the AD9959 takes some time to turn on after first initialization.
// To accomodate this, we retry the initialization process until the configuration
// process completes successfully.
loop {
match ad9959.initialize(
&mut reset_pin,
&mut io_update,
&mut delay,
ad9959::Mode::FourBitSerial,
design_parameters::DDS_MULTIPLIER,
) {
Ok(()) => break,
// If the cross-check readback failed, it's because the oscillator is not up
// yet.
Err(ad9959::Error::Check) => {}
other => other.unwrap(),
}
}
design_parameters::DDS_MULTIPLIER,
)
.unwrap();

ad9959.self_test().unwrap();

Expand Down

0 comments on commit 3c18a7f

Please sign in to comment.