| 1 | /*********************************************************************** |
| 2 | * This utility programs the ADF4351 frequency synthesizers, and |
| 3 | * initializes the Langford Device. |
| 4 | * |
| 5 | * (c) 2013 Per Vices Corporation |
| 6 | * See LICENSE.txt for license and copyright information. |
| 7 | **********************************************************************/ |
| 8 | |
| 9 | #include <iostream> |
| 10 | #include <string> |
| 11 | #include "adf4351_regs.hpp" |
| 12 | #include <fcntl.h> |
| 13 | #include <unistd.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "fsynth.hpp" |
| 19 | #include "../langford_ioctl.h" |
| 20 | |
| 21 | |
| 22 | int SetDDCFreq(std::string FileName, int Channel, double ActualFreq, double TargetFreq, int verbosity); |
| 23 | int WriteReg(std::string FileName, int Channel, uint32_t reg_val); |
| 24 | |
| 25 | |
| 26 | int main(int argc, char **argv) { |
| 27 | unsigned long TargetFreq; |
| 28 | // unsigned long RefIn=156250000; //Set Reference Input in Hz |
| 29 | unsigned long RefIn=125000000; //Set Reference Input in Hz |
| 30 | unsigned int chan_mod = 125; // Modulus used to figure Channel Spacing |
| 31 | double ActualFreq; |
| 32 | int rc, i; |
| 33 | int verbosity, lowspur; |
| 34 | |
| 35 | enum Channel_t{ |
| 36 | rx = 1, |
| 37 | tx = 2 |
| 38 | }; |
| 39 | Channel_t Channel; |
| 40 | |
| 41 | if ( (argc < 4) || (argc > 6) ){ |
| 42 | std::cerr << "Frequency Programmer: Phi production." << std::endl; |
| 43 | std::cerr << "Usage:" << std::endl; |
| 44 | std::cerr << "\t" << argv[0] << " [DEVICE_NAME] [CHANNEL] [TARGET_FREQ] (OUTPUT)" << std::endl; |
| 45 | std::cerr << "[DEVICE_NAME] default is /dev/langford" << std::endl; |
| 46 | std::cerr << "[CHANNEL] = {tx|rx} to program transmission or recieve branch" << std::endl; |
| 47 | std::cerr << "[TARGET_FREQ] is the desired frequency to be tuned, measured in Hertz (Hz). 0 for power down." << std::endl; |
| 48 | std::cerr << "[OUTPUT] = {0|1|2} is the (optional) output format (0 = silent (default), 1 = verbose, 2=driver)" << std::endl; |
| 49 | std::cerr << "[NOISE MODE] = {0|1} moderates output spurs (0 = low spur (default), 1 = low noise)" << std::endl; |
| 50 | //std::cerr << "channel = Tx corresponds to N61CE, N61DATA, N61CLK and N61LE" << std::endl; |
| 51 | //std::cerr << "channel = 2 corresponds to N62CE, N62DATA, N62CLK and N62LE" << std::endl; |
| 52 | return 1; |
| 53 | }; |
| 54 | |
| 55 | if ( (std::string(argv[2]) == "rx") || (int( strtoul(argv[2], 0, 0) ) == 1) ){ |
| 56 | Channel = rx; |
| 57 | } |
| 58 | else if ( (std::string(argv[2]) == "tx") || (int( strtoul(argv[2], 0, 0) ) == 2) ){ |
| 59 | Channel = tx; |
| 60 | } |
| 61 | else { |
| 62 | std::cerr << "Invalid Channel Selection: Valid values for [CHANNEL] are: {rx|tx}" << std::endl; |
| 63 | return 1; |
| 64 | }; |
| 65 | |
| 66 | if ( ( (atof(argv[3]) >= 34375000) && (atof(argv[3]) <=4400000000) ) || (atof(argv[3]) == 0) ) { |
| 67 | TargetFreq = atof(argv[3]); |
| 68 | } |
| 69 | else { |
| 70 | std::cerr << "Invalid Frequency: Valid values for [TARGET_FREQ] limited to set [34375000,4400000000], or 0 for power down." << std::endl; |
| 71 | return 1; |
| 72 | }; |
| 73 | |
| 74 | if ( (argc == 5) ){ |
| 75 | if (std::string(argv[4]) == "0") { verbosity = 0; } |
| 76 | else if (std::string(argv[4]) == "1") { verbosity = 1; std::cout.precision(10); } |
| 77 | else if (std::string(argv[4]) == "2") { verbosity = 2; std::cout.precision(10); } |
| 78 | else { std::cerr << "Invalid output selection: 0 for verbose mode, 1 for driver"; return 1;}; |
| 79 | } |
| 80 | else { verbosity = 0; }; |
| 81 | |
| 82 | if ( (argc == 6) ){ |
| 83 | if (std::string(argv[5]) == "0") { lowspur = 0; } |
| 84 | else if (std::string(argv[4]) == "1") { lowspur = 1; std::cout.precision(10); } |
| 85 | else { std::cerr << "Invalid Noise mode selection: 0 for low spur, 1 for low noise"; return 1;}; |
| 86 | } |
| 87 | else { lowspur = 0; }; |
| 88 | |
| 89 | //Initialize |
| 90 | fsynth_t fsynth; |
| 91 | |
| 92 | //Get Target Frequency (in MHz). |
| 93 | if (verbosity == 1){ |
| 94 | std::cout |
| 95 | << "\nRequesting Desired Frequency (@ Refin = " << (float)RefIn/1e6 |
| 96 | << "MHz) in MHz: " << std::dec << TargetFreq / 1000000 |
| 97 | << ", using MOD=" << chan_mod |
| 98 | << std::endl; |
| 99 | }; |
| 100 | //If target frequency is 0, power down VCO. |
| 101 | ActualFreq = (int(TargetFreq) == 0 ? fsynth.power_down = fsynth_t::POWER_DOWN_ENABLED : fsynth.set_freq(TargetFreq, RefIn, chan_mod, bool( int(verbosity) == 1 ? true : false ) ) ); |
| 102 | |
| 103 | fsynth.low_noise_and_spur = ( int(lowspur) == 0 ? fsynth_t::LOW_NOISE_AND_SPUR_LOW_SPUR : fsynth_t::LOW_NOISE_AND_SPUR_LOW_NOISE ); |
| 104 | |
| 105 | if (verbosity == 2) { |
| 106 | std::cout << ActualFreq << std::endl; |
| 107 | }; |
| 108 | |
| 109 | //Debug Information: |
| 110 | if (verbosity == 1){ |
| 111 | std::cout << "Programming Channel: " |
| 112 | << std::string( int(Channel) == 1 ? "rx" : "tx") |
| 113 | << std::endl; |
| 114 | //fsynth.show_regs(); |
| 115 | }; |
| 116 | |
| 117 | //Write register content |
| 118 | rc = 0; |
| 119 | //for (i = 0; i < 6; i++) { |
| 120 | for (i = 5; i >= 0; i--) { |
| 121 | if (verbosity == 1){ std::cout << "Register " << i << " => 0x"<< std::hex << fsynth.get_reg(i) << std::dec << std::endl; }; |
| 122 | rc |= WriteReg(argv[1], Channel, fsynth.get_reg(i)); |
| 123 | }; |
| 124 | |
| 125 | //Compensate for the remaining frequency difference in DSP |
| 126 | rc |= SetDDCFreq(argv[1], Channel, ActualFreq, TargetFreq, verbosity); |
| 127 | |
| 128 | // //Debug Information: |
| 129 | // if (debug == true){ |
| 130 | // std::cout |
| 131 | // << "DDC Phase is: " |
| 132 | // << std::string( bool(RevFreq) == 0 ? "-" : "+") |
| 133 | // << Phase << std::endl; |
| 134 | // }; |
| 135 | // |
| 136 | // if (driver == true){ |
| 137 | // std::cout |
| 138 | // << std::string( float( (ActualFreq - TargetFreq) / 125e6 )) << std::endl; |
| 139 | // }; |
| 140 | |
| 141 | if (verbosity == 1){ |
| 142 | std::cout << "Return code from io_ctl calls: " << rc << std::endl; |
| 143 | }; |
| 144 | |
| 145 | return rc; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | int SetDDCFreq(std::string FileName, int Channel, double ActualFreq, double TargetFreq, int verbose) { |
| 150 | int DevFD; |
| 151 | int SET_Phase; |
| 152 | int SET_RevFreq; |
| 153 | int RevFreq = (ActualFreq > TargetFreq); |
| 154 | int Phase; |
| 155 | int rc = 0; |
| 156 | |
| 157 | if (Channel == 1) { |
| 158 | SET_Phase = IOCTL_SET_RXPhase; |
| 159 | SET_RevFreq = IOCTL_SET_RXRevFreq; |
| 160 | } else if (Channel == 2) { |
| 161 | SET_Phase = IOCTL_SET_TXPhase; |
| 162 | SET_RevFreq = IOCTL_SET_TXRevFreq; |
| 163 | } else { |
| 164 | std::cerr << "Invalid selection for channel" << std::endl; |
| 165 | return 1; |
| 166 | }; |
| 167 | |
| 168 | DevFD = open(FileName.c_str(), O_RDWR); |
| 169 | if (!DevFD) { |
| 170 | std::cerr << "Cannot open " << FileName << std::endl; |
| 171 | return 1; |
| 172 | }; |
| 173 | |
| 174 | Phase = fabs(ActualFreq - TargetFreq) / 125e6 * 0xffffffff; |
| 175 | |
| 176 | rc |= ioctl(DevFD, SET_Phase, &Phase); |
| 177 | rc |= ioctl(DevFD, SET_RevFreq, &RevFreq); |
| 178 | |
| 179 | close(DevFD); |
| 180 | return rc; |
| 181 | }; |
| 182 | |
| 183 | |
| 184 | int WriteReg(std::string FileName, int Channel, uint32_t reg_val) { |
| 185 | int DevFD; |
| 186 | int val; |
| 187 | int rc = 0; |
| 188 | const unsigned long long val0 = 0, val1 = 1; |
| 189 | int NCE, NDATA, NCLK, NLE; |
| 190 | int i; |
| 191 | |
| 192 | if (Channel == 1) { |
| 193 | NCE = IOCTL_SET_N61CE; |
| 194 | NDATA = IOCTL_SET_N61DATA; |
| 195 | NCLK = IOCTL_SET_N61CLK; |
| 196 | NLE = IOCTL_SET_N61LE; |
| 197 | } else if (Channel == 2) { |
| 198 | NCE = IOCTL_SET_N62CE; |
| 199 | NDATA = IOCTL_SET_N62DATA; |
| 200 | NCLK = IOCTL_SET_N62CLK; |
| 201 | NLE = IOCTL_SET_N62LE; |
| 202 | } else { |
| 203 | std::cerr << "Invalid selection for channel" << std::endl; |
| 204 | return 1; |
| 205 | }; |
| 206 | |
| 207 | DevFD = open(FileName.c_str(), O_RDWR); |
| 208 | if (!DevFD) { |
| 209 | std::cerr << "Cannot open " << FileName << std::endl; |
| 210 | return 1; |
| 211 | }; |
| 212 | |
| 213 | rc |= ioctl(DevFD, NLE, &val0); |
| 214 | rc |= ioctl(DevFD, NCE, &val1); |
| 215 | for (i = 0; i < 32; i++) { |
| 216 | rc |= ioctl(DevFD, NCLK, &val0); |
| 217 | val = (reg_val >> (31 - i)) & 0x00000001; |
| 218 | rc |= ioctl(DevFD, NDATA, &val); |
| 219 | rc |= ioctl(DevFD, NCLK, &val1); |
| 220 | }; |
| 221 | rc |= ioctl(DevFD, NCLK, &val0); |
| 222 | rc |= ioctl(DevFD, NDATA, &val0); |
| 223 | rc |= ioctl(DevFD, NLE, &val1); |
| 224 | rc |= ioctl(DevFD, NLE, &val0); |
| 225 | |
| 226 | close(DevFD); |
| 227 | return rc; |
| 228 | }; |