From 2cb6092ddfcf5f3881faa455566d4f332b01d0ac Mon Sep 17 00:00:00 2001
From: Josh Blum <josh@joshknows.com>
Date: Mon, 31 Jan 2011 20:26:45 +0000
Subject: usrp-e100: combined clkconfig and fpga downloader into
 usrp-e-utility, simplified code

---
 host/lib/usrp/usrp_e100/fpga_downloader.cpp | 262 ++++++++++++++++++++++++++++
 1 file changed, 262 insertions(+)
 create mode 100644 host/lib/usrp/usrp_e100/fpga_downloader.cpp

(limited to 'host/lib/usrp/usrp_e100/fpga_downloader.cpp')

diff --git a/host/lib/usrp/usrp_e100/fpga_downloader.cpp b/host/lib/usrp/usrp_e100/fpga_downloader.cpp
new file mode 100644
index 000000000..926e6fcaf
--- /dev/null
+++ b/host/lib/usrp/usrp_e100/fpga_downloader.cpp
@@ -0,0 +1,262 @@
+//
+// 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 <uhd/config.hpp>
+#include <uhd/utils/assert.hpp>
+
+#include <iostream>
+#include <sstream>
+#include <fstream>
+#include <string>
+#include <cstdlib>
+#include <stdexcept>
+
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+
+#include <linux/spi/spidev.h>
+
+/*
+ * Configuration connections
+ *
+ * CCK    - MCSPI1_CLK
+ * DIN    - MCSPI1_MOSI
+ * PROG_B - GPIO_175     - output (change mux)
+ * DONE   - GPIO_173     - input  (change mux)
+ * INIT_B - GPIO_114     - input  (change mux)
+ *
+*/
+
+namespace usrp_e_fpga_downloader_utility{
+
+const unsigned int PROG_B = 175;
+const unsigned int DONE   = 173;
+const unsigned int INIT_B = 114;
+
+//static std::string bit_file = "safe_u1e.bin";
+
+const int BUF_SIZE = 4096;
+
+enum gpio_direction {IN, OUT};
+
+class gpio {
+	public:
+
+	gpio(unsigned int gpio_num, gpio_direction pin_direction);
+
+	bool get_value();
+	void set_value(bool state);
+
+	private:
+
+	std::stringstream base_path;
+	std::fstream value_file;	
+};
+
+class spidev {
+	public:
+
+	spidev(std::string dev_name);
+	~spidev();
+
+	void send(char *wbuf, char *rbuf, unsigned int nbytes);
+
+	private:
+
+	int fd;
+
+};
+
+gpio::gpio(unsigned int gpio_num, gpio_direction pin_direction)
+{
+	std::fstream export_file;
+
+	export_file.open("/sys/class/gpio/export", std::ios::out);
+	if (not export_file.is_open()) throw std::runtime_error(
+		"Failed to open gpio export file."
+	);
+
+	export_file << gpio_num << std::endl;
+
+	base_path << "/sys/class/gpio/gpio" << gpio_num << std::flush;
+
+	std::fstream direction_file;
+	std::string direction_file_name;
+
+	if (gpio_num != 114) {
+		direction_file_name = base_path.str() + "/direction";
+
+		direction_file.open(direction_file_name.c_str());
+		if (!direction_file.is_open())
+			std::cout << "Failed to open direction file." << std::endl;
+		if (pin_direction == OUT)
+			direction_file << "out" << std::endl;
+		else
+			direction_file << "in" << std::endl;
+	}
+
+	std::string value_file_name;
+
+	value_file_name = base_path.str() + "/value";
+
+	value_file.open(value_file_name.c_str(), std::ios_base::in | std::ios_base::out);
+	if (!value_file.is_open())
+		std::cout << "Failed to open value file." << std::endl;
+}
+
+bool gpio::get_value()
+{
+
+	std::string val;
+
+	std::getline(value_file, val);
+	value_file.seekg(0);
+
+	if (val == "0")
+		return false;
+	else if (val == "1")
+		return true;
+	else
+		std::cout << "Data read from value file|" << val << "|" << std::endl;
+
+	return false;
+}
+
+void gpio::set_value(bool state)
+{
+
+	if (state)
+		value_file << "1" << std::endl;
+	else
+		value_file << "0" << std::endl;
+}
+
+static void prepare_fpga_for_configuration(gpio &prog, gpio &)//init)
+{
+
+	prog.set_value(true);
+	prog.set_value(false);
+	prog.set_value(true);
+
+#if 0
+	bool ready_to_program(false);
+	unsigned int count(0);
+	do {
+		ready_to_program = init.get_value();
+		count++;
+
+		sleep(1);
+	} while (count < 10 && !ready_to_program);
+
+	if (count == 10) {
+		std::cout << "FPGA not ready for programming." << std::endl;
+		exit(-1);
+	}
+#endif
+}
+
+spidev::spidev(std::string fname)
+{
+	int ret;
+	int mode = 0;
+	int speed = 12000000;
+	int bits = 8;
+
+	fd = open(fname.c_str(), O_RDWR);
+
+	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
+	ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
+	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
+}
+	
+
+spidev::~spidev()
+{
+	close(fd);
+}
+
+void spidev::send(char *buf, char *rbuf, unsigned int nbytes)
+{
+	int ret;
+
+	struct spi_ioc_transfer tr;
+	tr.tx_buf = (unsigned long) buf;
+	tr.rx_buf = (unsigned long) rbuf;
+	tr.len = nbytes;
+	tr.delay_usecs = 0;
+	tr.speed_hz = 48000000;
+	tr.bits_per_word = 8;
+
+	ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);	
+
+}
+
+static void send_file_to_fpga(const std::string &file_name, gpio &error, gpio &done)
+{
+	std::ifstream bitstream;
+
+	bitstream.open(file_name.c_str(), std::ios::binary);
+	if (!bitstream.is_open()) throw std::runtime_error(
+		"Coult not open the file: " + file_name
+	);
+
+	spidev spi("/dev/spidev1.0");
+	char buf[BUF_SIZE];
+	char rbuf[BUF_SIZE];
+
+	do {
+		bitstream.read(buf, BUF_SIZE);
+		spi.send(buf, rbuf, bitstream.gcount());
+
+		if (error.get_value())
+			std::cout << "INIT_B went high, error occured." << std::endl;
+
+		if (!done.get_value())
+			std::cout << "Configuration complete." << std::endl;
+
+	} while (bitstream.gcount() == BUF_SIZE);
+}
+
+}//namespace usrp_e_fpga_downloader_utility
+
+void usrp_e100_load_fpga(const std::string &bin_file){
+	using namespace usrp_e_fpga_downloader_utility;
+
+	gpio gpio_prog_b(PROG_B, OUT);
+	gpio gpio_init_b(INIT_B, IN);
+	gpio gpio_done  (DONE,   IN);
+
+	std::cout << "Loading FPGA image: " << bin_file << "... " << std::flush;
+
+	if(std::system("/sbin/rmmod usrp_e") != 0){
+		std::cerr << "USRP-E100 FPGA downloader: could not unload usrp_e module" << std::endl;
+	}
+
+	prepare_fpga_for_configuration(gpio_prog_b, gpio_init_b);
+
+	std::cout << "done = " << gpio_done.get_value() << std::endl;
+
+	send_file_to_fpga(bin_file, gpio_init_b, gpio_done);
+
+	if(std::system("/sbin/modprobe usrp_e") != 0){
+		std::cerr << "USRP-E100 FPGA downloader: could not load usrp_e module" << std::endl;
+	}
+
+}
+
-- 
cgit v1.2.3


