diff options
author | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-11-09 11:03:42 -0500 |
---|---|---|
committer | Michael J. Chudobiak <mjc@avtechpulse.com> | 2012-11-09 11:03:42 -0500 |
commit | 65c7900768799338b143a024c64b32661a38200e (patch) | |
tree | c773313dd969889c87ae785998edb03b58a47359 /dds.c | |
parent | 3a706a1d515699369096e56764313c33158f58b7 (diff) |
untested: support for AD9834 + AD5620
Diffstat (limited to 'dds.c')
-rw-r--r-- | dds.c | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -0,0 +1,89 @@ +#include <stdbool.h> +#include <fcntl.h> +#include <stdio.h> +#include <unistd.h> + +#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); +} |