Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard / db_dbsrx.cpp @ ce5940f8

History | View | Annotate | Download (18.2 KB)

1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
// No RX IO Pins Used
19

    
20
// RX IO Functions
21

    
22
#include "max2118_regs.hpp"
23
#include <uhd/utils/static.hpp>
24
#include <uhd/utils/assert.hpp>
25
#include <uhd/utils/algorithm.hpp>
26
#include <uhd/types/ranges.hpp>
27
#include <uhd/types/dict.hpp>
28
#include <uhd/usrp/subdev_props.hpp>
29
#include <uhd/usrp/dboard_base.hpp>
30
#include <uhd/usrp/dboard_manager.hpp>
31
#include <boost/assign/list_of.hpp>
32
#include <boost/format.hpp>
33
#include <boost/thread.hpp>
34
#include <boost/math/special_functions/round.hpp>
35
#include <utility>
36

    
37
using namespace uhd;
38
using namespace uhd::usrp;
39
using namespace boost::assign;
40

    
41
/***********************************************************************
42
 * The DBSRX constants
43
 **********************************************************************/
44
static const bool dbsrx_debug = true;
45

    
46
static const freq_range_t dbsrx_freq_range(0.8e9, 2.4e9);
47

    
48
static const freq_range_t dbsrx_pfd_freq_range(0.15e6, 2.01e6);
49

    
50
static const prop_names_t dbsrx_antennas = list_of("J3");
51

    
52
static const uhd::dict<std::string, gain_range_t> dbsrx_gain_ranges = map_list_of
53
    ("GC1", gain_range_t(0, 56, 0.5))
54
    ("GC2", gain_range_t(0, 24, 1))
55
;
56

    
57
/***********************************************************************
58
 * The DBSRX dboard class
59
 **********************************************************************/
60
class dbsrx : public rx_dboard_base{
61
public:
62
    dbsrx(ctor_args_t args, boost::uint8_t max2118_addr);
63
    ~dbsrx(void);
64

    
65
    void rx_get(const wax::obj &key, wax::obj &val);
66
    void rx_set(const wax::obj &key, const wax::obj &val);
67

    
68
private:
69
    double _lo_freq;
70
    float _bandwidth;
71
    uhd::dict<std::string, float> _gains;
72
    max2118_write_regs_t _max2118_write_regs;
73
    max2118_read_regs_t _max2118_read_regs;
74
    boost::uint8_t _max2118_addr; //0x67 or 0x65 depending on which side
75

    
76
    void set_lo_freq(double target_freq);
77
    void set_gain(float gain, const std::string &name);
78
    void set_bandwidth(float bandwidth);
79

    
80
    void send_reg(boost::uint8_t start_reg, boost::uint8_t stop_reg){
81
        start_reg = boost::uint8_t(std::clip(int(start_reg), 0x0, 0x5));
82
        stop_reg = boost::uint8_t(std::clip(int(stop_reg), 0x0, 0x5));
83

    
84
        for(boost::uint8_t start_addr=start_reg; start_addr <= stop_reg; start_addr += sizeof(boost::uint32_t) - 1){
85
            int num_bytes = int(stop_reg - start_addr + 1) > int(sizeof(boost::uint32_t)) - 1 ? sizeof(boost::uint32_t) - 1 : stop_reg - start_addr + 1;
86

    
87
            //create buffer for register data (+1 for start address)
88
            byte_vector_t regs_vector(num_bytes + 1);
89

    
90
            //first byte is the address of first register
91
            regs_vector[0] = start_addr;
92

    
93
            //get the register data
94
            for(int i=0; i<num_bytes; i++){
95
                regs_vector[1+i] = _max2118_write_regs.get_reg(start_addr+i);
96
                if(dbsrx_debug) std::cerr << boost::format(
97
                    "DBSRX: send reg 0x%02x, value 0x%04x, start_addr = 0x%04x, num_bytes %d"
98
                ) % int(start_addr+i) % int(regs_vector[1+i]) % int(start_addr) % num_bytes << std::endl;
99
            }
100

    
101
            //send the data
102
            this->get_iface()->write_i2c(
103
                _max2118_addr, regs_vector
104
            );
105
        }
106
    }
107

    
108
    void read_reg(boost::uint8_t start_reg, boost::uint8_t stop_reg){
109
        static const boost::uint8_t status_addr = 0x0;
110
        start_reg = boost::uint8_t(std::clip(int(start_reg), 0x0, 0x1));
111
        stop_reg = boost::uint8_t(std::clip(int(stop_reg), 0x0, 0x1));
112

    
113
        for(boost::uint8_t start_addr=start_reg; start_addr <= stop_reg; start_addr += sizeof(boost::uint32_t)){
114
            int num_bytes = int(stop_reg - start_addr + 1) > int(sizeof(boost::uint32_t)) ? sizeof(boost::uint32_t) : stop_reg - start_addr + 1;
115

    
116
            //create address to start reading register data
117
            byte_vector_t address_vector(1);
118
            address_vector[0] = start_addr;
119

    
120
            /*
121
            //send the address
122
            this->get_iface()->write_i2c(
123
                _max2118_addr, address_vector
124
            );
125
            */
126

    
127
            //create buffer for register data
128
            byte_vector_t regs_vector(num_bytes);
129

    
130
            //read from i2c
131
            regs_vector = this->get_iface()->read_i2c(
132
                _max2118_addr, num_bytes
133
            );
134

    
135
            for(boost::uint8_t i=0; i < num_bytes; i++){
136
                if (i + start_addr >= status_addr){
137
                    _max2118_read_regs.set_reg(i + start_addr, regs_vector[i]);
138
                    if(dbsrx_debug) std::cerr << boost::format(
139
                        "DBSRX: set reg 0x%02x, value 0x%04x"
140
                    ) % int(i + start_addr) % int(_max2118_read_regs.get_reg(i + start_addr)) << std::endl;
141
                }
142
                if(dbsrx_debug) std::cerr << boost::format(
143
                    "DBSRX: read reg 0x%02x, value 0x%04x, start_addr = 0x%04x, num_bytes %d"
144
                ) % int(start_addr+i) % int(regs_vector[i]) % int(start_addr) % num_bytes << std::endl;
145
            }
146
        }
147
    }
148

    
149
    /*!
150
     * Is the LO locked?
151
     * \return true for locked
152
     */
153
    bool get_locked(void){
154
        read_reg(0x0, 0x1);
155

    
156
        //mask and return lock detect
157
        bool locked = 5 >= _max2118_read_regs.adc && _max2118_read_regs.adc >= 2;
158

    
159
        if(dbsrx_debug) std::cerr << boost::format(
160
            "DBSRX: locked %d"
161
        ) % locked << std::endl;
162

    
163
        return locked;
164
    }
165

    
166
};
167

    
168
/***********************************************************************
169
 * Register the DBSRX dboard
170
 **********************************************************************/
