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