Skip to content

Commit

Permalink
auto set buffersize by samplerate
Browse files Browse the repository at this point in the history
  • Loading branch information
jocover committed Aug 31, 2017
1 parent 152fe2c commit f857071
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
12 changes: 11 additions & 1 deletion PlutoSDR_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#endif

SoapyPlutoSDR::SoapyPlutoSDR( const SoapySDR::Kwargs &args ):
ctx(nullptr), dev(nullptr), rx_dev(nullptr),tx_dev(nullptr), decimation(false), interpolation(false)
ctx(nullptr), dev(nullptr), rx_dev(nullptr),tx_dev(nullptr), decimation(false), interpolation(false), rx_stream(nullptr)
{

if (args.count("label") != 0)
Expand Down Expand Up @@ -364,6 +364,8 @@ void SoapyPlutoSDR::setSampleRate( const int direction, const size_t channel, co

iio_channel_attr_write_longlong(iio_device_find_channel(rx_dev, "voltage0", false), "sampling_frequency", decimation?samplerate/8:samplerate);

if(rx_stream)
rx_stream->set_buffer_size_by_samplerate(decimation ? samplerate / 8 : samplerate);
}

if(direction==SOAPY_SDR_TX){
Expand Down Expand Up @@ -423,6 +425,10 @@ std::vector<double> SoapyPlutoSDR::listSampleRates( const int direction, const s
options.push_back(4e6);
options.push_back(5e6);
options.push_back(6e6);
options.push_back(7e6);
options.push_back(8e6);
options.push_back(9e6);
options.push_back(10e6);
return(options);

}
Expand Down Expand Up @@ -476,6 +482,10 @@ std::vector<double> SoapyPlutoSDR::listBandwidths( const int direction, const si
options.push_back(4e6);
options.push_back(5e6);
options.push_back(6e6);
options.push_back(7e6);
options.push_back(8e6);
options.push_back(9e6);
options.push_back(10e6);
return(options);

}
45 changes: 30 additions & 15 deletions PlutoSDR_Streaming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SoapySDR::Stream *SoapyPlutoSDR::setupStream(
if(direction ==SOAPY_SDR_RX){

stream->rx = std::shared_ptr<rx_streamer>(new rx_streamer(rx_dev, format,channels, args));

rx_stream = stream->rx;
}

if (direction == SOAPY_SDR_TX) {
Expand Down Expand Up @@ -142,7 +142,19 @@ int SoapyPlutoSDR::readStreamStatus(
return 0;
}

void rx_streamer::set_buffer_size_by_samplerate(const size_t _samplerate) {

uint32_t n = 1, x = uint32_t(_samplerate);
if ((x >> 16) == 0) { n = n + 16; x = x << 16; }
if ((x >> 24) == 0) { n = n + 8; x = x << 8; }
if ((x >> 28) == 0) { n = n + 4; x = x << 4; }
if ((x >> 30) == 0) { n = n + 2; x = x << 2; }
n = n - (x >> 31);

this->set_buffer_size(std::max(1 << (31 - n - 2), 16384));

SoapySDR_logf(SOAPY_SDR_INFO, "Auto setting Buffer Size: %d", buffer_size);
}

rx_streamer::rx_streamer(const iio_device *_dev, const std::string &_format, const std::vector<size_t> &channels, const SoapySDR::Kwargs &args):
dev(_dev),buffer_size(16384),buf(nullptr)
Expand Down Expand Up @@ -181,25 +193,19 @@ rx_streamer::rx_streamer(const iio_device *_dev, const std::string &_format, con
try
{
size_t bufferLength = std::stoi(args.at("bufflen"));
if(bufferLength>0)
buffer_size=bufferLength;
if (bufferLength > 0)
this->set_buffer_size(bufferLength);
}
catch (const std::invalid_argument &){}

}else{

long long samplerate;
uint32_t n=1,x;

iio_channel_attr_read_longlong(iio_device_find_channel(dev, "voltage0", false),"sampling_frequency",&samplerate);
x=uint32_t(samplerate);
if((x>>16)==0){n=n+16;x=x<<16;}
if((x>>24)==0){n=n+8;x=x<<8;}
if((x>>28)==0){n=n+4;x=x<<4;}
if((x>>30)==0){n=n+2;x=x<<2;}
n=n-(x>>31);
buffer_size=1<<(31-n-2);

SoapySDR_logf( SOAPY_SDR_INFO, "Auto setting Buffer Size: %d",buffer_size);

this->set_buffer_size_by_samplerate(samplerate);

}
buffer.reserve(buffer_size);
buffer.resize(buffer_size);
Expand Down Expand Up @@ -280,11 +286,9 @@ size_t rx_streamer::recv(void * const *buffs,

}


items_in_buffer -= items;
byte_offset += items * iio_buffer_step(buf);


return(items);

}
Expand Down Expand Up @@ -340,6 +344,17 @@ void rx_streamer::set_buffer_size(const size_t _buffer_size){

std::unique_lock<std::mutex> lock(mutex);

if (buf && this->buffer_size != _buffer_size) {
iio_buffer_destroy(buf);

buf = iio_device_create_buffer(dev, _buffer_size, false);
if (!buf) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Unable to create buffer!");
throw std::runtime_error("Unable to create buffer!\n");
}

}

this->buffer_size=_buffer_size;

}
Expand Down
3 changes: 3 additions & 0 deletions SoapyPlutoSDR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class rx_streamer {
int stop(const int flags,
const long long timeNs=100000);

void set_buffer_size_by_samplerate(const size_t _samplerate);

private:

void set_buffer_size(const size_t _buffer_size);
Expand Down Expand Up @@ -265,4 +267,5 @@ class SoapyPlutoSDR : public SoapySDR::Device{
iio_device *tx_dev;
mutable std::mutex device_mutex;
bool decimation, interpolation;
std::shared_ptr<rx_streamer> rx_stream;
};

0 comments on commit f857071

Please sign in to comment.