blob: 2a40f4668df71c31cb5cc9240b268056c3f96a6c [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! */
Harald Welte4aec1f12018-04-28 21:59:29 +0200134 //ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE);
135 ts_offset = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100136
137 switch (ref) {
138 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200139 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100140 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
141 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
142 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200143 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100144 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
145 goto out_close;
146 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200147 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200148 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100149 /* Assume an external 10 MHz reference clock */
150 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
151 goto out_close;
152 break;
153 default:
154 LOG(ALERT) << "Invalid reference type";
155 goto out_close;
156 }
157
Harald Welte940738e2018-03-07 07:50:57 +0100158 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200159 for (i=0; i<chans; i++) {
Harald Welteea8be222018-04-28 21:52:57 +0200160 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, 1.4001e6) < 0)
161 goto out_close;
162 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, 5e6) < 0)
163 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200164 LOG(INFO) << "Calibrating chan " << i;
165 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
166 goto out_close;
167 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
168 goto out_close;
169 }
Harald Welte940738e2018-03-07 07:50:57 +0100170
171 samplesRead = 0;
172 samplesWritten = 0;
173 started = false;
174
175 return NORMAL;
176
177out_close:
178 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
179 LMS_Close(m_lms_dev);
180 return -1;
181}
182
183bool LMSDevice::start()
184{
185 LOG(INFO) << "starting LMS...";
186
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200187 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100188
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200189 for (i=0; i<chans; i++) {
190 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
191 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100192
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200193 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
194 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100195
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200196 // Set gains to midpoint
197 setTxGain((minTxGain() + maxTxGain()) / 2, i);
198 setRxGain((minRxGain() + maxRxGain()) / 2, i);
199
200 m_lms_stream_rx[i] = {};
201 m_lms_stream_rx[i].isTx = false;
202 m_lms_stream_rx[i].channel = i;
203 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
204 m_lms_stream_rx[i].throughputVsLatency = 0.3;
205 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
206
207 m_lms_stream_tx[i] = {};
208 m_lms_stream_tx[i].isTx = true;
209 m_lms_stream_tx[i].channel = i;
210 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
211 m_lms_stream_tx[i].throughputVsLatency = 0.3;
212 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
213
214 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
215 return false;
216
217 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
218 return false;
219
220 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
221 return false;
222
223 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
224 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100225 }
226
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200227 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100228
229 started = true;
230 return true;
231}
232
233bool LMSDevice::stop()
234{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200235 unsigned int i;
236
Harald Welte940738e2018-03-07 07:50:57 +0100237 if (!started)
238 return true;
239
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200240 for (i=0; i<chans; i++) {
241 LMS_StopStream(&m_lms_stream_tx[i]);
242 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100243
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200244 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
245 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
246 }
Harald Welte940738e2018-03-07 07:50:57 +0100247
248 return true;
249}
250
251double LMSDevice::maxTxGain()
252{
253 return 60.0;
254}
255
256double LMSDevice::minTxGain()
257{
258 return 0.0;
259}
260
261double LMSDevice::maxRxGain()
262{
263 return 70.0;
264}
265
266double LMSDevice::minRxGain()
267{
268 return 0.0;
269}
270
271double LMSDevice::setTxGain(double dB, size_t chan)
272{
273 if (chan) {
274 LOG(ALERT) << "Invalid channel " << chan;
275 return 0.0;
276 }
277
278 if (dB > maxTxGain())
279 dB = maxTxGain();
280 if (dB < minTxGain())
281 dB = minTxGain();
282
283 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
284
285 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
286 LOG(ERR) << "Error setting TX gain";
287
288 return dB;
289}
290
291double LMSDevice::setRxGain(double dB, size_t chan)
292{
293 if (chan) {
294 LOG(ALERT) << "Invalid channel " << chan;
295 return 0.0;
296 }
297
298 dB = 47.0;
299
300 if (dB > maxRxGain())
301 dB = maxRxGain();
302 if (dB < minRxGain())
303 dB = minRxGain();
304
305 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
306
307 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
308 LOG(ERR) << "Error setting RX gain";
309
310 return dB;
311}
312
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200313int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100314{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200315 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
316 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100317 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200318 int i;
319
320 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100321 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200322 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100323 return i;
324 }
325 return -1;
326}
327
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200328bool LMSDevice::flush_recv(size_t num_pkts)
329{
330 #define CHUNK 625
331 int len = CHUNK * sps;
332 short *buffer = new short[len * 2];
333 int rc;
334 lms_stream_meta_t rx_metadata = {};
335 rx_metadata.flushPartialPacket = false;
336 rx_metadata.waitForTimestamp = false;
337
338 ts_initial = 0;
339
340 while (!ts_initial || (num_pkts-- > 0)) {
341 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
342 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
343 if (rc != len) {
344 LOG(ALERT) << "LMS: Device receive timed out";
345 delete[] buffer;
346 return false;
347 }
348
Harald Welte68f05412018-04-28 21:58:03 +0200349 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200350 }
351
352 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
353 delete[] buffer;
354 return true;
355}
356
Harald Welte940738e2018-03-07 07:50:57 +0100357bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
358{
359 int idx;
360
361 if (chan >= rx_paths.size()) {
362 LOG(ALERT) << "Requested non-existent channel " << chan;
363 return false;
364 }
365
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200366 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100367 if (idx < 0) {
368 LOG(ALERT) << "Invalid Rx Antenna";
369 return false;
370 }
371
372 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
373 LOG(ALERT) << "Unable to set Rx Antenna";
374 }
375
376 return true;
377}
378
379std::string LMSDevice::getRxAntenna(size_t chan)
380{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200381 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
382 int idx;
383
Harald Welte940738e2018-03-07 07:50:57 +0100384 if (chan >= rx_paths.size()) {
385 LOG(ALERT) << "Requested non-existent channel " << chan;
386 return "";
387 }
388
389 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
390 if (idx < 0) {
391 LOG(ALERT) << "Error getting Rx Antenna";
392 return "";
393 }
394
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200395 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100396 LOG(ALERT) << "Error getting Rx Antenna List";
397 return "";
398 }
399
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200400 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100401}
402
403bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
404{
405 int idx;
406
407 if (chan >= tx_paths.size()) {
408 LOG(ALERT) << "Requested non-existent channel " << chan;
409 return false;
410 }
411
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200412 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100413 if (idx < 0) {
414 LOG(ALERT) << "Invalid Rx Antenna";
415 return false;
416 }
417
418 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
419 LOG(ALERT) << "Unable to set Rx Antenna";
420 }
421
422 return true;
423}
424
425std::string LMSDevice::getTxAntenna(size_t chan)
426{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200427 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100428 int idx;
429
430 if (chan >= tx_paths.size()) {
431 LOG(ALERT) << "Requested non-existent channel " << chan;
432 return "";
433 }
434
435 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
436 if (idx < 0) {
437 LOG(ALERT) << "Error getting Tx Antenna";
438 return "";
439 }
440
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200441 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100442 LOG(ALERT) << "Error getting Tx Antenna List";
443 return "";
444 }
445
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200446 return name_list[idx];
447}
448
449bool LMSDevice::requiresRadioAlign()
450{
451 return false;
452}
453
454GSM::Time LMSDevice::minLatency() {
455 /* Empirical data from a handful of
456 relatively recent machines shows that the B100 will underrun when
457 the transmit threshold is reduced to a time of 6 and a half frames,
458 so we set a minimum 7 frame threshold. */
459 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100460}
461
462// NOTE: Assumes sequential reads
463int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
464 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
465{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200466 int rc = 0;
467 unsigned int i;
468 lms_stream_status_t status;
469 lms_stream_meta_t rx_metadata = {};
470 rx_metadata.flushPartialPacket = false;
471 rx_metadata.waitForTimestamp = false;
472 /* Shift read time with respect to transmit clock */
473 timestamp += ts_offset;
474 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100475
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200476 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100477 LOG(ALERT) << "Invalid channel combination " << bufs.size();
478 return -1;
479 }
480
Harald Welte940738e2018-03-07 07:50:57 +0100481 *overrun = false;
482 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200483 for (i = 0; i<chans; i++) {
484 thread_enable_cancel(false);
485 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200486 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
487 LOG(ALERT) << "chan "<< i << " recv buffer of len " << rc << " expect " << std::hex << timestamp << " got " << std::hex << (TIMESTAMP)rx_metadata.timestamp << " (" << std::hex << rx_metadata.timestamp <<") diff=" << rx_metadata.timestamp - timestamp;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200488 if (rc != len) {
489 LOG(ALERT) << "LMS: Device receive timed out";
490 }
Harald Welte940738e2018-03-07 07:50:57 +0100491
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200492 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
493 if (status.underrun > m_last_rx_underruns[i])
494 *underrun = true;
495 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100496
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200497 if (status.overrun > m_last_rx_overruns[i])
498 *overrun = true;
499 m_last_rx_overruns[i] = status.overrun;
500 }
501 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100502 }
503
504 samplesRead += rc;
505
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200506 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
507 rc = 0;
508
Harald Welte940738e2018-03-07 07:50:57 +0100509 return rc;
510}
511
512int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
513 bool * underrun, unsigned long long timestamp,
514 bool isControl)
515{
Harald Welte940738e2018-03-07 07:50:57 +0100516 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200517 unsigned int i;
518 lms_stream_status_t status;
519 lms_stream_meta_t tx_metadata = {};
520 tx_metadata.flushPartialPacket = false;
521 tx_metadata.waitForTimestamp = true;
522 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100523
524 if (isControl) {
525 LOG(ERR) << "Control packets not supported";
526 return 0;
527 }
528
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200529 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100530 LOG(ALERT) << "Invalid channel combination " << bufs.size();
531 return -1;
532 }
533
Harald Welte940738e2018-03-07 07:50:57 +0100534 *underrun = false;
535
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200536 for (i = 0; i<chans; i++) {
537 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
538 thread_enable_cancel(false);
539 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
540 if (rc != len) {
541 LOG(ALERT) << "LMS: Device send timed out";
542 }
543
544 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
545 if (status.underrun > m_last_tx_underruns[i])
546 *underrun = true;
547 m_last_tx_underruns[i] = status.underrun;
548 }
549 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100550 }
551
552 samplesWritten += rc;
553
554 return rc;
555}
556
557bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
558{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200559 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100560}
561
562bool LMSDevice::setTxFreq(double wFreq, size_t chan)
563{
564
565 if (chan) {
566 LOG(ALERT) << "Invalid channel " << chan;
567 return false;
568 }
569
570 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
571 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
572 return false;
573 }
574
575 return true;
576}
577
578bool LMSDevice::setRxFreq(double wFreq, size_t chan)
579{
580 if (chan) {
581 LOG(ALERT) << "Invalid channel " << chan;
582 return false;
583 }
584
585 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
586 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
587 return false;
588 }
589
590 return true;
591}
592
593RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
594 InterfaceType iface, size_t chans, double offset,
595 const std::vector < std::string > &tx_paths,
596 const std::vector < std::string > &rx_paths)
597{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200598 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100599}