bladerf: Use rational sample rate functions

libbladeRF provides accessors for rational sample rates, which the
integer sample rate functions use under the hood. Therefore, there's no
need to check if the requested rate contains a fractional portion and
switch between the two sets of functions.
wip-signat
Jon Szymaniak 9 years ago committed by Dimitri Stolnikov
parent 8bbd2b5bb5
commit 748ac00b25
  1. 41
      lib/bladerf/bladerf_common.cc
  2. 3
      lib/bladerf/bladerf_common.h
  3. 37
      lib/bladerf/bladerf_sink_c.cc
  4. 36
      lib/bladerf/bladerf_source_c.cc

@ -326,3 +326,44 @@ void bladerf_common::set_running( bool is_running )
_is_running = is_running;
}
double bladerf_common::set_sample_rate( bladerf_module module, double rate )
{
int status;
struct bladerf_rational_rate rational_rate, actual;
rational_rate.integer = (uint32_t)rate;
rational_rate.den = 10000;
rational_rate.num = (rate - rational_rate.integer) * rational_rate.den;
status = bladerf_set_rational_sample_rate( _dev.get(), module,
&rational_rate, &actual );
if ( status != 0 ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"Failed to set integer rate:" +
std::string(bladerf_strerror(status)));
}
return actual.integer + actual.num / (double)actual.den;
}
double bladerf_common::get_sample_rate( bladerf_module module )
{
int status;
double ret = 0.0;
struct bladerf_rational_rate rate;
status = bladerf_get_rational_sample_rate( _dev.get(), module, &rate );
if ( status != 0 ) {
throw std::runtime_error( std::string(__FUNCTION__) +
"Failed to get sample rate:" +
std::string(bladerf_strerror(status)) );
} else {
ret = rate.integer + rate.num / (double)rate.den;
}
return ret;
}

@ -64,6 +64,9 @@ protected:
/* Handle initialized and parameters common to both source & sink */
void init(dict_t &dict, const char *type);
double set_sample_rate(bladerf_module module, double rate);
double get_sample_rate(bladerf_module module);
osmosdr::freq_range_t freq_range();
osmosdr::meta_range_t sample_rates();
osmosdr::freq_range_t filter_bandwidths();

@ -306,45 +306,12 @@ osmosdr::meta_range_t bladerf_sink_c::get_sample_rates()
double bladerf_sink_c::set_sample_rate(double rate)
{
int ret;
uint32_t actual;
/* Set the Si5338 to be 2x this sample rate */
/* Check to see if the sample rate is an integer */
if( (uint32_t)round(rate) == (uint32_t)rate )
{
ret = bladerf_set_sample_rate( _dev.get(), BLADERF_MODULE_TX, (uint32_t)rate, &actual );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"Failed to set integer rate:" +
std::string(bladerf_strerror(ret)));
}
} else {
/* TODO: Fractional sample rate */
ret = bladerf_set_sample_rate( _dev.get(), BLADERF_MODULE_TX, (uint32_t)rate, &actual );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"Failed to set fractional rate: " +
std::string(bladerf_strerror(ret)));
}
}
return get_sample_rate();
return bladerf_common::set_sample_rate(BLADERF_MODULE_TX, rate);
}
double bladerf_sink_c::get_sample_rate()
{
int ret;
unsigned int rate = 0;
ret = bladerf_get_sample_rate( _dev.get(), BLADERF_MODULE_TX, &rate );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) +
"Failed to get sample rate:" +
std::string(bladerf_strerror(ret)));
}
return (double)rate;
return bladerf_common::get_sample_rate(BLADERF_MODULE_TX);
}
osmosdr::freq_range_t bladerf_sink_c::get_freq_range( size_t chan )

@ -330,44 +330,12 @@ osmosdr::meta_range_t bladerf_source_c::get_sample_rates()
double bladerf_source_c::set_sample_rate( double rate )
{
int ret;
uint32_t actual;
/* Check to see if the sample rate is an integer */
if( (uint32_t)round(rate) == (uint32_t)rate )
{
ret = bladerf_set_sample_rate( _dev.get(), BLADERF_MODULE_RX, (uint32_t)rate, &actual );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"has failed to set integer rate: " +
std::string(bladerf_strerror(ret)) );
}
} else {
/* TODO: Fractional sample rate */
ret = bladerf_set_sample_rate( _dev.get(), BLADERF_MODULE_RX, (uint32_t)rate, &actual );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"has failed to set fractional rate: " +
std::string(bladerf_strerror(ret)) );
}
}
return get_sample_rate();
return bladerf_common::set_sample_rate( BLADERF_MODULE_RX, rate);
}
double bladerf_source_c::get_sample_rate()
{
int ret;
unsigned int rate = 0;
ret = bladerf_get_sample_rate( _dev.get(), BLADERF_MODULE_RX, &rate );
if( ret ) {
throw std::runtime_error( std::string(__FUNCTION__) + " " +
"has failed to get sample rate, error " +
std::string(bladerf_strerror(ret)) );
}
return (double)rate;
return bladerf_common::get_sample_rate( BLADERF_MODULE_RX );
}
osmosdr::freq_range_t bladerf_source_c::get_freq_range( size_t chan )

Loading…
Cancel
Save