Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard / db_xcvr2450.cpp @ 64d15538

History | View | Annotate | Download (26.8 KB)

1
//
2
// Copyright 2010-2012 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
// TX IO Pins
19
#define HB_PA_OFF_TXIO      (1 << 15)    // 5GHz PA, 1 = off, 0 = on
20
#define LB_PA_OFF_TXIO      (1 << 14)    // 2.4GHz PA, 1 = off, 0 = on
21
#define ANTSEL_TX1_RX2_TXIO (1 << 13)    // 1 = Ant 1 to TX, Ant 2 to RX
22
#define ANTSEL_TX2_RX1_TXIO (1 << 12)    // 1 = Ant 2 to TX, Ant 1 to RX
23
#define TX_EN_TXIO          (1 << 11)    // 1 = TX on, 0 = TX off
24
#define AD9515DIV_TXIO      (1 << 4)     // 1 = Div  by 3, 0 = Div by 2
25

    
26
#define TXIO_MASK (HB_PA_OFF_TXIO | LB_PA_OFF_TXIO | ANTSEL_TX1_RX2_TXIO | ANTSEL_TX2_RX1_TXIO | TX_EN_TXIO | AD9515DIV_TXIO)
27

    
28
// TX IO Functions
29
#define HB_PA_TXIO               LB_PA_OFF_TXIO
30
#define LB_PA_TXIO               HB_PA_OFF_TXIO
31
#define TX_ENB_TXIO              TX_EN_TXIO
32
#define TX_DIS_TXIO              (HB_PA_OFF_TXIO | LB_PA_OFF_TXIO)
33
#define AD9515DIV_3_TXIO         AD9515DIV_TXIO
34
#define AD9515DIV_2_TXIO         0
35

    
36
// RX IO Pins
37
#define LOCKDET_RXIO (1 << 15)           // This is an INPUT!!!
38
#define POWER_RXIO   (1 << 14)           // 1 = power on, 0 = shutdown
39
#define RX_EN_RXIO   (1 << 13)           // 1 = RX on, 0 = RX off
40
#define RX_HP_RXIO   (1 << 12)           // 0 = Fc set by rx_hpf, 1 = 600 KHz
41

    
42
#define RXIO_MASK (POWER_RXIO | RX_EN_RXIO | RX_HP_RXIO)
43

    
44
// RX IO Functions
45
#define POWER_UP_RXIO            POWER_RXIO
46
#define POWER_DOWN_RXIO          0
47
#define RX_ENB_RXIO              RX_EN_RXIO
48
#define RX_DIS_RXIO              0
49

    
50
#include "max2829_regs.hpp"
51
#include <uhd/utils/log.hpp>
52
#include <uhd/utils/static.hpp>
53
#include <uhd/utils/safe_call.hpp>
54
#include <uhd/utils/assert_has.hpp>
55
#include <uhd/utils/algorithm.hpp>
56
#include <uhd/types/ranges.hpp>
57
#include <uhd/types/sensors.hpp>
58
#include <uhd/types/dict.hpp>
59
#include <uhd/usrp/dboard_base.hpp>
60
#include <uhd/usrp/dboard_manager.hpp>
61
#include <boost/assign/list_of.hpp>
62
#include <boost/format.hpp>
63
#include <boost/thread.hpp>
64
#include <boost/math/special_functions/round.hpp>
65
#include <utility>
66

    
67
using namespace uhd;
68
using namespace uhd::usrp;
69
using namespace boost::assign;
70

    
71
/***********************************************************************
72
 * The XCVR 2450 constants
73
 **********************************************************************/
74
static const freq_range_t xcvr_freq_range = list_of
75
    (range_t(2.4e9, 2.5e9))
76
    (range_t(4.9e9, 6.0e9))
77
;
78

    
79
//Multiplied by 2.0 for conversion to complex bandpass from lowpass
80
static const freq_range_t xcvr_tx_bandwidth_range = list_of
81
    (range_t(2.0*12e6))
82
    (range_t(2.0*18e6))
83
    (range_t(2.0*24e6))
84
;
85

    
86
//Multiplied by 2.0 for conversion to complex bandpass from lowpass
87
static const freq_range_t xcvr_rx_bandwidth_range = list_of
88
    (range_t(2.0*0.9*7.5e6, 2.0*1.1*7.5e6))
89
    (range_t(2.0*0.9*9.5e6, 2.0*1.1*9.5e6))
