From 08dfff379865656e94b31fd565a4b13b4609ea63 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 1 Nov 2010 18:56:38 -0700 Subject: uhd: created a meta range that is a range of ranges for gains and freqs created a templated range that that holds a start, stop, and step created a meta-range template that is a vector of ranges meta-range can calculate the overall start, stop, step or be indexed to get at components replaced instances of range.min, max, step with the functions start() stop() and step() the xcvr frequency range is now expressed in as two ranges (have to fix its clip function though) --- host/lib/usrp/dboard/db_rfx.cpp | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'host/lib/usrp/dboard/db_rfx.cpp') diff --git a/host/lib/usrp/dboard/db_rfx.cpp b/host/lib/usrp/dboard/db_rfx.cpp index 152198c3a..bb9e19708 100644 --- a/host/lib/usrp/dboard/db_rfx.cpp +++ b/host/lib/usrp/dboard/db_rfx.cpp @@ -93,9 +93,9 @@ public: void tx_set(const wax::obj &key, const wax::obj &val); private: - freq_range_t _freq_range; - uhd::dict _rx_gain_ranges; - uhd::dict _div2; + const freq_range_t _freq_range; + const uhd::dict _rx_gain_ranges; + const uhd::dict _div2; double _rx_lo_freq, _tx_lo_freq; std::string _rx_ant; uhd::dict _rx_gains; @@ -168,19 +168,17 @@ rfx_xcvr::rfx_xcvr( ctor_args_t args, const freq_range_t &freq_range, bool rx_div2, bool tx_div2 -) : xcvr_dboard_base(args){ - _freq_range = freq_range; - _div2[dboard_iface::UNIT_RX] = rx_div2; - _div2[dboard_iface::UNIT_TX] = tx_div2; - - if(this->get_rx_id() == 0x0024) { //RFX400 - _rx_gain_ranges = rfx400_rx_gain_ranges; - } - else { - _rx_gain_ranges = rfx_rx_gain_ranges; - } - - +): + xcvr_dboard_base(args), + _freq_range(freq_range), + _rx_gain_ranges((this->get_rx_id() == 0x0024)? + rfx400_rx_gain_ranges : rfx_rx_gain_ranges + ), + _div2(map_list_of + (dboard_iface::UNIT_RX, rx_div2) + (dboard_iface::UNIT_TX, tx_div2) + ) +{ //enable the clocks that we need this->get_iface()->set_clock_enabled(dboard_iface::UNIT_TX, true); this->get_iface()->set_clock_enabled(dboard_iface::UNIT_RX, true); @@ -204,12 +202,12 @@ rfx_xcvr::rfx_xcvr( this->get_iface()->set_atr_reg(dboard_iface::UNIT_RX, dboard_iface::ATR_REG_FULL_DUPLEX, POWER_UP | ANT_RX2| MIXER_ENB); //set some default values - set_rx_lo_freq((_freq_range.min + _freq_range.max)/2.0); - set_tx_lo_freq((_freq_range.min + _freq_range.max)/2.0); + set_rx_lo_freq((_freq_range.start() + _freq_range.stop())/2.0); + set_tx_lo_freq((_freq_range.start() + _freq_range.stop())/2.0); set_rx_ant("RX2"); BOOST_FOREACH(const std::string &name, _rx_gain_ranges.keys()){ - set_rx_gain(_rx_gain_ranges[name].min, name); + set_rx_gain(_rx_gain_ranges[name].start(), name); } } @@ -265,7 +263,7 @@ void rfx_xcvr::set_rx_gain(float gain, const std::string &name){ assert_has(_rx_gain_ranges.keys(), name, "rfx rx gain name"); if(name == "PGA0"){ float dac_volts = rx_pga0_gain_to_dac_volts(gain, - (_rx_gain_ranges["PGA0"].max - _rx_gain_ranges["PGA0"].min)); + (_rx_gain_ranges["PGA0"].stop() - _rx_gain_ranges["PGA0"].start())); _rx_gains[name] = gain; //write the new voltage to the aux dac @@ -294,7 +292,7 @@ double rfx_xcvr::set_lo_freq( ) % (target_freq/1e6) << std::endl; //clip the input - target_freq = std::clip(target_freq, _freq_range.min, _freq_range.max); + target_freq = std::clip(target_freq, _freq_range.start(), _freq_range.stop()); if (_div2[unit]) target_freq *= 2; //map prescalers to the register enums -- cgit v1.2.3 From 775383e635cdf27ede64f5936a649570b74d7c70 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 1 Nov 2010 20:57:09 -0700 Subject: uhd: added meta-range clip and implemented in dboards, fixed step calculation --- host/include/uhd/types/ranges.hpp | 8 ++++ host/include/uhd/types/ranges.ipp | 78 ++++++++++++++++++++++++++---------- host/lib/usrp/dboard/db_dbsrx.cpp | 6 +-- host/lib/usrp/dboard/db_rfx.cpp | 2 +- host/lib/usrp/dboard/db_tvrx.cpp | 6 +-- host/lib/usrp/dboard/db_wbx.cpp | 9 ++--- host/lib/usrp/dboard/db_xcvr2450.cpp | 10 +++-- 7 files changed, 82 insertions(+), 37 deletions(-) (limited to 'host/lib/usrp/dboard/db_rfx.cpp') diff --git a/host/include/uhd/types/ranges.hpp b/host/include/uhd/types/ranges.hpp index 0811fc4c8..206c64726 100644 --- a/host/include/uhd/types/ranges.hpp +++ b/host/include/uhd/types/ranges.hpp @@ -93,6 +93,14 @@ namespace uhd{ //! Get the overall step value for this meta-range. const T step(void) const; + /*! + * Clip the target value to a possible range value. + * \param value the value to clip to this range + * \param clip_step if true, clip to steps as well + * \return a value that is in one of the ranges + */ + const T clip(const T &value, bool clip_step = false) const; + }; typedef meta_range_t gain_range_t; diff --git a/host/include/uhd/types/ranges.ipp b/host/include/uhd/types/ranges.ipp index cb1628c53..7fd1bf2eb 100644 --- a/host/include/uhd/types/ranges.ipp +++ b/host/include/uhd/types/ranges.ipp @@ -19,6 +19,7 @@ #define INCLUDED_UHD_TYPES_RANGES_IPP #include +#include #include #include #include @@ -69,6 +70,21 @@ namespace uhd{ * meta_range_t implementation code ******************************************************************/ + namespace /*anon*/{ + template inline + void check_meta_range_monotonic(const meta_range_t &mr){ + if (mr.empty()){ + throw std::runtime_error("meta-range cannot be empty"); + } + for (size_t i = 1; i < mr.size(); i++){ + if (mr.at(i).start() < mr.at(i-1).stop()){ + throw std::runtime_error("meta-range is not monotonic"); + } + } + } + } //namespace /*anon*/ + + template meta_range_t::meta_range_t(void){ /* NOP */ } @@ -91,42 +107,60 @@ namespace uhd{ } template const T meta_range_t::start(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall start on empty meta-range"); - } - T min_start = this->at(0).start(); - for (size_t i = 1; i < this->size(); i++){ - min_start = std::min(min_start, this->at(i).start()); + check_meta_range_monotonic(*this); + T min_start = this->front().start(); + BOOST_FOREACH(const range_t &r, (*this)){ + min_start = std::min(min_start, r.start()); } return min_start; } template const T meta_range_t::stop(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall stop on empty meta-range"); - } - T max_stop = this->at(0).stop(); - for (size_t i = 1; i < this->size(); i++){ - max_stop = std::max(max_stop, this->at(i).stop()); + check_meta_range_monotonic(*this); + T max_stop = this->front().stop(); + BOOST_FOREACH(const range_t &r, (*this)){ + max_stop = std::max(max_stop, r.stop()); } return max_stop; } template const T meta_range_t::step(void) const{ - if (this->empty()){ - throw std::runtime_error("cannot calculate overall step on empty meta-range"); + check_meta_range_monotonic(*this); + std::vector non_zero_steps; + range_t last = this->front(); + BOOST_FOREACH(const range_t &r, (*this)){ + //steps at each range + if (r.step() != T(0)) non_zero_steps.push_back(r.step()); + //and steps in-between ranges + T ibtw_step = r.start() - last.stop(); + if (ibtw_step != T(0)) non_zero_steps.push_back(ibtw_step); + //store ref to last + last = r; } - T min_step = this->at(0).step(); - for (size_t i = 1; i < this->size(); i++){ - if (this->at(i).start() < this->at(i-1).stop()){ - throw std::runtime_error("cannot calculate overall range when start(n) < stop(n-1) "); + if (non_zero_steps.empty()) return T(0); //all zero steps, its zero... + return *std::min_element(non_zero_steps.begin(), non_zero_steps.end()); + } + + template const T meta_range_t::clip( + const T &value, bool clip_step + ) const{ + check_meta_range_monotonic(*this); + T last_stop = this->front().stop(); + BOOST_FOREACH(const range_t &r, (*this)){ + //in-between ranges, clip to nearest + if (value < r.start()){ + return (std::abs(value - r.start()) < std::abs(value - last_stop))? + r.start() : last_stop; } - if (this->at(i).start() != this->at(i).stop()){ - min_step = std::min(min_step, this->at(i).step()); + //in this range, clip here + if (value <= r.stop()){ + if (not clip_step or r.step() == T(0)) return value; + return boost::math::round((value - r.start())/r.step())*r.step() + r.start(); } - min_step = std::min(min_step, this->at(i).start() - this->at(i-1).stop()); + //continue on to the next range + last_stop = r.stop(); } - return min_step; + return last_stop; } } //namespace uhd diff --git a/host/lib/usrp/dboard/db_dbsrx.cpp b/host/lib/usrp/dboard/db_dbsrx.cpp index 70bf03158..5316fb952 100644 --- a/host/lib/usrp/dboard/db_dbsrx.cpp +++ b/host/lib/usrp/dboard/db_dbsrx.cpp @@ -229,7 +229,7 @@ dbsrx::~dbsrx(void){ * Tuning **********************************************************************/ void dbsrx::set_lo_freq(double target_freq){ - target_freq = std::clip(target_freq, dbsrx_freq_range.start(), dbsrx_freq_range.stop()); + target_freq = dbsrx_freq_range.clip(target_freq); double actual_freq=0.0, pfd_freq=0.0, ref_clock=0.0; int R=0, N=0, r=0, m=0; @@ -417,7 +417,7 @@ void dbsrx::set_lo_freq(double target_freq){ */ static int gain_to_gc2_vga_reg(float &gain){ int reg = 0; - gain = std::clip(gain, dbsrx_gain_ranges["GC2"].start(), dbsrx_gain_ranges["GC2"].stop()); + gain = dbsrx_gain_ranges["GC2"].clip(gain); // Half dB steps from 0-5dB, 1dB steps from 5-24dB if (gain < 5) { @@ -443,7 +443,7 @@ static int gain_to_gc2_vga_reg(float &gain){ */ static float gain_to_gc1_rfvga_dac(float &gain){ //clip the input - gain = std::clip(gain, dbsrx_gain_ranges["GC1"].start(), dbsrx_gain_ranges["GC1"].stop()); + gain = dbsrx_gain_ranges["GC1"].clip(gain); //voltage level constants static const float max_volts = float(1.2), min_volts = float(2.7); diff --git a/host/lib/usrp/dboard/db_rfx.cpp b/host/lib/usrp/dboard/db_rfx.cpp index bb9e19708..4e73fb3a3 100644 --- a/host/lib/usrp/dboard/db_rfx.cpp +++ b/host/lib/usrp/dboard/db_rfx.cpp @@ -292,7 +292,7 @@ double rfx_xcvr::set_lo_freq( ) % (target_freq/1e6) << std::endl; //clip the input - target_freq = std::clip(target_freq, _freq_range.start(), _freq_range.stop()); + target_freq = _freq_range.clip(target_freq); if (_div2[unit]) target_freq *= 2; //map prescalers to the register enums diff --git a/host/lib/usrp/dboard/db_tvrx.cpp b/host/lib/usrp/dboard/db_tvrx.cpp index 94ab86898..17fdad74a 100644 --- a/host/lib/usrp/dboard/db_tvrx.cpp +++ b/host/lib/usrp/dboard/db_tvrx.cpp @@ -277,7 +277,7 @@ static double gain_interp(double gain, boost::array db_vector, boost static float rf_gain_to_voltage(float gain, double lo_freq){ //clip the input - gain = std::clip(gain, get_tvrx_gain_ranges()["RF"].start(), get_tvrx_gain_ranges()["RF"].stop()); + gain = get_tvrx_gain_ranges()["RF"].clip(gain); //first we need to find out what band we're in, because gains are different across different bands std::string band = get_band(lo_freq + tvrx_if_freq); @@ -305,7 +305,7 @@ static float rf_gain_to_voltage(float gain, double lo_freq){ static float if_gain_to_voltage(float gain){ //clip the input - gain = std::clip(gain, get_tvrx_gain_ranges()["IF"].start(), get_tvrx_gain_ranges()["IF"].stop()); + gain = get_tvrx_gain_ranges()["IF"].clip(gain); double gain_volts = gain_interp(gain, tvrx_if_gains_db, tvrx_gains_volts); double dac_volts = gain_volts / opamp_gain; @@ -337,7 +337,7 @@ void tvrx::set_gain(float gain, const std::string &name){ */ void tvrx::set_freq(double freq) { - freq = std::clip(freq, tvrx_freq_range.start(), tvrx_freq_range.stop()); + freq = tvrx_freq_range.clip(freq); std::string prev_band = get_band(_lo_freq - tvrx_if_freq); std::string new_band = get_band(freq); diff --git a/host/lib/usrp/dboard/db_wbx.cpp b/host/lib/usrp/dboard/db_wbx.cpp index d68cd4eaa..dd5bd600b 100644 --- a/host/lib/usrp/dboard/db_wbx.cpp +++ b/host/lib/usrp/dboard/db_wbx.cpp @@ -198,16 +198,15 @@ wbx_xcvr::~wbx_xcvr(void){ **********************************************************************/ static int rx_pga0_gain_to_iobits(float &gain){ //clip the input - gain = std::clip(gain, wbx_rx_gain_ranges["PGA0"].start(), wbx_rx_gain_ranges["PGA0"].stop()); + gain = wbx_rx_gain_ranges["PGA0"].clip(gain); //convert to attenuation and update iobits for atr float attn = wbx_rx_gain_ranges["PGA0"].stop() - gain; //calculate the attenuation - int attn_code = int(floor(attn*2)); + int attn_code = boost::math::iround(attn*2); int iobits = ((~attn_code) << RX_ATTN_SHIFT) & RX_ATTN_MASK; - if (wbx_debug) std::cerr << boost::format( "WBX Attenuation: %f dB, Code: %d, IO Bits %x, Mask: %x" ) % attn % attn_code % (iobits & RX_ATTN_MASK) % RX_ATTN_MASK << std::endl; @@ -220,7 +219,7 @@ static int rx_pga0_gain_to_iobits(float &gain){ static float tx_pga0_gain_to_dac_volts(float &gain){ //clip the input - gain = std::clip(gain, wbx_tx_gain_ranges["PGA0"].start(), wbx_tx_gain_ranges["PGA0"].stop()); + gain = wbx_tx_gain_ranges["PGA0"].clip(gain); //voltage level constants static const float max_volts = float(0.5), min_volts = float(1.4); @@ -328,7 +327,7 @@ double wbx_xcvr::set_lo_freq( ) % (target_freq/1e6) << std::endl; //clip the input - target_freq = std::clip(target_freq, wbx_freq_range.start(), wbx_freq_range.stop()); + target_freq = wbx_freq_range.clip(target_freq); //map prescaler setting to mininmum integer divider (N) values (pg.18 prescaler) static const uhd::dict prescaler_to_min_int_div = map_list_of diff --git a/host/lib/usrp/dboard/db_xcvr2450.cpp b/host/lib/usrp/dboard/db_xcvr2450.cpp index d3084b0a0..a3a1e6242 100644 --- a/host/lib/usrp/dboard/db_xcvr2450.cpp +++ b/host/lib/usrp/dboard/db_xcvr2450.cpp @@ -84,7 +84,11 @@ static const uhd::dict xcvr_tx_gain_ranges = map_list ("BB", gain_range_t(0, 5, 1.5)) ; static const uhd::dict xcvr_rx_gain_ranges = map_list_of - ("LNA", gain_range_t(0, 30.5, 15)) + ("LNA", gain_range_t(list_of + (range_t(0)) + (range_t(15)) + (range_t(30.5)) + )) ("VGA", gain_range_t(0, 62, 2.0)) ; @@ -262,8 +266,8 @@ void xcvr2450::update_atr(void){ **********************************************************************/ void xcvr2450::set_lo_freq(double target_freq){ - //clip the input to the range (TODO FIXME not right) - target_freq = std::clip(target_freq, xcvr_freq_range.start(), xcvr_freq_range.stop()); + //clip the input to the range + target_freq = xcvr_freq_range.clip(target_freq); //variables used in the calculation below double scaler = xcvr2450::is_highband(target_freq)? (4.0/5.0) : (4.0/3.0); -- cgit v1.2.3 From b08ac6273fa6dd1e757ecaeb0b316592696099f7 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 11 Nov 2010 19:13:22 -0800 Subject: uhd: removed windows warnings, added string formatting in usrp-n --- host/lib/usrp/dboard/db_dbsrx2.cpp | 2 +- host/lib/usrp/dboard/db_rfx.cpp | 2 +- host/lib/usrp/usrp2/codec_impl.cpp | 14 +++++++------- host/lib/usrp/usrp2/dboard_impl.cpp | 4 ++-- host/lib/usrp/usrp2/dsp_impl.cpp | 4 ++-- host/lib/usrp/usrp2/usrp2_iface.cpp | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) (limited to 'host/lib/usrp/dboard/db_rfx.cpp') diff --git a/host/lib/usrp/dboard/db_dbsrx2.cpp b/host/lib/usrp/dboard/db_dbsrx2.cpp index bf5528688..cdafd6a78 100644 --- a/host/lib/usrp/dboard/db_dbsrx2.cpp +++ b/host/lib/usrp/dboard/db_dbsrx2.cpp @@ -203,7 +203,7 @@ dbsrx2::dbsrx2(ctor_args_t args) : rx_dboard_base(args){ set_bandwidth(40e6); // default bandwidth from datasheet get_locked(); - _max2112_write_regs.bbg = dbsrx2_gain_ranges["BBG"].start(); + _max2112_write_regs.bbg = boost::math::iround(dbsrx2_gain_ranges["BBG"].start()); send_reg(0x9, 0x9); } diff --git a/host/lib/usrp/dboard/db_rfx.cpp b/host/lib/usrp/dboard/db_rfx.cpp index 4e73fb3a3..74a9fb37b 100644 --- a/host/lib/usrp/dboard/db_rfx.cpp +++ b/host/lib/usrp/dboard/db_rfx.cpp @@ -171,7 +171,7 @@ rfx_xcvr::rfx_xcvr( ): xcvr_dboard_base(args), _freq_range(freq_range), - _rx_gain_ranges((this->get_rx_id() == 0x0024)? + _rx_gain_ranges((get_rx_id() == 0x0024)? rfx400_rx_gain_ranges : rfx_rx_gain_ranges ), _div2(map_list_of diff --git a/host/lib/usrp/usrp2/codec_impl.cpp b/host/lib/usrp/usrp2/codec_impl.cpp index 998d55297..e417bc340 100644 --- a/host/lib/usrp/usrp2/codec_impl.cpp +++ b/host/lib/usrp/usrp2/codec_impl.cpp @@ -30,9 +30,9 @@ using namespace boost::assign; //this only applies to USRP2P static const uhd::dict codec_rx_gain_ranges = map_list_of - ("analog", gain_range_t(0, 3.5, 3.5)) - ("digital", gain_range_t(0, 6.0, 0.5)) - ("digital-fine", gain_range_t(0, 0.5, 0.05)); + ("analog", gain_range_t(0, float(3.5), float(3.5))) + ("digital", gain_range_t(0, float(6.0), float(0.5))) + ("digital-fine", gain_range_t(0, float(0.5), float(0.05))); /*********************************************************************** @@ -62,16 +62,16 @@ void usrp2_mboard_impl::rx_codec_get(const wax::obj &key_, wax::obj &val){ switch(_iface->get_rev()){ case usrp2_iface::USRP_N200: case usrp2_iface::USRP_N210: - val = std::string(_iface->get_cname() + " adc - ads62p44"); + val = _iface->get_cname() + " adc - ads62p44"; break; case usrp2_iface::USRP2_REV3: case usrp2_iface::USRP2_REV4: - val = std::string(_iface->get_cname() + " adc - ltc2284"); + val = _iface->get_cname() + " adc - ltc2284"; break; case usrp2_iface::USRP_NXXX: - val = std::string(_iface->get_cname() + " adc - ??????"); + val = _iface->get_cname() + " adc - ??????"; break; } return; @@ -153,7 +153,7 @@ void usrp2_mboard_impl::tx_codec_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case CODEC_PROP_NAME: - val = std::string(_iface->get_cname() + " dac - ad9777"); + val = _iface->get_cname() + " dac - ad9777"; return; case CODEC_PROP_OTHERS: diff --git a/host/lib/usrp/usrp2/dboard_impl.cpp b/host/lib/usrp/usrp2/dboard_impl.cpp index f839a4058..4192c4f78 100644 --- a/host/lib/usrp/usrp2/dboard_impl.cpp +++ b/host/lib/usrp/usrp2/dboard_impl.cpp @@ -64,7 +64,7 @@ void usrp2_mboard_impl::rx_dboard_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case DBOARD_PROP_NAME: - val = std::string(_iface->get_cname() + " dboard (rx unit)"); + val = _iface->get_cname() + " dboard (rx unit)"; return; case DBOARD_PROP_SUBDEV: @@ -121,7 +121,7 @@ void usrp2_mboard_impl::tx_dboard_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case DBOARD_PROP_NAME: - val = std::string(_iface->get_cname() + " dboard (tx unit)"); + val = _iface->get_cname() + " dboard (tx unit)"; return; case DBOARD_PROP_SUBDEV: diff --git a/host/lib/usrp/usrp2/dsp_impl.cpp b/host/lib/usrp/usrp2/dsp_impl.cpp index 243a8e905..77ed594f5 100644 --- a/host/lib/usrp/usrp2/dsp_impl.cpp +++ b/host/lib/usrp/usrp2/dsp_impl.cpp @@ -61,7 +61,7 @@ void usrp2_mboard_impl::ddc_get(const wax::obj &key_, wax::obj &val){ switch(key.as()){ case DSP_PROP_NAME: - val = std::string(_iface->get_cname() + " ddc0"); + val = _iface->get_cname() + " ddc0"; return; case DSP_PROP_OTHERS: @@ -144,7 +144,7 @@ void usrp2_mboard_impl::duc_get(const wax::obj &key_, wax::obj &val){ switch(key.as()){ case DSP_PROP_NAME: - val = std::string(_iface->get_cname() + " duc0"); + val = _iface->get_cname() + " duc0"; return; case DSP_PROP_OTHERS: diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index 7208a47cf..2b32faffb 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -274,8 +274,8 @@ public: rev_type get_rev(void){ switch (boost::lexical_cast(mb_eeprom["rev"])){ - case 0x0003: return USRP2_REV3; - case 0x0004: return USRP2_REV4; + case 0x0300: return USRP2_REV3; + case 0x0400: return USRP2_REV4; case 0x0A00: return USRP_N200; case 0x0A01: return USRP_N210; } -- cgit v1.2.3