From 5b4d90582d827d7ba2483d7916b2f0400a5bd9fa Mon Sep 17 00:00:00 2001
From: Josh Blum <josh@joshknows.com>
Date: Thu, 3 Feb 2011 11:20:37 -0800
Subject: usrp-e100: added passthrough to images makefile, tweaks to
 usrp-e-utility

---
 host/lib/usrp/usrp_e100/fpga_downloader.cpp |  2 +-
 host/usrp_e_utils/usrp-e-utility.cpp        | 21 ++++++++++++---------
 images/Makefile                             | 16 ++++++++++++++++
 3 files changed, 29 insertions(+), 10 deletions(-)

(limited to 'host/lib/usrp/usrp_e100/fpga_downloader.cpp')

diff --git a/host/lib/usrp/usrp_e100/fpga_downloader.cpp b/host/lib/usrp/usrp_e100/fpga_downloader.cpp
index 926e6fcaf..c0013fcbd 100644
--- a/host/lib/usrp/usrp_e100/fpga_downloader.cpp
+++ b/host/lib/usrp/usrp_e100/fpga_downloader.cpp
@@ -1,5 +1,5 @@
 //
-// Copyright 2010 Ettus Research LLC
+// 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
diff --git a/host/usrp_e_utils/usrp-e-utility.cpp b/host/usrp_e_utils/usrp-e-utility.cpp
index 139aabc30..4b4fc0178 100644
--- a/host/usrp_e_utils/usrp-e-utility.cpp
+++ b/host/usrp_e_utils/usrp-e-utility.cpp
@@ -18,6 +18,7 @@
 #include <uhd/utils/safe_main.hpp>
 #include <boost/program_options.hpp>
 #include <boost/format.hpp>
