Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / dboard / db_rfx.cpp @ master

History | View | Annotate | Download (20.7 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
// IO Pin functions
19
#define POWER_IO     (1 << 7)   // Low enables power supply
20
#define ANTSW_IO     (1 << 6)   // On TX DB, 0 = TX, 1 = RX, on RX DB 0 = main ant, 1 = RX2
21
#define MIXER_IO     (1 << 5)   // Enable appropriate mixer
22
#define LOCKDET_MASK (1 << 2)   // Input pin
23

    
24
// Mixer constants
25
#define MIXER_ENB    MIXER_IO
26
#define MIXER_DIS    0
27

    
28
// Antenna constants
29
#define ANT_TX       0          //the tx line is transmitting
30
#define ANT_RX       ANTSW_IO   //the tx line is receiving
31
#define ANT_TXRX     0          //the rx line is on txrx
32
#define ANT_RX2      ANTSW_IO   //the rx line in on rx2
33
#define ANT_XX       0          //dont care how the antenna is set
34

    
35
#include "adf4360_regs.hpp"
36
#include <uhd/types/dict.hpp>
37
#include <uhd/types/ranges.hpp>
38
#include <uhd/types/sensors.hpp>
39
#include <uhd/utils/assert_has.hpp>
40
#include <uhd/utils/log.hpp>
41
#include <uhd/utils/static.hpp>
42
#include <uhd/utils/algorithm.hpp>
43
#include <uhd/utils/msg.hpp>
44
#include <uhd/usrp/dboard_id.hpp>
45
#include <uhd/usrp/dboard_base.hpp>
46
#include <uhd/usrp/dboard_manager.hpp>
47
#include <boost/assign/list_of.hpp>
48
#include <boost/bind.hpp>
49
#include <boost/format.hpp>
50
#include <boost/math/special_functions/round.hpp>
51

    
52
using namespace uhd;
53
using namespace uhd::usrp;
54
using namespace boost::assign;
55

    
56
/***********************************************************************
57
 * The RFX Series constants
58
 **********************************************************************/
59
static const std::vector<std::string> rfx_tx_antennas = list_of("TX/RX")("CAL");
60

    
61
static const std::vector<std::string> rfx_rx_antennas = list_of("TX/RX")("RX2")("CAL");
62

    
63
static const uhd::dict<std::string, gain_range_t> rfx_rx_gain_ranges = map_list_of
64
    ("PGA0", gain_range_t(0, 70, 0.022))
65
;
66

    
67
static const uhd::dict<std::string, gain_range_t> rfx400_rx_gain_ranges = map_list_of
68
    ("PGA0", gain_range_t(0, 45, 0.022))
69
;
70

    
71
/***********************************************************************
72
 * The RFX series of dboards
73
 **********************************************************************/
74
class rfx_xcvr : public xcvr_dboard_base{
75
public:
76
    rfx_xcvr(
77
        ctor_args_t args,
78
        const freq_range_t &freq_range,
79
        bool rx_div2, bool tx_div2
80
    );
81
    ~rfx_xcvr(void);
82

    
83
private:
84
    const freq_range_t _freq_range;
85
    const uhd::dict<std::string, gain_range_t> _rx_gain_ranges;
86
    const uhd::dict<dboard_iface::unit_t, bool> _div2;
87
    std::string  _rx_ant;
88
    uhd::dict<std::string, double> _rx_gains;
89
    boost::uint16_t _power_up;
90

    
91
    void set_rx_ant(const std::string &ant);
92
    void set_tx_ant(const std::string &ant);
93
    double set_rx_gain(double gain, const std::string &name);
94

    
95
    /*!
96
     * Set the LO frequency for the particular dboard unit.
97
     * \param unit which unit rx or tx
98
     * \param target_freq the desired frequency in Hz
99
     * \return the actual frequency in Hz
100
     */
101
    double set_lo_freq(dboard_iface::unit_t unit, double target_freq);
102

    
103
    /*!
104
     * Get the lock detect status of the LO.
105
     * \param unit which unit rx or tx
106
     * \return sensor for locked
107
     */
108
    sensor_value_t get_locked(dboard_iface::unit_t unit){
109
        const bool locked = (this->get_iface()->read_gpio(unit) & LOCKDET_MASK) != 0;
110
        return sensor_value_t("LO", locked, "locked", "unlocked");
111
    }
112

    
113
    /*!
114
     * Removed incorrect/confusing RSSI calculation
115
     * Limited dynamic range of sensor makes this less useful
116
     */
117
};
118

    
119
/***********************************************************************
120
 * Register the RFX dboards (min freq, max freq, rx div2, tx div2)
121
 **********************************************************************/
122
static dboard_base::sptr make_rfx_flex400(dboard_base::ctor_args_t args){
123
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(400e6, 500e6), true, true));
124
}
125

    
126
static dboard_base::sptr make_rfx_flex900(dboard_base::ctor_args_t args){
127
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(750e6, 1050e6), true, true));
128
}
129

    
130
static dboard_base::sptr make_rfx_flex1800(dboard_base::ctor_args_t args){
131
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(1500e6, 2100e6), false, false));
132
}
133

    
134
static dboard_base::sptr make_rfx_flex1200(dboard_base::ctor_args_t args){
135
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(1150e6, 1450e6), true, true));
136
}
137

    
138
static dboard_base::sptr make_rfx_flex2200(dboard_base::ctor_args_t args){
139
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(2000e6, 2400e6), false, false));
140
}
141

    
142
static dboard_base::sptr make_rfx_flex2400(dboard_base::ctor_args_t args){
143
    return dboard_base::sptr(new rfx_xcvr(args, freq_range_t(2300e6, 2900e6), false, false));
144
}
145

    
146
UHD_STATIC_BLOCK(reg_rfx_dboards){
147
    dboard_manager::register_dboard(0x0024, 0x0028, &make_rfx_flex400,  "RFX400");
148
    dboard_manager::register_dboard(0x0025, 0x0029, &make_rfx_flex900,  "RFX900");
149
    dboard_manager::register_dboard(0x0034, 0x0035, &make_rfx_flex1800, "RFX1800");
150
    dboard_manager::register_dboard(0x0026, 0x002a, &make_rfx_flex1200, "RFX1200");
151
    dboard_manager::register_dboard(0x002c, 0x002d, &make_rfx_flex2200, "RFX2200");
152
    dboard_manager::register_dboard(0x0027, 0x002b, &make_rfx_flex2400, "RFX2400");
153
}
154

    
155
/***********************************************************************
156
 * Structors
157
 **********************************************************************/
