1 //
2 // Copyright 2010-2011,2014 Ettus Research LLC
3 // Copyright 2018,2019 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 
8 #include <uhd/usrp/multi_usrp.hpp>
9 #include <uhd/utils/safe_main.hpp>
10 #include <uhd/utils/thread.hpp>
11 #include <boost/algorithm/string.hpp>
12 #include <boost/format.hpp>
13 #include <boost/program_options.hpp>
14 #include <boost/thread/thread.hpp>
15 #include <complex>
16 #include <csignal>
17 #include <iostream>
18 
19 namespace po = boost::program_options;
20 
21 static bool stop_signal_called = false;
sig_int_handler(int)22 void sig_int_handler(int)
23 {
24     stop_signal_called = true;
25 }
26 
UHD_SAFE_MAIN(int argc,char * argv[])27 int UHD_SAFE_MAIN(int argc, char* argv[])
28 {
29     // variables to be set by po
30     std::string args, channel_list, subdev, ref;
31     double seconds_in_future, rate, freq, rep_rate, gain, lo_offset, bw;
32     size_t total_num_samps;
33     float ampl;
34 
35     // setup the program options
36     po::options_description desc("Allowed options");
37     // clang-format off
38     desc.add_options()
39         ("help", "help message")
40         ("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args")
41         ("secs", po::value<double>(&seconds_in_future)->default_value(1.5), "delay before first burst")
42         ("repeat", "repeat burst")
43         ("rep-delay", po::value<double>(&rep_rate)->default_value(0.5), "delay between bursts")
44         ("nsamps", po::value<size_t>(&total_num_samps)->default_value(10000), "total number of samples to transmit")
45         ("rate", po::value<double>(&rate)->default_value(100e6/16), "rate of outgoing samples")
46         ("ampl", po::value<float>(&ampl)->default_value(float(0.3)), "amplitude of each sample")
47         ("freq", po::value<double>(&freq)->default_value(0), "center frequency")
48         ("gain", po::value<double>(&gain)->default_value(0), "gain")
49         ("dilv", "specify to disable inner-loop verbose")
50         ("channels", po::value<std::string>(&channel_list)->default_value("0"), "which channel(s) to use (specify \"0\", \"1\", \"0,1\", etc")
51         ("int-n", "tune USRP with integer-n tuning")
52         ("subdev", po::value<std::string>(&subdev), "subdevice specification")
53         ("ref", po::value<std::string>(&ref)->default_value("internal"), "reference source (internal, external, mimo)")
54         ("lo-offset", po::value<double>(&lo_offset)->default_value(0.0),
55             "Offset for frontend LO in Hz (optional)")
56         ("bw", po::value<double>(&bw), "analog frontend filter bandwidth in Hz")
57     ;
58     // clang-format on
59     po::variables_map vm;
60     po::store(po::parse_command_line(argc, argv, desc), vm);
61     po::notify(vm);
62 
63     // print the help message
64     if (vm.count("help")) {
65         std::cout << boost::format("UHD TX Timed Samples %s") % desc << std::endl;
66         return ~0;
67     }
68 
69     bool verbose = vm.count("dilv") == 0;
70     bool repeat  = vm.count("repeat") != 0;
71 
72     // create a usrp device
73     std::cout << std::endl;
74     std::cout << boost::format("Creating the usrp device with: %s...") % args
75               << std::endl;
76     uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
77 
78     // Lock mboard clocks
79     if (vm.count("ref")) {
80         usrp->set_clock_source(ref);
81     }
82 
83     // always select the subdevice first, the channel mapping affects the other settings
84     if (vm.count("subdev"))
85         usrp->set_tx_subdev_spec(subdev);
86 
87     std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
88 
89     // detect which channels to use
90     std::vector<std::string> channel_strings;
91     std::vector<size_t> channel_nums;
92     boost::split(channel_strings, channel_list, boost::is_any_of("\"',"));
93     for (size_t ch = 0; ch < channel_strings.size(); ch++) {
94         size_t chan = std::stoi(channel_strings[ch]);
95         if (chan >= usrp->get_tx_num_channels()) {
96             throw std::runtime_error("Invalid channel(s) specified.");
97         } else
98             channel_nums.push_back(std::stoi(channel_strings[ch]));
99     }
100 
101     // set the tx sample rate
102     std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate / 1e6) << std::endl;
103     usrp->set_tx_rate(rate);
104     std::cout << boost::format("Actual TX Rate: %f Msps...") % (usrp->get_tx_rate() / 1e6)
105               << std::endl
106               << std::endl;
107 
108     // set the center frequency
109     if (not vm.count("freq")) {
110         std::cerr << "Please specify the center frequency with --freq" << std::endl;
111         return ~0;
112     }
113 
114     std::cout << boost::format("Setting TX Freq: %f MHz...") % (freq / 1e6) << std::endl;
115     std::cout << boost::format("Setting TX LO Offset: %f MHz...") % (lo_offset / 1e6)
116               << std::endl;
117 
118     for (size_t i = 0; i < channel_nums.size(); i++) {
119         uhd::tune_request_t tune_request;
120         tune_request = uhd::tune_request_t(freq, lo_offset);
121 
122         if (vm.count("int-n"))
123             tune_request.args = uhd::device_addr_t("mode_n=integer");
124         usrp->set_tx_freq(tune_request, channel_nums[i]);
125     }
126     std::cout << boost::format("Actual TX Freq: %f MHz...") % (usrp->get_tx_freq() / 1e6)
127               << std::endl
128               << std::endl;
129 
130     std::cout << boost::format("Setting TX Gain: %f...") % (gain) << std::endl;
131     for (size_t i = 0; i < channel_nums.size(); i++)
132         usrp->set_tx_gain(gain, channel_nums[i]);
133     std::cout << boost::format("Actual TX Gain: %f...") % (usrp->get_tx_gain())
134               << std::endl
135               << std::endl;
136 
137     // set the analog frontend filter bandwidth
138     if (vm.count("bw")) {
139         std::cout << boost::format("Setting TX Bandwidth: %f MHz...") % (bw / 1e6)
140                   << std::endl;
141         usrp->set_tx_bandwidth(bw);
142         std::cout << boost::format("Actual TX Bandwidth: %f MHz...")
143                          % (usrp->get_tx_bandwidth() / 1e6)
144                   << std::endl
145                   << std::endl;
146     }
147 
148     std::cout << boost::format("Setting device timestamp to 0...") << std::endl;
149     usrp->set_time_now(uhd::time_spec_t(0.0));
150 
151     // create a transmit streamer
152     uhd::stream_args_t stream_args("fc32"); // complex floats
153     stream_args.channels             = channel_nums;
154     uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
155 
156     // allocate buffer with data to send
157     const size_t spb = tx_stream->get_max_num_samps();
158 
159     std::vector<std::complex<float>> buff(spb, std::complex<float>(ampl, ampl));
160     std::vector<std::complex<float>*> buffs(channel_nums.size(), &buff.front());
161 
162     std::signal(SIGINT, &sig_int_handler);
163     if (repeat)
164         std::cout << "Press Ctrl + C to quit..." << std::endl;
165 
166     double time_to_send = seconds_in_future;
167 
168     do {
169         // setup metadata for the first packet
170         uhd::tx_metadata_t md;
171         md.start_of_burst = true;
172         md.end_of_burst   = false;
173         md.has_time_spec  = true;
174         md.time_spec      = uhd::time_spec_t(time_to_send);
175 
176         // the first call to send() will block this many seconds before sending:
177         double timeout = std::max(rep_rate, seconds_in_future)
178                          + 0.1; // timeout (delay before transmit + padding)
179 
180         size_t num_acc_samps = 0; // number of accumulated samples
181         while (num_acc_samps < total_num_samps) {
182             size_t samps_to_send = total_num_samps - num_acc_samps;
183             if (samps_to_send > spb) {
184                 samps_to_send = spb;
185             } else {
186                 md.end_of_burst = true;
187             }
188 
189             // send a single packet
190             size_t num_tx_samps = tx_stream->send(buffs, samps_to_send, md, timeout);
191             // do not use time spec for subsequent packets
192             md.has_time_spec  = false;
193             md.start_of_burst = false;
194 
195             if (num_tx_samps < samps_to_send) {
196                 std::cerr << "Send timeout..." << std::endl;
197                 if (stop_signal_called) {
198                     exit(EXIT_FAILURE);
199                 }
200             }
201 
202             if (verbose) {
203                 std::cout << boost::format("Sent packet: %u samples") % num_tx_samps
204                           << std::endl;
205             }
206 
207             num_acc_samps += num_tx_samps;
208         }
209 
210         time_to_send += rep_rate;
211 
212         std::cout << std::endl << "Waiting for async burst ACK... " << std::flush;
213         uhd::async_metadata_t async_md;
214         size_t acks = 0;
215         // loop through all messages for the ACK packets (may have underflow messages in
216         // queue)
217         while (acks < channel_nums.size()
218                and tx_stream->recv_async_msg(async_md, seconds_in_future)) {
219             if (async_md.event_code == uhd::async_metadata_t::EVENT_CODE_BURST_ACK) {
220                 acks++;
221             }
222         }
223         std::cout << (acks == channel_nums.size() ? "success" : "fail") << std::endl;
224     } while (not stop_signal_called and repeat);
225 
226     // finished
227     std::cout << std::endl << "Done!" << std::endl << std::endl;
228 
229     return EXIT_SUCCESS;
230 }
231