+#include <stdexcept>
 #include <iostream>
 
 #include "fpga_downloader.cpp"
@@ -25,8 +26,6 @@
 
 namespace po = boost::program_options;
 
-static const std::string default_passthrough_path = "/usr/share/uhd/images/usrp_e1xx_passthrough.bin";
-
 int UHD_SAFE_MAIN(int argc, char *argv[]){
 
     //variables to be set by po
@@ -36,8 +35,8 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
     po::options_description desc("Allowed options");
     desc.add_options()
         ("help", "help message")
-        ("fpga", po::value<std::string>(&fpga_path)->default_value(""), "loads the specified FPGA file")
-        ("reclk",                                                       "runs the clock recovery")
+        ("fpga", po::value<std::string>(&fpga_path), "loads the specified FPGA file")
+        ("reclk",                                    "runs the clock recovery")
     ;
     po::variables_map vm;
     po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -50,20 +49,24 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){
     }
 
     bool loaded_fpga_image = false;
-    if (vm.count("fpga")){
+    if (vm.count("fpga") != 0){
         std::cout << "USRP-E Utility loading the FPGA..." << std::endl << std::endl;
         usrp_e100_load_fpga(fpga_path);
         loaded_fpga_image = true;
         sleep(1);
     }
 
-    if (vm.count("reclk")){
+    if (vm.count("reclk") != 0){
         std::cout << "USRP-E Utility running the clock recovery..." << std::endl << std::endl;
         //if an image was not loaded or specified, we load pass-through
-        if (fpga_path.empty()) fpga_path = default_passthrough_path;
-        if (not loaded_fpga_image) usrp_e100_load_fpga(fpga_path);
+        if (fpga_path.empty()) throw std::runtime_error(
+            "Please specify the path to the pass-though FPGA image for your device.\n"
+            "  usrp-e-utility --reclk --fpga=/usr/share/uhd/images/usrp_e1xx_pt_fpga.bin"
+        );
         clock_genconfig_main();
-        std::system("rm /tmp/usrp*hash"); //clear hash so driver must reload
+        if (std::system("rm /tmp/usrp*hash") != 0){ //clear hash so driver must reload
+            std::cerr << "No hash to remove! Don't worry, its not a problem." << std::endl;
+        }
     }
 
     std::cout << "Done!" << std::endl;
diff --git a/images/Makefile b/images/Makefile
index 34c18cdc2..cfc783ee4 100644
--- a/images/Makefile
+++ b/images/Makefile
@@ -151,6 +151,22 @@ $(_usrp_e100_fpga_bin): $(GLOBAL_DEPS)
 
 endif
 
+########################################################################
+# USRP-E100 pass-through fpga
+########################################################################
+ifdef HAS_XTCLSH
+
+_usrp_e100_pt_fpga_dir = $(TOP_FPGA_DIR)/usrp2/top/u1e_passthru
+_usrp_e100_pt_fpga_bin = $(BUILT_IMAGES_DIR)/usrp_e100_pt_fpga.bin
+IMAGES_LIST += $(_usrp_e100_pt_fpga_bin)
+
+$(_usrp_e100_pt_fpga_bin): $(GLOBAL_DEPS)
+	cd $(_usrp_e100_pt_fpga_dir) && make clean
+	cd $(_usrp_e100_pt_fpga_dir) && make bin
+	cp $(_usrp_e100_pt_fpga_dir)/build/passthru.bin $@
+
+endif
+
 ########################################################################
 # Build rules
 ########################################################################
-- 
cgit v1.2.3