diff options
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/config.hpp | 4 | ||||
-rw-r--r-- | host/include/uhd/types/dict.hpp | 4 | ||||
-rw-r--r-- | host/include/uhd/utils/algorithm.hpp | 30 | ||||
-rw-r--r-- | host/include/uhd/utils/assert.hpp | 18 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/dboard_iface.cpp | 57 | ||||
-rw-r--r-- | host/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | host/test/dict_test.cpp | 72 | ||||
-rw-r--r-- | host/test/error_test.cpp | 40 |
8 files changed, 154 insertions, 73 deletions
diff --git a/host/include/uhd/config.hpp b/host/include/uhd/config.hpp index 941219ac7..227c473ce 100644 --- a/host/include/uhd/config.hpp +++ b/host/include/uhd/config.hpp @@ -46,12 +46,12 @@ // http://gcc.gnu.org/wiki/Visibility // Generic helper definitions for shared library support -#if defined _WIN32 || defined __CYGWIN__ +#if BOOST_HAS_DECLSPEC #define UHD_HELPER_DLL_IMPORT __declspec(dllimport) #define UHD_HELPER_DLL_EXPORT __declspec(dllexport) #define UHD_HELPER_DLL_LOCAL #else - #if __GNUC__ >= 4 + #if __GNUG__ >= 4 #define UHD_HELPER_DLL_IMPORT __attribute__ ((visibility("default"))) #define UHD_HELPER_DLL_EXPORT __attribute__ ((visibility("default"))) #define UHD_HELPER_DLL_LOCAL __attribute__ ((visibility("hidden"))) diff --git a/host/include/uhd/types/dict.hpp b/host/include/uhd/types/dict.hpp index c8fbc5a9f..b5fb11120 100644 --- a/host/include/uhd/types/dict.hpp +++ b/host/include/uhd/types/dict.hpp @@ -29,7 +29,7 @@ namespace uhd{ /*! * A templated dictionary class with a python-like interface. */ - template <class Key, class Val> class dict{ + template <typename Key, typename Val> class dict{ public: typedef std::pair<Key, Val> pair_t; @@ -130,7 +130,7 @@ namespace uhd{ BOOST_FOREACH(pair_t &p, _map){ if (p.first == key) return p.second; } - _map.push_back(pair_t(key, Val())); + _map.push_back(std::make_pair(key, Val())); return _map.back().second; } diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp index 8fe9cde82..72b655745 100644 --- a/host/include/uhd/utils/algorithm.hpp +++ b/host/include/uhd/utils/algorithm.hpp @@ -19,43 +19,25 @@ #define INCLUDED_UHD_UTILS_ALGORITHM_HPP #include <algorithm> +#include <boost/range/functions.hpp> /*! * Useful templated functions and classes that I like to pretend are part of stl */ namespace std{ - template<class T, class InputIterator, class Function> - T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){ - T tmp = init; - for ( ; first != last; ++first ){ - tmp = fcn(tmp, *first); - } - return tmp; + template<typename Range, typename T> inline + bool has(const Range &range, const T &value){ + return boost::end(range) != std::find(boost::begin(range), boost::end(range), value); } - template<class T, class Iterable, class Function> - T reduce(Iterable iterable, Function fcn, T init = 0){ - return reduce(iterable.begin(), iterable.end(), fcn, init); - } - - template<class T, class InputIterator> - bool has(InputIterator first, InputIterator last, const T &elem){ - return last != std::find(first, last, elem); - } - - template<class T, class Iterable> - bool has(const Iterable &iterable, const T &elem){ - return has(iterable.begin(), iterable.end(), elem); - } - - template<class T> T signum(T n){ + template<typename T> inline T signum(T n){ if (n < 0) return -1; if (n > 0) return 1; return 0; } - template<class T> T clip(T val, T minVal, T maxVal){ + template<typename T> inline T clip(T val, T minVal, T maxVal){ return std::min(std::max(val, minVal), maxVal); } diff --git a/host/include/uhd/utils/assert.hpp b/host/include/uhd/utils/assert.hpp index 01beed757..ed0a95535 100644 --- a/host/include/uhd/utils/assert.hpp +++ b/host/include/uhd/utils/assert.hpp @@ -46,28 +46,28 @@ namespace uhd{ * The "what" in the error will show what is * being set and a list of known good values. * - * \param iterable a list of possible settings - * \param elem an element that may be in the list + * \param range a list of possible settings + * \param value an element that may be in the list * \param what a description of what is being set * \throw assertion_error when elem not in list */ - template<class T, class Iterable> void assert_has( - const Iterable &iterable, - const T &elem, + template<typename T, typename Range> void assert_has( + const Range &range, + const T &value, const std::string &what = "unknown" ){ - if (std::has(iterable, elem)) return; + if (std::has(range, value)) return; std::string possible_values = ""; size_t i = 0; - BOOST_FOREACH(const T &e, iterable){ + BOOST_FOREACH(const T &v, range){ if (i++ > 0) possible_values += ", "; - possible_values += boost::lexical_cast<std::string>(e); + possible_values += boost::lexical_cast<std::string>(v); } boost::throw_exception(uhd::assert_error() << assert_info(str(boost::format( "Error: %s is not a valid %s. " "Possible values are: [%s]." ) - % boost::lexical_cast<std::string>(elem) + % boost::lexical_cast<std::string>(value) % what % possible_values ))); } diff --git a/host/lib/usrp/usrp2/dboard_iface.cpp b/host/lib/usrp/usrp2/dboard_iface.cpp index 74d80163c..372a5af07 100644 --- a/host/lib/usrp/usrp2/dboard_iface.cpp +++ b/host/lib/usrp/usrp2/dboard_iface.cpp @@ -29,6 +29,7 @@ using namespace uhd; using namespace uhd::usrp; +using namespace boost::assign; class usrp2_dboard_iface : public dboard_iface{ public: @@ -122,49 +123,42 @@ double usrp2_dboard_iface::get_clock_rate(unit_t){ void usrp2_dboard_iface::set_clock_enabled(unit_t unit, bool enb){ switch(unit){ - case UNIT_RX: - _clk_ctrl->enable_rx_dboard_clock(enb); - return; - case UNIT_TX: - _clk_ctrl->enable_tx_dboard_clock(enb); - return; + case UNIT_RX: _clk_ctrl->enable_rx_dboard_clock(enb); return; + case UNIT_TX: _clk_ctrl->enable_tx_dboard_clock(enb); return; } } /*********************************************************************** * GPIO **********************************************************************/ -static int unit_to_shift(dboard_iface::unit_t unit){ - switch(unit){ - case dboard_iface::UNIT_RX: return 0; - case dboard_iface::UNIT_TX: return 16; - } - throw std::runtime_error("unknown unit type"); -} +static const uhd::dict<dboard_iface::unit_t, int> unit_to_shift = map_list_of + (dboard_iface::UNIT_RX, 0) + (dboard_iface::UNIT_TX, 16) +; void usrp2_dboard_iface::set_gpio_ddr(unit_t unit, boost::uint16_t value){ _ddr_shadow = \ - (_ddr_shadow & ~(0xffff << unit_to_shift(unit))) | - (boost::uint32_t(value) << unit_to_shift(unit)); + (_ddr_shadow & ~(0xffff << unit_to_shift[unit])) | + (boost::uint32_t(value) << unit_to_shift[unit]); _iface->poke32(FR_GPIO_DDR, _ddr_shadow); } boost::uint16_t usrp2_dboard_iface::read_gpio(unit_t unit){ - return boost::uint16_t(_iface->peek32(FR_GPIO_IO) >> unit_to_shift(unit)); + return boost::uint16_t(_iface->peek32(FR_GPIO_IO) >> unit_to_shift[unit]); } void usrp2_dboard_iface::set_atr_reg(unit_t unit, atr_reg_t atr, boost::uint16_t value){ //define mapping of unit to atr regs to register address static const uhd::dict< unit_t, uhd::dict<atr_reg_t, boost::uint32_t> - > unit_to_atr_to_addr = boost::assign::map_list_of - (UNIT_RX, boost::assign::map_list_of + > unit_to_atr_to_addr = map_list_of + (UNIT_RX, map_list_of (ATR_REG_IDLE, FR_ATR_IDLE_RXSIDE) (ATR_REG_TX_ONLY, FR_ATR_INTX_RXSIDE) (ATR_REG_RX_ONLY, FR_ATR_INRX_RXSIDE) (ATR_REG_FULL_DUPLEX, FR_ATR_FULL_RXSIDE) ) - (UNIT_TX, boost::assign::map_list_of + (UNIT_TX, map_list_of (ATR_REG_IDLE, FR_ATR_IDLE_TXSIDE) (ATR_REG_TX_ONLY, FR_ATR_INTX_TXSIDE) (ATR_REG_RX_ONLY, FR_ATR_INRX_TXSIDE) @@ -177,19 +171,10 @@ void usrp2_dboard_iface::set_atr_reg(unit_t unit, atr_reg_t atr, boost::uint16_t /*********************************************************************** * SPI **********************************************************************/ -/*! - * Static function to convert a unit type enum - * to an over-the-wire value for the spi device. - * \param unit the dboard interface unit type enum - * \return an over the wire representation - */ -static boost::uint8_t unit_to_otw_spi_dev(dboard_iface::unit_t unit){ - switch(unit){ - case dboard_iface::UNIT_TX: return SPI_SS_TX_DB; - case dboard_iface::UNIT_RX: return SPI_SS_RX_DB; - } - throw std::invalid_argument("unknown unit type"); -} +static const uhd::dict<dboard_iface::unit_t, int> unit_to_spi_dev = map_list_of + (dboard_iface::UNIT_TX, SPI_SS_TX_DB) + (dboard_iface::UNIT_RX, SPI_SS_RX_DB) +; void usrp2_dboard_iface::write_spi( unit_t unit, @@ -197,7 +182,7 @@ void usrp2_dboard_iface::write_spi( boost::uint32_t data, size_t num_bits ){ - _iface->transact_spi(unit_to_otw_spi_dev(unit), config, data, num_bits, false /*no rb*/); + _iface->transact_spi(unit_to_spi_dev[unit], config, data, num_bits, false /*no rb*/); } boost::uint32_t usrp2_dboard_iface::read_write_spi( @@ -206,7 +191,7 @@ boost::uint32_t usrp2_dboard_iface::read_write_spi( boost::uint32_t data, size_t num_bits ){ - return _iface->transact_spi(unit_to_otw_spi_dev(unit), config, data, num_bits, true /*rb*/); + return _iface->transact_spi(unit_to_spi_dev[unit], config, data, num_bits, true /*rb*/); } /*********************************************************************** @@ -224,7 +209,7 @@ byte_vector_t usrp2_dboard_iface::read_i2c(boost::uint8_t addr, size_t num_bytes * Aux DAX/ADC **********************************************************************/ void usrp2_dboard_iface::_write_aux_dac(unit_t unit){ - static const uhd::dict<unit_t, int> unit_to_spi_dac = boost::assign::map_list_of + static const uhd::dict<unit_t, int> unit_to_spi_dac = map_list_of (UNIT_RX, SPI_SS_RX_DAC) (UNIT_TX, SPI_SS_TX_DAC) ; @@ -248,7 +233,7 @@ void usrp2_dboard_iface::write_aux_dac(unit_t unit, int which, float value){ } float usrp2_dboard_iface::read_aux_adc(unit_t unit, int which){ - static const uhd::dict<unit_t, int> unit_to_spi_adc = boost::assign::map_list_of + static const uhd::dict<unit_t, int> unit_to_spi_adc = map_list_of (UNIT_RX, SPI_SS_RX_ADC) (UNIT_TX, SPI_SS_TX_ADC) ; diff --git a/host/test/CMakeLists.txt b/host/test/CMakeLists.txt index 1791d9082..61b0b503d 100644 --- a/host/test/CMakeLists.txt +++ b/host/test/CMakeLists.txt @@ -21,6 +21,8 @@ ADD_EXECUTABLE(main_test main_test.cpp addr_test.cpp + dict_test.cpp + error_test.cpp gain_handler_test.cpp vrt_test.cpp wax_test.cpp diff --git a/host/test/dict_test.cpp b/host/test/dict_test.cpp new file mode 100644 index 000000000..0501a7878 --- /dev/null +++ b/host/test/dict_test.cpp @@ -0,0 +1,72 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// + +#include <boost/test/unit_test.hpp> +#include <uhd/types/dict.hpp> +#include <boost/assign/list_of.hpp> + +BOOST_AUTO_TEST_CASE(test_dict_init){ + uhd::dict<int, int> d; + d[-1] = 3; + d[0] = 4; + d[1] = 5; + BOOST_CHECK(d.has_key(0)); + BOOST_CHECK(not d.has_key(2)); + BOOST_CHECK(d.keys()[1] == 0); + BOOST_CHECK(d.vals()[1] == 4); + BOOST_CHECK_EQUAL(d[-1], 3); +} + +BOOST_AUTO_TEST_CASE(test_dict_assign){ + uhd::dict<int, int> d = boost::assign::map_list_of + (-1, 3) + (0, 4) + (1, 5) + ; + BOOST_CHECK(d.has_key(0)); + BOOST_CHECK(not d.has_key(2)); + BOOST_CHECK(d.keys()[1] == 0); + BOOST_CHECK(d.vals()[1] == 4); + BOOST_CHECK_EQUAL(d[-1], 3); +} + +BOOST_AUTO_TEST_CASE(test_const_dict){ + const uhd::dict<int, int> d = boost::assign::map_list_of + (-1, 3) + (0, 4) + (1, 5) + ; + BOOST_CHECK(d.has_key(0)); + BOOST_CHECK(not d.has_key(2)); + BOOST_CHECK(d.keys()[1] == 0); + BOOST_CHECK(d.vals()[1] == 4); + BOOST_CHECK_EQUAL(d[-1], 3); + BOOST_CHECK_THROW(d[2], std::exception); +} + +BOOST_AUTO_TEST_CASE(test_dict_pop){ + uhd::dict<int, int> d = boost::assign::map_list_of + (-1, 3) + (0, 4) + (1, 5) + ; + BOOST_CHECK(d.has_key(0)); + BOOST_CHECK_EQUAL(d.pop(0), 4); + BOOST_CHECK(not d.has_key(0)); + BOOST_CHECK(d.keys()[0] == -1); + BOOST_CHECK(d.keys()[1] == 1); +} diff --git a/host/test/error_test.cpp b/host/test/error_test.cpp new file mode 100644 index 000000000..c5b0af45e --- /dev/null +++ b/host/test/error_test.cpp @@ -0,0 +1,40 @@ +// +// Copyright 2010 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// + +#include <boost/test/unit_test.hpp> +#include <uhd/utils/assert.hpp> +#include <boost/exception/diagnostic_information.hpp> +#include <vector> +#include <iostream> + +BOOST_AUTO_TEST_CASE(test_assert_has){ + std::vector<int> vec; + vec.push_back(2); + vec.push_back(3); + vec.push_back(5); + + //verify the std::has utility + BOOST_CHECK(std::has(vec, 2)); + BOOST_CHECK(not std::has(vec, 1)); + + std::cout << "The output of the assert_has error:" << std::endl; + try{ + uhd::assert_has(vec, 1, "prime"); + }catch(const boost::exception &e){ + std::cout << boost::diagnostic_information(e) << std::endl; + } +} |