From f9c3bfb83af1048db54de941df1898f2ec7fb1fc Mon Sep 17 00:00:00 2001 From: Thomas Tsou Date: Sat, 17 Oct 2009 22:28:43 -0400 Subject: [PATCH] usrp: Cleanup of usrp_basic Version specific libusb calls are pushed out of usrp_basic into usrp_prims where they belong. This leads to a single usrp_basic file. A new function in usrp_prims, usrp_deinit(), handles shutdown specific to version 1.0. --- usrp/host/include/usrp/usrp_prims.h | 9 ++ usrp/host/lib/Makefile.am | 24 ++- .../{usrp_basic_common.cc => usrp_basic.cc} | 67 +++++++- usrp/host/lib/usrp_basic_libusb0.cc | 137 ----------------- usrp/host/lib/usrp_basic_libusb1.cc | 145 ------------------ usrp/host/lib/usrp_prims_libusb0.cc | 6 + usrp/host/lib/usrp_prims_libusb1.cc | 11 +- 7 files changed, 99 insertions(+), 300 deletions(-) rename usrp/host/lib/{usrp_basic_common.cc => usrp_basic.cc} (93%) delete mode 100644 usrp/host/lib/usrp_basic_libusb0.cc delete mode 100644 usrp/host/lib/usrp_basic_libusb1.cc diff --git a/usrp/host/include/usrp/usrp_prims.h b/usrp/host/include/usrp/usrp_prims.h index 323287d0..4780d000 100644 --- a/usrp/host/include/usrp/usrp_prims.h +++ b/usrp/host/include/usrp/usrp_prims.h @@ -53,6 +53,15 @@ enum usrp_load_status_t { ULS_ERROR = 0, ULS_OK, ULS_ALREADY_LOADED }; void usrp_one_time_init (libusb_context **ctx = NULL); +/*! + * \brief deinitialize libusb + * + * libusb-0.1: No effect + * + * libusb-1.0: Deinitialize context ctx + */ +void usrp_deinit (libusb_context *ctx); + /* * force a rescan of the buses and devices */ diff --git a/usrp/host/lib/Makefile.am b/usrp/host/lib/Makefile.am index 72312ebb..e80f2711 100644 --- a/usrp/host/lib/Makefile.am +++ b/usrp/host/lib/Makefile.am @@ -61,8 +61,7 @@ BUILT_SOURCES += usrp_dbid.cc \ generic_CODE = \ fusb_generic.cc \ fusb_sysconfig_generic.cc \ - usrp_prims_libusb0.cc \ - usrp_basic_libusb0.cc + usrp_prims_libusb0.cc darwin_CODE = \ fusb_darwin.cc \ @@ -72,34 +71,29 @@ darwin_CODE = \ circular_linked_list.h \ darwin_libusb.h \ mld_threads.h \ - usrp_prims_libusb0.cc \ - usrp_basic_libusb0.cc + usrp_prims_libusb0.cc win32_CODE = \ fusb_win32.cc \ fusb_sysconfig_win32.cc \ - usrp_prims_libusb0.cc \ - usrp_basic_libusb0.cc + usrp_prims_libusb0.cc linux_CODE = \ fusb_linux.cc \ fusb_sysconfig_linux.cc \ - usrp_prims_libusb0.cc \ - usrp_basic_libusb0.cc + usrp_prims_libusb0.cc ra_wb_CODE = \ fusb_ra_wb.cc \ fusb_sysconfig_ra_wb.cc \ - usrp_prims_libusb0.cc \ - usrp_basic_libusb0.cc + usrp_prims_libusb0.cc libusb1_CODE = \ fusb_libusb1.cc \ fusb_sysconfig_libusb1.cc \ - usrp_prims_libusb1.cc \ - usrp_basic_libusb1.cc + usrp_prims_libusb1.cc # # include each _CODE entry here... @@ -116,14 +110,14 @@ EXTRA_libusrp_la_SOURCES = \ libusrp_la_common_SOURCES = \ fusb.cc \ md5.c \ - usrp_basic_common.cc \ + usrp_basic.cc \ usrp_config.cc \ usrp_dbid.cc \ usrp_local_sighandler.cc \ usrp_prims_common.cc \ usrp_standard.cc \ - db_wbxng_adf4350.cc \ - db_wbxng_adf4350_regs.cc \ + db_wbxng_adf4350.cc \ + db_wbxng_adf4350_regs.cc \ db_boards.cc \ db_base.cc \ db_basic.cc \ diff --git a/usrp/host/lib/usrp_basic_common.cc b/usrp/host/lib/usrp_basic.cc similarity index 93% rename from usrp/host/lib/usrp_basic_common.cc rename to usrp/host/lib/usrp_basic.cc index 721301de..5b2f7ff7 100644 --- a/usrp/host/lib/usrp_basic_common.cc +++ b/usrp/host/lib/usrp_basic.cc @@ -74,14 +74,12 @@ open_tx_interface (libusb_device *dev) return udh; } - ////////////////////////////////////////////////////////////////// // // usrp_basic // //////////////////////////////////////////////////////////////// - // Given: // CLKIN = 64 MHz // CLKSEL pin = high @@ -101,6 +99,71 @@ static unsigned char common_regs[] = { REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4 }; +usrp_basic::usrp_basic (int which_board, + libusb_device_handle * + open_interface (libusb_device *dev), + const std::string fpga_filename, + const std::string firmware_filename) + : d_udh (0), d_ctx (0), + d_usb_data_rate (16000000), // SWAG, see below + d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)), + d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2) +{ + /* + * SWAG: Scientific Wild Ass Guess. + * + * d_usb_data_rate is used only to determine how often to poll for over- and + * under-runs. We defualt it to 1/2 of our best case. Classes derived from + * usrp_basic (e.g., usrp_standard_tx and usrp_standard_rx) call + * set_usb_data_rate() to tell us the actual rate. This doesn't change our + * throughput, that's determined by the signal processing code in the FPGA + * (which we know nothing about), and the system limits determined by libusb, + * fusb_*, and the underlying drivers. + */ + memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows)); + + usrp_one_time_init (&d_ctx); + + if (!usrp_load_standard_bits (which_board, false, fpga_filename, + firmware_filename, d_ctx)) + throw std::runtime_error ("usrp_basic/usrp_load_standard_bits"); + + libusb_device *dev = usrp_find_device (which_board, false, d_ctx); + if (dev == 0){ + fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board); + throw std::runtime_error ("usrp_basic/usrp_find_device"); + } + + if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){ + fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n"); + throw std::runtime_error ("usrp_basic/bad_rev"); + } + + if ((d_udh = open_interface (dev)) == 0) + throw std::runtime_error ("usrp_basic/open_interface"); + + // initialize registers that are common to rx and tx + + if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))) { + fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n"); + throw std::runtime_error ("usrp_basic/init_9862"); + } + + _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode + _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs + +} + +usrp_basic::~usrp_basic () +{ + d_db.resize(0); // forget db shared ptrs + + if (d_udh) + usrp_close_interface (d_udh); + + usrp_deinit (d_ctx); +} + void usrp_basic::shutdown_daughterboards() { diff --git a/usrp/host/lib/usrp_basic_libusb0.cc b/usrp/host/lib/usrp_basic_libusb0.cc deleted file mode 100644 index 21748058..00000000 --- a/usrp/host/lib/usrp_basic_libusb0.cc +++ /dev/null @@ -1,137 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2003,2004,2008,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, - * Boston, MA 02110-1301, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include "usrp/usrp_prims.h" -#include "usrp_interfaces.h" -#include "fpga_regs_common.h" -#include "fpga_regs_standard.h" -#include "fusb.h" -#include "db_boards.h" -#include -#include -#include -#include -#include -#include -#include - -using namespace ad9862; - -#define NELEM(x) (sizeof (x) / sizeof (x[0])) - - -static const double POLLING_INTERVAL = 0.1; // seconds - - -////////////////////////////////////////////////////////////////// -// -// usrp_basic -// -//////////////////////////////////////////////////////////////// - - -// Given: -// CLKIN = 64 MHz -// CLKSEL pin = high -// -// CLKOUT1 = CLKIN = 64 MHz -// CLKOUT2 = CLKIN = 64 MHz -// ADC is clocked at 64 MHz -// DAC is clocked at 128 MHz - -static unsigned char common_regs[] = { - REG_GENERAL, 0, - REG_DLL, (DLL_DISABLE_INTERNAL_XTAL_OSC - | DLL_MULT_2X - | DLL_FAST), - REG_CLKOUT, CLKOUT2_EQ_DLL_OVER_2, - REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4 -}; - - -usrp_basic::usrp_basic (int which_board, - struct usb_dev_handle * - open_interface (struct usb_device *dev), - const std::string fpga_filename, - const std::string firmware_filename) - : d_udh (0), - d_usb_data_rate (16000000), // SWAG, see below - d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)), - d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2) -{ - /* - * SWAG: Scientific Wild Ass Guess. - * - * d_usb_data_rate is used only to determine how often to poll for over- and under-runs. - * We defualt it to 1/2 of our best case. Classes derived from usrp_basic (e.g., - * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the - * actual rate. This doesn't change our throughput, that's determined by the signal - * processing code in the FPGA (which we know nothing about), and the system limits - * determined by libusb, fusb_*, and the underlying drivers. - */ - memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows)); - - usrp_one_time_init (); - - if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename)) - throw std::runtime_error ("usrp_basic/usrp_load_standard_bits"); - - struct usb_device *dev = usrp_find_device (which_board); - if (dev == 0){ - fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board); - throw std::runtime_error ("usrp_basic/usrp_find_device"); - } - - if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){ - fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n"); - throw std::runtime_error ("usrp_basic/bad_rev"); - } - - if ((d_udh = open_interface (dev)) == 0) - throw std::runtime_error ("usrp_basic/open_interface"); - - // initialize registers that are common to rx and tx - - if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){ - fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n"); - throw std::runtime_error ("usrp_basic/init_9862"); - } - - _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode - _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs -} - -usrp_basic::~usrp_basic () -{ - // shutdown_daughterboards(); // call from ~usrp_basic_{tx,rx} - - d_db.resize(0); // forget db shared ptrs - - if (d_udh) - usb_close (d_udh); -} - diff --git a/usrp/host/lib/usrp_basic_libusb1.cc b/usrp/host/lib/usrp_basic_libusb1.cc deleted file mode 100644 index 35009dc6..00000000 --- a/usrp/host/lib/usrp_basic_libusb1.cc +++ /dev/null @@ -1,145 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2003,2004,2008,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, - * Boston, MA 02110-1301, USA. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include -#include "usrp/usrp_prims.h" -#include "usrp_interfaces.h" -#include "fpga_regs_common.h" -#include "fpga_regs_standard.h" -#include "fusb.h" -#include "db_boards.h" -#include -#include -#include -#include -#include -#include -#include - - -using namespace ad9862; - -#define NELEM(x) (sizeof (x) / sizeof (x[0])) - - -static const double POLLING_INTERVAL = 0.1; // seconds - - -////////////////////////////////////////////////////////////////// -// -// usrp_basic -// -//////////////////////////////////////////////////////////////// - - -// Given: -// CLKIN = 64 MHz -// CLKSEL pin = high -// -// These settings give us: -// CLKOUT1 = CLKIN = 64 MHz -// CLKOUT2 = CLKIN = 64 MHz -// ADC is clocked at 64 MHz -// DAC is clocked at 128 MHz - -static unsigned char common_regs[] = { - REG_GENERAL, 0, - REG_DLL, (DLL_DISABLE_INTERNAL_XTAL_OSC - | DLL_MULT_2X - | DLL_FAST), - REG_CLKOUT, CLKOUT2_EQ_DLL_OVER_2, - REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4 -}; - -usrp_basic::usrp_basic (int which_board, - struct libusb_device_handle * - open_interface (struct libusb_device *dev), - const std::string fpga_filename, - const std::string firmware_filename) - : d_udh (0), d_ctx (0), - d_usb_data_rate (16000000), // SWAG, see below - d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)), - d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2) -{ - /* - * SWAG: Scientific Wild Ass Guess. - * - * d_usb_data_rate is used only to determine how often to poll for over- and under-runs. - * We defualt it to 1/2 of our best case. Classes derived from usrp_basic (e.g., - * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the - * actual rate. This doesn't change our throughput, that's determined by the signal - * processing code in the FPGA (which we know nothing about), and the system limits - * determined by libusb, fusb_*, and the underlying drivers. - */ - memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows)); - - usrp_one_time_init (&d_ctx); - - if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename, d_ctx)) - throw std::runtime_error ("usrp_basic/usrp_load_standard_bits"); - - struct libusb_device *dev = usrp_find_device (which_board, false, d_ctx); - if (dev == 0){ - fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board); - throw std::runtime_error ("usrp_basic/usrp_find_device"); - } - - if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){ - fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n"); - throw std::runtime_error ("usrp_basic/bad_rev"); - } - - if ((d_udh = open_interface (dev)) == 0) - throw std::runtime_error ("usrp_basic/open_interface"); - - // initialize registers that are common to rx and tx - - if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){ - fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n"); - throw std::runtime_error ("usrp_basic/init_9862"); - } - - _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode - _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs - -} - -usrp_basic::~usrp_basic () -{ - // shutdown_daughterboards(); // call from ~usrp_basic_{tx,rx} - - d_db.resize(0); // forget db shared ptrs - - if (d_udh) - libusb_close (d_udh); - - // Each object _should_ be running in its own context. If running in default - // context then leave the instance open as it may be shared. - - if (d_ctx != NULL) - libusb_exit (d_ctx); -} - diff --git a/usrp/host/lib/usrp_prims_libusb0.cc b/usrp/host/lib/usrp_prims_libusb0.cc index 7053786d..4d1fd78c 100644 --- a/usrp/host/lib/usrp_prims_libusb0.cc +++ b/usrp/host/lib/usrp_prims_libusb0.cc @@ -105,6 +105,12 @@ usrp_one_time_init (libusb_context **ctx) } } +void +usrp_deinit (libusb_context *ctx) +{ + // nop +} + void usrp_rescan () { diff --git a/usrp/host/lib/usrp_prims_libusb1.cc b/usrp/host/lib/usrp_prims_libusb1.cc index fdd497ab..5dfe416e 100644 --- a/usrp/host/lib/usrp_prims_libusb1.cc +++ b/usrp/host/lib/usrp_prims_libusb1.cc @@ -135,6 +135,7 @@ _usb_control_transfer (struct libusb_device_handle *udh, int request_type, return ret; } + // ---------------------------------------------------------------- @@ -154,13 +155,21 @@ usrp_one_time_init (libusb_context **ctx) libusb_set_debug(*ctx, 3); } +void +usrp_deinit (struct libusb_context *ctx) +{ + // Each object _should_ be running in its own context. If running in default + // context then leave the instance open as it may be shared. + if (ctx != NULL) + libusb_exit (ctx); +} + void usrp_rescan () { // nop } - struct libusb_device * usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx) { -- 2.30.2