Added libusb1 specific usrp_prims and usrp_basic
[debian/gnuradio] / usrp / host / lib / usrp_basic_libusb1.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2004,2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <usrp/usrp_basic.h>
28 #include "usrp/usrp_prims.h"
29 #include "usrp_interfaces.h"
30 #include "fpga_regs_common.h"
31 #include "fpga_regs_standard.h"
32 #include "fusb.h"
33 #include "db_boards.h"
34 #include <libusb-1.0/libusb.h>
35 #include <stdexcept>
36 #include <assert.h>
37 #include <math.h>
38 #include <ad9862.h>
39 #include <string.h>
40 #include <cstdio>
41
42
43 using namespace ad9862;
44
45 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
46
47 // These set the buffer size used for each end point using the fast
48 // usb interface.  The kernel ends up locking down this much memory.
49
50 static const int FUSB_BUFFER_SIZE = fusb_sysconfig::default_buffer_size();
51 static const int FUSB_BLOCK_SIZE = fusb_sysconfig::max_block_size();
52 static const int FUSB_NBLOCKS    = FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE;
53
54
55 static const double POLLING_INTERVAL = 0.1;     // seconds
56
57
58 //////////////////////////////////////////////////////////////////
59 //
60 //                      usrp_basic
61 //
62 ////////////////////////////////////////////////////////////////
63
64
65 // Given:
66 //   CLKIN = 64 MHz
67 //   CLKSEL pin = high 
68 //
69 // These settings give us:
70 //   CLKOUT1 = CLKIN = 64 MHz
71 //   CLKOUT2 = CLKIN = 64 MHz
72 //   ADC is clocked at  64 MHz
73 //   DAC is clocked at 128 MHz
74
75 static unsigned char common_regs[] = {
76   REG_GENERAL,          0,
77   REG_DLL,              (DLL_DISABLE_INTERNAL_XTAL_OSC
78                          | DLL_MULT_2X
79                          | DLL_FAST),
80   REG_CLKOUT,           CLKOUT2_EQ_DLL_OVER_2,
81   REG_AUX_ADC_CLK,      AUX_ADC_CLK_CLK_OVER_4
82 };
83
84 usrp_basic::usrp_basic (int which_board, 
85                         struct libusb_device_handle *
86                         open_interface (struct libusb_device *dev),
87                         const std::string fpga_filename,
88                         const std::string firmware_filename)
89   : d_udh (0), d_ctx (0),
90     d_usb_data_rate (16000000), // SWAG, see below
91     d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)),
92     d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2)
93 {
94   /*
95    * SWAG: Scientific Wild Ass Guess.
96    *
97    * d_usb_data_rate is used only to determine how often to poll for over- and under-runs.
98    * We defualt it to 1/2  of our best case.  Classes derived from usrp_basic (e.g., 
99    * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the
100    * actual rate.  This doesn't change our throughput, that's determined by the signal
101    * processing code in the FPGA (which we know nothing about), and the system limits
102    * determined by libusb, fusb_*, and the underlying drivers.
103    */
104   memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows));
105
106   d_ctx = usrp_one_time_init(true);
107
108   if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename, d_ctx))
109     throw std::runtime_error ("usrp_basic/usrp_load_standard_bits");
110
111   struct libusb_device *dev = usrp_find_device (which_board, false, d_ctx);
112   if (dev == 0){
113     fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board);
114     throw std::runtime_error ("usrp_basic/usrp_find_device");
115   }
116
117   if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){
118     fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n");
119     throw std::runtime_error ("usrp_basic/bad_rev");
120   }
121
122   if ((d_udh = open_interface (dev)) == 0)
123     throw std::runtime_error ("usrp_basic/open_interface");
124
125   // initialize registers that are common to rx and tx
126
127   if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){
128     fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n");
129     throw std::runtime_error ("usrp_basic/init_9862");
130   }
131
132   _write_fpga_reg (FR_MODE, 0);         // ensure we're in normal mode
133   _write_fpga_reg (FR_DEBUG_EN, 0);     // disable debug outputs
134
135 }
136
137 usrp_basic::~usrp_basic ()
138 {
139   // shutdown_daughterboards();         // call from ~usrp_basic_{tx,rx}
140
141   d_db.resize(0); // forget db shared ptrs
142
143   if (d_udh)
144     libusb_close (d_udh);
145
146   // Each object should be running in it's own context. If running in default
147   // (NULL) context then something went wrong. 
148
149   assert (d_ctx != NULL);
150   libusb_exit (d_ctx);
151 }
152