2018-03-07 06:50:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 sysmocom - s.f.m.c. GmbH
|
2019-07-22 10:05:52 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0+
|
2018-03-07 06:50:57 +00:00
|
|
|
*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "Logger.h"
|
|
|
|
#include "Threads.h"
|
|
|
|
#include "LMSDevice.h"
|
2018-12-10 16:10:36 +00:00
|
|
|
#include "Utils.h"
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
#include <lime/LimeSuite.h>
|
|
|
|
|
2019-05-24 14:54:19 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "osmo_signal.h"
|
2018-04-25 10:17:10 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2019-05-24 14:54:19 +00:00
|
|
|
}
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
#define MAX_ANTENNA_LIST_SIZE 10
|
|
|
|
#define LMS_SAMPLE_RATE GSMRATE*32
|
|
|
|
#define GSM_CARRIER_BW 270000.0 /* 270kHz */
|
|
|
|
#define LMS_MIN_BW_SUPPORTED 2.5e6 /* 2.5mHz, minimum supported by LMS */
|
|
|
|
#define LMS_CALIBRATE_BW_HZ OSMO_MAX(GSM_CARRIER_BW, LMS_MIN_BW_SUPPORTED)
|
2019-05-03 14:15:06 +00:00
|
|
|
#define SAMPLE_BUF_SZ (1 << 20) /* Size of Rx timestamp based Ring buffer, in bytes */
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-06-13 21:21:57 +00:00
|
|
|
LMSDevice::LMSDevice(size_t tx_sps, size_t rx_sps, InterfaceType iface, size_t chans, double lo_offset,
|
2018-06-13 19:55:09 +00:00
|
|
|
const std::vector<std::string>& tx_paths,
|
|
|
|
const std::vector<std::string>& rx_paths):
|
2018-06-13 21:21:57 +00:00
|
|
|
RadioDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths),
|
|
|
|
m_lms_dev(NULL)
|
2018-03-07 06:50:57 +00:00
|
|
|
{
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "creating LMS device...";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
m_lms_stream_rx.resize(chans);
|
|
|
|
m_lms_stream_tx.resize(chans);
|
|
|
|
|
2019-05-03 14:15:06 +00:00
|
|
|
rx_buffers.resize(chans);
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-03 17:17:18 +00:00
|
|
|
LMSDevice::~LMSDevice()
|
|
|
|
{
|
2019-04-16 14:22:23 +00:00
|
|
|
unsigned int i;
|
2018-12-03 17:17:18 +00:00
|
|
|
LOGC(DDEV, INFO) << "Closing LMS device";
|
|
|
|
if (m_lms_dev) {
|
2019-04-16 14:22:23 +00:00
|
|
|
/* disable all channels */
|
|
|
|
for (i=0; i<chans; i++) {
|
|
|
|
LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, false);
|
|
|
|
LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, false);
|
|
|
|
}
|
2018-12-03 17:17:18 +00:00
|
|
|
LMS_Close(m_lms_dev);
|
|
|
|
m_lms_dev = NULL;
|
|
|
|
}
|
2019-05-03 14:15:06 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < rx_buffers.size(); i++)
|
|
|
|
delete rx_buffers[i];
|
2018-12-03 17:17:18 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
static void lms_log_callback(int lvl, const char *msg)
|
|
|
|
{
|
|
|
|
/* map lime specific log levels */
|
2018-04-25 10:17:10 +00:00
|
|
|
static const int lvl_map[5] = {
|
2018-03-07 06:50:57 +00:00
|
|
|
[0] = LOGL_FATAL,
|
2018-11-23 13:39:06 +00:00
|
|
|
[LMS_LOG_ERROR] = LOGL_ERROR,
|
|
|
|
[LMS_LOG_WARNING] = LOGL_NOTICE,
|
|
|
|
[LMS_LOG_INFO] = LOGL_INFO,
|
|
|
|
[LMS_LOG_DEBUG] = LOGL_DEBUG,
|
2018-03-07 06:50:57 +00:00
|
|
|
};
|
|
|
|
/* protect against future higher log level values (lower importance) */
|
2018-04-25 10:17:10 +00:00
|
|
|
if ((unsigned int) lvl >= ARRAY_SIZE(lvl_map))
|
2018-03-07 06:50:57 +00:00
|
|
|
lvl = ARRAY_SIZE(lvl_map)-1;
|
|
|
|
|
2018-11-23 13:39:51 +00:00
|
|
|
LOGLV(DLMS, lvl_map[lvl]) << msg;
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 17:00:30 +00:00
|
|
|
static void print_range(const char* name, lms_range_t *range)
|
|
|
|
{
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << name << ": Min=" << range->min << " Max=" << range->max
|
2018-05-29 17:00:30 +00:00
|
|
|
<< " Step=" << range->step;
|
|
|
|
}
|
|
|
|
|
2018-12-10 16:10:36 +00:00
|
|
|
/*! Find the device string that matches all filters from \a args.
|
|
|
|
* \param[in] info_list device addresses found by LMS_GetDeviceList()
|
|
|
|
* \param[in] count length of info_list
|
|
|
|
* \param[in] args dev-args value from osmo-trx.cfg, containing comma separated key=value pairs
|
|
|
|
* \return index of first matching device or -1 (no match) */
|
|
|
|
int info_list_find(lms_info_str_t* info_list, unsigned int count, const std::string &args)
|
|
|
|
{
|
|
|
|
unsigned int i, j;
|
|
|
|
vector<string> filters;
|
|
|
|
|
|
|
|
filters = comma_delimited_to_vector(args.c_str());
|
|
|
|
|
|
|
|
/* iterate over device addresses */
|
|
|
|
for (i=0; i < count; i++) {
|
|
|
|
/* check if all filters match */
|
|
|
|
bool match = true;
|
|
|
|
for (j=0; j < filters.size(); j++) {
|
|
|
|
if (!strstr(info_list[i], filters[j].c_str())) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
int LMSDevice::open(const std::string &args, int ref, bool swap_channels)
|
|
|
|
{
|
|
|
|
lms_info_str_t* info_list;
|
2019-04-16 15:10:13 +00:00
|
|
|
const lms_dev_info_t* device_info;
|
2019-04-16 14:35:53 +00:00
|
|
|
lms_range_t range_sr;
|
|
|
|
float_type sr_host, sr_rf;
|
2018-04-25 10:17:10 +00:00
|
|
|
unsigned int i, n;
|
2018-12-10 16:10:36 +00:00
|
|
|
int rc, dev_id;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Opening LMS device..";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
LMS_RegisterLogHandler(&lms_log_callback);
|
|
|
|
|
2018-06-13 21:32:42 +00:00
|
|
|
if ((n = LMS_GetDeviceList(NULL)) < 0)
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS_GetDeviceList(NULL) failed";
|
|
|
|
LOGC(DDEV, INFO) << "Devices found: " << n;
|
2018-06-13 21:32:42 +00:00
|
|
|
if (n < 1)
|
|
|
|
return -1;
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-06-13 21:32:42 +00:00
|
|
|
info_list = new lms_info_str_t[n];
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-06-13 21:32:42 +00:00
|
|
|
if (LMS_GetDeviceList(info_list) < 0)
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS_GetDeviceList(info_list) failed";
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-06-13 21:32:42 +00:00
|
|
|
for (i = 0; i < n; i++)
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Device [" << i << "]: " << info_list[i];
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-12-10 16:10:36 +00:00
|
|
|
dev_id = info_list_find(info_list, n, args);
|
|
|
|
if (dev_id == -1) {
|
|
|
|
LOGC(DDEV, ERROR) << "No LMS device found with address '" << args << "'";
|
|
|
|
delete[] info_list;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGC(DDEV, INFO) << "Using device[" << dev_id << "]";
|
|
|
|
rc = LMS_Open(&m_lms_dev, info_list[dev_id], NULL);
|
2018-04-25 10:17:10 +00:00
|
|
|
if (rc != 0) {
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS_GetDeviceList() failed)";
|
2018-04-25 10:17:10 +00:00
|
|
|
delete [] info_list;
|
2018-03-07 06:50:57 +00:00
|
|
|
return -1;
|
2018-04-25 10:17:10 +00:00
|
|
|
}
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
delete [] info_list;
|
|
|
|
|
2019-04-16 15:10:13 +00:00
|
|
|
device_info = LMS_GetDeviceInfo(m_lms_dev);
|
|
|
|
|
|
|
|
if ((ref != REF_EXTERNAL) && (ref != REF_INTERNAL)){
|
|
|
|
LOGC(DDEV, ERROR) << "Invalid reference type";
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if reference clock is external setup must happen _before_ calling LMS_Init */
|
|
|
|
/* FIXME make external reference frequency configurable */
|
|
|
|
if (ref == REF_EXTERNAL) {
|
|
|
|
LOGC(DDEV, INFO) << "Setting External clock reference to 10MHz";
|
|
|
|
/* Assume an external 10 MHz reference clock */
|
|
|
|
if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, 10000000.0) < 0)
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Init LMS device";
|
2018-04-28 19:38:58 +00:00
|
|
|
if (LMS_Init(m_lms_dev) != 0) {
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS_Init() failed";
|
2018-12-04 11:42:30 +00:00
|
|
|
goto out_close;
|
2018-04-28 19:38:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:10:13 +00:00
|
|
|
/* LimeSDR-Mini does not have switches but needs soldering to select external/internal clock */
|
|
|
|
/* LimeNET-Micro also does not like selecting internal clock*/
|
|
|
|
/* also set device specific maximum tx levels selected by phasenoise measurements*/
|
|
|
|
if (strncmp(device_info->deviceName,"LimeSDR-USB",11) == 0){
|
|
|
|
/* if reference clock is internal setup must happen _after_ calling LMS_Init */
|
|
|
|
/* according to lms using LMS_CLOCK_EXTREF with a frequency <= 0 is the correct way to set clock to internal reference*/
|
|
|
|
if (ref == REF_INTERNAL) {
|
|
|
|
LOGC(DDEV, INFO) << "Setting Internal clock reference";
|
|
|
|
if (LMS_SetClockFreq(m_lms_dev, LMS_CLOCK_EXTREF, -1) < 0)
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
maxTxGainClamp = 73.0;
|
|
|
|
} else if (strncmp(device_info->deviceName,"LimeSDR-Mini",12) == 0)
|
|
|
|
maxTxGainClamp = 66.0;
|
|
|
|
else
|
|
|
|
maxTxGainClamp = 71.0; /* "LimeNET-Micro", etc FIXME pciE based LMS boards?*/
|
|
|
|
|
2019-04-16 14:22:23 +00:00
|
|
|
/* enable all used channels */
|
|
|
|
for (i=0; i<chans; i++) {
|
|
|
|
if (LMS_EnableChannel(m_lms_dev, LMS_CH_RX, i, true) < 0)
|
|
|
|
goto out_close;
|
|
|
|
if (LMS_EnableChannel(m_lms_dev, LMS_CH_TX, i, true) < 0)
|
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2019-04-16 17:04:37 +00:00
|
|
|
/* set samplerate */
|
2018-05-29 17:00:30 +00:00
|
|
|
if (LMS_GetSampleRateRange(m_lms_dev, LMS_CH_RX, &range_sr))
|
2018-04-28 19:41:07 +00:00
|
|
|
goto out_close;
|
2018-05-29 17:00:30 +00:00
|
|
|
print_range("Sample Rate", &range_sr);
|
2018-04-28 19:41:07 +00:00
|
|
|
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Setting sample rate to " << GSMRATE*tx_sps << " " << tx_sps;
|
2018-06-13 20:47:48 +00:00
|
|
|
if (LMS_SetSampleRate(m_lms_dev, GSMRATE*tx_sps, 32) < 0)
|
2018-03-07 06:50:57 +00:00
|
|
|
goto out_close;
|
2018-05-29 17:00:30 +00:00
|
|
|
|
2018-04-28 19:41:07 +00:00
|
|
|
if (LMS_GetSampleRate(m_lms_dev, LMS_CH_RX, 0, &sr_host, &sr_rf))
|
|
|
|
goto out_close;
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Sample Rate: Host=" << sr_host << " RF=" << sr_rf;
|
2018-05-29 17:00:30 +00:00
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
/* FIXME: make this device/model dependent, like UHDDevice:dev_param_map! */
|
2018-06-13 20:47:48 +00:00
|
|
|
ts_offset = static_cast<TIMESTAMP>(8.9e-5 * GSMRATE * tx_sps); /* time * sample_rate */
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2019-04-16 17:04:37 +00:00
|
|
|
/* configure antennas */
|
2018-06-13 19:56:24 +00:00
|
|
|
if (!set_antennas()) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, FATAL) << "LMS antenna setting failed";
|
2019-04-16 17:04:37 +00:00
|
|
|
goto out_close;
|
2018-06-13 19:56:24 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 14:15:06 +00:00
|
|
|
/* Set up per-channel Rx timestamp based Ring buffers */
|
|
|
|
for (size_t i = 0; i < rx_buffers.size(); i++)
|
|
|
|
rx_buffers[i] = new smpl_buf(SAMPLE_BUF_SZ / sizeof(uint32_t));
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
started = false;
|
|
|
|
|
|
|
|
return NORMAL;
|
|
|
|
|
|
|
|
out_close:
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, FATAL) << "Error in LMS open, closing: " << LMS_GetLastErrorMessage();
|
2018-03-07 06:50:57 +00:00
|
|
|
LMS_Close(m_lms_dev);
|
2018-12-03 17:17:18 +00:00
|
|
|
m_lms_dev = NULL;
|
2018-03-07 06:50:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::start()
|
|
|
|
{
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "starting LMS...";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
unsigned int i;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-12-03 10:19:52 +00:00
|
|
|
if (started) {
|
|
|
|
LOGC(DDEV, ERR) << "Device already started";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-11 22:29:16 +00:00
|
|
|
/* configure the channels/streams */
|
2018-04-25 10:17:10 +00:00
|
|
|
for (i=0; i<chans; i++) {
|
2019-04-16 15:10:13 +00:00
|
|
|
/* Set gains for calibration/filter setup */
|
|
|
|
/* TX gain to maximum */
|
|
|
|
setTxGain(maxTxGain(), i);
|
|
|
|
/* RX gain to midpoint */
|
2018-11-26 18:26:52 +00:00
|
|
|
setRxGain((minRxGain() + maxRxGain()) / 2, i);
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2019-04-16 14:35:53 +00:00
|
|
|
/* set up Rx and Tx filters */
|
|
|
|
if (!do_filters(i))
|
|
|
|
return false;
|
|
|
|
/* Perform Rx and Tx calibration */
|
|
|
|
if (!do_calib(i))
|
|
|
|
return false;
|
|
|
|
|
2019-04-16 17:04:37 +00:00
|
|
|
/* configure Streams */
|
2018-04-25 10:17:10 +00:00
|
|
|
m_lms_stream_rx[i] = {};
|
|
|
|
m_lms_stream_rx[i].isTx = false;
|
|
|
|
m_lms_stream_rx[i].channel = i;
|
|
|
|
m_lms_stream_rx[i].fifoSize = 1024 * 1024;
|
|
|
|
m_lms_stream_rx[i].throughputVsLatency = 0.3;
|
|
|
|
m_lms_stream_rx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
m_lms_stream_tx[i] = {};
|
|
|
|
m_lms_stream_tx[i].isTx = true;
|
|
|
|
m_lms_stream_tx[i].channel = i;
|
|
|
|
m_lms_stream_tx[i].fifoSize = 1024 * 1024;
|
|
|
|
m_lms_stream_tx[i].throughputVsLatency = 0.3;
|
|
|
|
m_lms_stream_tx[i].dataFmt = lms_stream_t::LMS_FMT_I16;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (LMS_SetupStream(m_lms_dev, &m_lms_stream_rx[i]) < 0)
|
|
|
|
return false;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (LMS_SetupStream(m_lms_dev, &m_lms_stream_tx[i]) < 0)
|
|
|
|
return false;
|
2018-06-11 22:29:16 +00:00
|
|
|
}
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2018-06-11 22:29:16 +00:00
|
|
|
/* now start the streams in a second loop, as we can no longer call
|
|
|
|
* LMS_SetupStream() after LMS_StartStream() of the first stream */
|
|
|
|
for (i = 0; i < chans; i++) {
|
2018-04-25 10:17:10 +00:00
|
|
|
if (LMS_StartStream(&m_lms_stream_rx[i]) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (LMS_StartStream(&m_lms_stream_tx[i]) < 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_recv(10);
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
started = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::stop()
|
|
|
|
{
|
2018-04-25 10:17:10 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
if (!started)
|
|
|
|
return true;
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
for (i=0; i<chans; i++) {
|
|
|
|
LMS_StopStream(&m_lms_stream_tx[i]);
|
|
|
|
LMS_StopStream(&m_lms_stream_rx[i]);
|
2018-12-03 10:34:23 +00:00
|
|
|
}
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-12-03 10:34:23 +00:00
|
|
|
for (i=0; i<chans; i++) {
|
|
|
|
LMS_DestroyStream(m_lms_dev, &m_lms_stream_tx[i]);
|
|
|
|
LMS_DestroyStream(m_lms_dev, &m_lms_stream_rx[i]);
|
2018-04-25 10:17:10 +00:00
|
|
|
}
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-12-03 10:19:52 +00:00
|
|
|
started = false;
|
2018-03-07 06:50:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:35:53 +00:00
|
|
|
/* do rx/tx calibration - depends on gain, freq and bw */
|
|
|
|
bool LMSDevice::do_calib(size_t chan)
|
|
|
|
{
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, INFO) << "Calibrating";
|
2019-04-16 14:35:53 +00:00
|
|
|
if (LMS_Calibrate(m_lms_dev, LMS_CH_RX, chan, LMS_CALIBRATE_BW_HZ, 0) < 0)
|
|
|
|
return false;
|
|
|
|
if (LMS_Calibrate(m_lms_dev, LMS_CH_TX, chan, LMS_CALIBRATE_BW_HZ, 0) < 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do rx/tx filter config - depends on bw only? */
|
|
|
|
bool LMSDevice::do_filters(size_t chan)
|
|
|
|
{
|
|
|
|
lms_range_t range_lpfbw_rx, range_lpfbw_tx;
|
|
|
|
float_type lpfbw_rx, lpfbw_tx;
|
|
|
|
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, INFO) << "Setting filters";
|
2019-04-16 14:35:53 +00:00
|
|
|
if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_rx))
|
|
|
|
return false;
|
|
|
|
print_range("LPFBWRange Rx", &range_lpfbw_rx);
|
|
|
|
if (LMS_GetLPFBWRange(m_lms_dev, LMS_CH_RX, &range_lpfbw_tx))
|
|
|
|
return false;
|
|
|
|
print_range("LPFBWRange Tx", &range_lpfbw_tx);
|
|
|
|
|
|
|
|
lpfbw_rx = OSMO_MIN(OSMO_MAX(1.4001e6, range_lpfbw_rx.min), range_lpfbw_rx.max);
|
|
|
|
lpfbw_tx = OSMO_MIN(OSMO_MAX(5.2e6, range_lpfbw_tx.min), range_lpfbw_tx.max);
|
|
|
|
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, INFO) << "LPFBW: Rx=" << lpfbw_rx << " Tx=" << lpfbw_tx;
|
2019-04-16 14:35:53 +00:00
|
|
|
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, INFO) << "Setting LPFBW";
|
2019-04-16 14:35:53 +00:00
|
|
|
if (LMS_SetLPFBW(m_lms_dev, LMS_CH_RX, chan, lpfbw_rx) < 0)
|
|
|
|
return false;
|
|
|
|
if (LMS_SetLPFBW(m_lms_dev, LMS_CH_TX, chan, lpfbw_tx) < 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
double LMSDevice::maxTxGain()
|
|
|
|
{
|
2019-04-16 15:10:13 +00:00
|
|
|
return maxTxGainClamp;
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double LMSDevice::minTxGain()
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double LMSDevice::maxRxGain()
|
|
|
|
{
|
2018-05-08 16:46:28 +00:00
|
|
|
return 73.0;
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double LMSDevice::minRxGain()
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double LMSDevice::setTxGain(double dB, size_t chan)
|
|
|
|
{
|
|
|
|
if (dB > maxTxGain())
|
|
|
|
dB = maxTxGain();
|
|
|
|
if (dB < minTxGain())
|
|
|
|
dB = minTxGain();
|
|
|
|
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, NOTICE) << "Setting TX gain to " << dB << " dB";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
if (LMS_SetGaindB(m_lms_dev, LMS_CH_TX, chan, dB) < 0)
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERR) << "Error setting TX gain to " << dB << " dB";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
return dB;
|
|
|
|
}
|
|
|
|
|
|
|
|
double LMSDevice::setRxGain(double dB, size_t chan)
|
|
|
|
{
|
|
|
|
if (dB > maxRxGain())
|
|
|
|
dB = maxRxGain();
|
|
|
|
if (dB < minRxGain())
|
|
|
|
dB = minRxGain();
|
|
|
|
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, NOTICE) << "Setting RX gain to " << dB << " dB";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
if (LMS_SetGaindB(m_lms_dev, LMS_CH_RX, chan, dB) < 0)
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERR) << "Error setting RX gain to " << dB << " dB";
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
return dB;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
int LMSDevice::get_ant_idx(const std::string & name, bool dir_tx, size_t chan)
|
2018-03-07 06:50:57 +00:00
|
|
|
{
|
2018-04-25 10:17:10 +00:00
|
|
|
lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
|
|
|
|
const char* c_name = name.c_str();
|
2018-03-07 06:50:57 +00:00
|
|
|
int num_names;
|
2018-04-25 10:17:10 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
num_names = LMS_GetAntennaList(m_lms_dev, dir_tx, chan, name_list);
|
2018-03-07 06:50:57 +00:00
|
|
|
for (i = 0; i < num_names; i++) {
|
2018-04-25 10:17:10 +00:00
|
|
|
if (!strcmp(c_name, name_list[i]))
|
2018-03-07 06:50:57 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
bool LMSDevice::flush_recv(size_t num_pkts)
|
|
|
|
{
|
|
|
|
#define CHUNK 625
|
2018-06-13 20:47:48 +00:00
|
|
|
int len = CHUNK * tx_sps;
|
2019-04-25 17:22:07 +00:00
|
|
|
short *buffer = (short*) alloca(sizeof(short) * len * 2);
|
2018-04-25 10:17:10 +00:00
|
|
|
int rc;
|
|
|
|
lms_stream_meta_t rx_metadata = {};
|
|
|
|
rx_metadata.flushPartialPacket = false;
|
|
|
|
rx_metadata.waitForTimestamp = false;
|
|
|
|
|
|
|
|
ts_initial = 0;
|
|
|
|
|
|
|
|
while (!ts_initial || (num_pkts-- > 0)) {
|
|
|
|
rc = LMS_RecvStream(&m_lms_stream_rx[0], &buffer[0], len, &rx_metadata, 100);
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, DEBUG) << "Flush: Recv buffer of len " << rc << " at " << std::hex << rx_metadata.timestamp;
|
2018-04-25 10:17:10 +00:00
|
|
|
if (rc != len) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Flush: Device receive timed out";
|
2018-04-25 10:17:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:03 +00:00
|
|
|
ts_initial = rx_metadata.timestamp + len;
|
2018-04-25 10:17:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, INFO) << "Initial timestamp " << ts_initial << std::endl;
|
2018-04-25 10:17:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
bool LMSDevice::setRxAntenna(const std::string & ant, size_t chan)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (chan >= rx_paths.size()) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Requested non-existent channel " << chan;
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
idx = get_ant_idx(ant, LMS_CH_RX, chan);
|
2018-03-07 06:50:57 +00:00
|
|
|
if (idx < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Invalid Rx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LMS_SetAntenna(m_lms_dev, LMS_CH_RX, chan, idx) < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Unable to set Rx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LMSDevice::getRxAntenna(size_t chan)
|
|
|
|
{
|
2018-04-25 10:17:10 +00:00
|
|
|
lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
|
|
|
|
int idx;
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
if (chan >= rx_paths.size()) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Requested non-existent channel " << chan;
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = LMS_GetAntenna(m_lms_dev, LMS_CH_RX, chan);
|
|
|
|
if (idx < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error getting Rx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (LMS_GetAntennaList(m_lms_dev, LMS_CH_RX, chan, name_list) < idx) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error getting Rx Antenna List";
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
return name_list[idx];
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::setTxAntenna(const std::string & ant, size_t chan)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (chan >= tx_paths.size()) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Requested non-existent channel " << chan;
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
idx = get_ant_idx(ant, LMS_CH_TX, chan);
|
2018-03-07 06:50:57 +00:00
|
|
|
if (idx < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Invalid Rx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LMS_SetAntenna(m_lms_dev, LMS_CH_TX, chan, idx) < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Unable to set Rx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LMSDevice::getTxAntenna(size_t chan)
|
|
|
|
{
|
2018-04-25 10:17:10 +00:00
|
|
|
lms_name_t name_list[MAX_ANTENNA_LIST_SIZE]; /* large enough list for antenna names. */
|
2018-03-07 06:50:57 +00:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (chan >= tx_paths.size()) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Requested non-existent channel " << chan;
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = LMS_GetAntenna(m_lms_dev, LMS_CH_TX, chan);
|
|
|
|
if (idx < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error getting Tx Antenna";
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (LMS_GetAntennaList(m_lms_dev, LMS_CH_TX, chan, name_list) < idx) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error getting Tx Antenna List";
|
2018-03-07 06:50:57 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
return name_list[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::requiresRadioAlign()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSM::Time LMSDevice::minLatency() {
|
2019-04-16 14:13:02 +00:00
|
|
|
/* UNUSED on limesdr (only used on usrp1/2) */
|
|
|
|
return GSM::Time(0,0);
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
2019-07-29 18:11:25 +00:00
|
|
|
/*!
|
|
|
|
* Issue tracking description of several events: https://github.com/myriadrf/LimeSuite/issues/265
|
|
|
|
*/
|
|
|
|
void LMSDevice::update_stream_stats_rx(size_t chan, bool *overrun)
|
2018-12-04 19:56:32 +00:00
|
|
|
{
|
|
|
|
lms_stream_status_t status;
|
2019-05-24 14:54:19 +00:00
|
|
|
bool changed = false;
|
2018-12-04 19:56:32 +00:00
|
|
|
|
2019-05-24 14:54:19 +00:00
|
|
|
if (LMS_GetStreamStatus(&m_lms_stream_rx[chan], &status) != 0) {
|
2019-07-29 18:11:25 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Rx LMS_GetStreamStatus failed";
|
2019-05-24 14:54:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:11:25 +00:00
|
|
|
/* FIFO overrun is counted when Rx FIFO is full but new data comes from
|
|
|
|
the board and oldest samples in FIFO are overwritte. Value count
|
|
|
|
since the last call to LMS_GetStreamStatus(stream). */
|
2019-06-04 14:32:25 +00:00
|
|
|
if (status.overrun) {
|
2019-05-24 14:54:19 +00:00
|
|
|
changed = true;
|
|
|
|
*overrun = true;
|
2019-07-29 18:11:25 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Rx Overrun! ("
|
2019-05-24 14:54:19 +00:00
|
|
|
<< m_ctr[chan].rx_overruns << " -> "
|
|
|
|
<< status.overrun << ")";
|
|
|
|
}
|
2019-06-04 14:32:25 +00:00
|
|
|
m_ctr[chan].rx_overruns += status.overrun;
|
2019-05-24 14:54:19 +00:00
|
|
|
|
2019-07-29 18:11:25 +00:00
|
|
|
/* Dropped packets in Rx are counted when gaps in Rx timestamps are
|
|
|
|
detected (likely because buffer oveflow in hardware). Value count
|
|
|
|
since the last call to LMS_GetStreamStatus(stream). */
|
2019-05-24 14:54:19 +00:00
|
|
|
if (status.droppedPackets) {
|
|
|
|
changed = true;
|
2019-07-29 18:11:25 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Rx Dropped packets by HW! ("
|
2019-05-24 14:54:19 +00:00
|
|
|
<< m_ctr[chan].rx_dropped_samples << " -> "
|
|
|
|
<< m_ctr[chan].rx_dropped_samples +
|
|
|
|
status.droppedPackets
|
|
|
|
<< ")";
|
|
|
|
m_ctr[chan].rx_dropped_events++;
|
|
|
|
}
|
|
|
|
m_ctr[chan].rx_dropped_samples += status.droppedPackets;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
osmo_signal_dispatch(SS_DEVICE, S_DEVICE_COUNTER_CHANGE, &m_ctr[chan]);
|
|
|
|
|
2018-12-04 19:56:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
// NOTE: Assumes sequential reads
|
|
|
|
int LMSDevice::readSamples(std::vector < short *>&bufs, int len, bool * overrun,
|
|
|
|
TIMESTAMP timestamp, bool * underrun, unsigned *RSSI)
|
|
|
|
{
|
2019-05-03 14:15:06 +00:00
|
|
|
int rc, num_smpls, expect_smpls;
|
|
|
|
ssize_t avail_smpls;
|
|
|
|
TIMESTAMP expect_timestamp;
|
2018-04-25 10:17:10 +00:00
|
|
|
unsigned int i;
|
|
|
|
lms_stream_meta_t rx_metadata = {};
|
|
|
|
rx_metadata.flushPartialPacket = false;
|
|
|
|
rx_metadata.waitForTimestamp = false;
|
|
|
|
rx_metadata.timestamp = 0;
|
2018-03-07 06:50:57 +00:00
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (bufs.size() != chans) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Invalid channel combination " << bufs.size();
|
2018-03-07 06:50:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*overrun = false;
|
|
|
|
*underrun = false;
|
2019-05-03 14:15:06 +00:00
|
|
|
|
|
|
|
/* Check that timestamp is valid */
|
|
|
|
rc = rx_buffers[0]->avail_smpls(timestamp);
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGC(DDEV, ERROR) << rx_buffers[0]->str_code(rc);
|
|
|
|
LOGC(DDEV, ERROR) << rx_buffers[0]->str_status(timestamp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
for (i = 0; i<chans; i++) {
|
2019-05-03 14:15:06 +00:00
|
|
|
/* Receive samples from HW until we have enough */
|
|
|
|
while ((avail_smpls = rx_buffers[i]->avail_smpls(timestamp)) < len) {
|
|
|
|
thread_enable_cancel(false);
|
|
|
|
num_smpls = LMS_RecvStream(&m_lms_stream_rx[i], bufs[i], len - avail_smpls, &rx_metadata, 100);
|
2019-07-29 18:11:25 +00:00
|
|
|
update_stream_stats_rx(i, overrun);
|
2018-09-03 14:46:34 +00:00
|
|
|
thread_enable_cancel(true);
|
2019-05-03 14:15:06 +00:00
|
|
|
if (num_smpls <= 0) {
|
|
|
|
LOGCHAN(i, DDEV, ERROR) << "Device receive timed out (" << rc << " vs exp " << len << ").";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGCHAN(i, DDEV, DEBUG) "Received timestamp = " << (TIMESTAMP)rx_metadata.timestamp << " (" << num_smpls << ")";
|
|
|
|
|
|
|
|
expect_smpls = len - avail_smpls;
|
|
|
|
if (expect_smpls != num_smpls)
|
|
|
|
LOGCHAN(i, DDEV, NOTICE) << "Unexpected recv buffer len: expect "
|
|
|
|
<< expect_smpls << " got " << num_smpls
|
|
|
|
<< ", diff=" << expect_smpls - num_smpls;
|
|
|
|
|
|
|
|
expect_timestamp = timestamp + avail_smpls;
|
|
|
|
if (expect_timestamp != (TIMESTAMP)rx_metadata.timestamp)
|
|
|
|
LOGCHAN(i, DDEV, ERROR) << "Unexpected recv buffer timestamp: expect "
|
|
|
|
<< expect_timestamp << " got " << (TIMESTAMP)rx_metadata.timestamp
|
|
|
|
<< ", diff=" << rx_metadata.timestamp - expect_timestamp;
|
|
|
|
|
|
|
|
rc = rx_buffers[i]->write(bufs[i], num_smpls, (TIMESTAMP)rx_metadata.timestamp);
|
|
|
|
if (rc < 0) {
|
|
|
|
LOGCHAN(i, DDEV, ERROR) << rx_buffers[i]->str_code(rc);
|
|
|
|
LOGCHAN(i, DDEV, ERROR) << rx_buffers[i]->str_status(timestamp);
|
|
|
|
if (rc != smpl_buf::ERROR_OVERFLOW)
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-25 10:17:10 +00:00
|
|
|
}
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 14:15:06 +00:00
|
|
|
/* We have enough samples */
|
|
|
|
for (size_t i = 0; i < rx_buffers.size(); i++) {
|
|
|
|
rc = rx_buffers[i]->read(bufs[i], len, timestamp);
|
|
|
|
if ((rc < 0) || (rc != len)) {
|
|
|
|
LOGC(DDEV, ERROR) << rx_buffers[i]->str_code(rc);
|
|
|
|
LOGC(DDEV, ERROR) << rx_buffers[i]->str_status(timestamp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 10:17:10 +00:00
|
|
|
|
2019-05-03 14:15:06 +00:00
|
|
|
return len;
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 18:11:25 +00:00
|
|
|
void LMSDevice::update_stream_stats_tx(size_t chan, bool *underrun)
|
|
|
|
{
|
|
|
|
lms_stream_status_t status;
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
if (LMS_GetStreamStatus(&m_lms_stream_tx[chan], &status) != 0) {
|
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Tx LMS_GetStreamStatus failed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIFO underrun is counted when Tx is running but FIFO is empty for
|
|
|
|
>100 ms (500ms in older versions). Value count since the last call to
|
|
|
|
LMS_GetStreamStatus(stream). */
|
|
|
|
if (status.underrun) {
|
|
|
|
changed = true;
|
|
|
|
*underrun = true;
|
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Tx Underrun! ("
|
|
|
|
<< m_ctr[chan].tx_underruns << " -> "
|
|
|
|
<< status.underrun << ")";
|
|
|
|
}
|
|
|
|
m_ctr[chan].tx_underruns += status.underrun;
|
|
|
|
|
|
|
|
/* Dropped packets in Tx are counted only when timestamps are enabled
|
|
|
|
and SDR drops packet because of late timestamp. Value count since the
|
|
|
|
last call to LMS_GetStreamStatus(stream). */
|
|
|
|
if (status.droppedPackets) {
|
|
|
|
changed = true;
|
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Tx Dropped packets by HW! ("
|
|
|
|
<< m_ctr[chan].tx_dropped_samples << " -> "
|
|
|
|
<< m_ctr[chan].tx_dropped_samples +
|
|
|
|
status.droppedPackets
|
|
|
|
<< ")";
|
|
|
|
m_ctr[chan].tx_dropped_events++;
|
|
|
|
}
|
|
|
|
m_ctr[chan].tx_dropped_samples += status.droppedPackets;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
osmo_signal_dispatch(SS_DEVICE, S_DEVICE_COUNTER_CHANGE, &m_ctr[chan]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
int LMSDevice::writeSamples(std::vector < short *>&bufs, int len,
|
|
|
|
bool * underrun, unsigned long long timestamp,
|
|
|
|
bool isControl)
|
|
|
|
{
|
2018-09-20 09:17:16 +00:00
|
|
|
int rc = 0;
|
2018-04-25 10:17:10 +00:00
|
|
|
unsigned int i;
|
|
|
|
lms_stream_meta_t tx_metadata = {};
|
|
|
|
tx_metadata.flushPartialPacket = false;
|
|
|
|
tx_metadata.waitForTimestamp = true;
|
2018-06-11 22:27:09 +00:00
|
|
|
tx_metadata.timestamp = timestamp - ts_offset; /* Shift Tx time by offset */
|
2018-03-07 06:50:57 +00:00
|
|
|
|
|
|
|
if (isControl) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Control packets not supported";
|
2018-03-07 06:50:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
if (bufs.size() != chans) {
|
2019-04-25 17:52:49 +00:00
|
|
|
LOGC(DDEV, ERROR) << "Invalid channel combination " << bufs.size();
|
2018-03-07 06:50:57 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*underrun = false;
|
|
|
|
|
2018-04-25 10:17:10 +00:00
|
|
|
for (i = 0; i<chans; i++) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(i, DDEV, DEBUG) << "send buffer of len " << len << " timestamp " << std::hex << tx_metadata.timestamp;
|
2018-04-25 10:17:10 +00:00
|
|
|
thread_enable_cancel(false);
|
|
|
|
rc = LMS_SendStream(&m_lms_stream_tx[i], bufs[i], len, &tx_metadata, 100);
|
2019-07-29 18:11:25 +00:00
|
|
|
update_stream_stats_tx(i, underrun);
|
|
|
|
thread_enable_cancel(true);
|
2018-04-25 10:17:10 +00:00
|
|
|
if (rc != len) {
|
2019-07-29 18:11:25 +00:00
|
|
|
LOGCHAN(i, DDEV, ERROR) << "LMS: Device Tx timed out (" << rc << " vs exp " << len << ").";
|
|
|
|
return -1;
|
2018-04-25 10:17:10 +00:00
|
|
|
}
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::updateAlignment(TIMESTAMP timestamp)
|
|
|
|
{
|
2018-04-25 10:17:10 +00:00
|
|
|
return true;
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::setTxFreq(double wFreq, size_t chan)
|
|
|
|
{
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, NOTICE) << "Setting Tx Freq to " << wFreq << " Hz";
|
2018-12-04 13:09:53 +00:00
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_TX, chan, wFreq) < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error setting Tx Freq to " << wFreq << " Hz";
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LMSDevice::setRxFreq(double wFreq, size_t chan)
|
|
|
|
{
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, NOTICE) << "Setting Rx Freq to " << wFreq << " Hz";
|
2018-12-04 13:09:53 +00:00
|
|
|
|
2018-03-07 06:50:57 +00:00
|
|
|
if (LMS_SetLOFrequency(m_lms_dev, LMS_CH_RX, chan, wFreq) < 0) {
|
2019-05-03 17:40:00 +00:00
|
|
|
LOGCHAN(chan, DDEV, ERROR) << "Error setting Rx Freq to " << wFreq << " Hz";
|
2018-03-07 06:50:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioDevice *RadioDevice::make(size_t tx_sps, size_t rx_sps,
|
2018-06-13 21:21:57 +00:00
|
|
|
InterfaceType iface, size_t chans, double lo_offset,
|
2018-03-07 06:50:57 +00:00
|
|
|
const std::vector < std::string > &tx_paths,
|
|
|
|
const std::vector < std::string > &rx_paths)
|
|
|
|
{
|
2018-06-13 21:41:35 +00:00
|
|
|
if (tx_sps != rx_sps) {
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS Requires tx_sps == rx_sps";
|
2018-06-13 21:41:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (lo_offset != 0.0) {
|
2018-08-17 17:55:38 +00:00
|
|
|
LOGC(DDEV, ERROR) << "LMS doesn't support lo_offset";
|
2018-06-13 21:41:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-06-13 21:21:57 +00:00
|
|
|
return new LMSDevice(tx_sps, rx_sps, iface, chans, lo_offset, tx_paths, rx_paths);
|
2018-03-07 06:50:57 +00:00
|
|
|
}
|