2018-12-28 19:20:48 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2017 Edouard Griffiths, F4EXB //
|
|
|
|
// Copyright (C) 2017 Sergey Kostanbaev, Fairwaves Inc. //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// 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 General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <errno.h>
|
2018-12-30 19:43:24 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2018-12-28 19:20:48 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
#include "xtrx/devicextrx.h"
|
2018-12-28 19:20:48 -05:00
|
|
|
#include "xtrxinputsettings.h"
|
|
|
|
#include "xtrxinputthread.h"
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
XTRXInputThread::XTRXInputThread(struct xtrx_dev *dev, unsigned int nbChannels, unsigned int uniqueChannelIndex, QObject* parent) :
|
2018-12-28 19:20:48 -05:00
|
|
|
QThread(parent),
|
|
|
|
m_running(false),
|
2018-12-30 19:43:24 -05:00
|
|
|
m_dev(dev),
|
|
|
|
m_nbChannels(nbChannels),
|
|
|
|
m_uniqueChannelIndex(uniqueChannelIndex)
|
2018-12-28 19:20:48 -05:00
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
qDebug("XTRXInputThread::XTRXInputThread: nbChannels: %u uniqueChannelIndex: %u", nbChannels, uniqueChannelIndex);
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels = new Channel[2];
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 2; i++) {
|
|
|
|
m_channels[i].m_convertBuffer.resize(DeviceXTRX::blockSize, Sample{0,0});
|
|
|
|
}
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
XTRXInputThread::~XTRXInputThread()
|
|
|
|
{
|
2018-12-30 19:43:24 -05:00
|
|
|
qDebug("XTRXInputThread::~XTRXInputThread");
|
|
|
|
|
|
|
|
if (m_running) {
|
|
|
|
stopWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] m_channels;
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void XTRXInputThread::startWork()
|
|
|
|
{
|
2018-12-29 18:46:11 -05:00
|
|
|
if (m_running) {
|
|
|
|
return; // return if running already
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m_startWaitMutex.lock();
|
|
|
|
start();
|
2018-12-29 18:46:11 -05:00
|
|
|
|
|
|
|
while (!m_running) {
|
2018-12-28 19:20:48 -05:00
|
|
|
m_startWaiter.wait(&m_startWaitMutex, 100);
|
2018-12-29 18:46:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-28 19:20:48 -05:00
|
|
|
m_startWaitMutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XTRXInputThread::stopWork()
|
|
|
|
{
|
2018-12-29 18:46:11 -05:00
|
|
|
if (!m_running) {
|
|
|
|
return; // return if not running
|
|
|
|
}
|
2018-12-28 19:20:48 -05:00
|
|
|
|
|
|
|
m_running = false;
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void XTRXInputThread::run()
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
m_running = true;
|
|
|
|
m_startWaiter.wakeAll();
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
unsigned int nbFifos = getNbFifos();
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-31 04:43:11 -05:00
|
|
|
if ((m_nbChannels != 0) && (nbFifos != 0))
|
2018-12-30 19:43:24 -05:00
|
|
|
{
|
|
|
|
xtrx_run_params params;
|
|
|
|
xtrx_run_params_init(¶ms);
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
params.dir = XTRX_RX;
|
|
|
|
params.rx.chs = XTRX_CH_AB;
|
|
|
|
params.rx.wfmt = XTRX_WF_16;
|
|
|
|
params.rx.hfmt = XTRX_IQ_INT16;
|
2018-12-31 14:22:42 -05:00
|
|
|
params.rx_stream_start = 2*DeviceXTRX::blockSize; // was 2*8192
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
if (m_nbChannels == 1)
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
qDebug("XTRXInputThread::run: SI mode for channel #%u", m_uniqueChannelIndex);
|
2018-12-30 19:43:24 -05:00
|
|
|
params.rx.flags |= XTRX_RSP_SISO_MODE;
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
if (m_uniqueChannelIndex == 1) {
|
|
|
|
params.rx.flags |= XTRX_RSP_SWAP_AB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = xtrx_run_ex(m_dev, ¶ms);
|
|
|
|
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
qCritical("XTRXInputThread::run: could not start stream err:%d", res);
|
|
|
|
m_running = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
2018-12-30 19:43:24 -05:00
|
|
|
qDebug("XTRXInputThread::run: stream started");
|
|
|
|
}
|
|
|
|
|
2018-12-31 14:22:42 -05:00
|
|
|
const unsigned int elemSize = 4; // XTRX uses 4 byte I+Q samples
|
|
|
|
std::vector<std::vector<char>> buffMem(m_nbChannels, std::vector<char>(elemSize*DeviceXTRX::blockSize));
|
|
|
|
std::vector<void *> buffs(m_nbChannels);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < m_nbChannels; i++) {
|
|
|
|
buffs[i] = buffMem[i].data();
|
|
|
|
}
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
xtrx_recv_ex_info_t nfo;
|
|
|
|
nfo.samples = DeviceXTRX::blockSize;
|
2018-12-31 14:22:42 -05:00
|
|
|
nfo.buffer_count = m_nbChannels;
|
|
|
|
nfo.buffers = (void* const*) buffs.data();
|
2018-12-30 19:43:24 -05:00
|
|
|
nfo.flags = RCVEX_DONT_INSER_ZEROS | RCVEX_DROP_OLD_ON_OVERFLOW;
|
|
|
|
|
|
|
|
while (m_running)
|
|
|
|
{
|
|
|
|
res = xtrx_recv_sync_ex(m_dev, &nfo);
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
qCritical("XTRXInputThread::run read error: %d", res);
|
2018-12-31 04:43:11 -05:00
|
|
|
qDebug("XTRXInputThread::run: out_samples: %u out_events: %u", nfo.out_samples, nfo.out_events);
|
2018-12-30 19:43:24 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_nbChannels > 1) {
|
2018-12-31 14:22:42 -05:00
|
|
|
callbackMI((const qint16*) buffs[0], (const qint16*) buffs[1], 2 * nfo.out_samples);
|
2018-12-30 19:43:24 -05:00
|
|
|
} else {
|
2018-12-31 14:22:42 -05:00
|
|
|
callbackSI((const qint16*) buffs[0], 2 * nfo.out_samples);
|
2018-12-30 19:43:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = xtrx_stop(m_dev, XTRX_RX);
|
|
|
|
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
qCritical("XTRXInputThread::run: could not stop stream");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
2018-12-30 19:43:24 -05:00
|
|
|
qDebug("XTRXInputThread::run: stream stopped");
|
|
|
|
}
|
2018-12-29 18:46:11 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-30 19:43:24 -05:00
|
|
|
qWarning("XTRXInputThread::run: no channels or FIFO allocated. Aborting");
|
2018-12-29 18:46:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
m_running = false;
|
|
|
|
}
|
2018-12-28 19:20:48 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
unsigned int XTRXInputThread::getNbFifos()
|
|
|
|
{
|
|
|
|
unsigned int fifoCount = 0;
|
2018-12-28 19:20:48 -05:00
|
|
|
|
2018-12-31 04:43:11 -05:00
|
|
|
for (unsigned int i = 0; i < 2; i++)
|
2018-12-28 19:20:48 -05:00
|
|
|
{
|
2018-12-30 19:43:24 -05:00
|
|
|
if (m_channels[i].m_sampleFifo) {
|
|
|
|
fifoCount++;
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
return fifoCount;
|
|
|
|
}
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
void XTRXInputThread::setLog2Decimation(unsigned int channel, unsigned int log2_decim)
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
if (channel < 2) {
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[channel].m_log2Decim = log2_decim;
|
2018-12-29 18:46:11 -05:00
|
|
|
}
|
2018-12-30 19:43:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int XTRXInputThread::getLog2Decimation(unsigned int channel) const
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
if (channel < 2) {
|
2018-12-30 19:43:24 -05:00
|
|
|
return m_channels[channel].m_log2Decim;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2018-12-29 18:46:11 -05:00
|
|
|
}
|
2018-12-30 19:43:24 -05:00
|
|
|
}
|
2018-12-29 18:46:11 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
void XTRXInputThread::setFifo(unsigned int channel, SampleSinkFifo *sampleFifo)
|
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
if (channel < 2) {
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[channel].m_sampleFifo = sampleFifo;
|
|
|
|
}
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
SampleSinkFifo *XTRXInputThread::getFifo(unsigned int channel)
|
2018-12-28 19:20:48 -05:00
|
|
|
{
|
2018-12-31 04:43:11 -05:00
|
|
|
if (channel < 2) {
|
2018-12-30 19:43:24 -05:00
|
|
|
return m_channels[channel].m_sampleFifo;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void XTRXInputThread::callbackSI(const qint16* buf, qint32 len)
|
|
|
|
{
|
|
|
|
SampleVector::iterator it = m_channels[m_uniqueChannelIndex].m_convertBuffer.begin();
|
2018-12-28 19:20:48 -05:00
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
if (m_channels[m_uniqueChannelIndex].m_log2Decim == 0)
|
2018-12-28 19:20:48 -05:00
|
|
|
{
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate1(&it, buf, len);
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-30 19:43:24 -05:00
|
|
|
switch (m_channels[m_uniqueChannelIndex].m_log2Decim)
|
2018-12-28 19:20:48 -05:00
|
|
|
{
|
2018-12-30 04:18:57 -05:00
|
|
|
case 1:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate2_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate4_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
case 3:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate8_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
case 4:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate16_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
case 5:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate32_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
case 6:
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_decimators.decimate64_cen(&it, buf, len);
|
2018-12-30 04:18:57 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 19:43:24 -05:00
|
|
|
m_channels[m_uniqueChannelIndex].m_sampleFifo->write(m_channels[m_uniqueChannelIndex].m_convertBuffer.begin(), it);
|
|
|
|
}
|
|
|
|
|
2018-12-31 14:22:42 -05:00
|
|
|
void XTRXInputThread::callbackMI(const qint16* buf0, const qint16* buf1, qint32 len)
|
2018-12-30 19:43:24 -05:00
|
|
|
{
|
2018-12-31 14:22:42 -05:00
|
|
|
unsigned int uniqueChannelIndex = m_uniqueChannelIndex;
|
|
|
|
|
|
|
|
// channel 0
|
|
|
|
m_uniqueChannelIndex = 0;
|
|
|
|
callbackSI(buf0, len);
|
|
|
|
// channel 1
|
|
|
|
m_uniqueChannelIndex = 1;
|
|
|
|
callbackSI(buf1, len);
|
|
|
|
|
|
|
|
m_uniqueChannelIndex = uniqueChannelIndex;
|
2018-12-28 19:20:48 -05:00
|
|
|
}
|