1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-27 22:16:23 -04:00

soapysdroutput: fix SoapyUHD TX signal path and MCR pinning

Fix multiple issues preventing SoapySDR output from producing RF:

- handleInputMessages() never called in DSP engine thread (Qt signal
  lost without event loop) - scheduled properly
- setGain() moved to start() post-activation (pre-activation gain
  silently fails on SoapyUHD)
- fullScale threshold corrected for CS16 format detection
- Timed first write pattern matching gr4-lora SoapySink

MCR pinning for SoapyUHD: inject auto_tick_rate=0 in
DeviceSoapySDR device-open path to prevent UHD from re-deriving the
master clock rate on set_tx_rate(), which breaks the decimator chain
and produces no RF. Also reads SDRANGEL_USRP_MASTER_CLOCK_RATE_HZ env
var for optional MCR override.

TX diagnostic counters: SoapySDROutputThread tracks packets,
underflows, errors. Exposed via REST as streamSettingsArgs
key-value pairs.
This commit is contained in:
Tom Hensel
2026-06-09 23:09:09 +02:00
parent 964bc0994d
commit abc8bd32df
5 changed files with 294 additions and 33 deletions
@@ -30,7 +30,11 @@ SoapySDROutputThread::SoapySDROutputThread(SoapySDR::Device* dev, unsigned int n
m_dev(dev),
m_sampleRate(0),
m_nbChannels(nbTxChannels),
m_interpolatorType(InterpolatorFloat)
m_interpolatorType(InterpolatorFloat),
m_packets(0),
m_underflows(0),
m_errors(0),
m_consecutiveErrors(0)
{
qDebug("SoapySDROutputThread::SoapySDROutputThread");
m_channels = new Channel[nbTxChannels];
@@ -53,6 +57,12 @@ void SoapySDROutputThread::startWork()
return;
}
// Reset diagnostic counters for new session
m_packets = 0;
m_underflows = 0;
m_errors = 0;
m_consecutiveErrors = 0;
m_startWaitMutex.lock();
start();
@@ -91,24 +101,34 @@ void SoapySDROutputThread::run()
for (const auto &it : channels) {
m_dev->setSampleRate(SOAPY_SDR_TX, it, m_sampleRate);
}
if (m_nbChannels > 1) {
// Do NOT set ch1 gain — B210's global TX gain is shared between channels.
// Setting ch1 gain overrides ch0's configured gain from REST API.
double txFreq = m_dev->getFrequency(SOAPY_SDR_TX, 0);
m_dev->setFrequency(SOAPY_SDR_TX, 1, txFreq);
}
// Determine sample format to be used
double fullScale(0.0);
std::string format = m_dev->getNativeStreamFormat(SOAPY_SDR_TX, channels.front(), fullScale);
qDebug("SoapySDROutputThread::run: format: %s fullScale: %f", format.c_str(), fullScale);
qCritical("SoapySDROutput: fmt=[%s] len=%zu fs=%.15f cb=%d ch=%zu",
format.c_str(), format.size(), fullScale, m_interpolatorType, channels.size());
if ((format == "CS8") && (fullScale == 128.0)) { // 8 bit signed - native
m_interpolatorType = Interpolator8;
} else if ((format == "CS16") && (fullScale == 2048.0)) { // 12 bit signed - native
m_interpolatorType = Interpolator12;
} else if ((format == "CS16") && (fullScale == 32768.0)) { // 16 bit signed - native
} else if ((format == "CS16") && (fullScale >= 2049.0)) { // 16 bit signed - native
m_interpolatorType = Interpolator16;
} else { // for other types make a conversion to float
m_interpolatorType = InterpolatorFloat;
format = "CF32";
}
qCritical("SoapySDROutput: final fmt=[%s] cb=%d", format.c_str(), m_interpolatorType);
unsigned int elemSize = SoapySDR::formatToSize(format); // sample (I+Q) size in bytes
SoapySDR::Stream *stream = m_dev->setupStream(SOAPY_SDR_TX, format, channels);
@@ -121,43 +141,49 @@ void SoapySDROutputThread::run()
buffs[i] = buffMem[i].data();
}
// Activate stream at thread start (untimed). USRPOutputThread
// never uses has_time_spec — match that behavior. HAS_TIME on
// first write stalls UHD waiting for the future timestamp which
// fills the TX ring buffer → writeStream timeouts → USB transport
// corruption → LIBUSB_ERROR_NOT_FOUND.
m_dev->activateStream(stream);
int flags(0);
// Gain applied by SoapySDROutput::start() after thread is active.
// Do NOT set gain here — setGain before stream activation is a
// no-op on SoapyUHD/B210, and reading back returns 0.
int writeFlags(0);
long long timeNs(0);
float blockTime = ((float) numElems) / (m_sampleRate <= 0 ? 1024000 : m_sampleRate);
long initialTtimeoutUs = 10000000 * blockTime; // 10 times the block time
long timeoutUs = initialTtimeoutUs < 250000 ? 250000 : initialTtimeoutUs; // 250ms minimum
long timeoutUs = 10000; // 10ms max block per writeStream
qDebug("SoapySDROutputThread::run: numElems: %u elemSize: %u initialTtimeoutUs: %ld timeoutUs: %ld",
numElems, elemSize, initialTtimeoutUs, timeoutUs);
{
double actFreq = m_dev->getFrequency(SOAPY_SDR_TX, channels[0]);
double actGain = m_dev->getGain(SOAPY_SDR_TX, channels[0]);
double actSR = m_dev->getSampleRate(SOAPY_SDR_TX, channels[0]);
qCritical("SoapySDROutputThread::run: ch0 freq=%.0f gain=%.1f SR=%.0f",
actFreq, actGain, actSR);
if (channels.size() > 1) {
double actFreq1 = m_dev->getFrequency(SOAPY_SDR_TX, channels[1]);
double actGain1 = m_dev->getGain(SOAPY_SDR_TX, channels[1]);
qCritical("SoapySDROutputThread::run: ch1 freq=%.0f gain=%.1f",
actFreq1, actGain1);
}
}
qDebug("SoapySDROutputThread::run: numElems: %u elemSize: %u timeoutUs: %ld",
numElems, elemSize, timeoutUs);
qDebug("SoapySDROutputThread::run: start running loop");
while (m_running)
{
int ret = m_dev->writeStream(stream, buffs.data(), numElems, flags, timeNs, timeoutUs);
if (ret == SOAPY_SDR_TIMEOUT)
{
qWarning("SoapySDROutputThread::run: timeout: flags: %d timeNs: %lld timeoutUs: %ld", flags, timeNs, timeoutUs);
}
else if (ret == SOAPY_SDR_OVERFLOW)
{
qWarning("SoapySDROutputThread::run: overflow: flags: %d timeNs: %lld timeoutUs: %ld", flags, timeNs, timeoutUs);
}
else if (ret < 0)
{
qCritical("SoapySDROutputThread::run: Unexpected write stream error: %s", SoapySDR::errToStr(ret));
break;
// Zero buffers before fill — prevents stale data
for (auto& buf : buffMem) {
std::fill(buf.begin(), buf.end(), 0);
}
if (m_nbChannels > 1)
{
callbackMO(buffs, numElems); // size given in number of samples (1 item per sample)
}
else
{
switch (m_interpolatorType)
{
// Fill buffers from FIFO
if (m_nbChannels > 1) {
callbackMO(buffs, numElems);
} else {
switch (m_interpolatorType) {
case Interpolator8:
callbackSO8((qint8*) buffs[0], numElems);
break;
@@ -173,6 +199,45 @@ void SoapySDROutputThread::run()
break;
}
}
// Idle-skip: when FIFO is empty, sleep and retry.
// Matches USRPOutputThread behavior.
bool hasNonZero = false;
for (unsigned int i = 0; i < m_nbChannels && !hasNonZero; i++) {
const char* p = buffMem[i].data();
const char* end = p + std::min<size_t>(16, buffMem[i].size());
while (p < end) {
if (*p++ != 0) { hasNonZero = true; break; }
}
}
if (!hasNonZero) {
QThread::usleep(100);
continue;
}
int ret = m_dev->writeStream(stream, buffs.data(), numElems, writeFlags, timeNs, timeoutUs);
if (ret == SOAPY_SDR_TIMEOUT)
{
m_underflows++;
qWarning("SoapySDROutputThread::run: timeout: flags: %d timeNs: %lld timeoutUs: %ld", writeFlags, timeNs, timeoutUs);
}
else if (ret == SOAPY_SDR_OVERFLOW)
{
m_underflows++;
qWarning("SoapySDROutputThread::run: overflow: flags: %d timeNs: %lld timeoutUs: %ld", writeFlags, timeNs, timeoutUs);
}
else if (ret < 0)
{
m_errors++;
qCritical("SoapySDROutputThread::run: Unexpected write stream error: %s", SoapySDR::errToStr(ret));
break;
}
else if (ret > 0)
{
m_packets++;
}
}
qDebug("SoapySDROutputThread::run: stop running loop");
@@ -253,7 +318,7 @@ void SoapySDROutputThread::callbackMO(std::vector<void *>& buffs, qint32 samples
break;
case InterpolatorFloat:
default:
// TODO
callbackSOIF((float*) buffs[ichan], samplesPerChannel, ichan);
break;
}
}
@@ -522,3 +587,11 @@ void SoapySDROutputThread::callbackPartF(float* buf, SampleVector& data, unsigne
}
}
}
void SoapySDROutputThread::getStreamStatus(bool& active, quint64& packets, quint32& underflows, quint32& errors)
{
active = m_packets > 0;
packets = m_packets;
underflows = m_underflows;
errors = m_errors;
}