blob: d9b5ba60f0130271dcc97c405df17801e5907cf8 [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 Pedrol6493dc82018-05-29 19:00:30 +020080static void print_range(const char* name, lms_range_t *range)
81{
82 LOG(DEBUG) << name << ": Min=" << range->min << " Max=" << range->max
83 << " Step=" << range->step;
84}
85
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020086int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
87{
88 //lms_info_str_t dev_str;
89 lms_info_str_t* info_list;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +020090 lms_range_t range_lpfbw_rx, range_lpfbw_tx, range_sr;
91 float_type sr_host, sr_rf, lpfbw_rx, lpfbw_tx;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020092 uint16_t dac_val;
93 unsigned int i, n;
94 int rc;
95
96 LOG(INFO) << "Opening LMS device..";
Harald Welte940738e2018-03-07 07:50:57 +010097
98 LMS_RegisterLogHandler(&lms_log_callback);
99
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200100 if ((n = LMS_GetDeviceList(NULL)) < 0)
101 LOG(ERROR) << "LMS_GetDeviceList(NULL) failed";
102 LOG(DEBUG) << "Devices found: " << n;
103 if (n < 1)
104 return -1;
Harald Welte940738e2018-03-07 07:50:57 +0100105
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200106 info_list = new lms_info_str_t[n];
107
108 if (LMS_GetDeviceList(info_list) < 0) //Populate device list
109 LOG(ERROR) << "LMS_GetDeviceList(info_list) failed";
110
111 for (i = 0; i < n; i++) //print device list
112 LOG(DEBUG) << "Device [" << i << "]: " << info_list[i];
113
114 rc = LMS_Open(&m_lms_dev, info_list[0], NULL);
115 if (rc != 0) {
116 LOG(ERROR) << "LMS_GetDeviceList() failed)";
117 delete [] info_list;
118 return -1;
119 }
120
121 delete [] info_list;
122
Harald Welte9cb4f272018-04-28 21:38:58 +0200123 LOG(INFO) << "Init LMS device";
124 if (LMS_Init(m_lms_dev) != 0) {
125 LOG(ERROR) << "LMS_Init() failed";
126 return -1;
127 }
128
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200129 if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
Harald Weltea4381142018-04-28 21:41:07 +0200130 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200131 print_range("Sample Rate", &range_sr);
Harald Weltea4381142018-04-28 21:41:07 +0200132
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200133 LOG(DEBUG) << "Setting sample rate to " << GSMRATE*sps << " " << sps;
134 if (LMS_SetSampleRate(m_lms_dev, GSMRATE*sps, 32) < 0)
Harald Welte940738e2018-03-07 07:50:57 +0100135 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200136
Harald Weltea4381142018-04-28 21:41:07 +0200137 if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
138 goto out_close;
139 LOG(DEBUG) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200140
Harald Welte940738e2018-03-07 07:50:57 +0100141 /* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
Harald Welte4aec1f12018-04-28 21:59:29 +0200142 //ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE);
143 ts_offset = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100144
145 switch (ref) {
146 case REF_INTERNAL:
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200147 LOG(DEBUG) << "Setting Internal clock reference";
Harald Welte940738e2018-03-07 07:50:57 +0100148 /* Ugly API: Selecting clock source implicit by writing to VCTCXO DAC ?!? */
149 if (LMS_VCTCXORead(m_lms_dev, &dac_val) < 0)
150 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200151 LOG(DEBUG) << "Setting VCTCXO to " << dac_val;
Harald Welte940738e2018-03-07 07:50:57 +0100152 if (LMS_VCTCXOWrite(m_lms_dev, dac_val) < 0)
153 goto out_close;
154 break;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200155 case REF_EXTERNAL:
Harald Weltea5054b32018-04-28 21:41:34 +0200156 LOG(DEBUG) << "Setting External clock reference to " << 10000000.0;
Harald Welte940738e2018-03-07 07:50:57 +0100157 /* Assume an external 10 MHz reference clock */
158 if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
159 goto out_close;
160 break;
161 default:
162 LOG(ALERT) << "Invalid reference type";
163 goto out_close;
164 }
165
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200166 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
167 goto out_close;
168 print_range("LPFBWRange Rx", &range_lpfbw_rx);
169 if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
170 goto out_close;
171 print_range("LPFBWRange Tx", &range_lpfbw_tx);
172 lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
173 lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
174
175 LOG(DEBUG) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
176
Harald Welte940738e2018-03-07 07:50:57 +0100177 /* Perform Rx and Tx calibration */
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200178 for (i=0; i<chans; i++) {
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200179 LOG(INFO) << "Setting LPFBW chan " << i;
180 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, i, lpfbw_rx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200181 goto out_close;
Pau Espin Pedrol6493dc82018-05-29 19:00:30 +0200182 if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, i, lpfbw_tx) < 0)
Harald Welteea8be222018-04-28 21:52:57 +0200183 goto out_close;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200184 LOG(INFO) << "Calibrating chan " << i;
185 if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
186 goto out_close;
187 if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, i, LMS_CALIBRATE_BW_HZ, 0) < 0)
188 goto out_close;
189 }
Harald Welte940738e2018-03-07 07:50:57 +0100190
191 samplesRead = 0;
192 samplesWritten = 0;
193 started = false;
194
195 return NORMAL;
196
197out_close:
198 LOG(ALERT) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
199 LMS_Close(m_lms_dev);
200 return -1;
201}
202
203bool LMSDevice::start()
204{
205 LOG(INFO) << "starting LMS...";
206
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200207 unsigned int i;
Harald Welte940738e2018-03-07 07:50:57 +0100208
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200209 for (i=0; i<chans; i++) {
210 if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
211 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100212
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200213 if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
214 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100215
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200216 // Set gains to midpoint
217 setTxGain((minTxGain() + maxTxGain()) / 2, i);
218 setRxGain((minRxGain() + maxRxGain()) / 2, i);
219
220 m_lms_stream_rx[i] = {};
221 m_lms_stream_rx[i].isTx = false;
222 m_lms_stream_rx[i].channel = i;
223 m_lms_stream_rx[i].fifoSize = 1024 * 1024;
224 m_lms_stream_rx[i].throughputVsLatency = 0.3;
225 m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
226
227 m_lms_stream_tx[i] = {};
228 m_lms_stream_tx[i].isTx = true;
229 m_lms_stream_tx[i].channel = i;
230 m_lms_stream_tx[i].fifoSize = 1024 * 1024;
231 m_lms_stream_tx[i].throughputVsLatency = 0.3;
232 m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
233
234 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
235 return false;
236
237 if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
238 return false;
239
240 if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
241 return false;
242
243 if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
244 return false;
Harald Welte940738e2018-03-07 07:50:57 +0100245 }
246
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200247 flush_recv(10);
Harald Welte940738e2018-03-07 07:50:57 +0100248
249 started = true;
250 return true;
251}
252
253bool LMSDevice::stop()
254{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200255 unsigned int i;
256
Harald Welte940738e2018-03-07 07:50:57 +0100257 if (!started)
258 return true;
259
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200260 for (i=0; i<chans; i++) {
261 LMS_StopStream(&m_lms_stream_tx[i]);
262 LMS_StopStream(&m_lms_stream_rx[i]);
Harald Welte940738e2018-03-07 07:50:57 +0100263
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200264 LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
265 LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
266 }
Harald Welte940738e2018-03-07 07:50:57 +0100267
268 return true;
269}
270
271double LMSDevice::maxTxGain()
272{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200273 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100274}
275
276double LMSDevice::minTxGain()
277{
278 return 0.0;
279}
280
281double LMSDevice::maxRxGain()
282{
Pau Espin Pedrol587916e2018-05-08 18:46:28 +0200283 return 73.0;
Harald Welte940738e2018-03-07 07:50:57 +0100284}
285
286double LMSDevice::minRxGain()
287{
288 return 0.0;
289}
290
291double LMSDevice::setTxGain(double dB, size_t chan)
292{
293 if (chan) {
294 LOG(ALERT) << "Invalid channel " << chan;
295 return 0.0;
296 }
297
298 if (dB > maxTxGain())
299 dB = maxTxGain();
300 if (dB < minTxGain())
301 dB = minTxGain();
302
303 LOG(NOTICE) << "Setting TX gain to " << dB << " dB.";
304
305 if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
306 LOG(ERR) << "Error setting TX gain";
307
308 return dB;
309}
310
311double LMSDevice::setRxGain(double dB, size_t chan)
312{
313 if (chan) {
314 LOG(ALERT) << "Invalid channel " << chan;
315 return 0.0;
316 }
317
318 dB = 47.0;
319
320 if (dB > maxRxGain())
321 dB = maxRxGain();
322 if (dB < minRxGain())
323 dB = minRxGain();
324
325 LOG(NOTICE) << "Setting RX gain to " << dB << " dB.";
326
327 if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
328 LOG(ERR) << "Error setting RX gain";
329
330 return dB;
331}
332
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200333int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
Harald Welte940738e2018-03-07 07:50:57 +0100334{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200335 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
336 const char* c_name = name.c_str();
Harald Welte940738e2018-03-07 07:50:57 +0100337 int num_names;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200338 int i;
339
340 num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
Harald Welte940738e2018-03-07 07:50:57 +0100341 for (i = 0; i < num_names; i++) {
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200342 if (!strcmp(c_name, name_list[i]))
Harald Welte940738e2018-03-07 07:50:57 +0100343 return i;
344 }
345 return -1;
346}
347
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200348bool LMSDevice::flush_recv(size_t num_pkts)
349{
350 #define CHUNK 625
351 int len = CHUNK * sps;
352 short *buffer = new short[len * 2];
353 int rc;
354 lms_stream_meta_t rx_metadata = {};
355 rx_metadata.flushPartialPacket = false;
356 rx_metadata.waitForTimestamp = false;
357
358 ts_initial = 0;
359
360 while (!ts_initial || (num_pkts-- > 0)) {
361 rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
362 LOG(DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
363 if (rc != len) {
364 LOG(ALERT) << "LMS: Device receive timed out";
365 delete[] buffer;
366 return false;
367 }
368
Harald Welte68f05412018-04-28 21:58:03 +0200369 ts_initial = rx_metadata.timestamp + len;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200370 }
371
372 LOG(INFO) << "Initial timestamp " << ts_initial << std::endl;
373 delete[] buffer;
374 return true;
375}
376
Harald Welte940738e2018-03-07 07:50:57 +0100377bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
378{
379 int idx;
380
381 if (chan >= rx_paths.size()) {
382 LOG(ALERT) << "Requested non-existent channel " << chan;
383 return false;
384 }
385
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200386 idx = get_ant_idx(ant, LMS_CH_RX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100387 if (idx < 0) {
388 LOG(ALERT) << "Invalid Rx Antenna";
389 return false;
390 }
391
392 if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
393 LOG(ALERT) << "Unable to set Rx Antenna";
394 }
395
396 return true;
397}
398
399std::string LMSDevice::getRxAntenna(size_t chan)
400{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200401 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
402 int idx;
403
Harald Welte940738e2018-03-07 07:50:57 +0100404 if (chan >= rx_paths.size()) {
405 LOG(ALERT) << "Requested non-existent channel " << chan;
406 return "";
407 }
408
409 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
410 if (idx < 0) {
411 LOG(ALERT) << "Error getting Rx Antenna";
412 return "";
413 }
414
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200415 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100416 LOG(ALERT) << "Error getting Rx Antenna List";
417 return "";
418 }
419
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200420 return name_list[idx];
Harald Welte940738e2018-03-07 07:50:57 +0100421}
422
423bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
424{
425 int idx;
426
427 if (chan >= tx_paths.size()) {
428 LOG(ALERT) << "Requested non-existent channel " << chan;
429 return false;
430 }
431
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200432 idx = get_ant_idx(ant, LMS_CH_TX, chan);
Harald Welte940738e2018-03-07 07:50:57 +0100433 if (idx < 0) {
434 LOG(ALERT) << "Invalid Rx Antenna";
435 return false;
436 }
437
438 if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
439 LOG(ALERT) << "Unable to set Rx Antenna";
440 }
441
442 return true;
443}
444
445std::string LMSDevice::getTxAntenna(size_t chan)
446{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200447 lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
Harald Welte940738e2018-03-07 07:50:57 +0100448 int idx;
449
450 if (chan >= tx_paths.size()) {
451 LOG(ALERT) << "Requested non-existent channel " << chan;
452 return "";
453 }
454
455 idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
456 if (idx < 0) {
457 LOG(ALERT) << "Error getting Tx Antenna";
458 return "";
459 }
460
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200461 if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
Harald Welte940738e2018-03-07 07:50:57 +0100462 LOG(ALERT) << "Error getting Tx Antenna List";
463 return "";
464 }
465
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200466 return name_list[idx];
467}
468
469bool LMSDevice::requiresRadioAlign()
470{
471 return false;
472}
473
474GSM::Time LMSDevice::minLatency() {
475 /* Empirical data from a handful of
476 relatively recent machines shows that the B100 will underrun when
477 the transmit threshold is reduced to a time of 6 and a half frames,
478 so we set a minimum 7 frame threshold. */
479 return GSM::Time(6,7);
Harald Welte940738e2018-03-07 07:50:57 +0100480}
481
482// NOTE: Assumes sequential reads
483int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
484 TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
485{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200486 int rc = 0;
487 unsigned int i;
488 lms_stream_status_t status;
489 lms_stream_meta_t rx_metadata = {};
490 rx_metadata.flushPartialPacket = false;
491 rx_metadata.waitForTimestamp = false;
492 /* Shift read time with respect to transmit clock */
493 timestamp += ts_offset;
494 rx_metadata.timestamp = 0;
Harald Welte940738e2018-03-07 07:50:57 +0100495
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200496 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100497 LOG(ALERT) << "Invalid channel combination " << bufs.size();
498 return -1;
499 }
500
Harald Welte940738e2018-03-07 07:50:57 +0100501 *overrun = false;
502 *underrun = false;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200503 for (i = 0; i<chans; i++) {
504 thread_enable_cancel(false);
505 rc = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len, &rx_metadata, 100);
Harald Welte79024862018-04-28 22:13:31 +0200506 if (timestamp != (TIMESTAMP)rx_metadata.timestamp)
507 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 +0200508 if (rc != len) {
509 LOG(ALERT) << "LMS: Device receive timed out";
510 }
Harald Welte940738e2018-03-07 07:50:57 +0100511
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200512 if (LMS_GetStreamStatus(&m_lms_stream_rx[i], &status) == 0) {
513 if (status.underrun > m_last_rx_underruns[i])
514 *underrun = true;
515 m_last_rx_underruns[i] = status.underrun;
Harald Welte940738e2018-03-07 07:50:57 +0100516
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200517 if (status.overrun > m_last_rx_overruns[i])
518 *overrun = true;
519 m_last_rx_overruns[i] = status.overrun;
520 }
521 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100522 }
523
524 samplesRead += rc;
525
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200526 if (((TIMESTAMP) rx_metadata.timestamp) < timestamp)
527 rc = 0;
528
Harald Welte940738e2018-03-07 07:50:57 +0100529 return rc;
530}
531
532int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
533 bool * underrun, unsigned long long timestamp,
534 bool isControl)
535{
Harald Welte940738e2018-03-07 07:50:57 +0100536 int rc;
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200537 unsigned int i;
538 lms_stream_status_t status;
539 lms_stream_meta_t tx_metadata = {};
540 tx_metadata.flushPartialPacket = false;
541 tx_metadata.waitForTimestamp = true;
542 tx_metadata.timestamp = timestamp;
Harald Welte940738e2018-03-07 07:50:57 +0100543
544 if (isControl) {
545 LOG(ERR) << "Control packets not supported";
546 return 0;
547 }
548
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200549 if (bufs.size() != chans) {
Harald Welte940738e2018-03-07 07:50:57 +0100550 LOG(ALERT) << "Invalid channel combination " << bufs.size();
551 return -1;
552 }
553
Harald Welte940738e2018-03-07 07:50:57 +0100554 *underrun = false;
555
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200556 for (i = 0; i<chans; i++) {
557 LOG(ALERT) << "chan "<< i << " send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
558 thread_enable_cancel(false);
559 rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
560 if (rc != len) {
561 LOG(ALERT) << "LMS: Device send timed out";
562 }
563
564 if (LMS_GetStreamStatus(&m_lms_stream_tx[i], &status) == 0) {
565 if (status.underrun > m_last_tx_underruns[i])
566 *underrun = true;
567 m_last_tx_underruns[i] = status.underrun;
568 }
569 thread_enable_cancel(true);
Harald Welte940738e2018-03-07 07:50:57 +0100570 }
571
572 samplesWritten += rc;
573
574 return rc;
575}
576
577bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
578{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200579 return true;
Harald Welte940738e2018-03-07 07:50:57 +0100580}
581
582bool LMSDevice::setTxFreq(double wFreq, size_t chan)
583{
584
585 if (chan) {
586 LOG(ALERT) << "Invalid channel " << chan;
587 return false;
588 }
589
590 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
591 LOG(ALERT) << "set Tx: " << wFreq << " failed!";
592 return false;
593 }
594
595 return true;
596}
597
598bool LMSDevice::setRxFreq(double wFreq, size_t chan)
599{
600 if (chan) {
601 LOG(ALERT) << "Invalid channel " << chan;
602 return false;
603 }
604
605 if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
606 LOG(ALERT) << "set Rx: " << wFreq << " failed!";
607 return false;
608 }
609
610 return true;
611}
612
613RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
614 InterfaceType iface, size_t chans, double offset,
615 const std::vector < std::string > &tx_paths,
616 const std::vector < std::string > &rx_paths)
617{
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +0200618 return new LMSDevice(tx_sps, chans);
Harald Welte940738e2018-03-07 07:50:57 +0100619}