/* -*- c++ -*- */
/*
* Copyright 2003,2004,2006,2009 Free Software Foundation, Inc.
- *
+ *
* This file is part of GNU Radio
- *
+ *
* GNU Radio 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, or (at your option)
* any later version.
- *
+ *
* GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
#include "config.h"
#endif
-#include "usrp/usrp_prims.h"
+#include "usrp_primsi.h"
#include "usrp_commands.h"
#include "usrp_ids.h"
#include "usrp_i2c_addr.h"
#include <algorithm>
#include <ad9862.h>
#include <assert.h>
-
-#ifdef HAVE_LIBUSB_1
-#include <libusb-1.0/libusb.h>
-#else
-#include <usb.h>
-#endif
+#include "std_paths.h"
extern "C" {
#include "md5.h"
static const char *default_firmware_filename = "std.ihx";
static const char *default_fpga_filename = "std_2rxhb_2tx.rbf";
-
-#include "std_paths.h"
-#include <stdio.h>
-
static char *
find_file (const char *filename, int hw_rev)
{
}
-void power_down_9862s (libusb_device_handle *udh);
+static void power_down_9862s (libusb_device_handle *udh);
+
// ----------------------------------------------------------------
+/*
+ * q must be a real USRP, not an FX2. Return its hardware rev number.
+ */
+
+int
+usrp_hw_rev (libusb_device *q)
+{
+ libusb_device_descriptor desc = _get_usb_device_descriptor(q);
+ return desc.bcdDevice & 0x00FF;
+}
+
+/*
+ * q must be a real USRP, not an FX2. Return true if it's configured.
+ */
+static bool
+_usrp_configured_p (libusb_device *q)
+{
+ libusb_device_descriptor desc = _get_usb_device_descriptor(q);
+ return (desc.bcdDevice & 0xFF00) != 0;
+}
+
+bool
+usrp_usrp_p (libusb_device *q)
+{
+ libusb_device_descriptor desc = _get_usb_device_descriptor(q);
+ return (desc.idVendor == USB_VID_FSF
+ && desc.idProduct == USB_PID_FSF_USRP);
+}
+
+bool
+usrp_fx2_p (libusb_device *q)
+{
+ libusb_device_descriptor desc = _get_usb_device_descriptor(q);
+ return (desc.idVendor == USB_VID_CYPRESS
+ && desc.idProduct == USB_PID_CYPRESS_FX2);
+}
+
bool
usrp_usrp0_p (libusb_device *q)
{
return usrp_usrp_p (q) && _usrp_configured_p (q);
}
+
+// ----------------------------------------------------------------
+
+
libusb_device_handle *
usrp_open_cmd_interface (libusb_device *dev)
{
}
+// ----------------------------------------------------------------
+// write internal ram using Cypress vendor extension
+
+static bool
+write_internal_ram (libusb_device_handle *udh, unsigned char *buf,
+ int start_addr, size_t len)
+{
+ int addr;
+ int n;
+ int a;
+ int quanta = MAX_EP0_PKTSIZE;
+
+ for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
+ n = len + start_addr - addr;
+ if (n > quanta)
+ n = quanta;
+
+ a = _usb_control_transfer (udh, 0x40, 0xA0, addr, 0,
+ (unsigned char*)(buf + (addr - start_addr)), n, 1000);
+
+ if (a < 0){
+ fprintf(stderr,"write_internal_ram failed: %i\n", a);
+ return false;
+ }
+ }
+ return true;
+}
+
+
// ----------------------------------------------------------------
// whack the CPUCS register using the upload RAM vendor extension
if (!reset_cpu (udh, true)) // hold CPU in reset while loading firmware
goto fail;
-
+
char s[1024];
int length;
int addr;
if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_BEGIN, 0, 0) != 0)
goto fail;
-
+
while ((n = fread (buf, 1, sizeof (buf), fp)) > 0){
if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_XFER, buf, n) != n)
goto fail;
if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_END, 0, 0) != 0)
goto fail;
-
+
fclose (fp);
if (!usrp_set_hash (udh, FPGA_HASH_SLOT, hash))
usrp_set_fpga_reset (udh, 0); // fpga out of master reset
// now these commands will work
-
+
ok &= usrp_set_fpga_tx_enable (udh, 0);
ok &= usrp_set_fpga_rx_enable (udh, 0);
// ----------------------------------------------------------------
-bool
+bool
usrp_set_led (libusb_device_handle *udh, int which, bool on)
{
int r = write_cmd (udh, VRQ_SET_LED, on, which, 0, 0);
return r == 0;
}
+bool
+usrp_set_hash (libusb_device_handle *udh, int which,
+ const unsigned char hash[USRP_HASH_SIZE])
+{
+ which &= 1;
+
+ // we use the Cypress firmware down load command to jam it in.
+ int r = _usb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
+ (unsigned char *) hash, USRP_HASH_SIZE, 1000);
+
+ if (r < 0)
+ fprintf (stderr, "usrp: failed to set hash: %i\n", r);
+
+ return r == USRP_HASH_SIZE;
+}
+
+bool
+usrp_get_hash (libusb_device_handle *udh, int which,
+ unsigned char hash[USRP_HASH_SIZE])
+{
+ which &= 1;
+
+ // we use the Cypress firmware upload command to fetch it.
+ int r = _usb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
+ (unsigned char *) hash, USRP_HASH_SIZE, 1000);
+
+ if (r < 0)
+ fprintf (stderr, "usrp: failed to get hash: %i\n", r);
+
+ return r == USRP_HASH_SIZE;
+}
+
+
static bool
usrp_set_switch (libusb_device_handle *udh, int cmd_byte, bool on)
return write_cmd (udh, cmd_byte, on, 0, 0, 0) == 0;
}
-bool
+static bool
usrp1_fpga_write (libusb_device_handle *udh,
int regno, int value)
{
buf[1] = (value >> 16) & 0xff;
buf[2] = (value >> 8) & 0xff;
buf[3] = (value >> 0) & 0xff;
-
+
return usrp_spi_write (udh, 0x00 | (regno & 0x7f),
SPI_ENABLE_FPGA,
SPI_FMT_MSB | SPI_FMT_HDR_1,
buf, sizeof (buf));
}
-bool
+static bool
usrp1_fpga_read (libusb_device_handle *udh,
int regno, int *value)
{
return ok;
}
+bool
+usrp_write_fpga_reg (libusb_device_handle *udh, int reg, int value)
+{
+ switch (usrp_hw_rev (_get_usb_device (udh))){
+ case 0: // not supported ;)
+ abort();
+
+ default:
+ return usrp1_fpga_write (udh, reg, value);
+ }
+}
+
+bool
+usrp_read_fpga_reg (libusb_device_handle *udh, int reg, int *value)
+{
+ switch (usrp_hw_rev (_get_usb_device (udh))){
+ case 0: // not supported ;)
+ abort();
-bool
+ default:
+ return usrp1_fpga_read (udh, reg, value);
+ }
+}
+
+bool
usrp_set_fpga_reset (libusb_device_handle *udh, bool on)
{
return usrp_set_switch (udh, VRQ_FPGA_SET_RESET, on);
}
-bool
+bool
usrp_set_fpga_tx_enable (libusb_device_handle *udh, bool on)
{
return usrp_set_switch (udh, VRQ_FPGA_SET_TX_ENABLE, on);
}
-bool
+bool
usrp_set_fpga_rx_enable (libusb_device_handle *udh, bool on)
{
return usrp_set_switch (udh, VRQ_FPGA_SET_RX_ENABLE, on);
}
-bool
+bool
usrp_set_fpga_tx_reset (libusb_device_handle *udh, bool on)
{
return usrp_set_switch (udh, VRQ_FPGA_SET_TX_RESET, on);
}
-bool
+bool
usrp_set_fpga_rx_reset (libusb_device_handle *udh, bool on)
{
return usrp_set_switch (udh, VRQ_FPGA_SET_RX_RESET, on);
}
int r = md5_stream (fp, hash);
fclose (fp);
-
+
return r == 0;
}
{
unsigned char file_hash[USRP_HASH_SIZE];
unsigned char usrp_hash[USRP_HASH_SIZE];
-
+
if (access (filename, R_OK) != 0){
perror (filename);
return ULS_ERROR;
case ULS_OK:
// we loaded firmware successfully.
-
+
// It's highly likely that the board will renumerate (simulate a
// disconnect/reconnect sequence), invalidating our current
// handle.
// FIXME. Turn this into a loop that rescans until we refind ourselves
-
+
struct timespec t; // delay for 1 second
t.tv_sec = 2;
t.tv_nsec = 0;
{
char *e = getenv("USRP_VERBOSE");
bool verbose = e != 0;
-
+
switch (s){
case ULS_ERROR:
fprintf (stderr, "usrp: failed to load %s %s.\n", type, filename);
break;
-
+
case ULS_ALREADY_LOADED:
if (verbose)
fprintf (stderr, "usrp: %s %s already loaded.\n", type, filename);
libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
if (udh == 0)
return false;
-
+
s = usrp_load_fpga (udh, filename, force);
usrp_close_interface (udh);
load_status_msg (s, "fpga bitstream", filename);
{
unsigned char status;
*trouble = true;
-
+
if (write_cmd (udh, VRQ_GET_STATUS, 0, which,
&status, sizeof (status)) != sizeof (status))
return false;
unsigned char buf[1];
buf[0] = value;
-
+
return usrp_spi_write (udh, 0x00 | (regno & 0x3f),
which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
SPI_FMT_MSB | SPI_FMT_HDR_1,
return result;
}
+static void
+power_down_9862s (libusb_device_handle *udh)
+{
+ static const unsigned char regs[] = {
+ REG_RX_PWR_DN, 0x01, // everything
+ REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
+ REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
+ };
+
+ switch (usrp_hw_rev (_get_usb_device (udh))){
+ case 0:
+ break;
+
+ default:
+ usrp_9862_write_many_all (udh, regs, sizeof (regs));
+ break;
+ }
+}
+
static const int EEPROM_PAGESIZE = 16;
{
unsigned char cmd[2];
const unsigned char *p = (unsigned char *) buf;
-
+
// The simplest thing that could possibly work:
// all writes are single byte writes.
//
if (!r)
return false;
}
-
+
return true;
}
}
return true;
}
-
+
// ----------------------------------------------------------------
static bool
slot_to_codec (int slot, int *which_codec)
{
*which_codec = 0;
-
+
switch (slot){
case SLOT_TX_A:
case SLOT_RX_A:
int which_dac, int value)
{
int which_codec;
-
+
if (!slot_to_codec (slot, &which_codec))
return false;
}
value &= 0x0fff; // mask to 12-bits
-
+
if (which_dac == 3){
// dac 3 is really 12-bits. Use value as is.
bool r = true;
return r;
}
else {
- // dac 0, 1, and 2 are really 8 bits.
+ // dac 0, 1, and 2 are really 8 bits.
value = value >> 4; // shift value appropriately
return usrp_9862_write (udh, which_codec, 36 + which_dac, value);
}
| AUX_ADC_CTRL_REFSEL_B; // on chip reference
int rd_reg = 26; // base address of two regs to read for result
-
+
// program the ADC mux bits
if (tx_slot_p (slot))
aux_adc_control |= AUX_ADC_CTRL_SELECT_A2 | AUX_ADC_CTRL_SELECT_B2;
rd_reg += 2;
aux_adc_control |= AUX_ADC_CTRL_SELECT_A1 | AUX_ADC_CTRL_SELECT_B1;
}
-
+
// I'm not sure if we can set the mux and issue a start conversion
// in the same cycle, so let's do them one at a time.
if (r)
*value = ((v_hi << 2) | ((v_lo >> 6) & 0x3)) << 2; // format as 12-bit
-
+
return r;
}
return usrp_eeprom_write (udh, slot_to_i2c_addr (slot_id),
0, buf, sizeof (buf));
}
+
+// ----------------------------------------------------------------
+
+std::string
+usrp_serial_number(libusb_device_handle *udh)
+{
+ libusb_device_descriptor desc =
+ _get_usb_device_descriptor (_get_usb_device (udh));
+
+ unsigned char iserial = desc.iSerialNumber;
+ if (iserial == 0)
+ return "";
+
+ unsigned char buf[1024];
+ if (_get_usb_string_descriptor (udh, iserial, buf, sizeof(buf)) < 0)
+ return "";
+
+ return (char*) buf;
+}
+
+
+
+