From 945d30d91b8df0d788aabe1e947dd531f29bd3ed Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 24 Sep 2018 02:01:10 +0200 Subject: [PATCH] BladerRF2 input support (3) --- devices/bladerf2/devicebladerf2.h | 2 + devices/bladerf2/devicebladerf2shared.cpp | 4 +- devices/bladerf2/devicebladerf2shared.h | 8 + .../bladerf2input/bladerf2input.cpp | 381 ++++++++++++++++++ .../bladerf2input/bladerf2inputthread.cpp | 34 +- .../bladerf2input/bladerf2inputthread.h | 7 +- 6 files changed, 425 insertions(+), 11 deletions(-) diff --git a/devices/bladerf2/devicebladerf2.h b/devices/bladerf2/devicebladerf2.h index 30b2cf6a8..70df89f17 100644 --- a/devices/bladerf2/devicebladerf2.h +++ b/devices/bladerf2/devicebladerf2.h @@ -31,6 +31,8 @@ public: bool open(const char *serial); void close(); + bladerf *getDev() { return m_dev; } + bool openRx(int channel); bool openTx(int channel); void closeRx(int channel); diff --git a/devices/bladerf2/devicebladerf2shared.cpp b/devices/bladerf2/devicebladerf2shared.cpp index 4bb577367..8d8c00eea 100644 --- a/devices/bladerf2/devicebladerf2shared.cpp +++ b/devices/bladerf2/devicebladerf2shared.cpp @@ -18,7 +18,9 @@ DeviceBladeRF2Shared::DeviceBladeRF2Shared() : m_dev(0), - m_channel(-1) + m_channel(-1), + m_inputThread(0), + m_outputThread(0) {} DeviceBladeRF2Shared::~DeviceBladeRF2Shared() diff --git a/devices/bladerf2/devicebladerf2shared.h b/devices/bladerf2/devicebladerf2shared.h index 0f6378191..c72524a26 100644 --- a/devices/bladerf2/devicebladerf2shared.h +++ b/devices/bladerf2/devicebladerf2shared.h @@ -31,9 +31,15 @@ public: class InputThreadInterface { public: + virtual ~InputThreadInterface() = 0; virtual void startWork() = 0; virtual void stopWork() = 0; virtual bool isRunning() const = 0; + virtual unsigned int getNbChannels() const = 0; + virtual void setLog2Decimation(unsigned int channel, unsigned int log2_decim) = 0; + virtual unsigned int getLog2Decimation(unsigned int channel) const = 0; + virtual void setFcPos(unsigned int channel, int fcPos) = 0; + virtual int getFcPos(unsigned int channel) const = 0; virtual void setFifo(unsigned int channel, SampleSinkFifo *fifo) = 0; virtual SampleSinkFifo *getFifo(unsigned int channel) = 0; }; @@ -41,9 +47,11 @@ public: class OutputThreadInterface { public: + virtual ~OutputThreadInterface() = 0; virtual void startWork() = 0; virtual void stopWork() = 0; virtual bool isRunning() = 0; + virtual unsigned int getNbChannels() const = 0; virtual void setFifo(unsigned int channel, SampleSourceFifo *fifo) = 0; virtual SampleSourceFifo *getFifo(unsigned int channel) = 0; }; diff --git a/plugins/samplesource/bladerf2input/bladerf2input.cpp b/plugins/samplesource/bladerf2input/bladerf2input.cpp index ebbee96a3..d14df5a8d 100644 --- a/plugins/samplesource/bladerf2input/bladerf2input.cpp +++ b/plugins/samplesource/bladerf2input/bladerf2input.cpp @@ -30,6 +30,7 @@ #include "bladerf2/devicebladerf2shared.h" #include "bladerf2/devicebladerf2.h" +#include "bladerf2inputthread.h" #include "bladerf2input.h" @@ -218,3 +219,383 @@ void BladeRF2Input::init() applySettings(m_settings, true); } +bool BladeRF2Input::start() +{ + if (!m_deviceShared.m_dev) + { + qDebug("BladerfInput::start: no device object"); + return false; + } + + Bladerf2InputThread *bladerf2InputThread = 0; + bool needsStart = false; + + // find thread allocated by a buddy + const std::vector& sourceBuddies = m_deviceAPI->getSourceBuddies(); + std::vector::const_iterator it = sourceBuddies.begin(); + + for (; it != sourceBuddies.end(); ++it) + { + bladerf2InputThread = (Bladerf2InputThread*) ((DeviceBladeRF2Shared*) (*it)->getBuddySharedPtr())->m_inputThread; + + if (bladerf2InputThread) { + break; + } + } + + if (bladerf2InputThread) // if thread was allocated by a buddy + { + DeviceSourceAPI *sourceBuddy = m_deviceAPI->getSourceBuddies()[0]; + DeviceBladeRF2Shared *deviceBladeRF2Shared = (DeviceBladeRF2Shared*) sourceBuddy->getBuddySharedPtr(); + bladerf2InputThread = (Bladerf2InputThread*) deviceBladeRF2Shared->m_inputThread; + int nbOriginalChannels = bladerf2InputThread->getNbChannels(); + + if (m_deviceShared.m_channel+1 > nbOriginalChannels) // expansion + { + SampleSinkFifo **fifos = new SampleSinkFifo*[nbOriginalChannels]; + unsigned int *log2Decims = new unsigned int[nbOriginalChannels]; + int *fcPoss = new int[nbOriginalChannels]; + + for (int i = 0; i < nbOriginalChannels; i++) { // save original FIFO references and data + fifos[i] = bladerf2InputThread->getFifo(i); + log2Decims[i] = bladerf2InputThread->getLog2Decimation(i); + fcPoss[i] = bladerf2InputThread->getFcPos(i); + } + + bladerf2InputThread->stopWork(); + delete bladerf2InputThread; + bladerf2InputThread = new Bladerf2InputThread(m_deviceShared.m_dev->getDev(), m_deviceShared.m_channel+1); + + for (int i = 0; i < nbOriginalChannels; i++) { // restore original FIFO references + bladerf2InputThread->setFifo(i, fifos[i]); + bladerf2InputThread->setLog2Decimation(i, log2Decims[i]); + bladerf2InputThread->setFcPos(i, fcPoss[i]); + } + + // propagate new thread address to buddies + const std::vector& sourceBuddies = m_deviceAPI->getSourceBuddies(); + std::vector::const_iterator it = sourceBuddies.begin(); + + for (; it != sourceBuddies.end(); ++it) { + ((DeviceBladeRF2Shared*) (*it)->getBuddySharedPtr())->m_inputThread = bladerf2InputThread; + } + + needsStart = true; + } + } + else // first allocation + { + bladerf2InputThread = new Bladerf2InputThread(m_deviceShared.m_dev->getDev(), m_deviceShared.m_channel+1); + needsStart = true; + } + + bladerf2InputThread->setFifo(m_deviceShared.m_channel, &m_sampleFifo); + bladerf2InputThread->setLog2Decimation(m_deviceShared.m_channel, m_settings.m_log2Decim); + bladerf2InputThread->setFcPos(m_deviceShared.m_channel, (int) m_settings.m_fcPos); + m_deviceShared.m_inputThread = bladerf2InputThread; + + if (needsStart) { + bladerf2InputThread->startWork(); + } + + applySettings(m_settings, true); + + qDebug("BladerfInput::startInput: started"); + m_running = true; + + return true; +} + +void BladeRF2Input::stop() +{ + if (!m_running) { + return; + } + + int nbOriginalChannels = m_deviceShared.m_inputThread->getNbChannels(); + + if (nbOriginalChannels == 1) // SI mode + { + m_deviceShared.m_inputThread->stopWork(); + delete m_deviceShared.m_inputThread; + m_deviceShared.m_inputThread = 0; + m_running = false; + } + else if (m_deviceShared.m_channel == nbOriginalChannels - 1) // remove last MI channel => reduce + { + m_deviceShared.m_inputThread->stopWork(); + SampleSinkFifo **fifos = new SampleSinkFifo*[nbOriginalChannels-1]; + unsigned int *log2Decims = new unsigned int[nbOriginalChannels-1]; + int *fcPoss = new int[nbOriginalChannels-1]; + + for (int i = 0; i < nbOriginalChannels-1; i++) { // save original FIFO references + fifos[i] = m_deviceShared.m_inputThread->getFifo(i); + log2Decims[i] = m_deviceShared.m_inputThread->getLog2Decimation(i); + fcPoss[i] = m_deviceShared.m_inputThread->getFcPos(i); + } + + delete m_deviceShared.m_inputThread; + m_deviceShared.m_inputThread = new Bladerf2InputThread(m_deviceShared.m_dev->getDev(), nbOriginalChannels-1); + + for (int i = 0; i < nbOriginalChannels-1; i++) { // restore original FIFO references + m_deviceShared.m_inputThread->setFifo(i, fifos[i]); + m_deviceShared.m_inputThread->setLog2Decimation(i, log2Decims[i]); + m_deviceShared.m_inputThread->setFcPos(i, fcPoss[i]); + } + + // propagate new thread address to buddies + const std::vector& sourceBuddies = m_deviceAPI->getSourceBuddies(); + std::vector::const_iterator it = sourceBuddies.begin(); + + for (; it != sourceBuddies.end(); ++it) { + ((DeviceBladeRF2Shared*) (*it)->getBuddySharedPtr())->m_inputThread = m_deviceShared.m_inputThread; + } + + m_deviceShared.m_inputThread->startWork(); + } + else + { + m_deviceShared.m_inputThread->setFifo(m_deviceShared.m_channel, 0); // remove FIFO + } +} + +QByteArray BladeRF2Input::serialize() const +{ + return m_settings.serialize(); +} + +bool BladeRF2Input::deserialize(const QByteArray& data) +{ + bool success = true; + + if (!m_settings.deserialize(data)) + { + m_settings.resetToDefaults(); + success = false; + } + + MsgConfigureBladeRF2* message = MsgConfigureBladeRF2::create(m_settings, true); + m_inputMessageQueue.push(message); + + if (m_guiMessageQueue) + { + MsgConfigureBladeRF2* messageToGUI = MsgConfigureBladeRF2::create(m_settings, true); + m_guiMessageQueue->push(messageToGUI); + } + + return success; +} + +const QString& BladeRF2Input::getDeviceDescription() const +{ + return m_deviceDescription; +} + +int BladeRF2Input::getSampleRate() const +{ + int rate = m_settings.m_devSampleRate; + return (rate / (1<push(messageToGUI); + } +} + +bool BladeRF2Input::handleMessage(const Message& message) +{ + if (MsgConfigureBladeRF2::match(message)) + { + MsgConfigureBladeRF2& conf = (MsgConfigureBladeRF2&) message; + qDebug() << "BladeRF2Input::handleMessage: MsgConfigureBladeRF2"; + + if (!applySettings(conf.getSettings(), conf.getForce())) + { + qDebug("BladeRF2Input::handleMessage: MsgConfigureBladeRF2 config error"); + } + + return true; + } + else if (MsgFileRecord::match(message)) + { + MsgFileRecord& conf = (MsgFileRecord&) message; + qDebug() << "BladeRF2Input::handleMessage: MsgFileRecord: " << conf.getStartStop(); + + if (conf.getStartStop()) + { + if (m_settings.m_fileRecordName.size() != 0) { + m_fileSink->setFileName(m_settings.m_fileRecordName); + } else { + m_fileSink->genUniqueFileName(m_deviceAPI->getDeviceUID()); + } + + m_fileSink->startRecording(); + } + else + { + m_fileSink->stopRecording(); + } + + return true; + } + else if (MsgStartStop::match(message)) + { + MsgStartStop& cmd = (MsgStartStop&) message; + qDebug() << "BladeRF2Input::handleMessage: MsgStartStop: " << (cmd.getStartStop() ? "start" : "stop"); + + if (cmd.getStartStop()) + { + if (m_deviceAPI->initAcquisition()) + { + m_deviceAPI->startAcquisition(); + } + } + else + { + m_deviceAPI->stopAcquisition(); + } + + return true; + } + else + { + return false; + } +} + +bool BladeRF2Input::applySettings(const BladeRF2InputSettings& settings, bool force) +{ + bool forwardChange = false; + + struct bladerf *dev = m_deviceShared.m_dev->getDev(); + qDebug() << "BladeRF2Input::applySettings: m_dev: " << dev; + + if ((m_settings.m_dcBlock != settings.m_dcBlock) || + (m_settings.m_iqCorrection != settings.m_iqCorrection) || force) + { + m_deviceAPI->configureCorrections(settings.m_dcBlock, settings.m_iqCorrection); + } + + if ((m_settings.m_devSampleRate != settings.m_devSampleRate) || force) + { + forwardChange = true; + + if (dev != 0) + { + unsigned int actualSamplerate; + int status = bladerf_set_sample_rate(dev, BLADERF_CHANNEL_RX(m_deviceShared.m_channel), settings.m_devSampleRate, &actualSamplerate); + + if (status < 0) + { + qCritical("BladeRF2Input::applySettings: could not set sample rate: %d: %s", + settings.m_devSampleRate, bladerf_strerror(status)); + } + else + { + qDebug() << "BladeRF2Input::applySettings: bladerf_set_sample_rate(BLADERF_MODULE_RX) actual sample rate is " << actualSamplerate; + } + } + } + + if ((m_settings.m_bandwidth != settings.m_bandwidth) || force) + { + if (dev != 0) + { + unsigned int actualBandwidth; + int status = bladerf_set_bandwidth(dev, BLADERF_CHANNEL_RX(m_deviceShared.m_channel), settings.m_bandwidth, &actualBandwidth); + + if(status < 0) + { + qCritical("BladeRF2Input::applySettings: could not set bandwidth: %d: %s", + settings.m_bandwidth, bladerf_strerror(status)); + } + else + { + qDebug() << "BladeRF2Input::applySettings: bladerf_set_bandwidth(BLADERF_MODULE_RX) actual bandwidth is " << actualBandwidth; + } + } + } + + if ((m_settings.m_fcPos != settings.m_fcPos) || force) + { + if (m_deviceShared.m_inputThread != 0) + { + m_deviceShared.m_inputThread->setFcPos(m_deviceShared.m_channel, (int) settings.m_fcPos); + qDebug() << "BladeRF2Input::applySettings: set fc pos (enum) to " << (int) settings.m_fcPos; + } + } + + if ((m_settings.m_log2Decim != settings.m_log2Decim) || force) + { + forwardChange = true; + + if (m_deviceShared.m_inputThread != 0) + { + m_deviceShared.m_inputThread->setLog2Decimation(m_deviceShared.m_channel, settings.m_log2Decim); + qDebug() << "BladeRF2Input::applySettings: set decimation to " << (1<handleMessage(*notif); // forward to file sink + m_deviceAPI->getDeviceEngineInputMessageQueue()->push(notif); + } + + m_settings = settings; + + qDebug() << "BladeRF2Input::applySettings: " + << " m_centerFrequency: " << m_settings.m_centerFrequency << " Hz" + << " m_bandwidth: " << m_settings.m_bandwidth + << " m_log2Decim: " << m_settings.m_log2Decim + << " m_fcPos: " << m_settings.m_fcPos + << " m_devSampleRate: " << m_settings.m_devSampleRate + << " m_dcBlock: " << m_settings.m_dcBlock + << " m_iqCorrection: " << m_settings.m_iqCorrection; + + return true; +} diff --git a/plugins/samplesource/bladerf2input/bladerf2inputthread.cpp b/plugins/samplesource/bladerf2input/bladerf2inputthread.cpp index c84e02fc3..048576800 100644 --- a/plugins/samplesource/bladerf2input/bladerf2inputthread.cpp +++ b/plugins/samplesource/bladerf2input/bladerf2inputthread.cpp @@ -63,11 +63,11 @@ void Bladerf2InputThread::run() unsigned int nbFifos = getNbFifos(); - if (nbFifos > 0) + if ((m_nbChannels > 0) && (nbFifos > 0)) { int status; - if (nbFifos > 1) { + if (m_nbChannels > 1) { 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); @@ -99,7 +99,7 @@ void Bladerf2InputThread::run() } else { - qWarning("Bladerf2InputThread::run: no sample FIFOs registered. Aborting"); + qWarning("Bladerf2InputThread::run: no channels or FIFO allocated. Aborting"); } @@ -110,7 +110,7 @@ unsigned int Bladerf2InputThread::getNbFifos() { unsigned int fifoCount = 0; - for (int i = 0; i < m_nbChannels; i++) + for (unsigned int i = 0; i < m_nbChannels; i++) { if (m_channels[i].m_sampleFifo) { fifoCount++; @@ -122,28 +122,46 @@ unsigned int Bladerf2InputThread::getNbFifos() void Bladerf2InputThread::setLog2Decimation(unsigned int channel, unsigned int log2_decim) { - if ((channel >= 0) && (channel < m_nbChannels)) { + if (channel < m_nbChannels) { m_channels[channel].m_log2Decim = log2_decim; } } +unsigned int Bladerf2InputThread::getLog2Decimation(unsigned int channel) const +{ + if (channel < m_nbChannels) { + return m_channels[channel].m_log2Decim; + } else { + return 0; + } +} + void Bladerf2InputThread::setFcPos(unsigned int channel, int fcPos) { - if ((channel >= 0) && (channel < m_nbChannels)) { + if (channel < m_nbChannels) { m_channels[channel].m_fcPos = fcPos; } } +int Bladerf2InputThread::getFcPos(unsigned int channel) const +{ + if (channel < m_nbChannels) { + return m_channels[channel].m_fcPos; + } else { + return 0; + } +} + void Bladerf2InputThread::setFifo(unsigned int channel, SampleSinkFifo *sampleFifo) { - if ((channel >= 0) && (channel < m_nbChannels)) { + if (channel < m_nbChannels) { m_channels[channel].m_sampleFifo = sampleFifo; } } SampleSinkFifo *Bladerf2InputThread::getFifo(unsigned int channel) { - if ((channel >= 0) && (channel < m_nbChannels)) { + if (channel < m_nbChannels) { return m_channels[channel].m_sampleFifo; } else { return 0; diff --git a/plugins/samplesource/bladerf2input/bladerf2inputthread.h b/plugins/samplesource/bladerf2input/bladerf2inputthread.h index 23c929f0b..ba2718a93 100644 --- a/plugins/samplesource/bladerf2input/bladerf2inputthread.h +++ b/plugins/samplesource/bladerf2input/bladerf2inputthread.h @@ -42,8 +42,11 @@ public: virtual void startWork(); virtual void stopWork(); virtual bool isRunning() const { return m_running; } - void setLog2Decimation(unsigned int channel, unsigned int log2_decim); - void setFcPos(unsigned int channel, int fcPos); + virtual unsigned int getNbChannels() const { return m_nbChannels; } + virtual void setLog2Decimation(unsigned int channel, unsigned int log2_decim); + virtual unsigned int getLog2Decimation(unsigned int channel) const; + virtual void setFcPos(unsigned int channel, int fcPos); + virtual int getFcPos(unsigned int channel) const; virtual void setFifo(unsigned int channel, SampleSinkFifo *sampleFifo); virtual SampleSinkFifo *getFifo(unsigned int channel);