From 38eefb2eeaa9c5d83da8abcf5b7efa8eba5fe413 Mon Sep 17 00:00:00 2001
From: Josh Blum <josh@joshknows.com>
Date: Fri, 1 Jul 2011 14:03:04 -0700
Subject: usrp: moved fx2 stuff into common folder

---
 host/lib/usrp/CMakeLists.txt        |   2 +-
 host/lib/usrp/b100/b100_impl.hpp    |   2 +-
 host/lib/usrp/common/CMakeLists.txt |  29 +++
 host/lib/usrp/common/fx2_ctrl.cpp   | 462 ++++++++++++++++++++++++++++++++++++
 host/lib/usrp/common/fx2_ctrl.hpp   | 123 ++++++++++
 host/lib/usrp/fx2/CMakeLists.txt    |  28 ---
 host/lib/usrp/fx2/fx2_ctrl.cpp      | 462 ------------------------------------
 host/lib/usrp/fx2/fx2_ctrl.hpp      | 123 ----------
 host/lib/usrp/usrp1/usrp1_iface.hpp |   2 +-
 host/lib/usrp/usrp2/io_impl.cpp     |  17 +-
 10 files changed, 626 insertions(+), 624 deletions(-)
 create mode 100644 host/lib/usrp/common/CMakeLists.txt
 create mode 100644 host/lib/usrp/common/fx2_ctrl.cpp
 create mode 100644 host/lib/usrp/common/fx2_ctrl.hpp
 delete mode 100644 host/lib/usrp/fx2/CMakeLists.txt
 delete mode 100644 host/lib/usrp/fx2/fx2_ctrl.cpp
 delete mode 100644 host/lib/usrp/fx2/fx2_ctrl.hpp

