blob: 9cfa96dcc7aded651b79f61571fd844b2ef64f0b [file] [log] [blame]
Harald Welte940738e2018-03-07 07:50:57 +01001/*
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 Pedrolc7a0bf12018-04-25 12:17:10 +020027#include <osmocom/core/utils.h>
28
Harald Welte940738e2018-03-07 07:50:57 +010029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33using namespace std;
34
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020035constexpr double LMSDevice::masterClockRate;
Harald Welte940738e2018-03-07 07:50:57 +010036
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020037#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
43LMSDevice::LMSDevice(size_t sps, size_t chans):
44 m_lms_dev(NULL), sps(sps), chans(chans)
Harald Welte940738e2018-03-07 07:50:57 +010045{
46 LOG(INFO) << "creating LMS device...";
47
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020048 m_lms_stream_rx.resize(chans);
49 m_lms_stream_tx.resize(chans);
50
51 m_last_rx_underruns.resize(chans, 0);
52 m_last_rx_overruns.resize(chans, 0);
53 m_last_tx_underruns.resize(chans, 0);
54 m_last_tx_overruns.resize(chans, 0);
Harald Welte940738e2018-03-07 07:50:57 +010055}
56
57static void lms_log_callback(int lvl, const char *msg)
58{
59 /* map lime specific log levels */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020060 static const int lvl_map[5] = {
Harald Welte940738e2018-03-07 07:50:57 +010061 [0] = LOGL_FATAL,
62 [1] = LOGL_ERROR,
63 [2] = LOGL_NOTICE,
64 [3] = LOGL_INFO,
65 [4] = LOGL_DEBUG,
66 };
67 /* protect against future higher log level values (lower importance) */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020068 if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
Harald Welte940738e2018-03-07 07:50:57 +010069 lvl = ARRAY_SIZE(lvl_map)-1;
70
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020071 LOGLV(DLMS, lvl) << msg;
Harald Welte940738e2018-03-07 07:50:57 +010072}
73
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020074static void thread_enable_cancel(bool cancel)
Harald Welte940738e2018-03-07 07:50:57 +010075{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020076 cancel ? pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) :
77 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
78}
Harald Welte940738e2018-03-07 07:50:57 +010079
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020080int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
81{
82 //lms_info_str_t dev_str;
83 lms_info_str_t* info_list;
84 uint16_t dac_val;
85 unsigned int i, n;
86 int rc;
87
88 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +010089
90 LMS_RegisterLogHandler(&lms_log_callback);
91
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020092 if ((n = LMS_GetDeviceList(NULL)) < 0)
93 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
94 LOG(DEBUG) << "Devices found: " << n;
95 if (n < 1)
96 return -1;
Harald Welte940738e2018-03-07 07:50:57 +010097
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020098 info_list = new lms_info_str_t[n];
99
100 if (LMS_GetDeviceList(info_list) < 0) //Populate device list
101 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
102
103 for (i = 0; i < n; i++) //print device list
104 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
105
106 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
107 if (rc != 0) {
108 LOG(ERROR) << "LMS_GetDeviceList() failed)";
109 delete [] info_list;
110 return -1;
111 }
112
113 delete [] info_list;
114
Harald Welte9cb4f272018-04-28 21:38:58 +0200115 LOG(INFO) << "Init LMS device";
116 if (LMS_Init(m_lms_dev) != 0) {
117 LOG(ERROR) << "LMS_Init() failed";
118 return -1;
119 }
120
Harald Weltea4381142018-04-28 21:41:07 +0200121 lms_range_t range;
122 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range))
123 goto out_close;
124 LOG(DEBUG) << "Sample Rate: Min=" << range.min << " Max=" << range.max << " Step=" << range.step;
125
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200126 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
127 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100128 goto out_close;
Harald Weltea4381142018-04-28 21:41:07 +0200129 float_type sr_host, sr_rf;
130 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
131 goto out_close;
132 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Harald Welte940738e2018-03-07 07:50:57 +0100133 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200134 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE);
Harald Welte940738e2018-03-07 07:50:57 +0100135
136 switch (ref) {
137 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200138 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100139 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
140 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
141 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200142 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100143 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
144 goto out_close;
145 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200146 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200147 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100148 /* Assume an external 10 MHz reference clock */
149 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
150 goto out_close;
151 break;
152 default:
153 LOG(ALERT) << "Invalid reference type";
154 goto out_close;
155 }
156
Harald Welte940738e2018-03-07 07:50:57 +0100157 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200158 for (i=0; i<chans; i++) {
Harald Welteea8be222018-04-28 21:52:57 +0200159 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, 1.4001e6) < 0)
160 goto out_close;
161 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, 5e6) < 0)
162 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200163 LOG(INFO) << "Calibrating chan " << i;
164 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
165 goto out_close;
166 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
167 goto out_close;
168 }
Harald Welte940738e2018-03-07 07:50:57 +0100169
170 samplesRead = 0;
171 samplesWritten = 0;
172 started = false;
173
174 return NORMAL;
175
176out_close:
177 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
178 LMS_Close(m_lms_dev);
179 return -1;
180}
181
182bool LMSDevice::start()
183{
184 LOG(INFO) << "starting LMS...";
185
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200186 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100187
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200188 for (i=0; i<chans; i++) {
189 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
190 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100191
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200192 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
193 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100194
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200195 // Set gains to midpoint
196 setTxGain((minTxGain() + maxTxGain()) / 2, i);
197 setRxGain((minRxGain() + maxRxGain()) / 2, i);
198
199 m_lms_stream_rx[i] = {};
200 m_lms_stream_rx[i].isTx = false;
201 m_lms_stream_rx[i].channel = i;
202 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
203 m_lms_stream_rx[i].throughputVsLatency = 0.3;
204 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
205
206 m_lms_stream_tx[i] = {};
207 m_lms_stream_tx[i].isTx = true;
208 m_lms_stream_tx[i].channel = i;
209 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
210 m_lms_stream_tx[i].throughputVsLatency = 0.3;
211 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
212
213 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
214 return false;
215
216 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
217 return false;
218
219 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
220 return false;
221
222 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
223 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100224 }
225
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200226 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100227
228 started = true;
229 return true;
230}
231
232bool LMSDevice::stop()
233{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200234 unsigned int i;
235
Harald Welte940738e2018-03-07 07:50:57 +0100236 if (!started)
237 return true;
238
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200239 for (i=0; i<chans; i++) {
240 LMS_StopStream(&m_lms_stream_tx[i]);
241 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100242
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200243 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
244 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
245 }
Harald Welte940738e2018-03-07 07:50:57 +0100246
247 return true;
248}
249
250double LMSDevice::maxTxGain()
251{
252 return 60.0;
253}
254
255double LMSDevice::minTxGain()
256{
257 return 0.0;
258}
259
260double LMSDevice::maxRxGain()
261{
262 return 70.0;
263}
264
265double LMSDevice::minRxGain()
266{
267 return 0.0;
268}
269
270double LMSDevice::setTxGain(double dB, size_t chan)
271{
272 if (chan) {
273 LOG(ALERT) << "Invalid channel " << chan;
274 return 0.0;
275 }
276
277 if (dB > maxTxGain())
278 dB = maxTxGain();
279 if (dB < minTxGain())
280 dB = minTxGain();
281
282 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
283
284 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
285 LOG(ERR) << "Error setting TX gain";
286
287 return dB;
288}
289
290double LMSDevice::setRxGain(double dB, size_t chan)
291{
292 if (chan) {
293 LOG(ALERT) << "Invalid channel " << chan;
294 return 0.0;
295 }
296
297 dB = 47.0;
298
299 if (dB > maxRxGain())
300 dB = maxRxGain();
301 if (dB < minRxGain())
302 dB = minRxGain();
303
304 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
305
306 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
307 LOG(ERR) << "Error setting RX gain";
308
309 return dB;
310}
311
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200312int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100313{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200314 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
315 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100316 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200317 int i;
318
319 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100320 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200321 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100322 return i;
323 }
324 return -1;
325}
326
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200327bool LMSDevice::flush_recv(size_t num_pkts)
328{
329 #define CHUNK 625
330 int len = CHUNK * sps;
331 short *buffer = new short[len * 2];
332 int rc;
333 lms_stream_meta_t rx_metadata = {};
334 rx_metadata.flushPartialPacket = false;
335 rx_metadata.waitForTimestamp = false;
336
337 ts_initial = 0;
338
339 while (!ts_initial || (num_pkts-- > 0)) {
340 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
341 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
342 if (rc != len) {
343 LOG(ALERT) << "LMS: Device receive timed out";
344 delete[] buffer;
345 return false;
346 }
347
Harald Welte68f05412018-04-28 21:58:03 +0200348 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200349 }
350
351 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
352 delete[] buffer;
353 return true;
354}
355
Harald Welte940738e2018-03-07 07:50:57 +0100356bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
357{
358 int idx;
359
360 if (chan >= rx_paths.size()) {
361 LOG(ALERT) << "Requested non-existent channel " << chan;
362 return false;
363 }
364
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200365 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100366 if (idx < 0) {
367 LOG(ALERT) << "Invalid Rx Antenna";
368 return false;
369 }
370
371 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
372 LOG(ALERT) << "Unable to set Rx Antenna";
373 }
374
375 return true;
376}
377
378std::string LMSDevice::getRxAntenna(size_t chan)
379{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200380 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
381 int idx;
382
Harald Welte940738e2018-03-07 07:50:57 +0100383 if (chan >= rx_paths.size()) {
384 LOG(ALERT) << "Requested non-existent channel " << chan;
385 return "";
386 }
387
388 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
389 if (idx < 0) {
390 LOG(ALERT) << "Error getting Rx Antenna";
391 return "";
392 }
393
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200394 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100395 LOG(ALERT) << "Error getting Rx Antenna List";
396 return "";
397 }
398
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200399 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100400}
401
402bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
403{
404 int idx;
405
406 if (chan >= tx_paths.size()) {
407 LOG(ALERT) << "Requested non-existent channel " << chan;
408 return false;
409 }
410
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200411 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100412 if (idx < 0) {
413 LOG(ALERT) << "Invalid Rx Antenna";
414 return false;
415 }
416
417 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
418 LOG(ALERT) << "Unable to set Rx Antenna";
419 }
420
421 return true;
422}
423
424std::string LMSDevice::getTxAntenna(size_t chan)
425{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200426 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100427 int idx;
428
429 if (chan >= tx_paths.size()) {
430 LOG(ALERT) << "Requested non-existent channel " << chan;
431 return "";
432 }
433
434 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
435 if (idx < 0) {
436 LOG(ALERT) << "Error getting Tx Antenna";
437 return "";
438 }
439
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200440 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100441 LOG(ALERT) << "Error getting Tx Antenna List";
442 return "";
443 }
444
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200445 return name_list[idx];
446}
447
448bool LMSDevice::requiresRadioAlign()
449{
450 return false;
451}
452
453GSM::Time LMSDevice::minLatency() {
454 /* Empirical data from a handful of
455 relatively recent machines shows that the B100 will underrun when
456 the transmit threshold is reduced to a time of 6 and a half frames,
457 so we set a minimum 7 frame threshold. */
458 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100459}
460
461// NOTE: Assumes sequential reads
462int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
463 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
464{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200465 int rc = 0;
466 unsigned int i;
467 lms_stream_status_t status;
468 lms_stream_meta_t rx_metadata = {};
469 rx_metadata.flushPartialPacket = false;
470 rx_metadata.waitForTimestamp = false;
471 /* Shift read time with respect to transmit clock */
472 timestamp += ts_offset;
473 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100474
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200475 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100476 LOG(ALERT) << "Invalid channel combination " << bufs.size();
477 return -1;
478 }
479
Harald Welte940738e2018-03-07 07:50:57 +0100480 *overrun = false;
481 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200482 for (i = 0; i<chans; i++) {
483 thread_enable_cancel(false);
484 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
485 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;
486 if (rc != len) {
487 LOG(ALERT) << "LMS: Device receive timed out";
488 }
Harald Welte940738e2018-03-07 07:50:57 +0100489
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200490 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
491 if (status.underrun > m_last_rx_underruns[i])
492 *underrun = true;
493 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100494
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200495 if (status.overrun > m_last_rx_overruns[i])
496 *overrun = true;
497 m_last_rx_overruns[i] = status.overrun;
498 }
499 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100500 }
501
502 samplesRead += rc;
503
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200504 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
505 rc = 0;
506
Harald Welte940738e2018-03-07 07:50:57 +0100507 return rc;
508}
509
510int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
511 bool * underrun, unsigned long long timestamp,
512 bool isControl)
513{
Harald Welte940738e2018-03-07 07:50:57 +0100514 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200515 unsigned int i;
516 lms_stream_status_t status;
517 lms_stream_meta_t tx_metadata = {};
518 tx_metadata.flushPartialPacket = false;
519 tx_metadata.waitForTimestamp = true;
520 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100521
522 if (isControl) {
523 LOG(ERR) << "Control packets not supported";
524 return 0;
525 }
526
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200527 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100528 LOG(ALERT) << "Invalid channel combination " << bufs.size();
529 return -1;
530 }
531
Harald Welte940738e2018-03-07 07:50:57 +0100532 *underrun = false;
533
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200534 for (i = 0; i<chans; i++) {
535 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
536 thread_enable_cancel(false);
537 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
538 if (rc != len) {
539 LOG(ALERT) << "LMS: Device send timed out";
540 }
541
542 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
543 if (status.underrun > m_last_tx_underruns[i])
544 *underrun = true;
545 m_last_tx_underruns[i] = status.underrun;
546 }
547 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100548 }
549
550 samplesWritten += rc;
551
552 return rc;
553}
554
555bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
556{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200557 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100558}
559
560bool LMSDevice::setTxFreq(double wFreq, size_t chan)
561{
562
563 if (chan) {
564 LOG(ALERT) << "Invalid channel " << chan;
565 return false;
566 }
567
568 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
569 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
570 return false;
571 }
572
573 return true;
574}
575
576bool LMSDevice::setRxFreq(double wFreq, size_t chan)
577{
578 if (chan) {
579 LOG(ALERT) << "Invalid channel " << chan;
580 return false;
581 }
582
583 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
584 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
585 return false;
586 }
587
588 return true;
589}
590
591RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
592 InterfaceType iface, size_t chans, double offset,
593 const std::vector < std::string > &tx_paths,
594 const std::vector < std::string > &rx_paths)
595{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200596 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100597}