diff options
author | Josh Blum <josh@joshknows.com> | 2010-04-26 00:17:08 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-04-26 00:17:08 -0700 |
commit | 0c609b96574095affe12d9aaa53bead98faba4f3 (patch) | |
tree | 9e10b9d6ddabca60b33898514df5948f8ed02b18 /host | |
parent | 61ec6711bb4bbae7a8a26cc631eeb88fb3e7d688 (diff) | |
download | uhd-0c609b96574095affe12d9aaa53bead98faba4f3.tar.gz uhd-0c609b96574095affe12d9aaa53bead98faba4f3.tar.bz2 uhd-0c609b96574095affe12d9aaa53bead98faba4f3.zip |
Added i2c interface to serial.hpp, using in usrp2_iface for i2c and eeprom.
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/types/serial.hpp | 61 | ||||
-rw-r--r-- | host/lib/types.cpp | 27 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/usrp2_iface.cpp | 21 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/usrp2_iface.hpp | 48 |
4 files changed, 89 insertions, 68 deletions
diff --git a/host/include/uhd/types/serial.hpp b/host/include/uhd/types/serial.hpp index b0fe5d7bd..c134725f5 100644 --- a/host/include/uhd/types/serial.hpp +++ b/host/include/uhd/types/serial.hpp @@ -30,6 +30,67 @@ namespace uhd{ typedef std::vector<boost::uint8_t> byte_vector_t; /*! + * The i2c interface class: + * Provides i2c and eeprom functionality. + * A subclass should only have to implement the i2c routines. + * An eeprom implementation comes for free with the interface. + * + * The eeprom routines are implemented on top of i2c. + * The built in eeprom implementation only does single + * byte reads and byte writes over the i2c interface, + * so it should be portable across multiple eeproms. + * Override the eeprom routines if this is not acceptable. + */ + class UHD_API i2c_iface{ + public: + /*! + * Write bytes over the i2c. + * \param addr the address + * \param buf the vector of bytes + */ + virtual void write_i2c( + boost::uint8_t addr, + const byte_vector_t &buf + ) = 0; + + /*! + * Read bytes over the i2c. + * \param addr the address + * \param num_bytes number of bytes to read + * \return a vector of bytes + */ + virtual byte_vector_t read_i2c( + boost::uint8_t addr, + size_t num_bytes + ) = 0; + + /*! + * Write bytes to an eeprom. + * \param addr the address + * \param offset byte offset + * \param buf the vector of bytes + */ + virtual void write_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + const byte_vector_t &buf + ); + + /*! + * Read bytes from an eeprom. + * \param addr the address + * \param offset byte offset + * \param num_bytes number of bytes to read + * \return a vector of bytes + */ + virtual byte_vector_t read_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + size_t num_bytes + ); + }; + + /*! * The SPI configuration struct: * Used to configure a SPI transaction interface. */ diff --git a/host/lib/types.cpp b/host/lib/types.cpp index 91887840c..a1b9b21a9 100644 --- a/host/lib/types.cpp +++ b/host/lib/types.cpp @@ -31,6 +31,7 @@ #include <boost/foreach.hpp> #include <boost/format.hpp> #include <boost/cstdint.hpp> +#include <boost/assign/list_of.hpp> #include <stdexcept> #include <complex> @@ -250,3 +251,29 @@ spi_config_t::spi_config_t(edge_t edge){ mosi_edge = edge; miso_edge = edge; } + +void i2c_iface::write_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + const byte_vector_t &bytes +){ + BOOST_FOREACH(boost::uint8_t byte, bytes){ + //write a byte at a time, its easy that way + byte_vector_t cmd = boost::assign::list_of(offset)(byte); + this->write_i2c(addr, cmd); + } +} + +byte_vector_t i2c_iface::read_eeprom( + boost::uint8_t addr, + boost::uint8_t offset, + size_t num_bytes +){ + byte_vector_t bytes; + for (size_t i = 0; i < num_bytes; i++){ + //do a zero byte write to start read cycle + this->write_i2c(addr, byte_vector_t(1, offset)); + bytes.push_back(this->read_i2c(addr, 1).at(0)); + } + return bytes; +} diff --git a/host/lib/usrp/usrp2/usrp2_iface.cpp b/host/lib/usrp/usrp2/usrp2_iface.cpp index 76ee5d6fe..27b6f8907 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.cpp +++ b/host/lib/usrp/usrp2/usrp2_iface.cpp @@ -134,27 +134,6 @@ public: } /*********************************************************************** - * EEPROM - **********************************************************************/ - void write_eeprom(boost::uint8_t addr, boost::uint8_t offset, const byte_vector_t &bytes){ - BOOST_FOREACH(boost::uint8_t byte, bytes){ - //write a byte at a time, its easy that way - byte_vector_t cmd = boost::assign::list_of(offset)(byte); - this->write_i2c(addr, cmd); - } - } - - byte_vector_t read_eeprom(boost::uint8_t addr, boost::uint8_t offset, size_t num_bytes){ - byte_vector_t bytes; - for (size_t i = 0; i < num_bytes; i++){ - //do a zero byte write to start read cycle - write_i2c(addr, byte_vector_t(1, offset)); - bytes.push_back(read_i2c(addr, 1).at(0)); - } - return bytes; - } - -/*********************************************************************** * Send/Recv over control **********************************************************************/ usrp2_ctrl_data_t ctrl_send_and_recv(const usrp2_ctrl_data_t &out_data){ diff --git a/host/lib/usrp/usrp2/usrp2_iface.hpp b/host/lib/usrp/usrp2/usrp2_iface.hpp index 938359677..7158c58d0 100644 --- a/host/lib/usrp/usrp2/usrp2_iface.hpp +++ b/host/lib/usrp/usrp2/usrp2_iface.hpp @@ -39,7 +39,7 @@ * Provides a set of functions to implementation layer. * Including spi, peek, poke, control... */ -class usrp2_iface : boost::noncopyable{ +class usrp2_iface : public uhd::i2c_iface, boost::noncopyable{ public: typedef boost::shared_ptr<usrp2_iface> sptr; @@ -103,52 +103,6 @@ public: ) = 0; /*! - * Write bytes over the i2c. - * \param addr the address - * \param buf the vector of bytes - */ - virtual void write_i2c( - boost::uint8_t addr, - const uhd::byte_vector_t &buf - ) = 0; - - /*! - * Read bytes over the i2c. - * \param addr the address - * \param num_bytes number of bytes to read - * \return a vector of bytes - */ - virtual uhd::byte_vector_t read_i2c( - boost::uint8_t addr, - size_t num_bytes - ) = 0; - - /*! - * Write bytes to an eeprom. - * \param addr the address - * \param offset byte offset - * \param buf the vector of bytes - */ - virtual void write_eeprom( - boost::uint8_t addr, - boost::uint8_t offset, - const uhd::byte_vector_t &buf - ) = 0; - - /*! - * Read bytes from an eeprom. - * \param addr the address - * \param offset byte offset - * \param num_bytes number of bytes to read - * \return a vector of bytes - */ - virtual uhd::byte_vector_t read_eeprom( - boost::uint8_t addr, - boost::uint8_t offset, - size_t num_bytes - ) = 0; - - /*! * Get the master clock frequency. * \return the frequency in Hz */ |