2 // Copyright 2008,2009 Free Software Foundation, Inc.
4 // This file is part of GNU Radio
6 // GNU Radio is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either asversion 3, or (at your option)
11 // GNU Radio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GNU Radio; see the file COPYING. If not, write to
18 // the Free Software Foundation, Inc., 51 Franklin Street,
19 // Boston, MA 02110-1301, USA.
25 #include <usrp/db_wbxng.h>
26 #include "db_wbxng_adf4350.h"
27 #include <db_base_impl.h>
30 // d'board i/o pin defs
31 // Tx and Rx have shared defs, but different i/o regs
32 #define ENABLE_5 (1 << 7) // enables 5.0V power supply
33 #define ENABLE_33 (1 << 6) // enables 3.3V supply
34 //#define RX_TXN (1 << 15) // Tx only: T/R antenna switch for TX/RX port
35 //#define RX2_RX1N (1 << 15) // Rx only: antenna switch between RX2 and TX/RX port
36 #define RX_TXN ((1 << 5)|(1 << 15)) // Tx only: T/R antenna switch for TX/RX port
37 #define RX2_RX1N ((1 << 5)|(1 << 15)) // Rx only: antenna switch between RX2 and TX/RX port
38 #define RXBB_EN (1 << 4)
39 #define TXMOD_EN (1 << 4)
40 #define PLL_CE (1 << 3)
41 #define PLL_PDBRF (1 << 2)
42 #define PLL_MUXOUT (1 << 1)
43 #define PLL_LOCK_DETECT (1 << 0)
45 // RX Attenuator constants
47 #define ATTN_MASK (63 << ATTN_SHIFT)
49 wbxng_base::wbxng_base(usrp_basic_sptr _usrp, int which, int _power_on)
50 : db_base(_usrp, which), d_power_on(_power_on)
53 @param usrp: instance of usrp.source_c
54 @param which: which side: 0 or 1 corresponding to side A or B respectively
58 usrp()->_write_oe(d_which, 0, 0xffff); // turn off all outputs
61 d_spi_format = SPI_FMT_MSB | SPI_FMT_HDR_0;
63 _enable_refclk(false); // disable refclk
68 wbxng_base::~wbxng_base()
75 wbxng_base::_refclk_divisor()
81 wbxng_base::set_freq(double freq)
84 @returns (ok, actual_baseband_freq) where:
85 ok is True or False and indicates success or failure,
86 actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
90 freq_t int_freq = freq_t(std::max(freq_min(), std::min(freq, freq_max())));
92 bool ok = d_common->_set_freq(int_freq*2, _refclk_freq());
93 double freq_result = (double) d_common->_get_freq(_refclk_freq())/2.0;
94 struct freq_result_t args = {ok, freq_result};
96 /* Wait before reading Lock Detect*/
102 //fprintf(stderr,"Setting WBXNG frequency, requested %d, obtained %f, lock_detect %d\n",
103 // int_freq, freq_result, d_common->_get_locked());
106 // Offsetting the LO helps get the Tx carrier leakage out of the way.
107 // This also ensures that on Rx, we're not getting hosed by the
108 // FPGA's DC removal loop's time constant. We were seeing a
109 // problem when running with discontinuous transmission.
110 // Offsetting the LO made the problem go away.
111 //freq += d_lo_offset;
117 wbxng_base::_set_pga(float pga_gain)
120 usrp()->set_pga(0, pga_gain);
121 usrp()->set_pga(1, pga_gain);
124 usrp()->set_pga(2, pga_gain);
125 usrp()->set_pga(3, pga_gain);
131 wbxng_base::is_quadrature()
134 Return True if this board requires both I & Q analog channels.
136 This bit of info is useful when setting up the USRP Rx mux register.
142 wbxng_base::freq_min()
144 return (double) d_common->_get_min_freq()/2.0;
148 wbxng_base::freq_max()
150 return (double) d_common->_get_max_freq()/2.0;
153 // ----------------------------------------------------------------
155 wbxng_base_tx::wbxng_base_tx(usrp_basic_sptr _usrp, int which, int _power_on)
156 : wbxng_base(_usrp, which, _power_on)
159 @param usrp: instance of usrp.sink_c
160 @param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
164 d_spi_enable = SPI_ENABLE_TX_A;
167 d_spi_enable = SPI_ENABLE_TX_B;
170 d_common = new adf4350(_usrp, d_which, d_spi_enable);
172 // power up the transmit side, but don't enable the mixer
173 usrp()->_write_oe(d_which,(RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5), (RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5));
174 usrp()->write_io(d_which, (power_on()|RX_TXN|ENABLE_33|ENABLE_5), (RX_TXN|ENABLE_33|ENABLE_5));
175 //set_lo_offset(4e6);
178 d_common->_enable(true);
180 set_gain((gain_min() + gain_max()) / 2.0); // initialize gain
183 wbxng_base_tx::~wbxng_base_tx()
190 wbxng_base_tx::shutdown()
192 // fprintf(stderr, "wbxng_base_tx::shutdown d_is_shutdown = %d\n", d_is_shutdown);
195 d_is_shutdown = true;
196 // do whatever there is to do to shutdown
199 d_common->_enable(false);
201 // Power down and leave the T/R switch in the R position
202 usrp()->write_io(d_which, (power_off()|RX_TXN), (RX_TXN|ENABLE_33|ENABLE_5));
206 _write_control(_compute_control_reg());
208 _enable_refclk(false); // turn off refclk
214 wbxng_base_tx::set_auto_tr(bool on)
218 ok &= set_atr_mask (RX_TXN | TXMOD_EN);
219 ok &= set_atr_txval(0 | TXMOD_EN);
220 ok &= set_atr_rxval(RX_TXN);
223 ok &= set_atr_mask (0);
224 ok &= set_atr_txval(0);
225 ok &= set_atr_rxval(0);
231 wbxng_base_tx::set_enable(bool on)
234 Enable transmitter if on is true
238 int mask = RX_TXN | TXMOD_EN;
242 //d_common->_enable(true);
247 //d_common->_enable(false);
249 return usrp()->write_io(d_which, v, mask);
253 wbxng_base_tx::gain_min()
259 wbxng_base_tx::gain_max()
265 wbxng_base_tx::gain_db_per_step()
267 return gain_max()/(1+(1.4-0.5)*4096/3.3);
271 wbxng_base_tx::set_gain(float gain)
276 @param gain: gain in decibels
281 gain = std::max(gain_min(), std::min(gain, gain_max()));
283 float pga_gain, agc_gain;
284 float V_maxgain, V_mingain, V_fullscale, dac_value;
286 float maxgain = gain_max();
287 float mingain = gain_min();
294 dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale;
296 //fprintf(stderr, "TXGAIN: %f dB, Dac Code: %d, Voltage: %f\n", gain, int(dac_value), float((dac_value/4096.0)*V_fullscale));
297 assert(dac_value>=0 && dac_value<4096);
299 return (usrp()->write_aux_dac(d_which, 0, int(dac_value))
300 && _set_pga(usrp()->pga_max()));
305 /**************************************************************************/
308 wbxng_base_rx::wbxng_base_rx(usrp_basic_sptr _usrp, int which, int _power_on)
309 : wbxng_base(_usrp, which, _power_on)
312 @param usrp: instance of usrp.source_c
313 @param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
317 d_spi_enable = SPI_ENABLE_RX_A;
320 d_spi_enable = SPI_ENABLE_RX_B;
323 d_common = new adf4350(_usrp, d_which, d_spi_enable);
326 d_common->_enable(true);
328 usrp()->_write_oe(d_which, (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
329 usrp()->write_io(d_which, (power_on()|RX2_RX1N|RXBB_EN|ENABLE_33|ENABLE_5), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
330 //fprintf(stderr,"Setting WBXNG RXBB on");
332 // set up for RX on TX/RX port
333 select_rx_antenna("TX/RX");
335 bypass_adc_buffers(true);
342 wbxng_base_rx::~wbxng_base_rx()
348 wbxng_base_rx::shutdown()
350 // fprintf(stderr, "wbxng_base_rx::shutdown d_is_shutdown = %d\n", d_is_shutdown);
353 d_is_shutdown = true;
354 // do whatever there is to do to shutdown
356 // Power down VCO/PLL
357 d_common->_enable(false);
359 // fprintf(stderr, "wbxng_base_rx::shutdown before _write_control\n");
360 //_write_control(_compute_control_reg());
362 // fprintf(stderr, "wbxng_base_rx::shutdown before _enable_refclk\n");
363 _enable_refclk(false); // turn off refclk
365 // fprintf(stderr, "wbxng_base_rx::shutdown before set_auto_tr\n");
369 usrp()->write_io(d_which, power_off(), (RX2_RX1N|RXBB_EN|ATTN_MASK|ENABLE_33|ENABLE_5));
371 // fprintf(stderr, "wbxng_base_rx::shutdown after set_auto_tr\n");
376 wbxng_base_rx::set_auto_tr(bool on)
380 ok &= set_atr_mask (RXBB_EN|RX2_RX1N);
381 ok &= set_atr_txval( 0|RX2_RX1N);
382 ok &= set_atr_rxval(RXBB_EN| 0);
385 ok &= set_atr_mask (0);
386 ok &= set_atr_txval(0);
387 ok &= set_atr_rxval(0);
393 wbxng_base_rx::select_rx_antenna(int which_antenna)
396 Specify which antenna port to use for reception.
397 @param which_antenna: either 'TX/RX' or 'RX2'
400 if(which_antenna == 0) {
401 usrp()->write_io(d_which, 0,RX2_RX1N);
403 else if(which_antenna == 1) {
404 usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
413 wbxng_base_rx::select_rx_antenna(const std::string &which_antenna)
416 Specify which antenna port to use for reception.
417 @param which_antenna: either 'TX/RX' or 'RX2'
421 if(which_antenna == "TX/RX") {
422 usrp()->write_io(d_which, 0, RX2_RX1N);
424 else if(which_antenna == "RX2") {
425 usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
435 wbxng_base_rx::set_gain(float gain)
440 @param gain: gain in decibels
445 gain = std::max(gain_min(), std::min(gain, gain_max()));
447 float pga_gain, agc_gain;
449 float maxgain = gain_max() - usrp()->pga_max();
450 float mingain = gain_min();
452 pga_gain = gain-maxgain;
453 assert(pga_gain <= usrp()->pga_max());
461 return _set_attn(maxgain-agc_gain) && _set_pga(int(pga_gain));
465 wbxng_base_rx::_set_attn(float attn)
467 int attn_code = int(floor(attn/0.5));
468 unsigned int iobits = (~attn_code) << ATTN_SHIFT;
469 //fprintf(stderr, "Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x \n", attn, attn_code, iobits & ATTN_MASK, ATTN_MASK);
470 return usrp()->write_io(d_which, iobits, ATTN_MASK);
473 // ----------------------------------------------------------------
475 db_wbxng_tx::db_wbxng_tx(usrp_basic_sptr usrp, int which)
476 : wbxng_base_tx(usrp, which)
480 db_wbxng_tx::~db_wbxng_tx()
484 db_wbxng_rx::db_wbxng_rx(usrp_basic_sptr usrp, int which)
485 : wbxng_base_rx(usrp, which)
487 set_gain((gain_min() + gain_max()) / 2.0); // initialize gain
490 db_wbxng_rx::~db_wbxng_rx()
495 db_wbxng_rx::gain_min()
497 return usrp()->pga_min();
501 db_wbxng_rx::gain_max()
503 return usrp()->pga_max()+30.5;
507 db_wbxng_rx::gain_db_per_step()
514 db_wbxng_rx::i_and_q_swapped()