dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * This software is distributed under the terms of the GNU Affero Public License. |
| 5 | * See the COPYING file in the main directory for details. |
| 6 | * |
| 7 | * This use of this software may be subject to additional restrictions. |
| 8 | * See the LEGAL file in the main directory for details. |
| 9 | |
| 10 | This program is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU Affero General Public License as published by |
| 12 | the Free Software Foundation, either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU Affero General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU Affero General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /* |
| 27 | Compilation Flags |
| 28 | |
| 29 | SWLOOPBACK compile for software loopback testing |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 30 | */ |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | #include <stdint.h> |
| 34 | #include <string.h> |
| 35 | #include <stdlib.h> |
Alexander Huemer | 6fafd33 | 2018-01-12 15:04:02 +0100 | [diff] [blame] | 36 | #include "Logger.h" |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 37 | #include "Threads.h" |
| 38 | #include "USRPDevice.h" |
| 39 | |
kurtis.heimerl | 3758b16 | 2011-11-26 03:19:22 +0000 | [diff] [blame] | 40 | #ifdef HAVE_CONFIG_H |
| 41 | #include "config.h" |
| 42 | #endif |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace std; |
| 45 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 46 | enum dboardConfigType { |
| 47 | TXA_RXB, |
| 48 | TXB_RXA, |
| 49 | TXA_RXA, |
| 50 | TXB_RXB |
| 51 | }; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 52 | |
kurtis.heimerl | 3758b16 | 2011-11-26 03:19:22 +0000 | [diff] [blame] | 53 | #ifdef SINGLEDB |
| 54 | const dboardConfigType dboardConfig = TXA_RXA; |
| 55 | #else |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 56 | const dboardConfigType dboardConfig = TXA_RXB; |
kurtis.heimerl | 3758b16 | 2011-11-26 03:19:22 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 59 | const double USRPDevice::masterClockRate = 52.0e6; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 60 | |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 61 | USRPDevice::USRPDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, |
| 62 | size_t chans, double lo_offset, |
| 63 | const std::vector<std::string>& tx_paths, |
| 64 | const std::vector<std::string>& rx_paths): |
| 65 | RadioDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 66 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 67 | LOGC(DDEV, INFO) << "creating USRP device..."; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 68 | |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 69 | decimRate = (unsigned int) round(masterClockRate/((GSMRATE) * (double) tx_sps)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 70 | actualSampleRate = masterClockRate/decimRate; |
| 71 | rxGain = 0; |
| 72 | |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 73 | /* |
| 74 | * Undetermined delay b/w ping response timestamp and true |
| 75 | * receive timestamp. Values are empirically measured. With |
| 76 | * split sample rate Tx/Rx - 4/1 sps we need to need to |
| 77 | * compensate for advance rather than delay. |
| 78 | */ |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 79 | if (tx_sps == 1) |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 80 | pingOffset = 272; |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 81 | else if (tx_sps == 4) |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 82 | pingOffset = 269 - 7500; |
| 83 | else |
| 84 | pingOffset = 0; |
| 85 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 86 | #ifdef SWLOOPBACK |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 87 | samplePeriod = 1.0e6/actualSampleRate; |
| 88 | loopbackBufferSize = 0; |
| 89 | gettimeofday(&lastReadTime,NULL); |
| 90 | firstRead = false; |
| 91 | #endif |
| 92 | } |
| 93 | |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 94 | int USRPDevice::open(const std::string &, int, bool) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 95 | { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 96 | writeLock.unlock(); |
| 97 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 98 | LOGC(DDEV, INFO) << "opening USRP device.."; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 99 | #ifndef SWLOOPBACK |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 100 | string rbf = "std_inband.rbf"; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 101 | //string rbf = "inband_1rxhb_1tx.rbf"; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 102 | m_uRx.reset(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 103 | try { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 104 | m_uRx = usrp_standard_rx_sptr(usrp_standard_rx::make( |
Harald Welte | ce70ba5 | 2018-06-13 22:47:48 +0200 | [diff] [blame] | 105 | 0, decimRate * tx_sps, 1, -1, |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 106 | usrp_standard_rx::FPGA_MODE_NORMAL, |
| 107 | 1024, 16 * 8, rbf)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 108 | m_uRx->set_fpga_master_clock_freq(masterClockRate); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 109 | } |
Thomas Tsou | c064124 | 2013-10-11 14:55:31 -0400 | [diff] [blame] | 110 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 111 | catch(...) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 112 | LOGC(DDEV, ALERT) << "make failed on Rx"; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 113 | m_uRx.reset(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 114 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (m_uRx->fpga_master_clock_freq() != masterClockRate) |
| 118 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 119 | LOGC(DDEV, ALERT) << "WRONG FPGA clock freq = " << m_uRx->fpga_master_clock_freq() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 120 | << ", desired clock freq = " << masterClockRate; |
| 121 | m_uRx.reset(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 122 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 123 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 124 | |
| 125 | try { |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 126 | m_uTx = usrp_standard_tx_sptr(usrp_standard_tx::make( |
| 127 | 0, decimRate * 2, 1, -1, |
| 128 | 1024, 16 * 8, rbf)); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 129 | m_uTx->set_fpga_master_clock_freq(masterClockRate); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 130 | } |
Thomas Tsou | c064124 | 2013-10-11 14:55:31 -0400 | [diff] [blame] | 131 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 132 | catch(...) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 133 | LOGC(DDEV, ALERT) << "make failed on Tx"; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 134 | m_uTx.reset(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 135 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (m_uTx->fpga_master_clock_freq() != masterClockRate) |
| 139 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 140 | LOGC(DDEV, ALERT) << "WRONG FPGA clock freq = " << m_uTx->fpga_master_clock_freq() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 141 | << ", desired clock freq = " << masterClockRate; |
| 142 | m_uTx.reset(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 143 | return -1; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Harald Welte | a944001 | 2019-01-11 21:36:34 +0100 | [diff] [blame] | 146 | m_uRx->stop(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 147 | m_uTx->stop(); |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 148 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 149 | #endif |
| 150 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 151 | switch (dboardConfig) { |
| 152 | case TXA_RXB: |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 153 | txSubdevSpec = usrp_subdev_spec(0,0); |
| 154 | rxSubdevSpec = usrp_subdev_spec(1,0); |
| 155 | break; |
| 156 | case TXB_RXA: |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 157 | txSubdevSpec = usrp_subdev_spec(1,0); |
| 158 | rxSubdevSpec = usrp_subdev_spec(0,0); |
| 159 | break; |
| 160 | case TXA_RXA: |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 161 | txSubdevSpec = usrp_subdev_spec(0,0); |
| 162 | rxSubdevSpec = usrp_subdev_spec(0,0); |
| 163 | break; |
| 164 | case TXB_RXB: |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 165 | txSubdevSpec = usrp_subdev_spec(1,0); |
| 166 | rxSubdevSpec = usrp_subdev_spec(1,0); |
| 167 | break; |
| 168 | default: |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 169 | txSubdevSpec = usrp_subdev_spec(0,0); |
| 170 | rxSubdevSpec = usrp_subdev_spec(1,0); |
| 171 | } |
| 172 | |
kurtis.heimerl | b9e237e | 2011-11-26 03:17:59 +0000 | [diff] [blame] | 173 | m_dbTx = m_uTx->selected_subdev(txSubdevSpec); |
| 174 | m_dbRx = m_uRx->selected_subdev(rxSubdevSpec); |
| 175 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 176 | started = false; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 177 | |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 178 | return NORMAL; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 183 | bool USRPDevice::start() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 184 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 185 | LOGC(DDEV, INFO) << "starting USRP..."; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 186 | #ifndef SWLOOPBACK |
Harald Welte | a944001 | 2019-01-11 21:36:34 +0100 | [diff] [blame] | 187 | if (!m_uRx) return false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 188 | if (!m_uTx) return false; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 189 | |
Harald Welte | a944001 | 2019-01-11 21:36:34 +0100 | [diff] [blame] | 190 | m_uRx->stop(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 191 | m_uTx->stop(); |
| 192 | |
| 193 | writeLock.lock(); |
| 194 | // power up and configure daughterboards |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 195 | m_dbTx->set_enable(true); |
| 196 | m_uTx->set_mux(m_uTx->determine_tx_mux_value(txSubdevSpec)); |
| 197 | m_uRx->set_mux(m_uRx->determine_rx_mux_value(rxSubdevSpec)); |
| 198 | |
| 199 | if (!m_dbRx->select_rx_antenna(1)) |
| 200 | m_dbRx->select_rx_antenna(0); |
| 201 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 202 | writeLock.unlock(); |
| 203 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 204 | // Set gains to midpoint |
| 205 | setTxGain((minTxGain() + maxTxGain()) / 2); |
| 206 | setRxGain((minRxGain() + maxRxGain()) / 2); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 207 | |
| 208 | data = new short[currDataSize]; |
| 209 | dataStart = 0; |
| 210 | dataEnd = 0; |
| 211 | timeStart = 0; |
| 212 | timeEnd = 0; |
| 213 | timestampOffset = 0; |
| 214 | latestWriteTimestamp = 0; |
| 215 | lastPktTimestamp = 0; |
| 216 | hi32Timestamp = 0; |
| 217 | isAligned = false; |
| 218 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 219 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 220 | started = (m_uRx->start() && m_uTx->start()); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 221 | return started; |
| 222 | #else |
| 223 | gettimeofday(&lastReadTime,NULL); |
| 224 | return true; |
| 225 | #endif |
| 226 | } |
| 227 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 228 | bool USRPDevice::stop() |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 229 | { |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 230 | #ifndef SWLOOPBACK |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 231 | if (!m_uRx) return false; |
| 232 | if (!m_uTx) return false; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 233 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 234 | delete[] currData; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 235 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 236 | started = !(m_uRx->stop() && m_uTx->stop()); |
| 237 | return !started; |
| 238 | #else |
| 239 | return true; |
| 240 | #endif |
| 241 | } |
| 242 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 243 | double USRPDevice::maxTxGain() |
| 244 | { |
| 245 | return m_dbTx->gain_max(); |
| 246 | } |
| 247 | |
| 248 | double USRPDevice::minTxGain() |
| 249 | { |
| 250 | return m_dbTx->gain_min(); |
| 251 | } |
| 252 | |
| 253 | double USRPDevice::maxRxGain() |
| 254 | { |
| 255 | return m_dbRx->gain_max(); |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 256 | } |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 257 | |
| 258 | double USRPDevice::minRxGain() |
| 259 | { |
| 260 | return m_dbRx->gain_min(); |
| 261 | } |
| 262 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 263 | double USRPDevice::setTxGain(double dB, size_t chan) |
| 264 | { |
| 265 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 266 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 267 | return 0.0; |
| 268 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 269 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 270 | writeLock.lock(); |
| 271 | if (dB > maxTxGain()) |
| 272 | dB = maxTxGain(); |
| 273 | if (dB < minTxGain()) |
| 274 | dB = minTxGain(); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 275 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 276 | LOGC(DDEV, NOTICE) << "Setting TX gain to " << dB << " dB."; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 277 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 278 | if (!m_dbTx->set_gain(dB)) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 279 | LOGC(DDEV, ERR) << "Error setting TX gain"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 280 | |
| 281 | writeLock.unlock(); |
| 282 | |
| 283 | return dB; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 287 | double USRPDevice::setRxGain(double dB, size_t chan) |
| 288 | { |
| 289 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 290 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 291 | return 0.0; |
| 292 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 293 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 294 | dB = 47.0; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 295 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 296 | writeLock.lock(); |
| 297 | if (dB > maxRxGain()) |
| 298 | dB = maxRxGain(); |
| 299 | if (dB < minRxGain()) |
| 300 | dB = minRxGain(); |
| 301 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 302 | LOGC(DDEV, NOTICE) << "Setting RX gain to " << dB << " dB."; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 303 | |
| 304 | if (!m_dbRx->set_gain(dB)) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 305 | LOGC(DDEV, ERR) << "Error setting RX gain"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 306 | |
| 307 | writeLock.unlock(); |
| 308 | |
| 309 | return dB; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 312 | bool USRPDevice::setRxAntenna(const std::string &ant, size_t chan) |
| 313 | { |
| 314 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 315 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 316 | return false; |
| 317 | } |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 318 | LOGC(DDEV, ALERT) << "Not implemented"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 319 | return true; |
| 320 | } |
| 321 | |
| 322 | std::string USRPDevice::getRxAntenna(size_t chan) |
| 323 | { |
| 324 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 325 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 326 | return ""; |
| 327 | } |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 328 | LOGC(DDEV, ALERT) << "Not implemented"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 329 | return ""; |
| 330 | } |
| 331 | |
| 332 | bool USRPDevice::setTxAntenna(const std::string &ant, size_t chan) |
| 333 | { |
| 334 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 335 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 336 | return false; |
| 337 | } |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 338 | LOGC(DDEV, ALERT) << "Not implemented"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
| 342 | std::string USRPDevice::getTxAntenna(size_t chan) |
| 343 | { |
| 344 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 345 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 346 | return ""; |
| 347 | } |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 348 | LOGC(DDEV, ALERT) << "Not implemented"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 349 | return ""; |
| 350 | } |
| 351 | |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 352 | bool USRPDevice::requiresRadioAlign() |
| 353 | { |
| 354 | return true; |
| 355 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 356 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 357 | GSM::Time USRPDevice::minLatency() { |
| 358 | return GSM::Time(1,1); |
| 359 | } |
| 360 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 361 | // NOTE: Assumes sequential reads |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 362 | int USRPDevice::readSamples(std::vector<short *> &bufs, int len, bool *overrun, |
| 363 | TIMESTAMP timestamp, bool *underrun, unsigned *RSSI) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 364 | { |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 365 | #ifndef SWLOOPBACK |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 366 | if (!m_uRx) |
| 367 | return 0; |
| 368 | |
| 369 | short *buf = bufs[0]; |
| 370 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 371 | timestamp += timestampOffset; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 372 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 373 | if (timestamp + len < timeStart) { |
| 374 | memset(buf,0,len*2*sizeof(short)); |
| 375 | return len; |
| 376 | } |
| 377 | |
| 378 | if (underrun) *underrun = false; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 379 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 380 | uint32_t readBuf[2000]; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 381 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 382 | while (1) { |
| 383 | //guestimate USB read size |
| 384 | int readLen=0; |
| 385 | { |
| 386 | int numSamplesNeeded = timestamp + len - timeEnd; |
| 387 | if (numSamplesNeeded <=0) break; |
| 388 | readLen = 512 * ((int) ceil((float) numSamplesNeeded/126.0)); |
| 389 | if (readLen > 8000) readLen= (8000/512)*512; |
| 390 | } |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 391 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 392 | // read USRP packets, parse and save A/D data as needed |
| 393 | readLen = m_uRx->read((void *)readBuf,readLen,overrun); |
Pau Espin Pedrol | 4d179ab | 2018-09-10 10:36:04 +0200 | [diff] [blame] | 394 | for (int pktNum = 0; pktNum < (readLen/512); pktNum++) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 395 | // tmpBuf points to start of a USB packet |
| 396 | uint32_t* tmpBuf = (uint32_t *) (readBuf+pktNum*512/4); |
| 397 | TIMESTAMP pktTimestamp = usrp_to_host_u32(tmpBuf[1]); |
| 398 | uint32_t word0 = usrp_to_host_u32(tmpBuf[0]); |
| 399 | uint32_t chan = (word0 >> 16) & 0x1f; |
| 400 | unsigned payloadSz = word0 & 0x1ff; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 401 | LOGC(DDEV, DEBUG) << "first two bytes: " << hex << word0 << " " << dec << pktTimestamp; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 402 | |
| 403 | bool incrementHi32 = ((lastPktTimestamp & 0x0ffffffffll) > pktTimestamp); |
| 404 | if (incrementHi32 && (timeStart!=0)) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 405 | LOGC(DDEV, DEBUG) << "high 32 increment!!!"; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 406 | hi32Timestamp++; |
| 407 | } |
| 408 | pktTimestamp = (((TIMESTAMP) hi32Timestamp) << 32) | pktTimestamp; |
| 409 | lastPktTimestamp = pktTimestamp; |
| 410 | |
| 411 | if (chan == 0x01f) { |
| 412 | // control reply, check to see if its ping reply |
| 413 | uint32_t word2 = usrp_to_host_u32(tmpBuf[2]); |
| 414 | if ((word2 >> 16) == ((0x01 << 8) | 0x02)) { |
| 415 | timestamp -= timestampOffset; |
Thomas Tsou | c1f7c42 | 2013-10-11 13:49:55 -0400 | [diff] [blame] | 416 | timestampOffset = pktTimestamp - pingTimestamp + pingOffset; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 417 | LOGC(DDEV, DEBUG) << "updating timestamp offset to: " << timestampOffset; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 418 | timestamp += timestampOffset; |
| 419 | isAligned = true; |
| 420 | } |
| 421 | continue; |
| 422 | } |
| 423 | if (chan != 0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 424 | LOGC(DDEV, DEBUG) << "chan: " << chan << ", timestamp: " << pktTimestamp << ", sz:" << payloadSz; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 425 | continue; |
| 426 | } |
| 427 | if ((word0 >> 28) & 0x04) { |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 428 | if (underrun) *underrun = true; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 429 | LOGC(DDEV, DEBUG) << "UNDERRUN in TRX->USRP interface"; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 430 | } |
| 431 | if (RSSI) *RSSI = (word0 >> 21) & 0x3f; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 432 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 433 | if (!isAligned) continue; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 434 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 435 | unsigned cursorStart = pktTimestamp - timeStart + dataStart; |
| 436 | while (cursorStart*2 > currDataSize) { |
| 437 | cursorStart -= currDataSize/2; |
| 438 | } |
| 439 | if (cursorStart*2 + payloadSz/2 > currDataSize) { |
| 440 | // need to circle around buffer |
| 441 | memcpy(data+cursorStart*2,tmpBuf+2,(currDataSize-cursorStart*2)*sizeof(short)); |
| 442 | memcpy(data,tmpBuf+2+(currDataSize/2-cursorStart),payloadSz-(currDataSize-cursorStart*2)*sizeof(short)); |
| 443 | } |
| 444 | else { |
| 445 | memcpy(data+cursorStart*2,tmpBuf+2,payloadSz); |
| 446 | } |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 447 | if (pktTimestamp + payloadSz/2/sizeof(short) > timeEnd) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 448 | timeEnd = pktTimestamp+payloadSz/2/sizeof(short); |
| 449 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 450 | LOGC(DDEV, DEBUG) << "timeStart: " << timeStart << ", timeEnd: " << timeEnd << ", pktTimestamp: " << pktTimestamp; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 451 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 455 | // copy desired data to buf |
| 456 | unsigned bufStart = dataStart+(timestamp-timeStart); |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 457 | if (bufStart + len < currDataSize/2) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 458 | LOGC(DDEV, DEBUG) << "bufStart: " << bufStart; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 459 | memcpy(buf,data+bufStart*2,len*2*sizeof(short)); |
| 460 | memset(data+bufStart*2,0,len*2*sizeof(short)); |
| 461 | } |
| 462 | else { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 463 | LOGC(DDEV, DEBUG) << "len: " << len << ", currDataSize/2: " << currDataSize/2 << ", bufStart: " << bufStart; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 464 | unsigned firstLength = (currDataSize/2-bufStart); |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 465 | LOGC(DDEV, DEBUG) << "firstLength: " << firstLength; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 466 | memcpy(buf,data+bufStart*2,firstLength*2*sizeof(short)); |
| 467 | memset(data+bufStart*2,0,firstLength*2*sizeof(short)); |
| 468 | memcpy(buf+firstLength*2,data,(len-firstLength)*2*sizeof(short)); |
| 469 | memset(data,0,(len-firstLength)*2*sizeof(short)); |
| 470 | } |
| 471 | dataStart = (bufStart + len) % (currDataSize/2); |
| 472 | timeStart = timestamp + len; |
| 473 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 474 | return len; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 475 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 476 | #else |
| 477 | if (loopbackBufferSize < 2) return 0; |
| 478 | int numSamples = 0; |
| 479 | struct timeval currTime; |
| 480 | gettimeofday(&currTime,NULL); |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 481 | double timeElapsed = (currTime.tv_sec - lastReadTime.tv_sec)*1.0e6 + |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 482 | (currTime.tv_usec - lastReadTime.tv_usec); |
| 483 | if (timeElapsed < samplePeriod) {return 0;} |
| 484 | int numSamplesToRead = (int) floor(timeElapsed/samplePeriod); |
| 485 | if (numSamplesToRead < len) return 0; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 486 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 487 | if (numSamplesToRead > len) numSamplesToRead = len; |
| 488 | if (numSamplesToRead > loopbackBufferSize/2) { |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 489 | firstRead =false; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 490 | numSamplesToRead = loopbackBufferSize/2; |
| 491 | } |
| 492 | memcpy(buf,loopbackBuffer,sizeof(short)*2*numSamplesToRead); |
| 493 | loopbackBufferSize -= 2*numSamplesToRead; |
| 494 | memcpy(loopbackBuffer,loopbackBuffer+2*numSamplesToRead, |
| 495 | sizeof(short)*loopbackBufferSize); |
| 496 | numSamples = numSamplesToRead; |
| 497 | if (firstRead) { |
| 498 | int new_usec = lastReadTime.tv_usec + (int) round((double) numSamplesToRead * samplePeriod); |
| 499 | lastReadTime.tv_sec = lastReadTime.tv_sec + new_usec/1000000; |
| 500 | lastReadTime.tv_usec = new_usec % 1000000; |
| 501 | } |
| 502 | else { |
| 503 | gettimeofday(&lastReadTime,NULL); |
| 504 | firstRead = true; |
| 505 | } |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 506 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 507 | return numSamples; |
| 508 | #endif |
| 509 | } |
| 510 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 511 | int USRPDevice::writeSamples(std::vector<short *> &bufs, int len, |
| 512 | bool *underrun, unsigned long long timestamp, |
| 513 | bool isControl) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 514 | { |
| 515 | writeLock.lock(); |
| 516 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 517 | #ifndef SWLOOPBACK |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 518 | if (!m_uTx) |
| 519 | return 0; |
| 520 | |
| 521 | short *buf = bufs[0]; |
| 522 | |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 523 | static uint32_t outData[128*20]; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 524 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 525 | for (int i = 0; i < len*2; i++) { |
| 526 | buf[i] = host_to_usrp_short(buf[i]); |
| 527 | } |
| 528 | |
| 529 | int numWritten = 0; |
| 530 | unsigned isStart = 1; |
| 531 | unsigned RSSI = 0; |
| 532 | unsigned CHAN = (isControl) ? 0x01f : 0x00; |
| 533 | len = len*2*sizeof(short); |
| 534 | int numPkts = (int) ceil((float)len/(float)504); |
| 535 | unsigned isEnd = (numPkts < 2); |
| 536 | uint32_t *outPkt = outData; |
| 537 | int pktNum = 0; |
| 538 | while (numWritten < len) { |
| 539 | // pkt is pointer to start of a USB packet |
| 540 | uint32_t *pkt = outPkt + pktNum*128; |
| 541 | isEnd = (len - numWritten <= 504); |
| 542 | unsigned payloadLen = ((len - numWritten) < 504) ? (len-numWritten) : 504; |
| 543 | pkt[0] = (isStart << 12 | isEnd << 11 | (RSSI & 0x3f) << 5 | CHAN) << 16 | payloadLen; |
| 544 | pkt[1] = timestamp & 0x0ffffffffll; |
| 545 | memcpy(pkt+2,buf+(numWritten/sizeof(short)),payloadLen); |
| 546 | numWritten += payloadLen; |
| 547 | timestamp += payloadLen/2/sizeof(short); |
| 548 | isStart = 0; |
| 549 | pkt[0] = host_to_usrp_u32(pkt[0]); |
| 550 | pkt[1] = host_to_usrp_u32(pkt[1]); |
| 551 | pktNum++; |
| 552 | } |
| 553 | m_uTx->write((const void*) outPkt,sizeof(uint32_t)*128*numPkts,NULL); |
| 554 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 555 | writeLock.unlock(); |
| 556 | |
| 557 | return len/2/sizeof(short); |
| 558 | #else |
| 559 | int retVal = len; |
| 560 | memcpy(loopbackBuffer+loopbackBufferSize,buf,sizeof(short)*2*len); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 561 | loopbackBufferSize += retVal*2; |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 562 | |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 563 | return retVal; |
| 564 | #endif |
| 565 | } |
| 566 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 567 | bool USRPDevice::updateAlignment(TIMESTAMP timestamp) |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 568 | { |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 569 | #ifndef SWLOOPBACK |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 570 | short data[] = {0x00,0x02,0x00,0x00}; |
| 571 | uint32_t *wordPtr = (uint32_t *) data; |
kurtis.heimerl | c8739b8 | 2011-11-02 00:06:34 +0000 | [diff] [blame] | 572 | *wordPtr = host_to_usrp_u32(*wordPtr); |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 573 | bool tmpUnderrun; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 574 | |
| 575 | std::vector<short *> buf(1, data); |
| 576 | if (writeSamples(buf, 1, &tmpUnderrun, timestamp & 0x0ffffffffll, true)) { |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 577 | pingTimestamp = timestamp; |
| 578 | return true; |
| 579 | } |
| 580 | return false; |
| 581 | #else |
| 582 | return true; |
| 583 | #endif |
| 584 | } |
| 585 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 586 | #ifndef SWLOOPBACK |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 587 | bool USRPDevice::setTxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 588 | { |
| 589 | usrp_tune_result result; |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 590 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 591 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 592 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 593 | return false; |
| 594 | } |
| 595 | |
kurtis.heimerl | b9e237e | 2011-11-26 03:17:59 +0000 | [diff] [blame] | 596 | if (m_uTx->tune(txSubdevSpec.side, m_dbTx, wFreq, &result)) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 597 | LOGC(DDEV, INFO) << "set TX: " << wFreq << std::endl |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 598 | << " baseband freq: " << result.baseband_freq << std::endl |
| 599 | << " DDC freq: " << result.dxc_freq << std::endl |
| 600 | << " residual freq: " << result.residual_freq; |
| 601 | return true; |
| 602 | } |
| 603 | else { |
Harald Welte | f97d75b | 2019-01-11 22:41:58 +0100 | [diff] [blame] | 604 | LOGC(DDEV, ALERT) << "set TX: " << wFreq << " failed" << std::endl |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 605 | << " baseband freq: " << result.baseband_freq << std::endl |
| 606 | << " DDC freq: " << result.dxc_freq << std::endl |
| 607 | << " residual freq: " << result.residual_freq; |
| 608 | return false; |
| 609 | } |
| 610 | } |
| 611 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 612 | bool USRPDevice::setRxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 613 | { |
| 614 | usrp_tune_result result; |
| 615 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 616 | if (chan) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 617 | LOGC(DDEV, ALERT) << "Invalid channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 618 | return false; |
| 619 | } |
| 620 | |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 621 | if (m_uRx->tune(0, m_dbRx, wFreq, &result)) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 622 | LOGC(DDEV, INFO) << "set RX: " << wFreq << std::endl |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 623 | << " baseband freq: " << result.baseband_freq << std::endl |
| 624 | << " DDC freq: " << result.dxc_freq << std::endl |
| 625 | << " residual freq: " << result.residual_freq; |
| 626 | return true; |
| 627 | } |
| 628 | else { |
Harald Welte | f97d75b | 2019-01-11 22:41:58 +0100 | [diff] [blame] | 629 | LOGC(DDEV, ALERT) << "set RX: " << wFreq << " failed" << std::endl |
kurtis.heimerl | 79e71c9 | 2011-11-26 03:16:48 +0000 | [diff] [blame] | 630 | << " baseband freq: " << result.baseband_freq << std::endl |
| 631 | << " DDC freq: " << result.dxc_freq << std::endl |
| 632 | << " residual freq: " << result.residual_freq; |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | } |
dburgess | b3a0ca4 | 2011-10-12 07:44:40 +0000 | [diff] [blame] | 637 | |
| 638 | #else |
| 639 | bool USRPDevice::setTxFreq(double wFreq) { return true;}; |
| 640 | bool USRPDevice::setRxFreq(double wFreq) { return true;}; |
| 641 | #endif |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 642 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 643 | RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps, |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 644 | InterfaceType iface, size_t chans, double lo_offset, |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 645 | const std::vector<std::string>& tx_paths, |
| 646 | const std::vector<std::string>& rx_paths) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 647 | { |
Harald Welte | b229439 | 2018-06-13 23:42:19 +0200 | [diff] [blame] | 648 | if (tx_sps != rx_sps) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 649 | LOGC(DDEV, ERROR) << "USRP1 requires tx_sps == rx_sps"; |
Harald Welte | b229439 | 2018-06-13 23:42:19 +0200 | [diff] [blame] | 650 | return NULL; |
| 651 | } |
| 652 | if (chans != 1) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 653 | LOGC(DDEV, ERROR) << "USRP1 supports only 1 channel"; |
Harald Welte | b229439 | 2018-06-13 23:42:19 +0200 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | if (lo_offset != 0.0) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 657 | LOGC(DDEV, ERROR) << "USRP1 doesn't support lo_offset"; |
Harald Welte | b229439 | 2018-06-13 23:42:19 +0200 | [diff] [blame] | 658 | return NULL; |
| 659 | } |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 660 | return new USRPDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 661 | } |