Removed internal functions from external header file
[debian/gnuradio] / usrp / host / lib / usrp_prims_common.cc
index 4b695bec5cdca6c213eb5188af7ab04ab2b2c2cc..c55098dc3baad478b252e4f61b8bc0b4e0c15593 100644 (file)
 #include "config.h"
 #endif
 
-#include "usrp/usrp_prims.h"
+#ifdef HAVE_LIBUSB_1 
+#include <libusb-1.0/libusb.h>
+#else
+#include <usb.h>
+#endif
+
+#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>
-
-#if 1
-#include <usb.h>
-#else
-#include <libusb-1.0/libusb.h>
-#endif
+#include "std_paths.h"
 
 extern "C" {
 #include "md5.h"
@@ -66,23 +67,7 @@ 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";
 
-/*
- * Forward Declarations 
- */
-
-bool _usrp_configured_p (libusb_device *q);
-libusb_device_handle *usrp_open_interface(libusb_device *dev,
-                                          int interface,
-                                          int altinterface);
-bool write_internal_ram (libusb_device_handle *udh, unsigned char *buf,
-                         int start_addr, size_t len);
-int write_cmd (libusb_device_handle *udh, int request, int value,
-               int index, unsigned char *bytes, int len);
-
-#include "std_paths.h"
-#include <stdio.h>
-
-char *
+static char *
 find_file (const char *filename, int hw_rev)
 {
   const char **sp = std_paths;
@@ -105,7 +90,7 @@ find_file (const char *filename, int hw_rev)
   return 0;
 }
 
-const char *
+static const char *
 get_proto_filename(const std::string user_filename, const char *env_var, const char *def)
 {
   if (user_filename.length() != 0)
@@ -119,10 +104,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)
 {
@@ -154,6 +177,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)
 {
@@ -173,6 +200,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: %u\n", a);
+      return false;
+    }
+  }
+  return true;
+}
+
+
 // ----------------------------------------------------------------
 // whack the CPUCS register using the upload RAM vendor extension
 
@@ -360,14 +416,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);
+  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);
+  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)
 {
@@ -386,7 +467,7 @@ usrp1_fpga_write (libusb_device_handle *udh,
                         buf, sizeof (buf));
 }
 
-bool
+static bool
 usrp1_fpga_read (libusb_device_handle *udh,
                 int regno, int *value)
 {
@@ -404,6 +485,29 @@ 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();
+
+  default:
+    return usrp1_fpga_read (udh, reg, value);
+  }
+}
 
 bool 
 usrp_set_fpga_reset (libusb_device_handle *udh, bool on)
@@ -509,7 +613,30 @@ usrp_load_fpga (libusb_device_handle *udh,
                                            _usrp_load_fpga);
 }
 
-bool
+static libusb_device_handle *
+open_nth_cmd_interface (int nth, libusb_context *ctx)
+{
+
+  libusb_device *udev = usrp_find_device (nth, false, ctx);
+  if (udev == 0){
+    fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
+    return 0;
+  }
+
+  libusb_device_handle *udh;
+
+  udh = usrp_open_cmd_interface (udev);
+  if (udh == 0){
+    // FIXME this could be because somebody else has it open.
+    // We should delay and retry...
+    fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
+    return 0;
+  }
+
+  return udh;
+}
+
+static bool
 our_nanosleep (const struct timespec *delay)
 {
   struct timespec      new_delay = *delay;
@@ -537,7 +664,46 @@ mdelay (int millisecs)
   return our_nanosleep (&ts);
 }
 
-void
+
+usrp_load_status_t
+usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx)
+{
+  libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
+  if (udh == 0)
+    return ULS_ERROR;
+
+  usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
+  usrp_close_interface (udh);
+
+  switch (s){
+
+  case ULS_ALREADY_LOADED:              // nothing changed...
+    return ULS_ALREADY_LOADED;
+    break;
+
+  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;
+    our_nanosleep (&t);
+
+    return ULS_OK;
+
+  default:
+  case ULS_ERROR:               // some kind of problem
+    return ULS_ERROR;
+  }
+}
+
+static void
 load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
 {
   char *e = getenv("USRP_VERBOSE");
@@ -560,6 +726,70 @@ load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
   }
 }
 
+bool
+usrp_load_standard_bits (int nth, bool force,
+                        const std::string fpga_filename,
+                        const std::string firmware_filename,
+                        libusb_context *ctx)
+{
+  usrp_load_status_t   s;
+  const char           *filename;
+  const char           *proto_filename;
+  int hw_rev;
+
+  // first, figure out what hardware rev we're dealing with
+  {
+    libusb_device *udev = usrp_find_device (nth, false, ctx);
+    if (udev == 0){
+      fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
+      return false;
+    }
+    hw_rev = usrp_hw_rev (udev);
+  }
+
+  // start by loading the firmware
+
+  proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
+                                     default_firmware_filename);
+  filename = find_file(proto_filename, hw_rev);
+  if (filename == 0){
+    fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
+    return false;
+  }
+  s = usrp_load_firmware_nth (nth, filename, force, ctx);
+  load_status_msg (s, "firmware", filename);
+
+  if (s == ULS_ERROR)
+    return false;
+
+  // if we actually loaded firmware, we must reload fpga ...
+  if (s == ULS_OK)
+    force = true;
+
+  // now move on to the fpga configuration bitstream
+
+  proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
+                                     default_fpga_filename);
+  filename = find_file (proto_filename, hw_rev);
+  if (filename == 0){
+    fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
+    return false;
+  }
+  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);
+
+  if (s == ULS_ERROR)
+    return false;
+
+  return true;
+}
+
+
 bool
 _usrp_get_status (libusb_device_handle *udh, int which, bool *trouble)
 {
@@ -700,6 +930,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;
 
@@ -963,3 +1212,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;
+}
+
+
+
+