Statistics
| Branch: | Tag: | Revision:

root / host / lib / simple_device.cpp @ 83c463d0

History | View | Annotate | Download (10.6 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
#include <uhd/simple_device.hpp>
19
#include <uhd/device.hpp>
20
#include <uhd/utils.hpp>
21
#include <uhd/props.hpp>
22
#include <boost/algorithm/string.hpp>
23
#include <boost/algorithm/string/trim.hpp>
24
#include <boost/foreach.hpp>
25
#include <boost/format.hpp>
26
#include <stdexcept>
27

    
28
using namespace uhd;
29

    
30
tune_result_t::tune_result_t(void){
31
    /* NOP */
32
}
33

    
34
/***********************************************************************
35
 * Tune Helper Function
36
 **********************************************************************/
37
static tune_result_t tune(
38
    double target_freq,
39
    double lo_offset,
40
    wax::obj subdev,
41
    wax::obj dxc,
42
    bool is_tx
43
){
44
    wax::obj subdev_freq_proxy = subdev[SUBDEV_PROP_FREQ];
45
    bool subdev_quadrature = subdev[SUBDEV_PROP_QUADRATURE].as<bool>();
46
    bool subdev_spectrum_inverted = subdev[SUBDEV_PROP_SPECTRUM_INVERTED].as<bool>();
47
    wax::obj dxc_freq_proxy = dxc[std::string("freq")];
48
    double dxc_sample_rate = dxc[std::string("rate")].as<double>();
49

    
50
    // Ask the d'board to tune as closely as it can to target_freq+lo_offset
51
    double target_inter_freq = target_freq + lo_offset;
52
    subdev_freq_proxy = target_inter_freq;
53
    double actual_inter_freq = subdev_freq_proxy.as<double>();
54

    
55
    // Calculate the DDC setting that will downconvert the baseband from the
56
    // daughterboard to our target frequency.
57
    double delta_freq = target_freq - actual_inter_freq;
58
    double delta_sign = std::signum(delta_freq);
59
    delta_freq *= delta_sign;
60
    delta_freq = fmod(delta_freq, dxc_sample_rate);
61
    bool inverted = delta_freq > dxc_sample_rate/2.0;
62
    double target_dxc_freq = inverted? (delta_freq - dxc_sample_rate) : (-delta_freq);
63
    target_dxc_freq *= delta_sign;
64

    
65
    // If the spectrum is inverted, and the daughterboard doesn't do
66
    // quadrature downconversion, we can fix the inversion by flipping the
67
    // sign of the dxc_freq...  (This only happens using the basic_rx board)
68
    if (subdev_spectrum_inverted){
69
        inverted = not inverted;
70
    }
71
    if (inverted and not subdev_quadrature){
72
        target_dxc_freq *= -1.0;
73
        inverted = not inverted;
74
    }
75
    // down conversion versus up conversion, fight!
76
    // your mother is ugly and your going down...
77
    target_dxc_freq *= (is_tx)? -1.0 : +1.0;
78

    
79
    dxc_freq_proxy = target_dxc_freq;
80
    double actual_dxc_freq = dxc_freq_proxy.as<double>();
81

    
82
    //return some kind of tune result tuple/struct
83
    tune_result_t tune_result;
84
    tune_result.target_inter_freq = target_inter_freq;
85
    tune_result.actual_inter_freq = actual_inter_freq;
86
    tune_result.target_dxc_freq = target_dxc_freq;
87
    tune_result.actual_dxc_freq = actual_dxc_freq;
88
    tune_result.spectrum_inverted = inverted;
89
    return tune_result;
90
}
91

    
92
/***********************************************************************
93
 * Helper Functions
94
 **********************************************************************/
95
static std::string trim(const std::string &in){
96
    return boost::algorithm::trim_copy(in);
97
}
98

    
99
device_addr_t args_to_device_addr(const std::string &args){
100
    device_addr_t addr;
101

    
102
    //split the args at the semi-colons
103
    std::vector<std::string> pairs;
104
    boost::split(pairs, args, boost::is_any_of(";"));
105
    BOOST_FOREACH(std::string pair, pairs){
106
        if (trim(pair) == "") continue;
107

    
108
        //split the key value pairs at the equals
109
        std::vector<std::string> key_val;
110
        boost::split(key_val, pair, boost::is_any_of("="));
111
        if (key_val.size() != 2) throw std::runtime_error("invalid args string: "+args);
112
        addr[trim(key_val[0])] = trim(key_val[1]);
113
    }
114

    
115
    return addr;
116
}
117

    
118
static std::vector<double> get_xx_rates(wax::obj decerps, wax::obj rate){
119
    std::vector<double> rates;
120
    BOOST_FOREACH(size_t decerp, decerps.as<std::vector<size_t> >()){
121
        rates.push_back(rate.as<double>()/decerp);
122
    }
123
    return rates;
124
}
125

    
126
/***********************************************************************
127
 * Simple Device Implementation
128
 **********************************************************************/
129
class simple_device_impl : public simple_device{
130
public:
131
    simple_device_impl(const device_addr_t &addr){
132
        _dev = device::make(addr);
133
        _mboard = (*_dev)[DEVICE_PROP_MBOARD];
134
        _rx_ddc = _mboard[named_prop_t(MBOARD_PROP_RX_DSP, "ddc0")];
135
        _tx_duc = _mboard[named_prop_t(MBOARD_PROP_TX_DSP, "duc0")];
136

    
137
        //extract rx subdevice
138
        wax::obj rx_dboard = _mboard[MBOARD_PROP_RX_DBOARD];
139
        std::string rx_subdev_in_use = rx_dboard[DBOARD_PROP_USED_SUBDEVS].as<prop_names_t>().at(0);
140
        _rx_subdev = rx_dboard[named_prop_t(DBOARD_PROP_SUBDEV, rx_subdev_in_use)];
141

    
142
        //extract tx subdevice
143
        wax::obj tx_dboard = _mboard[MBOARD_PROP_TX_DBOARD];
144
        std::string tx_subdev_in_use = tx_dboard[DBOARD_PROP_USED_SUBDEVS].as<prop_names_t>().at(0);
145
        _tx_subdev = tx_dboard[named_prop_t(DBOARD_PROP_SUBDEV, tx_subdev_in_use)];
146
    }
147

    
148
    ~simple_device_impl(void){
149
        /* NOP */
150
    }
151

    
152
    device::sptr get_device(void){
153
        return _dev;
154
    }
155

    
156
    std::string get_name(void){
157
        return _mboard[MBOARD_PROP_NAME].as<std::string>();
158
    }
159

    
160
    /*******************************************************************
161
     * Streaming
162
     ******************************************************************/
163
    void set_streaming(bool enb){
164
        _rx_ddc[std::string("enabled")] = enb;
165
    }
166

    
167
    bool get_streaming(void){
168
        return _rx_ddc[std::string("enabled")].as<bool>();
169
    }
170

    
171
    /*******************************************************************
172
     * RX methods
173
     ******************************************************************/
174
    void set_rx_rate(double rate){
175
        double samp_rate = _rx_ddc[std::string("rate")].as<double>();
176
        assert_has(get_rx_rates(), rate, "simple device rx rate");
177
        _rx_ddc[std::string("decim")] = size_t(samp_rate/rate);
178
    }
179

    
180
    double get_rx_rate(void){
181
        double samp_rate = _rx_ddc[std::string("rate")].as<double>();
182
        size_t decim = _rx_ddc[std::string("decim")].as<size_t>();
183
        return samp_rate/decim;
184
    }
185

    
186
    std::vector<double> get_rx_rates(void){
187
        return get_xx_rates(_rx_ddc[std::string("decims")], _rx_ddc[std::string("rate")]);
188
    }
189

    
190
    tune_result_t set_rx_freq(double target_freq){
191
        double lo_offset = 0.0;
192
        //if the local oscillator will be in the passband, use an offset
193
        if (_rx_subdev[SUBDEV_PROP_LO_INTERFERES].as<bool>()){
194
            lo_offset = get_rx_rate()*2.0;
195
        }
196
        return tune(target_freq, lo_offset, _rx_subdev, _rx_ddc, false/* not tx */);
197
    }
198

    
199
    std::vector<double> get_rx_freq_range(void){
200
        std::vector<double> range(2);
201
        boost::tie(range[0], range[1]) = \
202
            _rx_subdev[SUBDEV_PROP_FREQ_RANGE].as<freq_range_t>();
203
        return range;
204
    }
205

    
206
    void set_rx_gain(float gain){
207
        _rx_subdev[SUBDEV_PROP_GAIN] = gain;
208
    }
209

    
210
    float get_rx_gain(void){
211
        return _rx_subdev[SUBDEV_PROP_GAIN].as<gain_t>();
212
    }
213

    
214
    std::vector<float> get_rx_gain_range(void){
215
        std::vector<float> range(3);
216
        boost::tie(range[0], range[1], range[2]) = \
217
            _rx_subdev[SUBDEV_PROP_GAIN_RANGE].as<gain_range_t>();
218
        return range;
219
    }
220

    
221
    void set_rx_antenna(const std::string &ant){
222
        _rx_subdev[SUBDEV_PROP_ANTENNA] = ant;
223
    }
224

    
225
    std::string get_rx_antenna(void){
226
        return _rx_subdev[SUBDEV_PROP_ANTENNA].as<std::string>();
227
    }
228

    
229
    std::vector<std::string> get_rx_antennas(void){
230
        return _rx_subdev[SUBDEV_PROP_ANTENNA_NAMES].as<std::vector<std::string> >();
231
    }
232

    
233
    /*******************************************************************
234
     * TX methods
235
     ******************************************************************/
236
    void set_tx_rate(double rate){
237
        double samp_rate = _tx_duc[std::string("rate")].as<double>();
238
        assert_has(get_tx_rates(), rate, "simple device tx rate");
239
        _tx_duc[std::string("interp")] = size_t(samp_rate/rate);
240
    }
241

    
242
    double get_tx_rate(void){
243
        double samp_rate = _tx_duc[std::string("rate")].as<double>();
244
        size_t interp = _tx_duc[std::string("interp")].as<size_t>();
245
        return samp_rate/interp;
246
    }
247

    
248
    std::vector<double> get_tx_rates(void){
249
        return get_xx_rates(_tx_duc[std::string("interps")], _tx_duc[std::string("rate")]);
250
    }
251

    
252
    tune_result_t set_tx_freq(double target_freq){
253
        double lo_offset = 0.0;
254
        //if the local oscillator will be in the passband, use an offset
255
        if (_tx_subdev[SUBDEV_PROP_LO_INTERFERES].as<bool>()){
256
            lo_offset = get_tx_rate()*2.0;
257
        }
258
        return tune(target_freq, lo_offset, _tx_subdev, _tx_duc, true/* is tx */);
259
    }
260

    
261
    std::vector<double> get_tx_freq_range(void){
262
        std::vector<double> range(2);
263
        boost::tie(range[0], range[1]) = \
264
            _tx_subdev[SUBDEV_PROP_FREQ_RANGE].as<freq_range_t>();
265
        return range;
266
    }
267

    
268
    void set_tx_gain(float gain){
269
        _tx_subdev[SUBDEV_PROP_GAIN] = gain;
270
    }
271

    
272
    float get_tx_gain(void){
273
        return _tx_subdev[SUBDEV_PROP_GAIN].as<gain_t>();
274
    }
275

    
276
    std::vector<float> get_tx_gain_range(void){
277
        std::vector<float> range(3);
278
        boost::tie(range[0], range[1], range[2]) = \
279
            _tx_subdev[SUBDEV_PROP_GAIN_RANGE].as<gain_range_t>();
280
        return range;
281
    }
282

    
283
    void set_tx_antenna(const std::string &ant){
284
        _tx_subdev[SUBDEV_PROP_ANTENNA] = ant;
285
    }
286

    
287
    std::string get_tx_antenna(void){
288
        return _tx_subdev[SUBDEV_PROP_ANTENNA].as<std::string>();
289
    }
290

    
291
    std::vector<std::string> get_tx_antennas(void){
292
        return _tx_subdev[SUBDEV_PROP_ANTENNA_NAMES].as<std::vector<std::string> >();
293
    }
294

    
295
private:
296
    device::sptr _dev;
297
    wax::obj _mboard;
298
    wax::obj _rx_ddc;
299
    wax::obj _tx_duc;
300
    wax::obj _rx_subdev;
301
    wax::obj _tx_subdev;
302
};
303

    
304
/***********************************************************************
305
 * The Make Function
306
 **********************************************************************/
307
simple_device::sptr simple_device::make(const std::string &args){
308
    return sptr(new simple_device_impl(args_to_device_addr(args)));
309
}