diff --git a/host/lib/usrp/CMakeLists.txt b/host/lib/usrp/CMakeLists.txt
index 77b13cb15..8ae379f73 100644
--- a/host/lib/usrp/CMakeLists.txt
+++ b/host/lib/usrp/CMakeLists.txt
@@ -32,7 +32,7 @@ LIBUHD_APPEND_SOURCES(
 
 INCLUDE_SUBDIRECTORY(cores)
 INCLUDE_SUBDIRECTORY(dboard)
-INCLUDE_SUBDIRECTORY(fx2)
+INCLUDE_SUBDIRECTORY(common)
 INCLUDE_SUBDIRECTORY(usrp1)
 INCLUDE_SUBDIRECTORY(usrp2)
 INCLUDE_SUBDIRECTORY(b100)
diff --git a/host/lib/usrp/b100/b100_impl.hpp b/host/lib/usrp/b100/b100_impl.hpp
index 4f5017c97..904c846e0 100644
--- a/host/lib/usrp/b100/b100_impl.hpp
+++ b/host/lib/usrp/b100/b100_impl.hpp
@@ -18,7 +18,7 @@
 #ifndef INCLUDED_B100_IMPL_HPP
 #define INCLUDED_B100_IMPL_HPP
 
-#include "../fx2/fx2_ctrl.hpp"
+#include "fx2_ctrl.hpp"
 #include "b100_ctrl.hpp"
 #include "clock_ctrl.hpp"
 #include "codec_ctrl.hpp"
diff --git a/host/lib/usrp/common/CMakeLists.txt b/host/lib/usrp/common/CMakeLists.txt
new file mode 100644
index 000000000..33f2c0ca1
--- /dev/null
+++ b/host/lib/usrp/common/CMakeLists.txt
@@ -0,0 +1,29 @@
+#
+# Copyright 2011 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/>.
+#
+
+########################################################################
+# This file included, use CMake directory variables
+########################################################################
+IF(ENABLE_USB)
+    INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/../firmware/fx2/common)
+    INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
+
+    LIBUHD_APPEND_SOURCES(
+        ${CMAKE_CURRENT_SOURCE_DIR}/fx2_ctrl.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/fx2_ctrl.hpp
+    )
+ENDIF(ENABLE_USB)
diff --git a/host/lib/usrp/common/fx2_ctrl.cpp b/host/lib/usrp/common/fx2_ctrl.cpp
new file mode 100644
index 000000000..66a03f131
--- /dev/null
+++ b/host/lib/usrp/common/fx2_ctrl.cpp
@@ -0,0 +1,462 @@
+//
+// Copyright 2010-2011 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 "fx2_ctrl.hpp"
+#include "usrp_commands.h"
+#include <uhd/utils/msg.hpp>
+#include <uhd/exception.hpp>
+#include <uhd/transport/usb_control.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/thread/thread.hpp>
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <cstring>
+
+using namespace uhd;
+using namespace uhd::usrp;
+
+#define FX2_FIRMWARE_LOAD 0xa0
+
+static const bool load_img_msg = true;
+
+/***********************************************************************
+ * Helper Functions
+ **********************************************************************/
+/*!
+ * Create a file hash
+ * The hash will be used to identify the loaded firmware and fpga image
+ * \param filename file used to generate hash value
+ * \return hash value in a size_t type
+ */
+static size_t generate_hash(const char *filename)
+{
+    std::ifstream file(filename);
+    if (not file){
+        throw uhd::io_error(std::string("cannot open input file ") + filename);
+    }
+
+    size_t hash = 0;
+
+    char ch;
+    while (file.get(ch)) {
+        boost::hash_combine(hash, ch);
+    }
+
+    if (not file.eof()){
+        throw uhd::io_error(std::string("file error ") + filename);
+    }
+
+    file.close();
+    return hash;
+}
+
+
+/*!
+ * Verify checksum of a Intel HEX record
+ * \param record a line from an Intel HEX file
+ * \return true if record is valid, false otherwise
+ */
+static bool checksum(std::string *record)
+{
+
+    size_t len = record->length();
+    unsigned int i;
+    unsigned char sum = 0;
+    unsigned int val;
+
+    for (i = 1; i < len; i += 2) {
+        std::istringstream(record->substr(i, 2)) >> std::hex >> val;
+        sum += val;
+    }
+
+    if (sum == 0)
+       return true;
+    else
+       return false;
+}
+
+
+/*!
+ * Parse Intel HEX record
+ *
+ * \param record a line from an Intel HEX file
+ * \param len output length of record
+ * \param addr output address
+ * \param type output type
+ * \param data output data
+ * \return true if record is sucessfully read, false on error
+ */
+bool parse_record(std::string *record, unsigned int &len,
+                  unsigned int &addr, unsigned int &type,
+                  unsigned char* data)
+{
+    unsigned int i;
+    std::string _data;
+    unsigned int val;
+
+    if (record->substr(0, 1) != ":")
+        return false;
+
+    std::istringstream(record->substr(1, 2)) >> std::hex >> len;
+    std::istringstream(record->substr(3, 4)) >> std::hex >> addr;
+    std::istringstream(record->substr(7, 2)) >> std::hex >> type;
+
+    for (i = 0; i < len; i++) {
+        std::istringstream(record->substr(9 + 2 * i, 2)) >> std::hex >> val;
+        data[i] = (unsigned char) val;
+    }
+
+    return true;
+}
+
+
+/*!
+ * USRP control implementation for device discovery and configuration
+ */
+class fx2_ctrl_impl : public fx2_ctrl {
+public:
+    fx2_ctrl_impl(uhd::transport::usb_control::sptr ctrl_transport)
+    {
+        _ctrl_transport = ctrl_transport;
+    }
+
+    void usrp_load_firmware(std::string filestring, bool force)
+    {
+        const char *filename = filestring.c_str();
+
+        size_t hash = generate_hash(filename);
+
+        size_t loaded_hash; usrp_get_firmware_hash(loaded_hash);
+
+        if (not force and (hash == loaded_hash)) return;
+
+        //FIXME: verify types
+        unsigned int len;
+        unsigned int addr;
+        unsigned int type;
+        unsigned char data[512];
+
+        std::ifstream file;
+        file.open(filename, std::ifstream::in);
+
+        if (!file.good()) {
+            throw uhd::io_error("usrp_load_firmware: cannot open firmware input file");
+        }
+
+        unsigned char reset_y = 1;
+        unsigned char reset_n = 0;
+
+        //hit the reset line
+        if (load_img_msg) UHD_MSG(status) << "Loading firmware image: " << filestring << "..." << std::flush;
+        usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_y, 1);
+
+        while (!file.eof()) {
+           std::string record;
+           file >> record;
+
+            //check for valid record
+            if (not checksum(&record) or not parse_record(&record, len, addr, type, data)) {
+                throw uhd::io_error("usrp_load_firmware: bad record checksum");
+            }
+
+            //type 0x00 is data
+            if (type == 0x00) {
+                int ret = usrp_control_write(FX2_FIRMWARE_LOAD, addr, 0, data, len);
+                if (ret < 0) throw uhd::io_error("usrp_load_firmware: usrp_control_write failed");
+            }
+            //type 0x01 is end
+            else if (type == 0x01) {
+                usrp_set_firmware_hash(hash); //set hash before reset
+                usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_n, 1);
+                file.close();
+
+                //wait for things to settle
+                boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
+                if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
+                return;
+            }
+            //type anything else is unhandled
+            else {
+                throw uhd::io_error("usrp_load_firmware: unsupported record");
+            }
+        }
+
+        //file did not end
+        throw uhd::io_error("usrp_load_firmware: bad record");
+    }
+
+    void usrp_init(void){
+        //disable
+        usrp_rx_enable(false);
+        usrp_tx_enable(false);
+
+        //toggle resets
+        usrp_rx_reset(true);
+        usrp_tx_reset(true);
+        usrp_rx_reset(false);
+        usrp_tx_reset(false);
+    }
+
+    void usrp_load_fpga(std::string filestring)
+    {
+        const char *filename = filestring.c_str();
+
+        size_t hash = generate_hash(filename);
+
+        size_t loaded_hash; usrp_get_fpga_hash(loaded_hash);
+
+        if (hash == loaded_hash) return;
+        const int ep0_size = 64;
+        unsigned char buf[ep0_size];
+
+        if (load_img_msg) UHD_MSG(status) << "Loading FPGA image: " << filestring << "..." << std::flush;
+        std::ifstream file;
+        file.open(filename, std::ios::in | std::ios::binary);
+        if (not file.good()) {
+            throw uhd::io_error("usrp_load_fpga: cannot open fpga input file");
+        }
+
+        usrp_fpga_reset(true); //holding the fpga in reset while loading
+
+        if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_BEGIN) < 0) {
+            throw uhd::io_error("usrp_load_fpga: fpga load error");
+        }
+
+        while (not file.eof()) {
+            file.read((char *)buf, sizeof(buf));
+            size_t n = file.gcount();
+            if(n == 0) continue;
+            int ret = usrp_control_write(VRQ_FPGA_LOAD, 0, FL_XFER, buf, n);
+            if (ret < 0 or size_t(ret) != n) {
+                throw uhd::io_error("usrp_load_fpga: fpga load error");
+            }
+        }
+
+        if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_END) < 0) {
+            throw uhd::io_error("usrp_load_fpga: fpga load error");
+        }
+
+        usrp_set_fpga_hash(hash);
+
+        usrp_fpga_reset(false); //done loading, take fpga out of reset
+
+        file.close();
+        if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
+    }
+
+    void usrp_load_eeprom(std::string filestring)
+    {
+        if (load_img_msg) UHD_MSG(status) << "Loading EEPROM image: " << filestring << "..." << std::flush;
+        const char *filename = filestring.c_str();
+        const boost::uint16_t i2c_addr = 0x50;
+
+        //FIXME: verify types
+        int len;
+        unsigned int addr;
+        unsigned char data[256];
+        unsigned char sendbuf[17];
+
+        std::ifstream file;
+        file.open(filename, std::ifstream::in);
+
+        if (not file.good()) {
+            throw uhd::io_error("usrp_load_eeprom: cannot open EEPROM input file");
+        }
+
+        file.read((char *)data, 256);
+        len = file.gcount();
+
+        if(len == 256) {
+            throw uhd::io_error("usrp_load_eeprom: image size too large");
+        }
+
+        const int pagesize = 16;
+        addr = 0;
+        while(len > 0) {
+            sendbuf[0] = addr;
+            memcpy(sendbuf+1, &data[addr], len > pagesize ? pagesize : len);
+            int ret = usrp_i2c_write(i2c_addr, sendbuf, (len > pagesize ? pagesize : len)+1);
+            if (ret < 0) {
+                throw uhd::io_error("usrp_load_eeprom: usrp_i2c_write failed");
+            }
+            addr += pagesize;
+            len -= pagesize;
+            boost::this_thread::sleep(boost::posix_time::milliseconds(100));
+        }
+        file.close();
+        if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
+    }
+
+
+    void usrp_set_led(int led_num, bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_SET_LED, on, led_num) >= 0);
+    }
+
+
+    void usrp_get_firmware_hash(size_t &hash)
+    {
+        UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_0_ADDR, 0,
+                                 (unsigned char*) &hash, sizeof(size_t)) >= 0);
+    }
+
+
+    void usrp_set_firmware_hash(size_t hash)
+    {
+        UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_0_ADDR, 0,
+                                  (unsigned char*) &hash, sizeof(size_t)) >= 0);
+
+    }
+
+
+    void usrp_get_fpga_hash(size_t &hash)
+    {
+        UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_1_ADDR, 0,
+                                 (unsigned char*) &hash, sizeof(size_t)) >= 0);
+    }
+
+
+    void usrp_set_fpga_hash(size_t hash)
+    {
+        UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_1_ADDR, 0,
+                                  (unsigned char*) &hash, sizeof(size_t)) >= 0);
+    }
+
+    void usrp_tx_enable(bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_ENABLE, on, 0) >= 0);
+    }
+
+
+    void usrp_rx_enable(bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_ENABLE, on, 0) >= 0);
+    }
+
+
+    void usrp_tx_reset(bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_RESET, on, 0) >= 0);
+    }
+
+
+    void usrp_rx_reset(bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_RESET, on, 0) >= 0);
+    }
+
+    void usrp_fpga_reset(bool on)
+    {
+        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RESET, on, 0) >= 0);
+    }
+
+    int usrp_control_write(boost::uint8_t request,
+                           boost::uint16_t value,
+                           boost::uint16_t index,
+                           unsigned char *buff,
+                           boost::uint16_t length)
+    {
+        return _ctrl_transport->submit(VRT_VENDOR_OUT,     // bmReqeustType
+                                       request,            // bRequest
+                                       value,              // wValue
+                                       index,              // wIndex
+                                       buff,               // data
+                                       length);            // wLength
+    }
+
+
+    int usrp_control_read(boost::uint8_t request,
+                          boost::uint16_t value,
+                          boost::uint16_t index,
+                          unsigned char *buff,
+                          boost::uint16_t length)
+    {
+        return _ctrl_transport->submit(VRT_VENDOR_IN,      // bmReqeustType
+                                       request,            // bRequest
+                                       value,              // wValue
+                                       index,              // wIndex
+                                       buff,               // data
+                                       length);            // wLength
+    }
+
+
+    int usrp_control_write_cmd(boost::uint8_t request, boost::uint16_t value, boost::uint16_t index)
+    {
+        return usrp_control_write(request, value, index, 0, 0);
+    }
+
+    int usrp_i2c_write(boost::uint16_t i2c_addr, unsigned char *buf, boost::uint16_t len)
+    {
+        return usrp_control_write(VRQ_I2C_WRITE, i2c_addr, 0, buf, len);
+    }
+
+    int usrp_i2c_read(boost::uint16_t i2c_addr, unsigned char *buf, boost::uint16_t len)
+    {
+        return usrp_control_read(VRQ_I2C_READ, i2c_addr, 0, buf, len);
+    }
+
+    static const bool iface_debug = false;
+    static const size_t max_i2c_data_bytes = 64;
+
+    void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes)
+    {
+        UHD_ASSERT_THROW(bytes.size() < max_i2c_data_bytes);
+
+        unsigned char buff[max_i2c_data_bytes];
+        std::copy(bytes.begin(), bytes.end(), buff);
+
+        int ret = this->usrp_i2c_write(addr & 0xff,
+                                             buff,
+                                             bytes.size());
+
+        if (iface_debug && (ret < 0))
+            uhd::runtime_error("USRP: failed i2c write");
+    }
+
+    byte_vector_t read_i2c(boost::uint8_t addr, size_t num_bytes)
+    {
+      UHD_ASSERT_THROW(num_bytes < max_i2c_data_bytes);
+
+      unsigned char buff[max_i2c_data_bytes];
+      int ret = this->usrp_i2c_read(addr & 0xff,
+                                            buff,
+                                            num_bytes);
+
+      if (iface_debug && ((ret < 0) || (unsigned)ret < (num_bytes)))
+          uhd::runtime_error("USRP: failed i2c read");
+
+      byte_vector_t out_bytes;
+      for (size_t i = 0; i < num_bytes; i++)
+          out_bytes.push_back(buff[i]);
+
+      return out_bytes;
+    }
+
+
+private:
+    uhd::transport::usb_control::sptr _ctrl_transport;
+};
+
+/***********************************************************************
+ * Public make function for fx2_ctrl interface
+ **********************************************************************/
+fx2_ctrl::sptr fx2_ctrl::make(uhd::transport::usb_control::sptr ctrl_transport){
+    return sptr(new fx2_ctrl_impl(ctrl_transport));
+}
+
diff --git a/host/lib/usrp/common/fx2_ctrl.hpp b/host/lib/usrp/common/fx2_ctrl.hpp
new file mode 100644
index 000000000..eeff6287d
--- /dev/null
+++ b/host/lib/usrp/common/fx2_ctrl.hpp
@@ -0,0 +1,123 @@
+//
+// Copyright 2010-2011 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/>.
+//
+
+#ifndef INCLUDED_USRP_CTRL_HPP
+#define INCLUDED_USRP_CTRL_HPP
+
+#include <uhd/transport/usb_control.hpp>
+#include <uhd/types/serial.hpp> //i2c iface
+#include <boost/shared_ptr.hpp>
+#include <boost/utility.hpp>
+
+namespace uhd{ namespace usrp{
+
+class fx2_ctrl : boost::noncopyable, public uhd::i2c_iface{
+public:
+    typedef boost::shared_ptr<fx2_ctrl> sptr;
+
+    /*!
+     * Make a usrp control object from a control transport
+     * \param ctrl_transport a USB control transport
+     * \return a new usrp control object
+     */
+    static sptr make(uhd::transport::usb_control::sptr ctrl_transport);
+
+    //! Call init after the fpga is loaded
+    virtual void usrp_init(void) = 0;
+
+    /*!
+     * Load firmware in Intel HEX Format onto device 
+     * \param filename name of firmware file
+     * \param force reload firmware if already loaded
+     */
+    virtual void usrp_load_firmware(std::string filename,
+                                   bool force = false) = 0;
+
+    /*!
+     * Load fpga file onto usrp 
+     * \param filename name of fpga image 
+     */
+    virtual void usrp_load_fpga(std::string filename) = 0;
+
+    /*!
+     * Load USB descriptor file in Intel HEX format into EEPROM
+     * \param filename name of EEPROM image
+     */
+    virtual void usrp_load_eeprom(std::string filestring) = 0;
+    
+    /*!
+     * Submit an IN transfer 
+     * \param request device specific request 
+     * \param value device specific field
+     * \param index device specific field
+     * \param buff buffer to place data
+     * \return number of bytes read or error 
+     */
+    virtual int usrp_control_read(boost::uint8_t request,
+                                  boost::uint16_t value,
+                                  boost::uint16_t index,
+                                  unsigned char *buff,
+                                  boost::uint16_t length) = 0;
+
+    /*!
+     * Submit an OUT transfer 
+     * \param request device specific request 
+     * \param value device specific field
+     * \param index device specific field
+     * \param buff buffer of data to be sent 
+     * \return number of bytes written or error 
+     */
+    virtual int usrp_control_write(boost::uint8_t request,
+                                   boost::uint16_t value,
+                                   boost::uint16_t index,
+                                   unsigned char *buff,
+                                   boost::uint16_t length) = 0;
+
+    /*!
+     * Perform an I2C write
+     * \param i2c_addr I2C device address
+     * \param buf data to be written 
+     * \param len length of data in bytes
+     * \return number of bytes written or error 
+     */
+
+    virtual int usrp_i2c_write(boost::uint16_t i2c_addr,
+                               unsigned char *buf, 
+                               boost::uint16_t len) = 0;
+
+    /*!
+     * Perform an I2C read
+     * \param i2c_addr I2C device address
+     * \param buf data to be read 
+     * \param len length of data in bytes
+     * \return number of bytes read or error 
+     */
+
+    virtual int usrp_i2c_read(boost::uint16_t i2c_addr,
+                               unsigned char *buf, 
+                               boost::uint16_t len) = 0;
+
+    //! enable/disable the rx path
+    virtual void usrp_rx_enable(bool on) = 0;
+
+    //! enable/disable the tx path
+    virtual void usrp_tx_enable(bool on) = 0;
+};
+
+}} //namespace uhd::usrp
+
+#endif /* INCLUDED_USRP_CTRL_HPP */
diff --git a/host/lib/usrp/fx2/CMakeLists.txt b/host/lib/usrp/fx2/CMakeLists.txt
deleted file mode 100644
index 1bb5ad683..000000000
--- a/host/lib/usrp/fx2/CMakeLists.txt
+++ /dev/null
@@ -1,28 +0,0 @@
-#
-# Copyright 2011 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/>.
-#
-
-########################################################################
-# This file included, use CMake directory variables
-########################################################################
-IF(ENABLE_USB)
-    INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/../firmware/fx2/common)
-
-    LIBUHD_APPEND_SOURCES(
-        ${CMAKE_CURRENT_SOURCE_DIR}/fx2_ctrl.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/fx2_ctrl.hpp
-    )
-ENDIF(ENABLE_USB)
diff --git a/host/lib/usrp/fx2/fx2_ctrl.cpp b/host/lib/usrp/fx2/fx2_ctrl.cpp
deleted file mode 100644
index 66a03f131..000000000
--- a/host/lib/usrp/fx2/fx2_ctrl.cpp
+++ /dev/null
@@ -1,462 +0,0 @@
-//
-// Copyright 2010-2011 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 "fx2_ctrl.hpp"
-#include "usrp_commands.h"
-#include <uhd/utils/msg.hpp>
-#include <uhd/exception.hpp>
-#include <uhd/transport/usb_control.hpp>
-#include <boost/functional/hash.hpp>
-#include <boost/thread/thread.hpp>
-#include <fstream>
-#include <sstream>
-#include <string>
-#include <vector>
-#include <cstring>
-
-using namespace uhd;
-using namespace uhd::usrp;
-
-#define FX2_FIRMWARE_LOAD 0xa0
-
-static const bool load_img_msg = true;
-
-/***********************************************************************
- * Helper Functions
- **********************************************************************/
-/*!
- * Create a file hash
- * The hash will be used to identify the loaded firmware and fpga image
- * \param filename file used to generate hash value
- * \return hash value in a size_t type
- */
-static size_t generate_hash(const char *filename)
-{
-    std::ifstream file(filename);
-    if (not file){
-        throw uhd::io_error(std::string("cannot open input file ") + filename);
-    }
-
-    size_t hash = 0;
-
-    char ch;
-    while (file.get(ch)) {
-        boost::hash_combine(hash, ch);
-    }
-
-    if (not file.eof()){
-        throw uhd::io_error(std::string("file error ") + filename);
-    }
-
-    file.close();
-    return hash;
-}
-
-
-/*!
- * Verify checksum of a Intel HEX record
- * \param record a line from an Intel HEX file
- * \return true if record is valid, false otherwise
- */
-static bool checksum(std::string *record)
-{
-
-    size_t len = record->length();
-    unsigned int i;
-    unsigned char sum = 0;
-    unsigned int val;
-
-    for (i = 1; i < len; i += 2) {
-        std::istringstream(record->substr(i, 2)) >> std::hex >> val;
-        sum += val;
-    }
-
-    if (sum == 0)
-       return true;
-    else
-       return false;
-}
-
-
-/*!
- * Parse Intel HEX record
- *
- * \param record a line from an Intel HEX file
- * \param len output length of record
- * \param addr output address
- * \param type output type
- * \param data output data
- * \return true if record is sucessfully read, false on error
- */
-bool parse_record(std::string *record, unsigned int &len,
-                  unsigned int &addr, unsigned int &type,
-                  unsigned char* data)
-{
-    unsigned int i;
-    std::string _data;
-    unsigned int val;
-
-    if (record->substr(0, 1) != ":")
-        return false;
-
-    std::istringstream(record->substr(1, 2)) >> std::hex >> len;
-    std::istringstream(record->substr(3, 4)) >> std::hex >> addr;
-    std::istringstream(record->substr(7, 2)) >> std::hex >> type;
-
-    for (i = 0; i < len; i++) {
-        std::istringstream(record->substr(9 + 2 * i, 2)) >> std::hex >> val;
-        data[i] = (unsigned char) val;
-    }
-
-    return true;
-}
-
-
-/*!
- * USRP control implementation for device discovery and configuration
- */
-class fx2_ctrl_impl : public fx2_ctrl {
-public:
-    fx2_ctrl_impl(uhd::transport::usb_control::sptr ctrl_transport)
-    {
-        _ctrl_transport = ctrl_transport;
-    }
-
-    void usrp_load_firmware(std::string filestring, bool force)
-    {
-        const char *filename = filestring.c_str();
-
-        size_t hash = generate_hash(filename);
-
-        size_t loaded_hash; usrp_get_firmware_hash(loaded_hash);
-
-        if (not force and (hash == loaded_hash)) return;
-
-        //FIXME: verify types
-        unsigned int len;
-        unsigned int addr;
-        unsigned int type;
-        unsigned char data[512];
-
-        std::ifstream file;
-        file.open(filename, std::ifstream::in);
-
-        if (!file.good()) {
-            throw uhd::io_error("usrp_load_firmware: cannot open firmware input file");
-        }
-
-        unsigned char reset_y = 1;
-        unsigned char reset_n = 0;
-
-        //hit the reset line
-        if (load_img_msg) UHD_MSG(status) << "Loading firmware image: " << filestring << "..." << std::flush;
-        usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_y, 1);
-
-        while (!file.eof()) {
-           std::string record;
-           file >> record;
-
-            //check for valid record
-            if (not checksum(&record) or not parse_record(&record, len, addr, type, data)) {
-                throw uhd::io_error("usrp_load_firmware: bad record checksum");
-            }
-
-            //type 0x00 is data
-            if (type == 0x00) {
-                int ret = usrp_control_write(FX2_FIRMWARE_LOAD, addr, 0, data, len);
-                if (ret < 0) throw uhd::io_error("usrp_load_firmware: usrp_control_write failed");
-            }
-            //type 0x01 is end
-            else if (type == 0x01) {
-                usrp_set_firmware_hash(hash); //set hash before reset
-                usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_n, 1);
-                file.close();
-
-                //wait for things to settle
-                boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
-                if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
-                return;
-            }
-            //type anything else is unhandled
-            else {
-                throw uhd::io_error("usrp_load_firmware: unsupported record");
-            }
-        }
-
-        //file did not end
-        throw uhd::io_error("usrp_load_firmware: bad record");
-    }
-
-    void usrp_init(void){
-        //disable
-        usrp_rx_enable(false);
-        usrp_tx_enable(false);
-
-        //toggle resets
-        usrp_rx_reset(true);
-        usrp_tx_reset(true);
-        usrp_rx_reset(false);
-        usrp_tx_reset(false);
-    }
-
-    void usrp_load_fpga(std::string filestring)
-    {
-        const char *filename = filestring.c_str();
-
-        size_t hash = generate_hash(filename);
-
-        size_t loaded_hash; usrp_get_fpga_hash(loaded_hash);
-
-        if (hash == loaded_hash) return;
-        const int ep0_size = 64;
-        unsigned char buf[ep0_size];
-
-        if (load_img_msg) UHD_MSG(status) << "Loading FPGA image: " << filestring << "..." << std::flush;
-        std::ifstream file;
-        file.open(filename, std::ios::in | std::ios::binary);
-        if (not file.good()) {
-            throw uhd::io_error("usrp_load_fpga: cannot open fpga input file");
-        }
-
-        usrp_fpga_reset(true); //holding the fpga in reset while loading
-
-        if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_BEGIN) < 0) {
-            throw uhd::io_error("usrp_load_fpga: fpga load error");
-        }
-
-        while (not file.eof()) {
-            file.read((char *)buf, sizeof(buf));
-            size_t n = file.gcount();
-            if(n == 0) continue;
-            int ret = usrp_control_write(VRQ_FPGA_LOAD, 0, FL_XFER, buf, n);
-            if (ret < 0 or size_t(ret) != n) {
-                throw uhd::io_error("usrp_load_fpga: fpga load error");
-            }
-        }
-
-        if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_END) < 0) {
-            throw uhd::io_error("usrp_load_fpga: fpga load error");
-        }
-
-        usrp_set_fpga_hash(hash);
-
-        usrp_fpga_reset(false); //done loading, take fpga out of reset
-
-        file.close();
-        if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
-    }
-
-    void usrp_load_eeprom(std::string filestring)
-    {
-        if (load_img_msg) UHD_MSG(status) << "Loading EEPROM image: " << filestring << "..." << std::flush;
-        const char *filename = filestring.c_str();
-        const boost::uint16_t i2c_addr = 0x50;
-
-        //FIXME: verify types
-        int len;
-        unsigned int addr;
-        unsigned char data[256];
-        unsigned char sendbuf[17];
-
-        std::ifstream file;
-        file.open(filename, std::ifstream::in);
-
-        if (not file.good()) {
-            throw uhd::io_error("usrp_load_eeprom: cannot open EEPROM input file");
-        }
-
-        file.read((char *)data, 256);
-        len = file.gcount();
-
-        if(len == 256) {
-            throw uhd::io_error("usrp_load_eeprom: image size too large");
-        }
-
-        const int pagesize = 16;
-        addr = 0;
-        while(len > 0) {
-            sendbuf[0] = addr;
-            memcpy(sendbuf+1, &data[addr], len > pagesize ? pagesize : len);
-            int ret = usrp_i2c_write(i2c_addr, sendbuf, (len > pagesize ? pagesize : len)+1);
-            if (ret < 0) {
-                throw uhd::io_error("usrp_load_eeprom: usrp_i2c_write failed");
-            }
-            addr += pagesize;
-            len -= pagesize;
-            boost::this_thread::sleep(boost::posix_time::milliseconds(100));
-        }
-        file.close();
-        if (load_img_msg) UHD_MSG(status) << " done" << std::endl;
-    }
-
-
-    void usrp_set_led(int led_num, bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_SET_LED, on, led_num) >= 0);
-    }
-
-
-    void usrp_get_firmware_hash(size_t &hash)
-    {
-        UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_0_ADDR, 0,
-                                 (unsigned char*) &hash, sizeof(size_t)) >= 0);
-    }
-
-
-    void usrp_set_firmware_hash(size_t hash)
-    {
-        UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_0_ADDR, 0,
-                                  (unsigned char*) &hash, sizeof(size_t)) >= 0);
-
-    }
-
-
-    void usrp_get_fpga_hash(size_t &hash)
-    {
-        UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_1_ADDR, 0,
-                                 (unsigned char*) &hash, sizeof(size_t)) >= 0);
-    }
-
-
-    void usrp_set_fpga_hash(size_t hash)
-    {
-        UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_1_ADDR, 0,
-                                  (unsigned char*) &hash, sizeof(size_t)) >= 0);
-    }
-
-    void usrp_tx_enable(bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_ENABLE, on, 0) >= 0);
-    }
-
-
-    void usrp_rx_enable(bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_ENABLE, on, 0) >= 0);
-    }
-
-
-    void usrp_tx_reset(bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_RESET, on, 0) >= 0);
-    }
-
-
-    void usrp_rx_reset(bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_RESET, on, 0) >= 0);
-    }
-
-    void usrp_fpga_reset(bool on)
-    {
-        UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RESET, on, 0) >= 0);
-    }
-
-    int usrp_control_write(boost::uint8_t request,
-                           boost::uint16_t value,
-                           boost::uint16_t index,
-                           unsigned char *buff,
-                           boost::uint16_t length)
-    {
-        return _ctrl_transport->submit(VRT_VENDOR_OUT,     // bmReqeustType
-                                       request,            // bRequest
-                                       value,              // wValue
-                                       index,              // wIndex
-                                       buff,               // data
-                                       length);            // wLength
-    }
-
-
-    int usrp_control_read(boost::uint8_t request,
-                          boost::uint16_t value,
-                          boost::uint16_t index,
-                          unsigned char *buff,
-                          boost::uint16_t length)
-    {
-        return _ctrl_transport->submit(VRT_VENDOR_IN,      // bmReqeustType
-                                       request,            // bRequest
-                                       value,              // wValue
-                                       index,              // wIndex
-                                       buff,               // data
-                                       length);            // wLength
-    }
-
-
-    int usrp_control_write_cmd(boost::uint8_t request, boost::uint16_t value, boost::uint16_t index)
-    {
-        return usrp_control_write(request, value, index, 0, 0);
-    }
-
-    int usrp_i2c_write(boost::uint16_t i2c_addr, unsigned char *buf, boost::uint16_t len)
-    {
-        return usrp_control_write(VRQ_I2C_WRITE, i2c_addr, 0, buf, len);
-    }
-
-    int usrp_i2c_read(boost::uint16_t i2c_addr, unsigned char *buf, boost::uint16_t len)
-    {
-        return usrp_control_read(VRQ_I2C_READ, i2c_addr, 0, buf, len);
-    }
-
-    static const bool iface_debug = false;
-    static const size_t max_i2c_data_bytes = 64;
-
-    void write_i2c(boost::uint8_t addr, const byte_vector_t &bytes)
-    {
-        UHD_ASSERT_THROW(bytes.size() < max_i2c_data_bytes);
-
-        unsigned char buff[max_i2c_data_bytes];
-        std::copy(bytes.begin(), bytes.end(), buff);
-
-        int ret = this->usrp_i2c_write(addr & 0xff,
-                                             buff,
-                                             bytes.size());
-
-        if (iface_debug && (ret < 0))
-            uhd::runtime_error("USRP: failed i2c write");
-    }
-
-    byte_vector_t read_i2c(boost::uint8_t addr, size_t num_bytes)
-    {
-      UHD_ASSERT_THROW(num_bytes < max_i2c_data_bytes);
-
-      unsigned char buff[max_i2c_data_bytes];
-      int ret = this->usrp_i2c_read(addr & 0xff,
-                                            buff,
-                                            num_bytes);
-
-      if (iface_debug && ((ret < 0) || (unsigned)ret < (num_bytes)))
-          uhd::runtime_error("USRP: failed i2c read");
-
-      byte_vector_t out_bytes;
-      for (size_t i = 0; i < num_bytes; i++)
-          out_bytes.push_back(buff[i]);
-
-      return out_bytes;
-    }
-
-
-private:
-    uhd::transport::usb_control::sptr _ctrl_transport;
-};
-
-/***********************************************************************
- * Public make function for fx2_ctrl interface
- **********************************************************************/
-fx2_ctrl::sptr fx2_ctrl::make(uhd::transport::usb_control::sptr ctrl_transport){
-    return sptr(new fx2_ctrl_impl(ctrl_transport));
-}
-
diff --git a/host/lib/usrp/fx2/fx2_ctrl.hpp b/host/lib/usrp/fx2/fx2_ctrl.hpp
deleted file mode 100644
index eeff6287d..000000000
--- a/host/lib/usrp/fx2/fx2_ctrl.hpp
+++ /dev/null
@@ -1,123 +0,0 @@
-//
-// Copyright 2010-2011 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/>.
-//
-
-#ifndef INCLUDED_USRP_CTRL_HPP
-#define INCLUDED_USRP_CTRL_HPP
-
-#include <uhd/transport/usb_control.hpp>
-#include <uhd/types/serial.hpp> //i2c iface
-#include <boost/shared_ptr.hpp>
-#include <boost/utility.hpp>
-
-namespace uhd{ namespace usrp{
-
-class fx2_ctrl : boost::noncopyable, public uhd::i2c_iface{
-public:
-    typedef boost::shared_ptr<fx2_ctrl> sptr;
-
-    /*!
-     * Make a usrp control object from a control transport
-     * \param ctrl_transport a USB control transport
-     * \return a new usrp control object
-     */
-    static sptr make(uhd::transport::usb_control::sptr ctrl_transport);
-
-    //! Call init after the fpga is loaded
-    virtual void usrp_init(void) = 0;
-
-    /*!
-     * Load firmware in Intel HEX Format onto device 
-     * \param filename name of firmware file
-     * \param force reload firmware if already loaded
-     */
-    virtual void usrp_load_firmware(std::string filename,
-                                   bool force = false) = 0;
-
-    /*!
-     * Load fpga file onto usrp 
-     * \param filename name of fpga image 
-     */
-    virtual void usrp_load_fpga(std::string filename) = 0;
-
-    /*!
-     * Load USB descriptor file in Intel HEX format into EEPROM
-     * \param filename name of EEPROM image
-     */
-    virtual void usrp_load_eeprom(std::string filestring) = 0;
-    
-    /*!
-     * Submit an IN transfer 
-     * \param request device specific request 
-     * \param value device specific field
-     * \param index device specific field
-     * \param buff buffer to place data
-     * \return number of bytes read or error 
-     */
-    virtual int usrp_control_read(boost::uint8_t request,
-                                  boost::uint16_t value,
-                                  boost::uint16_t index,
-                                  unsigned char *buff,
-                                  boost::uint16_t length) = 0;
-
-    /*!
-     * Submit an OUT transfer 
-     * \param request device specific request 
-     * \param value device specific field
-     * \param index device specific field
-     * \param buff buffer of data to be sent 
-     * \return number of bytes written or error 
-     */
-    virtual int usrp_control_write(boost::uint8_t request,
-                                   boost::uint16_t value,
-                                   boost::uint16_t index,
-                                   unsigned char *buff,
-                                   boost::uint16_t length) = 0;
-
-    /*!
-     * Perform an I2C write
-     * \param i2c_addr I2C device address
-     * \param buf data to be written 
-     * \param len length of data in bytes
-     * \return number of bytes written or error 
-     */
-
-    virtual int usrp_i2c_write(boost::uint16_t i2c_addr,
-                               unsigned char *buf, 
-                               boost::uint16_t len) = 0;
-
-    /*!
-     * Perform an I2C read
-     * \param i2c_addr I2C device address
-     * \param buf data to be read 
-     * \param len length of data in bytes
-     * \return number of bytes read or error 
-     */
-
-    virtual int usrp_i2c_read(boost::uint16_t i2c_addr,
-                               unsigned char *buf, 
-                               boost::uint16_t len) = 0;
-
-    //! enable/disable the rx path
-    virtual void usrp_rx_enable(bool on) = 0;
-
-    //! enable/disable the tx path
-    virtual void usrp_tx_enable(bool on) = 0;
-};
-
-}} //namespace uhd::usrp
-
-#endif /* INCLUDED_USRP_CTRL_HPP */
diff --git a/host/lib/usrp/usrp1/usrp1_iface.hpp b/host/lib/usrp/usrp1/usrp1_iface.hpp
index 3da754a87..c1ac34f25 100644
--- a/host/lib/usrp/usrp1/usrp1_iface.hpp
+++ b/host/lib/usrp/usrp1/usrp1_iface.hpp
@@ -18,11 +18,11 @@
 #ifndef INCLUDED_USRP1_IFACE_HPP
 #define INCLUDED_USRP1_IFACE_HPP
 
