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