171
// FIXME 0x67 is the default i2c address on USRP2
172
//       need to handle which side for USRP1 with different address
173
static dboard_base::sptr make_dbsrx(dboard_base::ctor_args_t args){
174
    return dboard_base::sptr(new dbsrx(args, 0x67));
175
}
176

    
177
//FIXME different dbid for USRP1 also
178
UHD_STATIC_BLOCK(reg_dbsrx_dboard){
179
    //register the factory function for the rx dbid
180
    dboard_manager::register_dboard(0x000D, &make_dbsrx, "DBSRX");
181
}
182

    
183
/***********************************************************************
184
 * Structors
185
 **********************************************************************/
186
dbsrx::dbsrx(ctor_args_t args, boost::uint8_t max2118_addr) : rx_dboard_base(args){
187
    //enable only the clocks we need
188
    this->get_iface()->set_clock_enabled(dboard_iface::UNIT_RX, true);
189

    
190
    //set the gpio directions and atr controls (identically)
191
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_RX, 0x0); // All unused in atr
192
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_RX, 0x0); // All Inputs
193

    
194
    //set the i2c address for the max2118
195
    _max2118_addr = max2118_addr;
196

    
197
    //send initial register settings
198
    this->send_reg(0x0, 0x5);
199

    
200
    //set defaults for LO, gains
201
    set_lo_freq(dbsrx_freq_range.min);
202
    BOOST_FOREACH(const std::string &name, dbsrx_gain_ranges.keys()){
203
        set_gain(dbsrx_gain_ranges[name].min, name);
204
    }
205

    
206
    set_bandwidth(22.27e6); // default bandwidth from datasheet
207

    
208
    get_locked();
209
}
210

    
211
dbsrx::~dbsrx(void){
212
}
213

    
214

    
215
/***********************************************************************
216
 * Tuning
217
 **********************************************************************/
