Integrated more usrp_prims code
authorttsou <ttsou@vt.edu>
Thu, 10 Sep 2009 18:23:10 +0000 (14:23 -0400)
committerttsou <ttsou@vt.edu>
Wed, 16 Sep 2009 21:08:51 +0000 (17:08 -0400)
usrp/host/include/usrp/usrp_prims.h.in
usrp/host/lib/usrp_prims_common.cc
usrp/host/lib/usrp_prims_libusb.cc
usrp/host/lib/usrp_prims_libusb1.cc
usrp/host/swig/usrp_prims.i

index bf71e3b88c6e0c57fcf6cb79f0ab40f56d0ce4e8..69f41e57dfbdd026617f36a02ab032884f0c4abc 100644 (file)
@@ -12,7 +12,6 @@ enum usrp_load_status_t { ULS_ERROR = 0, ULS_OK, ULS_ALREADY_LOADED };
  * new_context set to true since a new context is initiated each time.
  */
 
-//libusb_context* usrp_one_time_init (bool new_context);
 void usrp_one_time_init (libusb_context **ctx = NULL);
 
 /*
@@ -258,34 +257,22 @@ bool usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id,
 std::string usrp_serial_number(libusb_device_handle *udh);
 
 /*
- * Static helper functions
+ * Internal functions 
  */
 
 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);
-bool usrp_usrp_p (libusb_device *q);
-int usrp_hw_rev (libusb_device *q);
-bool our_nanosleep (const struct timespec *delay);
-const char *get_proto_filename (const std::string user_filename,
-                                const char *env_var,
-                                const char *def);
-char *find_file (const char *filename, int hw_rev);
-void load_status_msg (usrp_load_status_t s, const char *type,
-                      const char *filename);
+
 bool usrp1_fpga_write (libusb_device_handle *udh, int regno, int value);
 bool usrp1_fpga_read (libusb_device_handle *udh, int regno, int *value);
-bool usrp_set_switch (libusb_device_handle *udh, int cmd_byte, bool on);
-
-bool usrp_set_fpga_reset (libusb_device_handle *udh, bool on);
-bool usrp_set_fpga_tx_enable (libusb_device_handle *udh, bool on);
-bool usrp_set_fpga_rx_enable (libusb_device_handle *udh, bool on);
-bool usrp_set_fpga_tx_reset (libusb_device_handle *udh, bool on);
-bool usrp_set_fpga_rx_reset (libusb_device_handle *udh, bool on);
 
 #endif /* _USRP_PRIMS_H_ */
index 5ef6503d5ec8f427ed76a0ff6ffada84901465e0..af763acc27f1c9f74c9af5d43ea68fa616792993 100644 (file)
@@ -70,7 +70,7 @@ static const char *default_fpga_filename     = "std_2rxhb_2tx.rbf";
 #include "std_paths.h"
 #include <stdio.h>
 
-char *
+static char *
 find_file (const char *filename, int hw_rev)
 {
   const char **sp = std_paths;
@@ -93,7 +93,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)
@@ -349,7 +349,7 @@ usrp_set_led (libusb_device_handle *udh, int which, bool on)
 }
 
 
-bool
+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;
@@ -497,7 +497,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;
@@ -525,7 +548,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");
@@ -548,6 +610,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)
 {
index 4c826fa558c7762404e7f9d4bcd34a1f72134fdc..9371fb0e2dc8a03cf8a58ad1fc7611564d8307d6 100644 (file)
@@ -64,28 +64,6 @@ static const char *default_fpga_filename     = "std_2rxhb_2tx.rbf";
 #include "std_paths.h"
 #include <stdio.h>
 
-/*
-void
-usrp_one_time_init ()
-{
-  static bool first = true;
-
-  if (first) {
-    first = false;
-    usb_init ();                       // usb library init
-    usb_find_busses ();
-    usb_find_devices ();
-  }
-}
-
-libusb_context *
-usrp_one_time_init (bool new_context)
-{
-  usrp_one_time_init ();
-  return NULL; 
-}
-*/
-
 void
 usrp_one_time_init (libusb_context **ctx)
 {
@@ -323,137 +301,6 @@ usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
 }
 
 
-
-
-static usb_dev_handle *
-open_nth_cmd_interface (int nth)
-{
-  struct usb_device *udev = usrp_find_device (nth);
-  if (udev == 0){
-    fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
-    return 0;
-  }
-
-  struct usb_dev_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");
-    usb_strerror ();
-    return 0;
-  }
-
-  return udh;
-}
-
-
-usrp_load_status_t
-usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx){
-  struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
-  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);
-
-    usb_find_busses ();                // rescan busses and devices
-    usb_find_devices ();
-
-    return ULS_OK;
-
-  default:
-  case ULS_ERROR:              // some kind of problem
-    return ULS_ERROR;
-  }
-}
-
-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
-  {
-    struct usb_device *udev = usrp_find_device (nth);
-    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);
-  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;
-  }
-
-  struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
-  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;
-}
-
 void
 power_down_9862s (struct usb_dev_handle *udh)
 {
@@ -473,6 +320,7 @@ power_down_9862s (struct usb_dev_handle *udh)
   }
 }
 
