diff options
Diffstat (limited to 'host/lib/usrp/usrp1/usrp1_ctrl.cpp')
-rw-r--r-- | host/lib/usrp/usrp1/usrp1_ctrl.cpp | 206 |
1 files changed, 78 insertions, 128 deletions
diff --git a/host/lib/usrp/usrp1/usrp1_ctrl.cpp b/host/lib/usrp/usrp1/usrp1_ctrl.cpp index 09f854813..3fa6cb8b7 100644 --- a/host/lib/usrp/usrp1/usrp1_ctrl.cpp +++ b/host/lib/usrp/usrp1/usrp1_ctrl.cpp @@ -16,7 +16,8 @@ // #include "usrp1_ctrl.hpp" -#include "usrp_commands.h" +#include "usrp_commands.h" +#include <uhd/exception.hpp> #include <uhd/transport/usb_control.hpp> #include <boost/functional/hash.hpp> #include <boost/thread/thread.hpp> @@ -29,13 +30,6 @@ using namespace uhd; -enum firmware_code { - USRP_FPGA_LOAD_SUCCESS, - USRP_FPGA_ALREADY_LOADED, - USRP_FIRMWARE_LOAD_SUCCESS, - USRP_FIRMWARE_ALREADY_LOADED -}; - #define FX2_FIRMWARE_LOAD 0xa0 static const bool load_img_msg = true; @@ -45,15 +39,16 @@ static const bool load_img_msg = true; **********************************************************************/ /*! * Create a file hash - * The hash will be used to identify the loaded firmware and fpga image + * 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 (!file) - std::cerr << "error: cannot open input file " << filename << std::endl; + if (not file){ + throw uhd::io_error(std::string("cannot open input file ") + filename); + } size_t hash = 0; @@ -62,18 +57,19 @@ static size_t generate_hash(const char *filename) boost::hash_combine(hash, ch); } - if (!file.eof()) - std::cerr << "error: file error " << filename << std::endl; + if (not file.eof()){ + throw uhd::io_error(std::string("file error ") + filename); + } - file.close(); - return hash; + file.close(); + return hash; } /*! - * Verify checksum of a Intel HEX record + * Verify checksum of a Intel HEX record * \param record a line from an Intel HEX file - * \return true if record is valid, false otherwise + * \return true if record is valid, false otherwise */ static bool checksum(std::string *record) { @@ -123,7 +119,7 @@ bool parse_record(std::string *record, unsigned int &len, for (i = 0; i < len; i++) { std::istringstream(record->substr(9 + 2 * i, 2)) >> std::hex >> val; data[i] = (unsigned char) val; - } + } return true; } @@ -139,20 +135,15 @@ public: _ctrl_transport = ctrl_transport; } - int usrp_load_firmware(std::string filestring, bool force) + 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; - if (usrp_get_firmware_hash(loaded_hash) < 0) { - std::cerr << "firmware hash retrieval failed" << std::endl; - return -1; - } + size_t loaded_hash; usrp_get_firmware_hash(loaded_hash); - if (!force && (hash == loaded_hash)) - return USRP_FIRMWARE_ALREADY_LOADED; + if (not force and (hash == loaded_hash)) return; //FIXME: verify types unsigned int len; @@ -160,13 +151,11 @@ public: unsigned int type; unsigned char data[512]; - int ret; std::ifstream file; file.open(filename, std::ifstream::in); if (!file.good()) { - std::cerr << "cannot open firmware input file" << std::endl; - return -1; + throw uhd::io_error("usrp_load_firmware: cannot open firmware input file"); } unsigned char reset_y = 1; @@ -174,56 +163,41 @@ public: //hit the reset line if (load_img_msg) std::cout << "Loading firmware image: " << filestring << "..." << std::flush; - usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, - &reset_y, 1); - + usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_y, 1); + while (!file.eof()) { std::string record; file >> record; - - //check for valid record - if (!checksum(&record) || - !parse_record(&record, len, addr, type, data)) { - std::cerr << "error: bad record" << std::endl; - file.close(); - return -1; + + //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) { - ret = usrp_control_write(FX2_FIRMWARE_LOAD, addr, 0, - data, len); - if (ret < 0) { - std::cerr << "error: usrp_control_write failed: "; - std::cerr << ret << std::endl; - file.close(); - return -1; - } - } - //type 0x01 is end + 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); + 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) std::cout << " done" << std::endl; - return USRP_FIRMWARE_LOAD_SUCCESS; + return; } //type anything else is unhandled else { - std::cerr << "error: unsupported record" << std::endl; - file.close(); - return -1; + throw uhd::io_error("usrp_load_firmware: unsupported record"); } } - //file did not end - std::cerr << "error: bad record" << std::endl; - file.close(); - return -1; + //file did not end + throw uhd::io_error("usrp_load_firmware: bad record"); } void usrp_init(void){ @@ -241,64 +215,48 @@ public: usrp_tx_enable(true); } - int usrp_load_fpga(std::string filestring) + void usrp_load_fpga(std::string filestring) { const char *filename = filestring.c_str(); size_t hash = generate_hash(filename); - size_t loaded_hash; - if (usrp_get_fpga_hash(loaded_hash) < 0) { - std::cerr << "fpga hash retrieval failed" << std::endl; - return -1; - } + size_t loaded_hash; usrp_get_fpga_hash(loaded_hash); - if (hash == loaded_hash) - return USRP_FPGA_ALREADY_LOADED; + if (hash == loaded_hash) return; const int ep0_size = 64; unsigned char buf[ep0_size]; - int ret; if (load_img_msg) std::cout << "Loading FPGA image: " << filestring << "..." << std::flush; std::ifstream file; file.open(filename, std::ios::in | std::ios::binary); if (not file.good()) { - std::cerr << "cannot open fpga input file" << std::endl; - file.close(); - return -1; + throw uhd::io_error("usrp_load_fpga: cannot open fpga input file"); } if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_BEGIN) < 0) { - std::cerr << "fpga load error" << std::endl; - file.close(); - return -1; + 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(); - ret = usrp_control_write(VRQ_FPGA_LOAD, 0, FL_XFER, - buf, n); + int ret = usrp_control_write(VRQ_FPGA_LOAD, 0, FL_XFER, buf, n); if (ret < 0 or size_t(ret) != n) { - std::cerr << "fpga load error " << ret << std::endl; - file.close(); - return -1; + throw uhd::io_error("usrp_load_fpga: fpga load error"); } } - + if (usrp_control_write_cmd(VRQ_FPGA_LOAD, 0, FL_END) < 0) { - std::cerr << "fpga load error" << std::endl; - file.close(); - return -1; + throw uhd::io_error("usrp_load_fpga: fpga load error"); } usrp_set_fpga_hash(hash); file.close(); if (load_img_msg) std::cout << " done" << std::endl; - return 0; } - int usrp_load_eeprom(std::string filestring) + void usrp_load_eeprom(std::string filestring) { const char *filename = filestring.c_str(); const boost::uint16_t i2c_addr = 0x50; @@ -309,100 +267,92 @@ public: unsigned char data[256]; unsigned char sendbuf[17]; - int ret; std::ifstream file; file.open(filename, std::ifstream::in); - if (!file.good()) { - std::cerr << "cannot open EEPROM input file" << std::endl; - return -1; + 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) { - std::cerr << "error: image size too large" << std::endl; - file.close(); - return -1; + 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); - ret = usrp_i2c_write(i2c_addr, sendbuf, (len > pagesize ? pagesize : len)+1); - if (ret < 0) { - std::cerr << "error: usrp_i2c_write failed: "; - std::cerr << ret << std::endl; - file.close(); - return -1; - } - addr += pagesize; - len -= pagesize; - boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + 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(); - return 0; } - int usrp_set_led(int led_num, bool on) + void usrp_set_led(int led_num, bool on) { - return usrp_control_write_cmd(VRQ_SET_LED, on, led_num); + UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_SET_LED, on, led_num) >= 0); } - int usrp_get_firmware_hash(size_t &hash) + void usrp_get_firmware_hash(size_t &hash) { - return usrp_control_read(0xa0, USRP_HASH_SLOT_0_ADDR, 0, - (unsigned char*) &hash, sizeof(size_t)); + UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_0_ADDR, 0, + (unsigned char*) &hash, sizeof(size_t)) >= 0); } - int usrp_set_firmware_hash(size_t hash) + void usrp_set_firmware_hash(size_t hash) { - return usrp_control_write(0xa0, USRP_HASH_SLOT_0_ADDR, 0, - (unsigned char*) &hash, sizeof(size_t)); + UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_0_ADDR, 0, + (unsigned char*) &hash, sizeof(size_t)) >= 0); } - int usrp_get_fpga_hash(size_t &hash) + void usrp_get_fpga_hash(size_t &hash) { - return usrp_control_read(0xa0, USRP_HASH_SLOT_1_ADDR, 0, - (unsigned char*) &hash, sizeof(size_t)); + UHD_ASSERT_THROW(usrp_control_read(0xa0, USRP_HASH_SLOT_1_ADDR, 0, + (unsigned char*) &hash, sizeof(size_t)) >= 0); } - int usrp_set_fpga_hash(size_t hash) + void usrp_set_fpga_hash(size_t hash) { - return usrp_control_write(0xa0, USRP_HASH_SLOT_1_ADDR, 0, - (unsigned char*) &hash, sizeof(size_t)); + UHD_ASSERT_THROW(usrp_control_write(0xa0, USRP_HASH_SLOT_1_ADDR, 0, + (unsigned char*) &hash, sizeof(size_t)) >= 0); } - int usrp_tx_enable(bool on) + void usrp_tx_enable(bool on) { - return usrp_control_write_cmd(VRQ_FPGA_SET_TX_ENABLE, on, 0); + UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_ENABLE, on, 0) >= 0); } - int usrp_rx_enable(bool on) + void usrp_rx_enable(bool on) { - return usrp_control_write_cmd(VRQ_FPGA_SET_RX_ENABLE, on, 0); + UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_ENABLE, on, 0) >= 0); } - int usrp_tx_reset(bool on) + void usrp_tx_reset(bool on) { - return usrp_control_write_cmd(VRQ_FPGA_SET_TX_RESET, on, 0); + UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_RESET, on, 0) >= 0); } - int usrp_rx_reset(bool on) + void usrp_rx_reset(bool on) { - return usrp_control_write_cmd(VRQ_FPGA_SET_RX_RESET, on, 0); + UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RX_RESET, on, 0) >= 0); } @@ -417,7 +367,7 @@ public: value, // wValue index, // wIndex buff, // data - length); // wLength + length); // wLength } |