Statistics
| Branch: | Tag: | Revision:

root / host / lib / usrp / multi_usrp.cpp @ e033fc3d

History | View | Annotate | Download (27.1 KB)

1
//
2
// Copyright 2010-2011 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
#include <uhd/property_tree.hpp>
19
#include <uhd/usrp/multi_usrp.hpp>
20
#include <uhd/usrp/mboard_iface.hpp>
21
#include <uhd/utils/msg.hpp>
22
#include <uhd/exception.hpp>
23
#include <uhd/utils/msg.hpp>
24
#include <uhd/utils/gain_group.hpp>
25
#include <boost/thread.hpp>
26
#include <boost/foreach.hpp>
27
#include <boost/format.hpp>
28
#include <cmath>
29

    
30
using namespace uhd;
31
using namespace uhd::usrp;
32

    
33
const std::string multi_usrp::ALL_GAINS = "";
34

    
35
/***********************************************************************
36
 * Helper methods
37
 **********************************************************************/
38
static void do_samp_rate_warning_message(
39
    double target_rate,
40
    double actual_rate,
41
    const std::string &xx
42
){
43
    static const double max_allowed_error = 1.0; //Sps
44
    if (std::abs(target_rate - actual_rate) > max_allowed_error){
45
        UHD_MSG(warning) << boost::format(
46
            "The hardware does not support the requested %s sample rate:\n"
47
            "Target sample rate: %f MSps\n"
48
            "Actual sample rate: %f MSps\n"
49
        ) % xx % (target_rate/1e6) % (actual_rate/1e6);
50
    }
51
}
52

    
53
static void do_tune_freq_warning_message(
54
    double target_freq,
55
    double actual_freq,
56
    const std::string &xx
57
){
58
    static const double max_allowed_error = 1.0; //Hz
59
    if (std::abs(target_freq - actual_freq) > max_allowed_error){
60
        UHD_MSG(warning) << boost::format(
61
            "The hardware does not support the requested %s frequency:\n"
62
            "Target frequency: %f MHz\n"
63
            "Actual frequency: %f MHz\n"
64
        ) % xx % (target_freq/1e6) % (actual_freq/1e6);
65
    }
66
}
67

    
68
static meta_range_t make_overall_tune_range(
69
    const meta_range_t &fe_range,
70
    const meta_range_t &dsp_range,
71
    const double bw
72
){
73
    return meta_range_t(
74
        fe_range.start() + std::max(dsp_range.start(), -bw),
75
        fe_range.stop() + std::min(dsp_range.stop(), bw),
76
        dsp_range.step()
77
    );
78
}
79

    
80
/***********************************************************************
81
 * Gain helper functions
82
 **********************************************************************/
83
static double get_gain_value(property_tree::sptr subtree){
84
    return subtree->access<double>("value").get();
85
}
86

    
87
static void set_gain_value(property_tree::sptr subtree, const double gain){
88
    subtree->access<double>("value").set(gain);
89
}
90

    
91
static meta_range_t get_gain_range(property_tree::sptr subtree){
92
    return subtree->access<meta_range_t>("range").get();
93
}
94

    
95
static gain_fcns_t make_gain_fcns_from_subtree(property_tree::sptr subtree){
96
    gain_fcns_t gain_fcns;
97
    gain_fcns.get_range = boost::bind(&get_gain_range, subtree);
98
    gain_fcns.get_value = boost::bind(&get_gain_value, subtree);
99
    gain_fcns.set_value = boost::bind(&set_gain_value, subtree, _1);
100
    return gain_fcns;
101
}
102

    
103
/***********************************************************************
104
 * Tune Helper Functions
105
 **********************************************************************/
