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