#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, "%10f\n", &scale); // the string should be ?.??? but round up the width a bit just in case } 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); }