2011-05-26 21:52:12 +00:00
|
|
|
//
|
2014-01-24 15:17:42 +00:00
|
|
|
// Copyright 2010-2011,2014 Ettus Research LLC
|
2019-11-22 23:41:04 +00:00
|
|
|
// Copyright 2018,2019 Ettus Research, a National Instruments Company
|
2011-05-26 21:52:12 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2011-05-26 21:52:12 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <uhd/usrp/multi_usrp.hpp>
|
2019-01-14 18:35:25 +00:00
|
|
|
#include <uhd/utils/safe_main.hpp>
|
|
|
|
|
#include <uhd/utils/thread.hpp>
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
#include <boost/format.hpp>
|
2011-05-26 21:52:12 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
#include <boost/thread/thread.hpp>
|
2019-01-14 18:35:25 +00:00
|
|
|
#include <complex>
|
2011-05-26 21:52:12 +00:00
|
|
|
#include <csignal>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
|
|
static bool stop_signal_called = false;
|
2019-01-14 18:35:25 +00:00
|
|
|
void sig_int_handler(int)
|
|
|
|
|
{
|
|
|
|
|
stop_signal_called = true;
|
|
|
|
|
}
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
int UHD_SAFE_MAIN(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
// variables to be set by po
|
2019-11-22 23:41:04 +00:00
|
|
|
std::string args, channel_list, subdev, ref;
|
|
|
|
|
double seconds_in_future, rate, freq, rep_rate, gain, lo_offset, bw;
|
2011-05-26 21:52:12 +00:00
|
|
|
size_t total_num_samps;
|
|
|
|
|
float ampl;
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// setup the program options
|
2011-05-26 21:52:12 +00:00
|
|
|
po::options_description desc("Allowed options");
|
2019-01-10 23:19:48 +00:00
|
|
|
// clang-format off
|
2011-05-26 21:52:12 +00:00
|
|
|
desc.add_options()
|
|
|
|
|
("help", "help message")
|
|
|
|
|
("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args")
|
|
|
|
|
("secs", po::value<double>(&seconds_in_future)->default_value(1.5), "delay before first burst")
|
2011-05-27 19:32:30 +00:00
|
|
|
("repeat", "repeat burst")
|
|
|
|
|
("rep-delay", po::value<double>(&rep_rate)->default_value(0.5), "delay between bursts")
|
2011-05-26 21:52:12 +00:00
|
|
|
("nsamps", po::value<size_t>(&total_num_samps)->default_value(10000), "total number of samples to transmit")
|
|
|
|
|
("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of outgoing samples")
|
|
|
|
|
("ampl", po::value<float>(&l)->default_value(float(0.3)), "amplitude of each sample")
|
|
|
|
|
("freq", po::value<double>(&freq)->default_value(0), "center frequency")
|
2011-05-26 23:50:31 +00:00
|
|
|
("gain", po::value<double>(&gain)->default_value(0), "gain")
|
2011-05-26 21:52:12 +00:00
|
|
|
("dilv", "specify to disable inner-loop verbose")
|
2013-08-26 20:51:30 +00:00
|
|
|
("channels", po::value<std::string>(&channel_list)->default_value("0"), "which channel(s) to use (specify \"0\", \"1\", \"0,1\", etc")
|
2014-01-24 15:17:42 +00:00
|
|
|
("int-n", "tune USRP with integer-n tuning")
|
2019-11-22 23:41:04 +00:00
|
|
|
("subdev", po::value<std::string>(&subdev), "subdevice specification")
|
|
|
|
|
("ref", po::value<std::string>(&ref)->default_value("internal"), "reference source (internal, external, mimo)")
|
|
|
|
|
("lo-offset", po::value<double>(&lo_offset)->default_value(0.0),
|
|
|
|
|
"Offset for frontend LO in Hz (optional)")
|
|
|
|
|
("bw", po::value<double>(&bw), "analog frontend filter bandwidth in Hz")
|
2011-05-26 21:52:12 +00:00
|
|
|
;
|
2019-01-10 23:19:48 +00:00
|
|
|
// clang-format on
|
2011-05-26 21:52:12 +00:00
|
|
|
po::variables_map vm;
|
|
|
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
|
|
|
po::notify(vm);
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// print the help message
|
|
|
|
|
if (vm.count("help")) {
|
2011-05-26 21:52:12 +00:00
|
|
|
std::cout << boost::format("UHD TX Timed Samples %s") % desc << std::endl;
|
|
|
|
|
return ~0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool verbose = vm.count("dilv") == 0;
|
2019-01-14 18:35:25 +00:00
|
|
|
bool repeat = vm.count("repeat") != 0;
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// create a usrp device
|
2011-05-26 21:52:12 +00:00
|
|
|
std::cout << std::endl;
|
2019-01-14 18:35:25 +00:00
|
|
|
std::cout << boost::format("Creating the usrp device with: %s...") % args
|
|
|
|
|
<< std::endl;
|
2011-05-26 21:52:12 +00:00
|
|
|
uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
|
2019-11-22 23:41:04 +00:00
|
|
|
|
|
|
|
|
// Lock mboard clocks
|
|
|
|
|
if (vm.count("ref")) {
|
|
|
|
|
usrp->set_clock_source(ref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// always select the subdevice first, the channel mapping affects the other settings
|
|
|
|
|
if (vm.count("subdev"))
|
|
|
|
|
usrp->set_tx_subdev_spec(subdev);
|
|
|
|
|
|
2011-05-26 21:52:12 +00:00
|
|
|
std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// detect which channels to use
|
2013-08-26 20:51:30 +00:00
|
|
|
std::vector<std::string> channel_strings;
|
|
|
|
|
std::vector<size_t> channel_nums;
|
|
|
|
|
boost::split(channel_strings, channel_list, boost::is_any_of("\"',"));
|
2019-01-14 18:35:25 +00:00
|
|
|
for (size_t ch = 0; ch < channel_strings.size(); ch++) {
|
2017-06-28 02:06:50 +00:00
|
|
|
size_t chan = std::stoi(channel_strings[ch]);
|
2019-01-14 18:35:25 +00:00
|
|
|
if (chan >= usrp->get_tx_num_channels()) {
|
2013-08-26 20:51:30 +00:00
|
|
|
throw std::runtime_error("Invalid channel(s) specified.");
|
2019-01-14 18:35:25 +00:00
|
|
|
} else
|
|
|
|
|
channel_nums.push_back(std::stoi(channel_strings[ch]));
|
2013-08-26 20:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// set the tx sample rate
|
|
|
|
|
std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate / 1e6) << std::endl;
|
2011-05-26 21:52:12 +00:00
|
|
|
usrp->set_tx_rate(rate);
|
2019-01-14 18:35:25 +00:00
|
|
|
std::cout << boost::format("Actual TX Rate: %f Msps...") % (usrp->get_tx_rate() / 1e6)
|
|
|
|
|
<< std::endl
|
|
|
|
|
<< std::endl;
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-11-22 23:41:04 +00:00
|
|
|
// set the center frequency
|
|
|
|
|
if (not vm.count("freq")) {
|
|
|
|
|
std::cerr << "Please specify the center frequency with --freq" << std::endl;
|
|
|
|
|
return ~0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
std::cout << boost::format("Setting TX Freq: %f MHz...") % (freq / 1e6) << std::endl;
|
2019-11-22 23:41:04 +00:00
|
|
|
std::cout << boost::format("Setting TX LO Offset: %f MHz...") % (lo_offset / 1e6)
|
|
|
|
|
<< std::endl;
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
for (size_t i = 0; i < channel_nums.size(); i++) {
|
2019-11-22 23:41:04 +00:00
|
|
|
uhd::tune_request_t tune_request;
|
|
|
|
|
tune_request = uhd::tune_request_t(freq, lo_offset);
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
if (vm.count("int-n"))
|
|
|
|
|
tune_request.args = uhd::device_addr_t("mode_n=integer");
|
2014-01-24 15:17:42 +00:00
|
|
|
usrp->set_tx_freq(tune_request, channel_nums[i]);
|
|
|
|
|
}
|
2019-01-14 18:35:25 +00:00
|
|
|
std::cout << boost::format("Actual TX Freq: %f MHz...") % (usrp->get_tx_freq() / 1e6)
|
|
|
|
|
<< std::endl
|
|
|
|
|
<< std::endl;
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2011-05-26 23:50:31 +00:00
|
|
|
std::cout << boost::format("Setting TX Gain: %f...") % (gain) << std::endl;
|
2019-01-14 18:35:25 +00:00
|
|
|
for (size_t i = 0; i < channel_nums.size(); i++)
|
|
|
|
|
usrp->set_tx_gain(gain, channel_nums[i]);
|
|
|
|
|
std::cout << boost::format("Actual TX Gain: %f...") % (usrp->get_tx_gain())
|
|
|
|
|
<< std::endl
|
|
|
|
|
<< std::endl;
|
2011-05-26 23:50:31 +00:00
|
|
|
|
2019-11-22 23:41:04 +00:00
|
|
|
// set the analog frontend filter bandwidth
|
|
|
|
|
if (vm.count("bw")) {
|
|
|
|
|
std::cout << boost::format("Setting TX Bandwidth: %f MHz...") % (bw / 1e6)
|
|
|
|
|
<< std::endl;
|
|
|
|
|
usrp->set_tx_bandwidth(bw);
|
|
|
|
|
std::cout << boost::format("Actual TX Bandwidth: %f MHz...")
|
|
|
|
|
% (usrp->get_tx_bandwidth() / 1e6)
|
|
|
|
|
<< std::endl
|
|
|
|
|
<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-26 21:52:12 +00:00
|
|
|
std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
|
|
|
|
|
usrp->set_time_now(uhd::time_spec_t(0.0));
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// create a transmit streamer
|
|
|
|
|
uhd::stream_args_t stream_args("fc32"); // complex floats
|
|
|
|
|
stream_args.channels = channel_nums;
|
2011-10-06 16:25:54 +00:00
|
|
|
uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
|
2011-10-05 18:59:27 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// allocate buffer with data to send
|
2011-10-05 18:59:27 +00:00
|
|
|
const size_t spb = tx_stream->get_max_num_samps();
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
std::vector<std::complex<float>> buff(spb, std::complex<float>(ampl, ampl));
|
|
|
|
|
std::vector<std::complex<float>*> buffs(channel_nums.size(), &buff.front());
|
2011-05-26 21:52:12 +00:00
|
|
|
|
|
|
|
|
std::signal(SIGINT, &sig_int_handler);
|
2019-01-14 18:35:25 +00:00
|
|
|
if (repeat)
|
|
|
|
|
std::cout << "Press Ctrl + C to quit..." << std::endl;
|
2011-05-26 21:52:12 +00:00
|
|
|
|
|
|
|
|
double time_to_send = seconds_in_future;
|
|
|
|
|
|
2011-05-27 19:32:30 +00:00
|
|
|
do {
|
2019-01-14 18:35:25 +00:00
|
|
|
// setup metadata for the first packet
|
2011-05-26 21:52:12 +00:00
|
|
|
uhd::tx_metadata_t md;
|
|
|
|
|
md.start_of_burst = true;
|
2019-01-14 18:35:25 +00:00
|
|
|
md.end_of_burst = false;
|
|
|
|
|
md.has_time_spec = true;
|
|
|
|
|
md.time_spec = uhd::time_spec_t(time_to_send);
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// the first call to send() will block this many seconds before sending:
|
|
|
|
|
double timeout = std::max(rep_rate, seconds_in_future)
|
|
|
|
|
+ 0.1; // timeout (delay before transmit + padding)
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
size_t num_acc_samps = 0; // number of accumulated samples
|
|
|
|
|
while (num_acc_samps < total_num_samps) {
|
2016-08-02 21:23:06 +00:00
|
|
|
size_t samps_to_send = total_num_samps - num_acc_samps;
|
2019-01-14 18:35:25 +00:00
|
|
|
if (samps_to_send > spb) {
|
2016-08-02 21:23:06 +00:00
|
|
|
samps_to_send = spb;
|
|
|
|
|
} else {
|
|
|
|
|
md.end_of_burst = true;
|
|
|
|
|
}
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// send a single packet
|
|
|
|
|
size_t num_tx_samps = tx_stream->send(buffs, samps_to_send, md, timeout);
|
|
|
|
|
// do not use time spec for subsequent packets
|
|
|
|
|
md.has_time_spec = false;
|
2011-05-26 21:52:12 +00:00
|
|
|
md.start_of_burst = false;
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
if (num_tx_samps < samps_to_send) {
|
2016-08-02 21:23:06 +00:00
|
|
|
std::cerr << "Send timeout..." << std::endl;
|
2019-01-14 18:35:25 +00:00
|
|
|
if (stop_signal_called) {
|
2016-08-02 21:23:06 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
if (verbose) {
|
|
|
|
|
std::cout << boost::format("Sent packet: %u samples") % num_tx_samps
|
|
|
|
|
<< std::endl;
|
2016-08-02 21:23:06 +00:00
|
|
|
}
|
2011-05-26 21:52:12 +00:00
|
|
|
|
|
|
|
|
num_acc_samps += num_tx_samps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time_to_send += rep_rate;
|
|
|
|
|
|
|
|
|
|
std::cout << std::endl << "Waiting for async burst ACK... " << std::flush;
|
|
|
|
|
uhd::async_metadata_t async_md;
|
2016-08-02 21:23:06 +00:00
|
|
|
size_t acks = 0;
|
2019-01-14 18:35:25 +00:00
|
|
|
// loop through all messages for the ACK packets (may have underflow messages in
|
|
|
|
|
// queue)
|
|
|
|
|
while (acks < channel_nums.size()
|
|
|
|
|
and tx_stream->recv_async_msg(async_md, seconds_in_future)) {
|
|
|
|
|
if (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK) {
|
2016-08-02 21:23:06 +00:00
|
|
|
acks++;
|
|
|
|
|
}
|
2011-05-26 21:52:12 +00:00
|
|
|
}
|
2016-08-02 21:23:06 +00:00
|
|
|
std::cout << (acks == channel_nums.size() ? "success" : "fail") << std::endl;
|
2011-05-27 19:32:30 +00:00
|
|
|
} while (not stop_signal_called and repeat);
|
2011-05-26 21:52:12 +00:00
|
|
|
|
2019-01-14 18:35:25 +00:00
|
|
|
// finished
|
2011-05-26 21:52:12 +00:00
|
|
|
std::cout << std::endl << "Done!" << std::endl << std::endl;
|
|
|
|
|
|
2012-10-18 22:33:07 +00:00
|
|
|
return EXIT_SUCCESS;
|
2011-05-26 21:52:12 +00:00
|
|
|
}
|