90
    (range_t(2.0*0.9*14e6,  2.0*1.1*14e6))
91
    (range_t(2.0*0.9*18e6,  2.0*1.1*18e6))
92
;
93

    
94
static const std::vector<std::string> xcvr_antennas = list_of("J1")("J2");
95

    
96
static const uhd::dict<std::string, gain_range_t> xcvr_tx_gain_ranges = map_list_of
97
    ("VGA", gain_range_t(0, 30, 0.5))
98
    ("BB", gain_range_t(0, 5, 1.5))
99
;
100
static const uhd::dict<std::string, gain_range_t> xcvr_rx_gain_ranges = map_list_of
101
    ("LNA", gain_range_t(list_of
102
        (range_t(0))
103
        (range_t(15))
104
        (range_t(30.5))
105
    ))
106
    ("VGA", gain_range_t(0, 62, 2.0))
107
;
108

    
109
/***********************************************************************
110
 * The XCVR 2450 dboard class
111
 **********************************************************************/
112
class xcvr2450 : public xcvr_dboard_base{
113
public:
114
    xcvr2450(ctor_args_t args);
115
    ~xcvr2450(void);
116

    
117
private:
118
    double _lo_freq;
119
    double _rx_bandwidth, _tx_bandwidth;
120
    uhd::dict<std::string, double> _tx_gains, _rx_gains;
121
    std::string _tx_ant, _rx_ant;
122
    int _ad9515div;
123
    max2829_regs_t _max2829_regs;
124

    
125
    double set_lo_freq(double target_freq);
126
    double set_lo_freq_core(double target_freq);
127
    void set_tx_ant(const std::string &ant);
128
    void set_rx_ant(const std::string &ant);
129
    double set_tx_gain(double gain, const std::string &name);
130
    double set_rx_gain(double gain, const std::string &name);
131
    double set_rx_bandwidth(double bandwidth);
132
    double set_tx_bandwidth(double bandwidth);
133

    
134
    void update_atr(void);
135
    void spi_reset(void);
136
    void send_reg(boost::uint8_t addr){
137
        boost::uint32_t value = _max2829_regs.get_reg(addr);
138
        UHD_LOGV(often) << boost::format(
139
            "XCVR2450: send reg 0x%02x, value 0x%05x"
140
        ) % int(addr) % value << std::endl;
141
        this->get_iface()->write_spi(
142
            dboard_iface::UNIT_RX,
143
            spi_config_t::EDGE_RISE,
144
            value, 24
145
        );
146
    }
147

    
148
    static bool is_highband(double freq){return freq > 3e9;}
149

    
150
    /*!
151
     * Get the lock detect status of the LO.
152
     * \return sensor for locked
153
     */
154
    sensor_value_t get_locked(void){
155
        const bool locked = (this->get_iface()->read_gpio(dboard_iface::UNIT_RX) & LOCKDET_RXIO) != 0;
156
        return sensor_value_t("LO", locked, "locked", "unlocked");
157
    }
158

    
159
    /*!
160
     * Read the RSSI from the aux adc
161
     * \return the rssi sensor in dBm
162
     */
163
    sensor_value_t get_rssi(void){
164
        //*FIXME* RSSI depends on LNA Gain Setting (datasheet pg 16 top middle chart)
165
        double max_power = 0.0;
166
        switch(_max2829_regs.rx_lna_gain){
167
        case 0:
168
        case 1: max_power = 0;    break;
169
        case 2: max_power = -15;   break;
170
        case 3: max_power = -30.5; break;
171
        }
172

    
173
        //constants for the rssi calculation
174
        static const double min_v = 2.5, max_v = 0.5;
175
        static const double rssi_dyn_range = 60.0;
176
        //calculate the rssi from the voltage
177
        double voltage = this->get_iface()->read_aux_adc(dboard_iface::UNIT_RX, dboard_iface::AUX_ADC_B);
178
        double rssi = max_power - rssi_dyn_range*(voltage - min_v)/(max_v - min_v);
179
        return sensor_value_t("RSSI", rssi, "dBm");
180
    }
181
};
182

    
183
/***********************************************************************
184
 * Register the XCVR 2450 dboard
185
 **********************************************************************/
186
static dboard_base::sptr make_xcvr2450(dboard_base::ctor_args_t args){
187
    return dboard_base::sptr(new xcvr2450(args));
188
}
189

    
190
UHD_STATIC_BLOCK(reg_xcvr2450_dboard){
191
    //register the factory function for the rx and tx dbids
192
    dboard_manager::register_dboard(0x0061, 0x0060, &make_xcvr2450, "XCVR2450");
193
}
194

    
195
/***********************************************************************
196
 * Structors
197
 **********************************************************************/
