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 << 5) // Tx only: T/R antenna switch for TX/RX port
35 #define RX2_RX1N (1 << 5) // Rx only: antenna switch between RX2 and TX/RX port
36 #define RXBB_EN (1 << 4)
37 #define TXMOD_EN (1 << 4)
38 #define PLL_CE (1 << 3)
39 #define PLL_PDBRF (1 << 2)
40 #define PLL_MUXOUT (1 << 1)
41 #define PLL_LOCK_DETECT (1 << 0)
43 // RX Attenuator constants
45 #define ATTN_MASK (63 << ATTN_SHIFT)
47 wbxng_base::wbxng_base(usrp_basic_sptr _usrp, int which, int _power_on)
48 : db_base(_usrp, which), d_power_on(_power_on)
51 @param usrp: instance of usrp.source_c
52 @param which: which side: 0 or 1 corresponding to side A or B respectively
56 usrp()->_write_oe(d_which, 0, 0xffff); // turn off all outputs
59 d_spi_format = SPI_FMT_MSB | SPI_FMT_HDR_0;
61 _enable_refclk(false); // disable refclk
66 wbxng_base::~wbxng_base()
73 wbxng_base::set_freq(double freq)
76 @returns (ok, actual_baseband_freq) where:
77 ok is True or False and indicates success or failure,
78 actual_baseband_freq is the RF frequency that corresponds to DC in the IF.
81 freq_t int_freq = freq_t(freq);
82 bool ok = d_common->_set_freq(int_freq*2);
83 double freq_result = (double) d_common->_get_freq()/2.0;
84 struct freq_result_t args = {ok, freq_result};
86 /* Wait before reading Lock Detect*/
92 fprintf(stderr,"Setting WBXNG frequency, requested %d, obtained %f, lock_detect %d\n",
93 int_freq, freq_result, d_common->_get_locked());
96 // Offsetting the LO helps get the Tx carrier leakage out of the way.
97 // This also ensures that on Rx, we're not getting hosed by the
98 // FPGA's DC removal loop's time constant. We were seeing a
99 // problem when running with discontinuous transmission.
100 // Offsetting the LO made the problem go away.
101 //freq += d_lo_offset;
107 wbxng_base::_set_pga(float pga_gain)
110 usrp()->set_pga(0, pga_gain);
111 usrp()->set_pga(1, pga_gain);
114 usrp()->set_pga(2, pga_gain);
115 usrp()->set_pga(3, pga_gain);
121 wbxng_base::is_quadrature()
124 Return True if this board requires both I & Q analog channels.
126 This bit of info is useful when setting up the USRP Rx mux register.
132 wbxng_base::freq_min()
134 return (double) d_common->_get_min_freq()/2.0;
138 wbxng_base::freq_max()
140 return (double) d_common->_get_max_freq()/2.0;
143 // ----------------------------------------------------------------
145 wbxng_base_tx::wbxng_base_tx(usrp_basic_sptr _usrp, int which, int _power_on)
146 : wbxng_base(_usrp, which, _power_on)
149 @param usrp: instance of usrp.sink_c
150 @param which: 0 or 1 corresponding to side TX_A or TX_B respectively.
154 d_spi_enable = SPI_ENABLE_TX_A;
157 d_spi_enable = SPI_ENABLE_TX_B;
160 d_common = new adf4350(_usrp, d_which, d_spi_enable);
162 // power up the transmit side, but don't enable the mixer
163 usrp()->_write_oe(d_which,(RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5), (RX_TXN|TXMOD_EN|ENABLE_33|ENABLE_5));
164 usrp()->write_io(d_which, (power_on()|RX_TXN|ENABLE_33|ENABLE_5), (RX_TXN|ENABLE_33|ENABLE_5));
165 //set_lo_offset(4e6);
168 d_common->_enable(false);
170 set_gain((gain_min() + gain_max()) / 2.0); // initialize gain
173 wbxng_base_tx::~wbxng_base_tx()
180 wbxng_base_tx::shutdown()
182 // fprintf(stderr, "wbxng_base_tx::shutdown d_is_shutdown = %d\n", d_is_shutdown);
185 d_is_shutdown = true;
186 // do whatever there is to do to shutdown
189 d_common->_enable(false);
191 // Power down and leave the T/R switch in the R position
192 usrp()->write_io(d_which, (power_off()|RX_TXN), (RX_TXN|ENABLE_33|ENABLE_5));
196 _write_control(_compute_control_reg());
198 _enable_refclk(false); // turn off refclk
204 wbxng_base_tx::set_auto_tr(bool on)
208 ok &= set_atr_mask (RX_TXN | TXMOD_EN);
209 ok &= set_atr_txval(0 | TXMOD_EN);
210 ok &= set_atr_rxval(RX_TXN);
213 ok &= set_atr_mask (0);
214 ok &= set_atr_txval(0);
215 ok &= set_atr_rxval(0);
221 wbxng_base_tx::set_enable(bool on)
224 Enable transmitter if on is true
228 int mask = RX_TXN | TXMOD_EN;
232 d_common->_enable(true);
237 d_common->_enable(false);
239 return usrp()->write_io(d_which, v, mask);
243 wbxng_base_tx::gain_min()
245 return usrp()->pga_max();
249 wbxng_base_tx::gain_max()
251 return usrp()->pga_max() + 25.0;
255 wbxng_base_tx::gain_db_per_step()
261 wbxng_base_tx::set_gain(float gain)
266 @param gain: gain in decibels
271 gain = std::max(gain_min(), std::min(gain, gain_max()));
273 float pga_gain, agc_gain;
274 float V_maxgain, V_mingain, V_fullscale, dac_value;
276 float maxgain = gain_max() - usrp()->pga_max();
277 float mingain = gain_min();
279 pga_gain = gain-maxgain;
280 assert(pga_gain <= usrp()->pga_max());
291 dac_value = (agc_gain*(V_maxgain-V_mingain)/(maxgain-mingain) + V_mingain)*4096/V_fullscale;
293 fprintf(stderr, "TXGAIN: %f dB, Dac Code: %d, Voltage: %f\n", gain, int(dac_value), float((dac_value/4096.0)*V_fullscale));
294 assert(dac_value>=0 && dac_value<4096);
296 return (usrp()->write_aux_dac(d_which, 0, int(dac_value))
297 && _set_pga(int(pga_gain)));
301 /**************************************************************************/
304 wbxng_base_rx::wbxng_base_rx(usrp_basic_sptr _usrp, int which, int _power_on)
305 : wbxng_base(_usrp, which, _power_on)
308 @param usrp: instance of usrp.source_c
309 @param which: 0 or 1 corresponding to side RX_A or RX_B respectively.
313 d_spi_enable = SPI_ENABLE_RX_A;
316 d_spi_enable = SPI_ENABLE_RX_B;
319 d_common = new adf4350(_usrp, d_which, d_spi_enable);
322 d_common->_enable(true);
324 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));
325 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));
326 fprintf(stderr,"Setting WBXNG RXBB on");
328 // set up for RX on TX/RX port
329 select_rx_antenna("TX/RX");
331 bypass_adc_buffers(true);
338 wbxng_base_rx::~wbxng_base_rx()
344 wbxng_base_rx::shutdown()
346 // fprintf(stderr, "wbxng_base_rx::shutdown d_is_shutdown = %d\n", d_is_shutdown);
349 d_is_shutdown = true;
350 // do whatever there is to do to shutdown
353 usrp()->common_write_io(C_RX, d_which, power_off(), (ENABLE_33|ENABLE_5));
355 // Power down VCO/PLL
356 d_common->_enable(false);
358 // fprintf(stderr, "wbxng_base_rx::shutdown before _write_control\n");
359 //_write_control(_compute_control_reg());
361 // fprintf(stderr, "wbxng_base_rx::shutdown before _enable_refclk\n");
362 _enable_refclk(false); // turn off refclk
364 // fprintf(stderr, "wbxng_base_rx::shutdown before set_auto_tr\n");
367 // fprintf(stderr, "wbxng_base_rx::shutdown after set_auto_tr\n");
372 wbxng_base_rx::set_auto_tr(bool on)
376 ok &= set_atr_mask (RXBB_EN|RX2_RX1N);
377 ok &= set_atr_txval( 0|RX2_RX1N);
378 ok &= set_atr_rxval(RXBB_EN| 0);
381 ok &= set_atr_mask (0);
382 ok &= set_atr_txval(0);
383 ok &= set_atr_rxval(0);
389 wbxng_base_rx::select_rx_antenna(int which_antenna)
392 Specify which antenna port to use for reception.
393 @param which_antenna: either 'TX/RX' or 'RX2'
396 if(which_antenna == 0) {
397 usrp()->write_io(d_which, 0,RX2_RX1N);
399 else if(which_antenna == 1) {
400 usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
409 wbxng_base_rx::select_rx_antenna(const std::string &which_antenna)
412 Specify which antenna port to use for reception.
413 @param which_antenna: either 'TX/RX' or 'RX2'
417 if(which_antenna == "TX/RX") {
418 usrp()->write_io(d_which, 0, RX2_RX1N);
420 else if(which_antenna == "RX2") {
421 usrp()->write_io(d_which, RX2_RX1N, RX2_RX1N);
431 wbxng_base_rx::set_gain(float gain)
436 @param gain: gain in decibels
441 gain = std::max(gain_min(), std::min(gain, gain_max()));
443 float pga_gain, agc_gain;
445 float maxgain = gain_max() - usrp()->pga_max();
446 float mingain = gain_min();
448 pga_gain = gain-maxgain;
449 assert(pga_gain <= usrp()->pga_max());
457 return _set_attn(maxgain-agc_gain) && _set_pga(int(pga_gain));
461 wbxng_base_rx::_set_attn(float attn)
463 int attn_code = int(floor(attn/0.5));
464 unsigned int iobits = (~attn_code) << ATTN_SHIFT;
465 fprintf(stderr, "Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x \n", attn, attn_code, iobits & ATTN_MASK, ATTN_MASK);
466 return usrp()->write_io(d_which, iobits, ATTN_MASK);
469 // ----------------------------------------------------------------
471 db_wbxng_tx::db_wbxng_tx(usrp_basic_sptr usrp, int which)
472 : wbxng_base_tx(usrp, which)
476 db_wbxng_tx::~db_wbxng_tx()
480 db_wbxng_rx::db_wbxng_rx(usrp_basic_sptr usrp, int which)
481 : wbxng_base_rx(usrp, which)
483 set_gain((gain_min() + gain_max()) / 2.0); // initialize gain
486 db_wbxng_rx::~db_wbxng_rx()
491 db_wbxng_rx::gain_min()
493 return usrp()->pga_min();
497 db_wbxng_rx::gain_max()
499 return usrp()->pga_max()+30.5;
503 db_wbxng_rx::gain_db_per_step()
510 db_wbxng_rx::i_and_q_swapped()