Skip to content

Commit

Permalink
Decouple SamplesPacket from SDRDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
DominykasPetke committed Mar 14, 2024
1 parent ad9eefe commit e0b3ed5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/comms/PCIe/TRXLooper_PCIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ void TRXLooper_PCIE::TransmitPacketsLoop()
}

// drop old packets before forming, Rx is needed to get current timestamp
if (srcPkt->metadata.waitForTimestamp && mConfig.channels.at(lime::TRXDir::Rx).size() > 0)
if (srcPkt->useTimestamp && mConfig.channels.at(lime::TRXDir::Rx).size() > 0)
{
int64_t rxNow = mRx.lastTimestamp.load(std::memory_order_relaxed);
const int64_t txAdvance = srcPkt->metadata.timestamp - rxNow;
const int64_t txAdvance = srcPkt->timestamp - rxNow;
if (mConfig.hintSampleRate)
{
int64_t timeAdvance = ts_to_us(mConfig.hintSampleRate, txAdvance);
Expand Down Expand Up @@ -713,7 +713,7 @@ void TRXLooper_PCIE::ReceivePacketsLoop()
uint8_t* buffer = dmaBuffers[dma.swIndex % bufferCount];
const FPGA_RxDataPacket* pkt = reinterpret_cast<const FPGA_RxDataPacket*>(buffer);
if (outputPkt)
outputPkt->metadata.timestamp = pkt->counter;
outputPkt->timestamp = pkt->counter;

const int srcPktCount = mRxArgs.packetsToBatch;
for (int i = 0; i < srcPktCount; ++i)
Expand Down
6 changes: 3 additions & 3 deletions src/comms/PCIe/TxBufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ template<class T> class TxBufferManager
payloadSize = 0;
}

header->ignoreTimestamp(!src->metadata.waitForTimestamp);
header->ignoreTimestamp(!src->useTimestamp);
if (payloadSize == 0)
{
++packetsCreated;
header->counter = src->metadata.timestamp;
header->counter = src->timestamp;
bytesUsed += sizeof(StreamHeader);
}
const uint32_t freeSpace = std::min(maxPayloadSize - payloadSize, mCapacity - bytesUsed - 16);
Expand Down Expand Up @@ -138,7 +138,7 @@ template<class T> class TxBufferManager
}
if (!hasSpace())
return true;
return src->metadata.flushPartialPacket || sendBuffer;
return src->flush || sendBuffer;
}

/// @brief Gets the current size of the transfer.
Expand Down
6 changes: 3 additions & 3 deletions src/comms/USB/TRXLooper_USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ void TRXLooper_USB::TransmitPacketsLoop()
{
header->Clear();
++packetsCreated;
header->counter = srcPkt->metadata.timestamp;
header->counter = srcPkt->timestamp;
bytesUsed += sizeof(StreamHeader);
}

header->ignoreTimestamp(!srcPkt->metadata.waitForTimestamp);
header->ignoreTimestamp(!srcPkt->useTimestamp);

const uint32_t freeSpace = std::min(maxPayloadSize - payloadSize, bufferSize - bytesUsed);
uint32_t transferCount = std::min(freeSpace / bytesForFrame, static_cast<uint32_t>(srcPkt->size()));
Expand Down Expand Up @@ -234,7 +234,7 @@ void TRXLooper_USB::TransmitPacketsLoop()
payloadSize = 0;
packetsCreated = 0;

mTx.stats.timestamp = srcPkt->metadata.timestamp;
mTx.stats.timestamp = srcPkt->timestamp;

header = reinterpret_cast<StreamHeader*>(&buffers[bufferIndex * bufferSize]);
payloadPtr = reinterpret_cast<uint8_t*>(header) + sizeof(StreamHeader);
Expand Down
11 changes: 5 additions & 6 deletions src/protocols/SamplesPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <cassert>
#include <cstring>

#include "limesuite/SDRDevice.h"

namespace lime {

/**
Expand Down Expand Up @@ -113,7 +111,7 @@ template<uint8_t chCount> class SamplesPacket
for (uint8_t i = 0; i < chCount; ++i)
head[i] += toPop * frameSize;
offset += toPop;
metadata.timestamp += toPop; // Also offset timestamp
timestamp += toPop; // Also offset timestamp
return toPop;
}

Expand Down Expand Up @@ -172,6 +170,10 @@ template<uint8_t chCount> class SamplesPacket
}
}

uint64_t timestamp; ///< The timestamp of the packet.
bool useTimestamp; ///< Whether to use the timestamp or not.
bool flush; ///< Whether to flush the whole packet early or not.

private:
uint8_t* head[chCount];
uint8_t* tail[chCount];
Expand All @@ -181,9 +183,6 @@ template<uint8_t chCount> class SamplesPacket
uint32_t length;
uint32_t mCapacity;
uint8_t frameSize;

public:
SDRDevice::StreamMeta metadata; ///< The stream metadata of the packet.
};

} // namespace lime
Expand Down
24 changes: 6 additions & 18 deletions src/protocols/TRXLooper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,12 @@ template<class T> uint32_t TRXLooper::StreamRxTemplate(T* const* dest, uint32_t
//auto start = high_resolution_clock::now();
while (samplesProduced < count)
{
if (!mRx.stagingPacket && !mRx.fifo->pop(&mRx.stagingPacket, firstIteration, 250))
{
if (!mRx.stagingPacket && !mRx.fifo->pop(&mRx.stagingPacket, firstIteration, 2000))
return samplesProduced;
}

if (!timestampSet && meta)
{
meta->timestamp = mRx.stagingPacket->metadata.timestamp;
meta->timestamp = mRx.stagingPacket->timestamp;
timestampSet = true;
}

Expand Down Expand Up @@ -340,12 +338,10 @@ template<class T> uint32_t TRXLooper::StreamTxTemplate(const T* const* samples,
const int packetsToBatch = mTx.packetsToBatch;
const int32_t outputPktSize = SamplesPacketType::headerSize + packetsToBatch * samplesInPkt * sizeof(T);

if (mTx.stagingPacket && mTx.stagingPacket->metadata.timestamp + mTx.stagingPacket->size() != meta->timestamp)
if (mTx.stagingPacket && mTx.stagingPacket->timestamp + mTx.stagingPacket->size() != meta->timestamp)
{
if (!mTx.fifo->push(mTx.stagingPacket))
{
return 0;
}

mTx.stagingPacket = nullptr;
}
Expand All @@ -359,36 +355,28 @@ template<class T> uint32_t TRXLooper::StreamTxTemplate(const T* const* samples,
mTx.memPool->Allocate(outputPktSize), samplesInPkt * packetsToBatch, sizeof(T));

if (!mTx.stagingPacket)
{
break;
}

mTx.stagingPacket->Reset();
mTx.stagingPacket->metadata.timestamp = ts;
mTx.stagingPacket->metadata.waitForTimestamp = useTimestamp;
mTx.stagingPacket->timestamp = ts;
mTx.stagingPacket->useTimestamp = useTimestamp;
}

int consumed = mTx.stagingPacket->push(src, samplesRemaining);
src[0] += consumed;
if (useChannelB)
{
src[1] += consumed;
}

samplesRemaining -= consumed;
ts += consumed;

if (mTx.stagingPacket->isFull() || flush)
{
if (samplesRemaining == 0)
{
mTx.stagingPacket->metadata.flushPartialPacket = flush;
}
mTx.stagingPacket->flush = flush;

if (!mTx.fifo->push(mTx.stagingPacket))
{
break;
}

mTx.stagingPacket = nullptr;
}
Expand Down

0 comments on commit e0b3ed5

Please sign in to comment.