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 if (bw < 1e6 || bw > 33e6) {
157 fprintf(stderr, "db_dbs_rx::set_bw: bw (=%f) must be between 1e6 and 33e6 inclusive\n", bw);
161 // struct bw_t ret = {0, 0, 0};
162 int m_max, m_min, m_test, fdac_test;
164 m_max = int(std::min(31, (int)floor(_refclk_freq()/1e6)));
165 else if(bw >= 2e6) // Outside of Specs!
166 m_max = int(std::min(31, (int)floor(_refclk_freq()/.5e6)));
167 else // Way outside of Specs!
168 m_max = int(std::min(31, (int)floor(_refclk_freq()/.25e6)));
170 m_min = int(ceil(_refclk_freq()/2.5e6));
172 while(m_test >= m_min) {
173 fdac_test = static_cast<int>(round(((bw * m_test / _refclk_freq())-4)/.145));
180 if(m_test>=m_min && fdac_test>=0) {
182 _set_fdac(fdac_test);
186 //ret.div = _refclk_freq()/d_m*(4+0.145*d_fdac);
189 fprintf(stderr, "db_dbs_rx::set_bw: failed\n");
198 db_dbs_rx::_set_dl(int dl)
200 assert(dl == 0 || dl == 1);
206 db_dbs_rx::_set_gc2(int gc2)
208 assert(gc2<32 && gc2>=0);
214 db_dbs_rx::_set_gc1(int gc1)
216 assert(gc1>=0 && gc1<4096);
218 usrp()->write_aux_dac(d_which, 0, gc1);
222 db_dbs_rx::_set_pga(int pga_gain)
224 assert(pga_gain>=0 && pga_gain<=20);
226 usrp()->set_pga (0, pga_gain);
227 usrp()->set_pga (1, pga_gain);
230 usrp()->set_pga (2, pga_gain);
231 usrp()->set_pga (3, pga_gain);
236 db_dbs_rx::gain_min()
242 db_dbs_rx::gain_max()
248 db_dbs_rx::gain_db_per_step()
254 db_dbs_rx::set_gain(float gain)
258 // @param gain: gain in decibels
259 // @returns True/False
261 if(!(gain>=0 && gain<105)) {
262 throw std::runtime_error("gain out of range\n");
265 int gc1=0, gc2=0, dl=0, pga=0;
268 gc1 = int((-gain*1.85/56.0 + 2.6)*4096.0/3.3);
277 gc2 = static_cast<int>(round(31.0 * (1-gain/24.0)));
301 db_dbs_rx::_set_osc(int osc)
303 assert(osc>=0 && osc<8);
309 db_dbs_rx::_set_cp(int cp)
311 assert(cp>=0 && cp<4);
317 db_dbs_rx::_set_n(int n)
319 assert(n>256 && n<32768);
326 db_dbs_rx::_set_div2(int div2)
328 assert(div2 == 0 || div2 == 1);
334 db_dbs_rx::_set_r(int r)
336 assert(r>=0 && r<128);
338 d_r_int = static_cast<int>(round(log10(r)/log10(2)) - 1);
342 // FIXME How do we handle ADE and ADL properly?
344 db_dbs_rx::_set_ade(int ade)
346 assert(ade == 0 || ade == 1);
352 db_dbs_rx::freq_min()
358 db_dbs_rx::freq_max()
364 db_dbs_rx::set_freq(double freq)
366 // Set the frequency.
368 // @param freq: target RF frequency in Hz
369 // @type freq: double
371 // @returns (ok, actual_baseband_freq) where:
372 // ok is True or False and indicates success or failure,
373 // actual_baseband_freq is RF frequency that corresponds to DC in the IF.
375 freq_result_t args = {false, 0};
377 if(!(freq>=freq_min() && freq<=freq_max())) {
392 int rmin = std::max(2, (int)(_refclk_freq()/2e6));
393 int rmax = std::min(128, (int)(_refclk_freq()/500e3));
398 int best_delta = 10e6;
402 n = static_cast<int>(round(freq/(_refclk_freq()/r)));
403 if(r<rmin || n<256) {
407 delta = (int)fabs(n*_refclk_freq()/r - freq);
413 if(delta < best_delta*0.9) {
422 _set_n(static_cast<int>(round(best_n)));
427 else if(vcofreq < 2711e6)
429 else if(vcofreq < 3025e6)
431 else if(vcofreq < 3341e6)
433 else if(vcofreq < 3727e6)
435 else if(vcofreq < 4143e6)
437 else if(vcofreq < 4493e6)
446 std::vector<int> bytes(2);
447 while(adc_val == 0 || adc_val == 7) {
448 bytes = _read_status();
449 adc_val = bytes[0] >> 2;
458 else if(adc_val == 7) {
469 if(adc_val == 1 || adc_val == 2) {
472 else if(adc_val == 3 || adc_val == 4) {
480 args.baseband_freq = d_n * _refclk_freq() / d_r;
485 db_dbs_rx::_refclk_divisor()
487 //Return value to stick in REFCLK_DIVISOR register
492 db_dbs_rx::is_quadrature()
494 // Return True if this board requires both I & Q analog channels.