blob: 603b23d9d22ea5049a7c610a3ec7f77235e7381a [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
115 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
116 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100117 goto out_close;
118 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200119 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE);
Harald Welte940738e2018-03-07 07:50:57 +0100120
121 switch (ref) {
122 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200123 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100124 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
125 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
126 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200127 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100128 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
129 goto out_close;
130 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200131 case REF_EXTERNAL:
132 LOG(DEBUG) << "Setting Internal clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100133 /* Assume an external 10 MHz reference clock */
134 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
135 goto out_close;
136 break;
137 default:
138 LOG(ALERT) << "Invalid reference type";
139 goto out_close;
140 }
141
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200142 LOG(INFO) << "Init LMS device";
Harald Welte940738e2018-03-07 07:50:57 +0100143 if (LMS_Init(m_lms_dev) < 0)
144 goto out_close;
145
146 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200147 for (i=0; i<chans; i++) {
148 LOG(INFO) << "Calibrating chan " << i;
149 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
150 goto out_close;
151 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
152 goto out_close;
153 }
Harald Welte940738e2018-03-07 07:50:57 +0100154
155 samplesRead = 0;
156 samplesWritten = 0;
157 started = false;
158
159 return NORMAL;
160
161out_close:
162 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
163 LMS_Close(m_lms_dev);
164 return -1;
165}
166
167bool LMSDevice::start()
168{
169 LOG(INFO) << "starting LMS...";
170
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200171 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100172
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200173 for (i=0; i<chans; i++) {
174 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
175 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100176
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200177 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
178 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100179
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200180 // Set gains to midpoint
181 setTxGain((minTxGain() + maxTxGain()) / 2, i);
182 setRxGain((minRxGain() + maxRxGain()) / 2, i);
183
184 m_lms_stream_rx[i] = {};
185 m_lms_stream_rx[i].isTx = false;
186 m_lms_stream_rx[i].channel = i;
187 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
188 m_lms_stream_rx[i].throughputVsLatency = 0.3;
189 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
190
191 m_lms_stream_tx[i] = {};
192 m_lms_stream_tx[i].isTx = true;
193 m_lms_stream_tx[i].channel = i;
194 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
195 m_lms_stream_tx[i].throughputVsLatency = 0.3;
196 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
197
198 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
199 return false;
200
201 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
202 return false;
203
204 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
205 return false;
206
207 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
208 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100209 }
210
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200211 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100212
213 started = true;
214 return true;
215}
216
217bool LMSDevice::stop()
218{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200219 unsigned int i;
220
Harald Welte940738e2018-03-07 07:50:57 +0100221 if (!started)
222 return true;
223
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200224 for (i=0; i<chans; i++) {
225 LMS_StopStream(&m_lms_stream_tx[i]);
226 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100227
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200228 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
229 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
230 }
Harald Welte940738e2018-03-07 07:50:57 +0100231
232 return true;
233}
234
235double LMSDevice::maxTxGain()
236{
237 return 60.0;
238}
239
240double LMSDevice::minTxGain()
241{
242 return 0.0;
243}
244
245double LMSDevice::maxRxGain()
246{
247 return 70.0;
248}
249
250double LMSDevice::minRxGain()
251{
252 return 0.0;
253}
254
255double LMSDevice::setTxGain(double dB, size_t chan)
256{
257 if (chan) {
258 LOG(ALERT) << "Invalid channel " << chan;
259 return 0.0;
260 }
261
262 if (dB > maxTxGain())
263 dB = maxTxGain();
264 if (dB < minTxGain())
265 dB = minTxGain();
266
267 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
268
269 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
270 LOG(ERR) << "Error setting TX gain";
271
272 return dB;
273}
274
275double LMSDevice::setRxGain(double dB, size_t chan)
276{
277 if (chan) {
278 LOG(ALERT) << "Invalid channel " << chan;
279 return 0.0;
280 }
281
282 dB = 47.0;
283
284 if (dB > maxRxGain())
285 dB = maxRxGain();
286 if (dB < minRxGain())
287 dB = minRxGain();
288
289 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
290
291 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
292 LOG(ERR) << "Error setting RX gain";
293
294 return dB;
295}
296
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200297int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100298{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200299 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
300 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100301 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200302 int i;
303
304 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100305 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200306 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100307 return i;
308 }
309 return -1;
310}
311
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200312bool LMSDevice::flush_recv(size_t num_pkts)
313{
314 #define CHUNK 625
315 int len = CHUNK * sps;
316 short *buffer = new short[len * 2];
317 int rc;
318 lms_stream_meta_t rx_metadata = {};
319 rx_metadata.flushPartialPacket = false;
320 rx_metadata.waitForTimestamp = false;
321
322 ts_initial = 0;
323
324 while (!ts_initial || (num_pkts-- > 0)) {
325 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
326 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
327 if (rc != len) {
328 LOG(ALERT) << "LMS: Device receive timed out";
329 delete[] buffer;
330 return false;
331 }
332
333 ts_initial = rx_metadata.timestamp;
334 }
335
336 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
337 delete[] buffer;
338 return true;
339}
340
Harald Welte940738e2018-03-07 07:50:57 +0100341bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
342{
343 int idx;
344
345 if (chan >= rx_paths.size()) {
346 LOG(ALERT) << "Requested non-existent channel " << chan;
347 return false;
348 }
349
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200350 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100351 if (idx < 0) {
352 LOG(ALERT) << "Invalid Rx Antenna";
353 return false;
354 }
355
356 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
357 LOG(ALERT) << "Unable to set Rx Antenna";
358 }
359
360 return true;
361}
362
363std::string LMSDevice::getRxAntenna(size_t chan)
364{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200365 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
366 int idx;
367
Harald Welte940738e2018-03-07 07:50:57 +0100368 if (chan >= rx_paths.size()) {
369 LOG(ALERT) << "Requested non-existent channel " << chan;
370 return "";
371 }
372
373 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
374 if (idx < 0) {
375 LOG(ALERT) << "Error getting Rx Antenna";
376 return "";
377 }
378
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200379 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100380 LOG(ALERT) << "Error getting Rx Antenna List";
381 return "";
382 }
383
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200384 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100385}
386
387bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
388{
389 int idx;
390
391 if (chan >= tx_paths.size()) {
392 LOG(ALERT) << "Requested non-existent channel " << chan;
393 return false;
394 }
395
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200396 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100397 if (idx < 0) {
398 LOG(ALERT) << "Invalid Rx Antenna";
399 return false;
400 }
401
402 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
403 LOG(ALERT) << "Unable to set Rx Antenna";
404 }
405
406 return true;
407}
408
409std::string LMSDevice::getTxAntenna(size_t chan)
410{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200411 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100412 int idx;
413
414 if (chan >= tx_paths.size()) {
415 LOG(ALERT) << "Requested non-existent channel " << chan;
416 return "";
417 }
418
419 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
420 if (idx < 0) {
421 LOG(ALERT) << "Error getting Tx Antenna";
422 return "";
423 }
424
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200425 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100426 LOG(ALERT) << "Error getting Tx Antenna List";
427 return "";
428 }
429
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200430 return name_list[idx];
431}
432
433bool LMSDevice::requiresRadioAlign()
434{
435 return false;
436}
437
438GSM::Time LMSDevice::minLatency() {
439 /* Empirical data from a handful of
440 relatively recent machines shows that the B100 will underrun when
441 the transmit threshold is reduced to a time of 6 and a half frames,
442 so we set a minimum 7 frame threshold. */
443 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100444}
445
446// NOTE: Assumes sequential reads
447int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
448 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
449{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200450 int rc = 0;
451 unsigned int i;
452 lms_stream_status_t status;
453 lms_stream_meta_t rx_metadata = {};
454 rx_metadata.flushPartialPacket = false;
455 rx_metadata.waitForTimestamp = false;
456 /* Shift read time with respect to transmit clock */
457 timestamp += ts_offset;
458 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100459
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200460 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100461 LOG(ALERT) << "Invalid channel combination " << bufs.size();
462 return -1;
463 }
464
Harald Welte940738e2018-03-07 07:50:57 +0100465 *overrun = false;
466 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200467 for (i = 0; i<chans; i++) {
468 thread_enable_cancel(false);
469 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
470 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;
471 if (rc != len) {
472 LOG(ALERT) << "LMS: Device receive timed out";
473 }
Harald Welte940738e2018-03-07 07:50:57 +0100474
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200475 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
476 if (status.underrun > m_last_rx_underruns[i])
477 *underrun = true;
478 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100479
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200480 if (status.overrun > m_last_rx_overruns[i])
481 *overrun = true;
482 m_last_rx_overruns[i] = status.overrun;
483 }
484 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100485 }
486
487 samplesRead += rc;
488
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200489 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
490 rc = 0;
491
Harald Welte940738e2018-03-07 07:50:57 +0100492 return rc;
493}
494
495int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
496 bool * underrun, unsigned long long timestamp,
497 bool isControl)
498{
Harald Welte940738e2018-03-07 07:50:57 +0100499 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200500 unsigned int i;
501 lms_stream_status_t status;
502 lms_stream_meta_t tx_metadata = {};
503 tx_metadata.flushPartialPacket = false;
504 tx_metadata.waitForTimestamp = true;
505 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100506
507 if (isControl) {
508 LOG(ERR) << "Control packets not supported";
509 return 0;
510 }
511
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200512 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100513 LOG(ALERT) << "Invalid channel combination " << bufs.size();
514 return -1;
515 }
516
Harald Welte940738e2018-03-07 07:50:57 +0100517 *underrun = false;
518
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200519 for (i = 0; i<chans; i++) {
520 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
521 thread_enable_cancel(false);
522 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
523 if (rc != len) {
524 LOG(ALERT) << "LMS: Device send timed out";
525 }
526
527 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
528 if (status.underrun > m_last_tx_underruns[i])
529 *underrun = true;
530 m_last_tx_underruns[i] = status.underrun;
531 }
532 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100533 }
534
535 samplesWritten += rc;
536
537 return rc;
538}
539
540bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
541{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200542 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100543}
544
545bool LMSDevice::setTxFreq(double wFreq, size_t chan)
546{
547
548 if (chan) {
549 LOG(ALERT) << "Invalid channel " << chan;
550 return false;
551 }
552
553 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
554 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
555 return false;
556 }
557
558 return true;
559}
560
561bool LMSDevice::setRxFreq(double wFreq, size_t chan)
562{
563 if (chan) {
564 LOG(ALERT) << "Invalid channel " << chan;
565 return false;
566 }
567
568 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
569 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
570 return false;
571 }
572
573 return true;
574}
575
576RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
577 InterfaceType iface, size_t chans, double offset,
578 const std::vector < std::string > &tx_paths,
579 const std::vector < std::string > &rx_paths)
580{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200581 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100582}