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