198
xcvr2450::xcvr2450(ctor_args_t args) : xcvr_dboard_base(args){
199
    spi_reset(); //prepare the spi
200

    
201
    _rx_bandwidth = 9.5e6;
202
    _tx_bandwidth = 12.0e6;
203

    
204
    //setup the misc max2829 registers
205
    _max2829_regs.mimo_select         = max2829_regs_t::MIMO_SELECT_MIMO;
206
    _max2829_regs.band_sel_mimo       = max2829_regs_t::BAND_SEL_MIMO_MIMO;
207
    _max2829_regs.pll_cp_select       = max2829_regs_t::PLL_CP_SELECT_4MA;
208
    _max2829_regs.rssi_high_bw        = max2829_regs_t::RSSI_HIGH_BW_6MHZ;
209
    _max2829_regs.tx_lpf_coarse_adj   = max2829_regs_t::TX_LPF_COARSE_ADJ_12MHZ;
210
    _max2829_regs.rx_lpf_coarse_adj   = max2829_regs_t::RX_LPF_COARSE_ADJ_9_5MHZ;
211
    _max2829_regs.rx_lpf_fine_adj     = max2829_regs_t::RX_LPF_FINE_ADJ_100;
212
    _max2829_regs.rx_vga_gain_spi     = max2829_regs_t::RX_VGA_GAIN_SPI_SPI;
213
    _max2829_regs.rssi_output_range   = max2829_regs_t::RSSI_OUTPUT_RANGE_HIGH;
214
    _max2829_regs.rssi_op_mode        = max2829_regs_t::RSSI_OP_MODE_ENABLED;
215
    _max2829_regs.rssi_pin_fcn        = max2829_regs_t::RSSI_PIN_FCN_RSSI;
216
    _max2829_regs.rx_highpass         = max2829_regs_t::RX_HIGHPASS_100HZ;
217
    _max2829_regs.tx_vga_gain_spi     = max2829_regs_t::TX_VGA_GAIN_SPI_SPI;
218
    _max2829_regs.pa_driver_linearity = max2829_regs_t::PA_DRIVER_LINEARITY_78;
219
    _max2829_regs.tx_vga_linearity    = max2829_regs_t::TX_VGA_LINEARITY_78;
220
    _max2829_regs.tx_upconv_linearity = max2829_regs_t::TX_UPCONV_LINEARITY_78;
221

    
222
    //send initial register settings
223
    for(boost::uint8_t reg = 0x2; reg <= 0xC; reg++){
224
        this->send_reg(reg);
225
    }
226

    
227
    ////////////////////////////////////////////////////////////////////
228
    // Register RX properties
229
    ////////////////////////////////////////////////////////////////////
230
    this->get_rx_subtree()->create<std::string>("name")
231
        .set(get_rx_id().to_pp_string());
232
    this->get_rx_subtree()->create<sensor_value_t>("sensors/lo_locked")
233
        .publish(boost::bind(&xcvr2450::get_locked, this));
234
    this->get_rx_subtree()->create<sensor_value_t>("sensors/rssi")
235
        .publish(boost::bind(&xcvr2450::get_rssi, this));
236
    BOOST_FOREACH(const std::string &name, xcvr_rx_gain_ranges.keys()){
237
        this->get_rx_subtree()->create<double>("gains/"+name+"/value")
238
            .coerce(boost::bind(&xcvr2450::set_rx_gain, this, _1, name))
239
            .set(xcvr_rx_gain_ranges[name].start());
240
        this->get_rx_subtree()->create<meta_range_t>("gains/"+name+"/range")
241
            .set(xcvr_rx_gain_ranges[name]);
242
    }
243
    this->get_rx_subtree()->create<double>("freq/value")
244
        .coerce(boost::bind(&xcvr2450::set_lo_freq, this, _1))
245
        .set(double(2.45e9));
246
    this->get_rx_subtree()->create<meta_range_t>("freq/range")
247
        .set(xcvr_freq_range);
248
    this->get_rx_subtree()->create<std::string>("antenna/value")
249
        .subscribe(boost::bind(&xcvr2450::set_rx_ant, this, _1))
250
        .set(xcvr_antennas.at(0));
251
    this->get_rx_subtree()->create<std::vector<std::string> >("antenna/options")
252
        .set(xcvr_antennas);
253
    this->get_rx_subtree()->create<std::string>("connection")
254
        .set("IQ");
255
    this->get_rx_subtree()->create<bool>("enabled")
256
        .set(true); //always enabled
257
    this->get_rx_subtree()->create<bool>("use_lo_offset")
258
        .set(false);
259
    this->get_rx_subtree()->create<double>("bandwidth/value")
260
        .coerce(boost::bind(&xcvr2450::set_rx_bandwidth, this, _1)) //complex bandpass bandwidth 
261
        .set(2.0*_rx_bandwidth); //_rx_bandwidth in lowpass, convert to complex bandpass
262
    this->get_rx_subtree()->create<meta_range_t>("bandwidth/range")
263
        .set(xcvr_rx_bandwidth_range);
264

    
265
    ////////////////////////////////////////////////////////////////////
266
    // Register TX properties
267
    ////////////////////////////////////////////////////////////////////
268
    this->get_tx_subtree()->create<std::string>("name")
269
        .set(get_tx_id().to_pp_string());
270
    this->get_tx_subtree()->create<sensor_value_t>("sensors/lo_locked")
271
        .publish(boost::bind(&xcvr2450::get_locked, this));
272
    BOOST_FOREACH(const std::string &name, xcvr_tx_gain_ranges.keys()){
273
        this->get_tx_subtree()->create<double>("gains/"+name+"/value")
274
            .coerce(boost::bind(&xcvr2450::set_tx_gain, this, _1, name))
275
            .set(xcvr_tx_gain_ranges[name].start());
276
        this->get_tx_subtree()->create<meta_range_t>("gains/"+name+"/range")
277
            .set(xcvr_tx_gain_ranges[name]);
278
    }
279
    this->get_tx_subtree()->create<double>("freq/value")
280
        .coerce(boost::bind(&xcvr2450::set_lo_freq, this, _1))
281
        .set(double(2.45e9));
282
    this->get_tx_subtree()->create<meta_range_t>("freq/range")
283
        .set(xcvr_freq_range);
284
    this->get_tx_subtree()->create<std::string>("antenna/value")
285
        .subscribe(boost::bind(&xcvr2450::set_tx_ant, this, _1))
286
        .set(xcvr_antennas.at(1));
287
    this->get_tx_subtree()->create<std::vector<std::string> >("antenna/options")
288
        .set(xcvr_antennas);
289
    this->get_tx_subtree()->create<std::string>("connection")
290
        .set("QI");
291
    this->get_tx_subtree()->create<bool>("enabled")
292
        .set(true); //always enabled
293
    this->get_tx_subtree()->create<bool>("use_lo_offset")
294
        .set(false);
295
    this->get_tx_subtree()->create<double>("bandwidth/value")
296
        .coerce(boost::bind(&xcvr2450::set_tx_bandwidth, this, _1)) //complex bandpass bandwidth
297
        .set(2.0*_tx_bandwidth); //_tx_bandwidth in lowpass, convert to complex bandpass
298
    this->get_tx_subtree()->create<meta_range_t>("bandwidth/range")
299
        .set(xcvr_tx_bandwidth_range);
300

    
301
    //enable only the clocks we need
302
    this->get_iface()->set_clock_enabled(dboard_iface::UNIT_TX, true);
303

    
304
    //set the gpio directions and atr controls (identically)
305
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_TX, TXIO_MASK);
306
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_RX, RXIO_MASK);
307
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_TX, TXIO_MASK);
308
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_RX, RXIO_MASK);
309
}
310

    
311
xcvr2450::~xcvr2450(void){
312
    UHD_SAFE_CALL(spi_reset();)
313
}
314

    
315
void xcvr2450::spi_reset(void){
316
    //spi reset mode: global enable = off, tx and rx enable = on
317
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_IDLE, TX_ENB_TXIO);
318
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_IDLE, RX_ENB_RXIO | POWER_DOWN_RXIO);
319
    boost::this_thread::sleep(boost::posix_time::milliseconds(10));