218
void dbsrx::set_lo_freq(double target_freq){
219
    target_freq = std::clip(target_freq, dbsrx_freq_range.min, dbsrx_freq_range.max);
220

    
221
    double actual_freq=0.0, pfd_freq=0.0, ref_clock=0.0;
222
    int R=0, N=0, r=0;
223

    
224
    //choose refclock
225
    std::vector<double> clock_rates = this->get_iface()->get_clock_rates(dboard_iface::UNIT_RX);
226
    BOOST_FOREACH(ref_clock, std::reversed(std::sorted(clock_rates))){
227
        if (ref_clock != 4e6) continue;
228

    
229
        //choose R
230
        for(r = 0; r <= 6; r += 1) {
231
            //compute divider from setting
232
            R = pow(2, r+1);
233
            if (dbsrx_debug) std::cerr << boost::format("DBSRX R:%d\n") % R << std::endl;
234

    
235
            //compute PFD compare frequency = ref_clock/R
236
            pfd_freq = ref_clock / R;
237

    
238
            //constrain the PFD frequency to specified range
239
            if ((pfd_freq < dbsrx_pfd_freq_range.min) or (pfd_freq > dbsrx_pfd_freq_range.max)) continue;
240

    
241
            //compute N
242
            N = int(std::floor(target_freq/pfd_freq));
243

    
244
            //constrain N to specified range
245
            if ((N < 256) or (N > 32768)) continue;
246

    
247
            goto done_loop;
248
        }
249
    } 
250

    
251
    //Assert because we failed to find a suitable combination of ref_clock, R and N 
252
    UHD_ASSERT_THROW((pfd_freq < dbsrx_pfd_freq_range.min) or (pfd_freq > dbsrx_pfd_freq_range.max));
253
    UHD_ASSERT_THROW((N < 256) or (N > 32768));
254
    done_loop:
255

    
256
    //compute resulting output frequency
257
    actual_freq = pfd_freq * N;
258

    
259
    //apply ref_clock, R, and N settings
260
    this->get_iface()->set_clock_rate(dboard_iface::UNIT_RX, ref_clock);
261
    ref_clock = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX);
262
    _max2118_write_regs.r_divider = (max2118_write_regs_t::r_divider_t) r;
263
    _max2118_write_regs.set_n_divider(N);
264
    _max2118_write_regs.ade_vco_ade_read = max2118_write_regs_t::ADE_VCO_ADE_READ_ENABLED;
265
    send_reg(0x4,0x4);
266
    send_reg(0x0,0x2);
267
    
268
    //compute prescaler variables
269
    int scaler = actual_freq > 1125e6 ? 2 : 4;
270
    _max2118_write_regs.div2 = scaler == 4 ? max2118_write_regs_t::DIV2_DIV4 : max2118_write_regs_t::DIV2_DIV2;
271

    
272
    //compute vco frequency and select vco
273
    double vco_freq = actual_freq * scaler;
274
    int vco;
275
    if (vco_freq < 2433e6)
276
        vco = 0;
277
    else if (vco_freq < 2711e6)
278
        vco=1;
279
    else if (vco_freq < 3025e6)
280
        vco=2;
281
    else if (vco_freq < 3341e6)
282
        vco=3;
283
    else if (vco_freq < 3727e6)
284
        vco=4;
285
    else if (vco_freq < 4143e6)
286
        vco=5;
287
    else if (vco_freq < 4493e6)
288
        vco=6;
289
    else
290
        vco=7;
291

    
292
    //apply vco selection
293
    _max2118_write_regs.osc_band = vco;
294
    send_reg(0x2, 0x2);
295

    
296
    //check vtune for lock condition
297
    read_reg(0x0, 0x1);
298

    
299
    //if we are out of lock for chosen vco, change vco
300
    while ((_max2118_read_regs.adc == 0) or (_max2118_read_regs.adc == 7)){
301
        //vtune is too low, try lower frequency vco
302
        if (_max2118_read_regs.adc == 0){
303
            UHD_ASSERT_THROW(_max2118_write_regs.osc_band <= 0);
304
            _max2118_write_regs.osc_band -= 1;
305
        }
306

    
307
        //vtune is too high, try higher frequency vco
308
        if (_max2118_read_regs.adc == 7){
309
            UHD_ASSERT_THROW(_max2118_write_regs.osc_band >= 7);
310
            _max2118_write_regs.osc_band += 1;
311
        }
312

    
313
        //update vco selection and check vtune
314
        send_reg(0x2, 0x2);
315
        read_reg(0x0, 0x0);
316
    }
317
      
318
    //select charge pump bias current
319
    if (_max2118_read_regs.adc <= 2) _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_100UA;
320
    else if (_max2118_read_regs.adc >= 5) _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_400UA;
321
    else _max2118_write_regs.cp_current = max2118_write_regs_t::CP_CURRENT_I_CP_200UA;
322
    
323
    //update charge pump bias current setting
324
    send_reg(0x2, 0x2);
325

    
326
    //compute actual tuned frequency
