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