From 734ba429890b6e22e5dfbc29eb8cb2a1e4c45261 Mon Sep 17 00:00:00 2001 From: Jon Szymaniak Date: Sun, 12 Jan 2014 14:25:14 -0500 Subject: [PATCH] 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. --- lib/bladerf/bladerf_common.cc | 41 +++++++++++++++++++++++++++++++++ lib/bladerf/bladerf_common.h | 3 +++ lib/bladerf/bladerf_sink_c.cc | 37 ++--------------------------- lib/bladerf/bladerf_source_c.cc | 36 ++--------------------------- 4 files changed, 48 insertions(+), 69 deletions(-) diff --git a/lib/bladerf/bladerf_common.cc b/lib/bladerf/bladerf_common.cc index 2c2e97f..faea372 100644 --- a/lib/bladerf/bladerf_common.cc +++ b/lib/bladerf/bladerf_common.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; +} diff --git a/lib/bladerf/bladerf_common.h b/lib/bladerf/bladerf_common.h index 1ca0d35..81650cf 100644 --- a/lib/bladerf/bladerf_common.h +++ b/lib/bladerf/bladerf_common.h @@ -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(); diff --git a/lib/bladerf/bladerf_sink_c.cc b/lib/bladerf/bladerf_sink_c.cc index 732c946..eaf81f1 100644 --- a/lib/bladerf/bladerf_sink_c.cc +++ b/lib/bladerf/bladerf_sink_c.cc @@ -308,45 +308,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 ) diff --git a/lib/bladerf/bladerf_source_c.cc b/lib/bladerf/bladerf_source_c.cc index 136565d..ab9cea1 100644 --- a/lib/bladerf/bladerf_source_c.cc +++ b/lib/bladerf/bladerf_source_c.cc @@ -331,44 +331,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 )