320

    
321
    //take it back out of spi reset mode and wait a bit
322
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_IDLE, RX_DIS_RXIO | POWER_UP_RXIO);
323
    boost::this_thread::sleep(boost::posix_time::milliseconds(10));
324
}
325

    
326
/***********************************************************************
327
 * Update ATR regs which change with Antenna or Freq
328
 **********************************************************************/
329
void xcvr2450::update_atr(void){
330
    //calculate tx atr pins
331
    int band_sel   = (xcvr2450::is_highband(_lo_freq))? HB_PA_TXIO : LB_PA_TXIO;
332
    int tx_ant_sel = (_tx_ant == "J1")? ANTSEL_TX1_RX2_TXIO : ANTSEL_TX2_RX1_TXIO;
333
    int rx_ant_sel = (_rx_ant == "J2")? ANTSEL_TX1_RX2_TXIO : ANTSEL_TX2_RX1_TXIO;
334
    int xx_ant_sel = tx_ant_sel; //Prefer the tx antenna selection for full duplex,
335
    //due to the issue that USRP1 will take the value of full duplex for its TXATR.
336
    int ad9515div  = (_ad9515div == 3)? AD9515DIV_3_TXIO : AD9515DIV_2_TXIO;
337

    
338
    //set the tx registers
339
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_IDLE,        band_sel | ad9515div | TX_DIS_TXIO);
340
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_RX_ONLY,     band_sel | ad9515div | TX_DIS_TXIO | rx_ant_sel);
341
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_TX_ONLY,     band_sel | ad9515div | TX_ENB_TXIO | tx_ant_sel);
342
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_FULL_DUPLEX, band_sel | ad9515div | TX_ENB_TXIO | xx_ant_sel);
343

    
344
    //set the rx registers
