kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1 | /* |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 2 | * Device support for Ettus Research UHD driver |
kurtis.heimerl | 119ca9c | 2011-11-26 03:18:26 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2010,2011 Free Software Foundation, Inc. |
Tom Tsou | 05c6feb | 2016-06-22 16:09:44 -0700 | [diff] [blame] | 5 | * Copyright (C) 2015 Ettus Research LLC |
| 6 | * |
| 7 | * Author: Tom Tsou <tom.tsou@ettus.com> |
kurtis.heimerl | 119ca9c | 2011-11-26 03:18:26 +0000 | [diff] [blame] | 8 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: AGPL-3.0+ |
| 10 | * |
kurtis.heimerl | 119ca9c | 2011-11-26 03:18:26 +0000 | [diff] [blame] | 11 | * This program is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Affero General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Affero General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Affero General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * See the COPYING file in the main directory for details. |
| 24 | */ |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 25 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 26 | #include <map> |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 27 | #include "radioDevice.h" |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 28 | #include "UHDDevice.h" |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 29 | #include "Threads.h" |
| 30 | #include "Logger.h" |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 31 | |
kurtis.heimerl | ce31733 | 2011-11-26 03:18:39 +0000 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
Pau Espin Pedrol | 056ce13 | 2020-06-08 12:13:11 +0200 | [diff] [blame] | 36 | extern "C" { |
Pau Espin Pedrol | 553a250 | 2020-07-29 18:05:25 +0200 | [diff] [blame] | 37 | #include <osmocom/core/utils.h> |
Pau Espin Pedrol | 056ce13 | 2020-06-08 12:13:11 +0200 | [diff] [blame] | 38 | #include <osmocom/gsm/gsm_utils.h> |
Pau Espin Pedrol | 553a250 | 2020-07-29 18:05:25 +0200 | [diff] [blame] | 39 | #include <osmocom/vty/cpu_sched_vty.h> |
Pau Espin Pedrol | 056ce13 | 2020-06-08 12:13:11 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 42 | #ifdef USE_UHD_3_11 |
| 43 | #include <uhd/utils/log_add.hpp> |
| 44 | #include <uhd/utils/thread.hpp> |
| 45 | #else |
Tom Tsou | 72bf762 | 2017-03-07 14:16:46 -0800 | [diff] [blame] | 46 | #include <uhd/utils/msg.hpp> |
Pau Espin Pedrol | 1f4a009 | 2018-08-30 17:24:49 +0200 | [diff] [blame] | 47 | #include <uhd/utils/thread_priority.hpp> |
Tom Tsou | 72bf762 | 2017-03-07 14:16:46 -0800 | [diff] [blame] | 48 | #endif |
| 49 | |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame] | 50 | #define USRP_TX_AMPL 0.3 |
| 51 | #define UMTRX_TX_AMPL 0.7 |
Alexander Chemeris | cbfef6e | 2016-02-05 00:52:49 -0800 | [diff] [blame] | 52 | #define LIMESDR_TX_AMPL 0.3 |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 53 | #define SAMPLE_BUF_SZ (1 << 20) |
Thomas Tsou | 02d88d1 | 2013-04-05 15:36:30 -0400 | [diff] [blame] | 54 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 55 | /* |
| 56 | * UHD timeout value on streaming (re)start |
| 57 | * |
| 58 | * Allow some time for streaming to commence after the start command is issued, |
| 59 | * but consider a wait beyond one second to be a definite error condition. |
| 60 | */ |
| 61 | #define UHD_RESTART_TIMEOUT 1.0 |
| 62 | |
Alexander Chemeris | 4d029d8 | 2014-04-19 17:25:00 +0400 | [diff] [blame] | 63 | /* |
| 64 | * UmTRX specific settings |
| 65 | */ |
| 66 | #define UMTRX_VGA1_DEF -18 |
| 67 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 68 | /* |
Tom Tsou | c312905 | 2015-08-21 18:28:52 -0700 | [diff] [blame] | 69 | * USRP version dependent device timings |
| 70 | */ |
Tom Tsou | 72bf762 | 2017-03-07 14:16:46 -0800 | [diff] [blame] | 71 | #if defined(USE_UHD_3_9) || defined(USE_UHD_3_11) |
Tom Tsou | c312905 | 2015-08-21 18:28:52 -0700 | [diff] [blame] | 72 | #define B2XX_TIMING_1SPS 1.7153e-4 |
| 73 | #define B2XX_TIMING_4SPS 1.1696e-4 |
Tom Tsou | d2b0703 | 2016-04-26 19:28:59 -0700 | [diff] [blame] | 74 | #define B2XX_TIMING_4_4SPS 6.18462e-5 |
Tom Tsou | a93f789 | 2017-03-07 17:54:06 -0800 | [diff] [blame] | 75 | #define B2XX_TIMING_MCBTS 7e-5 |
Tom Tsou | 80cb080 | 2017-01-19 13:44:02 -0800 | [diff] [blame] | 76 | #else |
| 77 | #define B2XX_TIMING_1SPS 9.9692e-5 |
| 78 | #define B2XX_TIMING_4SPS 6.9248e-5 |
| 79 | #define B2XX_TIMING_4_4SPS 4.52308e-5 |
Tom Tsou | a93f789 | 2017-03-07 17:54:06 -0800 | [diff] [blame] | 80 | #define B2XX_TIMING_MCBTS 6.42452e-5 |
Tom Tsou | 80cb080 | 2017-01-19 13:44:02 -0800 | [diff] [blame] | 81 | #endif |
Tom Tsou | c312905 | 2015-08-21 18:28:52 -0700 | [diff] [blame] | 82 | |
| 83 | /* |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 84 | * Tx / Rx sample offset values. In a perfect world, there is no group delay |
| 85 | * though analog components, and behaviour through digital filters exactly |
| 86 | * matches calculated values. In reality, there are unaccounted factors, |
| 87 | * which are captured in these empirically measured (using a loopback test) |
| 88 | * timing correction values. |
| 89 | * |
| 90 | * Notes: |
| 91 | * USRP1 with timestamps is not supported by UHD. |
| 92 | */ |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 93 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 94 | static const dev_map_t dev_param_map { |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 95 | { std::make_tuple(USRP2, 1, 1), { 1, 0.0, 390625, 1.2184e-4, "N2XX 1 SPS" } }, |
| 96 | { std::make_tuple(USRP2, 4, 1), { 1, 0.0, 390625, 7.6547e-5, "N2XX 4/1 Tx/Rx SPS" } }, |
| 97 | { std::make_tuple(USRP2, 4, 4), { 1, 0.0, 390625, 4.6080e-5, "N2XX 4 SPS" } }, |
| 98 | { std::make_tuple(B100, 1, 1), { 1, 0.0, 400000, 1.2104e-4, "B100 1 SPS" } }, |
| 99 | { std::make_tuple(B100, 4, 1), { 1, 0.0, 400000, 7.9307e-5, "B100 4/1 Tx/Rx SPS" } }, |
| 100 | { std::make_tuple(B200, 1, 1), { 1, 26e6, GSMRATE, B2XX_TIMING_1SPS, "B200 1 SPS" } }, |
| 101 | { std::make_tuple(B200, 4, 1), { 1, 26e6, GSMRATE, B2XX_TIMING_4SPS, "B200 4/1 Tx/Rx SPS" } }, |
| 102 | { std::make_tuple(B200, 4, 4), { 1, 26e6, GSMRATE, B2XX_TIMING_4_4SPS, "B200 4 SPS" } }, |
| 103 | { std::make_tuple(B210, 1, 1), { 2, 26e6, GSMRATE, B2XX_TIMING_1SPS, "B210 1 SPS" } }, |
| 104 | { std::make_tuple(B210, 4, 1), { 2, 26e6, GSMRATE, B2XX_TIMING_4SPS, "B210 4/1 Tx/Rx SPS" } }, |
| 105 | { std::make_tuple(B210, 4, 4), { 2, 26e6, GSMRATE, B2XX_TIMING_4_4SPS, "B210 4 SPS" } }, |
| 106 | { std::make_tuple(E1XX, 1, 1), { 1, 52e6, GSMRATE, 9.5192e-5, "E1XX 1 SPS" } }, |
| 107 | { std::make_tuple(E1XX, 4, 1), { 1, 52e6, GSMRATE, 6.5571e-5, "E1XX 4/1 Tx/Rx SPS" } }, |
| 108 | { std::make_tuple(E3XX, 1, 1), { 2, 26e6, GSMRATE, 1.8462e-4, "E3XX 1 SPS" } }, |
| 109 | { std::make_tuple(E3XX, 4, 1), { 2, 26e6, GSMRATE, 1.2923e-4, "E3XX 4/1 Tx/Rx SPS" } }, |
| 110 | { std::make_tuple(X3XX, 1, 1), { 2, 0.0, 390625, 1.5360e-4, "X3XX 1 SPS" } }, |
| 111 | { std::make_tuple(X3XX, 4, 1), { 2, 0.0, 390625, 1.1264e-4, "X3XX 4/1 Tx/Rx SPS" } }, |
| 112 | { std::make_tuple(X3XX, 4, 4), { 2, 0.0, 390625, 5.6567e-5, "X3XX 4 SPS" } }, |
| 113 | { std::make_tuple(UMTRX, 1, 1), { 2, 0.0, GSMRATE, 9.9692e-5, "UmTRX 1 SPS" } }, |
| 114 | { std::make_tuple(UMTRX, 4, 1), { 2, 0.0, GSMRATE, 7.3846e-5, "UmTRX 4/1 Tx/Rx SPS"} }, |
| 115 | { std::make_tuple(UMTRX, 4, 4), { 2, 0.0, GSMRATE, 5.1503e-5, "UmTRX 4 SPS" } }, |
Tom Tsou | 4cafb0f | 2017-06-26 10:13:25 -0700 | [diff] [blame] | 116 | { std::make_tuple(LIMESDR, 4, 4), { 1, GSMRATE*32, GSMRATE, 8.9e-5, "LimeSDR 4 SPS" } }, |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 117 | { std::make_tuple(B2XX_MCBTS, 4, 4), { 1, 51.2e6, MCBTS_SPACING*4, B2XX_TIMING_MCBTS, "B200/B210 4 SPS Multi-ARFCN" } }, |
| 118 | }; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 119 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 120 | static const power_map_t dev_band_nom_power_param_map { |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 121 | { std::make_tuple(B200, GSM_BAND_850), { 89.75, 13.3, -7.5 } }, |
| 122 | { std::make_tuple(B200, GSM_BAND_900), { 89.75, 13.3, -7.5 } }, |
| 123 | { std::make_tuple(B200, GSM_BAND_1800), { 89.75, 7.5, -11.0 } }, |
| 124 | { std::make_tuple(B200, GSM_BAND_1900), { 89.75, 7.7, -11.0 } }, |
| 125 | { std::make_tuple(B210, GSM_BAND_850), { 89.75, 13.3, -7.5 } }, |
| 126 | { std::make_tuple(B210, GSM_BAND_900), { 89.75, 13.3, -7.5 } }, |
| 127 | { std::make_tuple(B210, GSM_BAND_1800), { 89.75, 7.5, -11.0 } }, |
| 128 | { std::make_tuple(B210, GSM_BAND_1900), { 89.75, 7.7, -11.0 } }, |
Pau Espin Pedrol | 056ce13 | 2020-06-08 12:13:11 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 131 | void *async_event_loop(uhd_device *dev) |
| 132 | { |
Pau Espin Pedrol | 5b60c98 | 2018-09-20 18:04:46 +0200 | [diff] [blame] | 133 | set_selfthread_name("UHDAsyncEvent"); |
Eric Wild | 1e17c4f | 2020-03-24 17:19:27 +0100 | [diff] [blame] | 134 | osmo_cpu_sched_vty_apply_localthread(); |
Thomas Tsou | 7553aa9 | 2013-11-08 12:50:03 -0500 | [diff] [blame] | 135 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 136 | while (1) { |
| 137 | dev->recv_async_msg(); |
| 138 | pthread_testcancel(); |
| 139 | } |
Thomas Tsou | 3f32ab5 | 2013-11-15 16:32:54 -0500 | [diff] [blame] | 140 | |
| 141 | return NULL; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 144 | #ifdef USE_UHD_3_11 |
| 145 | static void uhd_log_handler(const uhd::log::logging_info &info) |
| 146 | { |
| 147 | int level; |
| 148 | |
| 149 | switch (info.verbosity) |
| 150 | { |
| 151 | case uhd::log::trace: |
| 152 | case uhd::log::debug: |
| 153 | level = LOGL_DEBUG; |
| 154 | break; |
| 155 | case uhd::log::info: |
| 156 | level = LOGL_INFO; |
| 157 | break; |
| 158 | case uhd::log::warning: |
| 159 | level = LOGL_NOTICE; |
| 160 | break; |
| 161 | case uhd::log::error: |
| 162 | level = LOGL_ERROR; |
| 163 | break; |
| 164 | case uhd::log::fatal: |
| 165 | level = LOGL_FATAL; |
| 166 | break; |
| 167 | default: |
| 168 | level = LOGL_NOTICE; |
| 169 | } |
| 170 | |
| 171 | LOGSRC(DDEVDRV, level, info.file.c_str(), info.line) << "[" << info.component << "] " << info.message; |
| 172 | } |
| 173 | #else |
Tom Tsou | 72bf762 | 2017-03-07 14:16:46 -0800 | [diff] [blame] | 174 | /* |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 175 | Catch and drop underrun 'U' and overrun 'O' messages from stdout |
| 176 | since we already report using the logging facility. Direct |
| 177 | everything else appropriately. |
| 178 | */ |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 179 | static void uhd_msg_handler(uhd::msg::type_t type, const std::string &msg) |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 180 | { |
| 181 | switch (type) { |
| 182 | case uhd::msg::status: |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 183 | LOGC(DDEVDRV, INFO) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 184 | break; |
| 185 | case uhd::msg::warning: |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 186 | LOGC(DDEVDRV, NOTICE) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 187 | break; |
| 188 | case uhd::msg::error: |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 189 | LOGC(DDEVDRV, ERROR) << msg; |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 190 | break; |
| 191 | case uhd::msg::fastpath: |
| 192 | break; |
| 193 | } |
| 194 | } |
Tom Tsou | 72bf762 | 2017-03-07 14:16:46 -0800 | [diff] [blame] | 195 | #endif |
kurtis.heimerl | 0803ad9 | 2011-11-26 03:18:51 +0000 | [diff] [blame] | 196 | |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 197 | /* So far measurements done for B210 show really close to linear relationship |
| 198 | * between gain and real output power, so we simply adjust the measured offset |
| 199 | */ |
| 200 | static double TxGain2TxPower(const dev_band_desc &desc, double tx_gain_db) |
| 201 | { |
| 202 | return desc.nom_out_tx_power - (desc.nom_uhd_tx_gain - tx_gain_db); |
| 203 | } |
| 204 | static double TxPower2TxGain(const dev_band_desc &desc, double tx_power_dbm) |
| 205 | { |
| 206 | return desc.nom_uhd_tx_gain - (desc.nom_out_tx_power - tx_power_dbm); |
| 207 | } |
| 208 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 209 | uhd_device::uhd_device(InterfaceType iface, const struct trx_cfg *cfg) |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 210 | : RadioDevice(iface, cfg), band_manager(dev_band_nom_power_param_map, dev_param_map), rx_gain_min(0.0), |
| 211 | rx_gain_max(0.0), tx_spp(0), rx_spp(0), started(false), aligned(false), drop_cnt(0), prev_ts(0, 0), |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 212 | ts_initial(0), ts_offset(0), async_event_thrd(NULL) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 213 | { |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | uhd_device::~uhd_device() |
| 217 | { |
| 218 | stop(); |
| 219 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 220 | for (size_t i = 0; i < rx_buffers.size(); i++) |
| 221 | delete rx_buffers[i]; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 222 | } |
| 223 | |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 224 | void uhd_device::init_gains() |
| 225 | { |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 226 | double tx_gain_min, tx_gain_max; |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 227 | uhd::gain_range_t range; |
| 228 | |
Alexander Chemeris | 4d029d8 | 2014-04-19 17:25:00 +0400 | [diff] [blame] | 229 | if (dev_type == UMTRX) { |
| 230 | std::vector<std::string> gain_stages = usrp_dev->get_tx_gain_names(0); |
| 231 | if (gain_stages[0] == "VGA") { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 232 | LOGC(DDEV, WARNING) << "Update your UHD version for a proper Tx gain support"; |
Alexander Chemeris | 4d029d8 | 2014-04-19 17:25:00 +0400 | [diff] [blame] | 233 | } |
| 234 | if (gain_stages[0] == "VGA" || gain_stages[0] == "PA") { |
| 235 | range = usrp_dev->get_tx_gain_range(); |
| 236 | tx_gain_min = range.start(); |
| 237 | tx_gain_max = range.stop(); |
| 238 | } else { |
| 239 | range = usrp_dev->get_tx_gain_range("VGA2"); |
| 240 | tx_gain_min = UMTRX_VGA1_DEF + range.start(); |
| 241 | tx_gain_max = UMTRX_VGA1_DEF + range.stop(); |
| 242 | } |
| 243 | } else { |
| 244 | range = usrp_dev->get_tx_gain_range(); |
| 245 | tx_gain_min = range.start(); |
| 246 | tx_gain_max = range.stop(); |
| 247 | } |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 248 | LOGC(DDEV, INFO) << "Supported Tx gain range [" << tx_gain_min << "; " << tx_gain_max << "]"; |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 249 | |
| 250 | range = usrp_dev->get_rx_gain_range(); |
| 251 | rx_gain_min = range.start(); |
| 252 | rx_gain_max = range.stop(); |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 253 | LOGC(DDEV, INFO) << "Supported Rx gain range [" << rx_gain_min << "; " << rx_gain_max << "]"; |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 254 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 255 | for (size_t i = 0; i < tx_gains.size(); i++) { |
Alexander Chemeris | c4eab87 | 2015-05-17 23:25:57 -0400 | [diff] [blame] | 256 | double gain = (tx_gain_min + tx_gain_max) / 2; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 257 | LOGC(DDEV, INFO) << "Default setting Tx gain for channel " << i << " to " << gain; |
Alexander Chemeris | c4eab87 | 2015-05-17 23:25:57 -0400 | [diff] [blame] | 258 | usrp_dev->set_tx_gain(gain, i); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 259 | tx_gains[i] = usrp_dev->get_tx_gain(i); |
| 260 | } |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 261 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 262 | for (size_t i = 0; i < rx_gains.size(); i++) { |
Alexander Chemeris | c4eab87 | 2015-05-17 23:25:57 -0400 | [diff] [blame] | 263 | double gain = (rx_gain_min + rx_gain_max) / 2; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 264 | LOGC(DDEV, INFO) << "Default setting Rx gain for channel " << i << " to " << gain; |
Alexander Chemeris | c4eab87 | 2015-05-17 23:25:57 -0400 | [diff] [blame] | 265 | usrp_dev->set_rx_gain(gain, i); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 266 | rx_gains[i] = usrp_dev->get_rx_gain(i); |
| 267 | } |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 268 | |
| 269 | return; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 270 | |
kurtis.heimerl | 24481de | 2011-11-26 03:17:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 273 | void uhd_device::set_rates() |
Tom Tsou | 3f4a13f | 2016-05-03 19:02:24 -0700 | [diff] [blame] | 274 | { |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 275 | dev_desc desc = dev_param_map.at(dev_key(dev_type, tx_sps, rx_sps)); |
| 276 | if (desc.mcr != 0.0) |
| 277 | usrp_dev->set_master_clock_rate(desc.mcr); |
Tom Tsou | 3f4a13f | 2016-05-03 19:02:24 -0700 | [diff] [blame] | 278 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 279 | tx_rate = (dev_type != B2XX_MCBTS) ? desc.rate * tx_sps : desc.rate; |
| 280 | rx_rate = (dev_type != B2XX_MCBTS) ? desc.rate * rx_sps : desc.rate; |
Tom Tsou | 3f4a13f | 2016-05-03 19:02:24 -0700 | [diff] [blame] | 281 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 282 | usrp_dev->set_tx_rate(tx_rate); |
| 283 | usrp_dev->set_rx_rate(rx_rate); |
| 284 | tx_rate = usrp_dev->get_tx_rate(); |
| 285 | rx_rate = usrp_dev->get_rx_rate(); |
Tom Tsou | 3f4a13f | 2016-05-03 19:02:24 -0700 | [diff] [blame] | 286 | |
Tom Tsou | 1b6ab7d | 2017-06-15 15:31:08 -0700 | [diff] [blame] | 287 | ts_offset = static_cast<TIMESTAMP>(desc.offset * rx_rate); |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 288 | LOGC(DDEV, INFO) << "Rates configured for " << desc.desc_str; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 291 | double uhd_device::setRxGain(double db, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 292 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 293 | if (chan >= rx_gains.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 294 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 295 | return 0.0f; |
| 296 | } |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 297 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 298 | usrp_dev->set_rx_gain(db, chan); |
| 299 | rx_gains[chan] = usrp_dev->get_rx_gain(chan); |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 300 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 301 | LOGC(DDEV, INFO) << "Set RX gain to " << rx_gains[chan] << "dB (asked for " << db << "dB)"; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 302 | |
| 303 | return rx_gains[chan]; |
| 304 | } |
| 305 | |
| 306 | double uhd_device::getRxGain(size_t chan) |
| 307 | { |
| 308 | if (chan >= rx_gains.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 309 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 310 | return 0.0f; |
| 311 | } |
| 312 | |
| 313 | return rx_gains[chan]; |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Pau Espin Pedrol | e91544d | 2020-10-13 17:03:37 +0200 | [diff] [blame] | 316 | double uhd_device::rssiOffset(size_t chan) |
| 317 | { |
| 318 | double rssiOffset; |
| 319 | dev_band_desc desc; |
| 320 | |
| 321 | if (chan >= rx_gains.size()) { |
| 322 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
| 323 | return 0.0f; |
| 324 | } |
| 325 | |
| 326 | get_dev_band_desc(desc); |
| 327 | rssiOffset = rx_gains[chan] + desc.rxgain2rssioffset_rel; |
| 328 | return rssiOffset; |
| 329 | } |
| 330 | |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 331 | double uhd_device::setPowerAttenuation(int atten, size_t chan) { |
Pau Espin Pedrol | 58d80a0 | 2020-06-19 19:29:59 +0200 | [diff] [blame] | 332 | double tx_power, db; |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 333 | dev_band_desc desc; |
| 334 | |
| 335 | if (chan >= tx_gains.size()) { |
| 336 | LOGC(DDEV, ALERT) << "Requested non-existent channel" << chan; |
| 337 | return 0.0f; |
| 338 | } |
| 339 | |
| 340 | get_dev_band_desc(desc); |
Pau Espin Pedrol | 58d80a0 | 2020-06-19 19:29:59 +0200 | [diff] [blame] | 341 | tx_power = desc.nom_out_tx_power - atten; |
| 342 | db = TxPower2TxGain(desc, tx_power); |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 343 | |
| 344 | if (dev_type == UMTRX) { |
| 345 | std::vector<std::string> gain_stages = usrp_dev->get_tx_gain_names(0); |
| 346 | if (gain_stages[0] == "VGA" || gain_stages[0] == "PA") { |
| 347 | usrp_dev->set_tx_gain(db, chan); |
| 348 | } else { |
| 349 | // New UHD versions support split configuration of |
| 350 | // Tx gain stages. We utilize this to set the gain |
| 351 | // configuration, optimal for the Tx signal quality. |
| 352 | // From our measurements, VGA1 must be 18dB plus-minus |
| 353 | // one and VGA2 is the best when 23dB or lower. |
| 354 | usrp_dev->set_tx_gain(UMTRX_VGA1_DEF, "VGA1", chan); |
| 355 | usrp_dev->set_tx_gain(db-UMTRX_VGA1_DEF, "VGA2", chan); |
| 356 | } |
| 357 | } else { |
| 358 | usrp_dev->set_tx_gain(db, chan); |
| 359 | } |
| 360 | |
| 361 | tx_gains[chan] = usrp_dev->get_tx_gain(chan); |
| 362 | |
Pau Espin Pedrol | 58d80a0 | 2020-06-19 19:29:59 +0200 | [diff] [blame] | 363 | LOGC(DDEV, INFO) << "Set TX gain to " << tx_gains[chan] << "dB, ~" |
| 364 | << TxGain2TxPower(desc, tx_gains[chan]) << " dBm " |
| 365 | << "(asked for " << db << " dB, ~" << tx_power << " dBm)"; |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 366 | |
| 367 | return desc.nom_out_tx_power - TxGain2TxPower(desc, tx_gains[chan]); |
| 368 | } |
| 369 | double uhd_device::getPowerAttenuation(size_t chan) { |
| 370 | dev_band_desc desc; |
Pau Espin Pedrol | 2ab9218 | 2019-09-13 17:05:02 +0200 | [diff] [blame] | 371 | if (chan >= tx_gains.size()) { |
| 372 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
| 373 | return 0.0f; |
| 374 | } |
| 375 | |
Pau Espin Pedrol | b899c19 | 2020-06-08 14:49:33 +0200 | [diff] [blame] | 376 | get_dev_band_desc(desc); |
| 377 | return desc.nom_out_tx_power - TxGain2TxPower(desc, tx_gains[chan]); |
Pau Espin Pedrol | 2ab9218 | 2019-09-13 17:05:02 +0200 | [diff] [blame] | 378 | } |
| 379 | |
Pau Espin Pedrol | 0e09e7c | 2020-05-29 16:39:07 +0200 | [diff] [blame] | 380 | int uhd_device::getNominalTxPower(size_t chan) |
| 381 | { |
Pau Espin Pedrol | 056ce13 | 2020-06-08 12:13:11 +0200 | [diff] [blame] | 382 | dev_band_desc desc; |
| 383 | get_dev_band_desc(desc); |
| 384 | |
| 385 | return desc.nom_out_tx_power; |
Pau Espin Pedrol | 0e09e7c | 2020-05-29 16:39:07 +0200 | [diff] [blame] | 386 | } |
| 387 | |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 388 | /* |
| 389 | 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] | 390 | 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] | 391 | deal with the transport latency. Reject the USRP1 because UHD doesn't |
| 392 | support timestamped samples with it. |
| 393 | */ |
| 394 | bool uhd_device::parse_dev_type() |
| 395 | { |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 396 | uhd::property_tree::sptr prop_tree = usrp_dev->get_device()->get_tree(); |
| 397 | std::string devString = prop_tree->access<std::string>("/name").get(); |
| 398 | std::string mboardString = usrp_dev->get_mboard_name(); |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 399 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 400 | const std::map<std::string, std::pair<uhd_dev_type, TxWindowType>> devStringMap { |
| 401 | { "B100", { B100, TX_WINDOW_USRP1 } }, |
| 402 | { "B200", { B200, TX_WINDOW_USRP1 } }, |
| 403 | { "B200mini", { B200, TX_WINDOW_USRP1 } }, |
Piotr Krysik | aa60dda | 2017-12-04 00:29:16 +0700 | [diff] [blame] | 404 | { "B205mini", { B200, TX_WINDOW_USRP1 } }, |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 405 | { "B210", { B210, TX_WINDOW_USRP1 } }, |
| 406 | { "E100", { E1XX, TX_WINDOW_FIXED } }, |
| 407 | { "E110", { E1XX, TX_WINDOW_FIXED } }, |
| 408 | { "E310", { E3XX, TX_WINDOW_FIXED } }, |
| 409 | { "E3XX", { E3XX, TX_WINDOW_FIXED } }, |
| 410 | { "X300", { X3XX, TX_WINDOW_FIXED } }, |
| 411 | { "X310", { X3XX, TX_WINDOW_FIXED } }, |
Tom Tsou | 988a464 | 2017-06-15 09:16:53 -0700 | [diff] [blame] | 412 | { "USRP2", { USRP2, TX_WINDOW_FIXED } }, |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 413 | { "UmTRX", { UMTRX, TX_WINDOW_FIXED } }, |
ignasj | 87ed77b | 2017-06-13 23:37:46 +0300 | [diff] [blame] | 414 | { "LimeSDR", { LIMESDR, TX_WINDOW_FIXED } }, |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 415 | }; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 416 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 417 | // Compare UHD motherboard and device strings */ |
Tom Tsou | 988a464 | 2017-06-15 09:16:53 -0700 | [diff] [blame] | 418 | auto mapIter = devStringMap.begin(); |
| 419 | while (mapIter != devStringMap.end()) { |
| 420 | if (devString.find(mapIter->first) != std::string::npos || |
| 421 | mboardString.find(mapIter->first) != std::string::npos) { |
| 422 | dev_type = std::get<0>(mapIter->second); |
| 423 | tx_window = std::get<1>(mapIter->second); |
| 424 | return true; |
| 425 | } |
| 426 | mapIter++; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 429 | LOGC(DDEV, ALERT) << "Unsupported device " << devString; |
Tom Tsou | 988a464 | 2017-06-15 09:16:53 -0700 | [diff] [blame] | 430 | return false; |
kurtis.heimerl | b7358ce | 2011-11-26 03:18:57 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Tom Tsou | 3b093bb | 2016-05-03 19:04:15 -0700 | [diff] [blame] | 433 | /* |
| 434 | * Check for UHD version > 3.9.0 for E3XX support |
| 435 | */ |
| 436 | static bool uhd_e3xx_version_chk() |
| 437 | { |
| 438 | std::string ver = uhd::get_version_string(); |
| 439 | std::string major_str(ver.begin(), ver.begin() + 3); |
| 440 | std::string minor_str(ver.begin() + 4, ver.begin() + 7); |
| 441 | |
| 442 | int major_val = atoi(major_str.c_str()); |
| 443 | int minor_val = atoi(minor_str.c_str()); |
| 444 | |
| 445 | if (major_val < 3) |
| 446 | return false; |
| 447 | if (minor_val < 9) |
| 448 | return false; |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
Tom Tsou | 980525c | 2017-06-09 15:37:19 -0700 | [diff] [blame] | 453 | void uhd_device::set_channels(bool swap) |
| 454 | { |
| 455 | if (iface == MULTI_ARFCN) { |
| 456 | if (dev_type != B200 && dev_type != B210) |
| 457 | throw std::invalid_argument("Device does not support MCBTS"); |
| 458 | dev_type = B2XX_MCBTS; |
Tom Tsou | 980525c | 2017-06-09 15:37:19 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Tom Tsou | f611569 | 2017-06-26 10:13:25 -0700 | [diff] [blame] | 461 | if (chans > dev_param_map.at(dev_key(dev_type, tx_sps, rx_sps)).channels) |
Tom Tsou | 980525c | 2017-06-09 15:37:19 -0700 | [diff] [blame] | 462 | throw std::invalid_argument("Device does not support number of requested channels"); |
| 463 | |
| 464 | std::string subdev_string; |
| 465 | switch (dev_type) { |
| 466 | case B210: |
| 467 | case E3XX: |
| 468 | if (chans == 1) |
| 469 | subdev_string = swap ? "A:B" : "A:A"; |
| 470 | else if (chans == 2) |
| 471 | subdev_string = swap ? "A:B A:A" : "A:A A:B"; |
| 472 | break; |
| 473 | case X3XX: |
| 474 | case UMTRX: |
| 475 | if (chans == 1) |
| 476 | subdev_string = swap ? "B:0" : "A:0"; |
| 477 | else if (chans == 2) |
| 478 | subdev_string = swap ? "B:0 A:0" : "A:0 B:0"; |
| 479 | break; |
| 480 | default: |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | if (!subdev_string.empty()) { |
| 485 | uhd::usrp::subdev_spec_t spec(subdev_string); |
| 486 | usrp_dev->set_tx_subdev_spec(spec); |
| 487 | usrp_dev->set_rx_subdev_spec(spec); |
| 488 | } |
| 489 | } |
| 490 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 491 | int uhd_device::open() |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 492 | { |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 493 | const char *refstr; |
Eric | 5e6b10c | 2021-05-28 21:20:19 +0200 | [diff] [blame] | 494 | int clock_lock_attempts = 15; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 495 | |
Pau Espin Pedrol | 84231bd | 2019-12-20 22:47:06 +0100 | [diff] [blame] | 496 | /* Register msg handler. Different APIs depending on UHD version */ |
| 497 | #ifdef USE_UHD_3_11 |
| 498 | uhd::log::add_logger("OsmoTRX", &uhd_log_handler); |
| 499 | uhd::log::set_log_level(uhd::log::debug); |
| 500 | uhd::log::set_console_level(uhd::log::off); |
| 501 | uhd::log::set_logger_level("OsmoTRX", uhd::log::debug); |
| 502 | #else |
| 503 | uhd::msg::register_handler(&uhd_msg_handler); |
| 504 | #endif |
| 505 | |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 506 | // Find UHD devices |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 507 | uhd::device_addr_t addr(cfg->dev_args); |
ttsou | f60dafa | 2012-10-22 00:07:14 +0000 | [diff] [blame] | 508 | uhd::device_addrs_t dev_addrs = uhd::device::find(addr); |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 509 | if (dev_addrs.size() == 0) { |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 510 | LOGC(DDEV, ALERT) << "No UHD devices found with address '" << cfg->dev_args << "'"; |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 511 | return -1; |
kurtis.heimerl | 4403d4f | 2011-11-28 06:26:04 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Use the first found device |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 515 | LOGC(DDEV, INFO) << "Using discovered UHD device " << dev_addrs[0].to_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 516 | try { |
Tom Tsou | 5c7c178 | 2015-05-18 16:51:44 -0700 | [diff] [blame] | 517 | usrp_dev = uhd::usrp::multi_usrp::make(addr); |
d0gtail | ebb3769 | 2018-12-01 17:02:15 +0100 | [diff] [blame] | 518 | } catch(uhd::key_error::exception &e) { |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 519 | LOGC(DDEV, ALERT) << "UHD make failed, device " << cfg->dev_args << ", exception:\n" << e.what(); |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 520 | return -1; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 521 | } |
| 522 | |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 523 | // Check for a valid device type and set bus type |
| 524 | if (!parse_dev_type()) |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 525 | return -1; |
kurtis.heimerl | 523ae5a | 2011-11-28 06:26:01 +0000 | [diff] [blame] | 526 | |
Tom Tsou | 3b093bb | 2016-05-03 19:04:15 -0700 | [diff] [blame] | 527 | if ((dev_type == E3XX) && !uhd_e3xx_version_chk()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 528 | LOGC(DDEV, ALERT) << "E3XX requires UHD 003.009.000 or greater"; |
Tom Tsou | 3b093bb | 2016-05-03 19:04:15 -0700 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | |
Tom Tsou | 980525c | 2017-06-09 15:37:19 -0700 | [diff] [blame] | 532 | try { |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 533 | set_channels(cfg->swap_channels); |
| 534 | } catch (const std::exception &e) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 535 | LOGC(DDEV, ALERT) << "Channel setting failed - " << e.what(); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 536 | return -1; |
| 537 | } |
| 538 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 539 | if (!set_antennas()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 540 | LOGC(DDEV, ALERT) << "UHD antenna setting failed"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 544 | tx_freqs.resize(chans); |
| 545 | rx_freqs.resize(chans); |
| 546 | tx_gains.resize(chans); |
| 547 | rx_gains.resize(chans); |
| 548 | rx_buffers.resize(chans); |
| 549 | |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 550 | switch (cfg->clock_ref) { |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 551 | case REF_INTERNAL: |
| 552 | refstr = "internal"; |
| 553 | break; |
| 554 | case REF_EXTERNAL: |
| 555 | refstr = "external"; |
| 556 | break; |
| 557 | case REF_GPS: |
| 558 | refstr = "gpsdo"; |
| 559 | break; |
| 560 | default: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 561 | LOGC(DDEV, ALERT) << "Invalid reference type"; |
Tom Tsou | 2f3e60b | 2016-07-17 19:29:08 -0700 | [diff] [blame] | 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | usrp_dev->set_clock_source(refstr); |
Thomas Tsou | 010fff7 | 2013-10-16 00:31:18 -0400 | [diff] [blame] | 566 | |
Eric | 5e6b10c | 2021-05-28 21:20:19 +0200 | [diff] [blame] | 567 | std::vector<std::string> sensor_names = usrp_dev->get_mboard_sensor_names(); |
| 568 | if (std::find(sensor_names.begin(), sensor_names.end(), "ref_locked") != sensor_names.end()) { |
| 569 | LOGC(DDEV, INFO) << "Waiting for clock reference lock (max " << clock_lock_attempts << "s)..." << std::flush; |
| 570 | while (!usrp_dev->get_mboard_sensor("ref_locked", 0).to_bool() && clock_lock_attempts--) |
| 571 | sleep(1); |
| 572 | |
| 573 | if (!clock_lock_attempts) { |
| 574 | LOGC(DDEV, ALERT) << "Locking to external 10Mhz failed!"; |
| 575 | return -1; |
| 576 | } |
| 577 | } |
| 578 | LOGC(DDEV, INFO) << "Selected clock source is " << usrp_dev->get_clock_source(0); |
| 579 | |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 580 | try { |
| 581 | set_rates(); |
| 582 | } catch (const std::exception &e) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 583 | LOGC(DDEV, ALERT) << "UHD rate setting failed - " << e.what(); |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 584 | return -1; |
Tom Tsou | 1fb0ce6 | 2017-06-08 19:41:48 -0700 | [diff] [blame] | 585 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 586 | |
Alexander Chemeris | e171425 | 2015-04-28 23:09:02 -0400 | [diff] [blame] | 587 | // Set RF frontend bandwidth |
| 588 | if (dev_type == UMTRX) { |
| 589 | // Setting LMS6002D LPF to 500kHz gives us the best signal quality |
| 590 | for (size_t i = 0; i < chans; i++) { |
| 591 | usrp_dev->set_tx_bandwidth(500*1000*2, i); |
Tom Tsou | d6ae864 | 2017-03-30 17:22:58 -0700 | [diff] [blame] | 592 | usrp_dev->set_rx_bandwidth(500*1000*2, i); |
Alexander Chemeris | e171425 | 2015-04-28 23:09:02 -0400 | [diff] [blame] | 593 | } |
Alexander Chemeris | cbfef6e | 2016-02-05 00:52:49 -0800 | [diff] [blame] | 594 | } else if (dev_type == LIMESDR) { |
| 595 | for (size_t i = 0; i < chans; i++) { |
Pau Espin Pedrol | 55df1e4 | 2018-05-08 20:49:00 +0200 | [diff] [blame] | 596 | usrp_dev->set_tx_bandwidth(5.2e6, i); |
| 597 | usrp_dev->set_rx_bandwidth(1.4001e6, i); |
Alexander Chemeris | cbfef6e | 2016-02-05 00:52:49 -0800 | [diff] [blame] | 598 | } |
Alexander Chemeris | e171425 | 2015-04-28 23:09:02 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Thomas Tsou | cecfb2e | 2014-03-27 13:44:09 -0400 | [diff] [blame] | 601 | /* Create TX and RX streamers */ |
| 602 | uhd::stream_args_t stream_args("sc16"); |
| 603 | for (size_t i = 0; i < chans; i++) |
| 604 | stream_args.channels.push_back(i); |
| 605 | |
| 606 | tx_stream = usrp_dev->get_tx_stream(stream_args); |
| 607 | rx_stream = usrp_dev->get_rx_stream(stream_args); |
| 608 | |
| 609 | /* Number of samples per over-the-wire packet */ |
| 610 | tx_spp = tx_stream->get_max_num_samps(); |
| 611 | rx_spp = rx_stream->get_max_num_samps(); |
| 612 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 613 | // Create receive buffer |
Thomas Tsou | e3e8814 | 2013-04-05 20:42:41 -0400 | [diff] [blame] | 614 | size_t buf_len = SAMPLE_BUF_SZ / sizeof(uint32_t); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 615 | for (size_t i = 0; i < rx_buffers.size(); i++) |
Pau Espin Pedrol | 580c48b | 2019-05-03 14:38:36 +0200 | [diff] [blame] | 616 | rx_buffers[i] = new smpl_buf(buf_len); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 617 | |
Pau Espin Pedrol | ef192d3 | 2019-04-30 18:51:00 +0200 | [diff] [blame] | 618 | // Create vector buffer |
| 619 | pkt_bufs = std::vector<std::vector<short> >(chans, std::vector<short>(2 * rx_spp)); |
| 620 | for (size_t i = 0; i < pkt_bufs.size(); i++) |
| 621 | pkt_ptrs.push_back(&pkt_bufs[i].front()); |
| 622 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 623 | // Initialize and shadow gain values |
kurtis.heimerl | 02d0405 | 2011-11-26 03:17:10 +0000 | [diff] [blame] | 624 | init_gains(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 625 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 626 | // Print configuration |
Pau Espin Pedrol | 9279e0e | 2019-12-20 23:02:53 +0100 | [diff] [blame] | 627 | LOGC(DDEV, INFO) << "Device configuration: " << usrp_dev->get_pp_string(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 628 | |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 629 | if (iface == MULTI_ARFCN) |
| 630 | return MULTI_ARFCN; |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 631 | |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 632 | switch (dev_type) { |
| 633 | case B100: |
| 634 | return RESAMP_64M; |
| 635 | case USRP2: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 636 | case X3XX: |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 637 | return RESAMP_100M; |
Thomas Tsou | e788239 | 2014-02-13 14:46:23 -0500 | [diff] [blame] | 638 | case B200: |
| 639 | case B210: |
Thomas Tsou | a5c83ae | 2014-04-02 03:31:44 +0100 | [diff] [blame] | 640 | case E1XX: |
Tom Tsou | 4ad9ea6 | 2014-12-03 18:47:20 -0800 | [diff] [blame] | 641 | case E3XX: |
Alexander Chemeris | cbfef6e | 2016-02-05 00:52:49 -0800 | [diff] [blame] | 642 | case LIMESDR: |
Thomas Tsou | e90a42b | 2013-11-13 23:38:09 -0500 | [diff] [blame] | 643 | default: |
| 644 | break; |
Thomas Tsou | fe269fe | 2013-10-14 23:56:51 -0400 | [diff] [blame] | 645 | } |
Thomas Tsou | cb69f08 | 2013-04-08 14:18:26 -0400 | [diff] [blame] | 646 | |
| 647 | return NORMAL; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 648 | } |
| 649 | |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 650 | bool uhd_device::flush_recv(size_t num_pkts) |
| 651 | { |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 652 | uhd::rx_metadata_t md; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 653 | size_t num_smpls; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 654 | float timeout = UHD_RESTART_TIMEOUT; |
kurtis.heimerl | 187b03d | 2011-11-26 03:17:40 +0000 | [diff] [blame] | 655 | |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 656 | ts_initial = 0; |
| 657 | while (!ts_initial || (num_pkts-- > 0)) { |
| 658 | num_smpls = rx_stream->recv(pkt_ptrs, rx_spp, md, |
Thomas Tsou | b4cb4e2 | 2013-04-05 15:11:35 -0400 | [diff] [blame] | 659 | timeout, true); |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 660 | if (!num_smpls) { |
| 661 | switch (md.error_code) { |
| 662 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 663 | LOGC(DDEV, ALERT) << "Device timed out"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 664 | return false; |
kurtis.heimerl | 495d3c8 | 2011-11-26 03:18:02 +0000 | [diff] [blame] | 665 | default: |
| 666 | continue; |
| 667 | } |
| 668 | } |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 669 | |
Tom Tsou | d4d3daa | 2015-08-21 19:21:28 -0700 | [diff] [blame] | 670 | ts_initial = md.time_spec.to_ticks(rx_rate); |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 673 | LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl; |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 674 | |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 678 | bool uhd_device::restart() |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 679 | { |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 680 | /* Allow 100 ms delay to align multi-channel streams */ |
| 681 | double delay = 0.1; |
| 682 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 683 | aligned = false; |
| 684 | |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 685 | uhd::time_spec_t current = usrp_dev->get_time_now(); |
| 686 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 687 | 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] | 688 | cmd.stream_now = false; |
| 689 | cmd.time_spec = uhd::time_spec_t(current.get_real_secs() + delay); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 690 | |
kurtis.heimerl | 6829210 | 2011-11-26 03:17:28 +0000 | [diff] [blame] | 691 | usrp_dev->issue_stream_cmd(cmd); |
Thomas Tsou | 18d3b83 | 2014-02-13 14:55:23 -0500 | [diff] [blame] | 692 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 693 | return flush_recv(10); |
kurtis.heimerl | a14d4be | 2011-11-26 03:17:23 +0000 | [diff] [blame] | 694 | } |
| 695 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 696 | bool uhd_device::start() |
| 697 | { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 698 | LOGC(DDEV, INFO) << "Starting USRP..."; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 699 | |
| 700 | if (started) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 701 | LOGC(DDEV, ERROR) << "Device already started"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 702 | return false; |
| 703 | } |
| 704 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 705 | // Start asynchronous event (underrun check) loop |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 706 | async_event_thrd = new Thread(); |
| 707 | async_event_thrd->start((void * (*)(void*))async_event_loop, (void*)this); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 708 | |
| 709 | // Start streaming |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 710 | if (!restart()) |
| 711 | return false; |
kurtis.heimerl | 33f748f | 2011-11-26 03:16:57 +0000 | [diff] [blame] | 712 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 713 | // Display usrp time |
| 714 | double time_now = usrp_dev->get_time_now().get_real_secs(); |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 715 | LOGC(DDEV, INFO) << "The current time is " << time_now << " seconds"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 716 | |
| 717 | started = true; |
| 718 | return true; |
| 719 | } |
| 720 | |
| 721 | bool uhd_device::stop() |
| 722 | { |
Thomas Tsou | 2c79110 | 2013-11-15 23:00:28 -0500 | [diff] [blame] | 723 | if (!started) |
| 724 | return false; |
| 725 | |
| 726 | uhd::stream_cmd_t stream_cmd = |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 727 | uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS; |
| 728 | |
| 729 | usrp_dev->issue_stream_cmd(stream_cmd); |
| 730 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 731 | async_event_thrd->cancel(); |
| 732 | async_event_thrd->join(); |
| 733 | delete async_event_thrd; |
| 734 | |
Eric | 4080eb7 | 2020-07-16 18:08:30 +0200 | [diff] [blame] | 735 | /* reset internal buffer timestamps */ |
| 736 | for (size_t i = 0; i < rx_buffers.size(); i++) |
| 737 | rx_buffers[i]->reset(); |
| 738 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 739 | band_reset(); |
Pau Espin Pedrol | bb2cb9d | 2021-09-21 13:52:46 +0200 | [diff] [blame] | 740 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 741 | started = false; |
| 742 | return true; |
| 743 | } |
| 744 | |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 745 | 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] | 746 | { |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 747 | if (!num_smpls) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 748 | LOGC(DDEV, ERROR) << str_code(md); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 749 | |
| 750 | switch (md.error_code) { |
| 751 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 752 | LOGC(DDEV, ALERT) << "UHD: Receive timed out"; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 753 | return ERROR_TIMEOUT; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 754 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 755 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 756 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 757 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 758 | default: |
| 759 | return ERROR_UNHANDLED; |
| 760 | } |
| 761 | } |
| 762 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 763 | // Missing timestamp |
| 764 | if (!md.has_time_spec) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 765 | LOGC(DDEV, ALERT) << "UHD: Received packet missing timestamp"; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 766 | return ERROR_UNRECOVERABLE; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | // Monotonicity check |
Tom Tsou | a93f789 | 2017-03-07 17:54:06 -0800 | [diff] [blame] | 770 | if (md.time_spec < prev_ts) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 771 | LOGC(DDEV, ALERT) << "UHD: Loss of monotonic time"; |
| 772 | LOGC(DDEV, ALERT) << "Current time: " << md.time_spec.get_real_secs() << ", " |
ttsou | 724eb36 | 2012-03-13 02:20:01 +0000 | [diff] [blame] | 773 | << "Previous time: " << prev_ts.get_real_secs(); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 774 | return ERROR_TIMING; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Tom Tsou | a93f789 | 2017-03-07 17:54:06 -0800 | [diff] [blame] | 777 | // Workaround for UHD tick rounding bug |
| 778 | TIMESTAMP ticks = md.time_spec.to_ticks(rx_rate); |
| 779 | if (ticks - prev_ts.to_ticks(rx_rate) == rx_spp - 1) |
| 780 | md.time_spec = uhd::time_spec_t::from_ticks(++ticks, rx_rate); |
| 781 | |
| 782 | prev_ts = md.time_spec; |
| 783 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 784 | return 0; |
| 785 | } |
| 786 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 787 | int uhd_device::readSamples(std::vector<short *> &bufs, int len, bool *overrun, |
Pau Espin Pedrol | f8c0c46 | 2020-03-12 19:08:46 +0100 | [diff] [blame] | 788 | TIMESTAMP timestamp, bool *underrun) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 789 | { |
| 790 | ssize_t rc; |
| 791 | uhd::time_spec_t ts; |
| 792 | uhd::rx_metadata_t metadata; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 793 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 794 | if (bufs.size() != chans) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 795 | LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size(); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 796 | return -1; |
| 797 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 798 | |
Thomas Tsou | cf910dc | 2013-10-25 14:42:00 +0000 | [diff] [blame] | 799 | *overrun = false; |
| 800 | *underrun = false; |
| 801 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 802 | // Shift read time with respect to transmit clock |
| 803 | timestamp += ts_offset; |
| 804 | |
Tom Tsou | d4d3daa | 2015-08-21 19:21:28 -0700 | [diff] [blame] | 805 | ts = uhd::time_spec_t::from_ticks(timestamp, rx_rate); |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 806 | LOGC(DDEV, DEBUG) << "Requested timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 807 | |
| 808 | // Check that timestamp is valid |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 809 | rc = rx_buffers[0]->avail_smpls(timestamp); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 810 | if (rc < 0) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 811 | LOGC(DDEV, ERROR) << rx_buffers[0]->str_code(rc); |
| 812 | LOGC(DDEV, ERROR) << rx_buffers[0]->str_status(timestamp); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | // Receive samples from the usrp until we have enough |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 817 | while (rx_buffers[0]->avail_smpls(timestamp) < len) { |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 818 | thread_enable_cancel(false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 819 | size_t num_smpls = rx_stream->recv(pkt_ptrs, rx_spp, |
| 820 | metadata, 0.1, true); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 821 | thread_enable_cancel(true); |
| 822 | |
Pau Espin Pedrol | f58cd8a | 2018-02-05 13:04:41 +0100 | [diff] [blame] | 823 | // Check for errors |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 824 | rc = check_rx_md_err(metadata, num_smpls); |
| 825 | switch (rc) { |
| 826 | case ERROR_UNRECOVERABLE: |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 827 | LOGC(DDEV, ALERT) << "UHD: Version " << uhd::get_version_string(); |
| 828 | LOGC(DDEV, ALERT) << "UHD: Unrecoverable error, exiting..."; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 829 | exit(-1); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 830 | case ERROR_TIMEOUT: |
| 831 | // Assume stopping condition |
| 832 | return 0; |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 833 | case ERROR_TIMING: |
Thomas Tsou | c283916 | 2014-03-27 13:52:58 -0400 | [diff] [blame] | 834 | restart(); |
kurtis.heimerl | d4be074 | 2011-11-26 03:17:46 +0000 | [diff] [blame] | 835 | case ERROR_UNHANDLED: |
kurtis.heimerl | 513a629 | 2011-11-26 03:18:36 +0000 | [diff] [blame] | 836 | continue; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 837 | } |
| 838 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 839 | ts = metadata.time_spec; |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 840 | LOGC(DDEV, DEBUG) << "Received timestamp = " << ts.get_real_secs(); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 841 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 842 | for (size_t i = 0; i < rx_buffers.size(); i++) { |
| 843 | rc = rx_buffers[i]->write((short *) &pkt_bufs[i].front(), |
| 844 | num_smpls, |
Pau Espin Pedrol | 0a2a40f | 2019-12-19 13:52:57 +0100 | [diff] [blame] | 845 | ts.to_ticks(rx_rate)); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 846 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 847 | // Continue on local overrun, exit on other errors |
| 848 | if ((rc < 0)) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 849 | LOGC(DDEV, ERROR) << rx_buffers[i]->str_code(rc); |
| 850 | LOGC(DDEV, ERROR) << rx_buffers[i]->str_status(timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 851 | if (rc != smpl_buf::ERROR_OVERFLOW) |
| 852 | return 0; |
| 853 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
| 857 | // We have enough samples |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 858 | for (size_t i = 0; i < rx_buffers.size(); i++) { |
| 859 | rc = rx_buffers[i]->read(bufs[i], len, timestamp); |
| 860 | if ((rc < 0) || (rc != len)) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 861 | LOGC(DDEV, ERROR) << rx_buffers[i]->str_code(rc); |
| 862 | LOGC(DDEV, ERROR) << rx_buffers[i]->str_status(timestamp); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 863 | return 0; |
| 864 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | return len; |
| 868 | } |
| 869 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 870 | int uhd_device::writeSamples(std::vector<short *> &bufs, int len, bool *underrun, |
Pau Espin Pedrol | dfc6e5f | 2020-03-12 19:35:33 +0100 | [diff] [blame] | 871 | unsigned long long timestamp) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 872 | { |
| 873 | uhd::tx_metadata_t metadata; |
| 874 | metadata.has_time_spec = true; |
| 875 | metadata.start_of_burst = false; |
| 876 | metadata.end_of_burst = false; |
Tom Tsou | d4d3daa | 2015-08-21 19:21:28 -0700 | [diff] [blame] | 877 | metadata.time_spec = uhd::time_spec_t::from_ticks(timestamp, tx_rate); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 878 | |
Thomas Tsou | cf910dc | 2013-10-25 14:42:00 +0000 | [diff] [blame] | 879 | *underrun = false; |
| 880 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 881 | if (bufs.size() != chans) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 882 | LOGC(DDEV, ALERT) << "Invalid channel combination " << bufs.size(); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 883 | return -1; |
| 884 | } |
| 885 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 886 | // Drop a fixed number of packets (magic value) |
| 887 | if (!aligned) { |
| 888 | drop_cnt++; |
| 889 | |
| 890 | if (drop_cnt == 1) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 891 | LOGC(DDEV, DEBUG) << "Aligning transmitter: stop burst"; |
ttsou | 221f23e | 2012-08-08 00:51:34 +0000 | [diff] [blame] | 892 | *underrun = true; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 893 | metadata.end_of_burst = true; |
| 894 | } else if (drop_cnt < 30) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 895 | LOGC(DDEV, DEBUG) << "Aligning transmitter: packet advance"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 896 | return len; |
| 897 | } else { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 898 | LOGC(DDEV, DEBUG) << "Aligning transmitter: start burst"; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 899 | metadata.start_of_burst = true; |
| 900 | aligned = true; |
| 901 | drop_cnt = 0; |
| 902 | } |
| 903 | } |
| 904 | |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 905 | thread_enable_cancel(false); |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 906 | size_t num_smpls = tx_stream->send(bufs, len, metadata); |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 907 | thread_enable_cancel(true); |
| 908 | |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 909 | if (num_smpls != (unsigned) len) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 910 | LOGC(DDEV, ALERT) << "UHD: Device send timed out"; |
ttsou | b371ed5 | 2012-01-09 18:11:34 +0000 | [diff] [blame] | 911 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 912 | |
| 913 | return num_smpls; |
| 914 | } |
| 915 | |
| 916 | bool uhd_device::updateAlignment(TIMESTAMP timestamp) |
| 917 | { |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 918 | return true; |
| 919 | } |
| 920 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 921 | uhd::tune_request_t uhd_device::select_freq(double freq, size_t chan, bool tx) |
| 922 | { |
| 923 | double rf_spread, rf_freq; |
| 924 | std::vector<double> freqs; |
| 925 | uhd::tune_request_t treq(freq); |
| 926 | |
Alexander Chemeris | 90f7a01 | 2015-04-09 18:55:02 +0300 | [diff] [blame] | 927 | if (dev_type == UMTRX) { |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 928 | if (lo_offset != 0.0) |
| 929 | return uhd::tune_request_t(freq, lo_offset); |
Alexander Chemeris | 90f7a01 | 2015-04-09 18:55:02 +0300 | [diff] [blame] | 930 | |
| 931 | // Don't use DSP tuning, because LMS6002D PLL steps are small enough. |
| 932 | // We end up with DSP tuning just for 2-3Hz, which is meaningless and |
| 933 | // only distort the signal (because cordic is not ideal). |
| 934 | treq.target_freq = freq; |
| 935 | treq.rf_freq_policy = uhd::tune_request_t::POLICY_MANUAL; |
| 936 | treq.rf_freq = freq; |
| 937 | treq.dsp_freq_policy = uhd::tune_request_t::POLICY_MANUAL; |
| 938 | treq.dsp_freq = 0.0; |
Alexander Chemeris | f3b9af6 | 2015-06-20 00:05:51 +0300 | [diff] [blame] | 939 | return treq; |
Alexander Chemeris | 90f7a01 | 2015-04-09 18:55:02 +0300 | [diff] [blame] | 940 | } else if (chans == 1) { |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 941 | if (lo_offset == 0.0) |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 942 | return treq; |
| 943 | |
Harald Welte | 61707e8 | 2018-06-13 23:21:57 +0200 | [diff] [blame] | 944 | return uhd::tune_request_t(freq, lo_offset); |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 945 | } else if ((dev_type != B210) || (chans > 2) || (chan > 1)) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 946 | LOGC(DDEV, ALERT) << chans << " channels unsupported"; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 947 | return treq; |
| 948 | } |
| 949 | |
| 950 | if (tx) |
| 951 | freqs = tx_freqs; |
| 952 | else |
| 953 | freqs = rx_freqs; |
| 954 | |
| 955 | /* Tune directly if other channel isn't tuned */ |
| 956 | if (freqs[!chan] < 10.0) |
| 957 | return treq; |
| 958 | |
| 959 | /* Find center frequency between channels */ |
| 960 | rf_spread = fabs(freqs[!chan] - freq); |
Tom Tsou | f611569 | 2017-06-26 10:13:25 -0700 | [diff] [blame] | 961 | if (rf_spread > dev_param_map.at(dev_key(B210, tx_sps, rx_sps)).mcr) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 962 | LOGC(DDEV, ALERT) << rf_spread << "Hz tuning spread not supported\n"; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 963 | return treq; |
| 964 | } |
| 965 | |
| 966 | rf_freq = (freqs[!chan] + freq) / 2.0f; |
| 967 | |
| 968 | treq.rf_freq_policy = uhd::tune_request_t::POLICY_MANUAL; |
| 969 | treq.target_freq = freq; |
| 970 | treq.rf_freq = rf_freq; |
| 971 | |
| 972 | return treq; |
| 973 | } |
| 974 | |
| 975 | bool uhd_device::set_freq(double freq, size_t chan, bool tx) |
| 976 | { |
| 977 | std::vector<double> freqs; |
| 978 | uhd::tune_result_t tres; |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 979 | std::string str_dir = tx ? "Tx" : "Rx"; |
| 980 | |
| 981 | if (!update_band_from_freq(freq, chan, tx)) |
| 982 | return false; |
| 983 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 984 | uhd::tune_request_t treq = select_freq(freq, chan, tx); |
| 985 | |
| 986 | if (tx) { |
| 987 | tres = usrp_dev->set_tx_freq(treq, chan); |
| 988 | tx_freqs[chan] = usrp_dev->get_tx_freq(chan); |
| 989 | } else { |
| 990 | tres = usrp_dev->set_rx_freq(treq, chan); |
| 991 | rx_freqs[chan] = usrp_dev->get_rx_freq(chan); |
| 992 | } |
Pau Espin Pedrol | 9279e0e | 2019-12-20 23:02:53 +0100 | [diff] [blame] | 993 | LOGCHAN(chan, DDEV, INFO) << "set_freq(" << freq << ", " << str_dir << "): " << tres.to_pp_string() << std::endl; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 994 | |
Thomas Tsou | 8e17df7 | 2014-03-06 14:16:11 -0500 | [diff] [blame] | 995 | if ((chans == 1) || ((chans == 2) && dev_type == UMTRX)) |
| 996 | return true; |
| 997 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 998 | /* Manual RF policy means we intentionally tuned with a baseband |
| 999 | * offset for dual-channel purposes. Now retune the other channel |
| 1000 | * with the opposite corresponding frequency offset |
| 1001 | */ |
| 1002 | if (treq.rf_freq_policy == uhd::tune_request_t::POLICY_MANUAL) { |
| 1003 | if (tx) { |
| 1004 | treq = select_freq(tx_freqs[!chan], !chan, true); |
| 1005 | tres = usrp_dev->set_tx_freq(treq, !chan); |
| 1006 | tx_freqs[!chan] = usrp_dev->get_tx_freq(!chan); |
| 1007 | } else { |
| 1008 | treq = select_freq(rx_freqs[!chan], !chan, false); |
| 1009 | tres = usrp_dev->set_rx_freq(treq, !chan); |
| 1010 | rx_freqs[!chan] = usrp_dev->get_rx_freq(!chan); |
| 1011 | |
| 1012 | } |
Pau Espin Pedrol | 9279e0e | 2019-12-20 23:02:53 +0100 | [diff] [blame] | 1013 | LOGCHAN(chan, DDEV, INFO) << "set_freq(" << freq << ", " << str_dir << "): " << tres.to_pp_string() << std::endl; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return true; |
| 1017 | } |
| 1018 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1019 | bool uhd_device::setTxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1020 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1021 | if (chan >= tx_freqs.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1022 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1023 | return false; |
| 1024 | } |
| 1025 | |
Eric | c0f78a3 | 2023-05-12 13:00:14 +0200 | [diff] [blame^] | 1026 | return set_freq(wFreq, chan, true); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1029 | bool uhd_device::setRxFreq(double wFreq, size_t chan) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1030 | { |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1031 | if (chan >= rx_freqs.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1032 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1033 | return false; |
| 1034 | } |
Pau Espin Pedrol | 069f5cd | 2021-09-21 13:50:38 +0200 | [diff] [blame] | 1035 | |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1036 | return set_freq(wFreq, chan, false); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1039 | double uhd_device::getTxFreq(size_t chan) |
| 1040 | { |
| 1041 | if (chan >= tx_freqs.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1042 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1043 | return 0.0; |
| 1044 | } |
| 1045 | |
| 1046 | return tx_freqs[chan]; |
| 1047 | } |
| 1048 | |
| 1049 | double uhd_device::getRxFreq(size_t chan) |
| 1050 | { |
| 1051 | if (chan >= rx_freqs.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1052 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Thomas Tsou | 204a9f1 | 2013-10-29 18:34:16 -0400 | [diff] [blame] | 1053 | return 0.0; |
| 1054 | } |
| 1055 | |
| 1056 | return rx_freqs[chan]; |
| 1057 | } |
| 1058 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1059 | bool uhd_device::setRxAntenna(const std::string &ant, size_t chan) |
| 1060 | { |
| 1061 | std::vector<std::string> avail; |
| 1062 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1063 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | |
Vadim Yanitskiy | 68d8db4 | 2020-06-29 17:39:35 +0700 | [diff] [blame] | 1067 | /* UHD may throw a LookupError/IndexError here (see OS#4636) */ |
| 1068 | try { |
| 1069 | avail = usrp_dev->get_rx_antennas(chan); |
| 1070 | } catch (const uhd::index_error &e) { |
| 1071 | LOGC(DDEV, ALERT) << "UHD Error: " << e.what(); |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1075 | if (std::find(avail.begin(), avail.end(), ant) == avail.end()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1076 | LOGC(DDEV, ALERT) << "Requested non-existent Rx antenna " << ant << " on channel " << chan; |
| 1077 | LOGC(DDEV, INFO) << "Available Rx antennas: "; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1078 | for (std::vector<std::string>::const_iterator i = avail.begin(); i != avail.end(); ++i) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1079 | LOGC(DDEV, INFO) << "- '" << *i << "'"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1080 | return false; |
| 1081 | } |
| 1082 | usrp_dev->set_rx_antenna(ant, chan); |
| 1083 | rx_paths[chan] = usrp_dev->get_rx_antenna(chan); |
| 1084 | |
| 1085 | if (ant != rx_paths[chan]) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1086 | LOGC(DDEV, ALERT) << "Failed setting antenna " << ant << " on channel " << chan << ", got instead " << rx_paths[chan]; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1087 | return false; |
| 1088 | } |
| 1089 | |
| 1090 | return true; |
| 1091 | } |
| 1092 | |
| 1093 | std::string uhd_device::getRxAntenna(size_t chan) |
| 1094 | { |
| 1095 | if (chan >= rx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1096 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1097 | return ""; |
| 1098 | } |
| 1099 | return usrp_dev->get_rx_antenna(chan); |
| 1100 | } |
| 1101 | |
| 1102 | bool uhd_device::setTxAntenna(const std::string &ant, size_t chan) |
| 1103 | { |
| 1104 | std::vector<std::string> avail; |
| 1105 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1106 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1107 | return false; |
| 1108 | } |
| 1109 | |
Vadim Yanitskiy | 68d8db4 | 2020-06-29 17:39:35 +0700 | [diff] [blame] | 1110 | /* UHD may throw a LookupError/IndexError here (see OS#4636) */ |
| 1111 | try { |
| 1112 | avail = usrp_dev->get_tx_antennas(chan); |
| 1113 | } catch (const uhd::index_error &e) { |
| 1114 | LOGC(DDEV, ALERT) << "UHD Error: " << e.what(); |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1118 | if (std::find(avail.begin(), avail.end(), ant) == avail.end()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1119 | LOGC(DDEV, ALERT) << "Requested non-existent Tx antenna " << ant << " on channel " << chan; |
| 1120 | LOGC(DDEV, INFO) << "Available Tx antennas: "; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1121 | for (std::vector<std::string>::const_iterator i = avail.begin(); i != avail.end(); ++i) |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1122 | LOGC(DDEV, INFO) << "- '" << *i << "'"; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1123 | return false; |
| 1124 | } |
| 1125 | usrp_dev->set_tx_antenna(ant, chan); |
| 1126 | tx_paths[chan] = usrp_dev->get_tx_antenna(chan); |
| 1127 | |
| 1128 | if (ant != tx_paths[chan]) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1129 | LOGC(DDEV, ALERT) << "Failed setting antenna " << ant << " on channel " << chan << ", got instead " << tx_paths[chan]; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | std::string uhd_device::getTxAntenna(size_t chan) |
| 1137 | { |
| 1138 | if (chan >= tx_paths.size()) { |
Harald Welte | 5cc8858 | 2018-08-17 19:55:38 +0200 | [diff] [blame] | 1139 | LOGC(DDEV, ALERT) << "Requested non-existent channel " << chan; |
Pau Espin Pedrol | 77ce99a | 2018-02-05 13:05:06 +0100 | [diff] [blame] | 1140 | return ""; |
| 1141 | } |
| 1142 | return usrp_dev->get_tx_antenna(chan); |
| 1143 | } |
| 1144 | |
Pau Espin Pedrol | 0fc20d1 | 2018-04-24 17:48:52 +0200 | [diff] [blame] | 1145 | bool uhd_device::requiresRadioAlign() |
| 1146 | { |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
Pau Espin Pedrol | e564f0f | 2018-04-24 18:43:51 +0200 | [diff] [blame] | 1150 | GSM::Time uhd_device::minLatency() { |
| 1151 | /* Empirical data from a handful of |
| 1152 | relatively recent machines shows that the B100 will underrun when |
| 1153 | the transmit threshold is reduced to a time of 6 and a half frames, |
| 1154 | so we set a minimum 7 frame threshold. */ |
| 1155 | return GSM::Time(6,7); |
| 1156 | } |
| 1157 | |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 1158 | /* |
| 1159 | * Only allow sampling the Rx path lower than Tx and not vice-versa. |
| 1160 | * Using Tx with 4 SPS and Rx at 1 SPS is the only allowed mixed |
| 1161 | * combination. |
| 1162 | */ |
| 1163 | TIMESTAMP uhd_device::initialWriteTimestamp() |
| 1164 | { |
Tom Tsou | 7676427 | 2016-06-24 14:25:39 -0700 | [diff] [blame] | 1165 | if ((iface == MULTI_ARFCN) || (rx_sps == tx_sps)) |
Tom Tsou | 5cd70dc | 2016-03-06 01:28:40 -0800 | [diff] [blame] | 1166 | return ts_initial; |
| 1167 | else |
| 1168 | return ts_initial * tx_sps; |
| 1169 | } |
| 1170 | |
| 1171 | TIMESTAMP uhd_device::initialReadTimestamp() |
| 1172 | { |
| 1173 | return ts_initial; |
| 1174 | } |
| 1175 | |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame] | 1176 | double uhd_device::fullScaleInputValue() |
| 1177 | { |
Alexander Chemeris | cbfef6e | 2016-02-05 00:52:49 -0800 | [diff] [blame] | 1178 | if (dev_type == LIMESDR) |
ignasj | 28d8081 | 2017-06-13 23:37:46 +0300 | [diff] [blame] | 1179 | return (double) SHRT_MAX * LIMESDR_TX_AMPL; |
Alexander Chemeris | 88bbf1a | 2015-03-25 14:22:37 +0300 | [diff] [blame] | 1180 | if (dev_type == UMTRX) |
| 1181 | return (double) SHRT_MAX * UMTRX_TX_AMPL; |
| 1182 | else |
| 1183 | return (double) SHRT_MAX * USRP_TX_AMPL; |
| 1184 | } |
| 1185 | |
| 1186 | double uhd_device::fullScaleOutputValue() |
| 1187 | { |
| 1188 | return (double) SHRT_MAX; |
| 1189 | } |
| 1190 | |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1191 | bool uhd_device::recv_async_msg() |
| 1192 | { |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1193 | uhd::async_metadata_t md; |
Tom Tsou | eb54bdd | 2014-11-25 16:06:32 -0800 | [diff] [blame] | 1194 | |
| 1195 | thread_enable_cancel(false); |
| 1196 | bool rc = usrp_dev->get_device()->recv_async_msg(md); |
| 1197 | thread_enable_cancel(true); |
| 1198 | if (!rc) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1199 | return false; |
| 1200 | |
| 1201 | // Assume that any error requires resynchronization |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1202 | if (md.event_code != uhd::async_metadata_t::EVENT_CODE_BURST_ACK) { |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1203 | aligned = false; |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1204 | |
| 1205 | if ((md.event_code != uhd::async_metadata_t::EVENT_CODE_UNDERFLOW) && |
| 1206 | (md.event_code != uhd::async_metadata_t::EVENT_CODE_TIME_ERROR)) { |
Pau Espin Pedrol | ac927b2 | 2019-04-30 18:19:54 +0200 | [diff] [blame] | 1207 | LOGC(DDEV, ERROR) << str_code(md); |
ttsou | 60dc4c9 | 2012-08-08 23:30:23 +0000 | [diff] [blame] | 1208 | } |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | std::string uhd_device::str_code(uhd::rx_metadata_t metadata) |
| 1215 | { |
| 1216 | std::ostringstream ost("UHD: "); |
| 1217 | |
| 1218 | switch (metadata.error_code) { |
| 1219 | case uhd::rx_metadata_t::ERROR_CODE_NONE: |
| 1220 | ost << "No error"; |
| 1221 | break; |
| 1222 | case uhd::rx_metadata_t::ERROR_CODE_TIMEOUT: |
| 1223 | ost << "No packet received, implementation timed-out"; |
| 1224 | break; |
| 1225 | case uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND: |
| 1226 | ost << "A stream command was issued in the past"; |
| 1227 | break; |
| 1228 | case uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN: |
| 1229 | ost << "Expected another stream command"; |
| 1230 | break; |
| 1231 | case uhd::rx_metadata_t::ERROR_CODE_OVERFLOW: |
| 1232 | ost << "An internal receive buffer has filled"; |
| 1233 | break; |
Thomas Tsou | 3ebc772 | 2014-02-13 15:09:00 -0500 | [diff] [blame] | 1234 | case uhd::rx_metadata_t::ERROR_CODE_ALIGNMENT: |
| 1235 | ost << "Multi-channel alignment failed"; |
| 1236 | break; |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1237 | case uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET: |
| 1238 | ost << "The packet could not be parsed"; |
| 1239 | break; |
| 1240 | default: |
| 1241 | ost << "Unknown error " << metadata.error_code; |
| 1242 | } |
| 1243 | |
| 1244 | if (metadata.has_time_spec) |
| 1245 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 1246 | |
| 1247 | return ost.str(); |
| 1248 | } |
| 1249 | |
| 1250 | std::string uhd_device::str_code(uhd::async_metadata_t metadata) |
| 1251 | { |
| 1252 | std::ostringstream ost("UHD: "); |
| 1253 | |
| 1254 | switch (metadata.event_code) { |
| 1255 | case uhd::async_metadata_t::EVENT_CODE_BURST_ACK: |
| 1256 | ost << "A packet was successfully transmitted"; |
| 1257 | break; |
| 1258 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW: |
| 1259 | ost << "An internal send buffer has emptied"; |
| 1260 | break; |
| 1261 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR: |
| 1262 | ost << "Packet loss between host and device"; |
| 1263 | break; |
| 1264 | case uhd::async_metadata_t::EVENT_CODE_TIME_ERROR: |
| 1265 | ost << "Packet time was too late or too early"; |
| 1266 | break; |
| 1267 | case uhd::async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET: |
| 1268 | ost << "Underflow occurred inside a packet"; |
| 1269 | break; |
| 1270 | case uhd::async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST: |
| 1271 | ost << "Packet loss within a burst"; |
| 1272 | break; |
| 1273 | default: |
| 1274 | ost << "Unknown error " << metadata.event_code; |
| 1275 | } |
| 1276 | |
| 1277 | if (metadata.has_time_spec) |
| 1278 | ost << " at " << metadata.time_spec.get_real_secs() << " sec."; |
| 1279 | |
| 1280 | return ost.str(); |
| 1281 | } |
| 1282 | |
Eric Wild | 1e17c4f | 2020-03-24 17:19:27 +0100 | [diff] [blame] | 1283 | #ifndef IPCMAGIC |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 1284 | RadioDevice *RadioDevice::make(InterfaceType type, const struct trx_cfg *cfg) |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1285 | { |
Eric | 19e134a | 2023-05-10 23:50:38 +0200 | [diff] [blame] | 1286 | return new uhd_device(type, cfg); |
kurtis.heimerl | 965e757 | 2011-11-26 03:16:54 +0000 | [diff] [blame] | 1287 | } |
Eric Wild | 1e17c4f | 2020-03-24 17:19:27 +0100 | [diff] [blame] | 1288 | #endif |