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