1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
//
// Copyright 2010-2012 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <uhd/exception.hpp>
#include <uhd/transport/usb_control.hpp>
#include <uhd/utils/log.hpp>
#include <uhdlib/usrp/common/fx2_ctrl.hpp>
#include <stdint.h>
#include <boost/functional/hash.hpp>
#include <chrono>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
using namespace uhd;
using namespace uhd::usrp;
#define FX2_FIRMWARE_LOAD 0xa0
static const bool load_img_msg = true;
typedef uint32_t hash_type;
/***********************************************************************
* 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 hash_type 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_type(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;
if (len > (2 * (record->length() - 9))) // sanity check to prevent buffer overrun
return false;
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_fx2_reset(void) override
{
unsigned char reset_y = 1;
unsigned char reset_n = 0;
usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_y, 1);
usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_n, 1);
// wait for things to settle
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
void usrp_load_firmware(std::string filestring, bool force) override
{
const char* filename = filestring.c_str();
hash_type hash = generate_hash(filename);
hash_type 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_LOGGER_INFO("FX2") << "Loading firmware image: " << filestring << "...";
usrp_control_write(FX2_FIRMWARE_LOAD, 0xe600, 0, &reset_y, 1);
while (!file.eof()) {
std::string record;
file >> record;
if (!(record.length() > 0))
continue;
// 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
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (load_img_msg)
UHD_LOGGER_INFO("FX2") << "Firmware loaded";
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) override
{
// 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) override
{
const char* filename = filestring.c_str();
hash_type hash = generate_hash(filename);
hash_type 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_LOGGER_INFO("FX2") << "Loading FPGA image: " << filestring << "...";
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));
const std::streamsize n = file.gcount();
if (n == 0)
continue;
int ret = usrp_control_write(VRQ_FPGA_LOAD, 0, FL_XFER, buf, uint16_t(n));
if (ret < 0 or std::streamsize(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_LOGGER_INFO("FX2") << "FPGA image loaded";
}
void usrp_load_eeprom(std::string filestring) override
{
if (load_img_msg)
UHD_LOGGER_INFO("FX2") << "Loading EEPROM image: " << filestring << "...";
const char* filename = filestring.c_str();
const uint16_t i2c_addr = 0x50;
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);
std::streamsize 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 : size_t(len));
int ret = usrp_i2c_write(
i2c_addr, sendbuf, (len > pagesize ? pagesize : size_t(len)) + 1);
if (ret < 0) {
throw uhd::io_error("usrp_load_eeprom: usrp_i2c_write failed");
}
addr += pagesize;
len -= pagesize;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
file.close();
if (load_img_msg)
UHD_LOGGER_INFO("FX2") << "EEPROM image loaded";
}
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(hash_type& hash)
{
UHD_ASSERT_THROW(
usrp_control_read(
0xa0, USRP_HASH_SLOT_0_ADDR, 0, (unsigned char*)&hash, sizeof(hash))
>= 0);
}
void usrp_set_firmware_hash(hash_type hash)
{
UHD_ASSERT_THROW(
usrp_control_write(
0xa0, USRP_HASH_SLOT_0_ADDR, 0, (unsigned char*)&hash, sizeof(hash))
>= 0);
}
void usrp_get_fpga_hash(hash_type& hash)
{
UHD_ASSERT_THROW(
usrp_control_read(
0xa0, USRP_HASH_SLOT_1_ADDR, 0, (unsigned char*)&hash, sizeof(hash))
>= 0);
}
void usrp_set_fpga_hash(hash_type hash)
{
UHD_ASSERT_THROW(
usrp_control_write(
0xa0, USRP_HASH_SLOT_1_ADDR, 0, (unsigned char*)&hash, sizeof(hash))
>= 0);
}
void usrp_tx_enable(bool on) override
{
UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_TX_ENABLE, on, 0) >= 0);
}
void usrp_rx_enable(bool on) override
{
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) override
{
UHD_ASSERT_THROW(usrp_control_write_cmd(VRQ_FPGA_SET_RESET, on, 0) >= 0);
}
int usrp_control_write(uint8_t request,
uint16_t value,
uint16_t index,
unsigned char* buff,
uint16_t length) override
{
return _ctrl_transport->submit(VRT_VENDOR_OUT, // bmReqeustType
request, // bRequest
value, // wValue
index, // wIndex
buff, // data
length); // wLength
}
int usrp_control_read(uint8_t request,
uint16_t value,
uint16_t index,
unsigned char* buff,
uint16_t length) override
{
return _ctrl_transport->submit(VRT_VENDOR_IN, // bmReqeustType
request, // bRequest
value, // wValue
index, // wIndex
buff, // data
length); // wLength
}
int usrp_control_write_cmd(uint8_t request, uint16_t value, uint16_t index)
{
return usrp_control_write(request, value, index, 0, 0);
}
byte_vector_t read_eeprom(uint16_t addr, uint16_t offset, size_t num_bytes) override
{
this->write_i2c(addr, byte_vector_t(1, uint8_t(offset)));
return this->read_i2c(addr, num_bytes);
}
int usrp_i2c_write(uint16_t i2c_addr, unsigned char* buf, uint16_t len) override
{
return usrp_control_write(VRQ_I2C_WRITE, i2c_addr, 0, buf, len);
}
int usrp_i2c_read(uint16_t i2c_addr, unsigned char* buf, uint16_t len) override
{
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(uint16_t addr, const byte_vector_t& bytes) override
{
UHD_ASSERT_THROW(bytes.size() < max_i2c_data_bytes);
int ret =
this->usrp_i2c_write(addr, (unsigned char*)&bytes.front(), bytes.size());
if (iface_debug && (ret < 0))
uhd::runtime_error("USRP: failed i2c write");
}
byte_vector_t read_i2c(uint16_t addr, size_t num_bytes) override
{
UHD_ASSERT_THROW(num_bytes < max_i2c_data_bytes);
byte_vector_t bytes(num_bytes);
int ret = this->usrp_i2c_read(addr, (unsigned char*)&bytes.front(), num_bytes);
if (iface_debug && ((ret < 0) || (unsigned)ret < (num_bytes)))
uhd::runtime_error("USRP: failed i2c read");
return 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));
}
|