158
rfx_xcvr::rfx_xcvr(
159
    ctor_args_t args,
160
    const freq_range_t &freq_range,
161
    bool rx_div2, bool tx_div2
162
):
163
    xcvr_dboard_base(args),
164
    _freq_range(freq_range),
165
    _rx_gain_ranges((get_rx_id() == 0x0024)?
166
        rfx400_rx_gain_ranges : rfx_rx_gain_ranges
167
    ),
168
    _div2(map_list_of
169
        (dboard_iface::UNIT_RX, rx_div2)
170
        (dboard_iface::UNIT_TX, tx_div2)
171
    ),
172
    _power_up((get_rx_id() == 0x0024 && get_tx_id() == 0x0028) ? POWER_IO : 0)
173
{
174
    ////////////////////////////////////////////////////////////////////
175
    // Register RX properties
176
    ////////////////////////////////////////////////////////////////////
177
    if(get_rx_id() == 0x0024) this->get_rx_subtree()->create<std::string>("name").set("RFX400 RX");
178
    else if(get_rx_id() == 0x0025) this->get_rx_subtree()->create<std::string>("name").set("RFX900 RX");
179
    else if(get_rx_id() == 0x0034) this->get_rx_subtree()->create<std::string>("name").set("RFX1800 RX");
180
    else if(get_rx_id() == 0x0026) this->get_rx_subtree()->create<std::string>("name").set("RFX1200 RX");
181
    else if(get_rx_id() == 0x002c) this->get_rx_subtree()->create<std::string>("name").set("RFX2200 RX");
182
    else if(get_rx_id() == 0x0027) this->get_rx_subtree()->create<std::string>("name").set("RFX2400 RX");
183
    else this->get_rx_subtree()->create<std::string>("name").set("RFX RX");
184

    
185
    this->get_rx_subtree()->create<sensor_value_t>("sensors/lo_locked")
186
        .publish(boost::bind(&rfx_xcvr::get_locked, this, dboard_iface::UNIT_RX));
187
    BOOST_FOREACH(const std::string &name, _rx_gain_ranges.keys()){
188
        this->get_rx_subtree()->create<double>("gains/"+name+"/value")
189
            .coerce(boost::bind(&rfx_xcvr::set_rx_gain, this, _1, name))
190
            .set(_rx_gain_ranges[name].start());
191
        this->get_rx_subtree()->create<meta_range_t>("gains/"+name+"/range")
192
            .set(_rx_gain_ranges[name]);
193
    }
194
    this->get_rx_subtree()->create<double>("freq/value")
195
        .coerce(boost::bind(&rfx_xcvr::set_lo_freq, this, dboard_iface::UNIT_RX, _1))
196
        .set((_freq_range.start() + _freq_range.stop())/2.0);
197
    this->get_rx_subtree()->create<meta_range_t>("freq/range").set(_freq_range);
198
    this->get_rx_subtree()->create<std::string>("antenna/value")
199
        .subscribe(boost::bind(&rfx_xcvr::set_rx_ant, this, _1))
200
        .set("RX2");
201
    this->get_rx_subtree()->create<std::vector<std::string> >("antenna/options")
202
        .set(rfx_rx_antennas);
203
    this->get_rx_subtree()->create<std::string>("connection").set("QI");
204
    this->get_rx_subtree()->create<bool>("enabled").set(true); //always enabled
205
    this->get_rx_subtree()->create<bool>("use_lo_offset").set(false);
206
    this->get_rx_subtree()->create<double>("bandwidth/value").set(2*20.0e6); //20MHz low-pass, we want complex double-sided
207
    this->get_rx_subtree()->create<meta_range_t>("bandwidth/range")
208
        .set(freq_range_t(2*20.0e6, 2*20.0e6));
209

    
210
    ////////////////////////////////////////////////////////////////////
211
    // Register TX properties
212
    ////////////////////////////////////////////////////////////////////
213
    if(get_tx_id() == 0x0028) this->get_tx_subtree()->create<std::string>("name").set("RFX400 TX");
214
    else if(get_tx_id() == 0x0029) this->get_tx_subtree()->create<std::string>("name").set("RFX900 TX");
215
    else if(get_tx_id() == 0x0035) this->get_tx_subtree()->create<std::string>("name").set("RFX1800 TX");
216
    else if(get_tx_id() == 0x002a) this->get_tx_subtree()->create<std::string>("name").set("RFX1200 TX");
217
    else if(get_tx_id() == 0x002d) this->get_tx_subtree()->create<std::string>("name").set("RFX2200 TX");
218
    else if(get_tx_id() == 0x002b) this->get_tx_subtree()->create<std::string>("name").set("RFX2400 TX");
219
    else this->get_tx_subtree()->create<std::string>("name").set("RFX TX");
220

    
221
    this->get_tx_subtree()->create<sensor_value_t>("sensors/lo_locked")
222
        .publish(boost::bind(&rfx_xcvr::get_locked, this, dboard_iface::UNIT_TX));
223
    this->get_tx_subtree()->create<int>("gains"); //phony property so this dir exists
224
    this->get_tx_subtree()->create<double>("freq/value")
225
        .coerce(boost::bind(&rfx_xcvr::set_lo_freq, this, dboard_iface::UNIT_TX, _1))
226
        .set((_freq_range.start() + _freq_range.stop())/2.0);
227
    this->get_tx_subtree()->create<meta_range_t>("freq/range").set(_freq_range);
228
    this->get_tx_subtree()->create<std::string>("antenna/value")
229
        .subscribe(boost::bind(&rfx_xcvr::set_tx_ant, this, _1)).set(rfx_tx_antennas.at(0));
230
    this->get_tx_subtree()->create<std::vector<std::string> >("antenna/options")
231
        .set(rfx_tx_antennas);
232
    this->get_tx_subtree()->create<std::string>("connection").set("IQ");
233
    this->get_tx_subtree()->create<bool>("enabled").set(true); //always enabled
234
    this->get_tx_subtree()->create<bool>("use_lo_offset").set(true);
235
    this->get_tx_subtree()->create<double>("bandwidth/value").set(2*20.0e6); //20MHz low-pass, we want complex double-sided
236
    this->get_tx_subtree()->create<meta_range_t>("bandwidth/range")
237
        .set(freq_range_t(2*20.0e6, 2*20.0e6));
238

    
239
    //enable the clocks that we need
240
    this->get_iface()->set_clock_enabled(dboard_iface::UNIT_TX, true);
241
    this->get_iface()->set_clock_enabled(dboard_iface::UNIT_RX, true);
242

    
243
    //set the gpio directions and atr controls (identically)
244
    boost::uint16_t output_enables = POWER_IO | ANTSW_IO | MIXER_IO;
245
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_TX, output_enables);
246
    this->get_iface()->set_pin_ctrl(dboard_iface::UNIT_RX, output_enables);
