Statistics
| Branch: | Tag: | Revision:

root / gr-uhd / lib / gr_uhd_usrp_sink.cc @ 11d61c53

History | View | Annotate | Download (16.4 kB)

1
/*
2
 * Copyright 2010-2012 Free Software Foundation, Inc.
3
 * 
4
 * This file is part of GNU Radio
5
 * 
6
 * GNU Radio is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3, or (at your option)
9
 * any later version.
10
 * 
11
 * GNU Radio is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with GNU Radio; see the file COPYING.  If not, write to
18
 * the Free Software Foundation, Inc., 51 Franklin Street,
19
 * Boston, MA 02110-1301, USA.
20
 */
21
22
#include <gr_uhd_usrp_sink.h>
23
#include <gr_io_signature.h>
24
#include <stdexcept>
25
#include <boost/make_shared.hpp>
26
#include "gr_uhd_common.h"
27
28
static const pmt::pmt_t SOB_KEY = pmt::pmt_string_to_symbol("tx_sob");
29
static const pmt::pmt_t EOB_KEY = pmt::pmt_string_to_symbol("tx_eob");
30
static const pmt::pmt_t TIME_KEY = pmt::pmt_string_to_symbol("tx_time");
31
32
#include <uhd/convert.hpp>
33
inline gr_io_signature_sptr args_to_io_sig(const uhd::stream_args_t &args){
34
    const size_t nchan = std::max<size_t>(args.channels.size(), 1);
35
    #ifdef GR_UHD_USE_STREAM_API
36
        const size_t size = uhd::convert::get_bytes_per_item(args.cpu_format);
37
    #else
38
        size_t size = 0;
39
        if (args.cpu_format == "fc32") size = 8;
40
        if (args.cpu_format == "sc16") size = 4;
41
    #endif
42
    return gr_make_io_signature(nchan, nchan, size);
43
}
44
45
/***********************************************************************
46
 * UHD Multi USRP Sink Impl
47
 **********************************************************************/
