Statistics
| Branch: | Tag: | Revision:

root / host / examples / tx_samples_from_file.cpp @ fac15db5

History | View | Annotate | Download (7.56 KB)

1 1ab38031 Josh Blum
//
2
// Copyright 2011 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/utils/thread_priority.hpp>
19
#include <uhd/utils/safe_main.hpp>
20
#include <uhd/usrp/multi_usrp.hpp>
21
#include <boost/program_options.hpp>
22
#include <boost/format.hpp>
23
#include <boost/thread.hpp>
24
#include <iostream>
25
#include <fstream>
26
#include <complex>
27
28
namespace po = boost::program_options;
29
30
template<typename samp_type> void send_from_file(
31
    uhd::usrp::multi_usrp::sptr usrp,
32 a629bbe7 Josh Blum
    const std::string &cpu_format,
33 1ab38031 Josh Blum
    const std::string &file,
34
    size_t samps_per_buff
35
){
36 a629bbe7 Josh Blum
    //create a transmit streamer
37 fac15db5 Josh Blum
    uhd::stream_args_t stream_args(cpu_format);
38
    uhd::tx_streamer::sptr tx_stream = usrp->get_tx_stream(stream_args);
39 a629bbe7 Josh Blum
40 1ab38031 Josh Blum
    uhd::tx_metadata_t md;
41
    md.start_of_burst = false;
42
    md.end_of_burst = false;
43
    std::vector<samp_type> buff(samps_per_buff);
44
    std::ifstream infile(file.c_str(), std::ifstream::binary);
45
46
    //loop until the entire file has been read
47
    while(not md.end_of_burst){
48
49
        infile.read((char*)&buff.front(), buff.size()*sizeof(samp_type));
50
        size_t num_tx_samps = infile.gcount()/sizeof(samp_type);
51
52
        md.end_of_burst = infile.eof();
53
54 a629bbe7 Josh Blum
        tx_stream->send(&buff.front(), num_tx_samps, md);
55 1ab38031 Josh Blum
    }
56
57
    infile.close();
58
}
59
60
int UHD_SAFE_MAIN(int argc, char *argv[]){
61
    uhd::set_thread_priority_safe();
62
63
    //variables to be set by po
64 539c9b5a Jason Abele
    std::string args, file, type, ant, subdev, ref;
65 1ab38031 Josh Blum
    size_t spb;
66 60db6edf Josh Blum
    double rate, freq, gain, bw;
67 1ab38031 Josh Blum
68
    //setup the program options
69
    po::options_description desc("Allowed options");
70
    desc.add_options()
71
        ("help", "help message")
72
        ("args", po::value<std::string>(&args)->default_value(""), "multi uhd device address args")
73
        ("file", po::value<std::string>(&file)->default_value("usrp_samples.dat"), "name of the file to read binary samples from")
74 aa908faf Josh Blum
        ("type", po::value<std::string>(&type)->default_value("short"), "sample type: double, float, or short")
75 1ab38031 Josh Blum
        ("spb", po::value<size_t>(&spb)->default_value(10000), "samples per buffer")
76 60db6edf Josh Blum
        ("rate", po::value<double>(&rate), "rate of outgoing samples")
77 9b2ae7e0 Josh Blum
        ("freq", po::value<double>(&freq), "RF center frequency in Hz")
78 60db6edf Josh Blum
        ("gain", po::value<double>(&gain), "gain for the RF chain")
79 9b2ae7e0 Josh Blum
        ("ant", po::value<std::string>(&ant), "daughterboard antenna selection")
80
        ("subdev", po::value<std::string>(&subdev), "daughterboard subdevice specification")
81
        ("bw", po::value<double>(&bw), "daughterboard IF filter bandwidth in Hz")
82 539c9b5a Jason Abele
        ("ref", po::value<std::string>(&ref)->default_value("INTERNAL"), "waveform type (INTERNAL, EXTERNAL, MIMO)")
83 1ab38031 Josh Blum
    ;
84
    po::variables_map vm;
85
    po::store(po::parse_command_line(argc, argv, desc), vm);
86
    po::notify(vm);
87
88
    //print the help message
89
    if (vm.count("help")){
90
        std::cout << boost::format("UHD TX samples from file %s") % desc << std::endl;
91
        return ~0;
92
    }
93
94
    //create a usrp device
95
    std::cout << std::endl;
96
    std::cout << boost::format("Creating the usrp device with: %s...") % args << std::endl;
97
    uhd::usrp::multi_usrp::sptr usrp = uhd::usrp::multi_usrp::make(args);
98 60db6edf Josh Blum
99 539c9b5a Jason Abele
    //Lock mboard clocks
100
    if (ref == "MIMO") {
101
        uhd::clock_config_t clock_config;
102
        clock_config.ref_source = uhd::clock_config_t::REF_MIMO;
103
        clock_config.pps_source = uhd::clock_config_t::PPS_MIMO;
104
        usrp->set_clock_config(clock_config, 0);
105
    }
106
    else if (ref == "EXTERNAL") {
107
        usrp->set_clock_config(uhd::clock_config_t::external(), 0);
108
    }
109
    else if (ref == "INTERNAL") {
110
        usrp->set_clock_config(uhd::clock_config_t::internal(), 0);
111
    }
112
113 60db6edf Josh Blum
    //always select the subdevice first, the channel mapping affects the other settings
114
    if (vm.count("subdev")) usrp->set_tx_subdev_spec(subdev);
115
116 1ab38031 Josh Blum
    std::cout << boost::format("Using Device: %s") % usrp->get_pp_string() << std::endl;
117
118 60db6edf Josh Blum
    //set the sample rate
119
    if (not vm.count("rate")){
120
        std::cerr << "Please specify the sample rate with --rate" << std::endl;
121
        return ~0;
122
    }
123
    std::cout << boost::format("Setting TX Rate: %f Msps...") % (rate/1e6) << std::endl;
124 1ab38031 Josh Blum
    usrp->set_tx_rate(rate);
125 60db6edf Josh Blum
    std::cout << boost::format("Actual TX Rate: %f Msps...") % (usrp->get_tx_rate()/1e6) << std::endl << std::endl;
126 1ab38031 Josh Blum
127 60db6edf Josh Blum
    //set the center frequency
128
    if (not vm.count("freq")){
129
        std::cerr << "Please specify the center frequency with --freq" << std::endl;
130
        return ~0;
131
    }
132
    std::cout << boost::format("Setting TX Freq: %f MHz...") % (freq/1e6) << std::endl;
133 1ab38031 Josh Blum
    usrp->set_tx_freq(freq);
134 60db6edf Josh Blum
    std::cout << boost::format("Actual TX Freq: %f MHz...") % (usrp->get_tx_freq()/1e6) << std::endl << std::endl;
135
136
    //set the rf gain
137
    if (vm.count("gain")){
138
        std::cout << boost::format("Setting TX Gain: %f dB...") % gain << std::endl;
139
        usrp->set_tx_gain(gain);
140
        std::cout << boost::format("Actual TX Gain: %f dB...") % usrp->get_tx_gain() << std::endl << std::endl;
141
    }
142
143
    //set the IF filter bandwidth
144
    if (vm.count("bw")){
145
        std::cout << boost::format("Setting TX Bandwidth: %f MHz...") % bw << std::endl;
146
        usrp->set_tx_bandwidth(bw);
147
        std::cout << boost::format("Actual TX Bandwidth: %f MHz...") % usrp->get_tx_bandwidth() << std::endl << std::endl;
148
    }
149 1ab38031 Josh Blum
150 60db6edf Josh Blum
    //set the antenna
151
    if (vm.count("ant")) usrp->set_tx_antenna(ant);
152 1ab38031 Josh Blum
153
    boost::this_thread::sleep(boost::posix_time::seconds(1)); //allow for some setup time
154
155 539c9b5a Jason Abele
    //Check Ref and LO Lock detect
156
    std::vector<std::string> sensor_names;
157
    sensor_names = usrp->get_tx_sensor_names(0);
158
    if (std::find(sensor_names.begin(), sensor_names.end(), "lo_locked") != sensor_names.end()) {
159
        uhd::sensor_value_t lo_locked = usrp->get_tx_sensor("lo_locked",0);
160
        std::cout << boost::format("Checking TX: %s ...") % lo_locked.to_pp_string() << std::endl;
161
        UHD_ASSERT_THROW(lo_locked.to_bool());
162
    }
163
    sensor_names = usrp->get_mboard_sensor_names(0);
164
    if ((ref == "MIMO") and (std::find(sensor_names.begin(), sensor_names.end(), "mimo_locked") != sensor_names.end())) {
165
        uhd::sensor_value_t mimo_locked = usrp->get_mboard_sensor("mimo_locked",0);
166
        std::cout << boost::format("Checking TX: %s ...") % mimo_locked.to_pp_string() << std::endl;
167
        UHD_ASSERT_THROW(mimo_locked.to_bool());
168
    }
169
    if ((ref == "EXTERNAL") and (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end())) {
170
        uhd::sensor_value_t ref_locked = usrp->get_mboard_sensor("ref_locked",0);
171
        std::cout << boost::format("Checking TX: %s ...") % ref_locked.to_pp_string() << std::endl;
172
        UHD_ASSERT_THROW(ref_locked.to_bool());
173
    }
174
175 1ab38031 Josh Blum
    //send from file
176 a629bbe7 Josh Blum
    if (type == "double") send_from_file<std::complex<double> >(usrp, "fc64", file, spb);
177
    else if (type == "float") send_from_file<std::complex<float> >(usrp, "fc32", file, spb);
178
    else if (type == "short") send_from_file<std::complex<short> >(usrp, "sc16", file, spb);
179 1ab38031 Josh Blum
    else throw std::runtime_error("Unknown type " + type);
180
181
    //finished
182
    std::cout << std::endl << "Done!" << std::endl << std::endl;
183
184
    return 0;
185
}