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 | |
Thomas Tsou | 635f1bf | 2014-02-13 14:34:10 -0500 | [diff] [blame] | 35 | #define B2XX_CLK_RT 26e6 |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 36 | #define E1XX_CLK_RT 52e6 |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 37 | #define B100_BASE_RT 400000 |
Thomas Tsou | 03e6ecf | 2013-08-20 20:54:54 -0400 | [diff] [blame] | 38 | #define USRP2_BASE_RT 390625 |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame^] | 39 | #define USRP_TX_AMPL 0.3 |
| 40 | #define UMTRX_TX_AMPL 0.7 |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 41 | #define SAMPLE_BUF_SZ (1 << 20) |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 42 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 43 | /* |
| 44 | * UHD timeout value on streaming (re)start |
| 45 | * |
| 46 | * Allow some time for streaming to commence after the start command is issued, |
| 47 | * but consider a wait beyond one second to be a definite error condition. |
| 48 | */ |
| 49 | #define UHD_RESTART_TIMEOUT 1.0 |
| 50 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 51 | enum uhd_dev_type { |
| 52 | USRP1, |
| 53 | USRP2, |
| 54 | B100, |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 55 | B200, |
| 56 | B210, |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 57 | E1XX, |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 58 | E3XX, |
| 59 | X3XX, |
Thomas Tsou | c88d8d5 | 2013-08-21 17:55:54 -0400 | [diff] [blame] | 60 | UMTRX, |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 61 | NUM_USRP_TYPES, |
| 62 | }; |
kurtis.heimerl | ac0ee12 | 2011-11-28 06:25:58 +0000 | [diff] [blame] | 63 | |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 64 | struct uhd_dev_offset { |
| 65 | enum uhd_dev_type type; |
| 66 | int sps; |
| 67 | double offset; |
Thomas Tsou | 2c1f85a | 2013-11-13 22:53:15 -0500 | [diff] [blame] | 68 | const std::string desc; |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 69 | }; |
| 70 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 71 | /* |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 72 | * Tx / Rx sample offset values. In a perfect world, there is no group delay |
| 73 | * though analog components, and behaviour through digital filters exactly |
| 74 | * matches calculated values. In reality, there are unaccounted factors, |
| 75 | * which are captured in these empirically measured (using a loopback test) |
| 76 | * timing correction values. |
| 77 | * |
| 78 | * Notes: |
| 79 | * USRP1 with timestamps is not supported by UHD. |
| 80 | */ |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 81 | static struct uhd_dev_offset uhd_offsets[NUM_USRP_TYPES * 2] = { |
Thomas Tsou | 2c1f85a | 2013-11-13 22:53:15 -0500 | [diff] [blame] | 82 | { USRP1, 1, 0.0, "USRP1 not supported" }, |
| 83 | { USRP1, 4, 0.0, "USRP1 not supported"}, |
| 84 | { USRP2, 1, 1.2184e-4, "N2XX 1 SPS" }, |
| 85 | { USRP2, 4, 8.0230e-5, "N2XX 4 SPS" }, |
| 86 | { B100, 1, 1.2104e-4, "B100 1 SPS" }, |
| 87 | { B100, 4, 7.9307e-5, "B100 4 SPS" }, |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 88 | { B200, 1, 9.9692e-5, "B200 1 SPS" }, |
| 89 | { B200, 4, 6.9248e-5, "B200 4 SPS" }, |
| 90 | { B210, 1, 9.9692e-5, "B210 1 SPS" }, |
| 91 | { B210, 4, 6.9248e-5, "B210 4 SPS" }, |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 92 | { E1XX, 1, 9.5192e-5, "E1XX 1 SPS" }, |
| 93 | { E1XX, 4, 6.5571e-5, "E1XX 4 SPS" }, |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 94 | { E3XX, 1, 1.5000e-4, "E3XX 1 SPS" }, |
| 95 | { E3XX, 4, 1.2740e-4, "E3XX 4 SPS" }, |
| 96 | { X3XX, 1, 1.5360e-4, "X3XX 1 SPS"}, |
| 97 | { X3XX, 4, 1.1264e-4, "X3XX 4 SPS"}, |
Thomas Tsou | 2c1f85a | 2013-11-13 22:53:15 -0500 | [diff] [blame] | 98 | { UMTRX, 1, 9.9692e-5, "UmTRX 1 SPS" }, |
| 99 | { UMTRX, 4, 7.3846e-5, "UmTRX 4 SPS" }, |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 100 | }; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 101 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 102 | /* |
| 103 | * Offset handling for special cases. Currently used for UmTRX dual channel |
| 104 | * diversity receiver only. |
| 105 | */ |
| 106 | static struct uhd_dev_offset special_offsets[] = { |
| 107 | { UMTRX, 1, 8.0875e-5, "UmTRX diversity, 1 SPS" }, |
| 108 | { UMTRX, 4, 5.2103e-5, "UmTRX diversity, 4 SPS" }, |
| 109 | }; |
| 110 | |
| 111 | static double get_dev_offset(enum uhd_dev_type type, |
| 112 | int sps, bool diversity = false) |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 113 | { |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 114 | struct uhd_dev_offset *offset = NULL; |
Thomas Tsou | 2e622ff | 2013-11-15 18:35:04 -0500 | [diff] [blame] | 115 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 116 | /* Reject USRP1 */ |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 117 | if (type == USRP1) { |
| 118 | LOG(ERR) << "Invalid device type"; |
| 119 | return 0.0; |
| 120 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 121 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 122 | /* Special cases (e.g. diversity receiver) */ |
| 123 | if (diversity) { |
| 124 | if (type != UMTRX) { |
| 125 | LOG(ALERT) << "Diversity on UmTRX only"; |
| 126 | return 0.0; |
| 127 | } |
| 128 | |
| 129 | switch (sps) { |
| 130 | case 1: |
Thomas Tsou | 2e622ff | 2013-11-15 18:35:04 -0500 | [diff] [blame] | 131 | offset = &special_offsets[0]; |
| 132 | break; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 133 | case 4: |
| 134 | default: |
Thomas Tsou | 2e622ff | 2013-11-15 18:35:04 -0500 | [diff] [blame] | 135 | offset = &special_offsets[1]; |
| 136 | } |
| 137 | } else { |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 138 | /* Search for matching offset value */ |
| 139 | for (int i = 0; i < NUM_USRP_TYPES * 2; i++) { |
| 140 | if ((type == uhd_offsets[i].type) && |
| 141 | (sps == uhd_offsets[i].sps)) { |
| 142 | offset = &uhd_offsets[i]; |
| 143 | break; |
| 144 | } |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 148 | if (!offset) { |
| 149 | LOG(ERR) << "Invalid device configuration"; |
| 150 | return 0.0; |
| 151 | } |
| 152 | |
Thomas Tsou | 2e622ff | 2013-11-15 18:35:04 -0500 | [diff] [blame] | 153 | std::cout << "-- Setting " << offset->desc << std::endl; |
kurtis.heimerl | 7ac54b1 | 2011-11-26 03:17:49 +0000 | [diff] [blame] | 154 | |
Thomas Tsou | 2e622ff | 2013-11-15 18:35:04 -0500 | [diff] [blame] | 155 | return offset->offset; |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 156 | } |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 157 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 158 | /* |
| 159 | * Select sample rate based on device type and requested samples-per-symbol. |
| 160 | * The base rate is either GSM symbol rate, 270.833 kHz, or the minimum |
| 161 | * usable channel spacing of 400 kHz. |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 162 | */ |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 163 | static double select_rate(uhd_dev_type type, int sps, bool diversity = false) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 164 | { |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 165 | if (diversity && (type == UMTRX)) { |
| 166 | return GSMRATE * 4; |
| 167 | } else if (diversity) { |
| 168 | LOG(ALERT) << "Diversity supported on UmTRX only"; |
| 169 | return -9999.99; |
| 170 | } |
| 171 | |
Thomas Tsou | a57bc8a | 2013-09-05 08:16:47 +0800 | [diff] [blame] | 172 | if ((sps != 4) && (sps != 1)) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 173 | return -9999.99; |
| 174 | |
| 175 | switch (type) { |
| 176 | case USRP2: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 177 | case X3XX: |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 178 | return USRP2_BASE_RT * sps; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 179 | case B100: |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 180 | return B100_BASE_RT * sps; |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 181 | case B200: |
| 182 | case B210: |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 183 | case E1XX: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 184 | case E3XX: |
Thomas Tsou | c88d8d5 | 2013-08-21 17:55:54 -0400 | [diff] [blame] | 185 | case UMTRX: |
| 186 | return GSMRATE * sps; |
| 187 | default: |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | |
| 191 | LOG(ALERT) << "Unknown device type " << type; |
| 192 | return -9999.99; |
| 193 | } |
| 194 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 195 | /** Timestamp conversion |
| 196 | @param timestamp a UHD or OpenBTS timestamp |
| 197 | @param rate sample rate |
| 198 | @return the converted timestamp |
| 199 | */ |
| 200 | uhd::time_spec_t convert_time(TIMESTAMP ticks, double rate) |
| 201 | { |
| 202 | double secs = (double) ticks / rate; |
| 203 | return uhd::time_spec_t(secs); |
| 204 | } |
| 205 | |
| 206 | TIMESTAMP convert_time(uhd::time_spec_t ts, double rate) |
| 207 | { |
kurtis.heimerl | c7cb817 | 2011-11-26 03:17:26 +0000 | [diff] [blame] | 208 | TIMESTAMP ticks = ts.get_full_secs() * rate; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 209 | return ts.get_tick_count(rate) + ticks; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | Sample Buffer - Allows reading and writing of timed samples using OpenBTS |
| 214 | or UHD style timestamps. Time conversions are handled |
| 215 | internally or accessable through the static convert calls. |
| 216 | */ |
| 217 | class smpl_buf { |
| 218 | public: |
| 219 | /** Sample buffer constructor |
| 220 | @param len number of 32-bit samples the buffer should hold |
| 221 | @param rate sample clockrate |
| 222 | @param timestamp |
| 223 | */ |
| 224 | smpl_buf(size_t len, double rate); |
| 225 | ~smpl_buf(); |
| 226 | |
| 227 | /** Query number of samples available for reading |
| 228 | @param timestamp time of first sample |
| 229 | @return number of available samples or error |
| 230 | */ |
| 231 | ssize_t avail_smpls(TIMESTAMP timestamp) const; |
| 232 | ssize_t avail_smpls(uhd::time_spec_t timestamp) const; |
| 233 | |
| 234 | /** Read and write |
| 235 | @param buf pointer to buffer |
| 236 | @param len number of samples desired to read or write |
| 237 | @param timestamp time of first stample |
| 238 | @return number of actual samples read or written or error |
| 239 | */ |
| 240 | ssize_t read(void *buf, size_t len, TIMESTAMP timestamp); |
| 241 | ssize_t read(void *buf, size_t len, uhd::time_spec_t timestamp); |
| 242 | ssize_t write(void *buf, size_t len, TIMESTAMP timestamp); |
| 243 | ssize_t write(void *buf, size_t len, uhd::time_spec_t timestamp); |
| 244 | |
| 245 | /** Buffer status string |
| 246 | @return a formatted string describing internal buffer state |
| 247 | */ |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 248 | std::string str_status(size_t ts) const; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 249 | |
| 250 | /** Formatted error string |
| 251 | @param code an error code |
| 252 | @return a formatted error string |
| 253 | */ |
| 254 | static std::string str_code(ssize_t code); |
| 255 | |
| 256 | enum err_code { |
| 257 | ERROR_TIMESTAMP = -1, |
| 258 | ERROR_READ = -2, |
| 259 | ERROR_WRITE = -3, |
| 260 | ERROR_OVERFLOW = -4 |
| 261 | }; |
| 262 | |
| 263 | private: |
| 264 | uint32_t *data; |
| 265 | size_t buf_len; |
| 266 | |
| 267 | double clk_rt; |
| 268 | |
| 269 | TIMESTAMP time_start; |
| 270 | TIMESTAMP time_end; |
| 271 | |
| 272 | size_t data_start; |
| 273 | size_t data_end; |
| 274 | }; |
| 275 | |
| 276 | /* |
| 277 | uhd_device - UHD implementation of the Device interface. Timestamped samples |
| 278 | are sent to and received from the device. An intermediate buffer |
| 279 | on the receive side collects and aligns packets of samples. |
| 280 | Events and errors such as underruns are reported asynchronously |
| 281 | by the device and received in a separate thread. |
| 282 | */ |
| 283 | class uhd_device : public RadioDevice { |
| 284 | public: |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 285 | uhd_device(size_t sps, size_t chans, bool diversity, double offset); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 286 | ~uhd_device(); |
| 287 | |
Thomas Tsou | 010fff7 | 2013-10-16 00:31:18 -0400 | [diff] [blame] | 288 | int open(const std::string &args, bool extref); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 289 | bool start(); |
| 290 | bool stop(); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 291 | bool restart(); |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 292 | void setPriority(float prio); |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 293 | enum TxWindowType getWindowType() { return tx_window; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 294 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 295 | int readSamples(std::vector<short *> &bufs, int len, bool *overrun, |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 296 | TIMESTAMP timestamp, bool *underrun, unsigned *RSSI); |
| 297 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 298 | int writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 299 | TIMESTAMP timestamp, bool isControl); |
| 300 | |
| 301 | bool updateAlignment(TIMESTAMP timestamp); |
| 302 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 303 | bool setTxFreq(double wFreq, size_t chan); |
| 304 | bool setRxFreq(double wFreq, size_t chan); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 305 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 306 | inline TIMESTAMP initialWriteTimestamp() { return ts_initial * sps; } |
| 307 | inline TIMESTAMP initialReadTimestamp() { return ts_initial; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 308 | |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame^] | 309 | double fullScaleInputValue(); |
| 310 | double fullScaleOutputValue(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 311 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 312 | double setRxGain(double db, size_t chan); |
| 313 | double getRxGain(size_t chan); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 314 | double maxRxGain(void) { return rx_gain_max; } |
| 315 | double minRxGain(void) { return rx_gain_min; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 316 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 317 | double setTxGain(double db, size_t chan); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 318 | double maxTxGain(void) { return tx_gain_max; } |
| 319 | double minTxGain(void) { return tx_gain_min; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 320 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 321 | double getTxFreq(size_t chan); |
| 322 | double getRxFreq(size_t chan); |
| 323 | double getRxFreq(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 324 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 325 | inline double getSampleRate() { return tx_rate; } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 326 | inline double numberRead() { return rx_pkt_cnt; } |
| 327 | inline double numberWritten() { return 0; } |
| 328 | |
| 329 | /** Receive and process asynchronous message |
| 330 | @return true if message received or false on timeout or error |
| 331 | */ |
| 332 | bool recv_async_msg(); |
| 333 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 334 | enum err_code { |
| 335 | ERROR_TIMING = -1, |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 336 | ERROR_TIMEOUT = -2, |
| 337 | ERROR_UNRECOVERABLE = -3, |
| 338 | ERROR_UNHANDLED = -4, |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 341 | private: |
kurtis.heimerl | 856bbbe | 2011-12-15 00:50:16 +0000 | [diff] [blame] | 342 | uhd::usrp::multi_usrp::sptr usrp_dev; |
Thomas Tsou | b4cb4e2 | 2013-04-05 15:11:35 -0400 | [diff] [blame] | 343 | uhd::tx_streamer::sptr tx_stream; |
| 344 | uhd::rx_streamer::sptr rx_stream; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 345 | enum TxWindowType tx_window; |
| 346 | enum uhd_dev_type dev_type; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 347 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 348 | size_t sps, chans; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 349 | double tx_rate, rx_rate; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 350 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 351 | double tx_gain_min, tx_gain_max; |
| 352 | double rx_gain_min, rx_gain_max; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 353 | double offset; |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 354 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 355 | std::vector<double> tx_gains, rx_gains; |
| 356 | std::vector<double> tx_freqs, rx_freqs; |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 357 | size_t tx_spp, rx_spp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 358 | |
| 359 | bool started; |
| 360 | bool aligned; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 361 | |
| 362 | size_t rx_pkt_cnt; |
| 363 | size_t drop_cnt; |
| 364 | uhd::time_spec_t prev_ts; |
| 365 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 366 | TIMESTAMP ts_initial, ts_offset; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 367 | std::vector<smpl_buf *> rx_buffers; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 368 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 369 | void init_gains(); |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 370 | int set_master_clk(double rate); |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 371 | int set_rates(double tx_rate, double rx_rate); |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 372 | bool parse_dev_type(); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 373 | bool flush_recv(size_t num_pkts); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 374 | 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] | 375 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 376 | std::string str_code(uhd::rx_metadata_t metadata); |
| 377 | std::string str_code(uhd::async_metadata_t metadata); |
| 378 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 379 | uhd::tune_request_t select_freq(double wFreq, size_t chan, bool tx); |
| 380 | bool set_freq(double freq, size_t chan, bool tx); |
| 381 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 382 | Thread *async_event_thrd; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 383 | bool diversity; |
Tom Tsou | 93b7f37 | 2014-12-15 20:23:33 -0800 | [diff] [blame] | 384 | Mutex tune_lock; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | void *async_event_loop(uhd_device *dev) |
| 388 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 389 | dev->setPriority(0.43); |
| 390 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 391 | while (1) { |
| 392 | dev->recv_async_msg(); |
| 393 | pthread_testcancel(); |
| 394 | } |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 395 | |
| 396 | return NULL; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 397 | } |
| 398 | |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 399 | /* |
| 400 | Catch and drop underrun 'U' and overrun 'O' messages from stdout |
| 401 | since we already report using the logging facility. Direct |
| 402 | everything else appropriately. |
| 403 | */ |
| 404 | void uhd_msg_handler(uhd::msg::type_t type, const std::string &msg) |
| 405 | { |
| 406 | switch (type) { |
| 407 | case uhd::msg::status: |
| 408 | LOG(INFO) << msg; |
| 409 | break; |
| 410 | case uhd::msg::warning: |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 411 | LOG(WARNING) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 412 | break; |
| 413 | case uhd::msg::error: |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 414 | LOG(ERR) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 415 | break; |
| 416 | case uhd::msg::fastpath: |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 421 | static void thread_enable_cancel(bool cancel) |
| 422 | { |
| 423 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 424 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 425 | } |
| 426 | |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 427 | uhd_device::uhd_device(size_t sps, size_t chans, bool diversity, double offset) |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 428 | : tx_gain_min(0.0), tx_gain_max(0.0), |
| 429 | rx_gain_min(0.0), rx_gain_max(0.0), |
| 430 | tx_spp(0), rx_spp(0), |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 431 | started(false), aligned(false), rx_pkt_cnt(0), drop_cnt(0), |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 432 | prev_ts(0,0), ts_initial(0), ts_offset(0) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 433 | { |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 434 | this->sps = sps; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 435 | this->chans = chans; |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 436 | this->offset = offset; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 437 | this->diversity = diversity; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | uhd_device::~uhd_device() |
| 441 | { |
| 442 | stop(); |
| 443 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 444 | for (size_t i = 0; i < rx_buffers.size(); i++) |
| 445 | delete rx_buffers[i]; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 446 | } |
| 447 | |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 448 | void uhd_device::init_gains() |
| 449 | { |
| 450 | uhd::gain_range_t range; |
| 451 | |
| 452 | range = usrp_dev->get_tx_gain_range(); |
| 453 | tx_gain_min = range.start(); |
| 454 | tx_gain_max = range.stop(); |
| 455 | |
| 456 | range = usrp_dev->get_rx_gain_range(); |
| 457 | rx_gain_min = range.start(); |
| 458 | rx_gain_max = range.stop(); |
| 459 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 460 | for (size_t i = 0; i < tx_gains.size(); i++) { |
| 461 | usrp_dev->set_tx_gain((tx_gain_min + tx_gain_max) / 2, i); |
| 462 | tx_gains[i] = usrp_dev->get_tx_gain(i); |
| 463 | } |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 464 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 465 | for (size_t i = 0; i < rx_gains.size(); i++) { |
| 466 | usrp_dev->set_rx_gain((rx_gain_min + rx_gain_max) / 2, i); |
| 467 | rx_gains[i] = usrp_dev->get_rx_gain(i); |
| 468 | } |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 469 | |
| 470 | return; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 471 | |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 474 | int uhd_device::set_master_clk(double clk_rate) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 475 | { |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 476 | double actual, offset, limit = 1.0; |
kurtis.heimerl | ac0ee12 | 2011-11-28 06:25:58 +0000 | [diff] [blame] | 477 | |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 478 | try { |
| 479 | usrp_dev->set_master_clock_rate(clk_rate); |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 480 | } catch (const std::exception &ex) { |
| 481 | LOG(ALERT) << "UHD clock rate setting failed: " << clk_rate; |
| 482 | LOG(ALERT) << ex.what(); |
| 483 | return -1; |
| 484 | } |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 485 | |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 486 | actual = usrp_dev->get_master_clock_rate(); |
| 487 | offset = fabs(clk_rate - actual); |
| 488 | |
| 489 | if (offset > limit) { |
kurtis.heimerl | e332032 | 2011-11-28 06:26:08 +0000 | [diff] [blame] | 490 | LOG(ALERT) << "Failed to set master clock rate"; |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 491 | LOG(ALERT) << "Requested clock rate " << clk_rate; |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 492 | LOG(ALERT) << "Actual clock rate " << actual; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 493 | return -1; |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 494 | } |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 499 | int uhd_device::set_rates(double tx_rate, double rx_rate) |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 500 | { |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 501 | double offset_limit = 1.0; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 502 | double tx_offset, rx_offset; |
| 503 | |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 504 | /* B2XX and E1xx are the only device where we set FPGA clocking */ |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 505 | if ((dev_type == B200) || (dev_type == B210) || (dev_type == E3XX)) { |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 506 | if (set_master_clk(B2XX_CLK_RT) < 0) |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 507 | return -1; |
| 508 | } |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 509 | else if (dev_type == E1XX) { |
| 510 | if (set_master_clk(E1XX_CLK_RT) < 0) |
| 511 | return -1; |
| 512 | } |
kurtis.heimerl | d3e2590 | 2011-11-26 03:18:13 +0000 | [diff] [blame] | 513 | |
| 514 | // Set sample rates |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 515 | try { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 516 | usrp_dev->set_tx_rate(tx_rate); |
| 517 | usrp_dev->set_rx_rate(rx_rate); |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 518 | } catch (const std::exception &ex) { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 519 | LOG(ALERT) << "UHD rate setting failed"; |
Thomas Tsou | 7e06806 | 2013-04-08 19:39:37 -0400 | [diff] [blame] | 520 | LOG(ALERT) << ex.what(); |
| 521 | return -1; |
| 522 | } |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 523 | this->tx_rate = usrp_dev->get_tx_rate(); |
| 524 | this->rx_rate = usrp_dev->get_rx_rate(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 525 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 526 | tx_offset = fabs(this->tx_rate - tx_rate); |
| 527 | rx_offset = fabs(this->rx_rate - rx_rate); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 528 | if ((tx_offset > offset_limit) || (rx_offset > offset_limit)) { |
kurtis.heimerl | e332032 | 2011-11-28 06:26:08 +0000 | [diff] [blame] | 529 | LOG(ALERT) << "Actual sample rate differs from desired rate"; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 530 | LOG(ALERT) << "Tx/Rx (" << this->tx_rate << "/" |
| 531 | << this->rx_rate << ")"; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 532 | return -1; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 533 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 534 | |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 535 | return 0; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 538 | double uhd_device::setTxGain(double db, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 539 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 540 | if (chan >= tx_gains.size()) { |
| 541 | LOG(ALERT) << "Requested non-existent channel" << chan; |
| 542 | return 0.0f; |
| 543 | } |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 544 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 545 | usrp_dev->set_tx_gain(db, chan); |
| 546 | tx_gains[chan] = usrp_dev->get_tx_gain(chan); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 547 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 548 | LOG(INFO) << "Set TX gain to " << tx_gains[chan] << "dB"; |
| 549 | |
| 550 | return tx_gains[chan]; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 553 | double uhd_device::setRxGain(double db, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 554 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 555 | if (chan >= rx_gains.size()) { |
| 556 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 557 | return 0.0f; |
| 558 | } |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 559 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 560 | usrp_dev->set_rx_gain(db, chan); |
| 561 | rx_gains[chan] = usrp_dev->get_rx_gain(chan); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 562 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 563 | LOG(INFO) << "Set RX gain to " << rx_gains[chan] << "dB"; |
| 564 | |
| 565 | return rx_gains[chan]; |
| 566 | } |
| 567 | |
| 568 | double uhd_device::getRxGain(size_t chan) |
| 569 | { |
| 570 | if (chan >= rx_gains.size()) { |
| 571 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 572 | return 0.0f; |
| 573 | } |
| 574 | |
| 575 | return rx_gains[chan]; |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 576 | } |
| 577 | |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 578 | /* |
| 579 | Parse the UHD device tree and mboard name to find out what device we're |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 580 | dealing with. We need the window type so that the transceiver knows how to |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 581 | deal with the transport latency. Reject the USRP1 because UHD doesn't |
| 582 | support timestamped samples with it. |
| 583 | */ |
| 584 | bool uhd_device::parse_dev_type() |
| 585 | { |
| 586 | std::string mboard_str, dev_str; |
| 587 | uhd::property_tree::sptr prop_tree; |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 588 | size_t usrp1_str, usrp2_str, e100_str, e110_str, e310_str, |
| 589 | b100_str, b200_str, b210_str, x300_str, x310_str, umtrx_str; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 590 | |
| 591 | prop_tree = usrp_dev->get_device()->get_tree(); |
| 592 | dev_str = prop_tree->access<std::string>("/name").get(); |
| 593 | mboard_str = usrp_dev->get_mboard_name(); |
| 594 | |
| 595 | usrp1_str = dev_str.find("USRP1"); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 596 | usrp2_str = dev_str.find("USRP2"); |
| 597 | b100_str = mboard_str.find("B100"); |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 598 | b200_str = mboard_str.find("B200"); |
Thomas Tsou | 69d14c9 | 2013-10-11 14:27:35 -0400 | [diff] [blame] | 599 | b210_str = mboard_str.find("B210"); |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 600 | e100_str = mboard_str.find("E100"); |
| 601 | e110_str = mboard_str.find("E110"); |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 602 | e310_str = mboard_str.find("E310"); |
| 603 | x300_str = mboard_str.find("X300"); |
| 604 | x310_str = mboard_str.find("X310"); |
Thomas Tsou | c88d8d5 | 2013-08-21 17:55:54 -0400 | [diff] [blame] | 605 | umtrx_str = dev_str.find("UmTRX"); |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 606 | |
| 607 | if (usrp1_str != std::string::npos) { |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 608 | LOG(ALERT) << "USRP1 is not supported using the UHD driver"; |
| 609 | LOG(ALERT) << "Please compile with GNU Radio libusrp support"; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 610 | dev_type = USRP1; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
| 613 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 614 | if (b100_str != std::string::npos) { |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 615 | tx_window = TX_WINDOW_USRP1; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 616 | dev_type = B100; |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 617 | } else if (b200_str != std::string::npos) { |
Thomas Tsou | acc22fa | 2013-11-09 02:25:41 -0500 | [diff] [blame] | 618 | tx_window = TX_WINDOW_USRP1; |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 619 | dev_type = B200; |
Thomas Tsou | 69d14c9 | 2013-10-11 14:27:35 -0400 | [diff] [blame] | 620 | } else if (b210_str != std::string::npos) { |
Thomas Tsou | acc22fa | 2013-11-09 02:25:41 -0500 | [diff] [blame] | 621 | tx_window = TX_WINDOW_USRP1; |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 622 | dev_type = B210; |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 623 | } else if (e100_str != std::string::npos) { |
| 624 | tx_window = TX_WINDOW_FIXED; |
| 625 | dev_type = E1XX; |
| 626 | } else if (e110_str != std::string::npos) { |
| 627 | tx_window = TX_WINDOW_FIXED; |
| 628 | dev_type = E1XX; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 629 | } else if (usrp2_str != std::string::npos) { |
Thomas Tsou | acc22fa | 2013-11-09 02:25:41 -0500 | [diff] [blame] | 630 | tx_window = TX_WINDOW_FIXED; |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 631 | dev_type = USRP2; |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 632 | } else if (e310_str != std::string::npos) { |
| 633 | tx_window = TX_WINDOW_FIXED; |
| 634 | dev_type = E3XX; |
| 635 | } else if (x300_str != std::string::npos) { |
| 636 | tx_window = TX_WINDOW_FIXED; |
| 637 | dev_type = X3XX; |
| 638 | } else if (x310_str != std::string::npos) { |
| 639 | tx_window = TX_WINDOW_FIXED; |
| 640 | dev_type = X3XX; |
Thomas Tsou | c88d8d5 | 2013-08-21 17:55:54 -0400 | [diff] [blame] | 641 | } else if (umtrx_str != std::string::npos) { |
Thomas Tsou | acc22fa | 2013-11-09 02:25:41 -0500 | [diff] [blame] | 642 | tx_window = TX_WINDOW_FIXED; |
Thomas Tsou | c88d8d5 | 2013-08-21 17:55:54 -0400 | [diff] [blame] | 643 | dev_type = UMTRX; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 644 | } else { |
Thomas Tsou | 092f757 | 2013-04-04 17:04:39 -0400 | [diff] [blame] | 645 | LOG(ALERT) << "Unknown UHD device type " << dev_str; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 646 | return false; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Thomas Tsou | acc22fa | 2013-11-09 02:25:41 -0500 | [diff] [blame] | 649 | if (tx_window == TX_WINDOW_USRP1) { |
| 650 | LOG(INFO) << "Using USRP1 type transmit window for " |
| 651 | << dev_str << " " << mboard_str; |
| 652 | } else { |
| 653 | LOG(INFO) << "Using fixed transmit window for " |
| 654 | << dev_str << " " << mboard_str; |
| 655 | } |
| 656 | |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 657 | return true; |
| 658 | } |
| 659 | |
Thomas Tsou | 010fff7 | 2013-10-16 00:31:18 -0400 | [diff] [blame] | 660 | int uhd_device::open(const std::string &args, bool extref) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 661 | { |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 662 | // Find UHD devices |
ttsou | f60dafa | 2012-10-22 00:07:14 +0000 | [diff] [blame] | 663 | uhd::device_addr_t addr(args); |
| 664 | uhd::device_addrs_t dev_addrs = uhd::device::find(addr); |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 665 | if (dev_addrs.size() == 0) { |
ttsou | f60dafa | 2012-10-22 00:07:14 +0000 | [diff] [blame] | 666 | LOG(ALERT) << "No UHD devices found with address '" << args << "'"; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 667 | return -1; |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // Use the first found device |
| 671 | LOG(INFO) << "Using discovered UHD device " << dev_addrs[0].to_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 672 | try { |
kurtis.heimerl | 856bbbe | 2011-12-15 00:50:16 +0000 | [diff] [blame] | 673 | usrp_dev = uhd::usrp::multi_usrp::make(dev_addrs[0]); |
kurtis.heimerl | e380af3 | 2011-11-26 03:18:55 +0000 | [diff] [blame] | 674 | } catch(...) { |
ttsou | 3b5c0c1 | 2012-02-14 17:58:11 +0000 | [diff] [blame] | 675 | LOG(ALERT) << "UHD make failed, device " << dev_addrs[0].to_string(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 676 | return -1; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 677 | } |
| 678 | |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 679 | // Check for a valid device type and set bus type |
| 680 | if (!parse_dev_type()) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 681 | return -1; |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 682 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 683 | // Verify and set channels |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 684 | if ((dev_type == B210) && (chans == 2)) { |
| 685 | } else if ((dev_type == UMTRX) && (chans == 2)) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 686 | uhd::usrp::subdev_spec_t subdev_spec("A:0 B:0"); |
| 687 | usrp_dev->set_tx_subdev_spec(subdev_spec); |
| 688 | usrp_dev->set_rx_subdev_spec(subdev_spec); |
| 689 | } else if (chans != 1) { |
| 690 | LOG(ALERT) << "Invalid channel combination for device"; |
| 691 | return -1; |
| 692 | } |
| 693 | |
| 694 | tx_freqs.resize(chans); |
| 695 | rx_freqs.resize(chans); |
| 696 | tx_gains.resize(chans); |
| 697 | rx_gains.resize(chans); |
| 698 | rx_buffers.resize(chans); |
| 699 | |
Thomas Tsou | 010fff7 | 2013-10-16 00:31:18 -0400 | [diff] [blame] | 700 | if (extref) |
Thomas Tsou | 0169b31 | 2013-10-29 19:25:15 -0400 | [diff] [blame] | 701 | usrp_dev->set_clock_source("external"); |
Thomas Tsou | 010fff7 | 2013-10-16 00:31:18 -0400 | [diff] [blame] | 702 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 703 | // Set rates |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 704 | double _rx_rate; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 705 | double _tx_rate = select_rate(dev_type, sps); |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 706 | if (diversity) |
| 707 | _rx_rate = select_rate(dev_type, 1, true); |
| 708 | else |
| 709 | _rx_rate = _tx_rate / sps; |
| 710 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 711 | if ((_tx_rate < 0.0) || (_rx_rate < 0.0)) |
| 712 | return -1; |
| 713 | if (set_rates(_tx_rate, _rx_rate) < 0) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 714 | return -1; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 715 | |
Thomas Tsou | cecfb2e | 2014-03-27 13:44:09 -0400 | [diff] [blame] | 716 | /* Create TX and RX streamers */ |
| 717 | uhd::stream_args_t stream_args("sc16"); |
| 718 | for (size_t i = 0; i < chans; i++) |
| 719 | stream_args.channels.push_back(i); |
| 720 | |
| 721 | tx_stream = usrp_dev->get_tx_stream(stream_args); |
| 722 | rx_stream = usrp_dev->get_rx_stream(stream_args); |
| 723 | |
| 724 | /* Number of samples per over-the-wire packet */ |
| 725 | tx_spp = tx_stream->get_max_num_samps(); |
| 726 | rx_spp = rx_stream->get_max_num_samps(); |
| 727 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 728 | // Create receive buffer |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 729 | size_t buf_len = SAMPLE_BUF_SZ / sizeof(uint32_t); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 730 | for (size_t i = 0; i < rx_buffers.size(); i++) |
| 731 | rx_buffers[i] = new smpl_buf(buf_len, rx_rate); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 732 | |
| 733 | // Set receive chain sample offset |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 734 | double offset = get_dev_offset(dev_type, sps, diversity); |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 735 | if (offset == 0.0) { |
| 736 | LOG(ERR) << "Unsupported configuration, no correction applied"; |
| 737 | ts_offset = 0; |
| 738 | } else { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 739 | ts_offset = (TIMESTAMP) (offset * rx_rate); |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 740 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 741 | |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 742 | // Initialize and shadow gain values |
| 743 | init_gains(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 744 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 745 | // Print configuration |
kurtis.heimerl | 3998da7 | 2011-11-26 03:19:00 +0000 | [diff] [blame] | 746 | LOG(INFO) << "\n" << usrp_dev->get_pp_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 747 | |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 748 | if (diversity) |
| 749 | return DIVERSITY; |
| 750 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 751 | switch (dev_type) { |
| 752 | case B100: |
| 753 | return RESAMP_64M; |
| 754 | case USRP2: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 755 | case X3XX: |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 756 | return RESAMP_100M; |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 757 | case B200: |
| 758 | case B210: |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 759 | case E1XX: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 760 | case E3XX: |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 761 | default: |
| 762 | break; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 763 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 764 | |
| 765 | return NORMAL; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 766 | } |
| 767 | |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 768 | bool uhd_device::flush_recv(size_t num_pkts) |
| 769 | { |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 770 | uhd::rx_metadata_t md; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 771 | size_t num_smpls; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 772 | float timeout = UHD_RESTART_TIMEOUT; |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 773 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 774 | std::vector<std::vector<short> > |
| 775 | pkt_bufs(chans, std::vector<short>(2 * rx_spp)); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 776 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 777 | std::vector<short *> pkt_ptrs; |
| 778 | for (size_t i = 0; i < pkt_bufs.size(); i++) |
| 779 | pkt_ptrs.push_back(&pkt_bufs[i].front()); |
| 780 | |
| 781 | ts_initial = 0; |
| 782 | while (!ts_initial || (num_pkts-- > 0)) { |
| 783 | num_smpls = rx_stream->recv(pkt_ptrs, rx_spp, md, |
Thomas Tsou | b4cb4e2 | 2013-04-05 15:11:35 -0400 | [diff] [blame] | 784 | timeout, true); |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 785 | if (!num_smpls) { |
| 786 | switch (md.error_code) { |
| 787 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 788 | LOG(ALERT) << "Device timed out"; |
| 789 | return false; |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 790 | default: |
| 791 | continue; |
| 792 | } |
| 793 | } |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 794 | |
| 795 | ts_initial = convert_time(md.time_spec, rx_rate); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 798 | LOG(INFO) << "Initial timestamp " << ts_initial << std::endl; |
| 799 | |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 800 | return true; |
| 801 | } |
| 802 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 803 | bool uhd_device::restart() |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 804 | { |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 805 | /* Allow 100 ms delay to align multi-channel streams */ |
| 806 | double delay = 0.1; |
| 807 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 808 | aligned = false; |
| 809 | |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 810 | uhd::time_spec_t current = usrp_dev->get_time_now(); |
| 811 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 812 | uhd::stream_cmd_t cmd = uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS; |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 813 | cmd.stream_now = false; |
| 814 | cmd.time_spec = uhd::time_spec_t(current.get_real_secs() + delay); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 815 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 816 | usrp_dev->issue_stream_cmd(cmd); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 817 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 818 | return flush_recv(10); |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 819 | } |
| 820 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 821 | bool uhd_device::start() |
| 822 | { |
| 823 | LOG(INFO) << "Starting USRP..."; |
| 824 | |
| 825 | if (started) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 826 | LOG(ERR) << "Device already started"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 827 | return false; |
| 828 | } |
| 829 | |
Thomas Tsou | 61b4a6a | 2013-10-15 20:31:44 -0400 | [diff] [blame] | 830 | // Register msg handler |
| 831 | uhd::msg::register_handler(&uhd_msg_handler); |
| 832 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 833 | // Start asynchronous event (underrun check) loop |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 834 | async_event_thrd = new Thread(); |
| 835 | async_event_thrd->start((void * (*)(void*))async_event_loop, (void*)this); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 836 | |
| 837 | // Start streaming |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 838 | if (!restart()) |
| 839 | return false; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 840 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 841 | // Display usrp time |
| 842 | double time_now = usrp_dev->get_time_now().get_real_secs(); |
| 843 | LOG(INFO) << "The current time is " << time_now << " seconds"; |
| 844 | |
| 845 | started = true; |
| 846 | return true; |
| 847 | } |
| 848 | |
| 849 | bool uhd_device::stop() |
| 850 | { |
Thomas Tsou | 2c79110 | 2013-11-15 23:00:28 -0500 | [diff] [blame] | 851 | if (!started) |
| 852 | return false; |
| 853 | |
| 854 | uhd::stream_cmd_t stream_cmd = |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 855 | uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; |
| 856 | |
| 857 | usrp_dev->issue_stream_cmd(stream_cmd); |
| 858 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 859 | async_event_thrd->cancel(); |
| 860 | async_event_thrd->join(); |
| 861 | delete async_event_thrd; |
| 862 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 863 | started = false; |
| 864 | return true; |
| 865 | } |
| 866 | |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 867 | void uhd_device::setPriority(float prio) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 868 | { |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 869 | uhd::set_thread_priority_safe(prio); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 873 | 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] | 874 | { |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 875 | uhd::time_spec_t ts; |
| 876 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 877 | if (!num_smpls) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 878 | LOG(ERR) << str_code(md); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 879 | |
| 880 | switch (md.error_code) { |
| 881 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 882 | LOG(ALERT) << "UHD: Receive timed out"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 883 | return ERROR_TIMEOUT; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 884 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 885 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 886 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 887 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 888 | default: |
| 889 | return ERROR_UNHANDLED; |
| 890 | } |
| 891 | } |
| 892 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 893 | // Missing timestamp |
| 894 | if (!md.has_time_spec) { |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 895 | LOG(ALERT) << "UHD: Received packet missing timestamp"; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 896 | return ERROR_UNRECOVERABLE; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 897 | } |
| 898 | |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 899 | ts = md.time_spec; |
| 900 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 901 | // Monotonicity check |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 902 | if (ts < prev_ts) { |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 903 | LOG(ALERT) << "UHD: Loss of monotonic time"; |
ttsou | 724eb36 | 2012-03-13 02:20:01 +0000 | [diff] [blame] | 904 | LOG(ALERT) << "Current time: " << ts.get_real_secs() << ", " |
| 905 | << "Previous time: " << prev_ts.get_real_secs(); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 906 | return ERROR_TIMING; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 907 | } else { |
kurtis.heimerl | 3cc1da9 | 2011-11-26 03:17:02 +0000 | [diff] [blame] | 908 | prev_ts = ts; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 914 | int uhd_device::readSamples(std::vector<short *> &bufs, int len, bool *overrun, |
| 915 | TIMESTAMP timestamp, bool *underrun, unsigned *RSSI) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 916 | { |
| 917 | ssize_t rc; |
| 918 | uhd::time_spec_t ts; |
| 919 | uhd::rx_metadata_t metadata; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 920 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 921 | if (bufs.size() != chans) { |
| 922 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 923 | return -1; |
| 924 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 925 | |
Thomas Tsou | cf910dc | 2013-10-25 14:42:00 +0000 | [diff] [blame] | 926 | *overrun = false; |
| 927 | *underrun = false; |
| 928 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 929 | // Shift read time with respect to transmit clock |
| 930 | timestamp += ts_offset; |
| 931 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 932 | ts = convert_time(timestamp, rx_rate); |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 933 | LOG(DEBUG) << "Requested timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 934 | |
| 935 | // Check that timestamp is valid |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 936 | rc = rx_buffers[0]->avail_smpls(timestamp); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 937 | if (rc < 0) { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 938 | LOG(ERR) << rx_buffers[0]->str_code(rc); |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 939 | LOG(ERR) << rx_buffers[0]->str_status(timestamp); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 943 | // Create vector buffer |
| 944 | std::vector<std::vector<short> > |
| 945 | pkt_bufs(chans, std::vector<short>(2 * rx_spp)); |
| 946 | |
| 947 | std::vector<short *> pkt_ptrs; |
| 948 | for (size_t i = 0; i < pkt_bufs.size(); i++) |
| 949 | pkt_ptrs.push_back(&pkt_bufs[i].front()); |
| 950 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 951 | // Receive samples from the usrp until we have enough |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 952 | while (rx_buffers[0]->avail_smpls(timestamp) < len) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 953 | thread_enable_cancel(false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 954 | size_t num_smpls = rx_stream->recv(pkt_ptrs, rx_spp, |
| 955 | metadata, 0.1, true); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 956 | thread_enable_cancel(true); |
| 957 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 958 | rx_pkt_cnt++; |
| 959 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 960 | // Check for errors |
| 961 | rc = check_rx_md_err(metadata, num_smpls); |
| 962 | switch (rc) { |
| 963 | case ERROR_UNRECOVERABLE: |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 964 | LOG(ALERT) << "UHD: Version " << uhd::get_version_string(); |
| 965 | LOG(ALERT) << "UHD: Unrecoverable error, exiting..."; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 966 | exit(-1); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 967 | case ERROR_TIMEOUT: |
| 968 | // Assume stopping condition |
| 969 | return 0; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 970 | case ERROR_TIMING: |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 971 | restart(); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 972 | case ERROR_UNHANDLED: |
kurtis.heimerl | 513a629 | 2011-11-26 03:18:36 +0000 | [diff] [blame] | 973 | continue; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 974 | } |
| 975 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 976 | ts = metadata.time_spec; |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 977 | LOG(DEBUG) << "Received timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 978 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 979 | for (size_t i = 0; i < rx_buffers.size(); i++) { |
| 980 | rc = rx_buffers[i]->write((short *) &pkt_bufs[i].front(), |
| 981 | num_smpls, |
| 982 | metadata.time_spec); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 983 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 984 | // Continue on local overrun, exit on other errors |
| 985 | if ((rc < 0)) { |
| 986 | LOG(ERR) << rx_buffers[i]->str_code(rc); |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 987 | LOG(ERR) << rx_buffers[i]->str_status(timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 988 | if (rc != smpl_buf::ERROR_OVERFLOW) |
| 989 | return 0; |
| 990 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
| 994 | // We have enough samples |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 995 | for (size_t i = 0; i < rx_buffers.size(); i++) { |
| 996 | rc = rx_buffers[i]->read(bufs[i], len, timestamp); |
| 997 | if ((rc < 0) || (rc != len)) { |
| 998 | LOG(ERR) << rx_buffers[i]->str_code(rc); |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 999 | LOG(ERR) << rx_buffers[i]->str_status(timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | return len; |
| 1005 | } |
| 1006 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1007 | int uhd_device::writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
| 1008 | unsigned long long timestamp,bool isControl) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1009 | { |
| 1010 | uhd::tx_metadata_t metadata; |
| 1011 | metadata.has_time_spec = true; |
| 1012 | metadata.start_of_burst = false; |
| 1013 | metadata.end_of_burst = false; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 1014 | metadata.time_spec = convert_time(timestamp, tx_rate); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1015 | |
Thomas Tsou | cf910dc | 2013-10-25 14:42:00 +0000 | [diff] [blame] | 1016 | *underrun = false; |
| 1017 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1018 | // No control packets |
| 1019 | if (isControl) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 1020 | LOG(ERR) << "Control packets not supported"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1024 | if (bufs.size() != chans) { |
| 1025 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1029 | // Drop a fixed number of packets (magic value) |
| 1030 | if (!aligned) { |
| 1031 | drop_cnt++; |
| 1032 | |
| 1033 | if (drop_cnt == 1) { |
| 1034 | LOG(DEBUG) << "Aligning transmitter: stop burst"; |
ttsou | 221f23e | 2012-08-08 00:51:34 +0000 | [diff] [blame] | 1035 | *underrun = true; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1036 | metadata.end_of_burst = true; |
| 1037 | } else if (drop_cnt < 30) { |
kurtis.heimerl | 4e59d66 | 2011-11-26 03:19:11 +0000 | [diff] [blame] | 1038 | LOG(DEBUG) << "Aligning transmitter: packet advance"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1039 | return len; |
| 1040 | } else { |
| 1041 | LOG(DEBUG) << "Aligning transmitter: start burst"; |
| 1042 | metadata.start_of_burst = true; |
| 1043 | aligned = true; |
| 1044 | drop_cnt = 0; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1048 | thread_enable_cancel(false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1049 | size_t num_smpls = tx_stream->send(bufs, len, metadata); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1050 | thread_enable_cancel(true); |
| 1051 | |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 1052 | if (num_smpls != (unsigned) len) { |
| 1053 | LOG(ALERT) << "UHD: Device send timed out"; |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 1054 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1055 | |
| 1056 | return num_smpls; |
| 1057 | } |
| 1058 | |
| 1059 | bool uhd_device::updateAlignment(TIMESTAMP timestamp) |
| 1060 | { |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1061 | return true; |
| 1062 | } |
| 1063 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1064 | uhd::tune_request_t uhd_device::select_freq(double freq, size_t chan, bool tx) |
| 1065 | { |
| 1066 | double rf_spread, rf_freq; |
| 1067 | std::vector<double> freqs; |
| 1068 | uhd::tune_request_t treq(freq); |
| 1069 | |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 1070 | if ((chans == 1) || ((chans == 2) && dev_type == UMTRX)) { |
| 1071 | if (offset == 0.0) |
| 1072 | return treq; |
| 1073 | |
| 1074 | return uhd::tune_request_t(freq, offset); |
| 1075 | } else if ((dev_type != B210) || (chans > 2) || (chan > 1)) { |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1076 | LOG(ALERT) << chans << " channels unsupported"; |
| 1077 | return treq; |
| 1078 | } |
| 1079 | |
| 1080 | if (tx) |
| 1081 | freqs = tx_freqs; |
| 1082 | else |
| 1083 | freqs = rx_freqs; |
| 1084 | |
| 1085 | /* Tune directly if other channel isn't tuned */ |
| 1086 | if (freqs[!chan] < 10.0) |
| 1087 | return treq; |
| 1088 | |
| 1089 | /* Find center frequency between channels */ |
| 1090 | rf_spread = fabs(freqs[!chan] - freq); |
| 1091 | if (rf_spread > B2XX_CLK_RT) { |
| 1092 | LOG(ALERT) << rf_spread << "Hz tuning spread not supported\n"; |
| 1093 | return treq; |
| 1094 | } |
| 1095 | |
| 1096 | rf_freq = (freqs[!chan] + freq) / 2.0f; |
| 1097 | |
| 1098 | treq.rf_freq_policy = uhd::tune_request_t::POLICY_MANUAL; |
| 1099 | treq.target_freq = freq; |
| 1100 | treq.rf_freq = rf_freq; |
| 1101 | |
| 1102 | return treq; |
| 1103 | } |
| 1104 | |
| 1105 | bool uhd_device::set_freq(double freq, size_t chan, bool tx) |
| 1106 | { |
| 1107 | std::vector<double> freqs; |
| 1108 | uhd::tune_result_t tres; |
| 1109 | uhd::tune_request_t treq = select_freq(freq, chan, tx); |
| 1110 | |
| 1111 | if (tx) { |
| 1112 | tres = usrp_dev->set_tx_freq(treq, chan); |
| 1113 | tx_freqs[chan] = usrp_dev->get_tx_freq(chan); |
| 1114 | } else { |
| 1115 | tres = usrp_dev->set_rx_freq(treq, chan); |
| 1116 | rx_freqs[chan] = usrp_dev->get_rx_freq(chan); |
| 1117 | } |
| 1118 | LOG(INFO) << "\n" << tres.to_pp_string() << std::endl; |
| 1119 | |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 1120 | if ((chans == 1) || ((chans == 2) && dev_type == UMTRX)) |
| 1121 | return true; |
| 1122 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1123 | /* Manual RF policy means we intentionally tuned with a baseband |
| 1124 | * offset for dual-channel purposes. Now retune the other channel |
| 1125 | * with the opposite corresponding frequency offset |
| 1126 | */ |
| 1127 | if (treq.rf_freq_policy == uhd::tune_request_t::POLICY_MANUAL) { |
| 1128 | if (tx) { |
| 1129 | treq = select_freq(tx_freqs[!chan], !chan, true); |
| 1130 | tres = usrp_dev->set_tx_freq(treq, !chan); |
| 1131 | tx_freqs[!chan] = usrp_dev->get_tx_freq(!chan); |
| 1132 | } else { |
| 1133 | treq = select_freq(rx_freqs[!chan], !chan, false); |
| 1134 | tres = usrp_dev->set_rx_freq(treq, !chan); |
| 1135 | rx_freqs[!chan] = usrp_dev->get_rx_freq(!chan); |
| 1136 | |
| 1137 | } |
| 1138 | LOG(INFO) << "\n" << tres.to_pp_string() << std::endl; |
| 1139 | } |
| 1140 | |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1144 | bool uhd_device::setTxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1145 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1146 | if (chan >= tx_freqs.size()) { |
| 1147 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 1148 | return false; |
| 1149 | } |
Tom Tsou | 93b7f37 | 2014-12-15 20:23:33 -0800 | [diff] [blame] | 1150 | ScopedLock lock(tune_lock); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1151 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1152 | return set_freq(wFreq, chan, true); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1155 | bool uhd_device::setRxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1156 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1157 | if (chan >= rx_freqs.size()) { |
| 1158 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 1159 | return false; |
| 1160 | } |
Tom Tsou | 93b7f37 | 2014-12-15 20:23:33 -0800 | [diff] [blame] | 1161 | ScopedLock lock(tune_lock); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1162 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1163 | return set_freq(wFreq, chan, false); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1166 | double uhd_device::getTxFreq(size_t chan) |
| 1167 | { |
| 1168 | if (chan >= tx_freqs.size()) { |
| 1169 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 1170 | return 0.0; |
| 1171 | } |
| 1172 | |
| 1173 | return tx_freqs[chan]; |
| 1174 | } |
| 1175 | |
| 1176 | double uhd_device::getRxFreq(size_t chan) |
| 1177 | { |
| 1178 | if (chan >= rx_freqs.size()) { |
| 1179 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 1180 | return 0.0; |
| 1181 | } |
| 1182 | |
| 1183 | return rx_freqs[chan]; |
| 1184 | } |
| 1185 | |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame^] | 1186 | double uhd_device::fullScaleInputValue() |
| 1187 | { |
| 1188 | if (dev_type == UMTRX) |
| 1189 | return (double) SHRT_MAX * UMTRX_TX_AMPL; |
| 1190 | else |
| 1191 | return (double) SHRT_MAX * USRP_TX_AMPL; |
| 1192 | } |
| 1193 | |
| 1194 | double uhd_device::fullScaleOutputValue() |
| 1195 | { |
| 1196 | return (double) SHRT_MAX; |
| 1197 | } |
| 1198 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1199 | bool uhd_device::recv_async_msg() |
| 1200 | { |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1201 | uhd::async_metadata_t md; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1202 | |
| 1203 | thread_enable_cancel(false); |
| 1204 | bool rc = usrp_dev->get_device()->recv_async_msg(md); |
| 1205 | thread_enable_cancel(true); |
| 1206 | if (!rc) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | |
| 1209 | // Assume that any error requires resynchronization |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1210 | if (md.event_code != uhd::async_metadata_t::EVENT_CODE_BURST_ACK) { |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1211 | aligned = false; |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1212 | |
| 1213 | if ((md.event_code != uhd::async_metadata_t::EVENT_CODE_UNDERFLOW) && |
| 1214 | (md.event_code != uhd::async_metadata_t::EVENT_CODE_TIME_ERROR)) { |
| 1215 | LOG(ERR) << str_code(md); |
| 1216 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | return true; |
| 1220 | } |
| 1221 | |
| 1222 | std::string uhd_device::str_code(uhd::rx_metadata_t metadata) |
| 1223 | { |
| 1224 | std::ostringstream ost("UHD: "); |
| 1225 | |
| 1226 | switch (metadata.error_code) { |
| 1227 | case uhd::rx_metadata_t::ERROR_CODE_NONE: |
| 1228 | ost << "No error"; |
| 1229 | break; |
| 1230 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
| 1231 | ost << "No packet received, implementation timed-out"; |
| 1232 | break; |
| 1233 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 1234 | ost << "A stream command was issued in the past"; |
| 1235 | break; |
| 1236 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 1237 | ost << "Expected another stream command"; |
| 1238 | break; |
| 1239 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 1240 | ost << "An internal receive buffer has filled"; |
| 1241 | break; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1242 | case uhd::rx_metadata_t::ERROR_CODE_ALIGNMENT: |
| 1243 | ost << "Multi-channel alignment failed"; |
| 1244 | break; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1245 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 1246 | ost << "The packet could not be parsed"; |
| 1247 | break; |
| 1248 | default: |
| 1249 | ost << "Unknown error " << metadata.error_code; |
| 1250 | } |
| 1251 | |
| 1252 | if (metadata.has_time_spec) |
| 1253 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 1254 | |
| 1255 | return ost.str(); |
| 1256 | } |
| 1257 | |
| 1258 | std::string uhd_device::str_code(uhd::async_metadata_t metadata) |
| 1259 | { |
| 1260 | std::ostringstream ost("UHD: "); |
| 1261 | |
| 1262 | switch (metadata.event_code) { |
| 1263 | case uhd::async_metadata_t::EVENT_CODE_BURST_ACK: |
| 1264 | ost << "A packet was successfully transmitted"; |
| 1265 | break; |
| 1266 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW: |
| 1267 | ost << "An internal send buffer has emptied"; |
| 1268 | break; |
| 1269 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR: |
| 1270 | ost << "Packet loss between host and device"; |
| 1271 | break; |
| 1272 | case uhd::async_metadata_t::EVENT_CODE_TIME_ERROR: |
| 1273 | ost << "Packet time was too late or too early"; |
| 1274 | break; |
| 1275 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET: |
| 1276 | ost << "Underflow occurred inside a packet"; |
| 1277 | break; |
| 1278 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST: |
| 1279 | ost << "Packet loss within a burst"; |
| 1280 | break; |
| 1281 | default: |
| 1282 | ost << "Unknown error " << metadata.event_code; |
| 1283 | } |
| 1284 | |
| 1285 | if (metadata.has_time_spec) |
| 1286 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 1287 | |
| 1288 | return ost.str(); |
| 1289 | } |
| 1290 | |
| 1291 | smpl_buf::smpl_buf(size_t len, double rate) |
| 1292 | : buf_len(len), clk_rt(rate), |
| 1293 | time_start(0), time_end(0), data_start(0), data_end(0) |
| 1294 | { |
| 1295 | data = new uint32_t[len]; |
| 1296 | } |
| 1297 | |
| 1298 | smpl_buf::~smpl_buf() |
| 1299 | { |
| 1300 | delete[] data; |
| 1301 | } |
| 1302 | |
| 1303 | ssize_t smpl_buf::avail_smpls(TIMESTAMP timestamp) const |
| 1304 | { |
| 1305 | if (timestamp < time_start) |
| 1306 | return ERROR_TIMESTAMP; |
| 1307 | else if (timestamp >= time_end) |
| 1308 | return 0; |
| 1309 | else |
| 1310 | return time_end - timestamp; |
| 1311 | } |
| 1312 | |
| 1313 | ssize_t smpl_buf::avail_smpls(uhd::time_spec_t timespec) const |
| 1314 | { |
| 1315 | return avail_smpls(convert_time(timespec, clk_rt)); |
| 1316 | } |
| 1317 | |
| 1318 | ssize_t smpl_buf::read(void *buf, size_t len, TIMESTAMP timestamp) |
| 1319 | { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1320 | int type_sz = 2 * sizeof(short); |
| 1321 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1322 | // Check for valid read |
| 1323 | if (timestamp < time_start) |
| 1324 | return ERROR_TIMESTAMP; |
| 1325 | if (timestamp >= time_end) |
| 1326 | return 0; |
| 1327 | if (len >= buf_len) |
| 1328 | return ERROR_READ; |
| 1329 | |
| 1330 | // How many samples should be copied |
| 1331 | size_t num_smpls = time_end - timestamp; |
kurtis.heimerl | ec842de | 2012-11-23 08:37:32 +0000 | [diff] [blame] | 1332 | if (num_smpls > len) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1333 | num_smpls = len; |
| 1334 | |
| 1335 | // Starting index |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 1336 | size_t read_start = (data_start + (timestamp - time_start)) % buf_len; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Read it |
| 1339 | if (read_start + num_smpls < buf_len) { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1340 | size_t numBytes = len * type_sz; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1341 | memcpy(buf, data + read_start, numBytes); |
| 1342 | } else { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1343 | size_t first_cp = (buf_len - read_start) * type_sz; |
| 1344 | size_t second_cp = len * type_sz - first_cp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1345 | |
| 1346 | memcpy(buf, data + read_start, first_cp); |
| 1347 | memcpy((char*) buf + first_cp, data, second_cp); |
| 1348 | } |
| 1349 | |
| 1350 | data_start = (read_start + len) % buf_len; |
| 1351 | time_start = timestamp + len; |
| 1352 | |
| 1353 | if (time_start > time_end) |
| 1354 | return ERROR_READ; |
| 1355 | else |
| 1356 | return num_smpls; |
| 1357 | } |
| 1358 | |
| 1359 | ssize_t smpl_buf::read(void *buf, size_t len, uhd::time_spec_t ts) |
| 1360 | { |
| 1361 | return read(buf, len, convert_time(ts, clk_rt)); |
| 1362 | } |
| 1363 | |
| 1364 | ssize_t smpl_buf::write(void *buf, size_t len, TIMESTAMP timestamp) |
| 1365 | { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1366 | int type_sz = 2 * sizeof(short); |
| 1367 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1368 | // Check for valid write |
| 1369 | if ((len == 0) || (len >= buf_len)) |
| 1370 | return ERROR_WRITE; |
| 1371 | if ((timestamp + len) <= time_end) |
| 1372 | return ERROR_TIMESTAMP; |
| 1373 | |
| 1374 | // Starting index |
| 1375 | size_t write_start = (data_start + (timestamp - time_start)) % buf_len; |
| 1376 | |
| 1377 | // Write it |
| 1378 | if ((write_start + len) < buf_len) { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1379 | size_t numBytes = len * type_sz; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1380 | memcpy(data + write_start, buf, numBytes); |
| 1381 | } else { |
kurtis.heimerl | 1979c6f | 2011-11-26 03:18:24 +0000 | [diff] [blame] | 1382 | size_t first_cp = (buf_len - write_start) * type_sz; |
| 1383 | size_t second_cp = len * type_sz - first_cp; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1384 | |
| 1385 | memcpy(data + write_start, buf, first_cp); |
| 1386 | memcpy(data, (char*) buf + first_cp, second_cp); |
| 1387 | } |
| 1388 | |
| 1389 | data_end = (write_start + len) % buf_len; |
| 1390 | time_end = timestamp + len; |
| 1391 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 1392 | if (!data_start) |
| 1393 | data_start = write_start; |
| 1394 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1395 | if (((write_start + len) > buf_len) && (data_end > data_start)) |
| 1396 | return ERROR_OVERFLOW; |
| 1397 | else if (time_end <= time_start) |
| 1398 | return ERROR_WRITE; |
| 1399 | else |
| 1400 | return len; |
| 1401 | } |
| 1402 | |
| 1403 | ssize_t smpl_buf::write(void *buf, size_t len, uhd::time_spec_t ts) |
| 1404 | { |
| 1405 | return write(buf, len, convert_time(ts, clk_rt)); |
| 1406 | } |
| 1407 | |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 1408 | std::string smpl_buf::str_status(size_t ts) const |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1409 | { |
| 1410 | std::ostringstream ost("Sample buffer: "); |
| 1411 | |
Tom Tsou | 1ae2556 | 2014-12-05 12:56:34 -0800 | [diff] [blame] | 1412 | ost << "timestamp = " << ts; |
| 1413 | ost << ", length = " << buf_len; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1414 | ost << ", time_start = " << time_start; |
| 1415 | ost << ", time_end = " << time_end; |
| 1416 | ost << ", data_start = " << data_start; |
| 1417 | ost << ", data_end = " << data_end; |
| 1418 | |
| 1419 | return ost.str(); |
| 1420 | } |
| 1421 | |
| 1422 | std::string smpl_buf::str_code(ssize_t code) |
| 1423 | { |
| 1424 | switch (code) { |
| 1425 | case ERROR_TIMESTAMP: |
| 1426 | return "Sample buffer: Requested timestamp is not valid"; |
| 1427 | case ERROR_READ: |
| 1428 | return "Sample buffer: Read error"; |
| 1429 | case ERROR_WRITE: |
| 1430 | return "Sample buffer: Write error"; |
| 1431 | case ERROR_OVERFLOW: |
| 1432 | return "Sample buffer: Overrun"; |
| 1433 | default: |
| 1434 | return "Sample buffer: Unknown error"; |
| 1435 | } |
| 1436 | } |
| 1437 | |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 1438 | RadioDevice *RadioDevice::make(size_t sps, size_t chans, |
| 1439 | bool diversity, double offset) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1440 | { |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 1441 | return new uhd_device(sps, chans, diversity, offset); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1442 | } |