+// ----------------------------------------------------------------
 
 std::string
 usrp_serial_number(struct usb_dev_handle *udh)
index d75430fe35287eadd3d02f72b8e70d00948ad8ef..672974259d6bee79e5853d6b187ecb1ea765abe7 100644 (file)
@@ -64,14 +64,6 @@ static const char *default_fpga_filename     = "std_2rxhb_2tx.rbf";
 #include "std_paths.h"
 #include <stdio.h>
 
-/*
-void
-usrp_one_time_init ()
-{
-  usrp_one_time_init (false);
-}
-*/
-
 void
 usrp_one_time_init (libusb_context **ctx)
 {
@@ -81,34 +73,6 @@ usrp_one_time_init (libusb_context **ctx)
     fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
 }
 
-/*
-libusb_context *
-usrp_one_time_init (bool new_context)
-{
-
-  static bool first = true;
-  libusb_context *ctx = NULL;
-  int ret;
-
-  // On first call create default context in addition to any new requested
-  // context. The default context is probably useless in this form, but keep
-  // it for now due to possible compatibility reasons.
-
-  if (first) {
-    first = false;
-    if ((ret = libusb_init (NULL)) < 0)
-      fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
-  }
-
-  if (new_context) {
-    if ((ret = libusb_init (&ctx)) < 0)
-      fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
-  }
-
-  return ctx;
-}
-*/
-
 void
 usrp_rescan ()
 {
@@ -176,7 +140,9 @@ usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
   struct libusb_device *q;
   int   n_found = 0;
 
-//usrp_one_time_init (false);
+  // Make sure not operating on default context. There are cases where operating
+  // with a single global (NULL) context may be preferable, so this check can be
+  // skipped if you know what you're doing.
   assert (ctx != NULL);
 
   size_t cnt = libusb_get_device_list(ctx, &list);
@@ -194,10 +160,7 @@ usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
     }
   }
 
-/*
- * The list needs to be freed. Right now just release it if nothing is found.
- */
-
+  // The list needs to be freed. Right now just release it if nothing is found.
   libusb_free_device_list(list, 1);
 
   return 0;    // not found
@@ -206,7 +169,7 @@ usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
 struct libusb_device_handle *
 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
 {
-  struct libusb_device_handle *udh;
+  libusb_device_handle *udh;
   int ret;
 
   if (libusb_open (dev, &udh) < 0)
@@ -344,131 +307,6 @@ usrp_read_fpga_reg (struct libusb_device_handle *udh, int reg, int *value)
 }
 
 
-static libusb_device_handle *
-open_nth_cmd_interface (int nth, libusb_context *ctx)
-{
-
-  struct libusb_device *udev = usrp_find_device (nth, false, ctx);
-  if (udev == 0){
-    fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
-    return 0;
-  }
-
-  struct 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;
-}
-
-usrp_load_status_t
-usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx)
-{
-  struct 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;
-  }
-}
-
-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;
-
-  assert (ctx != NULL);
-
-  // first, figure out what hardware rev we're dealing with
-  {
-    struct 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;
-  }
-  struct 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;
-}
 
 void
 power_down_9862s (struct libusb_device_handle *udh)
index fd65c25ab4cbd3402ab0511fc9fa2124b5d8ebaa..0476858da2701623a262b48ba04509f13edab2ce 100644 (file)
@@ -61,7 +61,7 @@ void usrp_rescan ();
  *   configured USRP (firmware loaded)
  *   unconfigured Cypress FX2 (only if fx2_ok_p is true)
  */
-libusb_device *usrp_find_device (int nth, bool fx2_ok_p = false, libusb_context *ctx);
+libusb_device *usrp_find_device (int nth, bool fx2_ok_p = false, libusb_context *ctx = NULL);
 
 bool usrp_usrp_p (libusb_device *q);           //< is this a USRP
 bool usrp_usrp0_p (libusb_device *q);          //< is this a USRP Rev 0
@@ -114,7 +114,7 @@ usrp_load_firmware (libusb_device_handle *udh, const char *filename, bool force)
  * then rescans the busses and devices.
  */
 usrp_load_status_t
-usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx);
+usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx = NULL);
 
 /*!
  * \brief load fpga configuration bitstream