345
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_IDLE,        POWER_UP_RXIO | RX_DIS_RXIO);
346
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_RX_ONLY,     POWER_UP_RXIO | RX_ENB_RXIO);
347
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_TX_ONLY,     POWER_UP_RXIO | RX_DIS_RXIO);
348
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_FULL_DUPLEX, POWER_UP_RXIO | RX_DIS_RXIO);
349
}
350

    
351
/***********************************************************************
352
 * Tuning
353
 **********************************************************************/
354
double xcvr2450::set_lo_freq(double target_freq){
355
    //tune the LO and sleep a bit for lock
356
    //if not locked, try some carrier offsets
357
    double actual = 0.0;
358
    for (double offset = 0.0; offset <= 3e6; offset+=1e6){
359
        actual = this->set_lo_freq_core(target_freq + offset);
360
        boost::this_thread::sleep(boost::posix_time::milliseconds(50));
361
        if (this->get_locked().to_bool()) break;
362
    }
363
    return actual;
364
}
365

    
366
double xcvr2450::set_lo_freq_core(double target_freq){
367

    
368
    //clip the input to the range
369
    target_freq = xcvr_freq_range.clip(target_freq);
370

    
371
    //variables used in the calculation below
372
    double scaler = xcvr2450::is_highband(target_freq)? (4.0/5.0) : (4.0/3.0);
373
    double ref_freq = this->get_iface()->get_clock_rate(dboard_iface::UNIT_TX);
374
    int R, intdiv, fracdiv;
375

    
376
    //loop through values until we get a match
377
    for(_ad9515div = 2; _ad9515div <= 3; _ad9515div++){
378
        for(R = 1; R <= 7; R++){
379
            double N = (target_freq*scaler*R*_ad9515div)/ref_freq;
380
            intdiv = int(std::floor(N));
381
            fracdiv = boost::math::iround((N - intdiv)*double(1 << 16));
382
            //actual minimum is 128, but most chips seems to require higher to lock
383
            if (intdiv < 131 or intdiv > 255) continue;
384
            //constraints met: exit loop
385
            goto done_loop;
386
        }
387
    } done_loop:
388

    
389
    //calculate the actual freq from the values above
390
    double N = double(intdiv) + double(fracdiv)/double(1 << 16);
391
    _lo_freq = (N*ref_freq)/(scaler*R*_ad9515div);
392

    
393
    UHD_LOGV(often)
394
        << boost::format("XCVR2450 tune:\n")
395
        << boost::format("    R=%d, N=%f, ad9515=%d, scaler=%f\n") % R % N % _ad9515div % scaler
396
        << boost::format("    Ref    Freq=%fMHz\n") % (ref_freq/1e6)
397
        << boost::format("    Target Freq=%fMHz\n") % (target_freq/1e6)
398
        << boost::format("    Actual Freq=%fMHz\n") % (_lo_freq/1e6)
399
        << std::endl;
400

    
401
    //high-high band or low-high band?
402
    if(_lo_freq > (5.35e9 + 5.47e9)/2.0){
403
        UHD_LOGV(often) << "XCVR2450 tune: Using  high-high band" << std::endl;
404
        _max2829_regs.band_select_802_11a = max2829_regs_t::BAND_SELECT_802_11A_5_47GHZ_TO_5_875GHZ;
405
    }else{
406
        UHD_LOGV(often) << "XCVR2450 tune: Using  low-high band" << std::endl;
407
        _max2829_regs.band_select_802_11a = max2829_regs_t::BAND_SELECT_802_11A_4_9GHZ_TO_5_35GHZ;
408
    }
409

    
410
    //new band select settings and ad9515 divider
411
    this->update_atr();
412

    
413
    //load new counters into registers
414
    _max2829_regs.int_div_ratio_word = intdiv;
415
    _max2829_regs.frac_div_ratio_lsb = fracdiv & 0x3;
416
    _max2829_regs.frac_div_ratio_msb = fracdiv >> 2;
417
    this->send_reg(0x3); //integer
418
    this->send_reg(0x4); //fractional
419

    
420
    //load the reference divider and band select into registers
421
    //toggle the bandswitch from off to automatic (which really means start)
422
    _max2829_regs.ref_divider = R;
423
    _max2829_regs.band_select = (xcvr2450::is_highband(_lo_freq))?
424
                                max2829_regs_t::BAND_SELECT_5GHZ   :
425
                                max2829_regs_t::BAND_SELECT_2_4GHZ ;
426
    _max2829_regs.vco_bandswitch = max2829_regs_t::VCO_BANDSWITCH_DISABLE;
427
    this->send_reg(0x5);
428
    _max2829_regs.vco_bandswitch = max2829_regs_t::VCO_BANDSWITCH_AUTOMATIC;;
429
    this->send_reg(0x5);
430

    
431
    return _lo_freq;
432
}
433

    
434
/***********************************************************************
435
 * Antenna Handling
436
 **********************************************************************/
