Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 sysmocom - s.f.m.c. GmbH |
| 3 | * |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Affero General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Affero General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Affero General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include "Logger.h" |
| 22 | #include "Threads.h" |
| 23 | #include "LMSDevice.h" |
| 24 | |
| 25 | #include <lime/LimeSuite.h> |
| 26 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
| 28 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
| 33 | using namespace std; |
| 34 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 35 | constexpr double LMSDevice::masterClockRate; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 36 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 37 | #define MAX_ANTENNA_LIST_SIZE 10 |
| 38 | #define LMS_SAMPLE_RATE GSMRATE*32 |
| 39 | #define GSM_CARRIER_BW 270000.0 /* 270kHz */ |
| 40 | #define LMS_MIN_BW_SUPPORTED 2.5e6 /* 2.5mHz, minimum supported by LMS */ |
| 41 | #define LMS_CALIBRATE_BW_HZ OSMO_MAX(GSM_CARRIER_BW, LMS_MIN_BW_SUPPORTED) |
| 42 | |
Harald Welte | 105a61e | 2018-06-13 21:55:09 +0200 | [diff] [blame] | 43 | LMSDevice::LMSDevice(size_t sps, size_t chans, |
| 44 | const std::vector<std::string>& tx_paths, |
| 45 | const std::vector<std::string>& rx_paths): |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 46 | m_lms_dev(NULL), sps(sps), chans(chans) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 47 | { |
| 48 | LOG(INFO) << "creating LMS device..."; |
| 49 | |
Harald Welte | 105a61e | 2018-06-13 21:55:09 +0200 | [diff] [blame] | 50 | this->tx_paths = tx_paths; |
| 51 | this->rx_paths = rx_paths; |
| 52 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 53 | m_lms_stream_rx.resize(chans); |
| 54 | m_lms_stream_tx.resize(chans); |
| 55 | |
| 56 | m_last_rx_underruns.resize(chans, 0); |
| 57 | m_last_rx_overruns.resize(chans, 0); |
| 58 | m_last_tx_underruns.resize(chans, 0); |
| 59 | m_last_tx_overruns.resize(chans, 0); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static void lms_log_callback(int lvl, const char *msg) |
| 63 | { |
| 64 | /* map lime specific log levels */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 65 | static const int lvl_map[5] = { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 66 | [0] = LOGL_FATAL, |
| 67 | [1] = LOGL_ERROR, |
| 68 | [2] = LOGL_NOTICE, |
| 69 | [3] = LOGL_INFO, |
| 70 | [4] = LOGL_DEBUG, |
| 71 | }; |
| 72 | /* protect against future higher log level values (lower importance) */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 73 | if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map)) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 74 | lvl = ARRAY_SIZE(lvl_map)-1; |
| 75 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 76 | LOGLV(DLMS, lvl) << msg; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 79 | static void thread_enable_cancel(bool cancel) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 80 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 81 | cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) : |
| 82 | pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
| 83 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 84 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 85 | static void print_range(const char* name, lms_range_t *range) |
| 86 | { |
| 87 | LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max |
| 88 | << " Step=" << range->step; |
| 89 | } |
| 90 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 91 | int LMSDevice::open(const std::string &args, int ref, bool swap_channels) |
| 92 | { |
| 93 | //lms_info_str_t dev_str; |
| 94 | lms_info_str_t* info_list; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 95 | lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr; |
| 96 | float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 97 | uint16_t dac_val; |
| 98 | unsigned int i, n; |
| 99 | int rc; |
| 100 | |
| 101 | LOG(INFO) << "Opening LMS device.."; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 102 | |
| 103 | LMS_RegisterLogHandler(&lms_log_callback); |
| 104 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 105 | if ((n = LMS_GetDeviceList(NULL)) < 0) |
| 106 | LOG(ERROR) << "LMS_GetDeviceList(NULL) failed"; |
| 107 | LOG(DEBUG) << "Devices found: " << n; |
| 108 | if (n < 1) |
| 109 | return -1; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 110 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 111 | info_list = new lms_info_str_t[n]; |
| 112 | |
| 113 | if (LMS_GetDeviceList(info_list) < 0) //Populate device list |
| 114 | LOG(ERROR) << "LMS_GetDeviceList(info_list) failed"; |
| 115 | |
| 116 | for (i = 0; i < n; i++) //print device list |
| 117 | LOG(DEBUG) << "Device [" << i << "]: " << info_list[i]; |
| 118 | |
| 119 | rc = LMS_Open(&m_lms_dev, info_list[0], NULL); |
| 120 | if (rc != 0) { |
| 121 | LOG(ERROR) << "LMS_GetDeviceList() failed)"; |
| 122 | delete [] info_list; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | delete [] info_list; |
| 127 | |
Harald Welte | 9cb4f27 | 2018-04-28 21:38:58 +0200 | [diff] [blame] | 128 | LOG(INFO) << "Init LMS device"; |
| 129 | if (LMS_Init(m_lms_dev) != 0) { |
| 130 | LOG(ERROR) << "LMS_Init() failed"; |
| 131 | return -1; |
| 132 | } |
| 133 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 134 | if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr)) |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 135 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 136 | print_range("Sample Rate", &range_sr); |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 137 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 138 | LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps; |
| 139 | if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 140 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 141 | |
Harald Welte | a438114 | 2018-04-28 21:41:07 +0200 | [diff] [blame] | 142 | if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf)) |
| 143 | goto out_close; |
| 144 | LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 145 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 146 | /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */ |
Zydrunas Tamosevicius | 43f3678 | 2018-06-12 00:27:09 +0200 | [diff] [blame] | 147 | ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * sps); /* time * sample_rate */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 148 | |
| 149 | switch (ref) { |
| 150 | case REF_INTERNAL: |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 151 | LOG(DEBUG) << "Setting Internal clock reference"; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 152 | /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */ |
| 153 | if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0) |
| 154 | goto out_close; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 155 | LOG(DEBUG) << "Setting VCTCXO to " << dac_val; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 156 | if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0) |
| 157 | goto out_close; |
| 158 | break; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 159 | case REF_EXTERNAL: |
Harald Welte | a5054b3 | 2018-04-28 21:41:34 +0200 | [diff] [blame] | 160 | LOG(DEBUG) << "Setting External clock reference to " << 10000000.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 161 | /* Assume an external 10 MHz reference clock */ |
| 162 | if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0) |
| 163 | goto out_close; |
| 164 | break; |
| 165 | default: |
| 166 | LOG(ALERT) << "Invalid reference type"; |
| 167 | goto out_close; |
| 168 | } |
| 169 | |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 170 | if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx)) |
| 171 | goto out_close; |
| 172 | print_range("LPFBWRange Rx", &range_lpfbw_rx); |
| 173 | if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx)) |
| 174 | goto out_close; |
| 175 | print_range("LPFBWRange Tx", &range_lpfbw_tx); |
| 176 | lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max); |
| 177 | lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max); |
| 178 | |
| 179 | LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx; |
| 180 | |
Harald Welte | cfb9dac | 2018-06-13 21:56:24 +0200 | [diff] [blame^] | 181 | if (!set_antennas()) { |
| 182 | LOG(ALERT) << "LMS antenna setting failed"; |
| 183 | return -1; |
| 184 | } |
| 185 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 186 | /* Perform Rx and Tx calibration */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 187 | for (i=0; i<chans; i++) { |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 188 | LOG(INFO) << "Setting LPFBW chan " << i; |
| 189 | if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0) |
Harald Welte | ea8be22 | 2018-04-28 21:52:57 +0200 | [diff] [blame] | 190 | goto out_close; |
Pau Espin Pedrol | 6493dc8 | 2018-05-29 19:00:30 +0200 | [diff] [blame] | 191 | if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0) |
Harald Welte | ea8be22 | 2018-04-28 21:52:57 +0200 | [diff] [blame] | 192 | goto out_close; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 193 | LOG(INFO) << "Calibrating chan " << i; |
| 194 | if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 195 | goto out_close; |
| 196 | if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0) |
| 197 | goto out_close; |
| 198 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 199 | |
| 200 | samplesRead = 0; |
| 201 | samplesWritten = 0; |
| 202 | started = false; |
| 203 | |
| 204 | return NORMAL; |
| 205 | |
| 206 | out_close: |
| 207 | LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage(); |
| 208 | LMS_Close(m_lms_dev); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | bool LMSDevice::start() |
| 213 | { |
| 214 | LOG(INFO) << "starting LMS..."; |
| 215 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 216 | unsigned int i; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 217 | |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 218 | /* configure the channels/streams */ |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 219 | for (i=0; i<chans; i++) { |
| 220 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0) |
| 221 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 222 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 223 | if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0) |
| 224 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 225 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 226 | // Set gains to midpoint |
| 227 | setTxGain((minTxGain() + maxTxGain()) / 2, i); |
| 228 | setRxGain((minRxGain() + maxRxGain()) / 2, i); |
| 229 | |
| 230 | m_lms_stream_rx[i] = {}; |
| 231 | m_lms_stream_rx[i].isTx = false; |
| 232 | m_lms_stream_rx[i].channel = i; |
| 233 | m_lms_stream_rx[i].fifoSize = 1024 * 1024; |
| 234 | m_lms_stream_rx[i].throughputVsLatency = 0.3; |
| 235 | m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 236 | |
| 237 | m_lms_stream_tx[i] = {}; |
| 238 | m_lms_stream_tx[i].isTx = true; |
| 239 | m_lms_stream_tx[i].channel = i; |
| 240 | m_lms_stream_tx[i].fifoSize = 1024 * 1024; |
| 241 | m_lms_stream_tx[i].throughputVsLatency = 0.3; |
| 242 | m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16; |
| 243 | |
| 244 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0) |
| 245 | return false; |
| 246 | |
| 247 | if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0) |
| 248 | return false; |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 249 | } |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 250 | |
Zydrunas Tamosevicius | 0494d05 | 2018-06-12 00:29:16 +0200 | [diff] [blame] | 251 | /* now start the streams in a second loop, as we can no longer call |
| 252 | * LMS_SetupStream() after LMS_StartStream() of the first stream */ |
| 253 | for (i = 0; i < chans; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 254 | if (LMS_StartStream(&m_lms_stream_rx[i]) < 0) |
| 255 | return false; |
| 256 | |
| 257 | if (LMS_StartStream(&m_lms_stream_tx[i]) < 0) |
| 258 | return false; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 261 | flush_recv(10); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 262 | |
| 263 | started = true; |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | bool LMSDevice::stop() |
| 268 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 269 | unsigned int i; |
| 270 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 271 | if (!started) |
| 272 | return true; |
| 273 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 274 | for (i=0; i<chans; i++) { |
| 275 | LMS_StopStream(&m_lms_stream_tx[i]); |
| 276 | LMS_StopStream(&m_lms_stream_rx[i]); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 277 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 278 | LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false); |
| 279 | LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false); |
| 280 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | double LMSDevice::maxTxGain() |
| 286 | { |
Pau Espin Pedrol | 587916e | 2018-05-08 18:46:28 +0200 | [diff] [blame] | 287 | return 73.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | double LMSDevice::minTxGain() |
| 291 | { |
| 292 | return 0.0; |
| 293 | } |
| 294 | |
| 295 | double LMSDevice::maxRxGain() |
| 296 | { |
Pau Espin Pedrol | 587916e | 2018-05-08 18:46:28 +0200 | [diff] [blame] | 297 | return 73.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | double LMSDevice::minRxGain() |
| 301 | { |
| 302 | return 0.0; |
| 303 | } |
| 304 | |
| 305 | double LMSDevice::setTxGain(double dB, size_t chan) |
| 306 | { |
| 307 | if (chan) { |
| 308 | LOG(ALERT) << "Invalid channel " << chan; |
| 309 | return 0.0; |
| 310 | } |
| 311 | |
| 312 | if (dB > maxTxGain()) |
| 313 | dB = maxTxGain(); |
| 314 | if (dB < minTxGain()) |
| 315 | dB = minTxGain(); |
| 316 | |
| 317 | LOG(NOTICE) << "Setting TX gain to " << dB << " dB."; |
| 318 | |
| 319 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0) |
| 320 | LOG(ERR) << "Error setting TX gain"; |
| 321 | |
| 322 | return dB; |
| 323 | } |
| 324 | |
| 325 | double LMSDevice::setRxGain(double dB, size_t chan) |
| 326 | { |
| 327 | if (chan) { |
| 328 | LOG(ALERT) << "Invalid channel " << chan; |
| 329 | return 0.0; |
| 330 | } |
| 331 | |
Zydrunas Tamosevicius | ff44185 | 2018-06-12 00:35:27 +0200 | [diff] [blame] | 332 | dB = 34.0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 333 | |
| 334 | if (dB > maxRxGain()) |
| 335 | dB = maxRxGain(); |
| 336 | if (dB < minRxGain()) |
| 337 | dB = minRxGain(); |
| 338 | |
| 339 | LOG(NOTICE) << "Setting RX gain to " << dB << " dB."; |
| 340 | |
| 341 | if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0) |
| 342 | LOG(ERR) << "Error setting RX gain"; |
| 343 | |
| 344 | return dB; |
| 345 | } |
| 346 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 347 | int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 348 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 349 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 350 | const char* c_name = name.c_str(); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 351 | int num_names; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 352 | int i; |
| 353 | |
| 354 | num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 355 | for (i = 0; i < num_names; i++) { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 356 | if (!strcmp(c_name, name_list[i])) |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 357 | return i; |
| 358 | } |
| 359 | return -1; |
| 360 | } |
| 361 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 362 | bool LMSDevice::flush_recv(size_t num_pkts) |
| 363 | { |
| 364 | #define CHUNK 625 |
| 365 | int len = CHUNK * sps; |
| 366 | short *buffer = new short[len * 2]; |
| 367 | int rc; |
| 368 | lms_stream_meta_t rx_metadata = {}; |
| 369 | rx_metadata.flushPartialPacket = false; |
| 370 | rx_metadata.waitForTimestamp = false; |
| 371 | |
| 372 | ts_initial = 0; |
| 373 | |
| 374 | while (!ts_initial || (num_pkts-- > 0)) { |
| 375 | rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100); |
| 376 | LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp; |
| 377 | if (rc != len) { |
| 378 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 379 | delete[] buffer; |
| 380 | return false; |
| 381 | } |
| 382 | |
Harald Welte | 68f0541 | 2018-04-28 21:58:03 +0200 | [diff] [blame] | 383 | ts_initial = rx_metadata.timestamp + len; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | LOG(INFO) << "Initial timestamp " << ts_initial << std::endl; |
| 387 | delete[] buffer; |
| 388 | return true; |
| 389 | } |
| 390 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 391 | bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan) |
| 392 | { |
| 393 | int idx; |
| 394 | |
| 395 | if (chan >= rx_paths.size()) { |
| 396 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 397 | return false; |
| 398 | } |
| 399 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 400 | idx = get_ant_idx(ant, LMS_CH_RX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 401 | if (idx < 0) { |
| 402 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) { |
| 407 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 408 | } |
| 409 | |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | std::string LMSDevice::getRxAntenna(size_t chan) |
| 414 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 415 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
| 416 | int idx; |
| 417 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 418 | if (chan >= rx_paths.size()) { |
| 419 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 420 | return ""; |
| 421 | } |
| 422 | |
| 423 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan); |
| 424 | if (idx < 0) { |
| 425 | LOG(ALERT) << "Error getting Rx Antenna"; |
| 426 | return ""; |
| 427 | } |
| 428 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 429 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 430 | LOG(ALERT) << "Error getting Rx Antenna List"; |
| 431 | return ""; |
| 432 | } |
| 433 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 434 | return name_list[idx]; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan) |
| 438 | { |
| 439 | int idx; |
| 440 | |
| 441 | if (chan >= tx_paths.size()) { |
| 442 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 443 | return false; |
| 444 | } |
| 445 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 446 | idx = get_ant_idx(ant, LMS_CH_TX, chan); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 447 | if (idx < 0) { |
| 448 | LOG(ALERT) << "Invalid Rx Antenna"; |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) { |
| 453 | LOG(ALERT) << "Unable to set Rx Antenna"; |
| 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | std::string LMSDevice::getTxAntenna(size_t chan) |
| 460 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 461 | lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 462 | int idx; |
| 463 | |
| 464 | if (chan >= tx_paths.size()) { |
| 465 | LOG(ALERT) << "Requested non-existent channel " << chan; |
| 466 | return ""; |
| 467 | } |
| 468 | |
| 469 | idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan); |
| 470 | if (idx < 0) { |
| 471 | LOG(ALERT) << "Error getting Tx Antenna"; |
| 472 | return ""; |
| 473 | } |
| 474 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 475 | if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 476 | LOG(ALERT) << "Error getting Tx Antenna List"; |
| 477 | return ""; |
| 478 | } |
| 479 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 480 | return name_list[idx]; |
| 481 | } |
| 482 | |
| 483 | bool LMSDevice::requiresRadioAlign() |
| 484 | { |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | GSM::Time LMSDevice::minLatency() { |
| 489 | /* Empirical data from a handful of |
| 490 | relatively recent machines shows that the B100 will underrun when |
| 491 | the transmit threshold is reduced to a time of 6 and a half frames, |
| 492 | so we set a minimum 7 frame threshold. */ |
| 493 | return GSM::Time(6,7); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // NOTE: Assumes sequential reads |
| 497 | int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun, |
| 498 | TIMESTAMP timestamp, bool * underrun, unsigned *RSSI) |
| 499 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 500 | int rc = 0; |
| 501 | unsigned int i; |
| 502 | lms_stream_status_t status; |
| 503 | lms_stream_meta_t rx_metadata = {}; |
| 504 | rx_metadata.flushPartialPacket = false; |
| 505 | rx_metadata.waitForTimestamp = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 506 | rx_metadata.timestamp = 0; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 507 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 508 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 509 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 510 | return -1; |
| 511 | } |
| 512 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 513 | *overrun = false; |
| 514 | *underrun = false; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 515 | for (i = 0; i<chans; i++) { |
| 516 | thread_enable_cancel(false); |
| 517 | rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100); |
Harald Welte | 7902486 | 2018-04-28 22:13:31 +0200 | [diff] [blame] | 518 | if (timestamp != (TIMESTAMP)rx_metadata.timestamp) |
| 519 | LOG(ALERT) << "chan "<< i << " recv buffer of len " << rc << " expect " << std::hex << timestamp << " got " << std::hex << (TIMESTAMP)rx_metadata.timestamp << " (" << std::hex << rx_metadata.timestamp <<") diff=" << rx_metadata.timestamp - timestamp; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 520 | if (rc != len) { |
| 521 | LOG(ALERT) << "LMS: Device receive timed out"; |
| 522 | } |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 523 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 524 | if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) { |
| 525 | if (status.underrun > m_last_rx_underruns[i]) |
| 526 | *underrun = true; |
| 527 | m_last_rx_underruns[i] = status.underrun; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 528 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 529 | if (status.overrun > m_last_rx_overruns[i]) |
| 530 | *overrun = true; |
| 531 | m_last_rx_overruns[i] = status.overrun; |
| 532 | } |
| 533 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | samplesRead += rc; |
| 537 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 538 | if (((TIMESTAMP) rx_metadata.timestamp) < timestamp) |
| 539 | rc = 0; |
| 540 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 541 | return rc; |
| 542 | } |
| 543 | |
| 544 | int LMSDevice::writeSamples(std::vector < short *>&bufs, int len, |
| 545 | bool * underrun, unsigned long long timestamp, |
| 546 | bool isControl) |
| 547 | { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 548 | int rc; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 549 | unsigned int i; |
| 550 | lms_stream_status_t status; |
| 551 | lms_stream_meta_t tx_metadata = {}; |
| 552 | tx_metadata.flushPartialPacket = false; |
| 553 | tx_metadata.waitForTimestamp = true; |
Zydrunas Tamosevicius | 43f3678 | 2018-06-12 00:27:09 +0200 | [diff] [blame] | 554 | tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */ |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 555 | |
| 556 | if (isControl) { |
| 557 | LOG(ERR) << "Control packets not supported"; |
| 558 | return 0; |
| 559 | } |
| 560 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 561 | if (bufs.size() != chans) { |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 562 | LOG(ALERT) << "Invalid channel combination " << bufs.size(); |
| 563 | return -1; |
| 564 | } |
| 565 | |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 566 | *underrun = false; |
| 567 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 568 | for (i = 0; i<chans; i++) { |
Zydrunas Tamosevicius | fd268b6 | 2018-06-12 00:27:53 +0200 | [diff] [blame] | 569 | LOG(DEBUG) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp; |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 570 | thread_enable_cancel(false); |
| 571 | rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100); |
| 572 | if (rc != len) { |
| 573 | LOG(ALERT) << "LMS: Device send timed out"; |
| 574 | } |
| 575 | |
| 576 | if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) { |
| 577 | if (status.underrun > m_last_tx_underruns[i]) |
| 578 | *underrun = true; |
| 579 | m_last_tx_underruns[i] = status.underrun; |
| 580 | } |
| 581 | thread_enable_cancel(true); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | samplesWritten += rc; |
| 585 | |
| 586 | return rc; |
| 587 | } |
| 588 | |
| 589 | bool LMSDevice::updateAlignment(TIMESTAMP timestamp) |
| 590 | { |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 591 | return true; |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | bool LMSDevice::setTxFreq(double wFreq, size_t chan) |
| 595 | { |
| 596 | |
| 597 | if (chan) { |
| 598 | LOG(ALERT) << "Invalid channel " << chan; |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) { |
| 603 | LOG(ALERT) << "set Tx: " << wFreq << " failed!"; |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | bool LMSDevice::setRxFreq(double wFreq, size_t chan) |
| 611 | { |
| 612 | if (chan) { |
| 613 | LOG(ALERT) << "Invalid channel " << chan; |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) { |
| 618 | LOG(ALERT) << "set Rx: " << wFreq << " failed!"; |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | return true; |
| 623 | } |
| 624 | |
| 625 | RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps, |
| 626 | InterfaceType iface, size_t chans, double offset, |
| 627 | const std::vector < std::string > &tx_paths, |
| 628 | const std::vector < std::string > &rx_paths) |
| 629 | { |
Harald Welte | 105a61e | 2018-06-13 21:55:09 +0200 | [diff] [blame] | 630 | return new LMSDevice(tx_sps, chans, tx_paths, rx_paths); |
Harald Welte | 940738e | 2018-03-07 07:50:57 +0100 | [diff] [blame] | 631 | } |