106
static const double RX_SIGN = +1.0;
107
static const double TX_SIGN = -1.0;
108

    
109
static tune_result_t tune_xx_subdev_and_dsp(
110
    const double xx_sign,
111
    property_tree::sptr dsp_subtree,
112
    property_tree::sptr rf_fe_subtree,
113
    const tune_request_t &tune_request
114
){
115
    //------------------------------------------------------------------
116
    //-- calculate the LO offset, only used with automatic policy
117
    //------------------------------------------------------------------
118
    double lo_offset = 0.0;
119
    if (rf_fe_subtree->access<bool>("use_lo_offset").get()){
120
        //If the local oscillator will be in the passband, use an offset.
121
        //But constrain the LO offset by the width of the filter bandwidth.
122
        const double rate = dsp_subtree->access<double>("rate/value").get();
123
        const double bw = rf_fe_subtree->access<double>("bandwidth/value").get();
124
        if (bw > rate) lo_offset = std::min((bw - rate)/2, rate/2);
125
    }
126

    
127
    //------------------------------------------------------------------
128
    //-- set the RF frequency depending upon the policy
129
    //------------------------------------------------------------------
130
    double target_rf_freq = 0.0;
131
    switch (tune_request.rf_freq_policy){
132
    case tune_request_t::POLICY_AUTO:
133
        target_rf_freq = tune_request.target_freq + lo_offset;
134
        rf_fe_subtree->access<double>("freq/value").set(target_rf_freq);
135
        break;
136

    
137
    case tune_request_t::POLICY_MANUAL:
138
        target_rf_freq = tune_request.rf_freq;
139
        rf_fe_subtree->access<double>("freq/value").set(target_rf_freq);
140
        break;
141

    
142
    case tune_request_t::POLICY_NONE: break; //does not set
143
    }
144
    const double actual_rf_freq = rf_fe_subtree->access<double>("freq/value").get();
145

    
146
    //------------------------------------------------------------------
147
    //-- calculate the dsp freq, only used with automatic policy
148
    //------------------------------------------------------------------
149
    double target_dsp_freq = actual_rf_freq - tune_request.target_freq;
150

    
151
    //invert the sign on the dsp freq for transmit
152
    target_dsp_freq *= xx_sign;
153

    
154
    //------------------------------------------------------------------
155
    //-- set the dsp frequency depending upon the dsp frequency policy
156
    //------------------------------------------------------------------
157
    switch (tune_request.dsp_freq_policy){
158
    case tune_request_t::POLICY_AUTO:
159
        dsp_subtree->access<double>("freq/value").set(target_dsp_freq);
160
        break;
161

    
162
    case tune_request_t::POLICY_MANUAL:
163
        target_dsp_freq = tune_request.dsp_freq;
164
        dsp_subtree->access<double>("freq/value").set(target_dsp_freq);
165
        break;
166

    
167
    case tune_request_t::POLICY_NONE: break; //does not set
168
    }
169
    const double actual_dsp_freq = dsp_subtree->access<double>("freq/value").get();
170

    
171
    //------------------------------------------------------------------
172
    //-- load and return the tune result
173
    //------------------------------------------------------------------
174
    tune_result_t tune_result;
175
    tune_result.target_rf_freq = target_rf_freq;
176
    tune_result.actual_rf_freq = actual_rf_freq;
177
    tune_result.target_dsp_freq = target_dsp_freq;
178
    tune_result.actual_dsp_freq = actual_dsp_freq;
179
    return tune_result;
180
}
181

    
182
static double derive_freq_from_xx_subdev_and_dsp(
183
    const double xx_sign,
184
    property_tree::sptr dsp_subtree,
185
    property_tree::sptr rf_fe_subtree
186
){
187
    //extract actual dsp and IF frequencies
188
    const double actual_rf_freq = rf_fe_subtree->access<double>("freq/value").get();
189
    const double actual_dsp_freq = dsp_subtree->access<double>("freq/value").get();
190

    
191
    //invert the sign on the dsp freq for transmit
192
    return actual_rf_freq - actual_dsp_freq * xx_sign;
193
}
194

    
195
/***********************************************************************
196
 * Multi USRP Implementation
197
 **********************************************************************/