437
void xcvr2450::set_tx_ant(const std::string &ant){
438
    assert_has(xcvr_antennas, ant, "xcvr antenna name");
439
   _tx_ant = ant;
440
    this->update_atr(); //sets the atr to the new antenna setting
441
}
442

    
443
void xcvr2450::set_rx_ant(const std::string &ant){
444
    assert_has(xcvr_antennas, ant, "xcvr antenna name");
445
    _rx_ant = ant;
446
    this->update_atr(); //sets the atr to the new antenna setting
447
}
448

    
449
/***********************************************************************
450
 * Gain Handling
451
 **********************************************************************/
452
/*!
453
 * Convert a requested gain for the tx vga into the integer register value.
454
 * The gain passed into the function will be set to the actual value.
455
 * \param gain the requested gain in dB
456
 * \return 6 bit the register value
457
 */
458
static int gain_to_tx_vga_reg(double &gain){
459
    //calculate the register value
460
    int reg = uhd::clip(boost::math::iround(gain*60/30.0) + 3, 0, 63);
461

    
462
    //calculate the actual gain value
463
    if (reg < 4)       gain = 0;
464
    else if (reg < 48) gain = double(reg/2 - 1);
465
    else               gain = double(reg/2.0 - 1.5);
466

    
467
    //return register value
468
    return reg;
469
}
470

    
471
/*!
472
 * Convert a requested gain for the tx bb into the integer register value.
473
 * The gain passed into the function will be set to the actual value.
474
 * \param gain the requested gain in dB
475
 * \return gain enum value
476
 */
477
static max2829_regs_t::tx_baseband_gain_t gain_to_tx_bb_reg(double &gain){
478
    int reg = uhd::clip(boost::math::iround(gain*3/5.0), 0, 3);
479
    switch(reg){
480
    case 0:
481
        gain = 0;
482
        return max2829_regs_t::TX_BASEBAND_GAIN_0DB;
483
    case 1:
484
        gain = 2;
485
        return max2829_regs_t::TX_BASEBAND_GAIN_2DB;
486
    case 2:
487
        gain = 3.5;
488
        return max2829_regs_t::TX_BASEBAND_GAIN_3_5DB;
489
    case 3:
490
        gain = 5;
491
        return max2829_regs_t::TX_BASEBAND_GAIN_5DB;
492
    }
493
    UHD_THROW_INVALID_CODE_PATH();
494
}
495

    
496
/*!
497
 * Convert a requested gain for the rx vga into the integer register value.
498
 * The gain passed into the function will be set to the actual value.
499
 * \param gain the requested gain in dB
500
 * \return 5 bit the register value
501
 */