327
    _lo_freq = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX) / pow(2,(1 + _max2118_write_regs.r_divider)) * _max2118_write_regs.get_n_divider();
328

    
329
    //debug output of calculated variables
330
    if (dbsrx_debug) std::cerr
331
        << boost::format("DBSRX tune:\n")
332
        << boost::format("    VCO=%d, CP=%d, PFD Freq=%fMHz\n") % int(_max2118_write_regs.osc_band) % _max2118_write_regs.cp_current % (pfd_freq/1e6)
333
        << boost::format("    R=%d, N=%f, scaler=%d, div2=%d\n") % R % N % scaler % int(_max2118_write_regs.div2)
334
        << boost::format("    Ref    Freq=%fMHz\n") % (ref_clock/1e6)
335
        << boost::format("    Target Freq=%fMHz\n") % (target_freq/1e6)
336
        << boost::format("    Actual Freq=%fMHz\n") % (_lo_freq/1e6)
337
        << std::endl;
338
}
339

    
340
/***********************************************************************
341
 * Gain Handling
342
 **********************************************************************/
343
/*!
344
 * Convert a requested gain for the GC2 vga into the integer register value.
345
 * The gain passed into the function will be set to the actual value.
346
 * \param gain the requested gain in dB
347
 * \return 5 bit the register value
348
 */
349
static int gain_to_gc2_vga_reg(float &gain){
350
    int reg = 0;
351
    gain = std::clip<float>(boost::math::iround(gain), dbsrx_gain_ranges["GC2"].min, dbsrx_gain_ranges["GC2"].max);
352

    
353
    // Half dB steps from 0-5dB, 1dB steps from 5-24dB
354
    if (gain < 5) {
355
        reg = boost::math::iround(31.0 - gain/0.5);
356
        gain = float(boost::math::iround(gain)) * 0.5;
357
    } else {
358
        reg = boost::math::iround(22.0 - (gain - 4.0));
359
        gain = float(boost::math::iround(gain));
360
    }
361

    
362
    if (dbsrx_debug) std::cerr << boost::format(
363
        "DBSRX GC2 Gain: %f dB, reg: %d"
364
    ) % gain % reg << std::endl;
365

    
366
    return reg;
367
}
368

    
369
/*!
370
 * Convert a requested gain for the GC1 rf vga into the dac_volts value.
371
 * The gain passed into the function will be set to the actual value.
372
 * \param gain the requested gain in dB
373
 * \return dac voltage value
374
 */
375
static float gain_to_gc1_rfvga_dac(float &gain){
376
    //clip the input
377
    gain = std::clip<float>(gain, dbsrx_gain_ranges["GC1"].min, dbsrx_gain_ranges["GC1"].max);
378

    
379
    //voltage level constants
380
    static const float max_volts = float(2.7), min_volts = float(1.2);
381
    static const float slope = (max_volts-min_volts)/dbsrx_gain_ranges["GC1"].max;
382

    
383
    //calculate the voltage for the aux dac
384
    float dac_volts = gain*slope + min_volts;
385

    
386
    if (dbsrx_debug) std::cerr << boost::format(
387
        "DBSRX GC1 Gain: %f dB, dac_volts: %f V"
388
    ) % gain % dac_volts << std::endl;
389

    
390
    //the actual gain setting
391
    gain = (dac_volts - min_volts)/slope;
392

    
393
    return dac_volts;
394
}
395

    
396
void dbsrx::set_gain(float gain, const std::string &name){
397
    assert_has(dbsrx_gain_ranges.keys(), name, "dbsrx gain name");
398
    if (name == "GC2"){
399
        _max2118_write_regs.gc2 = gain_to_gc2_vga_reg(gain);
400
        send_reg(0x5, 0x5);
401
    }
402
    else if(name == "GC1"){
403
        //write the new voltage to the aux dac
404
        this->get_iface()->write_aux_dac(dboard_iface::UNIT_RX, dboard_iface::AUX_DAC_A, gain_to_gc1_rfvga_dac(gain));
405
    }
406
    else UHD_THROW_INVALID_CODE_PATH();
407
    _gains[name] = gain;
408
}
409

    
410
/***********************************************************************
411
 * Bandwidth Handling
412
 **********************************************************************/
