| 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"cxx . . $ `<file>/libs/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"cxx . . $ `<file>/libs/fsynth.hpp`/ |
| 19 | #include "../langford_ioctl.h"cxx . . $ `<file>/langford_ioctl.h`/ |
| 20 | |
| 21 | |
| 22 | int SetDDCFreqcxx . . $ SetDDCFreq(20d847afbf6d1cd3).(stdcxx . . $ std/::stringcxx . . $ std/string# FileName, int Channel, double ActualFreq, double TargetFreq, int verbosity); |
| 23 | int WriteRegcxx . . $ WriteReg(e62d13286998b6de).(stdcxx . . $ std/::stringcxx . . $ std/string# FileName, int Channel, uint32_tcxx . . $ uint32_t# reg_val); |
| 24 | |
| 25 | |
| 26 | int maincxx . . $ main(afcd3168fc1188a4).(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_tcxx . . $ main(afcd3168fc1188a4).Channel_t#{ |
| 36 | rxcxx . . $ main(afcd3168fc1188a4).rx. = 1, |
| 37 | txcxx . . $ main(afcd3168fc1188a4).tx. = 2 |
| 38 | }; |
| 39 | Channel_tcxx . . $ main(afcd3168fc1188a4).Channel_t# Channel; |
| 40 | |
| 41 | if ( (argc < 4) || (argc > 6) ){ |
| 42 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Frequency Programmer: Phi production." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 43 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Usage:" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 44 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\t" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). argv[0] <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " [DEVICE_NAME] [CHANNEL] [TARGET_FREQ] (OUTPUT)" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 45 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "[DEVICE_NAME] default is /dev/langford" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 46 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "[CHANNEL] = {tx|rx} to program transmission or recieve branch" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 47 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "[TARGET_FREQ] is the desired frequency to be tuned, measured in Hertz (Hz). 0 for power down." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 48 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "[OUTPUT] = {0|1|2} is the (optional) output format (0 = silent (default), 1 = verbose, 2=driver)" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 49 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "[NOISE MODE] = {0|1} moderates output spurs (0 = low spur (default), 1 = low noise)" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 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 ( (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[2]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "rx") || (int( strtoulcxx . . $ strtoul(335a43d87d12a1b6).(argv[2], 0, 0) ) == 1) ){ |
| 56 | Channel = rxcxx . . $ main(afcd3168fc1188a4).rx.; |
| 57 | } |
| 58 | else if ( (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[2]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "tx") || (int( strtoulcxx . . $ strtoul(335a43d87d12a1b6).(argv[2], 0, 0) ) == 2) ){ |
| 59 | Channel = txcxx . . $ main(afcd3168fc1188a4).tx.; |
| 60 | } |
| 61 | else { |
| 62 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid Channel Selection: Valid values for [CHANNEL] are: {rx|tx}" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 63 | return 1; |
| 64 | }; |
| 65 | |
| 66 | if ( ( (atofcxx . . $ atof(9b0361fcbc284e4f).(argv[3]) >= 34375000) && (atofcxx . . $ atof(9b0361fcbc284e4f).(argv[3]) <=4400000000) ) || (atofcxx . . $ atof(9b0361fcbc284e4f).(argv[3]) == 0) ) { |
| 67 | TargetFreq = atofcxx . . $ atof(9b0361fcbc284e4f).(argv[3]); |
| 68 | } |
| 69 | else { |
| 70 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid Frequency: Valid values for [TARGET_FREQ] limited to set [34375000,4400000000], or 0 for power down." <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 71 | return 1; |
| 72 | }; |
| 73 | |
| 74 | if ( (argc == 5) ){ |
| 75 | if (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[4]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "0") { verbosity = 0; } |
| 76 | else if (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[4]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "1") { verbosity = 1; stdcxx . . $ std/::coutcxx . . $ std/cout..precisioncxx . . $ std/ios_base#precision(8ce0d1b217db7fd1).(10); } |
| 77 | else if (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[4]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "2") { verbosity = 2; stdcxx . . $ std/::coutcxx . . $ std/cout..precisioncxx . . $ std/ios_base#precision(8ce0d1b217db7fd1).(10); } |
| 78 | else { stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid output selection: 0 for verbose mode, 1 for driver"; return 1;}; |
| 79 | } |
| 80 | else { verbosity = 0; }; |
| 81 | |
| 82 | if ( (argc == 6) ){ |
| 83 | if (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[5]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "0") { lowspur = 0; } |
| 84 | else if (stdcxx . . $ std/::stringcxx . . $ std/string#(argv[4]) ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "1") { lowspur = 1; stdcxx . . $ std/::coutcxx . . $ std/cout..precisioncxx . . $ std/ios_base#precision(8ce0d1b217db7fd1).(10); } |
| 85 | else { stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid Noise mode selection: 0 for low spur, 1 for low noise"; return 1;}; |
| 86 | } |
| 87 | else { lowspur = 0; }; |
| 88 | |
| 89 | //Initialize |
| 90 | fsynth_tcxx . . $ fsynth_t# fsynthcxx . . $ fsynth_t#fsynth_t(49f6e7a06ebc5aa8).; |
| 91 | |
| 92 | //Get Target Frequency (in MHz). |
| 93 | if (verbosity == 1){ |
| 94 | stdcxx . . $ std/::coutcxx . . $ std/cout. |
| 95 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\nRequesting Desired Frequency (@ Refin = " <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). (float)RefIn/1e6 |
| 96 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "MHz) in MHz: " <<cxx . . $ std/basic_ostream#`operator<<`(d90e0e305b0e25c2). stdcxx . . $ std/::deccxx . . $ std/dec(cf36cc481190cd1f). <<cxx . . $ std/basic_ostream#`operator<<`(b09cd8ec4365c7ed). TargetFreq / 1000000 |
| 97 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ", using MOD=" <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). chan_mod |
| 98 | <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 99 | }; |
| 100 | //If target frequency is 0, power down VCO. |
| 101 | ActualFreq = (int(TargetFreq) == 0 ? fsynth.power_downcxx . . $ adf4351_regs_t#power_down. = fsynth_tcxx . . $ fsynth_t#::POWER_DOWN_ENABLEDcxx . . $ adf4351_regs_t#POWER_DOWN_ENABLED. : fsynth.set_freqcxx . . $ adf4351_regs_t#set_freq(a52d78fe2e46090b).(TargetFreq, RefIn, chan_mod, bool( int(verbosity) == 1 ? true : false ) ) ); |
| 102 | |
| 103 | fsynth.low_noise_and_spurcxx . . $ adf4351_regs_t#low_noise_and_spur. = ( int(lowspur) == 0 ? fsynth_tcxx . . $ fsynth_t#::LOW_NOISE_AND_SPUR_LOW_SPURcxx . . $ adf4351_regs_t#LOW_NOISE_AND_SPUR_LOW_SPUR. : fsynth_tcxx . . $ fsynth_t#::LOW_NOISE_AND_SPUR_LOW_NOISEcxx . . $ adf4351_regs_t#LOW_NOISE_AND_SPUR_LOW_NOISE. ); |
| 104 | |
| 105 | if (verbosity == 2) { |
| 106 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/basic_ostream#`operator<<`(968942d578224538). ActualFreq <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 107 | }; |
| 108 | |
| 109 | //Debug Information: |
| 110 | if (verbosity == 1){ |
| 111 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Programming Channel: " |
| 112 | <<cxx . . $ std/`operator<<`(88df5bef833f7f83). stdcxx . . $ std/::stringcxx . . $ std/string#( int(Channel) == 1 ? "rx" : "tx") |
| 113 | <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 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){ stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Register " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). i <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " => 0x"<<cxx . . $ std/basic_ostream#`operator<<`(d90e0e305b0e25c2). stdcxx . . $ std/::hexcxx . . $ std/hex(cf36cc481190cd1f). <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). fsynth.get_regcxx . . $ adf4351_regs_t#get_reg(6a0e611fa90938fe).(i) <<cxx . . $ std/basic_ostream#`operator<<`(d90e0e305b0e25c2). stdcxx . . $ std/::deccxx . . $ std/dec(cf36cc481190cd1f). <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; }; |
| 122 | rc |= WriteRegcxx . . $ WriteReg(e62d13286998b6de).(argvcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).[1], Channel, fsynth.get_regcxx . . $ adf4351_regs_t#get_reg(6a0e611fa90938fe).(i)); |
| 123 | }; |
| 124 | |
| 125 | //Compensate for the remaining frequency difference in DSP |
| 126 | rc |= SetDDCFreqcxx . . $ SetDDCFreq(20d847afbf6d1cd3).(argvcxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).[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 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Return code from io_ctl calls: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). rc <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 143 | }; |
| 144 | |
| 145 | return rc; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | int SetDDCFreqcxx . . $ SetDDCFreq(20d847afbf6d1cd3).(stdcxx . . $ std/::stringcxx . . $ 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_RXPhasecxx . . $ `langford_ioctl.h:98:9`!; |
| 159 | SET_RevFreq = IOCTL_SET_RXRevFreqcxx . . $ `langford_ioctl.h:108:9`!; |
| 160 | } else if (Channel == 2) { |
| 161 | SET_Phase = IOCTL_SET_TXPhasecxx . . $ `langford_ioctl.h:101:9`!; |
| 162 | SET_RevFreq = IOCTL_SET_TXRevFreqcxx . . $ `langford_ioctl.h:110:9`!; |
| 163 | } else { |
| 164 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid selection for channel" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 165 | return 1; |
| 166 | }; |
| 167 | |
| 168 | DevFD = open(FileName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!); |
| 169 | if (!DevFD) { |
| 170 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Cannot open " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). FileName <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 171 | return 1; |
| 172 | }; |
| 173 | |
| 174 | Phase = fabscxx . . $ fabs(fee4f1d22bc8e617).(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 WriteRegcxx . . $ WriteReg(e62d13286998b6de).(stdcxx . . $ std/::stringcxx . . $ std/string# FileName, int Channel, uint32_tcxx . . $ 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_N61CEcxx . . $ `langford_ioctl.h:28:9`!; |
| 194 | NDATA = IOCTL_SET_N61DATAcxx . . $ `langford_ioctl.h:30:9`!; |
| 195 | NCLK = IOCTL_SET_N61CLKcxx . . $ `langford_ioctl.h:32:9`!; |
| 196 | NLE = IOCTL_SET_N61LEcxx . . $ `langford_ioctl.h:34:9`!; |
| 197 | } else if (Channel == 2) { |
| 198 | NCE = IOCTL_SET_N62CEcxx . . $ `langford_ioctl.h:36:9`!; |
| 199 | NDATA = IOCTL_SET_N62DATAcxx . . $ `langford_ioctl.h:38:9`!; |
| 200 | NCLK = IOCTL_SET_N62CLKcxx . . $ `langford_ioctl.h:40:9`!; |
| 201 | NLE = IOCTL_SET_N62LEcxx . . $ `langford_ioctl.h:42:9`!; |
| 202 | } else { |
| 203 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid selection for channel" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 204 | return 1; |
| 205 | }; |
| 206 | |
| 207 | DevFD = open(FileName.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!); |
| 208 | if (!DevFD) { |
| 209 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Cannot open " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). FileName <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). stdcxx . . $ std/::endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 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 | }; |