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