502
static int gain_to_rx_vga_reg(double &gain){
503
    int reg = uhd::clip(boost::math::iround(gain/2.0), 0, 31);
504
    gain = double(reg*2);
505
    return reg;
506
}
507

    
508
/*!
509
 * Convert a requested gain for the rx lna into the integer register value.
510
 * The gain passed into the function will be set to the actual value.
511
 * \param gain the requested gain in dB
512
 * \return 2 bit the register value
513
 */
514
static int gain_to_rx_lna_reg(double &gain){
515
    int reg = uhd::clip(boost::math::iround(gain*2/30.5) + 1, 0, 3);
516
    switch(reg){
517
    case 0:
518
    case 1: gain = 0;    break;
519
    case 2: gain = 15;   break;
520
    case 3: gain = 30.5; break;
521
    }
522
    return reg;
523
}
524

    
525
double xcvr2450::set_tx_gain(double gain, const std::string &name){
526
    assert_has(xcvr_tx_gain_ranges.keys(), name, "xcvr tx gain name");
527
    if (name == "VGA"){
528
        _max2829_regs.tx_vga_gain = gain_to_tx_vga_reg(gain);
529
        send_reg(0xC);
530
    }
531
    else if(name == "BB"){
532
        _max2829_regs.tx_baseband_gain = gain_to_tx_bb_reg(gain);
533
        send_reg(0x9);
534
    }
535
    else UHD_THROW_INVALID_CODE_PATH();
536
    _tx_gains[name] = gain;
537

    
538
    return gain;
539
}
540

    
541
double xcvr2450::set_rx_gain(double gain, const std::string &name){
542
    assert_has(xcvr_rx_gain_ranges.keys(), name, "xcvr rx gain name");
543
    if (name == "VGA"){
544
        _max2829_regs.rx_vga_gain = gain_to_rx_vga_reg(gain);
545
        send_reg(0xB);
546
    }
547
    else if(name == "LNA"){
548
        _max2829_regs.rx_lna_gain = gain_to_rx_lna_reg(gain);
549
        send_reg(0xB);
550
    }
551
    else UHD_THROW_INVALID_CODE_PATH();
552
    _rx_gains[name] = gain;
553

    
554
    return gain;
555
}
556

    
557

    
558
/***********************************************************************
559
 * Bandwidth Handling
560
 **********************************************************************/
