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