413
void dbsrx::set_bandwidth(float bandwidth){
414
    //clip the input
415
    bandwidth = std::clip<float>(bandwidth, 4e6, 33e6);
416
    
417
    //calculate ref_freq
418
    float ref_freq = this->get_iface()->get_clock_rate(dboard_iface::UNIT_RX);
419

    
420
    //FIXME this contraint needs to be in the set_freq and needs to assert if it can't hit the range
421
    //calculate acceptable m_divider for filter tuning
422
    int m = 1;
423
    while (ref_freq/m < 1e6 or ref_freq/m > 2.5e6){ m++; }
424
    _max2118_write_regs.m_divider = m;
425

    
426
    _bandwidth = float((ref_freq/1e6/_max2118_write_regs.m_divider)*(4+0.145*_max2118_write_regs.f_dac)*1e6);
427

    
428
    _max2118_write_regs.f_dac = int(((bandwidth*_max2118_write_regs.m_divider/ref_freq) - 4)/0.145);
429

    
430
    if (dbsrx_debug) std::cerr << boost::format(
431
        "DBSRX Filter Bandwidth: %f MHz, m: %d, f_dac: %d\n"
432
    ) % (_bandwidth/1e6) % m % int(_max2118_write_regs.f_dac) << std::endl;
433

    
434
    this->send_reg(0x3, 0x5);
435
}
436

    
437
/***********************************************************************
438
 * RX Get and Set
439
 **********************************************************************/
440
void dbsrx::rx_get(const wax::obj &key_, wax::obj &val){
441
    wax::obj key; std::string name;
442
    boost::tie(key, name) = extract_named_prop(key_);
443

    
444
    //handle the get request conditioned on the key
445
    switch(key.as<subdev_prop_t>()){
446
    case SUBDEV_PROP_NAME:
447
        val = get_rx_id().to_pp_string();
448
        return;
449

    
450
    case SUBDEV_PROP_OTHERS:
451
        val = prop_names_t(); //empty
452
        return;
453

    
454
    case SUBDEV_PROP_GAIN:
455
        assert_has(_gains.keys(), name, "dbsrx gain name");
456
        val = _gains[name];
457
        return;
458

    
459
    case SUBDEV_PROP_GAIN_RANGE:
460
        assert_has(dbsrx_gain_ranges.keys(), name, "dbsrx gain name");
461
        val = dbsrx_gain_ranges[name];
462
        return;
463

    
464
    case SUBDEV_PROP_GAIN_NAMES:
465
        val = prop_names_t(dbsrx_gain_ranges.keys());
466
        return;
467

    
468
    case SUBDEV_PROP_FREQ:
469
        val = _lo_freq;
470
        return;
471

    
472
    case SUBDEV_PROP_FREQ_RANGE:
473
        val = dbsrx_freq_range;
474
        return;
475

    
476
    case SUBDEV_PROP_ANTENNA:
477
        val = std::string("J3");
478
        return;
479

    
480
    case SUBDEV_PROP_ANTENNA_NAMES:
481
        val = dbsrx_antennas;
482
        return;
483

    
484
/*
485
    case SUBDEV_PROP_QUADRATURE:
486
        val = true;
487
        return;
488

489
    case SUBDEV_PROP_IQ_SWAPPED:
490
        val = false;
491
        return;
492

493
    case SUBDEV_PROP_SPECTRUM_INVERTED:
494
        val = false;
495
        return;
496
*/
497
    case SUBDEV_PROP_CONNECTION:
498
        val = SUBDEV_CONN_COMPLEX_IQ;
499
        return;
500

    
501
    case SUBDEV_PROP_USE_LO_OFFSET:
502
        val = false;
503
        return;
504

    
505
    case SUBDEV_PROP_LO_LOCKED:
506
        val = this->get_locked();
507
        return;
508

    
509
/*
510
    case SUBDEV_PROP_RSSI:
511
        val = this->get_rssi();
512
        return;
513
*/
514

    
515
    case SUBDEV_PROP_BANDWIDTH:
516
        val = _bandwidth;
517
        return;
518

    
519
    default: UHD_THROW_PROP_GET_ERROR();
520
    }
521
}
522

    
523
void dbsrx::rx_set(const wax::obj &key_, const wax::obj &val){
524
    wax::obj key; std::string name;
525
    boost::tie(key, name) = extract_named_prop(key_);
526

    
527
    //handle the get request conditioned on the key
528
    switch(key.as<subdev_prop_t>()){
529

    
530
    case SUBDEV_PROP_FREQ:
531
        this->set_lo_freq(val.as<double>());
532
        return;
533

    
534
    case SUBDEV_PROP_GAIN:
535
        this->set_gain(val.as<float>(), name);
536
        return;
537

    
538
    case SUBDEV_PROP_BANDWIDTH:
539
        this->set_bandwidth(val.as<float>());
540
        return;
541

    
542
    default: UHD_THROW_PROP_SET_ERROR();
543
    }
544
}
545