48
class uhd_usrp_sink_impl : public uhd_usrp_sink{
49
public:
50
    uhd_usrp_sink_impl(
51
        const uhd::device_addr_t &device_addr,
52
        const uhd::stream_args_t &stream_args
53
    ):
54
        gr_sync_block(
55
            "gr uhd usrp sink",
56
            args_to_io_sig(stream_args),
57
            gr_make_io_signature(0, 0, 0)
58
        ),
59
        _stream_args(stream_args),
60
        _nchan(std::max<size_t>(1, stream_args.channels.size())),
61
        _stream_now(_nchan == 1),
62
        _start_time_set(false)
63
    {
64
        if (stream_args.cpu_format == "fc32") _type = boost::make_shared<uhd::io_type_t>(uhd::io_type_t::COMPLEX_FLOAT32);
65
        if (stream_args.cpu_format == "sc16") _type = boost::make_shared<uhd::io_type_t>(uhd::io_type_t::COMPLEX_INT16);
66
        _dev = uhd::usrp::multi_usrp::make(device_addr);
67
    }
68
69
    uhd::dict<std::string, std::string> get_usrp_info(size_t chan){
70
        #ifdef UHD_USRP_MULTI_USRP_GET_USRP_INFO_API
71
        return _dev->get_usrp_tx_info(chan);
72
        #else
73
        throw std::runtime_error("not implemented in this version");
74
        #endif
75
    }
76
77
    void set_subdev_spec(const std::string &spec, size_t mboard){
78
        return _dev->set_tx_subdev_spec(spec, mboard);
79
    }
80
81
    std::string get_subdev_spec(size_t mboard){
82
        return _dev->get_tx_subdev_spec(mboard).to_string();
83
    }
84
85
    void set_samp_rate(double rate){
86
        _dev->set_tx_rate(rate);
87
        _sample_rate = this->get_samp_rate();
88
    }
89
90
    double get_samp_rate(void){
91
        return _dev->get_tx_rate();
92
    }
93
94
    uhd::meta_range_t get_samp_rates(void){
95
        #ifdef UHD_USRP_MULTI_USRP_GET_RATES_API
96
        return _dev->get_tx_rates();
97
        #else
98
        throw std::runtime_error("not implemented in this version");
99
        #endif
100
    }
101
102
    uhd::tune_result_t set_center_freq(
103
        const uhd::tune_request_t tune_request, size_t chan
104
    ){
105
        return _dev->set_tx_freq(tune_request, chan);
106
    }
107
108
    double get_center_freq(size_t chan){
109
        return _dev->get_tx_freq(chan);
110
    }
111
112
    uhd::freq_range_t get_freq_range(size_t chan){
113
        return _dev->get_tx_freq_range(chan);
114
    }
115
116
    void set_gain(double gain, size_t chan){
117
        return _dev->set_tx_gain(gain, chan);
118
    }
119
120
    void set_gain(double gain, const std::string &name, size_t chan){
121
        return _dev->set_tx_gain(gain, name, chan);
122
    }
123
124
    double get_gain(size_t chan){
125
        return _dev->get_tx_gain(chan);
126
    }
127
128
    double get_gain(const std::string &name, size_t chan){
129
        return _dev->get_tx_gain(name, chan);
130
    }
131
132
    std::vector<std::string> get_gain_names(size_t chan){
133
        return _dev->get_tx_gain_names(chan);
134
    }
135
136
    uhd::gain_range_t get_gain_range(size_t chan){
137
        return _dev->get_tx_gain_range(chan);
138
    }
139
140
    uhd::gain_range_t get_gain_range(const std::string &name, size_t chan){
141
        return _dev->get_tx_gain_range(name, chan);
142
    }
143
144
    void set_antenna(const std::string &ant, size_t chan){
145
        return _dev->set_tx_antenna(ant, chan);
146
    }
147
148
    std::string get_antenna(size_t chan){
149
        return _dev->get_tx_antenna(chan);
150
    }
151
152
    std::vector<std::string> get_antennas(size_t chan){
153
        return _dev->get_tx_antennas(chan);
154
    }
155
156
    void set_bandwidth(double bandwidth, size_t chan){
157
        return _dev->set_tx_bandwidth(bandwidth, chan);
158
    }
159
160
    void set_dc_offset(const std::complex<double> &offset, size_t chan){
161
        #ifdef UHD_USRP_MULTI_USRP_FRONTEND_CAL_API
162
        return _dev->set_tx_dc_offset(offset, chan);
163
        #else
164
        throw std::runtime_error("not implemented in this version");
165
        #endif
166
    }
167
168
    void set_iq_balance(const std::complex<double> &correction, size_t chan){
169
        #ifdef UHD_USRP_MULTI_USRP_FRONTEND_CAL_API
170
        return _dev->set_tx_iq_balance(correction, chan);
171
        #else
172
        throw std::runtime_error("not implemented in this version");
173
        #endif
174
    }
175
176
    uhd::sensor_value_t get_sensor(const std::string &name, size_t chan){
177
        return _dev->get_tx_sensor(name, chan);
178
    }
179
180
    std::vector<std::string> get_sensor_names(size_t chan){
181
        return _dev->get_tx_sensor_names(chan);
182
    }
183
184
    uhd::sensor_value_t get_mboard_sensor(const std::string &name, size_t mboard){
185
        return _dev->get_mboard_sensor(name, mboard);
186
    }
187
188
    std::vector<std::string> get_mboard_sensor_names(size_t mboard){
189
        return _dev->get_mboard_sensor_names(mboard);
190
    }
191
192
    void set_clock_config(const uhd::clock_config_t &clock_config, size_t mboard){
193
        return _dev->set_clock_config(clock_config, mboard);
194
    }
195
196
    void set_time_source(const std::string &source, const size_t mboard){
197
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
198
        return _dev->set_time_source(source, mboard);
199
        #else
200
        throw std::runtime_error("not implemented in this version");
201
        #endif
202
    }
203
204
    std::string get_time_source(const size_t mboard){
205
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
206
        return _dev->get_time_source(mboard);
207
        #else
208
        throw std::runtime_error("not implemented in this version");
209
        #endif
210
    }
211
212
    std::vector<std::string> get_time_sources(const size_t mboard){
213
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
214
        return _dev->get_time_sources(mboard);
215
        #else
216
        throw std::runtime_error("not implemented in this version");
217
        #endif
218
    }
219
220
    void set_clock_source(const std::string &source, const size_t mboard){
221
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
222
        return _dev->set_clock_source(source, mboard);
223
        #else
224
        throw std::runtime_error("not implemented in this version");
225
        #endif
226
    }
227
228
    std::string get_clock_source(const size_t mboard){
229
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
230
        return _dev->get_clock_source(mboard);
231
        #else
232
        throw std::runtime_error("not implemented in this version");
233
        #endif
234
    }
235
236
    std::vector<std::string> get_clock_sources(const size_t mboard){
237
        #ifdef UHD_USRP_MULTI_USRP_REF_SOURCES_API
238
        return _dev->get_clock_sources(mboard);
239
        #else
240
        throw std::runtime_error("not implemented in this version");
241
        #endif
242
    }
243
244
    double get_clock_rate(size_t mboard){
245
        return _dev->get_master_clock_rate(mboard);
246
    }
247
248
    void set_clock_rate(double rate, size_t mboard){
249
        return _dev->set_master_clock_rate(rate, mboard);
250
    }
251
252
    uhd::time_spec_t get_time_now(size_t mboard = 0){
253
        return _dev->get_time_now(mboard);
254
    }
255
256
    uhd::time_spec_t get_time_last_pps(size_t mboard){
257
        return _dev->get_time_last_pps(mboard);
258
    }
259
260
    void set_time_now(const uhd::time_spec_t &time_spec, size_t mboard){
261
        return _dev->set_time_now(time_spec, mboard);
262
    }
263
264
    void set_time_next_pps(const uhd::time_spec_t &time_spec){
265
        return _dev->set_time_next_pps(time_spec);
266
    }
267
268
    void set_time_unknown_pps(const uhd::time_spec_t &time_spec){
269
        return _dev->set_time_unknown_pps(time_spec);
270
    }
271
272
    void set_command_time(const uhd::time_spec_t &time_spec, size_t mboard){
273
        #ifdef UHD_USRP_MULTI_USRP_COMMAND_TIME_API
274
        return _dev->set_command_time(time_spec, mboard);
275
        #else
276
        throw std::runtime_error("not implemented in this version");
277
        #endif
278
    }
279
280
    void clear_command_time(size_t mboard){
281
        #ifdef UHD_USRP_MULTI_USRP_COMMAND_TIME_API
282
        return _dev->clear_command_time(mboard);
283
        #else
284
        throw std::runtime_error("not implemented in this version");
285
        #endif
286
    }
287
288
    uhd::usrp::dboard_iface::sptr get_dboard_iface(size_t chan){
289
        return _dev->get_tx_dboard_iface(chan);
290
    }
291
292
    uhd::usrp::multi_usrp::sptr get_device(void){
293
        return _dev;
294
    }
295
296
    void set_user_register(const uint8_t addr, const uint32_t data, size_t mboard){
297
        #ifdef UHD_USRP_MULTI_USRP_USER_REGS_API
298
        _dev->set_user_register(addr, data, mboard);
299
        #else
300
        throw std::runtime_error("not implemented in this version");
301
        #endif
302
    }
303
304
305
/***********************************************************************
306
 * Work
307
 **********************************************************************/
308
    int work(
309
        int noutput_items,
310
        gr_vector_const_void_star &input_items,
311
        gr_vector_void_star &output_items
312
    ){
313
        int ninput_items = noutput_items; //cuz its a sync block
314
315
        //send a mid-burst packet with time spec
316
        _metadata.start_of_burst = false;
317
        _metadata.end_of_burst = false;
318
319
        //collect tags in this work()
320
        const uint64_t samp0_count = nitems_read(0);
321
        get_tags_in_range(_tags, 0, samp0_count, samp0_count + ninput_items);
322
        if (not _tags.empty()) this->tag_work(ninput_items);
323
324
        #ifdef GR_UHD_USE_STREAM_API
325
        //send all ninput_items with metadata
326
        const size_t num_sent = _tx_stream->send(
327
            input_items, ninput_items, _metadata, 1.0
328
        );
329
        #else
330
        const size_t num_sent = _dev->get_device()->send(
331
            input_items, ninput_items, _metadata,
332
            *_type, uhd::device::SEND_MODE_FULL_BUFF, 1.0
333
        );
334
        #endif
335
336
        //increment the timespec by the number of samples sent
337
        _metadata.time_spec += uhd::time_spec_t(0, num_sent, _sample_rate);
338
        return num_sent;
339
    }
340
341
/***********************************************************************
342
 * Tag Work
343
 **********************************************************************/
344
    inline void tag_work(int &ninput_items){
345
        //the for loop below assumes tags sorted by count low -> high
346
        std::sort(_tags.begin(), _tags.end(), gr_tag_t::offset_compare);
347
348
        //extract absolute sample counts
349
        const gr_tag_t &tag0 = _tags.front();
350
        const uint64_t tag0_count = tag0.offset;
351
        const uint64_t samp0_count = this->nitems_read(0);
352
353
        //only transmit nsamples from 0 to the first tag
354
        //this ensures that the next work starts on a tag
355
        if (samp0_count != tag0_count){
356
            ninput_items = tag0_count - samp0_count;
357
            return;
358
        }
359
360
        //time will not be set unless a time tag is found
361
        _metadata.has_time_spec = false;
362
363
        //process all of the tags found with the same count as tag0
364
        BOOST_FOREACH(const gr_tag_t &my_tag, _tags){
365
            const uint64_t my_tag_count = my_tag.offset;
366
            const pmt::pmt_t &key = my_tag.key;
367
            const pmt::pmt_t &value = my_tag.value;
368
369
            //determine how many samples to send...
370
            //from zero until the next tag or end of work
371
            if (my_tag_count != tag0_count){
372
                ninput_items = my_tag_count - samp0_count;
373
                break;
374
            }
375
376
            //handle end of burst with a mini end of burst packet
377
            else if (pmt::pmt_equal(key, EOB_KEY)){
378
                _metadata.end_of_burst = pmt::pmt_to_bool(value);
379
                ninput_items = 1;
380
                return;
381
            }
382
383
            //set the start of burst flag in the metadata
384
            else if (pmt::pmt_equal(key, SOB_KEY)){
385
                _metadata.start_of_burst = pmt::pmt_to_bool(value);
386
            }
387
388
            //set the time specification in the metadata
389
            else if (pmt::pmt_equal(key, TIME_KEY)){
390
                _metadata.has_time_spec = true;
391
                _metadata.time_spec = uhd::time_spec_t(
392
                    pmt::pmt_to_uint64(pmt_tuple_ref(value, 0)),
393
                    pmt::pmt_to_double(pmt_tuple_ref(value, 1))
394
                );
395
            }
396
        }
397
    }
398
399
    void set_start_time(const uhd::time_spec_t &time){
400
        _start_time = time;
401
        _start_time_set = true;
402
        _stream_now = false;
403
    }
404
405
    //Send an empty start-of-burst packet to begin streaming.
406
    //Set at a time in the near future to avoid late packets.
407
    bool start(void){
408
        #ifdef GR_UHD_USE_STREAM_API
409
        _tx_stream = _dev->get_tx_stream(_stream_args);
410
        #endif
411
412
        _metadata.start_of_burst = true;
413
        _metadata.end_of_burst = false;
414
        _metadata.has_time_spec = not _stream_now;
415
        if (_start_time_set){
416
            _start_time_set = false; //cleared for next run
417
            _metadata.time_spec = _start_time;
418
        }
419
        else{
420
            _metadata.time_spec = get_time_now() + uhd::time_spec_t(0.01);
421
        }
422
423
        #ifdef GR_UHD_USE_STREAM_API
424
        _tx_stream->send(
425
            gr_vector_const_void_star(_nchan), 0, _metadata, 1.0
426
        );
427
        #else
428
        _dev->get_device()->send(
429
            gr_vector_const_void_star(_nchan), 0, _metadata,
430
            *_type, uhd::device::SEND_MODE_ONE_PACKET, 1.0
431
        );
432
        #endif
433
        return true;
434
    }
435
436
    //Send an empty end-of-burst packet to end streaming.
437
    //Ending the burst avoids an underflow error on stop.
438
    bool stop(void){
439
        _metadata.start_of_burst = false;
440
        _metadata.end_of_burst = true;
441
        _metadata.has_time_spec = false;
442
443
        #ifdef GR_UHD_USE_STREAM_API
444
        _tx_stream->send(gr_vector_const_void_star(_nchan), 0, _metadata, 1.0);
445
        #else
446
        _dev->get_device()->send(
447
            gr_vector_const_void_star(_nchan), 0, _metadata,
448
            *_type, uhd::device::SEND_MODE_ONE_PACKET, 1.0
449
        );
450
        #endif
451
        return true;
452
    }
453
454
private:
455
    uhd::usrp::multi_usrp::sptr _dev;
456
    const uhd::stream_args_t _stream_args;
457
    boost::shared_ptr<uhd::io_type_t> _type;
458
    #ifdef GR_UHD_USE_STREAM_API
459
    uhd::tx_streamer::sptr _tx_stream;
460
    #endif
461
    size_t _nchan;
462
    bool _stream_now;
463
    uhd::tx_metadata_t _metadata;
464
    double _sample_rate;
465
466
    uhd::time_spec_t _start_time;
467
    bool _start_time_set;
468
469
    //stream tags related stuff
470
    std::vector<gr_tag_t> _tags;
471
};
472
473
/***********************************************************************
474
 * Make UHD Multi USRP Sink
475
 **********************************************************************/
