From 65c7900768799338b143a024c64b32661a38200e Mon Sep 17 00:00:00 2001 From: "Michael J. Chudobiak" Date: Fri, 9 Nov 2012 11:03:42 -0500 Subject: untested: support for AD9834 + AD5620 --- dds.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 dds.c (limited to 'dds.c') diff --git a/dds.c b/dds.c new file mode 100644 index 0000000..8002d78 --- /dev/null +++ b/dds.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +#include "dds.h" + +#define IIODEVICEPATH "/sys/bus/iio/devices/iio:device" +#define DACIIODEVICE "0" +#define DDSIIODEVICE "1" + +#define DDSFREQNODE "dds0_freq0" +#define DDSOUTPUTENABLENODE "dds0_out_enable" +#define DDSSQOUTPUTENABLENODE "dds0_out1_enable" +#define DDSWAVETYPENODE "dds0_out0_wavetype" +#define DDSWAVETYPESINE "sine\n" +#define DDSWAVETYPETRI "triangle\n" + +#define DACSCALENODE "out_voltage_scale" +#define DACOUTPUTNODE "out_voltage0_raw" + +#define ENABLE "1\n" +#define DISABLE "0\n" + +void dds_setamplitude(float millivolts) +{ + int scalefd = open(IIODEVICEPATH""DACIIODEVICE"/"DACSCALENODE, O_RDONLY); + int outputfd = open(IIODEVICEPATH""DACIIODEVICE"/"DACOUTPUTNODE, O_WRONLY); + + if (outputfd < 0 || scalefd < 0) { + printf("Couldn't open one or more of the control nodes for the dac\n"); + } + + // read the scale value and turn it into a float + float scale = 0; + char scalebuff[64]; + + if (read(scalefd, scalebuff, sizeof(scalebuff))) { + sscanf(scalebuff, "%f\n", &scale); + } + + if (scale == 0) { + printf("couldn't read scale or scale is invalid\n"); + return; + } + + // create a buffer with the scaled millivolt value and write it + char outputbuff[64]; + int outputchars = snprintf(outputbuff, sizeof(outputbuff), "%d\n", (int) (millivolts / scale)); + write(outputfd, outputbuff, outputchars); + + close(scalefd); + close(outputfd); +} + +void dds_setupwave(int frequency, bool squarewaveoutput, bool triangle) +{ + int wavetypefd = open(IIODEVICEPATH""DDSIIODEVICE"/"DDSWAVETYPENODE, O_WRONLY); + int outputenablefd = open(IIODEVICEPATH""DDSIIODEVICE"/"DDSOUTPUTENABLENODE, O_WRONLY); + int sqwaveenablefd = open(IIODEVICEPATH""DDSIIODEVICE"/"DDSSQOUTPUTENABLENODE, O_WRONLY); + int freqfd = open(IIODEVICEPATH""DDSIIODEVICE"/"DDSFREQNODE, O_WRONLY); + + if (wavetypefd < 0 || outputenablefd < 0 || sqwaveenablefd < 0 || freqfd < 0) { + printf("Couldn't open one or more of the control nodes for the dds\n"); + return; + } + + if (triangle) { + // squarewave output on the signbit out pin can't be selected at the same time as triangle wave + write(sqwaveenablefd, DISABLE, sizeof(DISABLE)); + write(wavetypefd, DDSWAVETYPETRI, sizeof(DDSWAVETYPETRI)); + } else { + // enable/disable the square wave ouput and reset the wavetype + write(sqwaveenablefd, squarewaveoutput ? ENABLE : DISABLE, squarewaveoutput ? sizeof(ENABLE) : sizeof(DISABLE)); + write(wavetypefd, DDSWAVETYPESINE, sizeof(DDSWAVETYPESINE)); + } + + // generate a buffer with the frequency and set it + char freqbuff[64]; + int freqchars = snprintf(freqbuff, sizeof(freqbuff), "%d\n", frequency); + write(freqfd, freqbuff, freqchars); + + write(outputenablefd, ENABLE, sizeof(ENABLE)); + + close(sqwaveenablefd); + close(wavetypefd); + close(freqfd); + close(outputenablefd); +} -- cgit