blob: 9301cf40d62620ce1eb9673ec96bb64307bf1827 [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:
147 LOG(DEBUG) << "Setting Internal 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++) {
159 LOG(INFO) << "Calibrating chan " << i;
160 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
161 goto out_close;
162 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
163 goto out_close;
164 }
Harald Welte940738e2018-03-07 07:50:57 +0100165
166 samplesRead = 0;
167 samplesWritten = 0;
168 started = false;
169
170 return NORMAL;
171
172out_close:
173 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
174 LMS_Close(m_lms_dev);
175 return -1;
176}
177
178bool LMSDevice::start()
179{
180 LOG(INFO) << "starting LMS...";
181
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200182 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100183
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200184 for (i=0; i<chans; i++) {
185 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
186 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100187
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200188 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
189 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100190
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200191 // Set gains to midpoint
192 setTxGain((minTxGain() + maxTxGain()) / 2, i);
193 setRxGain((minRxGain() + maxRxGain()) / 2, i);
194
195 m_lms_stream_rx[i] = {};
196 m_lms_stream_rx[i].isTx = false;
197 m_lms_stream_rx[i].channel = i;
198 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
199 m_lms_stream_rx[i].throughputVsLatency = 0.3;
200 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
201
202 m_lms_stream_tx[i] = {};
203 m_lms_stream_tx[i].isTx = true;
204 m_lms_stream_tx[i].channel = i;
205 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
206 m_lms_stream_tx[i].throughputVsLatency = 0.3;
207 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
208
209 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
210 return false;
211
212 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
213 return false;
214
215 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
216 return false;
217
218 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
219 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100220 }
221
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200222 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100223
224 started = true;
225 return true;
226}
227
228bool LMSDevice::stop()
229{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200230 unsigned int i;
231
Harald Welte940738e2018-03-07 07:50:57 +0100232 if (!started)
233 return true;
234
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200235 for (i=0; i<chans; i++) {
236 LMS_StopStream(&m_lms_stream_tx[i]);
237 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100238
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200239 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
240 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
241 }
Harald Welte940738e2018-03-07 07:50:57 +0100242
243 return true;
244}
245
246double LMSDevice::maxTxGain()
247{
248 return 60.0;
249}
250
251double LMSDevice::minTxGain()
252{
253 return 0.0;
254}
255
256double LMSDevice::maxRxGain()
257{
258 return 70.0;
259}
260
261double LMSDevice::minRxGain()
262{
263 return 0.0;
264}
265
266double LMSDevice::setTxGain(double dB, size_t chan)
267{
268 if (chan) {
269 LOG(ALERT) << "Invalid channel " << chan;
270 return 0.0;
271 }
272
273 if (dB > maxTxGain())
274 dB = maxTxGain();
275 if (dB < minTxGain())
276 dB = minTxGain();
277
278 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
279
280 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
281 LOG(ERR) << "Error setting TX gain";
282
283 return dB;
284}
285
286double LMSDevice::setRxGain(double dB, size_t chan)
287{
288 if (chan) {
289 LOG(ALERT) << "Invalid channel " << chan;
290 return 0.0;
291 }
292
293 dB = 47.0;
294
295 if (dB > maxRxGain())
296 dB = maxRxGain();
297 if (dB < minRxGain())
298 dB = minRxGain();
299
300 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
301
302 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
303 LOG(ERR) << "Error setting RX gain";
304
305 return dB;
306}
307
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200308int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100309{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200310 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
311 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100312 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200313 int i;
314
315 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100316 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200317 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100318 return i;
319 }
320 return -1;
321}
322
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200323bool LMSDevice::flush_recv(size_t num_pkts)
324{
325 #define CHUNK 625
326 int len = CHUNK * sps;
327 short *buffer = new short[len * 2];
328 int rc;
329 lms_stream_meta_t rx_metadata = {};
330 rx_metadata.flushPartialPacket = false;
331 rx_metadata.waitForTimestamp = false;
332
333 ts_initial = 0;
334
335 while (!ts_initial || (num_pkts-- > 0)) {
336 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
337 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
338 if (rc != len) {
339 LOG(ALERT) << "LMS: Device receive timed out";
340 delete[] buffer;
341 return false;
342 }
343
344 ts_initial = rx_metadata.timestamp;
345 }
346
347 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
348 delete[] buffer;
349 return true;
350}
351
Harald Welte940738e2018-03-07 07:50:57 +0100352bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
353{
354 int idx;
355
356 if (chan >= rx_paths.size()) {
357 LOG(ALERT) << "Requested non-existent channel " << chan;
358 return false;
359 }
360
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200361 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100362 if (idx < 0) {
363 LOG(ALERT) << "Invalid Rx Antenna";
364 return false;
365 }
366
367 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
368 LOG(ALERT) << "Unable to set Rx Antenna";
369 }
370
371 return true;
372}
373
374std::string LMSDevice::getRxAntenna(size_t chan)
375{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200376 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
377 int idx;
378
Harald Welte940738e2018-03-07 07:50:57 +0100379 if (chan >= rx_paths.size()) {
380 LOG(ALERT) << "Requested non-existent channel " << chan;
381 return "";
382 }
383
384 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
385 if (idx < 0) {
386 LOG(ALERT) << "Error getting Rx Antenna";
387 return "";
388 }
389
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200390 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100391 LOG(ALERT) << "Error getting Rx Antenna List";
392 return "";
393 }
394
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200395 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100396}
397
398bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
399{
400 int idx;
401
402 if (chan >= tx_paths.size()) {
403 LOG(ALERT) << "Requested non-existent channel " << chan;
404 return false;
405 }
406
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200407 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100408 if (idx < 0) {
409 LOG(ALERT) << "Invalid Rx Antenna";
410 return false;
411 }
412
413 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
414 LOG(ALERT) << "Unable to set Rx Antenna";
415 }
416
417 return true;
418}
419
420std::string LMSDevice::getTxAntenna(size_t chan)
421{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200422 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100423 int idx;
424
425 if (chan >= tx_paths.size()) {
426 LOG(ALERT) << "Requested non-existent channel " << chan;
427 return "";
428 }
429
430 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
431 if (idx < 0) {
432 LOG(ALERT) << "Error getting Tx Antenna";
433 return "";
434 }
435
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200436 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100437 LOG(ALERT) << "Error getting Tx Antenna List";
438 return "";
439 }
440
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200441 return name_list[idx];
442}
443
444bool LMSDevice::requiresRadioAlign()
445{
446 return false;
447}
448
449GSM::Time LMSDevice::minLatency() {
450 /* Empirical data from a handful of
451 relatively recent machines shows that the B100 will underrun when
452 the transmit threshold is reduced to a time of 6 and a half frames,
453 so we set a minimum 7 frame threshold. */
454 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100455}
456
457// NOTE: Assumes sequential reads
458int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
459 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
460{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200461 int rc = 0;
462 unsigned int i;
463 lms_stream_status_t status;
464 lms_stream_meta_t rx_metadata = {};
465 rx_metadata.flushPartialPacket = false;
466 rx_metadata.waitForTimestamp = false;
467 /* Shift read time with respect to transmit clock */
468 timestamp += ts_offset;
469 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100470
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200471 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100472 LOG(ALERT) << "Invalid channel combination " << bufs.size();
473 return -1;
474 }
475
Harald Welte940738e2018-03-07 07:50:57 +0100476 *overrun = false;
477 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200478 for (i = 0; i<chans; i++) {
479 thread_enable_cancel(false);
480 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
481 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;
482 if (rc != len) {
483 LOG(ALERT) << "LMS: Device receive timed out";
484 }
Harald Welte940738e2018-03-07 07:50:57 +0100485
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200486 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
487 if (status.underrun > m_last_rx_underruns[i])
488 *underrun = true;
489 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100490
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200491 if (status.overrun > m_last_rx_overruns[i])
492 *overrun = true;
493 m_last_rx_overruns[i] = status.overrun;
494 }
495 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100496 }
497
498 samplesRead += rc;
499
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200500 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
501 rc = 0;
502
Harald Welte940738e2018-03-07 07:50:57 +0100503 return rc;
504}
505
506int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
507 bool * underrun, unsigned long long timestamp,
508 bool isControl)
509{
Harald Welte940738e2018-03-07 07:50:57 +0100510 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200511 unsigned int i;
512 lms_stream_status_t status;
513 lms_stream_meta_t tx_metadata = {};
514 tx_metadata.flushPartialPacket = false;
515 tx_metadata.waitForTimestamp = true;
516 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100517
518 if (isControl) {
519 LOG(ERR) << "Control packets not supported";
520 return 0;
521 }
522
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200523 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100524 LOG(ALERT) << "Invalid channel combination " << bufs.size();
525 return -1;
526 }
527
Harald Welte940738e2018-03-07 07:50:57 +0100528 *underrun = false;
529
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200530 for (i = 0; i<chans; i++) {
531 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
532 thread_enable_cancel(false);
533 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
534 if (rc != len) {
535 LOG(ALERT) << "LMS: Device send timed out";
536 }
537
538 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
539 if (status.underrun > m_last_tx_underruns[i])
540 *underrun = true;
541 m_last_tx_underruns[i] = status.underrun;
542 }
543 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100544 }
545
546 samplesWritten += rc;
547
548 return rc;
549}
550
551bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
552{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200553 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100554}
555
556bool LMSDevice::setTxFreq(double wFreq, size_t chan)
557{
558
559 if (chan) {
560 LOG(ALERT) << "Invalid channel " << chan;
561 return false;
562 }
563
564 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
565 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
566 return false;
567 }
568
569 return true;
570}
571
572bool LMSDevice::setRxFreq(double wFreq, size_t chan)
573{
574 if (chan) {
575 LOG(ALERT) << "Invalid channel " << chan;
576 return false;
577 }
578
579 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
580 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
581 return false;
582 }
583
584 return true;
585}
586
587RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
588 InterfaceType iface, size_t chans, double offset,
589 const std::vector < std::string > &tx_paths,
590 const std::vector < std::string > &rx_paths)
591{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200592 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100593}