diff options
author | Josh Blum <josh@joshknows.com> | 2011-02-22 10:19:09 -0800 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-02-22 10:19:09 -0800 |
commit | 814a5c40c6fbaedba3dafabbe30d00a05af6a274 (patch) | |
tree | 79bd05d506e6e0366c729b56cc3b37d734a0aebe /host/lib/usrp/dboard_eeprom.cpp | |
parent | a8bb5ec900d8f2d3d2f274a921d19b564c668323 (diff) | |
download | uhd-814a5c40c6fbaedba3dafabbe30d00a05af6a274.tar.gz uhd-814a5c40c6fbaedba3dafabbe30d00a05af6a274.tar.bz2 uhd-814a5c40c6fbaedba3dafabbe30d00a05af6a274.zip |
uhd: added 9 byte serial to dboard eeprom class
Diffstat (limited to 'host/lib/usrp/dboard_eeprom.cpp')
-rw-r--r-- | host/lib/usrp/dboard_eeprom.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/host/lib/usrp/dboard_eeprom.cpp b/host/lib/usrp/dboard_eeprom.cpp index fa3631948..60d13548a 100644 --- a/host/lib/usrp/dboard_eeprom.cpp +++ b/host/lib/usrp/dboard_eeprom.cpp @@ -18,6 +18,7 @@ #include <uhd/usrp/dboard_eeprom.hpp> #include <uhd/utils/assert.hpp> #include <boost/format.hpp> +#include <algorithm> #include <iostream> using namespace uhd; @@ -25,6 +26,30 @@ using namespace uhd::usrp; static const bool _dboard_eeprom_debug = false; +/*********************************************************************** + * Utility functions + **********************************************************************/ + +//! create a string from a byte vector, return empty if invalid ascii +static const std::string bytes_to_string(const byte_vector_t &bytes){ + std::string out; + BOOST_FOREACH(boost::uint8_t byte, bytes){ + if (byte < 32 or byte > 127) return out; + out += byte; + } + return out; +} + +//! create a byte vector from a string, null terminate unless max length +static const byte_vector_t string_to_bytes(const std::string &string, size_t max_length){ + byte_vector_t bytes; + for (size_t i = 0; i < std::min(string.size(), max_length); i++){ + bytes.push_back(string[i]); + } + if (bytes.size() < max_length - 1) bytes.push_back('\0'); + return bytes; +} + //////////////////////////////////////////////////////////////////////// // format of daughterboard EEPROM // 00: 0xDB code for ``I'm a daughterboard'' @@ -49,6 +74,8 @@ static const bool _dboard_eeprom_debug = false; #define DB_EEPROM_OFFSET_0_MSB 0x06 #define DB_EEPROM_OFFSET_1_LSB 0x07 // offset correction for ADC or DAC 1 #define DB_EEPROM_OFFSET_1_MSB 0x08 +#define DB_EEPROM_SERIAL 0x09 +#define DB_EEPROM_SERIAL_LEN 0x09 //9 ASCII characters #define DB_EEPROM_CHKSUM 0x1f #define DB_EEPROM_CLEN 0x20 // length of common portion of eeprom @@ -76,24 +103,43 @@ dboard_eeprom_t::dboard_eeprom_t(const byte_vector_t &bytes){ ) << std::endl; } } + try{ UHD_ASSERT_THROW(bytes.size() >= DB_EEPROM_CLEN); UHD_ASSERT_THROW(bytes[DB_EEPROM_MAGIC] == DB_EEPROM_MAGIC_VALUE); UHD_ASSERT_THROW(bytes[DB_EEPROM_CHKSUM] == checksum(bytes)); + + //parse the ids id = dboard_id_t::from_uint16(0 | (boost::uint16_t(bytes[DB_EEPROM_ID_LSB]) << 0) | (boost::uint16_t(bytes[DB_EEPROM_ID_MSB]) << 8) ); + + //parse the serial + serial = bytes_to_string( + byte_vector_t(&bytes.at(DB_EEPROM_SERIAL), + &bytes.at(DB_EEPROM_SERIAL+DB_EEPROM_SERIAL_LEN)) + ); + }catch(const uhd::assert_error &){ id = dboard_id_t::none(); + serial = ""; } } byte_vector_t dboard_eeprom_t::get_eeprom_bytes(void){ byte_vector_t bytes(DB_EEPROM_CLEN, 0); //defaults to all zeros bytes[DB_EEPROM_MAGIC] = DB_EEPROM_MAGIC_VALUE; + + //load the id bytes bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id.to_uint16() >> 0); bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id.to_uint16() >> 8); + + //load the serial bytes + byte_vector_t ser_bytes = string_to_bytes(serial, DB_EEPROM_SERIAL_LEN); + std::copy(ser_bytes.begin(), ser_bytes.end(), &bytes.at(DB_EEPROM_SERIAL)); + + //load the checksum bytes[DB_EEPROM_CHKSUM] = checksum(bytes); return bytes; } |