| 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <fcntl.h> |
| 4 | #include <unistd.h> |
| 5 | #include <sys/ioctl.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <stdlib.h> |
| 9 | #include "driver1_ioctl.h" |
| 10 | |
| 11 | #include <boost/assign/list_of.hpp> |
| 12 | #include "dict.hpp" |
| 13 | #include "adf4350_regs.hpp" |
| 14 | #include "ranges.hpp" |
| 15 | |
| 16 | const float LO_Freq = 156.25e6; |
| 17 | const float Samp_Freq = 200e6; |
| 18 | |
| 19 | using namespace std; |
| 20 | using namespace boost::assign; |
| 21 | using namespace uhd; |
| 22 | |
| 23 | void WriteLOSerialReg(int &DevFD, int &rc, unsigned int Content) { |
| 24 | int i; |
| 25 | const unsigned long long val0 = 0, val1 = 1; |
| 26 | unsigned long long val; |
| 27 | |
| 28 | rc |= ioctl(DevFD, IOCTL_SET_LO_LE, &val0); |
| 29 | rc |= ioctl(DevFD, IOCTL_SET_LO_CE, &val1); |
| 30 | for (i = 0; i < 32; i++) { |
| 31 | rc |= ioctl(DevFD, IOCTL_SET_LO_CLK, &val0); |
| 32 | val = (Content >> (31 - i)) & 0x00000001; |
| 33 | rc |= ioctl(DevFD, IOCTL_SET_LO_DATA, &val); |
| 34 | rc |= ioctl(DevFD, IOCTL_SET_LO_CLK, &val1); |
| 35 | }; |
| 36 | rc |= ioctl(DevFD, IOCTL_SET_LO_CLK, &val0); |
| 37 | rc |= ioctl(DevFD, IOCTL_SET_LO_LE, &val1); |
| 38 | rc |= ioctl(DevFD, IOCTL_SET_LO_LE, &val0); |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | double SetLOFreq(int &DevFD, int rc, double TargetFreq) { |
| 43 | cout << boost::format( |
| 44 | "Local oscillator: target frequency %f Mhz" |
| 45 | ) % (TargetFreq/1e6) << std::endl; |
| 46 | |
| 47 | //map prescaler setting to mininmum integer divider (N) values (pg.18 prescaler) |
| 48 | static const uhd::dict<int, int> prescaler_to_min_int_div = map_list_of |
| 49 | (0,23) //adf4350_regs_t::PRESCALER_4_5 |
| 50 | (1,75) //adf4350_regs_t::PRESCALER_8_9 |
| 51 | ; |
| 52 | |
| 53 | //map rf divider select output dividers to enums |
| 54 | static const uhd::dict<int, adf4350_regs_t::rf_divider_select_t> rfdivsel_to_enum = map_list_of |
| 55 | (1, adf4350_regs_t::RF_DIVIDER_SELECT_DIV1) |
| 56 | (2, adf4350_regs_t::RF_DIVIDER_SELECT_DIV2) |
| 57 | (4, adf4350_regs_t::RF_DIVIDER_SELECT_DIV4) |
| 58 | (8, adf4350_regs_t::RF_DIVIDER_SELECT_DIV8) |
| 59 | (16, adf4350_regs_t::RF_DIVIDER_SELECT_DIV16) |
| 60 | (32, adf4350_regs_t::RF_DIVIDER_SELECT_DIV32) |
| 61 | (64, adf4350_regs_t::RF_DIVIDER_SELECT_DIV64) |
| 62 | ; |
| 63 | |
| 64 | double actual_freq, pfd_freq; |
| 65 | //************************************************************************** |
| 66 | //Set reference oscillator frequency here |
| 67 | //************************************************************************** |
| 68 | double ref_freq = LO_Freq;//this->get_iface()->get_clock_rate(unit); |
| 69 | int R=0, BS=0, N=0, FRAC=0, MOD=0; |
| 70 | int RFdiv = 1; |
| 71 | adf4350_regs_t::reference_divide_by_2_t T = adf4350_regs_t::REFERENCE_DIVIDE_BY_2_DISABLED; |
| 72 | adf4350_regs_t::reference_doubler_t D = adf4350_regs_t::REFERENCE_DOUBLER_DISABLED; |
| 73 | |
| 74 | //Reference doubler for 50% duty cycle |
| 75 | // if ref_freq < 12.5MHz enable regs.reference_divide_by_2 |
| 76 | if(ref_freq <= 12.5e6) D = adf4350_regs_t::REFERENCE_DOUBLER_ENABLED; |
| 77 | |
| 78 | //increase RF divider until acceptable VCO frequency |
| 79 | //start with TargetFreq*2 because mixer has divide by 2 |
| 80 | double vco_freq = TargetFreq; |
| 81 | while (vco_freq < 2.2e9) { |
| 82 | vco_freq *= 2; |
| 83 | RFdiv *= 2; |
| 84 | } |
| 85 | |
| 86 | //use 8/9 prescaler for vco_freq > 3 GHz (pg.18 prescaler) |
| 87 | adf4350_regs_t::prescaler_t prescaler = vco_freq > 3e9 ? adf4350_regs_t::PRESCALER_8_9 : adf4350_regs_t::PRESCALER_4_5; |
| 88 | |
| 89 | /* |
| 90 | * The goal here is to loop though possible R dividers, |
| 91 | * band select clock dividers, N (int) dividers, and FRAC |
| 92 | * (frac) dividers. |
| 93 | * |
| 94 | * Calculate the N and F dividers for each set of values. |
| 95 | * The loop exists when it meets all of the constraints. |
| 96 | * The resulting loop values are loaded into the registers. |
| 97 | * |
| 98 | * from pg.21 |
| 99 | * |
| 100 | * f_pfd = f_ref*(1+D)/(R*(1+T)) |
| 101 | * f_vco = (N + (FRAC/MOD))*f_pfd |
| 102 | * N = f_vco/f_pfd - FRAC/MOD = f_vco*((R*(T+1))/(f_ref*(1+D))) - FRAC/MOD |
| 103 | * f_rf = f_vco/RFdiv) |
| 104 | * f_actual = f_rf/2 |
| 105 | */ |
| 106 | for(R = 1; R <= 1023; R+=1){ |
| 107 | //PFD input frequency = f_ref/R ... ignoring Reference doubler/divide-by-2 (D & T) |
| 108 | pfd_freq = ref_freq*(1+D)/(R*(1+T)); |
| 109 | |
| 110 | //keep the PFD frequency at or below 25MHz (Loop Filter Bandwidth) |
| 111 | if (pfd_freq > 31.25e6) continue; |
| 112 | |
| 113 | //ignore fractional part of tuning |
| 114 | N = int(std::floor(vco_freq/pfd_freq)); |
| 115 | |
| 116 | //keep N > minimum int divider requirement |
| 117 | if (N < prescaler_to_min_int_div[prescaler]) continue; |
| 118 | |
| 119 | for(BS=1; BS <= 255; BS+=1){ |
| 120 | //keep the band select frequency at or below 125KHz |
| 121 | //constraint on band select clock |
| 122 | if (pfd_freq/BS > 125e3) continue; |
| 123 | goto done_loop; |
| 124 | } |
| 125 | } done_loop: |
| 126 | |
| 127 | //Fractional-N calculation |
| 128 | MOD = 4095; //max fractional accuracy |
| 129 | FRAC = int((vco_freq/pfd_freq - N)*MOD); |
| 130 | |
| 131 | //Reference divide-by-2 for 50% duty cycle |
| 132 | // if R even, move one divide by 2 to to regs.reference_divide_by_2 |
| 133 | if(R % 2 == 0){ |
| 134 | T = adf4350_regs_t::REFERENCE_DIVIDE_BY_2_ENABLED; |
| 135 | R /= 2; |
| 136 | } |
| 137 | |
| 138 | //actual frequency calculation |
| 139 | actual_freq = double((N + (double(FRAC)/double(MOD)))*ref_freq*(1+int(D))/(R*(1+int(T)))/RFdiv); |
| 140 | |
| 141 | |
| 142 | cout |
| 143 | << boost::format("Local oscillator: ref=%0.2f, outdiv=%f, fbdiv=%f") % (ref_freq*(1+int(D))/(R*(1+int(T)))) % double(RFdiv*2) % double(N + double(FRAC)/double(MOD)) << std::endl |
| 144 | |
| 145 | << boost::format("Local oscillator: R=%d, BS=%d, N=%d, FRAC=%d, MOD=%d, T=%d, D=%d, RFdiv=%d" |
| 146 | ) % R % BS % N % FRAC % MOD % T % D % RFdiv << std::endl |
| 147 | << boost::format("Local oscillator (MHz): REQ=%0.2f, ACT=%0.2f, VCO=%0.2f, PFD=%0.2f, BAND=%0.2f" |
| 148 | ) % (TargetFreq/1e6) % (actual_freq/1e6) % (vco_freq/1e6) % (pfd_freq/1e6) % (pfd_freq/BS/1e6) << std::endl; |
| 149 | |
| 150 | //load the register values |
| 151 | adf4350_regs_t regs; |
| 152 | |
| 153 | regs.frac_12_bit = FRAC; |
| 154 | regs.int_16_bit = N; |
| 155 | regs.mod_12_bit = MOD; |
| 156 | regs.prescaler = prescaler; |
| 157 | regs.r_counter_10_bit = R; |
| 158 | regs.reference_divide_by_2 = T; |
| 159 | regs.reference_doubler = D; |
| 160 | regs.band_select_clock_div = BS; |
| 161 | UHD_ASSERT_THROW(rfdivsel_to_enum.has_key(RFdiv)); |
| 162 | regs.rf_divider_select = rfdivsel_to_enum[RFdiv]; |
| 163 | regs.aux_output_power = adf4350_regs_t::AUX_OUTPUT_POWER_M45DBM; |
| 164 | regs.aux_output_enable = adf4350_regs_t::AUX_OUTPUT_ENABLE_DISABLED; |
| 165 | regs.aux_output_select = adf4350_regs_t::AUX_OUTPUT_SELECT_DIVIDED; |
| 166 | |
| 167 | // if (unit == dboard_iface::UNIT_RX) { |
| 168 | freq_range_t rx_lo_5dbm = list_of |
| 169 | (range_t(0.05e9, 1.4e9)) |
| 170 | ; |
| 171 | |
| 172 | freq_range_t rx_lo_2dbm = list_of |
| 173 | (range_t(1.4e9, 2.2e9)) |
| 174 | ; |
| 175 | |
| 176 | if (actual_freq == rx_lo_5dbm.clip(actual_freq)) regs.output_power = adf4350_regs_t::OUTPUT_POWER_5DBM; |
| 177 | |
| 178 | if (actual_freq == rx_lo_2dbm.clip(actual_freq)) regs.output_power = adf4350_regs_t::OUTPUT_POWER_2DBM; |
| 179 | |
| 180 | /* } else if (unit == dboard_iface::UNIT_TX) { |
| 181 | freq_range_t tx_lo_5dbm = list_of |
| 182 | (range_t(0.05e9, 1.7e9)) |
| 183 | (range_t(1.9e9, 2.2e9)) |
| 184 | ; |
| 185 | |
| 186 | freq_range_t tx_lo_m1dbm = list_of |
| 187 | (range_t(1.7e9, 1.9e9)) |
| 188 | ; |
| 189 | |
| 190 | if (actual_freq == tx_lo_5dbm.clip(actual_freq)) regs.output_power = adf4350_regs_t::OUTPUT_POWER_5DBM; |
| 191 | |
| 192 | if (actual_freq == tx_lo_m1dbm.clip(actual_freq)) regs.output_power = adf4350_regs_t::OUTPUT_POWER_M1DBM; |
| 193 | |
| 194 | } |
| 195 | */ |
| 196 | //write the registers |
| 197 | //correct power-up sequence to write registers (5, 4, 3, 2, 1, 0) |
| 198 | int addr; |
| 199 | |
| 200 | for(addr=5; addr>=0; addr--){ |
| 201 | cout << boost::format( |
| 202 | "Local oscillator: (0x%02x): 0x%08x" |
| 203 | ) % addr % regs.get_reg(addr) << std::endl; |
| 204 | /* |
| 205 | this->get_iface()->write_spi( |
| 206 | unit, spi_config_t::EDGE_RISE, |
| 207 | regs.get_reg(addr), 32 |
| 208 | ); |
| 209 | */ |
| 210 | WriteLOSerialReg(DevFD, rc, regs.get_reg(addr)); |
| 211 | } |
| 212 | |
| 213 | //return the actual frequency |
| 214 | cout << boost::format( |
| 215 | "Local oscillator: actual frequency %f Mhz" |
| 216 | ) % (actual_freq/1e6) << std::endl; |
| 217 | return actual_freq; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | int main(int argc, char **argv) { |
| 222 | string cmd_str = ""; |
| 223 | unsigned long long val, ulltmp; |
| 224 | int DevFD; |
| 225 | int rc = 0; |
| 226 | unsigned int tmp; |
| 227 | |
| 228 | if (argc != 4) { |
| 229 | cerr << argv[0] << " device_name -x val" << endl; |
| 230 | return 1; |
| 231 | }; |
| 232 | |
| 233 | DevFD = open(argv[1], O_RDWR); |
| 234 | if (!DevFD) { |
| 235 | cerr << "Cannot open " << argv[1] << endl; |
| 236 | return 1; |
| 237 | }; |
| 238 | |
| 239 | cmd_str = argv[2]; |
| 240 | val = strtoul(argv[3], 0, 0); |
| 241 | cout << "Got command: " << cmd_str << endl; |
| 242 | cout << "Got value: " << val << " = 0x" << std::hex << val << std::dec << endl; |
| 243 | |
| 244 | if (cmd_str == "-g") { |
| 245 | //Gain |
| 246 | rc |= ioctl(DevFD, IOCTL_GET_RFDDCEN, &ulltmp); |
| 247 | if (ulltmp) { |
| 248 | cout << "RF stage enabled, setting RF gain" << endl; |
| 249 | rc |= ioctl(DevFD, IOCTL_SET_AT0, &val); |
| 250 | rc |= ioctl(DevFD, IOCTL_SET_AT1, &val); |
| 251 | rc |= ioctl(DevFD, IOCTL_SET_AT2, &val); |
| 252 | rc |= ioctl(DevFD, IOCTL_SET_AT3, &val); |
| 253 | } else { |
| 254 | cout << "RF stage disabled, setting DC gain" << endl; |
| 255 | rc |= ioctl(DevFD, IOCTL_SET_DCGAIN, &val); |
| 256 | }; |
| 257 | } else if (cmd_str == "-c") { |
| 258 | //DC HILO |
| 259 | rc |= ioctl(DevFD, IOCTL_SET_DCHILO, &val); |
| 260 | } else if (cmd_str == "-m") { |
| 261 | //Enable DC stage (val = 0 is base band, val = 1 is RF) |
| 262 | //Disable gain stages appropriately |
| 263 | if (val) { |
| 264 | //RF stage |
| 265 | val = 0; |
| 266 | rc |= ioctl(DevFD, IOCTL_SET_DCGAIN, &val); |
| 267 | rc |= ioctl(DevFD, IOCTL_SET_DCEN, &val); |
| 268 | rc |= ioctl(DevFD, IOCTL_SET_DCHILO, &val); |
| 269 | val = 1; |
| 270 | } else { |
| 271 | //Direct sampling |
| 272 | rc |= ioctl(DevFD, IOCTL_SET_AT0, &val); |
| 273 | rc |= ioctl(DevFD, IOCTL_SET_AT1, &val); |
| 274 | rc |= ioctl(DevFD, IOCTL_SET_AT2, &val); |
| 275 | rc |= ioctl(DevFD, IOCTL_SET_AT3, &val); |
| 276 | }; |
| 277 | //The following pins are set to 1 when RF IQ sampling is enabled |
| 278 | rc |= ioctl(DevFD, IOCTL_SET_RFDDCEN, &val); |
| 279 | //The following pins are set to 1 when base band direct sampling is enabled |
| 280 | val = !val; |
| 281 | rc |= ioctl(DevFD, IOCTL_SET_BBSB, &val); |
| 282 | rc |= ioctl(DevFD, IOCTL_SET_RFSB, &val); |
| 283 | rc |= ioctl(DevFD, IOCTL_SET_DCEN, &val); |
| 284 | } else if (cmd_str == "-d") { |
| 285 | //Decimation |
| 286 | if (val <= 1) { |
| 287 | cerr << "Decimation factor must be larger than 1" << endl; |
| 288 | return 1; |
| 289 | } else if (val <= 2) { |
| 290 | tmp = 0; |
| 291 | } else if (val <= 4) { |
| 292 | tmp = 1; |
| 293 | } else if (val <= 8) { |
| 294 | tmp = 3; |
| 295 | } else if (val <= 16) { |
| 296 | tmp = 7; |
| 297 | } else if (val <= 32) { |
| 298 | tmp = 15; |
| 299 | } else if (val <= 64) { |
| 300 | tmp = 31; |
| 301 | } else if (val <= 128) { |
| 302 | tmp = 63; |
| 303 | }; |
| 304 | cout << "Effective sampling rate after decimation is " << Samp_Freq / (tmp + 1) / 2e6 << "Msps" << endl; |
| 305 | val = tmp; |
| 306 | rc |= ioctl(DevFD, IOCTL_SET_DECEN, &val); |
| 307 | } else if (cmd_str == "-f") { |
| 308 | rc |= ioctl(DevFD, IOCTL_GET_RFDDCEN, &ulltmp); |
| 309 | if (ulltmp) { |
| 310 | //RF DDC enabled, set RF LO freq |
| 311 | cout << "RF stage enabled, setting RF LO frequency first" << endl; |
| 312 | if (val < LO_Freq * 5.5) { |
| 313 | //Cannot get LO frequency low enough, we will need to use the DDC more |
| 314 | if (LO_Freq * 5.5 - val > Samp_Freq / 4) { |
| 315 | //Difference in frequency is too large: DDC cannot make up for the difference |
| 316 | cout << "Frequency is too low with RF stage enabled. Lowest frequency possible with RF stage enabled is " << LO_Freq * 5.5 - Samp_Freq / 4 << "Hz" << endl; |
| 317 | } |
| 318 | SetLOFreq(DevFD, rc, LO_Freq * 5.5); |
| 319 | val = LO_Freq * 5.5 - val; |
| 320 | } else { |
| 321 | //Set the LO frequency as close as possible |
| 322 | val = round(val - SetLOFreq(DevFD, rc, val)); |
| 323 | }; |
| 324 | }; |
| 325 | cout << "Setting DDC frequency to " << val << endl; |
| 326 | tmp = ((float) val) * (((int64_t) 1) << 32) / (Samp_Freq / 2); |
| 327 | cout << "Phase inc: " << tmp << " = 0x" << std::hex << tmp << std::dec << endl; |
| 328 | cout << "Actual frequency: " << (Samp_Freq / 2 * tmp / (((int64_t) 1) << 32)) << endl; |
| 329 | val = tmp; |
| 330 | rc |= ioctl(DevFD, IOCTL_SET_PHASEINC, &val); |
| 331 | val = (ulltmp ? 0 : 1); |
| 332 | rc |= ioctl(DevFD, IOCTL_GET_REVDDCF, &val); |
| 333 | } else if (cmd_str == "-l") { |
| 334 | //Write raw LO registers |
| 335 | WriteLOSerialReg(DevFD, rc, val); |
| 336 | } else if (cmd_str == "-s") { |
| 337 | //Print status |
| 338 | rc |= ioctl(DevFD, IOCTL_GET_LO_MUXOUT, &val); |
| 339 | cout << "LO mux out = " << val << endl; |
| 340 | rc |= ioctl(DevFD, IOCTL_GET_LO_LD, &val); |
| 341 | cout << "LO PLL lock detect = " << val << endl; |
| 342 | } else { |
| 343 | cerr << "Unknown command: " << cmd_str << endl; |
| 344 | return 1; |
| 345 | }; |
| 346 | |
| 347 | close(DevFD); |
| 348 | |
| 349 | cout << "Return code from value change: " << rc << endl; |
| 350 | |
| 351 | return 0; |
| 352 | }; |