1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-07 08:24:43 -04:00

USRP driver improvements.

Set bit size to be 16 for interpolators and decimators.
Match buffer size to UHD.
Don't destroy TX stream, as there appears to be a bug when recreating it.
Catch exception when checking for TX underflow.
Increase RX FIFO size, to reduce overflows at high sample rates.
Allow RX to continue streaming after receiving timeout.
This commit is contained in:
Jon Beniston
2020-10-23 13:12:37 +01:00
parent 8b05670814
commit 209be94947
9 changed files with 115 additions and 59 deletions
+8 -2
View File
@@ -51,6 +51,7 @@ USRPInput::USRPInput(DeviceAPI *deviceAPI) :
m_deviceAPI(deviceAPI),
m_settings(),
m_usrpInputThread(nullptr),
m_bufSamples(0),
m_deviceDescription("USRPInput"),
m_running(false),
m_channelAcquired(false)
@@ -91,7 +92,9 @@ void USRPInput::destroy()
bool USRPInput::openDevice()
{
if (!m_sampleFifo.setSize(96000 * 4))
// B210 supports up to 50MSa/s, so a fairly large FIFO is probably a good idea
// Should it be bigger still?
if (!m_sampleFifo.setSize(2000000))
{
qCritical("USRPInput::openDevice: could not allocate SampleFifo");
return false;
@@ -325,6 +328,9 @@ bool USRPInput::acquireChannel()
stream_args.channels = channel_nums;
m_streamId = m_deviceShared.m_deviceParams->getDevice()->get_rx_stream(stream_args);
// Match our receive buffer size to what UHD uses
m_bufSamples = m_streamId->get_max_num_samps();
}
catch (std::exception& e)
{
@@ -375,7 +381,7 @@ bool USRPInput::start()
// start / stop streaming is done in the thread.
m_usrpInputThread = new USRPInputThread(m_streamId, &m_sampleFifo);
m_usrpInputThread = new USRPInputThread(m_streamId, m_bufSamples, &m_sampleFifo);
qDebug("USRPInput::start: thread created");
applySettings(m_settings, true);
@@ -227,6 +227,7 @@ private:
DeviceUSRPShared m_deviceShared;
bool m_channelAcquired;
uhd::rx_streamer::sptr m_streamId;
size_t m_bufSamples;
QNetworkAccessManager *m_networkManager;
QNetworkRequest m_networkRequest;
@@ -26,20 +26,35 @@
#include "usrpinputsettings.h"
#include "usrpinputthread.h"
USRPInputThread::USRPInputThread(uhd::rx_streamer::sptr stream, SampleSinkFifo* sampleFifo, QObject* parent) :
USRPInputThread::USRPInputThread(uhd::rx_streamer::sptr stream, size_t bufSamples, SampleSinkFifo* sampleFifo, QObject* parent) :
QThread(parent),
m_running(false),
m_stream(stream),
m_convertBuffer(DeviceUSRP::blockSize),
m_bufSamples(bufSamples),
m_convertBuffer(bufSamples),
m_sampleFifo(sampleFifo),
m_log2Decim(0)
{
std::fill(m_buf, m_buf + 2*DeviceUSRP::blockSize, 0);
// *2 as samples are I+Q
m_buf = new qint16[2*bufSamples];
std::fill(m_buf, m_buf + 2*bufSamples, 0);
}
USRPInputThread::~USRPInputThread()
{
stopWork();
delete m_buf;
}
void USRPInputThread::issueStreamCmd(bool start)
{
uhd::stream_cmd_t stream_cmd(start ? uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS : uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
stream_cmd.num_samps = size_t(0);
stream_cmd.stream_now = true;
stream_cmd.time_spec = uhd::time_spec_t();
m_stream->issue_stream_cmd(stream_cmd);
qDebug() << "USRPInputThread::issueStreamCmd " << (start ? "start" : "stop");
}
void USRPInputThread::startWork()
@@ -48,12 +63,8 @@ void USRPInputThread::startWork()
try
{
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS);
stream_cmd.num_samps = size_t(0);
stream_cmd.stream_now = true;
stream_cmd.time_spec = uhd::time_spec_t();
m_stream->issue_stream_cmd(stream_cmd);
// Start streaming
issueStreamCmd(true);
// Reset stats
m_packets = 0;
@@ -84,18 +95,20 @@ void USRPInputThread::stopWork()
try
{
uhd::rx_metadata_t md;
uhd::stream_cmd_t stream_cmd(uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS);
stream_cmd.stream_now = true;
m_stream->issue_stream_cmd(stream_cmd);
// Stop streaming
issueStreamCmd(false);
// Clear out any data left in the stream, otherwise we'll get an
// exception 'recv buffer smaller than vrt packet offset' when restarting
while (!md.end_of_burst)
md.end_of_burst = false;
md.error_code = uhd::rx_metadata_t::ERROR_CODE_NONE;
while (!md.end_of_burst && (md.error_code != uhd::rx_metadata_t::ERROR_CODE_TIMEOUT))
{
try
{
//qDebug() << "USRPInputThread::stopWork: recing until end of burst";
m_stream->recv(m_buf, DeviceUSRP::blockSize, md);
md.reset();
m_stream->recv(m_buf, m_bufSamples, md);
}
catch (std::exception& e)
{
@@ -127,27 +140,39 @@ void USRPInputThread::run()
{
while (m_running)
{
const size_t samples_received = m_stream->recv(m_buf, DeviceUSRP::blockSize, md);
md.reset();
const size_t samples_received = m_stream->recv(m_buf, m_bufSamples, md);
m_packets++;
if (samples_received != DeviceUSRP::blockSize)
if (samples_received != m_bufSamples)
{
qDebug("USRPInputThread::run - received %ld/%d samples", samples_received, DeviceUSRP::blockSize);
qDebug("USRPInputThread::run - received %ld/%d samples", samples_received, m_bufSamples);
}
if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_TIMEOUT)
{
qDebug("USRPInputThread::run - timeout - ending thread");
m_timeouts++;
// It seems we can't recover after a timeout, so stop thread
m_running = false;
// Restart streaming
issueStreamCmd(false);
issueStreamCmd(true);
qDebug("USRPInputThread::run - timeout - restarting");
}
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_OVERFLOW)
{
qDebug("USRPInputThread::run - overflow");
m_overflows++;
}
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_LATE_COMMAND)
qDebug("USRPInputThread::run - late command error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_BROKEN_CHAIN)
qDebug("USRPInputThread::run - broken chain error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_ALIGNMENT)
qDebug("USRPInputThread::run - alignment error");
else if (md.error_code == uhd::rx_metadata_t::ERROR_CODE_BAD_PACKET)
qDebug("USRPInputThread::run - bad packet error");
callbackIQ(m_buf, 2 * samples_received);
if (samples_received > 0)
callbackIQ(m_buf, 2 * samples_received);
}
}
catch (std::exception& e)
@@ -36,7 +36,7 @@ class USRPInputThread : public QThread, public DeviceUSRPShared::ThreadInterface
Q_OBJECT
public:
USRPInputThread(uhd::rx_streamer::sptr stream, SampleSinkFifo* sampleFifo, QObject* parent = 0);
USRPInputThread(uhd::rx_streamer::sptr stream, size_t bufSamples, SampleSinkFifo* sampleFifo, QObject* parent = 0);
~USRPInputThread();
virtual void startWork();
@@ -45,6 +45,7 @@ public:
virtual bool isRunning() { return m_running; }
void setLog2Decimation(unsigned int log2_decim);
void getStreamStatus(bool& active, quint32& overflows, quint32& m_timeouts);
void issueStreamCmd(bool start);
private:
QMutex m_startWaitMutex;
@@ -56,13 +57,14 @@ private:
quint32 m_timeouts;
uhd::rx_streamer::sptr m_stream;
qint16 m_buf[2*DeviceUSRP::blockSize]; //must hold I+Q values of each sample hence 2xcomplex size
qint16 *m_buf;
size_t m_bufSamples;
SampleVector m_convertBuffer;
SampleSinkFifo* m_sampleFifo;
unsigned int m_log2Decim; // soft decimation
Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 12, true> m_decimatorsIQ;
Decimators<qint32, qint16, SDR_RX_SAMP_SZ, 16, true> m_decimatorsIQ;
void run();
void callbackIQ(const qint16* buf, qint32 len);