1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-27 02:09:14 -05:00
sdrangel/plugins/samplesource/bladerf2input/bladerf2inputthread.cpp

405 lines
13 KiB
C++
Raw Normal View History

2018-09-23 13:56:24 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 Edouard Griffiths, F4EXB //
// //
// 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 //
// (at your option) any later version. //
2018-09-23 13:56:24 -04:00
// //
// 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 "dsp/samplesinkfifo.h"
2018-09-23 13:56:24 -04:00
#include "bladerf2inputthread.h"
BladeRF2InputThread::BladeRF2InputThread(struct bladerf* dev, unsigned int nbRxChannels, QObject* parent) :
2018-09-23 13:56:24 -04:00
QThread(parent),
m_running(false),
m_dev(dev),
m_nbChannels(nbRxChannels),
m_iqOrder(true)
2018-09-23 13:56:24 -04:00
{
qDebug("BladeRF2InputThread::BladeRF2InputThread");
2018-09-23 13:56:24 -04:00
m_channels = new Channel[nbRxChannels];
for (unsigned int i = 0; i < nbRxChannels; i++) {
m_channels[i].m_convertBuffer.resize(DeviceBladeRF2::blockSize, Sample{0,0});
}
2018-09-23 13:56:24 -04:00
m_buf = new qint16[2*DeviceBladeRF2::blockSize*nbRxChannels];
}
BladeRF2InputThread::~BladeRF2InputThread()
2018-09-23 13:56:24 -04:00
{
qDebug("BladeRF2InputThread::~BladeRF2InputThread");
2018-09-23 13:56:24 -04:00
if (m_running) {
stopWork();
}
delete[] m_buf;
delete[] m_channels;
}
void BladeRF2InputThread::startWork()
2018-09-23 13:56:24 -04:00
{
m_startWaitMutex.lock();
start();
while(!m_running) {
m_startWaiter.wait(&m_startWaitMutex, 100);
}
m_startWaitMutex.unlock();
}
void BladeRF2InputThread::stopWork()
2018-09-23 13:56:24 -04:00
{
m_running = false;
wait();
}
void BladeRF2InputThread::run()
2018-09-23 13:56:24 -04:00
{
int res;
m_running = true;
m_startWaiter.wakeAll();
unsigned int nbFifos = getNbFifos();
2018-09-23 20:01:10 -04:00
if ((m_nbChannels > 0) && (nbFifos > 0))
2018-09-23 13:56:24 -04:00
{
int status;
2018-09-23 20:01:10 -04:00
if (m_nbChannels > 1) {
2018-09-23 13:56:24 -04:00
status = bladerf_sync_config(m_dev, BLADERF_RX_X2, BLADERF_FORMAT_SC16_Q11, 64, 8192, 32, 10000);
} else {
status = bladerf_sync_config(m_dev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11, 64, 8192, 32, 10000);
}
if (status < 0)
{
qCritical("BladeRF2InputThread::run: cannot configure streams: %s", bladerf_strerror(status));
2018-09-23 13:56:24 -04:00
}
else
{
qDebug("BladeRF2InputThread::run: start running loop");
2018-09-23 13:56:24 -04:00
while (m_running)
{
if (m_nbChannels > 1) {
res = bladerf_sync_rx(m_dev, m_buf, DeviceBladeRF2::blockSize*m_nbChannels, NULL, 10000);
} else {
res = bladerf_sync_rx(m_dev, m_buf, DeviceBladeRF2::blockSize, NULL, 10000);
}
2018-09-23 13:56:24 -04:00
if (res < 0)
{
qCritical("BladeRF2InputThread::run sync Rx error: %s", bladerf_strerror(res));
2018-09-23 13:56:24 -04:00
break;
}
if (m_nbChannels > 1)
{
2018-09-23 13:56:24 -04:00
callbackMI(m_buf, DeviceBladeRF2::blockSize);
}
else
{
if (m_iqOrder) {
callbackSIIQ(m_buf, 2*DeviceBladeRF2::blockSize);
} else {
callbackSIQI(m_buf, 2*DeviceBladeRF2::blockSize);
}
2018-09-23 13:56:24 -04:00
}
}
qDebug("BladeRF2InputThread::run: stop running loop");
2018-09-23 13:56:24 -04:00
}
}
else
{
qWarning("BladeRF2InputThread::run: no channels or FIFO allocated. Aborting");
2018-09-23 13:56:24 -04:00
}
m_running = false;
}
unsigned int BladeRF2InputThread::getNbFifos()
2018-09-23 13:56:24 -04:00
{
unsigned int fifoCount = 0;
2018-09-23 20:01:10 -04:00
for (unsigned int i = 0; i < m_nbChannels; i++)
2018-09-23 13:56:24 -04:00
{
if (m_channels[i].m_sampleFifo) {
fifoCount++;
}
}
return fifoCount;
}
void BladeRF2InputThread::setLog2Decimation(unsigned int channel, unsigned int log2_decim)
2018-09-23 13:56:24 -04:00
{
2018-09-23 20:01:10 -04:00
if (channel < m_nbChannels) {
2018-09-23 13:56:24 -04:00
m_channels[channel].m_log2Decim = log2_decim;
}
}
unsigned int BladeRF2InputThread::getLog2Decimation(unsigned int channel) const
2018-09-23 20:01:10 -04:00
{
if (channel < m_nbChannels) {
return m_channels[channel].m_log2Decim;
} else {
return 0;
}
}
void BladeRF2InputThread::setFcPos(unsigned int channel, int fcPos)
2018-09-23 13:56:24 -04:00
{
2018-09-23 20:01:10 -04:00
if (channel < m_nbChannels) {
2018-09-23 13:56:24 -04:00
m_channels[channel].m_fcPos = fcPos;
}
}
int BladeRF2InputThread::getFcPos(unsigned int channel) const
2018-09-23 20:01:10 -04:00
{
if (channel < m_nbChannels) {
return m_channels[channel].m_fcPos;
} else {
return 0;
}
}
void BladeRF2InputThread::setFifo(unsigned int channel, SampleSinkFifo *sampleFifo)
2018-09-23 13:56:24 -04:00
{
2018-09-23 20:01:10 -04:00
if (channel < m_nbChannels) {
2018-09-23 13:56:24 -04:00
m_channels[channel].m_sampleFifo = sampleFifo;
}
}
SampleSinkFifo *BladeRF2InputThread::getFifo(unsigned int channel)
2018-09-23 13:56:24 -04:00
{
2018-09-23 20:01:10 -04:00
if (channel < m_nbChannels) {
2018-09-23 13:56:24 -04:00
return m_channels[channel].m_sampleFifo;
} else {
return 0;
}
}
void BladeRF2InputThread::callbackMI(const qint16* buf, qint32 samplesPerChannel)
2018-09-23 13:56:24 -04:00
{
// TODO: write a set of decimators that can take interleaved samples in input directly
int status = bladerf_deinterleave_stream_buffer(BLADERF_RX_X2, BLADERF_FORMAT_SC16_Q11 , samplesPerChannel*m_nbChannels, (void *) buf);
if (status < 0)
{
qCritical("BladeRF2InputThread::callbackMI: cannot de-interleave buffer: %s", bladerf_strerror(status));
2018-09-23 13:56:24 -04:00
return;
}
for (unsigned int channel = 0; channel < m_nbChannels; channel++)
{
if (m_channels[channel].m_sampleFifo)
{
if (m_iqOrder) {
callbackSIIQ(&buf[2*samplesPerChannel*channel], 2*samplesPerChannel, channel);
} else {
callbackSIQI(&buf[2*samplesPerChannel*channel], 2*samplesPerChannel, channel);
}
2018-09-23 13:56:24 -04:00
}
}
}
void BladeRF2InputThread::callbackSIIQ(const qint16* buf, qint32 len, unsigned int channel)
2018-09-23 13:56:24 -04:00
{
SampleVector::iterator it = m_channels[channel].m_convertBuffer.begin();
if (m_channels[channel].m_log2Decim == 0)
{
m_channels[channel].m_decimatorsIQ.decimate1(&it, buf, len);
2018-09-23 13:56:24 -04:00
}
else
{
if (m_channels[channel].m_fcPos == 0) // Infra
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsIQ.decimate2_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 2:
m_channels[channel].m_decimatorsIQ.decimate4_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 3:
m_channels[channel].m_decimatorsIQ.decimate8_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 4:
m_channels[channel].m_decimatorsIQ.decimate16_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 5:
m_channels[channel].m_decimatorsIQ.decimate32_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 6:
m_channels[channel].m_decimatorsIQ.decimate64_inf(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
default:
break;
}
}
else if (m_channels[channel].m_fcPos == 1) // Supra
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsIQ.decimate2_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 2:
m_channels[channel].m_decimatorsIQ.decimate4_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 3:
m_channels[channel].m_decimatorsIQ.decimate8_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 4:
m_channels[channel].m_decimatorsIQ.decimate16_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 5:
m_channels[channel].m_decimatorsIQ.decimate32_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 6:
m_channels[channel].m_decimatorsIQ.decimate64_sup(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
default:
break;
}
}
else if (m_channels[channel].m_fcPos == 2) // Center
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsIQ.decimate2_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 2:
m_channels[channel].m_decimatorsIQ.decimate4_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 3:
m_channels[channel].m_decimatorsIQ.decimate8_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 4:
m_channels[channel].m_decimatorsIQ.decimate16_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 5:
m_channels[channel].m_decimatorsIQ.decimate32_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
case 6:
m_channels[channel].m_decimatorsIQ.decimate64_cen(&it, buf, len);
2018-09-23 13:56:24 -04:00
break;
default:
break;
}
}
}
m_channels[channel].m_sampleFifo->write(m_channels[channel].m_convertBuffer.begin(), it);
}
void BladeRF2InputThread::callbackSIQI(const qint16* buf, qint32 len, unsigned int channel)
{
SampleVector::iterator it = m_channels[channel].m_convertBuffer.begin();
if (m_channels[channel].m_log2Decim == 0)
{
m_channels[channel].m_decimatorsQI.decimate1(&it, buf, len);
}
else
{
if (m_channels[channel].m_fcPos == 0) // Infra
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsQI.decimate2_inf(&it, buf, len);
break;
case 2:
m_channels[channel].m_decimatorsQI.decimate4_inf(&it, buf, len);
break;
case 3:
m_channels[channel].m_decimatorsQI.decimate8_inf(&it, buf, len);
break;
case 4:
m_channels[channel].m_decimatorsQI.decimate16_inf(&it, buf, len);
break;
case 5:
m_channels[channel].m_decimatorsQI.decimate32_inf(&it, buf, len);
break;
case 6:
m_channels[channel].m_decimatorsQI.decimate64_inf(&it, buf, len);
break;
default:
break;
}
}
else if (m_channels[channel].m_fcPos == 1) // Supra
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsQI.decimate2_sup(&it, buf, len);
break;
case 2:
m_channels[channel].m_decimatorsQI.decimate4_sup(&it, buf, len);
break;
case 3:
m_channels[channel].m_decimatorsQI.decimate8_sup(&it, buf, len);
break;
case 4:
m_channels[channel].m_decimatorsQI.decimate16_sup(&it, buf, len);
break;
case 5:
m_channels[channel].m_decimatorsQI.decimate32_sup(&it, buf, len);
break;
case 6:
m_channels[channel].m_decimatorsQI.decimate64_sup(&it, buf, len);
break;
default:
break;
}
}
else if (m_channels[channel].m_fcPos == 2) // Center
{
switch (m_channels[channel].m_log2Decim)
{
case 1:
m_channels[channel].m_decimatorsQI.decimate2_cen(&it, buf, len);
break;
case 2:
m_channels[channel].m_decimatorsQI.decimate4_cen(&it, buf, len);
break;
case 3:
m_channels[channel].m_decimatorsQI.decimate8_cen(&it, buf, len);
break;
case 4:
m_channels[channel].m_decimatorsQI.decimate16_cen(&it, buf, len);
break;
case 5:
m_channels[channel].m_decimatorsQI.decimate32_cen(&it, buf, len);
break;
case 6:
m_channels[channel].m_decimatorsQI.decimate64_cen(&it, buf, len);
break;
default:
break;
}
}
}
m_channels[channel].m_sampleFifo->write(m_channels[channel].m_convertBuffer.begin(), it);
}