diff --git a/devices/limesdr/devicelimesdrshared.h b/devices/limesdr/devicelimesdrshared.h index 130f67cf3..1ed170e64 100644 --- a/devices/limesdr/devicelimesdrshared.h +++ b/devices/limesdr/devicelimesdrshared.h @@ -73,14 +73,16 @@ public: virtual void startWork() = 0; virtual void stopWork() = 0; virtual void setDeviceSampleRate(int sampleRate) = 0; + virtual bool isRunning() = 0; }; DeviceLimeSDRParams *m_deviceParams; //!< unique hardware device parameters - std::size_t m_channel; //!< logical device channel number (-1 if none) + int m_channel; //!< logical device channel number (-1 if none) ThreadInterface *m_thread; //!< holds the thread address if started else 0 int m_ncoFrequency; uint64_t m_centerFrequency; uint32_t m_log2Soft; + bool m_threadWasRunning; //!< flag to know if thread needs to be resumed after suspend static const float m_sampleFifoLengthInSeconds; static const int m_sampleFifoMinSize; @@ -91,7 +93,8 @@ public: m_thread(0), m_ncoFrequency(0), m_centerFrequency(0), - m_log2Soft(0) + m_log2Soft(0), + m_threadWasRunning(false) {} ~DeviceLimeSDRShared() diff --git a/plugins/samplesink/limesdroutput/limesdroutput.cpp b/plugins/samplesink/limesdroutput/limesdroutput.cpp index 94b3c950c..9ce814df8 100644 --- a/plugins/samplesink/limesdroutput/limesdroutput.cpp +++ b/plugins/samplesink/limesdroutput/limesdroutput.cpp @@ -42,7 +42,8 @@ LimeSDROutput::LimeSDROutput(DeviceSinkAPI *deviceAPI) : m_limeSDROutputThread(0), m_deviceDescription(), m_running(false), - m_firstConfig(true) + m_firstConfig(true), + m_channelAcquired(false) { m_streamId.handle = 0; suspendBuddies(); @@ -98,7 +99,10 @@ bool LimeSDROutput::openDevice() { DeviceSinkAPI *buddy = m_deviceAPI->getSinkBuddies()[i]; DeviceLimeSDRShared *buddyShared = (DeviceLimeSDRShared *) buddy->getBuddySharedPtr(); - busyChannels[buddyShared->m_channel] = 1; + + if (buddyShared->m_channel >= 0) { + busyChannels[buddyShared->m_channel] = 1; + } } std::size_t ch = 0; @@ -150,36 +154,6 @@ bool LimeSDROutput::openDevice() m_deviceAPI->setBuddySharedPtr(&m_deviceShared); // propagate common parameters to API - // acquire the channel - - if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, m_deviceShared.m_channel, true) != 0) - { - qCritical("LimeSDROutput::openDevice: cannot enable Tx channel %lu", m_deviceShared.m_channel); - return false; - } - else - { - qDebug("LimeSDROutput::openDevice: Tx channel %lu enabled", m_deviceShared.m_channel); - } - - // set up the stream - - m_streamId.channel = m_deviceShared.m_channel; // channel number - m_streamId.fifoSize = 512 * 1024; // fifo size in samples (SR / 10 take ~5MS/s) - m_streamId.throughputVsLatency = 0.0; // optimize for min latency - m_streamId.isTx = true; // TX channel - m_streamId.dataFmt = lms_stream_t::LMS_FMT_I12; // 12-bit integers - - if (LMS_SetupStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId) != 0) - { - qCritical("LimeSDROutput::start: cannot setup the stream on Tx channel %lu", m_deviceShared.m_channel); - return false; - } - else - { - qDebug("LimeSDROutput::start: stream set up on Tx channel %lu", m_deviceShared.m_channel); - } - return true; } @@ -243,18 +217,7 @@ void LimeSDROutput::closeDevice() return; } - // destroy the stream - LMS_DestroyStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId); - m_streamId.handle = 0; - - // release the channel - - if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, m_deviceShared.m_channel, false) != 0) - { - qWarning("LimeSDROutput::closeDevice: cannot disable Tx channel %lu", m_deviceShared.m_channel); - } - - m_deviceShared.m_channel = -1; + if (m_running) stop(); // No buddies so effectively close the device @@ -264,6 +227,60 @@ void LimeSDROutput::closeDevice() delete m_deviceShared.m_deviceParams; m_deviceShared.m_deviceParams = 0; } + + m_deviceShared.m_channel = -1; // effectively release the channel for the possible buddies +} + +bool LimeSDROutput::acquireChannel() +{ + // acquire the channel + + if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, m_deviceShared.m_channel, true) != 0) + { + qCritical("LimeSDROutput::acquireChannel: cannot enable Tx channel %d", m_deviceShared.m_channel); + return false; + } + else + { + qDebug("LimeSDROutput::acquireChannel: Tx channel %d enabled", m_deviceShared.m_channel); + } + + // set up the stream + + m_streamId.channel = m_deviceShared.m_channel; // channel number + m_streamId.fifoSize = 512 * 1024; // fifo size in samples (SR / 10 take ~5MS/s) + m_streamId.throughputVsLatency = 0.0; // optimize for min latency + m_streamId.isTx = true; // TX channel + m_streamId.dataFmt = lms_stream_t::LMS_FMT_I12; // 12-bit integers + + if (LMS_SetupStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId) != 0) + { + qCritical("LimeSDROutput::acquireChannel: cannot setup the stream on Tx channel %d", m_deviceShared.m_channel); + return false; + } + else + { + qDebug("LimeSDROutput::acquireChannel: stream set up on Tx channel %d", m_deviceShared.m_channel); + } + + m_channelAcquired = true; + return true; +} + +void LimeSDROutput::releaseChannel() +{ + // destroy the stream + LMS_DestroyStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId); + m_streamId.handle = 0; + + // release the channel + + if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, m_deviceShared.m_channel, false) != 0) + { + qWarning("LimeSDROutput::releaseChannel: cannot disable Tx channel %d", m_deviceShared.m_channel); + } + + m_channelAcquired = false; } bool LimeSDROutput::start() @@ -274,6 +291,11 @@ bool LimeSDROutput::start() if (m_running) stop(); + if (!acquireChannel()) + { + return false; + } + applySettings(m_settings, true); // start / stop streaming is done in the thread. @@ -310,6 +332,8 @@ void LimeSDROutput::stop() m_deviceShared.m_thread = 0; m_running = false; + + releaseChannel(); } const QString& LimeSDROutput::getDeviceDescription() const @@ -481,6 +505,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo bool forwardChangeTxDSP = false; bool forwardChangeAllDSP = false; bool suspendOwnThread = false; + bool ownThreadWasRunning = false; bool suspendTxThread = false; bool suspendAllThread = false; bool doCalibration = false; @@ -522,8 +547,14 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_thread && buddySharedPtr->m_thread->isRunning()) + { buddySharedPtr->m_thread->stopWork(); + buddySharedPtr->m_threadWasRunning = true; + } + else + { + buddySharedPtr->m_threadWasRunning = false; } } @@ -534,13 +565,21 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSink)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_thread && buddySharedPtr->m_thread->isRunning()) + { buddySharedPtr->m_thread->stopWork(); + buddySharedPtr->m_threadWasRunning = true; + } + else + { + buddySharedPtr->m_threadWasRunning = false; } } - if (m_limeSDROutputThread) { + if (m_limeSDROutputThread && m_limeSDROutputThread->isRunning()) + { m_limeSDROutputThread->stopWork(); + ownThreadWasRunning = true; } } else if (suspendTxThread) @@ -557,14 +596,18 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo } } - if (m_limeSDROutputThread) { + if (m_limeSDROutputThread && m_limeSDROutputThread->isRunning()) + { m_limeSDROutputThread->stopWork(); + ownThreadWasRunning = true; } } else if (suspendOwnThread) { - if (m_limeSDROutputThread) { + if (m_limeSDROutputThread && m_limeSDROutputThread->isRunning()) + { m_limeSDROutputThread->stopWork(); + ownThreadWasRunning = true; } } @@ -572,7 +615,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo if ((m_settings.m_gain != settings.m_gain) || force) { - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (LMS_SetGaindB(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -630,7 +673,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo if ((m_settings.m_lpfBW != settings.m_lpfBW) || force) { - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (LMS_SetLPFBW(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -650,7 +693,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo if ((m_settings.m_lpfFIRBW != settings.m_lpfFIRBW) || (m_settings.m_lpfFIREnable != settings.m_lpfFIREnable) || force) { - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (LMS_SetGFIRLPF(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -675,7 +718,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo if ((m_settings.m_ncoFrequency != settings.m_ncoFrequency) || (m_settings.m_ncoEnable != settings.m_ncoEnable) || force || forceNCOFrequency) { - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (DeviceLimeSDR::setNCOFrequency(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -713,7 +756,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo if ((m_settings.m_antennaPath != settings.m_antennaPath) || force) { - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (DeviceLimeSDR::setTxAntennaPath(m_deviceShared.m_deviceParams->getDevice(), m_deviceShared.m_channel, @@ -735,7 +778,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { forwardChangeTxDSP = true; - if (m_deviceShared.m_deviceParams->getDevice() != 0) + if (m_deviceShared.m_deviceParams->getDevice() != 0 && m_channelAcquired) { if (LMS_SetLOFrequency(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -755,7 +798,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo m_settings = settings; - if (doCalibration) + if (doCalibration && m_channelAcquired) { if (LMS_Calibrate(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_TX, @@ -763,11 +806,11 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo m_settings.m_lpfBW, 0) < 0) { - qCritical("LimeSDROutput::applySettings: calibration failed on Tx channel %lu", m_deviceShared.m_channel); + qCritical("LimeSDROutput::applySettings: calibration failed on Tx channel %d", m_deviceShared.m_channel); } else { - qDebug("LimeSDROutput::applySettings: calibration successful on Tx channel %lu", m_deviceShared.m_channel); + qDebug("LimeSDROutput::applySettings: calibration successful on Tx channel %d", m_deviceShared.m_channel); } } @@ -782,7 +825,7 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } @@ -794,12 +837,12 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSink)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } - if (m_limeSDROutputThread) { + if (ownThreadWasRunning) { m_limeSDROutputThread->startWork(); } } @@ -812,16 +855,16 @@ bool LimeSDROutput::applySettings(const LimeSDROutputSettings& settings, bool fo { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSink)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } - if (m_limeSDROutputThread) { + if (ownThreadWasRunning) { m_limeSDROutputThread->startWork(); } } - else if (suspendOwnThread) + else if (ownThreadWasRunning) { if (m_limeSDROutputThread) { m_limeSDROutputThread->startWork(); diff --git a/plugins/samplesink/limesdroutput/limesdroutput.h b/plugins/samplesink/limesdroutput/limesdroutput.h index 81abf835a..f5238644d 100644 --- a/plugins/samplesink/limesdroutput/limesdroutput.h +++ b/plugins/samplesink/limesdroutput/limesdroutput.h @@ -235,11 +235,14 @@ private: bool m_running; DeviceLimeSDRShared m_deviceShared; bool m_firstConfig; + bool m_channelAcquired; lms_stream_t m_streamId; bool openDevice(); void closeDevice(); + bool acquireChannel(); + void releaseChannel(); void suspendBuddies(); void resumeBuddies(); bool applySettings(const LimeSDROutputSettings& settings, bool force = false); diff --git a/plugins/samplesink/limesdroutput/limesdroutputthread.h b/plugins/samplesink/limesdroutput/limesdroutputthread.h index 7587321c0..7acd125cc 100644 --- a/plugins/samplesink/limesdroutput/limesdroutputthread.h +++ b/plugins/samplesink/limesdroutput/limesdroutputthread.h @@ -40,6 +40,7 @@ public: virtual void startWork(); virtual void stopWork(); virtual void setDeviceSampleRate(int __attribute__((unused)) sampleRate) {} + virtual bool isRunning() { return m_running; } void setLog2Interpolation(unsigned int log2_ioterp); void setFcPos(int fcPos); diff --git a/plugins/samplesource/limesdrinput/limesdrinput.cpp b/plugins/samplesource/limesdrinput/limesdrinput.cpp index f1dc4fade..b266d4b00 100644 --- a/plugins/samplesource/limesdrinput/limesdrinput.cpp +++ b/plugins/samplesource/limesdrinput/limesdrinput.cpp @@ -109,7 +109,10 @@ bool LimeSDRInput::openDevice() { DeviceSourceAPI *buddy = m_deviceAPI->getSourceBuddies()[i]; DeviceLimeSDRShared *buddyShared = (DeviceLimeSDRShared *) buddy->getBuddySharedPtr(); - busyChannels[buddyShared->m_channel] = 1; + + if (buddyShared->m_channel >= 0) { + busyChannels[buddyShared->m_channel] = 1; + } } std::size_t ch = 0; @@ -165,12 +168,12 @@ bool LimeSDRInput::openDevice() if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_RX, m_deviceShared.m_channel, true) != 0) { - qCritical("LimeSDRInput::openDevice: cannot enable Rx channel %lu", m_deviceShared.m_channel); + qCritical("LimeSDRInput::openDevice: cannot enable Rx channel %d", m_deviceShared.m_channel); return false; } else { - qDebug("LimeSDRInput::openDevice: Rx channel %lu enabled", m_deviceShared.m_channel); + qDebug("LimeSDRInput::openDevice: Rx channel %d enabled", m_deviceShared.m_channel); } // set up the stream @@ -183,12 +186,12 @@ bool LimeSDRInput::openDevice() if (LMS_SetupStream(m_deviceShared.m_deviceParams->getDevice(), &m_streamId) != 0) { - qCritical("LimeSDRInput::start: cannot setup the stream on Rx channel %lu", m_deviceShared.m_channel); + qCritical("LimeSDRInput::start: cannot setup the stream on Rx channel %d", m_deviceShared.m_channel); return false; } else { - qDebug("LimeSDRInput::start: stream set up on Rx channel %lu", m_deviceShared.m_channel); + qDebug("LimeSDRInput::start: stream set up on Rx channel %d", m_deviceShared.m_channel); } return true; @@ -262,7 +265,7 @@ void LimeSDRInput::closeDevice() if (LMS_EnableChannel(m_deviceShared.m_deviceParams->getDevice(), LMS_CH_RX, m_deviceShared.m_channel, false) != 0) { - qWarning("LimeSDRInput::closeDevice: cannot disable Rx channel %lu", m_deviceShared.m_channel); + qWarning("LimeSDRInput::closeDevice: cannot disable Rx channel %d", m_deviceShared.m_channel); } m_deviceShared.m_channel = -1; @@ -492,6 +495,7 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc bool forwardChangeRxDSP = false; bool forwardChangeAllDSP = false; bool suspendOwnThread = false; + bool ownThreadWasRunning = false; bool suspendRxThread = false; bool suspendAllThread = false; bool doCalibration = false; @@ -540,8 +544,14 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_thread && buddySharedPtr->m_thread->isRunning()) + { buddySharedPtr->m_thread->stopWork(); + buddySharedPtr->m_threadWasRunning = true; + } + else + { + buddySharedPtr->m_threadWasRunning = false; } } @@ -554,11 +564,18 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc if (buddySharedPtr->m_thread) { buddySharedPtr->m_thread->stopWork(); + buddySharedPtr->m_threadWasRunning = true; + } + else + { + buddySharedPtr->m_threadWasRunning = false; } } - if (m_limeSDRInputThread) { + if (m_limeSDRInputThread && m_limeSDRInputThread->isRunning()) + { m_limeSDRInputThread->stopWork(); + ownThreadWasRunning = true; } } else if (suspendRxThread) @@ -575,14 +592,18 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc } } - if (m_limeSDRInputThread) { + if (m_limeSDRInputThread && m_limeSDRInputThread->isRunning()) + { m_limeSDRInputThread->stopWork(); + ownThreadWasRunning = true; } } else if (suspendOwnThread) { - if (m_limeSDRInputThread) { + if (m_limeSDRInputThread && m_limeSDRInputThread->isRunning()) + { m_limeSDRInputThread->stopWork(); + ownThreadWasRunning = true; } } @@ -926,11 +947,11 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc m_settings.m_lpfBW, 0) < 0) { - qCritical("LimeSDRInput::applySettings: calibration failed on Rx channel %lu", m_deviceShared.m_channel); + qCritical("LimeSDRInput::applySettings: calibration failed on Rx channel %d", m_deviceShared.m_channel); } else { - qDebug("LimeSDRInput::applySettings: calibration successful on Rx channel %lu", m_deviceShared.m_channel); + qDebug("LimeSDRInput::applySettings: calibration successful on Rx channel %d", m_deviceShared.m_channel); } } @@ -945,7 +966,7 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } @@ -957,12 +978,12 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSink)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } - if (m_limeSDRInputThread) { + if (ownThreadWasRunning) { m_limeSDRInputThread->startWork(); } } @@ -975,18 +996,18 @@ bool LimeSDRInput::applySettings(const LimeSDRInputSettings& settings, bool forc { DeviceLimeSDRShared *buddySharedPtr = (DeviceLimeSDRShared *) (*itSource)->getBuddySharedPtr(); - if (buddySharedPtr->m_thread) { + if (buddySharedPtr->m_threadWasRunning) { buddySharedPtr->m_thread->startWork(); } } - if (m_limeSDRInputThread) { + if (ownThreadWasRunning) { m_limeSDRInputThread->startWork(); } } else if (suspendOwnThread) { - if (m_limeSDRInputThread) { + if (ownThreadWasRunning) { m_limeSDRInputThread->startWork(); } } diff --git a/plugins/samplesource/limesdrinput/limesdrinputthread.h b/plugins/samplesource/limesdrinput/limesdrinputthread.h index 38dbec979..57577d78f 100644 --- a/plugins/samplesource/limesdrinput/limesdrinputthread.h +++ b/plugins/samplesource/limesdrinput/limesdrinputthread.h @@ -40,6 +40,7 @@ public: virtual void startWork(); virtual void stopWork(); virtual void setDeviceSampleRate(int sampleRate __attribute__((unused))) {} + virtual bool isRunning() { return m_running; } void setLog2Decimation(unsigned int log2_decim); void setFcPos(int fcPos);