561
static max2829_regs_t::tx_lpf_coarse_adj_t bandwidth_to_tx_lpf_coarse_reg(double &bandwidth){
562
    int reg = uhd::clip(boost::math::iround((bandwidth-6.0e6)/6.0e6), 1, 3);
563

    
564
    switch(reg){
565
    case 1: // bandwidth < 15MHz
566
        bandwidth = 12e6;
567
        return max2829_regs_t::TX_LPF_COARSE_ADJ_12MHZ;
568
    case 2: // 15MHz < bandwidth < 21MHz
569
        bandwidth = 18e6;
570
        return max2829_regs_t::TX_LPF_COARSE_ADJ_18MHZ;
571
    case 3: // bandwidth > 21MHz
572
        bandwidth = 24e6;
573
        return max2829_regs_t::TX_LPF_COARSE_ADJ_24MHZ;
574
    }
575
    UHD_THROW_INVALID_CODE_PATH();
576
}
577

    
578
static max2829_regs_t::rx_lpf_fine_adj_t bandwidth_to_rx_lpf_fine_reg(double &bandwidth, double requested_bandwidth){
579
    int reg = uhd::clip(boost::math::iround((requested_bandwidth/bandwidth)/0.05), 18, 22);
580

    
581
    switch(reg){
582
    case 18: // requested_bandwidth < 92.5%
583
        bandwidth = 0.9 * bandwidth;
584
        return max2829_regs_t::RX_LPF_FINE_ADJ_90;
585
    case 19: // 92.5% < requested_bandwidth < 97.5%
586
        bandwidth = 0.95 * bandwidth;
587
        return max2829_regs_t::RX_LPF_FINE_ADJ_95;
588
    case 20: // 97.5% < requested_bandwidth < 102.5%
589
        bandwidth = 1.0 * bandwidth;
590
        return max2829_regs_t::RX_LPF_FINE_ADJ_100;
591
    case 21: // 102.5% < requested_bandwidth < 107.5%
592
        bandwidth = 1.05 * bandwidth;
593
        return max2829_regs_t::RX_LPF_FINE_ADJ_105;
594
    case 22: // 107.5% < requested_bandwidth
595
        bandwidth = 1.1 * bandwidth;
596
        return max2829_regs_t::RX_LPF_FINE_ADJ_110;
597
    }
598
    UHD_THROW_INVALID_CODE_PATH();
599
}
600

    
601
static max2829_regs_t::rx_lpf_coarse_adj_t bandwidth_to_rx_lpf_coarse_reg(double &bandwidth){
602
    int reg = uhd::clip(boost::math::iround((bandwidth-7.0e6)/1.0e6), 0, 11);
603

    
604
    switch(reg){
605
    case 0: // bandwidth < 7.5MHz
606
    case 1: // 7.5MHz < bandwidth < 8.5MHz
607
        bandwidth = 7.5e6;
608
        return max2829_regs_t::RX_LPF_COARSE_ADJ_7_5MHZ;
609
    case 2: // 8.5MHz < bandwidth < 9.5MHz
610
    case 3: // 9.5MHz < bandwidth < 10.5MHz
611
    case 4: // 10.5MHz < bandwidth < 11.5MHz
612
        bandwidth = 9.5e6;
613
        return max2829_regs_t::RX_LPF_COARSE_ADJ_9_5MHZ;
614
    case 5: // 11.5MHz < bandwidth < 12.5MHz
615
    case 6: // 12.5MHz < bandwidth < 13.5MHz
616
    case 7: // 13.5MHz < bandwidth < 14.5MHz
617
    case 8: // 14.5MHz < bandwidth < 15.5MHz
618
        bandwidth = 14e6;
619
        return max2829_regs_t::RX_LPF_COARSE_ADJ_14MHZ;
620
    case 9: // 15.5MHz < bandwidth < 16.5MHz
621
    case 10: // 16.5MHz < bandwidth < 17.5MHz
622
    case 11: // 17.5MHz < bandwidth
623
        bandwidth = 18e6;
624
        return max2829_regs_t::RX_LPF_COARSE_ADJ_18MHZ;
625
    }
626
    UHD_THROW_INVALID_CODE_PATH();
627
}
628

    
629
double xcvr2450::set_rx_bandwidth(double bandwidth){
630
    double requested_bandwidth = bandwidth;
631

    
632
    //convert complex bandpass to lowpass bandwidth
633
    bandwidth = bandwidth/2.0;
634

    
635
    //compute coarse low pass cutoff frequency setting
636
    _max2829_regs.rx_lpf_coarse_adj = bandwidth_to_rx_lpf_coarse_reg(bandwidth);
637

    
638
    //compute fine low pass cutoff frequency setting
639
    _max2829_regs.rx_lpf_fine_adj = bandwidth_to_rx_lpf_fine_reg(bandwidth, requested_bandwidth);
640

    
641
    //shadow bandwidth setting
642
    _rx_bandwidth = bandwidth;
643

    
644
    //update register
645
    send_reg(0x7);
646

    
647
    UHD_LOGV(often) << boost::format(
648
        "XCVR2450 RX Bandwidth (lp_fc): %f Hz, coarse reg: %d, fine reg: %d"
649
    ) % _rx_bandwidth % (int(_max2829_regs.rx_lpf_coarse_adj)) % (int(_max2829_regs.rx_lpf_fine_adj)) << std::endl;
650

    
651
    return 2.0*_rx_bandwidth;
652
}
653

    
654
double xcvr2450::set_tx_bandwidth(double bandwidth){
655
    //convert complex bandpass to lowpass bandwidth
656
    bandwidth = bandwidth/2.0;
657

    
658
    //compute coarse low pass cutoff frequency setting
659
    _max2829_regs.tx_lpf_coarse_adj = bandwidth_to_tx_lpf_coarse_reg(bandwidth);
660

    
661
    //shadow bandwidth setting
662
    _tx_bandwidth = bandwidth;
663

    
664
    //update register
665
    send_reg(0x7);
666

    
667
    UHD_LOGV(often) << boost::format(
668
        "XCVR2450 TX Bandwidth (lp_fc): %f Hz, coarse reg: %d"
669
    ) % _tx_bandwidth % (int(_max2829_regs.tx_lpf_coarse_adj)) << std::endl;
670

    
671
    //convert lowpass back to complex bandpass bandwidth
672
    return 2.0*_tx_bandwidth;
673
}