[−][src]Crate ad983x
This is a platform agnostic Rust driver for the AD9833, AD9834, AD9837
and AD9838 low-power programmable waveform generators / direct digital
synthesizers (DDS) using the embedded-hal
traits.
This driver allows you to:
- Enable/disable/reset the device. See
enable()
. - Set the frequency registers. See:
set_frequency()
. - Select the output frequency register. See:
select_frequency()
. - Set the phase registers. See:
set_phase()
. - Select the output phase register. See:
select_phase()
. - Set the frequency registers MSBs/LSBs separately. See:
set_frequency_msb()
. - Set the output waveform. See:
set_output_waveform()
. - Power down/up device parts. See:
set_powered_down()
. - Select control source on AD9834/AD9838. See:
set_control_source()
.
The devices
The AD9833, AD9834, AD9837 and AD9838 are low power, programmable waveform generators capable of producing sine, triangular, and square wave outputs. Waveform generation is required in various types of sensing, actuation, and time domain reflectometry (TDR) applications. The output frequency and phase are software programmable, allowing easy tuning. No external components are needed. The frequency registers are 28 bits wide: with a 25 MHz clock rate, resolution of 0.1 Hz can be achieved; with a 1 MHz clock rate, the AD9833 can be tuned to 0.004 Hz resolution.
The devices are written to via a 3-wire serial interface (SPI). This serial interface operates at clock rates up to 40 MHz and is compatible with DSP and microcontroller standards. The devices operate with a power supply from 2.3 V to 5.5 V.
Datasheets:
Application Note:
Article explaining DDS using an AD9833:
Pin / Software control source on AD9834/AD9838
AD9834/AD9838 devices offer the possibility to control several functions either through hardware pins or software settings. While hardware pin control is selected, these software operations will be ignored by the hardware. This driver allows this as well as it would be a valid use case to configure the status of these functions while on hardware pin control mode in preparation for a smooth switch to software control.
Usage examples (see also examples folder)
To use this driver, import this crate and an embedded_hal
implementation,
then instantiate the appropriate device.
In the following examples an instance of the device AD9833 will be created
as an example. Other devices can be created with similar methods like:
Ad983x::new_ad9837(...)
.
Please find additional examples using hardware in this repository: driver-examples.
This includes an example MIDI player that plays Beethoven's ninth symphony.
Set the frequency register 0 and enable
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, FrequencyRegister}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9833(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation dds.set_frequency(FrequencyRegister::F0, 4724).unwrap(); dds.enable().unwrap(); // Given a 25 MHz clock, this now outputs a sine wave // with a frequency of 440 Hz, which is a standard // A4 tone. // Get SPI device and CS pin back let (_spi, _chip_select) = dds.destroy();
Set frequency registers 0 and 1 and alternate between them
With a 25 MHz clock this alternates between A4 and D5 tones.
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, FrequencyRegister}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9833(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation // A4 tone for a 25 MHz clock dds.set_frequency(FrequencyRegister::F0, 4724).unwrap(); // D5 tone for a 25 MHz clock dds.set_frequency(FrequencyRegister::F1, 6306).unwrap(); dds.enable().unwrap(); loop { // some delay dds.select_frequency(FrequencyRegister::F1).unwrap(); // some delay dds.select_frequency(FrequencyRegister::F0).unwrap(); }
Set the phase register 1 and select it
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, PhaseRegister}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9833(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation dds.set_phase(PhaseRegister::P1, 4724).unwrap(); dds.select_phase(PhaseRegister::P1).unwrap();
Set output waveform to be triangular
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, OutputWaveform}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9833(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation dds.set_output_waveform(OutputWaveform::Triangle).unwrap();
Power down the DAC
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, PoweredDown}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9833(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation dds.set_powered_down(PoweredDown::Dac).unwrap();
Use hardware pins as control source
extern crate ad983x; extern crate linux_embedded_hal; use ad983x::{Ad983x, ControlSource}; use linux_embedded_hal::{Pin, Spidev}; let spi = Spidev::open("/dev/spidev0.0").unwrap(); let chip_select = Pin::new(25); let mut dds = Ad983x::new_ad9838(spi, chip_select); dds.reset().unwrap(); // reset is necessary before operation dds.set_control_source(ControlSource::HardwarePins).unwrap(); // Hardware pins can now be used to control the device. // The corresponding software settings will be ignored.
Structs
Ad983x | AD983x direct digital synthesizer |
Enums
ControlSource | Hardware pin / software control source for the functions: frequency register selection, phase register selection, reset of internal registers, and DAC power-down. (Only available on AD9834 and AD9838 devices) |
Error | All possible errors in this crate |
FrequencyRegister | Frequency registers |
OutputWaveform | Output waveform |
PhaseRegister | Phase registers |
PoweredDown | Powered-down device configuration |
SignBitOutput | Sign bit output on AD9834/AD9838 devices |
Constants
MODE | SPI mode (CPOL = 1, CPHA = 0) |