1
1
/*
2
- * Copyright 2010-2011 Free Software Foundation, Inc.
2
+ * Copyright 2010-2012 Free Software Foundation, Inc.
3
3
*
4
4
* This file is part of GNU Radio
5
5
*
25
25
#include < iostream>
26
26
#include < boost/format.hpp>
27
27
#include < boost/make_shared.hpp>
28
+ #include " gr_uhd_common.h"
28
29
29
30
static const pmt::pmt_t TIME_KEY = pmt::pmt_string_to_symbol(" rx_time" );
30
31
@@ -58,7 +59,8 @@ class uhd_usrp_source_impl : public uhd_usrp_source{
58
59
_stream_args(stream_args),
59
60
_nchan(std::max<size_t >(1 , stream_args.channels.size())),
60
61
_stream_now(_nchan == 1 ),
61
- _tag_now(false )
62
+ _tag_now(false ),
63
+ _start_time_set(false )
62
64
{
63
65
if (stream_args.cpu_format == " fc32" ) _type = boost::make_shared<uhd::io_type_t >(uhd::io_type_t ::COMPLEX_FLOAT32);
64
66
if (stream_args.cpu_format == " sc16" ) _type = boost::make_shared<uhd::io_type_t >(uhd::io_type_t ::COMPLEX_INT16);
@@ -371,6 +373,12 @@ class uhd_usrp_source_impl : public uhd_usrp_source{
371
373
return num_samps;
372
374
}
373
375
376
+ void set_start_time (const uhd::time_spec_t &time){
377
+ _start_time = time ;
378
+ _start_time_set = true ;
379
+ _stream_now = false ;
380
+ }
381
+
374
382
bool start (void ){
375
383
#ifdef GR_UHD_USE_STREAM_API
376
384
_rx_stream = _dev->get_rx_stream (_stream_args);
@@ -380,7 +388,13 @@ class uhd_usrp_source_impl : public uhd_usrp_source{
380
388
static const double reasonable_delay = 0.1 ; // order of magnitude over RTT
381
389
uhd::stream_cmd_t stream_cmd (uhd::stream_cmd_t ::STREAM_MODE_START_CONTINUOUS);
382
390
stream_cmd.stream_now = _stream_now;
383
- stream_cmd.time_spec = get_time_now () + uhd::time_spec_t (reasonable_delay);
391
+ if (_start_time_set){
392
+ _start_time_set = false ; // cleared for next run
393
+ stream_cmd.time_spec = _start_time;
394
+ }
395
+ else {
396
+ stream_cmd.time_spec = get_time_now () + uhd::time_spec_t (reasonable_delay);
397
+ }
384
398
_dev->issue_stream_cmd (stream_cmd);
385
399
_tag_now = true ;
386
400
return true ;
@@ -418,17 +432,48 @@ class uhd_usrp_source_impl : public uhd_usrp_source{
418
432
}
419
433
420
434
std::vector<std::complex<float > > finite_acquisition (const size_t nsamps){
435
+ if (_nchan != 1 ) throw std::runtime_error (" finite_acquisition: usrp source has multiple channels, call finite_acquisition_v" );
436
+ return finite_acquisition_v (nsamps).front ();
437
+ }
438
+
439
+ std::vector<std::vector<std::complex<float > > > finite_acquisition_v (const size_t nsamps){
421
440
#ifdef GR_UHD_USE_STREAM_API
441
+
442
+ // kludgy way to ensure rx streamer exsists
443
+ if (!_rx_stream){
444
+ this ->start ();
445
+ this ->stop ();
446
+ }
447
+
448
+ // flush so there is no queued-up data
449
+ this ->flush ();
450
+
451
+ // create a multi-dimensional container to hold an array of sample buffers
452
+ std::vector<std::vector<std::complex<float > > > samps (_nchan, std::vector<std::complex<float > >(nsamps));
453
+
454
+ // load the void* vector of buffer pointers
455
+ std::vector<void *> buffs (_nchan);
456
+ for (size_t i = 0 ; i < _nchan; i++){
457
+ buffs[i] = &samps[i].front ();
458
+ }
459
+
460
+ // tell the device to stream a finite amount
422
461
uhd::stream_cmd_t cmd (uhd::stream_cmd_t ::STREAM_MODE_NUM_SAMPS_AND_DONE);
423
462
cmd.num_samps = nsamps;
424
- cmd.stream_now = true ;
463
+ cmd.stream_now = _stream_now;
464
+ static const double reasonable_delay = 0.1 ; // order of magnitude over RTT
465
+ cmd.time_spec = get_time_now () + uhd::time_spec_t (reasonable_delay);
425
466
_dev->issue_stream_cmd (cmd);
426
467
427
- std::vector<std::complex< float > > samps (nsamps);
468
+ // receive samples until timeout
428
469
const size_t actual_num_samps = _rx_stream->recv (
429
- &samps. front () , nsamps, _metadata, 0.1
470
+ buffs , nsamps, _metadata, 1.0
430
471
);
431
- samps.resize (actual_num_samps);
472
+
473
+ // resize the resulting sample buffers
474
+ for (size_t i = 0 ; i < _nchan; i++){
475
+ samps[i].resize (actual_num_samps);
476
+ }
432
477
433
478
return samps;
434
479
#else
@@ -448,6 +493,9 @@ class uhd_usrp_source_impl : public uhd_usrp_source{
448
493
bool _stream_now, _tag_now;
449
494
uhd::rx_metadata_t _metadata;
450
495
pmt::pmt_t _id;
496
+
497
+ uhd::time_spec_t _start_time;
498
+ bool _start_time_set;
451
499
};
452
500
453
501
@@ -477,6 +525,7 @@ boost::shared_ptr<uhd_usrp_source> uhd_make_usrp_source(
477
525
const uhd::device_addr_t &device_addr,
478
526
const uhd::stream_args_t &stream_args
479
527
){
528
+ gr_uhd_check_abi ();
480
529
return boost::shared_ptr<uhd_usrp_source>(
481
530
new uhd_usrp_source_impl (device_addr, stream_args)
482
531
);
0 commit comments