247
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_TX, output_enables);
248
    this->get_iface()->set_gpio_ddr(dboard_iface::UNIT_RX, output_enables);
249

    
250
    //setup the tx atr (this does not change with antenna)
251
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_IDLE,        _power_up | ANT_XX | MIXER_DIS);
252
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_RX_ONLY,     _power_up | ANT_RX | MIXER_DIS);
253
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_TX | MIXER_ENB);
254
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_TX | MIXER_ENB);
255

    
256
    //setup the rx atr (this does not change with antenna)
257
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_IDLE,        _power_up | ANT_XX | MIXER_DIS);
258
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_XX | MIXER_DIS);
259
    this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_RX2| MIXER_ENB);
260
}
261

    
262
rfx_xcvr::~rfx_xcvr(void){
263
    /* NOP */
264
}
265

    
266
/***********************************************************************
267
 * Antenna Handling
268
 **********************************************************************/
269
void rfx_xcvr::set_rx_ant(const std::string &ant){
270
    //validate input
271
    assert_has(rfx_rx_antennas, ant, "rfx rx antenna name");
272

    
273
    //set the rx atr regs that change with antenna setting
274
    if (ant == "CAL") {
275
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_TXRX  | MIXER_ENB);
276
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_TXRX  | MIXER_ENB);
277
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_RX_ONLY,     _power_up | MIXER_ENB | ANT_TXRX );
278
    } 
