kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1 | /* |
kurtis.heimerl | 119ca9c | 2011-11-26 03:18:26 +0000 | [diff] [blame] | 2 | * Device support for Ettus Research UHD driver |
| 3 | * Written by Thomas Tsou <ttsou@vt.edu> |
| 4 | * |
| 5 | * Copyright 2010,2011 Free Software Foundation, Inc. |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * See the COPYING file in the main directory for details. |
| 20 | */ |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 21 | |
| 22 | #include "radioDevice.h" |
| 23 | #include "Threads.h" |
| 24 | #include "Logger.h" |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 25 | #include <uhd/version.hpp> |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 26 | #include <uhd/property_tree.hpp> |
kurtis.heimerl | 856bbbe | 2011-12-15 00:50:16 +0000 | [diff] [blame] | 27 | #include <uhd/usrp/multi_usrp.hpp> |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 28 | #include <uhd/utils/thread_priority.hpp> |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 29 | #include <uhd/utils/msg.hpp> |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 30 | |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 31 | #ifdef HAVE_CONFIG_H |
| 32 | #include "config.h" |
| 33 | #endif |
| 34 | |
kurtis.heimerl | ac0ee12 | 2011-11-28 06:25:58 +0000 | [diff] [blame] | 35 | #define U1_DEFAULT_CLK_RT 64e6 |
| 36 | #define U2_DEFAULT_CLK_RT 100e6 |
| 37 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 38 | /* |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 39 | master_clk_rt - Master clock frequency - ignored if host resampling is |
| 40 | enabled |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 41 | |
| 42 | rx_smpl_offset - Timing correction in seconds between receive and |
| 43 | transmit timestamps. This value corrects for delays on |
| 44 | on the RF side of the timestamping point of the device. |
kurtis.heimerl | be6abe1 | 2011-11-26 03:17:05 +0000 | [diff] [blame] | 45 | This value is generally empirically measured. |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 46 | |
kurtis.heimerl | 7ac54b1 | 2011-11-26 03:17:49 +0000 | [diff] [blame] | 47 | smpl_buf_sz - The receive sample buffer size in bytes. |
| 48 | |
| 49 | tx_ampl - Transmit amplitude must be between 0 and 1.0 |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 50 | */ |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 51 | const double master_clk_rt = 52e6; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 52 | const size_t smpl_buf_sz = (1 << 20); |
kurtis.heimerl | 7ac54b1 | 2011-11-26 03:17:49 +0000 | [diff] [blame] | 53 | const float tx_ampl = .3; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 54 | |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 55 | #ifdef RESAMPLE |
| 56 | const double rx_smpl_offset = .00005; |
| 57 | #else |
| 58 | const double rx_smpl_offset = .0000869; |
| 59 | #endif |
| 60 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 61 | /** Timestamp conversion |
| 62 | @param timestamp a UHD or OpenBTS timestamp |
| 63 | @param rate sample rate |
| 64 | @return the converted timestamp |
| 65 | */ |
| 66 | uhd::time_spec_t convert_time(TIMESTAMP ticks, double rate) |
| 67 | { |
| 68 | double secs = (double) ticks / rate; |
| 69 | return uhd::time_spec_t(secs); |
| 70 | } |
| 71 | |
| 72 | TIMESTAMP convert_time(uhd::time_spec_t ts, double rate) |
| 73 | { |
kurtis.heimerl | c7cb817 | 2011-11-26 03:17:26 +0000 | [diff] [blame] | 74 | TIMESTAMP ticks = ts.get_full_secs() * rate; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 75 | return ts.get_tick_count(rate) + ticks; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | Sample Buffer - Allows reading and writing of timed samples using OpenBTS |
| 80 | or UHD style timestamps. Time conversions are handled |
| 81 | internally or accessable through the static convert calls. |
| 82 | */ |
| 83 | class smpl_buf { |
| 84 | public: |
| 85 | /** Sample buffer constructor |
| 86 | @param len number of 32-bit samples the buffer should hold |
| 87 | @param rate sample clockrate |
| 88 | @param timestamp |
| 89 | */ |
| 90 | smpl_buf(size_t len, double rate); |
| 91 | ~smpl_buf(); |
| 92 | |
| 93 | /** Query number of samples available for reading |
| 94 | @param timestamp time of first sample |
| 95 | @return number of available samples or error |
| 96 | */ |
| 97 | ssize_t avail_smpls(TIMESTAMP timestamp) const; |
| 98 | ssize_t avail_smpls(uhd::time_spec_t timestamp) const; |
| 99 | |
| 100 | /** Read and write |
| 101 | @param buf pointer to buffer |
| 102 | @param len number of samples desired to read or write |
| 103 | @param timestamp time of first stample |
| 104 | @return number of actual samples read or written or error |
| 105 | */ |
| 106 | ssize_t read(void *buf, size_t len, TIMESTAMP timestamp); |
| 107 | ssize_t read(void *buf, size_t len, uhd::time_spec_t timestamp); |
| 108 | ssize_t write(void *buf, size_t len, TIMESTAMP timestamp); |
| 109 | ssize_t write(void *buf, size_t len, uhd::time_spec_t timestamp); |
| 110 | |
| 111 | /** Buffer status string |
| 112 | @return a formatted string describing internal buffer state |
| 113 | */ |
| 114 | std::string str_status() const; |
| 115 | |
| 116 | /** Formatted error string |
| 117 | @param code an error code |
| 118 | @return a formatted error string |
| 119 | */ |
| 120 | static std::string str_code(ssize_t code); |
| 121 | |
| 122 | enum err_code { |
| 123 | ERROR_TIMESTAMP = -1, |
| 124 | ERROR_READ = -2, |
| 125 | ERROR_WRITE = -3, |
| 126 | ERROR_OVERFLOW = -4 |
| 127 | }; |
| 128 | |
| 129 | private: |
| 130 | uint32_t *data; |
| 131 | size_t buf_len; |
| 132 | |
| 133 | double clk_rt; |
| 134 | |
| 135 | TIMESTAMP time_start; |
| 136 | TIMESTAMP time_end; |
| 137 | |
| 138 | size_t data_start; |
| 139 | size_t data_end; |
| 140 | }; |
| 141 | |
| 142 | /* |
| 143 | uhd_device - UHD implementation of the Device interface. Timestamped samples |
| 144 | are sent to and received from the device. An intermediate buffer |
| 145 | on the receive side collects and aligns packets of samples. |
| 146 | Events and errors such as underruns are reported asynchronously |
| 147 | by the device and received in a separate thread. |
| 148 | */ |
| 149 | class uhd_device : public RadioDevice { |
| 150 | public: |
| 151 | uhd_device(double rate, bool skip_rx); |
| 152 | ~uhd_device(); |
| 153 | |
| 154 | bool open(); |
| 155 | bool start(); |
| 156 | bool stop(); |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 157 | void restart(uhd::time_spec_t ts); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 158 | void setPriority(); |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 159 | enum busType getBus() { return bus; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 160 | |
| 161 | int readSamples(short *buf, int len, bool *overrun, |
| 162 | TIMESTAMP timestamp, bool *underrun, unsigned *RSSI); |
| 163 | |
| 164 | int writeSamples(short *buf, int len, bool *underrun, |
| 165 | TIMESTAMP timestamp, bool isControl); |
| 166 | |
| 167 | bool updateAlignment(TIMESTAMP timestamp); |
| 168 | |
| 169 | bool setTxFreq(double wFreq); |
| 170 | bool setRxFreq(double wFreq); |
| 171 | |
| 172 | inline TIMESTAMP initialWriteTimestamp() { return 0; } |
| 173 | inline TIMESTAMP initialReadTimestamp() { return 0; } |
| 174 | |
kurtis.heimerl | 7ac54b1 | 2011-11-26 03:17:49 +0000 | [diff] [blame] | 175 | inline double fullScaleInputValue() { return 32000 * tx_ampl; } |
| 176 | inline double fullScaleOutputValue() { return 32000; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 177 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 178 | double setRxGain(double db); |
| 179 | double getRxGain(void) { return rx_gain; } |
| 180 | double maxRxGain(void) { return rx_gain_max; } |
| 181 | double minRxGain(void) { return rx_gain_min; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 182 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 183 | double setTxGain(double db); |
| 184 | double maxTxGain(void) { return tx_gain_max; } |
| 185 | double minTxGain(void) { return tx_gain_min; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 186 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 187 | double getTxFreq() { return tx_freq; } |
| 188 | double getRxFreq() { return rx_freq; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 189 | |
| 190 | inline double getSampleRate() { return actual_smpl_rt; } |
| 191 | inline double numberRead() { return rx_pkt_cnt; } |
| 192 | inline double numberWritten() { return 0; } |
| 193 | |
| 194 | /** Receive and process asynchronous message |
| 195 | @return true if message received or false on timeout or error |
| 196 | */ |
| 197 | bool recv_async_msg(); |
| 198 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 199 | enum err_code { |
| 200 | ERROR_TIMING = -1, |
| 201 | ERROR_UNRECOVERABLE = -2, |
| 202 | ERROR_UNHANDLED = -3, |
| 203 | }; |
| 204 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 205 | private: |
kurtis.heimerl | 856bbbe | 2011-12-15 00:50:16 +0000 | [diff] [blame] | 206 | uhd::usrp::multi_usrp::sptr usrp_dev; |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 207 | enum busType bus; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 208 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 209 | double desired_smpl_rt, actual_smpl_rt; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 210 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 211 | double tx_gain, tx_gain_min, tx_gain_max; |
| 212 | double rx_gain, rx_gain_min, rx_gain_max; |
| 213 | |
| 214 | double tx_freq, rx_freq; |
| 215 | size_t tx_spp, rx_spp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 216 | |
| 217 | bool started; |
| 218 | bool aligned; |
| 219 | bool skip_rx; |
| 220 | |
| 221 | size_t rx_pkt_cnt; |
| 222 | size_t drop_cnt; |
| 223 | uhd::time_spec_t prev_ts; |
| 224 | |
| 225 | TIMESTAMP ts_offset; |
| 226 | smpl_buf *rx_smpl_buf; |
| 227 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 228 | void init_gains(); |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 229 | void set_ref_clk(bool ext_clk); |
| 230 | double set_rates(double rate); |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 231 | bool parse_dev_type(); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 232 | bool flush_recv(size_t num_pkts); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 233 | int check_rx_md_err(uhd::rx_metadata_t &md, ssize_t num_smpls); |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 234 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 235 | std::string str_code(uhd::rx_metadata_t metadata); |
| 236 | std::string str_code(uhd::async_metadata_t metadata); |
| 237 | |
| 238 | Thread async_event_thrd; |
| 239 | }; |
| 240 | |
| 241 | void *async_event_loop(uhd_device *dev) |
| 242 | { |
| 243 | while (1) { |
| 244 | dev->recv_async_msg(); |
| 245 | pthread_testcancel(); |
| 246 | } |
| 247 | } |
| 248 | |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 249 | /* |
| 250 | Catch and drop underrun 'U' and overrun 'O' messages from stdout |
| 251 | since we already report using the logging facility. Direct |
| 252 | everything else appropriately. |
| 253 | */ |
| 254 | void uhd_msg_handler(uhd::msg::type_t type, const std::string &msg) |
| 255 | { |
| 256 | switch (type) { |
| 257 | case uhd::msg::status: |
| 258 | LOG(INFO) << msg; |
| 259 | break; |
| 260 | case uhd::msg::warning: |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 261 | LOG(WARNING) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 262 | break; |
| 263 | case uhd::msg::error: |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 264 | LOG(ERR) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 265 | break; |
| 266 | case uhd::msg::fastpath: |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 271 | uhd_device::uhd_device(double rate, bool skip_rx) |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 272 | : desired_smpl_rt(rate), actual_smpl_rt(0), |
| 273 | tx_gain(0.0), tx_gain_min(0.0), tx_gain_max(0.0), |
| 274 | rx_gain(0.0), rx_gain_min(0.0), rx_gain_max(0.0), |
| 275 | tx_freq(0.0), rx_freq(0.0), tx_spp(0), rx_spp(0), |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 276 | started(false), aligned(false), rx_pkt_cnt(0), drop_cnt(0), |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 277 | prev_ts(0,0), ts_offset(0), rx_smpl_buf(NULL) |
| 278 | { |
| 279 | this->skip_rx = skip_rx; |
| 280 | } |
| 281 | |
| 282 | uhd_device::~uhd_device() |
| 283 | { |
| 284 | stop(); |
| 285 | |
| 286 | if (rx_smpl_buf) |
| 287 | delete rx_smpl_buf; |
| 288 | } |
| 289 | |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 290 | void uhd_device::init_gains() |
| 291 | { |
| 292 | uhd::gain_range_t range; |
| 293 | |
| 294 | range = usrp_dev->get_tx_gain_range(); |
| 295 | tx_gain_min = range.start(); |
| 296 | tx_gain_max = range.stop(); |
| 297 | |
| 298 | range = usrp_dev->get_rx_gain_range(); |
| 299 | rx_gain_min = range.start(); |
| 300 | rx_gain_max = range.stop(); |
| 301 | |
| 302 | usrp_dev->set_tx_gain((tx_gain_min + tx_gain_max) / 2); |
| 303 | usrp_dev->set_rx_gain((rx_gain_min + rx_gain_max) / 2); |
| 304 | |
| 305 | tx_gain = usrp_dev->get_tx_gain(); |
| 306 | rx_gain = usrp_dev->get_rx_gain(); |
| 307 | |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | void uhd_device::set_ref_clk(bool ext_clk) |
| 312 | { |
| 313 | uhd::clock_config_t clk_cfg; |
| 314 | |
| 315 | clk_cfg.pps_source = uhd::clock_config_t::PPS_SMA; |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 316 | |
| 317 | if (ext_clk) |
| 318 | clk_cfg.ref_source = uhd::clock_config_t::REF_SMA; |
| 319 | else |
| 320 | clk_cfg.ref_source = uhd::clock_config_t::REF_INT; |
| 321 | |
| 322 | usrp_dev->set_clock_config(clk_cfg); |
| 323 | |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | double uhd_device::set_rates(double rate) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 328 | { |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 329 | double actual_rt, actual_clk_rt; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 330 | |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 331 | #ifndef RESAMPLE |
kurtis.heimerl | ac0ee12 | 2011-11-28 06:25:58 +0000 | [diff] [blame] | 332 | // Make sure we can set the master clock rate on this device |
| 333 | actual_clk_rt = usrp_dev->get_master_clock_rate(); |
| 334 | if (actual_clk_rt > U1_DEFAULT_CLK_RT) { |
| 335 | LOG(ALERT) << "Cannot set clock rate on this device"; |
| 336 | LOG(ALERT) << "Please compile with host resampling support"; |
| 337 | return -1.0; |
| 338 | } |
| 339 | |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 340 | // Set master clock rate |
| 341 | usrp_dev->set_master_clock_rate(master_clk_rt); |
| 342 | actual_clk_rt = usrp_dev->get_master_clock_rate(); |
| 343 | |
| 344 | if (actual_clk_rt != master_clk_rt) { |
kurtis.heimerl | e332032 | 2011-11-28 06:26:08 +0000 | [diff] [blame] | 345 | LOG(ALERT) << "Failed to set master clock rate"; |
| 346 | LOG(ALERT) << "Actual clock rate " << actual_clk_rt; |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 347 | return -1.0; |
| 348 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 349 | #endif |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 350 | |
| 351 | // Set sample rates |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 352 | usrp_dev->set_tx_rate(rate); |
| 353 | usrp_dev->set_rx_rate(rate); |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 354 | actual_rt = usrp_dev->get_tx_rate(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 355 | |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 356 | if (actual_rt != rate) { |
kurtis.heimerl | e332032 | 2011-11-28 06:26:08 +0000 | [diff] [blame] | 357 | LOG(ALERT) << "Actual sample rate differs from desired rate"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 358 | return -1.0; |
| 359 | } |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 360 | if (usrp_dev->get_rx_rate() != actual_rt) { |
kurtis.heimerl | e332032 | 2011-11-28 06:26:08 +0000 | [diff] [blame] | 361 | LOG(ALERT) << "Transmit and receive sample rates do not match"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 362 | return -1.0; |
| 363 | } |
| 364 | |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 365 | return actual_rt; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 366 | } |
| 367 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 368 | double uhd_device::setTxGain(double db) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 369 | { |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 370 | usrp_dev->set_tx_gain(db); |
| 371 | tx_gain = usrp_dev->get_tx_gain(); |
| 372 | |
| 373 | LOG(INFO) << "Set TX gain to " << tx_gain << "dB"; |
| 374 | |
| 375 | return tx_gain; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 376 | } |
| 377 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 378 | double uhd_device::setRxGain(double db) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 379 | { |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 380 | usrp_dev->set_rx_gain(db); |
| 381 | rx_gain = usrp_dev->get_rx_gain(); |
| 382 | |
| 383 | LOG(INFO) << "Set RX gain to " << rx_gain << "dB"; |
| 384 | |
| 385 | return rx_gain; |
| 386 | } |
| 387 | |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 388 | /* |
| 389 | Parse the UHD device tree and mboard name to find out what device we're |
| 390 | dealing with. We need the bus type so that the transceiver knows how to |
| 391 | deal with the transport latency. Reject the USRP1 because UHD doesn't |
| 392 | support timestamped samples with it. |
| 393 | */ |
| 394 | bool uhd_device::parse_dev_type() |
| 395 | { |
| 396 | std::string mboard_str, dev_str; |
| 397 | uhd::property_tree::sptr prop_tree; |
| 398 | size_t usrp1_str, usrp2_str, b100_str1, b100_str2; |
| 399 | |
| 400 | prop_tree = usrp_dev->get_device()->get_tree(); |
| 401 | dev_str = prop_tree->access<std::string>("/name").get(); |
| 402 | mboard_str = usrp_dev->get_mboard_name(); |
| 403 | |
| 404 | usrp1_str = dev_str.find("USRP1"); |
| 405 | b100_str1 = dev_str.find("B-Series"); |
| 406 | b100_str2 = mboard_str.find("B100"); |
| 407 | |
| 408 | if (usrp1_str != std::string::npos) { |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 409 | LOG(ALERT) << "USRP1 is not supported using the UHD driver"; |
| 410 | LOG(ALERT) << "Please compile with GNU Radio libusrp support"; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
| 414 | if ((b100_str1 != std::string::npos) || (b100_str2 != std::string::npos)) { |
| 415 | bus = USB; |
| 416 | LOG(INFO) << "Using USB bus for " << dev_str; |
| 417 | } else { |
| 418 | bus = NET; |
| 419 | LOG(INFO) << "Using network bus for " << dev_str; |
| 420 | } |
| 421 | |
| 422 | return true; |
| 423 | } |
| 424 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 425 | bool uhd_device::open() |
| 426 | { |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 427 | // Register msg handler |
| 428 | uhd::msg::register_handler(&uhd_msg_handler); |
| 429 | |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 430 | // Find UHD devices |
| 431 | uhd::device_addr_t args(""); |
| 432 | uhd::device_addrs_t dev_addrs = uhd::device::find(args); |
| 433 | if (dev_addrs.size() == 0) { |
| 434 | LOG(ALERT) << "No UHD devices found"; |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | // Use the first found device |
| 439 | LOG(INFO) << "Using discovered UHD device " << dev_addrs[0].to_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 440 | try { |
kurtis.heimerl | 856bbbe | 2011-12-15 00:50:16 +0000 | [diff] [blame] | 441 | usrp_dev = uhd::usrp::multi_usrp::make(dev_addrs[0]); |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 442 | } catch(...) { |
kurtis.heimerl | e417c5a | 2011-11-26 03:19:25 +0000 | [diff] [blame] | 443 | LOG(ALERT) << "UHD make failed"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 447 | // Check for a valid device type and set bus type |
| 448 | if (!parse_dev_type()) |
| 449 | return false; |
| 450 | |
kurtis.heimerl | c6b9b70 | 2011-11-26 03:19:19 +0000 | [diff] [blame] | 451 | #ifdef EXTREF |
| 452 | set_ref_clk(true); |
| 453 | #endif |
| 454 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 455 | // Number of samples per over-the-wire packet |
| 456 | tx_spp = usrp_dev->get_device()->get_max_send_samps_per_packet(); |
| 457 | rx_spp = usrp_dev->get_device()->get_max_recv_samps_per_packet(); |
| 458 | |
| 459 | // Set rates |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 460 | actual_smpl_rt = set_rates(desired_smpl_rt); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 461 | if (actual_smpl_rt < 0) |
| 462 | return false; |
| 463 | |
| 464 | // Create receive buffer |
| 465 | size_t buf_len = smpl_buf_sz / sizeof(uint32_t); |
| 466 | rx_smpl_buf = new smpl_buf(buf_len, actual_smpl_rt); |
| 467 | |
| 468 | // Set receive chain sample offset |
| 469 | ts_offset = (TIMESTAMP)(rx_smpl_offset * actual_smpl_rt); |
| 470 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 471 | // Initialize and shadow gain values |
| 472 | init_gains(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 473 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 474 | // Print configuration |
kurtis.heimerl | 3998da7 | 2011-11-26 03:19:00 +0000 | [diff] [blame] | 475 | LOG(INFO) << "\n" << usrp_dev->get_pp_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 480 | bool uhd_device::flush_recv(size_t num_pkts) |
| 481 | { |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 482 | uhd::rx_metadata_t md; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 483 | size_t num_smpls; |
| 484 | uint32_t buff[rx_spp]; |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 485 | float timeout; |
| 486 | |
| 487 | // Use .01 sec instead of the default .1 sec |
| 488 | timeout = .01; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 489 | |
| 490 | for (size_t i = 0; i < num_pkts; i++) { |
| 491 | num_smpls = usrp_dev->get_device()->recv( |
| 492 | buff, |
| 493 | rx_spp, |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 494 | md, |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 495 | uhd::io_type_t::COMPLEX_INT16, |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 496 | uhd::device::RECV_MODE_ONE_PACKET, |
| 497 | timeout); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 498 | |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 499 | if (!num_smpls) { |
| 500 | switch (md.error_code) { |
| 501 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
| 502 | return true; |
| 503 | default: |
| 504 | continue; |
| 505 | } |
| 506 | } |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 512 | void uhd_device::restart(uhd::time_spec_t ts) |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 513 | { |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 514 | uhd::stream_cmd_t cmd = uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; |
| 515 | usrp_dev->issue_stream_cmd(cmd); |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 516 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 517 | flush_recv(50); |
| 518 | |
| 519 | usrp_dev->set_time_now(ts); |
| 520 | aligned = false; |
| 521 | |
| 522 | cmd = uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS; |
| 523 | cmd.stream_now = true; |
| 524 | usrp_dev->issue_stream_cmd(cmd); |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 525 | } |
| 526 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 527 | bool uhd_device::start() |
| 528 | { |
| 529 | LOG(INFO) << "Starting USRP..."; |
| 530 | |
| 531 | if (started) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 532 | LOG(ERR) << "Device already started"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | |
| 536 | setPriority(); |
| 537 | |
| 538 | // Start asynchronous event (underrun check) loop |
| 539 | async_event_thrd.start((void * (*)(void*))async_event_loop, (void*)this); |
| 540 | |
| 541 | // Start streaming |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 542 | restart(uhd::time_spec_t(0.0)); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 543 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 544 | // Display usrp time |
| 545 | double time_now = usrp_dev->get_time_now().get_real_secs(); |
| 546 | LOG(INFO) << "The current time is " << time_now << " seconds"; |
| 547 | |
| 548 | started = true; |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | bool uhd_device::stop() |
| 553 | { |
| 554 | uhd::stream_cmd_t stream_cmd = |
| 555 | uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; |
| 556 | |
| 557 | usrp_dev->issue_stream_cmd(stream_cmd); |
| 558 | |
| 559 | started = false; |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | void uhd_device::setPriority() |
| 564 | { |
| 565 | uhd::set_thread_priority_safe(); |
| 566 | return; |
| 567 | } |
| 568 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 569 | int uhd_device::check_rx_md_err(uhd::rx_metadata_t &md, ssize_t num_smpls) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 570 | { |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 571 | uhd::time_spec_t ts; |
| 572 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 573 | if (!num_smpls) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 574 | LOG(ERR) << str_code(md); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 575 | |
| 576 | switch (md.error_code) { |
| 577 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 578 | LOG(ALERT) << "UHD: Receive timed out"; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 579 | return ERROR_UNRECOVERABLE; |
| 580 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 581 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 582 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 583 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 584 | default: |
| 585 | return ERROR_UNHANDLED; |
| 586 | } |
| 587 | } |
| 588 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 589 | // Missing timestamp |
| 590 | if (!md.has_time_spec) { |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 591 | LOG(ALERT) << "UHD: Received packet missing timestamp"; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 592 | return ERROR_UNRECOVERABLE; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 593 | } |
| 594 | |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 595 | ts = md.time_spec; |
| 596 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 597 | // Monotonicity check |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 598 | if (ts < prev_ts) { |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 599 | LOG(ALERT) << "UHD: Loss of monotonic time"; |
| 600 | LOG(ERR) << "UHD: Current time: " << ts.get_real_secs(); |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 601 | LOG(ERR) << "UHD: Previous time: " << prev_ts.get_real_secs(); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 602 | return ERROR_TIMING; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 603 | } else { |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 604 | prev_ts = ts; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | int uhd_device::readSamples(short *buf, int len, bool *overrun, |
| 611 | TIMESTAMP timestamp, bool *underrun, unsigned *RSSI) |
| 612 | { |
| 613 | ssize_t rc; |
| 614 | uhd::time_spec_t ts; |
| 615 | uhd::rx_metadata_t metadata; |
| 616 | uint32_t pkt_buf[rx_spp]; |
| 617 | |
| 618 | if (skip_rx) |
| 619 | return 0; |
| 620 | |
| 621 | // Shift read time with respect to transmit clock |
| 622 | timestamp += ts_offset; |
| 623 | |
| 624 | ts = convert_time(timestamp, actual_smpl_rt); |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 625 | LOG(DEBUG) << "Requested timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 626 | |
| 627 | // Check that timestamp is valid |
| 628 | rc = rx_smpl_buf->avail_smpls(timestamp); |
| 629 | if (rc < 0) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 630 | LOG(ERR) << rx_smpl_buf->str_code(rc); |
| 631 | LOG(ERR) << rx_smpl_buf->str_status(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | // Receive samples from the usrp until we have enough |
| 636 | while (rx_smpl_buf->avail_smpls(timestamp) < len) { |
| 637 | size_t num_smpls = usrp_dev->get_device()->recv( |
| 638 | (void*)pkt_buf, |
| 639 | rx_spp, |
| 640 | metadata, |
| 641 | uhd::io_type_t::COMPLEX_INT16, |
| 642 | uhd::device::RECV_MODE_ONE_PACKET); |
| 643 | |
| 644 | rx_pkt_cnt++; |
| 645 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 646 | // Check for errors |
| 647 | rc = check_rx_md_err(metadata, num_smpls); |
| 648 | switch (rc) { |
| 649 | case ERROR_UNRECOVERABLE: |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 650 | LOG(ALERT) << "UHD: Version " << uhd::get_version_string(); |
| 651 | LOG(ALERT) << "UHD: Unrecoverable error, exiting..."; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 652 | exit(-1); |
| 653 | case ERROR_TIMING: |
| 654 | restart(prev_ts); |
| 655 | case ERROR_UNHANDLED: |
kurtis.heimerl | 513a629 | 2011-11-26 03:18:36 +0000 | [diff] [blame] | 656 | continue; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 657 | } |
| 658 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 659 | |
| 660 | ts = metadata.time_spec; |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 661 | LOG(DEBUG) << "Received timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 662 | |
| 663 | rc = rx_smpl_buf->write(pkt_buf, |
| 664 | num_smpls, |
| 665 | metadata.time_spec); |
| 666 | |
| 667 | // Continue on local overrun, exit on other errors |
| 668 | if ((rc < 0)) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 669 | LOG(ERR) << rx_smpl_buf->str_code(rc); |
| 670 | LOG(ERR) << rx_smpl_buf->str_status(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 671 | if (rc != smpl_buf::ERROR_OVERFLOW) |
| 672 | return 0; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // We have enough samples |
| 677 | rc = rx_smpl_buf->read(buf, len, timestamp); |
| 678 | if ((rc < 0) || (rc != len)) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 679 | LOG(ERR) << rx_smpl_buf->str_code(rc); |
| 680 | LOG(ERR) << rx_smpl_buf->str_status(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | return len; |
| 685 | } |
| 686 | |
| 687 | int uhd_device::writeSamples(short *buf, int len, bool *underrun, |
| 688 | unsigned long long timestamp,bool isControl) |
| 689 | { |
| 690 | uhd::tx_metadata_t metadata; |
| 691 | metadata.has_time_spec = true; |
| 692 | metadata.start_of_burst = false; |
| 693 | metadata.end_of_burst = false; |
| 694 | metadata.time_spec = convert_time(timestamp, actual_smpl_rt); |
| 695 | |
| 696 | // No control packets |
| 697 | if (isControl) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 698 | LOG(ERR) << "Control packets not supported"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | // Drop a fixed number of packets (magic value) |
| 703 | if (!aligned) { |
| 704 | drop_cnt++; |
| 705 | |
| 706 | if (drop_cnt == 1) { |
| 707 | LOG(DEBUG) << "Aligning transmitter: stop burst"; |
| 708 | metadata.end_of_burst = true; |
| 709 | } else if (drop_cnt < 30) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 710 | LOG(DEBUG) << "Aligning transmitter: packet advance"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 711 | *underrun = true; |
| 712 | return len; |
| 713 | } else { |
| 714 | LOG(DEBUG) << "Aligning transmitter: start burst"; |
| 715 | metadata.start_of_burst = true; |
| 716 | aligned = true; |
| 717 | drop_cnt = 0; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | size_t num_smpls = usrp_dev->get_device()->send(buf, |
| 722 | len, |
| 723 | metadata, |
| 724 | uhd::io_type_t::COMPLEX_INT16, |
| 725 | uhd::device::SEND_MODE_FULL_BUFF); |
| 726 | |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame^] | 727 | if (num_smpls != (unsigned) len) { |
| 728 | LOG(ALERT) << "UHD: Device send timed out"; |
| 729 | LOG(ALERT) << "UHD: Version " << uhd::get_version_string(); |
| 730 | LOG(ALERT) << "UHD: Unrecoverable error, exiting..."; |
| 731 | exit(-1); |
| 732 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 733 | |
| 734 | return num_smpls; |
| 735 | } |
| 736 | |
| 737 | bool uhd_device::updateAlignment(TIMESTAMP timestamp) |
| 738 | { |
kurtis.heimerl | 13074c9 | 2011-11-26 03:17:43 +0000 | [diff] [blame] | 739 | aligned = false; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 740 | return true; |
| 741 | } |
| 742 | |
| 743 | bool uhd_device::setTxFreq(double wFreq) |
| 744 | { |
| 745 | uhd::tune_result_t tr = usrp_dev->set_tx_freq(wFreq); |
kurtis.heimerl | 3998da7 | 2011-11-26 03:19:00 +0000 | [diff] [blame] | 746 | LOG(INFO) << "\n" << tr.to_pp_string(); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 747 | tx_freq = usrp_dev->get_tx_freq(); |
| 748 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | bool uhd_device::setRxFreq(double wFreq) |
| 753 | { |
| 754 | uhd::tune_result_t tr = usrp_dev->set_rx_freq(wFreq); |
kurtis.heimerl | 3998da7 | 2011-11-26 03:19:00 +0000 | [diff] [blame] | 755 | LOG(INFO) << "\n" << tr.to_pp_string(); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 756 | rx_freq = usrp_dev->get_rx_freq(); |
| 757 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 758 | return true; |
| 759 | } |
| 760 | |
| 761 | bool uhd_device::recv_async_msg() |
| 762 | { |
| 763 | uhd::async_metadata_t metadata; |
| 764 | if (!usrp_dev->get_device()->recv_async_msg(metadata)) |
| 765 | return false; |
| 766 | |
| 767 | // Assume that any error requires resynchronization |
| 768 | if (metadata.event_code != uhd::async_metadata_t::EVENT_CODE_BURST_ACK) { |
| 769 | aligned = false; |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 770 | LOG(ERR) << str_code(metadata); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | std::string uhd_device::str_code(uhd::rx_metadata_t metadata) |
| 777 | { |
| 778 | std::ostringstream ost("UHD: "); |
| 779 | |
| 780 | switch (metadata.error_code) { |
| 781 | case uhd::rx_metadata_t::ERROR_CODE_NONE: |
| 782 | ost << "No error"; |
| 783 | break; |
| 784 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
| 785 | ost << "No packet received, implementation timed-out"; |
| 786 | break; |
| 787 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 788 | ost << "A stream command was issued in the past"; |
| 789 | break; |
| 790 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 791 | ost << "Expected another stream command"; |
| 792 | break; |
| 793 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 794 | ost << "An internal receive buffer has filled"; |
| 795 | break; |
| 796 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 797 | ost << "The packet could not be parsed"; |
| 798 | break; |
| 799 | default: |
| 800 | ost << "Unknown error " << metadata.error_code; |
| 801 | } |
| 802 | |
| 803 | if (metadata.has_time_spec) |
| 804 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 805 | |
| 806 | return ost.str(); |
| 807 | } |
| 808 | |
| 809 | std::string uhd_device::str_code(uhd::async_metadata_t metadata) |
| 810 | { |
| 811 | std::ostringstream ost("UHD: "); |
| 812 | |
| 813 | switch (metadata.event_code) { |
| 814 | case uhd::async_metadata_t::EVENT_CODE_BURST_ACK: |
| 815 | ost << "A packet was successfully transmitted"; |
| 816 | break; |
| 817 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW: |
| 818 | ost << "An internal send buffer has emptied"; |
| 819 | break; |
| 820 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR: |
| 821 | ost << "Packet loss between host and device"; |
| 822 | break; |
| 823 | case uhd::async_metadata_t::EVENT_CODE_TIME_ERROR: |
| 824 | ost << "Packet time was too late or too early"; |
| 825 | break; |
| 826 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET: |
| 827 | ost << "Underflow occurred inside a packet"; |
| 828 | break; |
| 829 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST: |
| 830 | ost << "Packet loss within a burst"; |
| 831 | break; |
| 832 | default: |
| 833 | ost << "Unknown error " << metadata.event_code; |
| 834 | } |
| 835 | |
| 836 | if (metadata.has_time_spec) |
| 837 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 838 | |
| 839 | return ost.str(); |
| 840 | } |
| 841 | |
| 842 | smpl_buf::smpl_buf(size_t len, double rate) |
| 843 | : buf_len(len), clk_rt(rate), |
| 844 | time_start(0), time_end(0), data_start(0), data_end(0) |
| 845 | { |
| 846 | data = new uint32_t[len]; |
| 847 | } |
| 848 | |
| 849 | smpl_buf::~smpl_buf() |
| 850 | { |
| 851 | delete[] data; |
| 852 | } |
| 853 | |
| 854 | ssize_t smpl_buf::avail_smpls(TIMESTAMP timestamp) const |
| 855 | { |
| 856 | if (timestamp < time_start) |
| 857 | return ERROR_TIMESTAMP; |
| 858 | else if (timestamp >= time_end) |
| 859 | return 0; |
| 860 | else |
| 861 | return time_end - timestamp; |
| 862 | } |
| 863 | |
| 864 | ssize_t smpl_buf::avail_smpls(uhd::time_spec_t timespec) const |
| 865 | { |
| 866 | return avail_smpls(convert_time(timespec, clk_rt)); |
| 867 | } |
| 868 | |
| 869 | ssize_t smpl_buf::read(void *buf, size_t len, TIMESTAMP timestamp) |
| 870 | { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 871 | int type_sz = 2 * sizeof(short); |
| 872 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 873 | // Check for valid read |
| 874 | if (timestamp < time_start) |
| 875 | return ERROR_TIMESTAMP; |
| 876 | if (timestamp >= time_end) |
| 877 | return 0; |
| 878 | if (len >= buf_len) |
| 879 | return ERROR_READ; |
| 880 | |
| 881 | // How many samples should be copied |
| 882 | size_t num_smpls = time_end - timestamp; |
| 883 | if (num_smpls > len); |
| 884 | num_smpls = len; |
| 885 | |
| 886 | // Starting index |
| 887 | size_t read_start = data_start + (timestamp - time_start); |
| 888 | |
| 889 | // Read it |
| 890 | if (read_start + num_smpls < buf_len) { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 891 | size_t numBytes = len * type_sz; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 892 | memcpy(buf, data + read_start, numBytes); |
| 893 | } else { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 894 | size_t first_cp = (buf_len - read_start) * type_sz; |
| 895 | size_t second_cp = len * type_sz - first_cp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 896 | |
| 897 | memcpy(buf, data + read_start, first_cp); |
| 898 | memcpy((char*) buf + first_cp, data, second_cp); |
| 899 | } |
| 900 | |
| 901 | data_start = (read_start + len) % buf_len; |
| 902 | time_start = timestamp + len; |
| 903 | |
| 904 | if (time_start > time_end) |
| 905 | return ERROR_READ; |
| 906 | else |
| 907 | return num_smpls; |
| 908 | } |
| 909 | |
| 910 | ssize_t smpl_buf::read(void *buf, size_t len, uhd::time_spec_t ts) |
| 911 | { |
| 912 | return read(buf, len, convert_time(ts, clk_rt)); |
| 913 | } |
| 914 | |
| 915 | ssize_t smpl_buf::write(void *buf, size_t len, TIMESTAMP timestamp) |
| 916 | { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 917 | int type_sz = 2 * sizeof(short); |
| 918 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 919 | // Check for valid write |
| 920 | if ((len == 0) || (len >= buf_len)) |
| 921 | return ERROR_WRITE; |
| 922 | if ((timestamp + len) <= time_end) |
| 923 | return ERROR_TIMESTAMP; |
| 924 | |
| 925 | // Starting index |
| 926 | size_t write_start = (data_start + (timestamp - time_start)) % buf_len; |
| 927 | |
| 928 | // Write it |
| 929 | if ((write_start + len) < buf_len) { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 930 | size_t numBytes = len * type_sz; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 931 | memcpy(data + write_start, buf, numBytes); |
| 932 | } else { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 933 | size_t first_cp = (buf_len - write_start) * type_sz; |
| 934 | size_t second_cp = len * type_sz - first_cp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 935 | |
| 936 | memcpy(data + write_start, buf, first_cp); |
| 937 | memcpy(data, (char*) buf + first_cp, second_cp); |
| 938 | } |
| 939 | |
| 940 | data_end = (write_start + len) % buf_len; |
| 941 | time_end = timestamp + len; |
| 942 | |
| 943 | if (((write_start + len) > buf_len) && (data_end > data_start)) |
| 944 | return ERROR_OVERFLOW; |
| 945 | else if (time_end <= time_start) |
| 946 | return ERROR_WRITE; |
| 947 | else |
| 948 | return len; |
| 949 | } |
| 950 | |
| 951 | ssize_t smpl_buf::write(void *buf, size_t len, uhd::time_spec_t ts) |
| 952 | { |
| 953 | return write(buf, len, convert_time(ts, clk_rt)); |
| 954 | } |
| 955 | |
| 956 | std::string smpl_buf::str_status() const |
| 957 | { |
| 958 | std::ostringstream ost("Sample buffer: "); |
| 959 | |
| 960 | ost << "length = " << buf_len; |
| 961 | ost << ", time_start = " << time_start; |
| 962 | ost << ", time_end = " << time_end; |
| 963 | ost << ", data_start = " << data_start; |
| 964 | ost << ", data_end = " << data_end; |
| 965 | |
| 966 | return ost.str(); |
| 967 | } |
| 968 | |
| 969 | std::string smpl_buf::str_code(ssize_t code) |
| 970 | { |
| 971 | switch (code) { |
| 972 | case ERROR_TIMESTAMP: |
| 973 | return "Sample buffer: Requested timestamp is not valid"; |
| 974 | case ERROR_READ: |
| 975 | return "Sample buffer: Read error"; |
| 976 | case ERROR_WRITE: |
| 977 | return "Sample buffer: Write error"; |
| 978 | case ERROR_OVERFLOW: |
| 979 | return "Sample buffer: Overrun"; |
| 980 | default: |
| 981 | return "Sample buffer: Unknown error"; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | RadioDevice *RadioDevice::make(double smpl_rt, bool skip_rx) |
| 986 | { |
| 987 | return new uhd_device(smpl_rt, skip_rx); |
| 988 | } |