aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/usrp/usrp1/mb_eeprom.cpp
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-09-26 18:22:25 -0700
committerMartin Braun <martin.braun@ettus.com>2017-09-29 10:50:56 -0700
commit83dde40090e0bbd91c304602cc0e3c365f7878bb (patch)
treec404968e3d6d35795f331ade6ad3c176f3c1bc8b /host/lib/usrp/usrp1/mb_eeprom.cpp
parent59736a5bf512db83a6bd7250e14b13c7464770fc (diff)
downloaduhd-83dde40090e0bbd91c304602cc0e3c365f7878bb.tar.gz
uhd-83dde40090e0bbd91c304602cc0e3c365f7878bb.tar.bz2
uhd-83dde40090e0bbd91c304602cc0e3c365f7878bb.zip
uhd: Changed mboard_eeprom_t interface, refactored MB EEPROM code
- uhd::usrp::mboard_eeprom_t is now simply a map. Its commit() method has no utility being a public API call, because the user never gets access to the appropriate I2C object (Minor API breakage) - The central mboard_eeprom.cpp file was broken up and put into many smaller compilation units in every device's implementation folder. - Renamed some of the constants (e.g. B000_* -> USRP1_*, N100_* -> N200_*) - Removed the N000_* EEPROM code, because, well, you know, there's no such device
Diffstat (limited to 'host/lib/usrp/usrp1/mb_eeprom.cpp')
-rw-r--r--host/lib/usrp/usrp1/mb_eeprom.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/host/lib/usrp/usrp1/mb_eeprom.cpp b/host/lib/usrp/usrp1/mb_eeprom.cpp
new file mode 100644
index 000000000..ba05b6fd0
--- /dev/null
+++ b/host/lib/usrp/usrp1/mb_eeprom.cpp
@@ -0,0 +1,90 @@
+//
+// Copyright 2017 Ettus Research (National Instruments Corp.)
+//
+// SPDX-License-Identifier: GPL-3.0
+//
+
+#include "usrp1_impl.hpp"
+#include "eeprom_utils.hpp"
+#include <uhd/usrp/mboard_eeprom.hpp>
+#include <uhd/types/byte_vector.hpp>
+
+namespace {
+ const uint8_t USRP1_EEPROM_ADDR = 0x50;
+ const size_t USRP1_SERIAL_LEN = 8;
+
+ //use char array so we dont need to attribute packed
+ struct usrp1_eeprom_map{
+ unsigned char _r[221];
+ unsigned char mcr[4];
+ unsigned char name[NAME_MAX_LEN];
+ unsigned char serial[USRP1_SERIAL_LEN];
+ };
+}
+
+using namespace uhd;
+using uhd::usrp::mboard_eeprom_t;
+
+mboard_eeprom_t usrp1_impl::get_mb_eeprom(uhd::i2c_iface::sptr iface)
+{
+ mboard_eeprom_t mb_eeprom;
+
+ //extract the serial
+ mb_eeprom["serial"] = uhd::bytes_to_string(iface->read_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, serial), USRP1_SERIAL_LEN
+ ));
+
+ //extract the name
+ mb_eeprom["name"] = uhd::bytes_to_string(iface->read_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, name), NAME_MAX_LEN
+ ));
+
+ //extract master clock rate as a 32-bit uint in Hz
+ uint32_t master_clock_rate;
+ const byte_vector_t rate_bytes = iface->read_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, mcr), sizeof(master_clock_rate)
+ );
+ std::copy(
+ rate_bytes.begin(), rate_bytes.end(), //input
+ reinterpret_cast<uint8_t *>(&master_clock_rate) //output
+ );
+ master_clock_rate = ntohl(master_clock_rate);
+ if (master_clock_rate > 1e6 and master_clock_rate < 1e9){
+ mb_eeprom["mcr"] = std::to_string(master_clock_rate);
+ }
+ else mb_eeprom["mcr"] = "";
+
+ return mb_eeprom;
+}
+
+void usrp1_impl::set_mb_eeprom(const mboard_eeprom_t &mb_eeprom)
+{
+ auto &iface = _fx2_ctrl;
+
+ //store the serial
+ if (mb_eeprom.has_key("serial")) iface->write_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, serial),
+ string_to_bytes(mb_eeprom["serial"], USRP1_SERIAL_LEN)
+ );
+
+ //store the name
+ if (mb_eeprom.has_key("name")) iface->write_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, name),
+ string_to_bytes(mb_eeprom["name"], NAME_MAX_LEN)
+ );
+
+ //store the master clock rate as a 32-bit uint in Hz
+ if (mb_eeprom.has_key("mcr")){
+ uint32_t master_clock_rate = uint32_t(std::stod(mb_eeprom["mcr"]));
+ master_clock_rate = htonl(master_clock_rate);
+ const byte_vector_t rate_bytes(
+ reinterpret_cast<const uint8_t *>(&master_clock_rate),
+ reinterpret_cast<const uint8_t *>(&master_clock_rate)
+ + sizeof(master_clock_rate)
+ );
+ iface->write_eeprom(
+ USRP1_EEPROM_ADDR, offsetof(usrp1_eeprom_map, mcr), rate_bytes
+ );
+ }
+}
+