198
class multi_usrp_impl : public multi_usrp{
199
public:
200
    multi_usrp_impl(const device_addr_t &addr){
201
        _dev = device::make(addr);
202
        _tree = (*_dev)[0].as<property_tree::sptr>();
203
    }
204

    
205
    device::sptr get_device(void){
206
        return _dev;
207
    }
208

    
209
    /*******************************************************************
210
     * Mboard methods
211
     ******************************************************************/
212
    void set_master_clock_rate(double rate, size_t mboard){
213
        if (mboard != ALL_MBOARDS){
214
            _tree->access<double>(mb_root(mboard) / "tick_rate").set(rate);
215
            return;
216
        }
217
        for (size_t m = 0; m < get_num_mboards(); m++){
218
            set_master_clock_rate(rate, m);
219
        }
220
    }
221

    
222
    double get_master_clock_rate(size_t mboard){
223
        return _tree->access<double>(mb_root(mboard) / "tick_rate").get();
224
    }
225

    
226
    std::string get_pp_string(void){
227
        std::string buff = str(boost::format(
228
            "%s USRP:\n"
229
            "  Device: %s\n"
230
        )
231
            % ((get_num_mboards() > 1)? "Multi" : "Single")
232
            % (_tree->access<std::string>("/name").get())
233
        );
234
        for (size_t m = 0; m < get_num_mboards(); m++){
235
            buff += str(boost::format(
236
                "  Mboard %d: %s\n"
237
            ) % m
238
                % (_tree->access<std::string>(mb_root(m) / "name").get())
239
            );
240
        }
241

    
242
        //----------- rx side of life ----------------------------------
243
        for (size_t m = 0, chan = 0; m < get_num_mboards(); m++){
244
            for (; chan < (m + 1)*get_rx_subdev_spec(m).size(); chan++){
245
                buff += str(boost::format(
246
                    "  RX Channel: %u\n"
247
                    "    RX DSP: %s\n"
248
                    "    RX Dboard: %s\n"
249
                    "    RX Subdev: %s\n"
250
                ) % chan
251
                    % rx_dsp_root(chan).leaf()
252
                    % rx_rf_fe_root(chan).branch_path().branch_path().leaf()
253
                    % (_tree->access<std::string>(rx_rf_fe_root(chan) / "name").get())
254
                );
255
            }
256
        }
257

    
258
        //----------- tx side of life ----------------------------------
259
        for (size_t m = 0, chan = 0; m < get_num_mboards(); m++){
260
            for (; chan < (m + 1)*get_tx_subdev_spec(m).size(); chan++){
261
                buff += str(boost::format(
262
                    "  TX Channel: %u\n"
263
                    "    TX DSP: %s\n"
264
                    "    TX Dboard: %s\n"
265
                    "    TX Subdev: %s\n"
266
                ) % chan
267
                    % tx_dsp_root(chan).leaf()
268
                    % tx_rf_fe_root(chan).branch_path().branch_path().leaf()
269
                    % (_tree->access<std::string>(tx_rf_fe_root(chan) / "name").get())
270
                );
271
            }
272
        }
273

    
274
        return buff;
275
    }
276

    
277
    std::string get_mboard_name(size_t mboard){
278
        return _tree->access<std::string>(mb_root(mboard) / "name").get();
279
    }
280

    
281
    time_spec_t get_time_now(size_t mboard = 0){
282
        return _tree->access<time_spec_t>(mb_root(mboard) / "time/now").get();
283
    }
284

    
285
    time_spec_t get_time_last_pps(size_t mboard = 0){
286
        return _tree->access<time_spec_t>(mb_root(mboard) / "time/pps").get();
287
    }
288

    
289
    void set_time_now(const time_spec_t &time_spec, size_t mboard){
290
        if (mboard != ALL_MBOARDS){
291
            _tree->access<time_spec_t>(mb_root(mboard) / "time/now").set(time_spec);
292
            return;
293
        }
294
        for (size_t m = 0; m < get_num_mboards(); m++){
295
            set_time_now(time_spec, m);
296
        }
297
    }
298

    
299
    void set_time_next_pps(const time_spec_t &time_spec){
300
        for (size_t m = 0; m < get_num_mboards(); m++){
301
            _tree->access<time_spec_t>(mb_root(m) / "time/pps").set(time_spec);
302
        }
303
    }
304

    
305
    void set_time_unknown_pps(const time_spec_t &time_spec){
306
        UHD_MSG(status) << "    1) catch time transition at pps edge" << std::endl;
307
        time_spec_t time_start = get_time_now();
308
        time_spec_t time_start_last_pps = get_time_last_pps();
309
        while(true){
310
            if (get_time_last_pps() != time_start_last_pps) break;
311
            if ((get_time_now() - time_start) > time_spec_t(1.1)){
312
                throw uhd::runtime_error(
313
                    "Board 0 may not be getting a PPS signal!\n"
314
                    "No PPS detected within the time interval.\n"
315
                    "See the application notes for your device.\n"
316
                );
317
            }
318
        }
319

    
320
        UHD_MSG(status) << "    2) set times next pps (synchronously)" << std::endl;
321
        set_time_next_pps(time_spec);
322
        boost::this_thread::sleep(boost::posix_time::seconds(1));
323

    
324
        //verify that the time registers are read to be within a few RTT
325
        for (size_t m = 1; m < get_num_mboards(); m++){
326
            time_spec_t time_0 = this->get_time_now(0);
327
            time_spec_t time_i = this->get_time_now(m);
328
            if (time_i < time_0 or (time_i - time_0) > time_spec_t(0.01)){ //10 ms: greater than RTT but not too big
329
                UHD_MSG(warning) << boost::format(
330
                    "Detected time deviation between board %d and board 0.\n"
331
                    "Board 0 time is %f seconds.\n"
332
                    "Board %d time is %f seconds.\n"
333
                ) % m % time_0.get_real_secs() % m % time_i.get_real_secs();
334
            }
335
        }
336
    }
337

    
338
    bool get_time_synchronized(void){
339
        for (size_t m = 1; m < get_num_mboards(); m++){
340
            time_spec_t time_0 = this->get_time_now(0);
341
            time_spec_t time_i = this->get_time_now(m);
342
            if (time_i < time_0 or (time_i - time_0) > time_spec_t(0.01)) return false;
343
        }
344
        return true;
345
    }
346

    
347
    void issue_stream_cmd(const stream_cmd_t &stream_cmd, size_t chan){
348
        if (chan != ALL_CHANS){
349
            _tree->access<stream_cmd_t>(rx_dsp_root(chan) / "stream_cmd").set(stream_cmd);
350
            return;
351
        }
352
        for (size_t c = 0; c < get_rx_num_channels(); c++){
353
            issue_stream_cmd(stream_cmd, c);
354
        }
355
    }
356

    
357
    void set_clock_config(const clock_config_t &clock_config, size_t mboard){
358
        if (mboard != ALL_MBOARDS){
359
            //set the reference source...
360
            std::string clock_source;
361
            switch(clock_config.ref_source){
362
            case clock_config_t::REF_INT: clock_source = "internal"; break;
363
            case clock_config_t::PPS_SMA: clock_source = "external"; break;
364
            case clock_config_t::PPS_MIMO: clock_source = "mimo"; break;
365
            default: clock_source = "unknown";
366
            }
367
            if (clock_source == "external" and clock_config.pps_polarity == clock_config_t::PPS_NEG) clock_source = "_external_";
368
            _tree->access<std::string>(mb_root(mboard) / "clock_source" / "value").set(clock_source);
369

    
370
            //set the time source
371
            std::string time_source;
372
            switch(clock_config.pps_source){
373
            case clock_config_t::PPS_INT: time_source = "internal"; break;
374
            case clock_config_t::PPS_SMA: time_source = "external"; break;
375
            case clock_config_t::PPS_MIMO: time_source = "mimo"; break;
376
            default: time_source = "unknown";
377
            }
378
            _tree->access<std::string>(mb_root(mboard) / "time_source" / "value").set(time_source);
379
            return;
380
        }
381
        for (size_t m = 0; m < get_num_mboards(); m++){
382
            set_clock_config(clock_config, m);
383
        }
384
    }
385

    
386
    size_t get_num_mboards(void){
387
        return _tree->list("/mboards").size();
388
    }
389

    
390
    sensor_value_t get_mboard_sensor(const std::string &name, size_t mboard){
391
        return _tree->access<sensor_value_t>(mb_root(mboard) / "sensors" / name).get();
392
    }
393

    
394
    std::vector<std::string> get_mboard_sensor_names(size_t mboard){
395
        return _tree->list(mb_root(mboard) / "sensors");
396
    }
397

    
398
    mboard_iface::sptr get_mboard_iface(size_t){
399
        return mboard_iface::sptr(); //not implemented
400
    }
401

    
402
    /*******************************************************************
403
     * RX methods
404
     ******************************************************************/
405
    void set_rx_subdev_spec(const subdev_spec_t &spec, size_t mboard){
406
        if (mboard != ALL_MBOARDS){
407
            _tree->access<subdev_spec_t>(mb_root(mboard) / "rx_subdev_spec").set(spec);
408
            return;
409
        }
410
        for (size_t m = 0; m < get_num_mboards(); m++){
411
            set_rx_subdev_spec(spec, m);
412
        }
413
    }
414

    
415
    subdev_spec_t get_rx_subdev_spec(size_t mboard){
416
        return _tree->access<subdev_spec_t>(mb_root(mboard) / "rx_subdev_spec").get();
417
    }
418

    
419
    size_t get_rx_num_channels(void){
420
        size_t sum = 0;
421
        for (size_t m = 0; m < get_num_mboards(); m++){
422
            sum += get_rx_subdev_spec(m).size();
423
        }
424
        return sum;
425
    }
426

    
427
    std::string get_rx_subdev_name(size_t chan){
428
        return _tree->access<std::string>(rx_rf_fe_root(chan) / "name").get();
429
    }
430

    
431
    void set_rx_rate(double rate, size_t chan){
432
        if (chan != ALL_CHANS){
433
            _tree->access<double>(rx_dsp_root(chan) / "rate" / "value").set(rate);
434
            do_samp_rate_warning_message(rate, get_rx_rate(chan), "RX");
435
            return;
436
        }
437
        for (size_t c = 0; c < get_rx_num_channels(); c++){
438
            set_rx_rate(rate, c);
439
        }
440
    }
441

    
442
    double get_rx_rate(size_t chan){
443
        return _tree->access<double>(rx_dsp_root(chan) / "rate" / "value").get();
444
    }
445

    
446
    tune_result_t set_rx_freq(const tune_request_t &tune_request, size_t chan){
447
        tune_result_t r = tune_xx_subdev_and_dsp(RX_SIGN, _tree->subtree(rx_dsp_root(chan)), _tree->subtree(rx_rf_fe_root(chan)), tune_request);
448
        do_tune_freq_warning_message(tune_request.target_freq, get_rx_freq(chan), "RX");
449
        return r;
450
    }
451

    
452
    double get_rx_freq(size_t chan){
453
        return derive_freq_from_xx_subdev_and_dsp(RX_SIGN, _tree->subtree(rx_dsp_root(chan)), _tree->subtree(rx_rf_fe_root(chan)));
454
    }
455

    
456
    freq_range_t get_rx_freq_range(size_t chan){
457
        return make_overall_tune_range(
458
            _tree->access<meta_range_t>(rx_rf_fe_root(chan) / "freq" / "range").get(),
459
            _tree->access<meta_range_t>(rx_dsp_root(chan) / "freq" / "range").get(),
460
            this->get_rx_bandwidth(chan)
461
        );
462
    }
463

    
464
    void set_rx_gain(double gain, const std::string &name, size_t chan){
465
        return rx_gain_group(chan)->set_value(gain, name);
466
    }
467

    
468
    double get_rx_gain(const std::string &name, size_t chan){
469
        return rx_gain_group(chan)->get_value(name);
470
    }
471

    
472
    gain_range_t get_rx_gain_range(const std::string &name, size_t chan){
473
        return rx_gain_group(chan)->get_range(name);
474
    }
475

    
476
    std::vector<std::string> get_rx_gain_names(size_t chan){
477
        return rx_gain_group(chan)->get_names();
478
    }
479

    
480
    void set_rx_antenna(const std::string &ant, size_t chan){
481
        _tree->access<std::string>(rx_rf_fe_root(chan) / "antenna" / "value").set(ant);
482
    }
483

    
484
    std::string get_rx_antenna(size_t chan){
485
        return _tree->access<std::string>(rx_rf_fe_root(chan) / "antenna" / "value").get();
486
    }
487

    
488
    std::vector<std::string> get_rx_antennas(size_t chan){
489
        return _tree->access<std::vector<std::string> >(rx_rf_fe_root(chan) / "antenna" / "options").get();
490
    }
491

    
492
    void set_rx_bandwidth(double bandwidth, size_t chan){
493
        _tree->access<double>(rx_rf_fe_root(chan) / "bandwidth" / "value").set(bandwidth);
494
    }
495

    
496
    double get_rx_bandwidth(size_t chan){
497
        return _tree->access<double>(rx_rf_fe_root(chan) / "bandwidth" / "value").get();
498
    }
499

    
500
    dboard_iface::sptr get_rx_dboard_iface(size_t chan){
501
        return _tree->access<dboard_iface::sptr>(rx_rf_fe_root(chan).branch_path().branch_path() / "iface").get();
502
    }
503

    
504
    sensor_value_t get_rx_sensor(const std::string &name, size_t chan){
505
        return _tree->access<sensor_value_t>(rx_rf_fe_root(chan) / "sensors" / name).get();
506
    }
507

    
508
    std::vector<std::string> get_rx_sensor_names(size_t chan){
509
        return _tree->list(rx_rf_fe_root(chan) / "sensors");
510
    }
511

    
512
    /*******************************************************************
513
     * TX methods
514
     ******************************************************************/
515
    void set_tx_subdev_spec(const subdev_spec_t &spec, size_t mboard){
516
        if (mboard != ALL_MBOARDS){
517
            _tree->access<subdev_spec_t>(mb_root(mboard) / "tx_subdev_spec").set(spec);
518
            return;
519
        }
520
        for (size_t m = 0; m < get_num_mboards(); m++){
521
            set_tx_subdev_spec(spec, m);
522
        }
523
    }
524

    
525
    subdev_spec_t get_tx_subdev_spec(size_t mboard){
526
        return _tree->access<subdev_spec_t>(mb_root(mboard) / "tx_subdev_spec").get();
527
    }
528

    
529
    std::string get_tx_subdev_name(size_t chan){
530
        return _tree->access<std::string>(tx_rf_fe_root(chan) / "name").get();
531
    }
532

    
533
    size_t get_tx_num_channels(void){
534
        size_t sum = 0;
535
        for (size_t m = 0; m < get_num_mboards(); m++){
536
            sum += get_tx_subdev_spec(m).size();
537
        }
538
        return sum;
539
    }
540

    
541
    void set_tx_rate(double rate, size_t chan){
542
        if (chan != ALL_CHANS){
543
            _tree->access<double>(tx_dsp_root(chan) / "rate" / "value").set(rate);
544
            do_samp_rate_warning_message(rate, get_tx_rate(chan), "TX");
545
            return;
546
        }
547
        for (size_t c = 0; c < get_tx_num_channels(); c++){
548
            set_tx_rate(rate, c);
549
        }
550
    }
551

    
552
    double get_tx_rate(size_t chan){
553
        return _tree->access<double>(tx_dsp_root(chan) / "rate" / "value").get();
554
    }
555

    
556
    tune_result_t set_tx_freq(const tune_request_t &tune_request, size_t chan){
557
        tune_result_t r = tune_xx_subdev_and_dsp(TX_SIGN, _tree->subtree(tx_dsp_root(chan)), _tree->subtree(tx_rf_fe_root(chan)), tune_request);
558
        do_tune_freq_warning_message(tune_request.target_freq, get_tx_freq(chan), "TX");
559
        return r;
560
    }
561

    
562
    double get_tx_freq(size_t chan){
563
        return derive_freq_from_xx_subdev_and_dsp(TX_SIGN, _tree->subtree(tx_dsp_root(chan)), _tree->subtree(tx_rf_fe_root(chan)));
564
    }
565

    
566
    freq_range_t get_tx_freq_range(size_t chan){
567
        return make_overall_tune_range(
568
            _tree->access<meta_range_t>(tx_rf_fe_root(chan) / "freq" / "range").get(),
569
            _tree->access<meta_range_t>(tx_dsp_root(chan) / "freq" / "range").get(),
570
            this->get_tx_bandwidth(chan)
571
        );
572
    }
573

    
574
    void set_tx_gain(double gain, const std::string &name, size_t chan){
575
        return tx_gain_group(chan)->set_value(gain, name);
576
    }
577

    
578
    double get_tx_gain(const std::string &name, size_t chan){
579
        return tx_gain_group(chan)->get_value(name);
580
    }
581

    
582
    gain_range_t get_tx_gain_range(const std::string &name, size_t chan){
583
        return tx_gain_group(chan)->get_range(name);
584
    }
585

    
586
    std::vector<std::string> get_tx_gain_names(size_t chan){
587
        return tx_gain_group(chan)->get_names();
588
    }
589

    
590
    void set_tx_antenna(const std::string &ant, size_t chan){
591
        _tree->access<std::string>(tx_rf_fe_root(chan) / "antenna" / "value").set(ant);
592
    }
593

    
594
    std::string get_tx_antenna(size_t chan){
595
        return _tree->access<std::string>(tx_rf_fe_root(chan) / "antenna" / "value").get();
596
    }
597

    
598
    std::vector<std::string> get_tx_antennas(size_t chan){
599
        return _tree->access<std::vector<std::string> >(tx_rf_fe_root(chan) / "antenna" / "options").get();
600
    }
601

    
602
    void set_tx_bandwidth(double bandwidth, size_t chan){
603
        _tree->access<double>(tx_rf_fe_root(chan) / "bandwidth" / "value").set(bandwidth);
604
    }
605

    
606
    double get_tx_bandwidth(size_t chan){
607
        return _tree->access<double>(tx_rf_fe_root(chan) / "bandwidth" / "value").get();
608
    }
609

    
610
    dboard_iface::sptr get_tx_dboard_iface(size_t chan){
611
        return _tree->access<dboard_iface::sptr>(tx_rf_fe_root(chan).branch_path().branch_path() / "iface").get();
612
    }
613

    
614
    sensor_value_t get_tx_sensor(const std::string &name, size_t chan){
615
        return _tree->access<sensor_value_t>(tx_rf_fe_root(chan) / "sensors" / name).get();
616
    }
617

    
618
    std::vector<std::string> get_tx_sensor_names(size_t chan){
619
        return _tree->list(tx_rf_fe_root(chan) / "sensors");
620
    }
621

    
622
private:
623
    device::sptr _dev;
624
    property_tree::sptr _tree;
625

    
626
    struct mboard_chan_pair{
627
        size_t mboard, chan;
628
        mboard_chan_pair(void): mboard(0), chan(0){}
629
    };
630

    
631
    mboard_chan_pair rx_chan_to_mcp(size_t chan){
632
        mboard_chan_pair mcp;
633
        mcp.chan = chan;
634
        for (mcp.mboard = 0; mcp.mboard < get_num_mboards(); mcp.mboard++){
635
            size_t sss = get_rx_subdev_spec(mcp.mboard).size();
636
            if (mcp.chan < sss) break;
637
            mcp.chan -= sss;
638
        }
639
        return mcp;
640
    }
641

    
642
    mboard_chan_pair tx_chan_to_mcp(size_t chan){
643
        mboard_chan_pair mcp;
644
        mcp.chan = chan;
645
        for (mcp.mboard = 0; mcp.mboard < get_num_mboards(); mcp.mboard++){
646
            size_t sss = get_tx_subdev_spec(mcp.mboard).size();
647
            if (mcp.chan < sss) break;
648
            mcp.chan -= sss;
649
        }
650
        return mcp;
651
    }
652

    
653
    fs_path mb_root(const size_t mboard){
654
        const std::string name = _tree->list("/mboards").at(mboard);
655
        return "/mboards/" + name;
656
    }
657

    
658
    fs_path rx_dsp_root(const size_t chan){
659
        mboard_chan_pair mcp = rx_chan_to_mcp(chan);
660
        const std::string name = _tree->list(mb_root(mcp.mboard) / "rx_dsps").at(mcp.chan);
661
        return mb_root(mcp.mboard) / "rx_dsps" / name;
662
    }
663

    
664
    fs_path tx_dsp_root(const size_t chan){
665
        mboard_chan_pair mcp = tx_chan_to_mcp(chan);
666
        const std::string name = _tree->list(mb_root(mcp.mboard) / "tx_dsps").at(mcp.chan);
667
        return mb_root(mcp.mboard) / "tx_dsps" / name;
668
    }
669

    
670
    fs_path rx_rf_fe_root(const size_t chan){
671
        mboard_chan_pair mcp = rx_chan_to_mcp(chan);
672
        const subdev_spec_pair_t spec = get_rx_subdev_spec(mcp.mboard).at(mcp.chan);
673
        return mb_root(mcp.mboard) / "dboards" / spec.db_name / "rx_frontends" / spec.sd_name;
674
    }
675

    
676
    fs_path tx_rf_fe_root(const size_t chan){
677
        mboard_chan_pair mcp = tx_chan_to_mcp(chan);
678
        const subdev_spec_pair_t spec = get_tx_subdev_spec(mcp.mboard).at(mcp.chan);
679
        return mb_root(mcp.mboard) / "dboards" / spec.db_name / "tx_frontends" / spec.sd_name;
680
    }
681

    
682
    gain_group::sptr rx_gain_group(size_t chan){
683
        mboard_chan_pair mcp = rx_chan_to_mcp(chan);
684
        const subdev_spec_pair_t spec = get_rx_subdev_spec(mcp.mboard).at(mcp.chan);
685
        gain_group::sptr gg = gain_group::make();
686
        BOOST_FOREACH(const std::string &name, _tree->list(mb_root(mcp.mboard) / "rx_codecs" / spec.db_name / "gains")){
687
            gg->register_fcns("ADC-"+name, make_gain_fcns_from_subtree(_tree->subtree(mb_root(mcp.mboard) / "rx_codecs" / spec.db_name / "gains" / name)), 0 /* low prio */);
688
        }
689
        BOOST_FOREACH(const std::string &name, _tree->list(rx_rf_fe_root(chan) / "gains")){
690
            gg->register_fcns(name, make_gain_fcns_from_subtree(_tree->subtree(rx_rf_fe_root(chan) / "gains" / name)), 1 /* high prio */);
691
        }
692
        return gg;
693
    }
694

    
695
    gain_group::sptr tx_gain_group(size_t chan){
696
        mboard_chan_pair mcp = tx_chan_to_mcp(chan);
697
        const subdev_spec_pair_t spec = get_tx_subdev_spec(mcp.mboard).at(mcp.chan);
698
        gain_group::sptr gg = gain_group::make();
699
        BOOST_FOREACH(const std::string &name, _tree->list(mb_root(mcp.mboard) / "tx_codecs" / spec.db_name / "gains")){
700
            gg->register_fcns("ADC-"+name, make_gain_fcns_from_subtree(_tree->subtree(mb_root(mcp.mboard) / "tx_codecs" / spec.db_name / "gains" / name)), 1 /* high prio */);
701
        }
702
        BOOST_FOREACH(const std::string &name, _tree->list(tx_rf_fe_root(chan) / "gains")){
703
            gg->register_fcns(name, make_gain_fcns_from_subtree(_tree->subtree(tx_rf_fe_root(chan) / "gains" / name)), 0 /* low prio */);
704
        }
705
        return gg;
706
    }
707
};
708

    
709
/***********************************************************************
710
 * The Make Function
711
 **********************************************************************/
712
multi_usrp::sptr multi_usrp::make(const device_addr_t &dev_addr){
713
    return sptr(new multi_usrp_impl(dev_addr));
714
}