279
    else {
280
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_XX | MIXER_DIS);
281
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_RX2| MIXER_ENB);
282
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_RX_ONLY,     _power_up | MIXER_ENB |
283
            ((ant == "TX/RX")? ANT_TXRX : ANT_RX2));
284
    }
285

    
286
    //shadow the setting
287
    _rx_ant = ant;
288
}
289

    
290
void rfx_xcvr::set_tx_ant(const std::string &ant){
291
    assert_has(rfx_tx_antennas, ant, "rfx tx antenna name");
292

    
293
    //set the tx atr regs that change with antenna setting
294
    if (ant == "CAL") {
295
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_RX | MIXER_ENB);
296
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_RX | MIXER_ENB);
297
    } 
298
    else {
299
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_TX_ONLY,     _power_up | ANT_TX | MIXER_ENB);
300
        this->get_iface()->set_atr_reg(dboard_iface::UNIT_TX, dboard_iface::ATR_REG_FULL_DUPLEX, _power_up | ANT_TX | MIXER_ENB);
301
    }
302
}
303

    
304
/***********************************************************************
305
 * Gain Handling
306
 **********************************************************************/
307
static double rx_pga0_gain_to_dac_volts(double &gain, double range){
308
    //voltage level constants (negative slope)
309
    static const double max_volts = .2, min_volts = 1.2;
310
    static const double slope = (max_volts-min_volts)/(range);
311

    
312
    //calculate the voltage for the aux dac
313
    double dac_volts = uhd::clip<double>(gain*slope + min_volts, max_volts, min_volts);
314

    
315
    //the actual gain setting
316
    gain = (dac_volts - min_volts)/slope;
317

    
318
    return dac_volts;
319
}
320

    
321
double rfx_xcvr::set_rx_gain(double gain, const std::string &name){
322
    assert_has(_rx_gain_ranges.keys(), name, "rfx rx gain name");
323
    if(name == "PGA0"){
324
        double dac_volts = rx_pga0_gain_to_dac_volts(gain, 
325
                              (_rx_gain_ranges["PGA0"].stop() - _rx_gain_ranges["PGA0"].start()));
326

    
327
        //write the new voltage to the aux dac
328
        this->get_iface()->write_aux_dac(dboard_iface::UNIT_RX, dboard_iface::AUX_DAC_A, dac_volts);
329

    
330
        return gain;
331
    }
332
    else UHD_THROW_INVALID_CODE_PATH();
333
}
334

    
335
/***********************************************************************
336
 * Tuning
337
 **********************************************************************/
