Integrated more usrp_prims code
[debian/gnuradio] / usrp / host / lib / usrp_prims_common.cc
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)
 {