+#include "fx2_ctrl.hpp"
 #include "wb_iface.hpp"
 #include <uhd/types/serial.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/utility.hpp>
-#include "../fx2/fx2_ctrl.hpp"
 
 /*!
  * The usrp1 interface class:
diff --git a/host/lib/usrp/usrp2/io_impl.cpp b/host/lib/usrp/usrp2/io_impl.cpp
index 64c099ff6..0c583f3ca 100644
--- a/host/lib/usrp/usrp2/io_impl.cpp
+++ b/host/lib/usrp/usrp2/io_impl.cpp
@@ -52,11 +52,6 @@ static UHD_INLINE double from_time_dur(const pt::time_duration &time_dur){
 /***********************************************************************
  * constants
  **********************************************************************/
-static const int underflow_flags = 0
-    | async_metadata_t::EVENT_CODE_UNDERFLOW
-    | async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET
-;
-
 static const size_t vrt_send_header_offset_words32 = 1;
 
 /***********************************************************************
@@ -219,11 +214,17 @@ void usrp2_impl::io_impl::recv_pirate_loop(
                     fc_mon.update_fc_condition(uhd::ntohx(fc_word32));
                     continue;
                 }
-
-                //print the famous U, and push the metadata into the message queue
-                if (metadata.event_code & underflow_flags) UHD_MSG(fastpath) << "U";
                 //else UHD_MSG(often) << "metadata.event_code " << metadata.event_code << std::endl;
                 async_msg_fifo.push_with_pop_on_full(metadata);
+
+                if (metadata.event_code &
+                    ( async_metadata_t::EVENT_CODE_UNDERFLOW
+                    | async_metadata_t::EVENT_CODE_UNDERFLOW_IN_PACKET)
+                ) UHD_MSG(fastpath) << "U";
+                else if (metadata.event_code &
+                    ( async_metadata_t::EVENT_CODE_SEQ_ERROR
+                    | async_metadata_t::EVENT_CODE_SEQ_ERROR_IN_BURST)
+                ) UHD_MSG(fastpath) << "S";
             }
             else{
                 //TODO unknown received packet, may want to print error...
-- 
cgit v1.2.3