summaryrefslogtreecommitdiff
path: root/usrp/host/lib
diff options
context:
space:
mode:
authorttsou2009-09-10 14:23:10 -0400
committerttsou2009-09-16 17:08:51 -0400
commite67bd8ae9773731c00d2636833a9120ed3cc2ec1 (patch)
tree9a553ba466f1c24e97efa5e256cb4e5699e38126 /usrp/host/lib
parentfff854782eb2fbfc2a49e07ed9941b3beccc3e83 (diff)
downloadgnuradio-e67bd8ae9773731c00d2636833a9120ed3cc2ec1.tar.gz
gnuradio-e67bd8ae9773731c00d2636833a9120ed3cc2ec1.tar.bz2
gnuradio-e67bd8ae9773731c00d2636833a9120ed3cc2ec1.zip
Integrated more usrp_prims code
Diffstat (limited to 'usrp/host/lib')
-rw-r--r--usrp/host/lib/usrp_prims_common.cc136
-rw-r--r--usrp/host/lib/usrp_prims_libusb.cc154
-rw-r--r--usrp/host/lib/usrp_prims_libusb1.cc172
3 files changed, 137 insertions, 325 deletions
diff --git a/usrp/host/lib/usrp_prims_common.cc b/usrp/host/lib/usrp_prims_common.cc
index 5ef6503d5..af763acc2 100644
--- a/usrp/host/lib/usrp_prims_common.cc
+++ b/usrp/host/lib/usrp_prims_common.cc
@@ -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");
@@ -549,6 +611,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)
{
unsigned char status;
diff --git a/usrp/host/lib/usrp_prims_libusb.cc b/usrp/host/lib/usrp_prims_libusb.cc
index 4c826fa55..9371fb0e2 100644
--- a/usrp/host/lib/usrp_prims_libusb.cc
+++ b/usrp/host/lib/usrp_prims_libusb.cc
@@ -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)
diff --git a/usrp/host/lib/usrp_prims_libusb1.cc b/usrp/host/lib/usrp_prims_libusb1.cc
index d75430fe3..672974259 100644
--- a/usrp/host/lib/usrp_prims_libusb1.cc
+++ b/usrp/host/lib/usrp_prims_libusb1.cc
@@ -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)