Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB Hotplug Disconnect events #49

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
12 changes: 12 additions & 0 deletions GUI/fftviewer_wxgui/fftviewer_frFFTviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "limesuiteng/StreamConfig.h"
#include "limesuiteng/complex.h"
#include <array>
#include <optional>

using namespace std;
using namespace lime;
Expand Down Expand Up @@ -463,9 +464,16 @@ void fftviewer_frFFTviewer::StreamingLoop(

const lime::complex32f_t* src[2] = { txPattern[0].data(), txPattern[1].data() };*/

std::optional<std::size_t> callbackId{ std::nullopt };
try
{
pthis->device->StreamSetup(config, chipIndex);
callbackId = pthis->device->AddHotplugDisconnectCallback(
[](void* data) {
auto* const viewer = reinterpret_cast<fftviewer_frFFTviewer*>(data);
viewer->StopStreaming();
},
pthis);
pthis->device->StreamStart(chipIndex);
} catch (std::logic_error& e)
{
Expand Down Expand Up @@ -646,6 +654,10 @@ void fftviewer_frFFTviewer::StreamingLoop(
}*/

kiss_fft_free(m_fftCalcPlan);
if (callbackId)
{
pthis->device->RemoveHotplugDisconnectCallback(callbackId.value());
}
pthis->stopProcessing.store(true);
pthis->device->StreamStop(chipIndex);
pthis->device->StreamDestroy(chipIndex);
Expand Down
18 changes: 13 additions & 5 deletions GUI/limeGUIFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#endif //__BORLANDC__

#include <wx/msgdlg.h>
#include <wx/string.h>

#include "chips/LMS7002M/lms7002_mainPanel.h"

#include "limeGUIFrame.h"
#include "dlgAbout.h"
#include "lms7suiteEvents.h"
Expand All @@ -19,9 +19,7 @@
#include "utility/pnlMiniLog.h"
#include "FPGAcontrols_wxgui.h"
#include "utility/SPI_wxgui.h"
#include <wx/string.h>
#include "utility/dlgDeviceInfo.h"
#include <functional>
#include "boards/pnlBoardControls.h"
#include "protocols/LMSBoards.h"
#include "utility/SPI_wxgui.h"
Expand All @@ -34,6 +32,9 @@
#include "limesuiteng/SDRDescriptor.h"
#include "DeviceTreeNode.h"
#include "limesuiteng/Logger.h"
#include "DeviceConnectionPanel.h"

#include <functional>

using namespace std;
using namespace lime;
Expand All @@ -43,8 +44,6 @@ static constexpr int controlColumn = 1;

limeGUIFrame* limeGUIFrame::obj_ptr = nullptr;

int limeGUIFrame::m_lmsSelection = 0;

void limeGUIFrame::OnGlobalLogEvent(const lime::LogLevel level, const std::string& message)
{
if (obj_ptr == nullptr || obj_ptr->mMiniLog == nullptr)
Expand Down Expand Up @@ -399,6 +398,15 @@ void limeGUIFrame::OnDeviceHandleChange(wxCommandEvent& event)
wxPostEvent(this, evt);
UpdateConnections(lmsControl);

lmsControl->AddHotplugDisconnectCallback(
[&](void* data) {
auto* evt = new wxCommandEvent();
evt->SetEventType(limeEVT_SDR_HANDLE_SELECTED);
evt->SetString(wxEmptyString);
wxQueueEvent(this, evt);
},
this);

Fit();
} catch (std::runtime_error& e)
{
Expand Down
24 changes: 12 additions & 12 deletions GUI/limeGUIFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,28 @@
#include <wx/choice.h>

#include <map>
#include "IModuleFrame.h"
#include "ISOCPanel.h"
#include "DeviceConnectionPanel.h"
#include "dlgAbout.h"
#include "limeGUI.h"

class pnlMiniLog;
class lms7002_mainPanel;
class fftviewer_frFFTviewer;
class LMS_Programming_wxgui;
class pnlBoardControls;
class IModuleFrame;
class ISOCPanel;

namespace lime {
class SDRDevice;
class DeviceConnectionPanel;
} // namespace lime

class limeGUIFrame : public wxFrame
{
protected:
public:
limeGUIFrame(wxWindow* parent, const AppArgs& appArgs);
~limeGUIFrame();

private:
void AddModule(IModuleFrame* module, const std::string& title);
void RemoveModule(IModuleFrame* module);
// Handlers for AppFrame events.
Expand All @@ -57,13 +64,6 @@ class limeGUIFrame : public wxFrame

void DeviceTreeSelectionChanged(wxTreeEvent& event);

public:
limeGUIFrame(wxWindow* parent, const AppArgs& appArgs);

virtual ~limeGUIFrame();
static int m_lmsSelection;

protected:
static void OnGlobalLogEvent(const lime::LogLevel level, const std::string& message);
void OnLogMessage(wxCommandEvent& event);
void UpdateConnections(lime::SDRDevice* port);
Expand Down
2 changes: 1 addition & 1 deletion embedded/lms7002m/privates.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void lms7002m_sleep(long timeInMicroseconds)
void lms7002m_spi_write(lms7002m_context* self, uint16_t address, uint16_t value)
{
uint32_t mosi = address << 16 | value;
mosi |= 1 << 31;
mosi |= 0x80000000;
self->hooks.spi16_transact(&mosi, NULL, 1, self->hooks.spi16_userData);
}

Expand Down
10 changes: 10 additions & 0 deletions src/boards/LMS7002M_SDRDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,4 +1194,14 @@ OpStatus LMS7002M_SDRDevice::LMS7002TestSignalConfigure(LMS7002M& chip, const Ch
return OpStatus::Success;
}

std::size_t LMS7002M_SDRDevice::AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData)
{
return 0;
}

void LMS7002M_SDRDevice::RemoveHotplugDisconnectCallback(std::size_t id)
{
return;
}

} // namespace lime
3 changes: 3 additions & 0 deletions src/boards/LMS7002M_SDRDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class LIME_API LMS7002M_SDRDevice : public SDRDevice
/// @copydoc FPGA::WriteRegister()
virtual OpStatus WriteFPGARegister(uint32_t address, uint32_t value);

std::size_t AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData) override;
void RemoveHotplugDisconnectCallback(std::size_t id) override;

protected:
static RFSOCDescriptor GetDefaultLMS7002MDescriptor();
static OpStatus UpdateFPGAInterfaceFrequency(LMS7002M& soc, FPGA& fpga, uint8_t chipIndex);
Expand Down
40 changes: 39 additions & 1 deletion src/boards/LimeSDR/LimeSDR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <cmath>
#include <memory>

using namespace lime;
using namespace lime::LMS64CProtocol;
using namespace lime::LMS7002MCSR_Data;
using namespace std::literals::string_literals;

namespace lime {

static const uint8_t SPI_LMS7002M = 0;
static const uint8_t SPI_FPGA = 1;
static const uint8_t SPI_ADF4002 = 2;
Expand Down Expand Up @@ -144,6 +145,18 @@ LimeSDR::LimeSDR(std::shared_ptr<IComms> spiLMS,

mDeviceDescriptor = descriptor;

mStreamPort->AddOnHotplugDisconnectCallback(
[](void* userData) {
auto* const sdr = reinterpret_cast<LimeSDR*>(userData);

// Call in reverse order so that the "smaller" scoped callbacks run first.
for (auto iter = sdr->disconnectCallbacks.rbegin(); iter != sdr->disconnectCallbacks.rend(); ++iter)
{
(*iter)();
}
},
this);

//must configure synthesizer before using LimeSDR
/*if (info.device == LMS_DEV_LIMESDR && info.hardware < 4)
{
Expand Down Expand Up @@ -539,3 +552,28 @@ OpStatus LimeSDR::MemoryRead(std::shared_ptr<DataStorage> storage, Region region
return OpStatus::Error;
return mfpgaPort->MemoryRead(region.address, data, region.size);
}

std::size_t LimeSDR::AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData)
{
std::size_t id = 0;

if (!disconnectCallbacks.empty())
{
// As long as elements are not out of order this guarantees a unique ID in the array.
id = disconnectCallbacks.back().id + 1;
}

disconnectCallbacks.push_back({ function, userData, id });

return id;
}

void LimeSDR::RemoveHotplugDisconnectCallback(std::size_t id)
{
disconnectCallbacks.erase(std::remove_if(disconnectCallbacks.begin(),
disconnectCallbacks.end(),
[&id](const CallbackInfo<HotplugDisconnectCallbackType>& info) { return id == info.id; }),
disconnectCallbacks.end());
}

} // namespace lime
6 changes: 6 additions & 0 deletions src/boards/LimeSDR/LimeSDR.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class LimeSDR : public LMS7002M_SDRDevice
OpStatus MemoryWrite(std::shared_ptr<DataStorage> storage, Region region, const void* data) override;
OpStatus MemoryRead(std::shared_ptr<DataStorage> storage, Region region, void* data) override;

std::size_t AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData) override;
void RemoveHotplugDisconnectCallback(std::size_t id) override;

protected:
SDRDescriptor GetDeviceInfo();
void ResetUSBFIFO();
Expand All @@ -70,6 +73,9 @@ class LimeSDR : public LMS7002M_SDRDevice
std::shared_ptr<IComms> mlms7002mPort;
std::shared_ptr<IComms> mfpgaPort;
bool mConfigInProgress;

std::vector<CallbackInfo<HotplugDisconnectCallbackType>> disconnectCallbacks;
std::size_t mStreamStopCallbackId;
};

} // namespace lime
Expand Down
40 changes: 39 additions & 1 deletion src/boards/LimeSDR_Mini/LimeSDR_Mini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include <stdexcept>
#include <cmath>

using namespace lime;
using namespace lime::LMS64CProtocol;
using namespace lime::EqualizerCSR;
using namespace lime::LMS7002MCSR_Data;
using namespace std::literals::string_literals;

namespace lime {

static const uint8_t SPI_LMS7002M = 0;
static const uint8_t SPI_FPGA = 1;

Expand Down Expand Up @@ -198,6 +199,18 @@ LimeSDR_Mini::LimeSDR_Mini(std::shared_ptr<IComms> spiLMS,
descriptor.socTree->children.push_back(fpgaNode);

mDeviceDescriptor = descriptor;

mStreamPort->AddOnHotplugDisconnectCallback(
[](void* userData) {
auto* const mini = reinterpret_cast<LimeSDR_Mini*>(userData);

// Call in reverse order so that the "smaller" scoped callbacks run first.
for (auto iter = mini->disconnectCallbacks.rbegin(); iter != mini->disconnectCallbacks.rend(); ++iter)
{
(*iter)();
}
},
this);
}

LimeSDR_Mini::~LimeSDR_Mini()
Expand Down Expand Up @@ -597,3 +610,28 @@ void LimeSDR_Mini::SetSerialNumber(const std::string& number)
sscanf(number.c_str(), "%16lX", &sn);
mDeviceDescriptor.serialNumber = sn;
}

std::size_t LimeSDR_Mini::AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData)
{
std::size_t id = 0;

if (!disconnectCallbacks.empty())
{
// As long as elements are not out of order this guarantees a unique ID in the array.
id = disconnectCallbacks.back().id + 1;
}

disconnectCallbacks.push_back({ function, userData, id });

return id;
}

void LimeSDR_Mini::RemoveHotplugDisconnectCallback(std::size_t id)
{
disconnectCallbacks.erase(std::remove_if(disconnectCallbacks.begin(),
disconnectCallbacks.end(),
[&id](const CallbackInfo<HotplugDisconnectCallbackType>& info) { return id == info.id; }),
disconnectCallbacks.end());
}

} // namespace lime
6 changes: 5 additions & 1 deletion src/boards/LimeSDR_Mini/LimeSDR_Mini.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace lime {

class USBGeneric;
class IComms;
class IUSB;

Expand Down Expand Up @@ -49,6 +48,9 @@ class LimeSDR_Mini : public LMS7002M_SDRDevice

void SetSerialNumber(const std::string& number);

std::size_t AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData) override;
void RemoveHotplugDisconnectCallback(std::size_t id) override;

protected:
SDRDescriptor GetDeviceInfo();
static OpStatus UpdateFPGAInterface(void* userData);
Expand All @@ -59,6 +61,8 @@ class LimeSDR_Mini : public LMS7002M_SDRDevice
std::shared_ptr<IComms> mlms7002mPort;
std::shared_ptr<IComms> mfpgaPort;
bool mConfigInProgress{};

std::vector<CallbackInfo<HotplugDisconnectCallbackType>> disconnectCallbacks;
};

} // namespace lime
Expand Down
10 changes: 10 additions & 0 deletions src/boards/MMX8/MM_X8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,4 +836,14 @@ OpStatus LimeSDR_MMX8::UploadTxWaveform(const StreamConfig& config, uint8_t modu
return mSubDevices[moduleIndex]->UploadTxWaveform(config, 0, samples, count);
}

std::size_t LimeSDR_MMX8::AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData)
{
return 0;
}

void LimeSDR_MMX8::RemoveHotplugDisconnectCallback(std::size_t id)
{
return;
}

} //namespace lime
3 changes: 3 additions & 0 deletions src/boards/MMX8/MM_X8.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ class LimeSDR_MMX8 : public SDRDevice
OpStatus MemoryRead(std::shared_ptr<DataStorage> storage, Region region, void* data) override;
OpStatus UploadTxWaveform(const StreamConfig& config, uint8_t moduleIndex, const void** samples, uint32_t count) override;

std::size_t AddHotplugDisconnectCallback(const HotplugDisconnectCallbackType& function, void* userData) override;
void RemoveHotplugDisconnectCallback(std::size_t id) override;

private:
std::shared_ptr<IComms> mMainFPGAcomms;
SDRDescriptor mDeviceDescriptor;
Expand Down
2 changes: 2 additions & 0 deletions src/cli/limeTRX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,8 @@ int main(int argc, char** argv)
else
device->StreamStart(chipIndex);

device->AddHotplugDisconnectCallback([](void* data) { *reinterpret_cast<bool*>(data) = true; }, &stopProgram);

auto startTime = std::chrono::high_resolution_clock::now();
auto t1 = startTime - std::chrono::seconds(2); // rewind t1 to do update on first loop
auto t2 = t1;
Expand Down
9 changes: 9 additions & 0 deletions src/comms/USB/FT601/FT601.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,13 @@ int FT601::FT_SetStreamPipe(unsigned char ep, size_t size)
}
#endif

void FT601::AddOnHotplugDisconnectCallback(const IUSB::HotplugDisconnectCallbackType& function, void* userData)
{
#ifdef __unix__
libusb_impl.AddOnHotplugDisconnectCallback(function, userData);
#else
// Hotplug events are not supported by the library
#endif
}

} // namespace lime
Loading