sdrangel/plugins/samplesource/limesdrinput/limesdrinput.cpp

246 lines
8.4 KiB
C++
Raw Normal View History

2017-04-13 12:18:15 -04:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <QMutexLocker>
2017-04-14 12:30:37 -04:00
#include <cstddef>
2017-04-13 12:18:15 -04:00
#include <string.h>
2017-04-14 12:30:37 -04:00
#include "lime/LimeSuite.h"
2017-04-15 22:58:52 -04:00
#include "device/devicesourceapi.h"
#include "device/devicesinkapi.h"
2017-04-13 12:18:15 -04:00
#include "limesdrinput.h"
2017-04-15 22:58:52 -04:00
#include "limesdrinputthread.h"
2017-04-14 12:30:37 -04:00
#include "limesdr/devicelimesdrparam.h"
2017-04-13 12:18:15 -04:00
LimeSDRInput::LimeSDRInput(DeviceSourceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_settings(),
m_limeSDRInputThread(0),
m_deviceDescription(),
m_running(false)
{
openDevice();
}
LimeSDRInput::~LimeSDRInput()
{
if (m_running) stop();
closeDevice();
}
bool LimeSDRInput::openDevice()
{
// look for Rx buddies and get reference to common parameters
// if there is a channel left take the first available
if (m_deviceAPI->getSourceBuddies().size() > 0) // look source sibling first
{
2017-04-15 22:58:52 -04:00
DeviceSourceAPI *sourceBuddy = m_deviceAPI->getSourceBuddies()[0];
m_deviceShared = *((DeviceLimeSDRShared *) sourceBuddy->getBuddySharedPtr()); // copy shared data
2017-04-14 12:30:37 -04:00
DeviceLimeSDRParams *deviceParams = m_deviceShared.m_deviceParams; // get device parameters
if (deviceParams == 0)
{
qCritical("LimeSDRInput::openDevice: cannot get device parameters from Rx buddy");
return false; // the device params should have been created by the buddy
}
2017-04-13 12:18:15 -04:00
2017-04-14 12:30:37 -04:00
if (m_deviceAPI->getSourceBuddies().size() == deviceParams->m_nbRxChannels)
2017-04-13 12:18:15 -04:00
{
return false; // no more Rx channels available in device
}
// look for unused channel number
2017-04-14 12:30:37 -04:00
char *busyChannels = new char[deviceParams->m_nbRxChannels];
memset(busyChannels, 0, deviceParams->m_nbRxChannels);
2017-04-13 12:18:15 -04:00
for (int i = 0; i < m_deviceAPI->getSourceBuddies().size(); i++)
{
2017-04-15 22:58:52 -04:00
DeviceSourceAPI *buddy = m_deviceAPI->getSourceBuddies()[i];
DeviceLimeSDRShared *buddyShared = (DeviceLimeSDRShared *) buddy->getBuddySharedPtr();
2017-04-14 12:30:37 -04:00
busyChannels[buddyShared->m_channel] = 1;
2017-04-13 12:18:15 -04:00
}
2017-04-14 12:30:37 -04:00
std::size_t ch = 0;
2017-04-13 12:18:15 -04:00
2017-04-14 12:30:37 -04:00
for (;ch < deviceParams->m_nbRxChannels; ch++)
2017-04-13 12:18:15 -04:00
{
if (busyChannels[ch] == 0) {
break; // first available is the good one
}
}
2017-04-14 12:30:37 -04:00
m_deviceShared.m_channel = ch;
2017-04-13 12:18:15 -04:00
delete[] busyChannels;
}
// look for Tx buddies and get reference to common parameters
// take the first Rx channel
else if (m_deviceAPI->getSinkBuddies().size() > 0) // then sink
{
DeviceSinkAPI *sinkBuddy = m_deviceAPI->getSinkBuddies()[0];
2017-04-15 22:58:52 -04:00
m_deviceShared = *((DeviceLimeSDRShared *) sinkBuddy->getBuddySharedPtr()); // copy parameters
2017-04-14 12:30:37 -04:00
if (m_deviceShared.m_deviceParams == 0)
{
qCritical("LimeSDRInput::openDevice: cannot get device parameters from Tx buddy");
return false; // the device params should have been created by the buddy
}
m_deviceShared.m_channel = 0; // take first channel
2017-04-13 12:18:15 -04:00
}
// There are no buddies then create the first LimeSDR common parameters
// open the device this will also populate common fields
// take the first Rx channel
else
{
2017-04-14 12:30:37 -04:00
m_deviceShared.m_deviceParams = new DeviceLimeSDRParams();
2017-04-15 22:58:52 -04:00
char serial[256];
strcpy(serial, qPrintable(m_deviceAPI->getSampleSourceSerial()));
m_deviceShared.m_deviceParams->open(serial);
2017-04-14 12:30:37 -04:00
m_deviceShared.m_channel = 0; // take first channel
2017-04-13 12:18:15 -04:00
}
2017-04-14 12:30:37 -04:00
m_deviceAPI->setBuddySharedPtr(&m_deviceShared); // propagate common parameters to API
// acquire the channel
if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_RX, m_deviceShared.m_channel, true) != 0)
{
2017-04-15 22:58:52 -04:00
qCritical("LimeSDRInput::openDevice: cannot enable Rx channel %lu", m_deviceShared.m_channel);
2017-04-14 12:30:37 -04:00
return false;
}
2017-04-13 12:18:15 -04:00
return true;
}
void LimeSDRInput::closeDevice()
{
2017-04-14 12:30:37 -04:00
// release the channel
if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_RX, m_deviceShared.m_channel, false) != 0)
2017-04-13 12:18:15 -04:00
{
2017-04-15 22:58:52 -04:00
qWarning("LimeSDRInput::closeDevice: cannot disable Rx channel %lu", m_deviceShared.m_channel);
2017-04-13 12:18:15 -04:00
}
2017-04-13 17:40:14 -04:00
2017-04-14 12:30:37 -04:00
m_deviceShared.m_channel = -1;
// No buddies so effectively close the device
if ((m_deviceAPI->getSinkBuddies().size() == 0) && (m_deviceAPI->getSourceBuddies().size() == 0))
{
m_deviceShared.m_deviceParams->close();
delete m_deviceShared.m_deviceParams;
m_deviceShared.m_deviceParams = 0;
}
2017-04-13 12:18:15 -04:00
}
bool LimeSDRInput::start()
2017-04-13 12:18:15 -04:00
{
2017-04-15 04:31:16 -04:00
if (!m_deviceShared.m_deviceParams->getDevice()) {
2017-04-13 12:18:15 -04:00
return false;
}
if (m_running) stop();
2017-04-15 04:31:16 -04:00
// set up the stream
m_streamId.channel = m_deviceShared.m_channel; //channel number
m_streamId.fifoSize = 1024 * 128; //fifo size in samples
m_streamId.throughputVsLatency = 1.0; //optimize for max throughput
m_streamId.isTx = false; //RX channel
m_streamId.dataFmt = lms_stream_t::LMS_FMT_I12; //12-bit integers
if (LMS_SetupStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId) != 0)
{
2017-04-15 22:58:52 -04:00
qCritical("LimeSDRInput::start: cannot setup the stream on Rx channel %lu", m_deviceShared.m_channel);
2017-04-15 04:31:16 -04:00
return false;
}
// start / stop streaming is done in the thread.
if ((m_limeSDRInputThread = new LimeSDRInputThread(&m_streamId, &m_sampleFifo)) == 0)
2017-04-13 12:18:15 -04:00
{
qFatal("LimeSDRInput::start: out of memory");
stop();
return false;
}
2017-04-14 12:30:37 -04:00
m_limeSDRInputThread->setLog2Decimation(m_settings.m_log2SoftDecim);
2017-04-13 12:18:15 -04:00
m_limeSDRInputThread->setFcPos((int) m_settings.m_fcPos);
m_limeSDRInputThread->startWork();
applySettings(m_settings, true);
m_running = true;
return true;
}
void LimeSDRInput::stop()
{
if (m_limeSDRInputThread != 0)
{
m_limeSDRInputThread->stopWork();
delete m_limeSDRInputThread;
m_limeSDRInputThread = 0;
}
2017-04-15 04:31:16 -04:00
// destroy the stream
LMS_DestroyStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId);
2017-04-13 12:18:15 -04:00
m_running = false;
}
2017-04-15 22:58:52 -04:00
std::size_t LimeSDRInput::getChannelIndex()
{
return m_deviceShared.m_channel;
}
2017-04-15 05:45:01 -04:00
void LimeSDRInput::getLORange(float& minF, float& maxF, float& stepF) const
{
lms_range_t range = m_deviceShared.m_deviceParams->m_loRangeRx;
minF = range.min;
maxF = range.max;
stepF = range.step;
}
void LimeSDRInput::getSRRange(float& minF, float& maxF, float& stepF) const
{
lms_range_t range = m_deviceShared.m_deviceParams->m_srRangeRx;
minF = range.min;
maxF = range.max;
stepF = range.step;
}
void LimeSDRInput::getLPRange(float& minF, float& maxF, float& stepF) const
{
lms_range_t range = m_deviceShared.m_deviceParams->m_lpfRangeRx;
minF = range.min;
maxF = range.max;
stepF = range.step;
}
2017-04-15 22:58:52 -04:00
int LimeSDRInput::getLPIndex(float lpfBW) const
{
lms_range_t range = m_deviceShared.m_deviceParams->m_lpfRangeRx;
return (int) ((lpfBW - range.min) / range.step);
}
uint32_t LimeSDRInput::getHWLog2Decim() const
{
return m_deviceShared.m_deviceParams->m_log2OvSRRx;
}
2017-04-15 05:45:01 -04:00