Skip to content

Commit 33d5a94

Browse files
committed
Fix USB streaming transfers stuttering at high speeds
1 parent b3b748f commit 33d5a94

4 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/comms/USB/USBDMAEmulation.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ static constexpr int maxAsyncTransfers = 16;
1212
USBDMAEmulation::USBDMAEmulation(std::shared_ptr<IUSB> port, uint8_t endpoint, DataTransferDirection dir)
1313
: port(port)
1414
, counters{}
15-
, lastRequestIndex(0)
1615
, endpoint(endpoint)
1716
, dir(dir)
1817
, continuous(false)
@@ -99,7 +98,6 @@ OpStatus USBDMAEmulation::Enable(bool enable)
9998
}
10099

101100
counters.transfersCompleted = 0;
102-
lastRequestIndex = 0;
103101

104102
// for USB nothing is needed to be done to just enable DMA
105103
isEnabled = true;
@@ -121,7 +119,7 @@ OpStatus USBDMAEmulation::EnableContinuous(bool enable, uint32_t maxTransferSize
121119
return OpStatus::Success;
122120
// For continuous transferring, preemptively request data to be transferred
123121
std::unique_lock lck{ queuesMutex };
124-
lastRequestIndex = 0;
122+
int lastRequestIndex = 0;
125123
port->FlushEndpoint();
126124
while (!transfers.empty())
127125
{
@@ -144,7 +142,7 @@ void USBDMAEmulation::UpdateProducerStates()
144142
{
145143
AsyncXfer* async = pendingXfers.front();
146144
assert(async);
147-
int timeout_ms = 100; // just checking if the transfer is complete, not waiting.
145+
const int timeout_ms = 0; // just check if transfer is completed. Do not wait.
148146
OpStatus status = port->WaitForXfer(async->xfer, timeout_ms);
149147
if (status != OpStatus::Success)
150148
break;
@@ -154,8 +152,6 @@ void USBDMAEmulation::UpdateProducerStates()
154152
++counters.transfersCompleted;
155153
counters.transfersCompleted &= 0xFFFF;
156154
}
157-
if (!continuous)
158-
return;
159155
}
160156

161157
USBDMAEmulation::State USBDMAEmulation::GetCounters()
@@ -172,20 +168,16 @@ OpStatus USBDMAEmulation::SubmitRequest(uint64_t index, uint32_t bytesCount, Dat
172168
assert(bytesCount > 0);
173169
assert(index < mappings.size());
174170

175-
int count = 1;
176171
std::unique_lock lck{ queuesMutex };
177-
count = std::min(size_t(count), transfers.size());
178-
if (!transfers.empty() && count > 0)
172+
if (!transfers.empty())
179173
{
180174
AsyncXfer* async = transfers.front();
181175
async->requestedSize = bytesCount;
182176
OpStatus status = port->BeginDataXfer(async->xfer, mappings[index].buffer, async->requestedSize, endpoint);
183-
lastRequestIndex = index; //(lastRequestIndex + 1) % mappings.size();
184177
if (status != OpStatus::Success)
185178
return OpStatus::Error;
186179
transfers.pop();
187180
pendingXfers.push(async);
188-
--count;
189181
return OpStatus::Success;
190182
}
191183
return OpStatus::Error;

src/comms/USB/USBDMAEmulation.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class USBDMAEmulation : public IDMA
5656
std::string name;
5757
std::shared_ptr<IUSB> port;
5858
State counters;
59-
uint16_t lastRequestIndex;
6059
uint8_t endpoint;
6160
DataTransferDirection dir;
6261
bool continuous;

src/comms/USB/UnixUsb.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#endif
1717

1818
#include "limesuiteng/Logger.h"
19+
#include "threadHelper.h"
1920

2021
using namespace std::literals::string_literals;
2122

@@ -47,7 +48,6 @@ static int SessionRefCountIncrement()
4748
int returnCode = libusb_init(&gContextLibUsb); // Initialize the library for the session we just declared
4849
if (returnCode < 0)
4950
lime::error("UnixUsb::libusb_init Error %i", returnCode); // There was an error
50-
5151
#if LIBUSBX_API_VERSION < 0x01000106
5252
libusb_set_debug(gContextLibUsb, 3); // Set verbosity level to 3, as suggested in the documentation
5353
#else
@@ -56,7 +56,14 @@ static int SessionRefCountIncrement()
5656
LIBUSB_LOG_LEVEL_INFO); // Set verbosity level to info, as suggested in the documentation
5757
#endif
5858
if (gContextLibUsb)
59+
{
5960
gUSBProcessingThread = std::thread(HandleLibusbEvents, gContextLibUsb);
61+
SetOSThreadPriority(ThreadPriority::HIGHEST, ThreadPolicy::REALTIME, &gUSBProcessingThread);
62+
#ifdef __linux__
63+
char threadName[16] = "limeUSB"; // limited to 16 chars, including null byte.
64+
pthread_setname_np(gUSBProcessingThread.native_handle(), threadName);
65+
#endif
66+
}
6067
}
6168
return activeUSBconnections;
6269
}

src/streaming/TRXLooper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,11 @@ void TRXLooper::ReceivePacketsLoop()
812812
fpgaTicks,
813813
static_cast<int64_t>((diff.GetSeconds() + diff.GetFracSeconds()) * 1e9));
814814
}
815-
lime::debug("Loss: pkt:%li exp: %016lx, got: %016lx, diff: %li, timeDiff:%lins",
815+
lime::debug("Loss: pkt:%li exp: %016lx, got: %016lx, diff: %+li, timeDiff:%+lins",
816816
stats.packets + i,
817817
expectedTimestamp.GetTicks(),
818818
hwts.GetTicks(),
819-
expectedTimestamp.GetTicks() - hwts.GetTicks(),
819+
hwts.GetTicks() - expectedTimestamp.GetTicks(),
820820
static_cast<int64_t>((diff.GetSeconds() + diff.GetFracSeconds()) * 1e9));
821821
++stats.loss;
822822
loss.add(1);

0 commit comments

Comments
 (0)