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