476
boost::shared_ptr<uhd_usrp_sink> uhd_make_usrp_sink(
477
    const uhd::device_addr_t &device_addr,
478
    const uhd::io_type_t &io_type,
479
    size_t num_channels
480
){
481
    //fill in the streamer args
482
    uhd::stream_args_t stream_args;
483
    switch(io_type.tid){
484
    case uhd::io_type_t::COMPLEX_FLOAT32: stream_args.cpu_format = "fc32"; break;
485
    case uhd::io_type_t::COMPLEX_INT16: stream_args.cpu_format = "sc16"; break;
486
    default: throw std::runtime_error("only complex float and shorts known to work");
487
    }
488
    stream_args.otw_format = "sc16"; //only sc16 known to work
489
    for (size_t chan = 0; chan < num_channels; chan++)
490
        stream_args.channels.push_back(chan); //linear mapping
491
492
    return uhd_make_usrp_sink(device_addr, stream_args);
493
}
494
495
boost::shared_ptr<uhd_usrp_sink> uhd_make_usrp_sink(
496
    const uhd::device_addr_t &device_addr,
497
    const uhd::stream_args_t &stream_args
498
){
499
    gr_uhd_check_abi();
500
    return boost::shared_ptr<uhd_usrp_sink>(
501
        new uhd_usrp_sink_impl(device_addr, stream_args)
502
    );
503
}