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