2 // Copyright 2008 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.
21 #include <db_dbs_rx.h>
22 #include <db_base_impl.h>
26 /*****************************************************************************/
29 db_dbs_rx::db_dbs_rx(usrp_basic_sptr _usrp, int which)
30 : db_base(_usrp, which)
32 // Control DBS receiver based USRP daughterboard.
34 // @param usrp: instance of usrp.source_c
35 // @param which: which side: 0, 1 corresponding to RX_A or RX_B respectively
37 usrp()->_write_oe(d_which, 0x0001, 0x0001);
62 set_gain((gain_min() + gain_max()) / 2.0); // initialize gain
64 bypass_adc_buffers(true);
67 db_dbs_rx::~db_dbs_rx()
77 // do whatever there is to do to shutdown orderly
78 _enable_refclk(false);
83 db_dbs_rx::_write_reg (int regno, int v)
85 //regno is in [0,5], v is value to write to register"""
86 assert (0 <= regno && regno <= 5);
87 std::vector<int> args(2);
90 usrp()->write_i2c (d_i2c_addr, int_seq_to_str (args));
94 db_dbs_rx::_write_regs (int starting_regno, const std::vector<int> &vals)
96 // starting_regno is in [0,5],
97 // vals is a seq of integers to write to consecutive registers"""
100 std::vector<int> args;
101 args.push_back(starting_regno);
102 args.insert(args.end(), vals.begin(), vals.end());
103 usrp()->write_i2c (d_i2c_addr, int_seq_to_str (args));
107 db_dbs_rx::_read_status ()
109 //If successful, return list of two ints: [status_info, filter_DAC]"""
110 std::string s = usrp()->read_i2c (d_i2c_addr, 2);
112 std::vector<int> ret(0);
115 return str_to_int_seq (s);
119 db_dbs_rx::_send_reg(int regno)
121 assert(0 <= regno && regno <= 5);
123 _write_reg(0,(d_div2<<7) + (d_n>>8));
125 _write_reg(1,d_n & 255);
127 _write_reg(2,d_osc + (d_cp<<3) + (d_r_int<<5));
129 _write_reg(3,d_fdac);
131 _write_reg(4,d_m + (d_dl<<5) + (d_ade<<6) + (d_adl<<7));
133 _write_reg(5,d_gc2 + (d_diag<<5));
138 db_dbs_rx::_set_m(int m)
146 db_dbs_rx::_set_fdac(int fdac)
148 assert(fdac>=0 && fdac<128);
154 db_dbs_rx::set_bw (float bw)
156 assert(bw>=1e6 && bw<=33e6);
158 struct bw_t ret = {0, 0, 0};
159 int m_max, m_min, m_test, fdac_test;
161 m_max = int(std::min(31, (int)floor(_refclk_freq()/1e6)));
162 else if(bw >= 2e6) // Outside of Specs!
163 m_max = int(std::min(31, (int)floor(_refclk_freq()/.5e6)));
164 else // Way outside of Specs!
165 m_max = int(std::min(31, (int)floor(_refclk_freq()/.25e6)));
167 m_min = int(ceil(_refclk_freq()/2.5e6));
169 while(m_test >= m_min) {
170 fdac_test = static_cast<int>(round(((bw * m_test / _refclk_freq())-4)/.145));
177 if(m_test>=m_min && fdac_test>=0) {
179 _set_fdac(fdac_test);
183 ret.div = _refclk_freq()/d_m*(4+0.145*d_fdac);
186 fprintf(stderr, "Failed to set bw\n");
193 db_dbs_rx::_set_dl(int dl)
195 assert(dl == 0 || dl == 1);
201 db_dbs_rx::_set_gc2(int gc2)
203 assert(gc2<32 && gc2>=0);
209 db_dbs_rx::_set_gc1(int gc1)
211 assert(gc1>=0 && gc1<4096);
213 usrp()->write_aux_dac(d_which, 0, gc1);
217 db_dbs_rx::_set_pga(int pga_gain)
219 assert(pga_gain>=0 && pga_gain<=20);
221 usrp()->set_pga (0, pga_gain);
222 usrp()->set_pga (1, pga_gain);
225 usrp()->set_pga (2, pga_gain);
226 usrp()->set_pga (3, pga_gain);
231 db_dbs_rx::gain_min()
237 db_dbs_rx::gain_max()
243 db_dbs_rx::gain_db_per_step()
249 db_dbs_rx::set_gain(float gain)
253 // @param gain: gain in decibels
254 // @returns True/False
256 if(!(gain>=0 && gain<105)) {
257 throw std::runtime_error("gain out of range\n");
260 int gc1=0, gc2=0, dl=0, pga=0;
263 gc1 = int((-gain*1.85/56.0 + 2.6)*4096.0/3.3);
272 gc2 = static_cast<int>(round(31.0 * (1-gain/24.0)));
296 db_dbs_rx::_set_osc(int osc)
298 assert(osc>=0 && osc<8);
304 db_dbs_rx::_set_cp(int cp)
306 assert(cp>=0 && cp<4);
312 db_dbs_rx::_set_n(int n)
314 assert(n>256 && n<32768);
321 db_dbs_rx::_set_div2(int div2)
323 assert(div2 == 0 || div2 == 1);
329 db_dbs_rx::_set_r(int r)
331 assert(r>=0 && r<128);
333 d_r_int = static_cast<int>(round(log10(r)/log10(2)) - 1);
337 // FIXME How do we handle ADE and ADL properly?
339 db_dbs_rx::_set_ade(int ade)
341 assert(ade == 0 || ade == 1);
347 db_dbs_rx::freq_min()
353 db_dbs_rx::freq_max()
359 db_dbs_rx::set_freq(double freq)
361 // Set the frequency.
363 // @param freq: target RF frequency in Hz
364 // @type freq: double
366 // @returns (ok, actual_baseband_freq) where:
367 // ok is True or False and indicates success or failure,
368 // actual_baseband_freq is RF frequency that corresponds to DC in the IF.
370 freq_result_t args = {false, 0};
372 if(!(freq>=freq_min() && freq<=freq_max())) {
387 int rmin = std::max(2, (int)(_refclk_freq()/2e6));
388 int rmax = std::min(128, (int)(_refclk_freq()/500e3));
393 int best_delta = 10e6;
397 n = static_cast<int>(round(freq/(_refclk_freq()/r)));
398 if(r<rmin || n<256) {
402 delta = (int)fabs(n*_refclk_freq()/r - freq);
408 if(delta < best_delta*0.9) {
417 _set_n(static_cast<int>(round(best_n)));
422 else if(vcofreq < 2711e6)
424 else if(vcofreq < 3025e6)
426 else if(vcofreq < 3341e6)
428 else if(vcofreq < 3727e6)
430 else if(vcofreq < 4143e6)
432 else if(vcofreq < 4493e6)
441 std::vector<int> bytes(2);
442 while(adc_val == 0 || adc_val == 7) {
443 bytes = _read_status();
444 adc_val = bytes[0] >> 2;
453 else if(adc_val == 7) {
464 if(adc_val == 1 || adc_val == 2) {
467 else if(adc_val == 3 || adc_val == 4) {
475 args.baseband_freq = d_n * _refclk_freq() / d_r;
480 db_dbs_rx::_refclk_divisor()
482 //Return value to stick in REFCLK_DIVISOR register
487 db_dbs_rx::is_quadrature()
489 // Return True if this board requires both I & Q analog channels.