blob: 0442e27153d3f04373aa6bde4d744e1117fb801e [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
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200121 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
122 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100123 goto out_close;
124 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200125 ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE);
Harald Welte940738e2018-03-07 07:50:57 +0100126
127 switch (ref) {
128 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200129 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100130 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
131 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
132 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200133 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100134 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
135 goto out_close;
136 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200137 case REF_EXTERNAL:
138 LOG(DEBUG) << "Setting Internal clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100139 /* Assume an external 10 MHz reference clock */
140 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
141 goto out_close;
142 break;
143 default:
144 LOG(ALERT) << "Invalid reference type";
145 goto out_close;
146 }
147
Harald Welte940738e2018-03-07 07:50:57 +0100148 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200149 for (i=0; i<chans; i++) {
150 LOG(INFO) << "Calibrating chan " << i;
151 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
152 goto out_close;
153 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
154 goto out_close;
155 }
Harald Welte940738e2018-03-07 07:50:57 +0100156
157 samplesRead = 0;
158 samplesWritten = 0;
159 started = false;
160
161 return NORMAL;
162
163out_close:
164 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
165 LMS_Close(m_lms_dev);
166 return -1;
167}
168
169bool LMSDevice::start()
170{
171 LOG(INFO) << "starting LMS...";
172
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200173 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100174
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200175 for (i=0; i<chans; i++) {
176 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
177 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100178
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200179 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
180 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100181
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200182 // Set gains to midpoint
183 setTxGain((minTxGain() + maxTxGain()) / 2, i);
184 setRxGain((minRxGain() + maxRxGain()) / 2, i);
185
186 m_lms_stream_rx[i] = {};
187 m_lms_stream_rx[i].isTx = false;
188 m_lms_stream_rx[i].channel = i;
189 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
190 m_lms_stream_rx[i].throughputVsLatency = 0.3;
191 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
192
193 m_lms_stream_tx[i] = {};
194 m_lms_stream_tx[i].isTx = true;
195 m_lms_stream_tx[i].channel = i;
196 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
197 m_lms_stream_tx[i].throughputVsLatency = 0.3;
198 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
199
200 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
201 return false;
202
203 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
204 return false;
205
206 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
207 return false;
208
209 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
210 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100211 }
212
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200213 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100214
215 started = true;
216 return true;
217}
218
219bool LMSDevice::stop()
220{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200221 unsigned int i;
222
Harald Welte940738e2018-03-07 07:50:57 +0100223 if (!started)
224 return true;
225
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200226 for (i=0; i<chans; i++) {
227 LMS_StopStream(&m_lms_stream_tx[i]);
228 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100229
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200230 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
231 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
232 }
Harald Welte940738e2018-03-07 07:50:57 +0100233
234 return true;
235}
236
237double LMSDevice::maxTxGain()
238{
239 return 60.0;
240}
241
242double LMSDevice::minTxGain()
243{
244 return 0.0;
245}
246
247double LMSDevice::maxRxGain()
248{
249 return 70.0;
250}
251
252double LMSDevice::minRxGain()
253{
254 return 0.0;
255}
256
257double LMSDevice::setTxGain(double dB, size_t chan)
258{
259 if (chan) {
260 LOG(ALERT) << "Invalid channel " << chan;
261 return 0.0;
262 }
263
264 if (dB > maxTxGain())
265 dB = maxTxGain();
266 if (dB < minTxGain())
267 dB = minTxGain();
268
269 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
270
271 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
272 LOG(ERR) << "Error setting TX gain";
273
274 return dB;
275}
276
277double LMSDevice::setRxGain(double dB, size_t chan)
278{
279 if (chan) {
280 LOG(ALERT) << "Invalid channel " << chan;
281 return 0.0;
282 }
283
284 dB = 47.0;
285
286 if (dB > maxRxGain())
287 dB = maxRxGain();
288 if (dB < minRxGain())
289 dB = minRxGain();
290
291 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
292
293 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
294 LOG(ERR) << "Error setting RX gain";
295
296 return dB;
297}
298
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200299int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100300{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200301 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
302 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100303 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200304 int i;
305
306 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100307 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200308 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100309 return i;
310 }
311 return -1;
312}
313
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200314bool LMSDevice::flush_recv(size_t num_pkts)
315{
316 #define CHUNK 625
317 int len = CHUNK * sps;
318 short *buffer = new short[len * 2];
319 int rc;
320 lms_stream_meta_t rx_metadata = {};
321 rx_metadata.flushPartialPacket = false;
322 rx_metadata.waitForTimestamp = false;
323
324 ts_initial = 0;
325
326 while (!ts_initial || (num_pkts-- > 0)) {
327 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
328 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
329 if (rc != len) {
330 LOG(ALERT) << "LMS: Device receive timed out";
331 delete[] buffer;
332 return false;
333 }
334
335 ts_initial = rx_metadata.timestamp;
336 }
337
338 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
339 delete[] buffer;
340 return true;
341}
342
Harald Welte940738e2018-03-07 07:50:57 +0100343bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
344{
345 int idx;
346
347 if (chan >= rx_paths.size()) {
348 LOG(ALERT) << "Requested non-existent channel " << chan;
349 return false;
350 }
351
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200352 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100353 if (idx < 0) {
354 LOG(ALERT) << "Invalid Rx Antenna";
355 return false;
356 }
357
358 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
359 LOG(ALERT) << "Unable to set Rx Antenna";
360 }
361
362 return true;
363}
364
365std::string LMSDevice::getRxAntenna(size_t chan)
366{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200367 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
368 int idx;
369
Harald Welte940738e2018-03-07 07:50:57 +0100370 if (chan >= rx_paths.size()) {
371 LOG(ALERT) << "Requested non-existent channel " << chan;
372 return "";
373 }
374
375 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
376 if (idx < 0) {
377 LOG(ALERT) << "Error getting Rx Antenna";
378 return "";
379 }
380
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200381 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100382 LOG(ALERT) << "Error getting Rx Antenna List";
383 return "";
384 }
385
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200386 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100387}
388
389bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
390{
391 int idx;
392
393 if (chan >= tx_paths.size()) {
394 LOG(ALERT) << "Requested non-existent channel " << chan;
395 return false;
396 }
397
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200398 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100399 if (idx < 0) {
400 LOG(ALERT) << "Invalid Rx Antenna";
401 return false;
402 }
403
404 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
405 LOG(ALERT) << "Unable to set Rx Antenna";
406 }
407
408 return true;
409}
410
411std::string LMSDevice::getTxAntenna(size_t chan)
412{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200413 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100414 int idx;
415
416 if (chan >= tx_paths.size()) {
417 LOG(ALERT) << "Requested non-existent channel " << chan;
418 return "";
419 }
420
421 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
422 if (idx < 0) {
423 LOG(ALERT) << "Error getting Tx Antenna";
424 return "";
425 }
426
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200427 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100428 LOG(ALERT) << "Error getting Tx Antenna List";
429 return "";
430 }
431
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200432 return name_list[idx];
433}
434
435bool LMSDevice::requiresRadioAlign()
436{
437 return false;
438}
439
440GSM::Time LMSDevice::minLatency() {
441 /* Empirical data from a handful of
442 relatively recent machines shows that the B100 will underrun when
443 the transmit threshold is reduced to a time of 6 and a half frames,
444 so we set a minimum 7 frame threshold. */
445 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100446}
447
448// NOTE: Assumes sequential reads
449int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
450 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
451{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200452 int rc = 0;
453 unsigned int i;
454 lms_stream_status_t status;
455 lms_stream_meta_t rx_metadata = {};
456 rx_metadata.flushPartialPacket = false;
457 rx_metadata.waitForTimestamp = false;
458 /* Shift read time with respect to transmit clock */
459 timestamp += ts_offset;
460 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100461
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200462 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100463 LOG(ALERT) << "Invalid channel combination " << bufs.size();
464 return -1;
465 }
466
Harald Welte940738e2018-03-07 07:50:57 +0100467 *overrun = false;
468 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200469 for (i = 0; i<chans; i++) {
470 thread_enable_cancel(false);
471 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
472 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;
473 if (rc != len) {
474 LOG(ALERT) << "LMS: Device receive timed out";
475 }
Harald Welte940738e2018-03-07 07:50:57 +0100476
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200477 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
478 if (status.underrun > m_last_rx_underruns[i])
479 *underrun = true;
480 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100481
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200482 if (status.overrun > m_last_rx_overruns[i])
483 *overrun = true;
484 m_last_rx_overruns[i] = status.overrun;
485 }
486 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100487 }
488
489 samplesRead += rc;
490
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200491 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
492 rc = 0;
493
Harald Welte940738e2018-03-07 07:50:57 +0100494 return rc;
495}
496
497int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
498 bool * underrun, unsigned long long timestamp,
499 bool isControl)
500{
Harald Welte940738e2018-03-07 07:50:57 +0100501 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200502 unsigned int i;
503 lms_stream_status_t status;
504 lms_stream_meta_t tx_metadata = {};
505 tx_metadata.flushPartialPacket = false;
506 tx_metadata.waitForTimestamp = true;
507 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100508
509 if (isControl) {
510 LOG(ERR) << "Control packets not supported";
511 return 0;
512 }
513
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200514 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100515 LOG(ALERT) << "Invalid channel combination " << bufs.size();
516 return -1;
517 }
518
Harald Welte940738e2018-03-07 07:50:57 +0100519 *underrun = false;
520
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200521 for (i = 0; i<chans; i++) {
522 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
523 thread_enable_cancel(false);
524 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
525 if (rc != len) {
526 LOG(ALERT) << "LMS: Device send timed out";
527 }
528
529 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
530 if (status.underrun > m_last_tx_underruns[i])
531 *underrun = true;
532 m_last_tx_underruns[i] = status.underrun;
533 }
534 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100535 }
536
537 samplesWritten += rc;
538
539 return rc;
540}
541
542bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
543{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200544 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100545}
546
547bool LMSDevice::setTxFreq(double wFreq, size_t chan)
548{
549
550 if (chan) {
551 LOG(ALERT) << "Invalid channel " << chan;
552 return false;
553 }
554
555 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
556 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
557 return false;
558 }
559
560 return true;
561}
562
563bool LMSDevice::setRxFreq(double wFreq, size_t chan)
564{
565 if (chan) {
566 LOG(ALERT) << "Invalid channel " << chan;
567 return false;
568 }
569
570 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
571 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
572 return false;
573 }
574
575 return true;
576}
577
578RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
579 InterfaceType iface, size_t chans, double offset,
580 const std::vector < std::string > &tx_paths,
581 const std::vector < std::string > &rx_paths)
582{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200583 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100584}