///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2015 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 //
// //
// 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 . //
///////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include "util/simpleserializer.h"
#include "dsp/dspcommands.h"
#include "bladerfgui.h"
#include "bladerfinput.h"
#include "bladerfthread.h"
#include "bladerfserializer.h"
MESSAGE_CLASS_DEFINITION(BladerfInput::MsgConfigureBladerf, Message)
MESSAGE_CLASS_DEFINITION(BladerfInput::MsgReportBladerf, Message)
BladerfInput::Settings::Settings() :
m_centerFrequency(435000*1000),
m_devSampleRate(3072000),
m_lnaGain(0),
m_vga1(20),
m_vga2(9),
m_bandwidth(1500000),
m_log2Decim(0),
m_fcPos(FC_POS_INFRA),
m_xb200(false),
m_xb200Path(BLADERF_XB200_MIX),
m_xb200Filter(BLADERF_XB200_AUTO_1DB)
{
}
void BladerfInput::Settings::resetToDefaults()
{
m_centerFrequency = 435000*1000;
m_devSampleRate = 3072000;
m_lnaGain = 0;
m_vga1 = 20;
m_vga2 = 9;
m_bandwidth = 1500000;
m_log2Decim = 0;
m_fcPos = FC_POS_INFRA;
m_xb200 = false;
m_xb200Path = BLADERF_XB200_MIX;
m_xb200Filter = BLADERF_XB200_AUTO_1DB;
}
QByteArray BladerfInput::Settings::serialize() const
{
BladeRFSerializer::BladeRFData data;
data.m_data.m_lnaGain = m_lnaGain;
data.m_data.m_RxGain1 = m_vga1;
data.m_data.m_RxGain2 = m_vga2;
data.m_data.m_log2Decim = m_log2Decim;
data.m_xb200 = m_xb200;
data.m_xb200Path = (int) m_xb200Path;
data.m_xb200Filter = (int) m_xb200Filter;
data.m_data.m_bandwidth = m_bandwidth;
data.m_data.m_fcPosition = (int) m_fcPos;
data.m_data.m_frequency = m_centerFrequency;
data.m_data.m_rate = m_devSampleRate;
QByteArray byteArray;
BladeRFSerializer::writeSerializedData(data, byteArray);
return byteArray;
}
bool BladerfInput::Settings::deserialize(const QByteArray& serializedData)
{
BladeRFSerializer::BladeRFData data;
bool valid = BladeRFSerializer::readSerializedData(serializedData, data);
m_lnaGain = data.m_data.m_lnaGain;
m_vga1 = data.m_data.m_RxGain1;
m_vga2 = data.m_data.m_RxGain2;
m_log2Decim = data.m_data.m_log2Decim;
m_xb200 = data.m_xb200;
m_xb200Path = (bladerf_xb200_path) data.m_xb200Path;
m_xb200Filter = (bladerf_xb200_filter) data.m_xb200Filter;
m_bandwidth = data.m_data.m_bandwidth;
m_fcPos = (fcPos_t) data.m_data.m_fcPosition;
m_centerFrequency = data.m_data.m_frequency;
m_devSampleRate = data.m_data.m_rate;
return valid;
}
BladerfInput::BladerfInput() :
m_settings(),
m_dev(0),
m_bladerfThread(0),
m_deviceDescription("BladeRF")
{
}
BladerfInput::~BladerfInput()
{
stop();
}
bool BladerfInput::init(const Message& cmd)
{
return false;
}
bool BladerfInput::start(int device)
{
QMutexLocker mutexLocker(&m_mutex);
if (m_dev != 0)
{
stop();
}
int res;
int fpga_loaded;
if (!m_sampleFifo.setSize(96000 * 4))
{
qCritical("Could not allocate SampleFifo");
return false;
}
if ((m_dev = open_bladerf_from_serial(0)) == 0) // TODO: fix; Open first available device as there is no proper handling for multiple devices
{
qCritical("could not open BladeRF");
return false;
}
fpga_loaded = bladerf_is_fpga_configured(m_dev);
if (fpga_loaded < 0)
{
qCritical("Failed to check FPGA state: %s",
bladerf_strerror(fpga_loaded));
return false;
}
else if (fpga_loaded == 0)
{
qCritical("The device's FPGA is not loaded.");
return false;
}
// TODO: adjust USB transfer data according to sample rate
if ((res = bladerf_sync_config(m_dev, BLADERF_MODULE_RX, BLADERF_FORMAT_SC16_Q11, 64, 8192, 32, 10000)) < 0)
{
qCritical("bladerf_sync_config with return code %d", res);
goto failed;
}
if ((res = bladerf_enable_module(m_dev, BLADERF_MODULE_RX, true)) < 0)
{
qCritical("bladerf_enable_module with return code %d", res);
goto failed;
}
if((m_bladerfThread = new BladerfThread(m_dev, &m_sampleFifo)) == NULL) {
qFatal("out of memory");
goto failed;
}
m_bladerfThread->startWork();
mutexLocker.unlock();
applySettings(m_settings, true);
qDebug("BladerfInput::startInput: started");
return true;
failed:
stop();
return false;
}
void BladerfInput::stop()
{
QMutexLocker mutexLocker(&m_mutex);
if(m_bladerfThread != 0)
{
m_bladerfThread->stopWork();
delete m_bladerfThread;
m_bladerfThread = 0;
}
if(m_dev != 0)
{
bladerf_close(m_dev);
m_dev = 0;
}
}
const QString& BladerfInput::getDeviceDescription() const
{
return m_deviceDescription;
}
int BladerfInput::getSampleRate() const
{
int rate = m_settings.m_devSampleRate;
return (rate / (1<setSamplerate(m_settings.m_devSampleRate);
}
}
}
if ((m_settings.m_bandwidth != settings.m_bandwidth) || force)
{
m_settings.m_bandwidth = settings.m_bandwidth;
if(m_dev != 0)
{
unsigned int actualBandwidth;
if( bladerf_set_bandwidth(m_dev, BLADERF_MODULE_RX, m_settings.m_bandwidth, &actualBandwidth) < 0)
{
qCritical("could not set bandwidth: %d", m_settings.m_bandwidth);
}
else
{
qDebug() << "bladerf_set_bandwidth(BLADERF_MODULE_RX) actual bandwidth is " << actualBandwidth;
}
}
}
if ((m_settings.m_log2Decim != settings.m_log2Decim) || force)
{
m_settings.m_log2Decim = settings.m_log2Decim;
forwardChange = true;
if(m_dev != 0)
{
m_bladerfThread->setLog2Decimation(m_settings.m_log2Decim);
qDebug() << "BladerfInput: set decimation to " << (1<setFcPos((int) m_settings.m_fcPos);
qDebug() << "BladerfInput: set fc pos (enum) to " << (int) m_settings.m_fcPos;
}
}
if (m_settings.m_centerFrequency != settings.m_centerFrequency)
{
forwardChange = true;
}
m_settings.m_centerFrequency = settings.m_centerFrequency;
qint64 deviceCenterFrequency = m_settings.m_centerFrequency;
qint64 f_img = deviceCenterFrequency;
qint64 f_cut = deviceCenterFrequency + m_settings.m_bandwidth/2;
if ((m_settings.m_log2Decim == 0) || (m_settings.m_fcPos == FC_POS_CENTER))
{
deviceCenterFrequency = m_settings.m_centerFrequency;
f_img = deviceCenterFrequency;
f_cut = deviceCenterFrequency + m_settings.m_bandwidth/2;
}
else
{
if (m_settings.m_fcPos == FC_POS_INFRA)
{
deviceCenterFrequency = m_settings.m_centerFrequency + (m_settings.m_devSampleRate / 4);
f_img = deviceCenterFrequency + m_settings.m_devSampleRate/2;
f_cut = deviceCenterFrequency + m_settings.m_bandwidth/2;
}
else if (m_settings.m_fcPos == FC_POS_SUPRA)
{
deviceCenterFrequency = m_settings.m_centerFrequency - (m_settings.m_devSampleRate / 4);
f_img = deviceCenterFrequency - m_settings.m_devSampleRate/2;
f_cut = deviceCenterFrequency - m_settings.m_bandwidth/2;
}
}
if (m_dev != NULL)
{
if (bladerf_set_frequency( m_dev, BLADERF_MODULE_RX, deviceCenterFrequency ) != 0)
{
qDebug("bladerf_set_frequency(%lld) failed", m_settings.m_centerFrequency);
}
}
if (forwardChange)
{
int sampleRate = m_settings.m_devSampleRate/(1<push(notif);
}
qDebug() << "BladerfInput::applySettings: center freq: " << m_settings.m_centerFrequency << " Hz"
<< " device center freq: " << deviceCenterFrequency << " Hz"
<< " device sample rate: " << m_settings.m_devSampleRate << "Hz"
<< " Actual sample rate: " << m_settings.m_devSampleRate/(1<