338
double rfx_xcvr::set_lo_freq(
339
    dboard_iface::unit_t unit,
340
    double target_freq
341
){
342
    UHD_LOGV(often) << boost::format(
343
        "RFX tune: target frequency %f Mhz"
344
    ) % (target_freq/1e6) << std::endl;
345

    
346
    //clip the input
347
    target_freq = _freq_range.clip(target_freq);
348
    if (_div2[unit]) target_freq *= 2;
349

    
350
    //rfx400 rx is a special case with div2 in mixer, so adf4360 must output fundamental
351
    bool is_rx_rfx400 = ((get_rx_id() == 0x0024) && unit != dboard_iface::UNIT_TX);
352

    
353
    //map prescalers to the register enums
354
    static const uhd::dict<int, adf4360_regs_t::prescaler_value_t> prescaler_to_enum = map_list_of
355
        (8,  adf4360_regs_t::PRESCALER_VALUE_8_9)
356
        (16, adf4360_regs_t::PRESCALER_VALUE_16_17)
357
        (32, adf4360_regs_t::PRESCALER_VALUE_32_33)
358
    ;
359

    
360
    //map band select clock dividers to enums
361
    static const uhd::dict<int, adf4360_regs_t::band_select_clock_div_t> bandsel_to_enum = map_list_of
362
        (1, adf4360_regs_t::BAND_SELECT_CLOCK_DIV_1)
363
        (2, adf4360_regs_t::BAND_SELECT_CLOCK_DIV_2)
364
        (4, adf4360_regs_t::BAND_SELECT_CLOCK_DIV_4)
365
        (8, adf4360_regs_t::BAND_SELECT_CLOCK_DIV_8)
366
    ;
367

    
368
    double actual_freq=0, ref_freq = this->get_iface()->get_clock_rate(unit);
369
    int R=0, BS=0, P=0, B=0, A=0;
370

    
371
    /*
372
     * The goal here to to loop though possible R dividers,
373
     * band select clock dividers, and prescaler values.
374
     * Calculate the A and B counters for each set of values.
375
     * The loop exits when it meets all of the constraints.
376
     * The resulting loop values are loaded into the registers.
377
     *
378
     * fvco = [P*B + A] * fref/R
379
     * fvco*R/fref = P*B + A = N
380
     */
381
    for(R = 2; R <= 32; R+=2){
382
        BOOST_FOREACH(BS, bandsel_to_enum.keys()){
383
            if (ref_freq/R/BS > 1e6) continue; //constraint on band select clock
384
            BOOST_FOREACH(P, prescaler_to_enum.keys()){
385
                //calculate B and A from N
386
                double N = target_freq*R/ref_freq;
387
                B = int(std::floor(N/P));
388
                A = boost::math::iround(N - P*B);
389
                if (B < A or B > 8191 or B < 3 or A > 31) continue; //constraints on A, B
390
                //calculate the actual frequency
391
                actual_freq = double(P*B + A)*ref_freq/R;
392
                if (actual_freq/P > 300e6) continue; //constraint on prescaler output
393
                //constraints met: exit loop
394
                goto done_loop;
395
            }
396
        }
397
    } done_loop:
398

    
399
    UHD_LOGV(often) << boost::format(
400
        "RFX tune: R=%d, BS=%d, P=%d, B=%d, A=%d, DIV2=%d"
401
    ) % R % BS % P % B % A % int(_div2[unit] && (!is_rx_rfx400)) << std::endl;
402

    
403
    //load the register values
404
    adf4360_regs_t regs;
405
    regs.core_power_level        = adf4360_regs_t::CORE_POWER_LEVEL_10MA;
406
    regs.counter_operation       = adf4360_regs_t::COUNTER_OPERATION_NORMAL;
407
    regs.muxout_control          = adf4360_regs_t::MUXOUT_CONTROL_DLD;
408
    regs.phase_detector_polarity = adf4360_regs_t::PHASE_DETECTOR_POLARITY_POS;
409
    regs.charge_pump_output      = adf4360_regs_t::CHARGE_PUMP_OUTPUT_NORMAL;
410
    regs.cp_gain_0               = adf4360_regs_t::CP_GAIN_0_SET1;
411
    regs.mute_till_ld            = adf4360_regs_t::MUTE_TILL_LD_ENB;
412
    regs.output_power_level      = adf4360_regs_t::OUTPUT_POWER_LEVEL_3_5MA;
413
    regs.current_setting1        = adf4360_regs_t::CURRENT_SETTING1_0_31MA;
414
    regs.current_setting2        = adf4360_regs_t::CURRENT_SETTING2_0_31MA;
415
    regs.power_down              = adf4360_regs_t::POWER_DOWN_NORMAL_OP;
416
    regs.prescaler_value         = prescaler_to_enum[P];
417
    regs.a_counter               = A;
418
    regs.b_counter               = B;
419
    regs.cp_gain_1               = adf4360_regs_t::CP_GAIN_1_SET1;
420
    regs.divide_by_2_output      = (_div2[unit] && (!is_rx_rfx400)) ?  // Special case RFX400 RX Mixer divides by two
421
                                    adf4360_regs_t::DIVIDE_BY_2_OUTPUT_DIV2 :
422
                                    adf4360_regs_t::DIVIDE_BY_2_OUTPUT_FUND ;
423
    regs.divide_by_2_prescaler   = adf4360_regs_t::DIVIDE_BY_2_PRESCALER_FUND;
424
    regs.r_counter               = R;
425
    regs.ablpw                   = adf4360_regs_t::ABLPW_3_0NS;
426
    regs.lock_detect_precision   = adf4360_regs_t::LOCK_DETECT_PRECISION_5CYCLES;
427
    regs.test_mode_bit           = 0;
428
    regs.band_select_clock_div   = bandsel_to_enum[BS];
429

    
430
    //write the registers
431
    std::vector<adf4360_regs_t::addr_t> addrs = list_of //correct power-up sequence to write registers (R, C, N)
432
        (adf4360_regs_t::ADDR_RCOUNTER)
433
        (adf4360_regs_t::ADDR_CONTROL)
434
        (adf4360_regs_t::ADDR_NCOUNTER)
435
    ;
436
    BOOST_FOREACH(adf4360_regs_t::addr_t addr, addrs){
437
        this->get_iface()->write_spi(
438
            unit, spi_config_t::EDGE_RISE,
439
            regs.get_reg(addr), 24
440
        );
441
    }
442

    
443
    //return the actual frequency
444
    if (_div2[unit]) actual_freq /= 2;
445
    UHD_LOGV(often) << boost::format(
446
        "RFX tune: actual frequency %f Mhz"
447
    ) % (actual_freq/1e6) << std::endl;
448
    return actual_freq;
449
}