usrp: Added error checking on hash transfers
[debian/gnuradio] / usrp / host / lib / usrp_prims_common.cc
index af763acc27f1c9f74c9af5d43ea68fa616792993..689f2154de42ab423c47c05251474b02ef8ddbf5 100644 (file)
@@ -1,19 +1,19 @@
 /* -*- 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,
@@ -24,7 +24,7 @@
 #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"
@@ -66,10 +61,6 @@ static const int hash_slot_addr[2] = {
 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)
 {
@@ -107,10 +98,48 @@ get_proto_filename(const std::string user_filename, const char *env_var, const c
 }
 
 
-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)
 {
@@ -142,6 +171,10 @@ usrp_configured_usrp_p (libusb_device *q)
   return usrp_usrp_p (q) && _usrp_configured_p (q);
 }
 
+
+// ----------------------------------------------------------------
+
+
 libusb_device_handle *
 usrp_open_cmd_interface (libusb_device *dev)
 {
@@ -161,6 +194,35 @@ usrp_open_tx_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
 
@@ -193,7 +255,7 @@ _usrp_load_firmware (libusb_device_handle *udh, const char *filename,
   if (!reset_cpu (udh, true))  // hold CPU in reset while loading firmware
     goto fail;
 
-  
+
   char s[1024];
   int length;
   int addr;
@@ -285,7 +347,7 @@ _usrp_load_fpga (libusb_device_handle *udh, const char *filename,
 
   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;
@@ -293,7 +355,7 @@ _usrp_load_fpga (libusb_device_handle *udh, const char *filename,
 
   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))
@@ -306,7 +368,7 @@ _usrp_load_fpga (libusb_device_handle *udh, const char *filename,
   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);
 
@@ -340,7 +402,7 @@ _usrp_load_fpga (libusb_device_handle *udh, const char *filename,
 
 // ----------------------------------------------------------------
 
-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);
@@ -348,6 +410,39 @@ usrp_set_led (libusb_device_handle *udh, int which, bool on)
   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)
@@ -355,7 +450,7 @@ 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)
 {
@@ -367,14 +462,14 @@ usrp1_fpga_write (libusb_device_handle *udh,
   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)
 {
@@ -392,32 +487,55 @@ usrp1_fpga_read (libusb_device_handle *udh,
   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);
@@ -440,7 +558,7 @@ compute_hash (const char *filename, unsigned char hash[USRP_HASH_SIZE])
   }
   int r = md5_stream (fp, hash);
   fclose (fp);
-  
+
   return r == 0;
 }
 
@@ -455,7 +573,7 @@ usrp_conditionally_load_something (libusb_device_handle *udh,
 {
   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;
@@ -567,13 +685,13 @@ usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_contex
 
   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;
@@ -592,12 +710,12 @@ load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
 {
   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);
@@ -662,7 +780,7 @@ usrp_load_standard_bits (int nth, bool force,
   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);
@@ -679,7 +797,7 @@ _usrp_get_status (libusb_device_handle *udh, int which, bool *trouble)
 {
   unsigned char        status;
   *trouble = true;
-  
+
   if (write_cmd (udh, VRQ_GET_STATUS, 0, which,
                 &status, sizeof (status)) != sizeof (status))
     return false;
@@ -764,7 +882,7 @@ usrp_9862_write (libusb_device_handle *udh, int which_codec,
   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,
@@ -814,6 +932,25 @@ usrp_9862_write_many_all (libusb_device_handle *udh,
   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;
 
@@ -823,7 +960,7 @@ usrp_eeprom_write (libusb_device_handle *udh, int i2c_addr,
 {
   unsigned char cmd[2];
   const unsigned char *p = (unsigned char *) buf;
-  
+
   // The simplest thing that could possibly work:
   //   all writes are single byte writes.
   //
@@ -838,7 +975,7 @@ usrp_eeprom_write (libusb_device_handle *udh, int i2c_addr,
     if (!r)
       return false;
   }
-  
+
   return true;
 }
 
@@ -865,14 +1002,14 @@ usrp_eeprom_read (libusb_device_handle *udh, int i2c_addr,
   }
   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:
@@ -909,7 +1046,7 @@ usrp_write_aux_dac (libusb_device_handle *udh, int slot,
                    int which_dac, int value)
 {
   int which_codec;
-  
+
   if (!slot_to_codec (slot, &which_codec))
     return false;
 
@@ -919,7 +1056,7 @@ usrp_write_aux_dac (libusb_device_handle *udh, int slot,
   }
 
   value &= 0x0fff;     // mask to 12-bits
-  
+
   if (which_dac == 3){
     // dac 3 is really 12-bits.  Use value as is.
     bool r = true;
@@ -928,7 +1065,7 @@ usrp_write_aux_dac (libusb_device_handle *udh, int slot,
     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);
   }
@@ -955,7 +1092,7 @@ usrp_read_aux_adc (libusb_device_handle *udh, int slot,
     | 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;
@@ -963,7 +1100,7 @@ usrp_read_aux_adc (libusb_device_handle *udh, int slot,
     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.
 
@@ -987,7 +1124,7 @@ usrp_read_aux_adc (libusb_device_handle *udh, int slot,
 
   if (r)
     *value = ((v_hi << 2) | ((v_lo >> 6) & 0x3)) << 2; // format as 12-bit
-  
+
   return r;
 }
 
@@ -1077,3 +1214,26 @@ usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id,
   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;
+}
+
+
+
+