Applied libusb-1.0 patch set from Thomas Tsou <ttsou@vt.edu>:
[debian/gnuradio] / usrp / host / lib / usrp_basic.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 using namespace ad9862;
43
44 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
45
46 // These set the buffer size used for each end point using the fast
47 // usb interface.  The kernel ends up locking down this much memory.
48
49 static const int FUSB_BUFFER_SIZE = fusb_sysconfig::default_buffer_size();
50 static const int FUSB_BLOCK_SIZE = fusb_sysconfig::max_block_size();
51 static const int FUSB_NBLOCKS    = FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE;
52
53
54 static const double POLLING_INTERVAL = 0.1;     // seconds
55
56 ////////////////////////////////////////////////////////////////
57
58 static struct libusb_device_handle *
59 open_rx_interface (struct libusb_device *dev)
60 {
61   struct libusb_device_handle *udh = usrp_open_rx_interface (dev);
62   if (udh == 0){
63     fprintf (stderr, "usrp_basic_rx: can't open rx interface\n");
64   }
65   return udh;
66 }
67
68 static struct libusb_device_handle *
69 open_tx_interface (struct libusb_device *dev)
70 {
71   struct libusb_device_handle *udh = usrp_open_tx_interface (dev);
72   if (udh == 0){
73     fprintf (stderr, "usrp_basic_tx: can't open tx interface\n");
74   }
75   return udh;
76 }
77
78
79 //////////////////////////////////////////////////////////////////
80 //
81 //                      usrp_basic
82 //
83 ////////////////////////////////////////////////////////////////
84
85
86 // Given:
87 //   CLKIN = 64 MHz
88 //   CLKSEL pin = high 
89 //
90 // These settings give us:
91 //   CLKOUT1 = CLKIN = 64 MHz
92 //   CLKOUT2 = CLKIN = 64 MHz
93 //   ADC is clocked at  64 MHz
94 //   DAC is clocked at 128 MHz
95
96 static unsigned char common_regs[] = {
97   REG_GENERAL,          0,
98   REG_DLL,              (DLL_DISABLE_INTERNAL_XTAL_OSC
99                          | DLL_MULT_2X
100                          | DLL_FAST),
101   REG_CLKOUT,           CLKOUT2_EQ_DLL_OVER_2,
102   REG_AUX_ADC_CLK,      AUX_ADC_CLK_CLK_OVER_4
103 };
104
105
106 usrp_basic::usrp_basic (int which_board, 
107                         struct libusb_device_handle *
108                         open_interface (struct libusb_device *dev),
109                         const std::string fpga_filename,
110                         const std::string firmware_filename)
111   : d_udh (0),
112     d_usb_data_rate (16000000), // SWAG, see below
113     d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)),
114     d_verbose (false), d_fpga_master_clock_freq(64000000), d_db(2)
115 {
116   /*
117    * SWAG: Scientific Wild Ass Guess.
118    *
119    * d_usb_data_rate is used only to determine how often to poll for over- and under-runs.
120    * We defualt it to 1/2  of our best case.  Classes derived from usrp_basic (e.g., 
121    * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the
122    * actual rate.  This doesn't change our throughput, that's determined by the signal
123    * processing code in the FPGA (which we know nothing about), and the system limits
124    * determined by libusb, fusb_*, and the underlying drivers.
125    */
126   memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows));
127
128   usrp_one_time_init ();
129
130   if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename))
131     throw std::runtime_error ("usrp_basic/usrp_load_standard_bits");
132
133   struct libusb_device *dev = usrp_find_device (which_board);
134   if (dev == 0){
135     fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board);
136     throw std::runtime_error ("usrp_basic/usrp_find_device");
137   }
138
139   if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){
140     fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n");
141     throw std::runtime_error ("usrp_basic/bad_rev");
142   }
143
144   if ((d_udh = open_interface (dev)) == 0)
145     throw std::runtime_error ("usrp_basic/open_interface");
146
147   // initialize registers that are common to rx and tx
148
149   if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){
150     fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n");
151     throw std::runtime_error ("usrp_basic/init_9862");
152   }
153
154   _write_fpga_reg (FR_MODE, 0);         // ensure we're in normal mode
155   _write_fpga_reg (FR_DEBUG_EN, 0);     // disable debug outputs
156 }
157
158 void
159 usrp_basic::shutdown_daughterboards()
160 {
161   // nuke d'boards before we close down USB in ~usrp_basic
162   // shutdown() will do any board shutdown while the USRP can still
163   // be talked to
164   for(size_t i = 0; i < d_db.size(); i++) 
165     for(size_t j = 0; j < d_db[i].size(); j++) 
166       d_db[i][j]->shutdown();
167 }
168
169 usrp_basic::~usrp_basic ()
170 {
171   // shutdown_daughterboards();         // call from ~usrp_basic_{tx,rx}
172
173   d_db.resize(0); // forget db shared ptrs
174
175   if (d_udh)
176     libusb_close (d_udh);
177
178   // There's no reference count on the number of times libusb is initialized.
179   // libusb_init can be called multiple times, but libusb_exit shuts down
180   // everything. Leave libusb running for now. Need to add a count so that it
181   // exits nicely. 
182
183   //libusb_exit (NULL);
184 }
185
186 void
187 usrp_basic::init_db(usrp_basic_sptr u)
188 {
189   if (u.get() != this)
190     throw std::invalid_argument("u is not this");
191
192   d_db[0] = instantiate_dbs(d_dbid[0], u, 0);
193   d_db[1] = instantiate_dbs(d_dbid[1], u, 1);
194 }
195
196 std::vector<db_base_sptr> 
197 usrp_basic::db(int which_side)
198 {
199   which_side &= 0x1;    // clamp it to avoid any reporting any errors
200   return d_db[which_side];
201 }
202
203 bool
204 usrp_basic::is_valid(const usrp_subdev_spec &ss)
205 {
206   if (ss.side < 0 || ss.side > 1)
207     return false;
208
209   if (ss.subdev < 0 || ss.subdev >= d_db[ss.side].size())
210     return false;
211
212   return true;
213 }
214
215 db_base_sptr
216 usrp_basic::selected_subdev(const usrp_subdev_spec &ss)
217 {
218   if (!is_valid(ss))
219     throw std::invalid_argument("invalid subdev_spec");
220
221   return d_db[ss.side][ss.subdev];
222 }
223
224 bool
225 usrp_basic::start ()
226 {
227   return true;          // nop
228 }
229
230 bool
231 usrp_basic::stop ()
232 {
233   return true;          // nop
234 }
235
236 void
237 usrp_basic::set_usb_data_rate (int usb_data_rate)
238 {
239   d_usb_data_rate = usb_data_rate;
240   d_bytes_per_poll = (int) (usb_data_rate * POLLING_INTERVAL);
241 }
242
243 bool
244 usrp_basic::_write_aux_dac (int slot, int which_dac, int value)
245 {
246   return usrp_write_aux_dac (d_udh, slot, which_dac, value);
247 }
248
249 bool
250 usrp_basic::_read_aux_adc (int slot, int which_adc, int *value)
251 {
252   return usrp_read_aux_adc (d_udh, slot, which_adc, value);
253 }
254
255 int
256 usrp_basic::_read_aux_adc (int slot, int which_adc)
257 {
258   int   value;
259   if (!_read_aux_adc (slot, which_adc, &value))
260     return READ_FAILED;
261
262   return value;
263 }
264
265 bool
266 usrp_basic::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
267 {
268   return usrp_eeprom_write (d_udh, i2c_addr, eeprom_offset, buf.data (), buf.size ());
269 }
270
271 std::string
272 usrp_basic::read_eeprom (int i2c_addr, int eeprom_offset, int len)
273 {
274   if (len <= 0)
275     return "";
276
277   char buf[len];
278
279   if (!usrp_eeprom_read (d_udh, i2c_addr, eeprom_offset, buf, len))
280     return "";
281
282   return std::string (buf, len);
283 }
284
285 bool
286 usrp_basic::write_i2c (int i2c_addr, const std::string buf)
287 {
288   return usrp_i2c_write (d_udh, i2c_addr, buf.data (), buf.size ());
289 }
290
291 std::string
292 usrp_basic::read_i2c (int i2c_addr, int len)
293 {
294   if (len <= 0)
295     return "";
296
297   char buf[len];
298
299   if (!usrp_i2c_read (d_udh, i2c_addr, buf, len))
300     return "";
301
302   return std::string (buf, len);
303 }
304
305 std::string
306 usrp_basic::serial_number()
307 {
308   return usrp_serial_number(d_udh);
309 }
310
311 // ----------------------------------------------------------------
312
313 bool
314 usrp_basic::set_adc_offset (int which_adc, int offset)
315 {
316   if (which_adc < 0 || which_adc > 3)
317     return false;
318
319   return _write_fpga_reg (FR_ADC_OFFSET_0 + which_adc, offset);
320 }
321
322 bool
323 usrp_basic::set_dac_offset (int which_dac, int offset, int offset_pin)
324 {
325   if (which_dac < 0 || which_dac > 3)
326     return false;
327
328   int which_codec = which_dac >> 1;
329   int tx_a = (which_dac & 0x1) == 0;
330   int lo = ((offset & 0x3) << 6) | (offset_pin & 0x1);
331   int hi = (offset >> 2);
332   bool ok;
333
334   if (tx_a){
335     ok =  _write_9862 (which_codec, REG_TX_A_OFFSET_LO, lo);
336     ok &= _write_9862 (which_codec, REG_TX_A_OFFSET_HI, hi);
337   }
338   else {
339     ok =  _write_9862 (which_codec, REG_TX_B_OFFSET_LO, lo);
340     ok &= _write_9862 (which_codec, REG_TX_B_OFFSET_HI, hi);
341   }
342   return ok;
343 }
344
345 bool
346 usrp_basic::set_adc_buffer_bypass (int which_adc, bool bypass)
347 {
348   if (which_adc < 0 || which_adc > 3)
349     return false;
350
351   int codec = which_adc >> 1;
352   int reg = (which_adc & 1) == 0 ? REG_RX_A : REG_RX_B;
353
354   unsigned char cur_rx;
355   unsigned char cur_pwr_dn;
356
357   // If the input buffer is bypassed, we need to power it down too.
358
359   bool ok = _read_9862 (codec, reg, &cur_rx);
360   ok &= _read_9862 (codec, REG_RX_PWR_DN, &cur_pwr_dn);
361   if (!ok)
362     return false;
363
364   if (bypass){
365     cur_rx |= RX_X_BYPASS_INPUT_BUFFER;
366     cur_pwr_dn |= ((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B;
367   }
368   else {
369     cur_rx &= ~RX_X_BYPASS_INPUT_BUFFER;
370     cur_pwr_dn &= ~(((which_adc & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B);
371   }
372
373   ok &= _write_9862 (codec, reg, cur_rx);
374   ok &= _write_9862 (codec, REG_RX_PWR_DN, cur_pwr_dn);
375   return ok;
376 }
377
378 bool
379 usrp_basic::set_dc_offset_cl_enable(int bits, int mask)
380 {
381   return _write_fpga_reg(FR_DC_OFFSET_CL_EN, 
382                          (d_fpga_shadows[FR_DC_OFFSET_CL_EN] & ~mask) | (bits & mask));
383 }
384
385 // ----------------------------------------------------------------
386
387 bool
388 usrp_basic::_write_fpga_reg (int regno, int value)
389 {
390   if (d_verbose){
391     fprintf (stdout, "_write_fpga_reg(%3d, 0x%08x)\n", regno, value);
392     fflush (stdout);
393   }
394
395   if (regno >= 0 && regno < MAX_REGS)
396     d_fpga_shadows[regno] = value;
397
398   return usrp_write_fpga_reg (d_udh, regno, value);
399 }
400
401 bool
402 usrp_basic::_write_fpga_reg_masked (int regno, int value, int mask)
403 {
404   //Only use this for registers who actually use a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
405   //value is a 16 bits value and mask is a 16 bits mask
406   if (d_verbose){
407     fprintf (stdout, "_write_fpga_reg_masked(%3d, 0x%04x,0x%04x)\n", regno, value, mask);
408     fflush (stdout);
409   }
410
411   if (regno >= 0 && regno < MAX_REGS)
412     d_fpga_shadows[regno] = value;
413
414   return usrp_write_fpga_reg (d_udh, regno, (value & 0xffff) | ((mask & 0xffff)<<16));
415 }
416
417
418 bool
419 usrp_basic::_read_fpga_reg (int regno, int *value)
420 {
421   return usrp_read_fpga_reg (d_udh, regno, value);
422 }
423
424 int
425 usrp_basic::_read_fpga_reg (int regno)
426 {
427   int value;
428   if (!_read_fpga_reg (regno, &value))
429     return READ_FAILED;
430   return value;
431 }
432
433 bool
434 usrp_basic::_write_9862 (int which_codec, int regno, unsigned char value)
435 {
436   if (0 && d_verbose){
437     // FIXME really want to enable logging in usrp_prims:usrp_9862_write
438     fprintf(stdout, "_write_9862(codec = %d, regno = %2d, val = 0x%02x)\n", which_codec, regno, value);
439     fflush(stdout);
440   }
441
442   return usrp_9862_write (d_udh, which_codec, regno, value);
443 }
444
445
446 bool
447 usrp_basic::_read_9862 (int which_codec, int regno, unsigned char *value) const
448 {
449   return usrp_9862_read (d_udh, which_codec, regno, value);
450 }
451
452 int
453 usrp_basic::_read_9862 (int which_codec, int regno) const
454 {
455   unsigned char value;
456   if (!_read_9862 (which_codec, regno, &value))
457     return READ_FAILED;
458   return value;
459 }
460
461 bool
462 usrp_basic::_write_spi (int optional_header, int enables, int format, std::string buf)
463 {
464   return usrp_spi_write (d_udh, optional_header, enables, format,
465                          buf.data(), buf.size());
466 }
467
468 std::string
469 usrp_basic::_read_spi (int optional_header, int enables, int format, int len)
470 {
471   if (len <= 0)
472     return "";
473   
474   char buf[len];
475
476   if (!usrp_spi_read (d_udh, optional_header, enables, format, buf, len))
477     return "";
478
479   return std::string (buf, len);
480 }
481
482
483 bool
484 usrp_basic::_set_led (int which_led, bool on)
485 {
486   return usrp_set_led (d_udh, which_led, on);
487 }
488
489 bool
490 usrp_basic::write_atr_tx_delay(int value)
491 {
492   return _write_fpga_reg(FR_ATR_TX_DELAY, value);
493 }
494
495 bool
496 usrp_basic::write_atr_rx_delay(int value)
497 {
498   return _write_fpga_reg(FR_ATR_RX_DELAY, value);
499 }
500
501 /*
502  * ----------------------------------------------------------------
503  * Routines to access and control daughterboard specific i/o
504  * ----------------------------------------------------------------
505  */
506 static int
507 slot_id_to_oe_reg (int slot_id)
508 {
509   static int reg[4]  = { FR_OE_0, FR_OE_1, FR_OE_2, FR_OE_3 };
510   assert (0 <= slot_id && slot_id < 4);
511   return reg[slot_id];
512 }
513
514 static int
515 slot_id_to_io_reg (int slot_id)
516 {
517   static int reg[4]  = { FR_IO_0, FR_IO_1, FR_IO_2, FR_IO_3 };
518   assert (0 <= slot_id && slot_id < 4);
519   return reg[slot_id];
520 }
521
522 static int
523 slot_id_to_refclk_reg(int slot_id)
524 {
525   static int reg[4]  = { FR_TX_A_REFCLK, FR_RX_A_REFCLK, FR_TX_B_REFCLK, FR_RX_B_REFCLK };
526   assert (0 <= slot_id && slot_id < 4);
527   return reg[slot_id];
528 }
529
530 static int
531 slot_id_to_atr_mask_reg(int slot_id)
532 {
533   static int reg[4]  = { FR_ATR_MASK_0, FR_ATR_MASK_1, FR_ATR_MASK_2, FR_ATR_MASK_3 };
534   assert (0 <= slot_id && slot_id < 4);
535   return reg[slot_id];
536 }
537
538 static int
539 slot_id_to_atr_txval_reg(int slot_id)
540 {
541   static int reg[4]  = { FR_ATR_TXVAL_0, FR_ATR_TXVAL_1, FR_ATR_TXVAL_2, FR_ATR_TXVAL_3 };
542   assert (0 <= slot_id && slot_id < 4);
543   return reg[slot_id];
544 }
545
546 static int
547 slot_id_to_atr_rxval_reg(int slot_id)
548 {
549   static int reg[4]  = { FR_ATR_RXVAL_0, FR_ATR_RXVAL_1, FR_ATR_RXVAL_2, FR_ATR_RXVAL_3 };
550   assert (0 <= slot_id && slot_id < 4);
551   return reg[slot_id];
552 }
553
554 static int
555 to_slot(txrx_t txrx, int which_side)
556 {
557   // TX_A = 0
558   // RX_A = 1
559   // TX_B = 2
560   // RX_B = 3
561   return ((which_side & 0x1) << 1) | ((txrx & 0x1) == C_RX);
562 }
563
564 bool
565 usrp_basic::common_set_pga(txrx_t txrx, int which_amp, double gain)
566 {
567   if (which_amp < 0 || which_amp > 3)
568     return false;
569
570   gain = std::min(common_pga_max(txrx),
571                   std::max(common_pga_min(txrx), gain));
572
573   int codec = which_amp >> 1;   
574   int int_gain = (int) rint((gain - common_pga_min(txrx)) / common_pga_db_per_step(txrx));
575
576   if (txrx == C_TX){            // 0 and 1 are same, as are 2 and 3
577     return _write_9862(codec, REG_TX_PGA, int_gain);
578   }
579   else {
580     int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;
581
582     // read current value to get input buffer bypass flag.
583     unsigned char cur_rx;
584     if (!_read_9862(codec, reg, &cur_rx))
585       return false;
586
587     cur_rx = (cur_rx & RX_X_BYPASS_INPUT_BUFFER) | (int_gain & 0x7f);
588     return _write_9862(codec, reg, cur_rx);
589   }
590 }
591
592 double
593 usrp_basic::common_pga(txrx_t txrx, int which_amp) const
594 {
595   if (which_amp < 0 || which_amp > 3)
596     return READ_FAILED;
597
598   if (txrx == C_TX){
599     int codec = which_amp >> 1;
600     unsigned char v;
601     bool ok = _read_9862 (codec, REG_TX_PGA, &v);
602     if (!ok)
603       return READ_FAILED;
604
605     return (pga_db_per_step() * v) + pga_min();
606   }
607   else {
608     int codec = which_amp >> 1;
609     int reg = (which_amp & 1) == 0 ? REG_RX_A : REG_RX_B;
610     unsigned char v;
611     bool ok = _read_9862 (codec, reg, &v);
612     if (!ok)
613       return READ_FAILED;
614
615     return (pga_db_per_step() * (v & 0x1f)) + pga_min();
616   }
617 }
618
619 double
620 usrp_basic::common_pga_min(txrx_t txrx) const
621 {
622   if (txrx == C_TX)
623     return -20.0;
624   else
625     return   0.0;
626 }
627
628 double
629 usrp_basic::common_pga_max(txrx_t txrx) const
630 {
631   if (txrx == C_TX)
632     return   0.0;
633   else
634     return  20.0;
635 }
636
637 double
638 usrp_basic::common_pga_db_per_step(txrx_t txrx) const
639 {
640   if (txrx == C_TX)
641     return  20.0 / 255;
642   else
643     return  20.0 / 20;
644 }
645
646 bool
647 usrp_basic::_common_write_oe(txrx_t txrx, int which_side, int value, int mask)
648 {
649   if (! (0 <= which_side && which_side <= 1))
650     return false;
651
652   return _write_fpga_reg(slot_id_to_oe_reg(to_slot(txrx, which_side)),
653                          (mask << 16) | (value & 0xffff));
654 }
655
656 bool
657 usrp_basic::common_write_io(txrx_t txrx, int which_side, int value, int mask)
658 {
659   if (! (0 <= which_side && which_side <= 1))
660     return false;
661
662   return _write_fpga_reg(slot_id_to_io_reg(to_slot(txrx, which_side)),
663                          (mask << 16) | (value & 0xffff));
664 }
665
666 bool
667 usrp_basic::common_read_io(txrx_t txrx, int which_side, int *value)
668 {
669   if (! (0 <= which_side && which_side <= 1))
670     return false;
671
672   int t;
673   int reg = which_side + 1;     // FIXME, *very* magic number (fix in serial_io.v)
674   bool ok = _read_fpga_reg(reg, &t);
675   if (!ok)
676     return false;
677
678   if (txrx == C_TX){
679     *value = t & 0xffff;                // FIXME, more magic
680     return true;
681   }
682   else {
683     *value = (t >> 16) & 0xffff;        // FIXME, more magic
684     return true;
685   }
686 }
687
688 int
689 usrp_basic::common_read_io(txrx_t txrx, int which_side)
690 {
691   int   value;
692   if (!common_read_io(txrx, which_side, &value))
693     return READ_FAILED;
694   return value;
695 }
696
697 bool
698 usrp_basic::common_write_refclk(txrx_t txrx, int which_side, int value)
699 {
700   if (! (0 <= which_side && which_side <= 1))
701     return false;
702
703   return _write_fpga_reg(slot_id_to_refclk_reg(to_slot(txrx, which_side)),
704                          value);
705 }
706
707 bool
708 usrp_basic::common_write_atr_mask(txrx_t txrx, int which_side, int value)
709 {
710   if (! (0 <= which_side && which_side <= 1))
711     return false;
712
713   return _write_fpga_reg(slot_id_to_atr_mask_reg(to_slot(txrx, which_side)),
714                          value);
715 }
716
717 bool
718 usrp_basic::common_write_atr_txval(txrx_t txrx, int which_side, int value)
719 {
720   if (! (0 <= which_side && which_side <= 1))
721     return false;
722
723   return _write_fpga_reg(slot_id_to_atr_txval_reg(to_slot(txrx, which_side)),
724                          value);
725 }
726
727 bool
728 usrp_basic::common_write_atr_rxval(txrx_t txrx, int which_side, int value)
729 {
730   if (! (0 <= which_side && which_side <= 1))
731     return false;
732
733   return _write_fpga_reg(slot_id_to_atr_rxval_reg(to_slot(txrx, which_side)),
734                          value);
735 }
736
737 bool
738 usrp_basic::common_write_aux_dac(txrx_t txrx, int which_side, int which_dac, int value)
739 {
740   return _write_aux_dac(to_slot(txrx, which_side), which_dac, value);
741 }
742
743 bool
744 usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc, int *value)
745 {
746   return _read_aux_adc(to_slot(txrx, which_side), which_adc, value);
747 }
748
749 int
750 usrp_basic::common_read_aux_adc(txrx_t txrx, int which_side, int which_adc)
751 {
752   return _read_aux_adc(to_slot(txrx, which_side), which_adc);
753 }
754
755
756 ////////////////////////////////////////////////////////////////
757 //
758 //                         usrp_basic_rx
759 //
760 ////////////////////////////////////////////////////////////////
761
762 static unsigned char rx_init_regs[] = {
763   REG_RX_PWR_DN,        0,
764   REG_RX_A,             0,      // minimum gain = 0x00 (max gain = 0x14)
765   REG_RX_B,             0,      // minimum gain = 0x00 (max gain = 0x14)
766   REG_RX_MISC,          (RX_MISC_HS_DUTY_CYCLE | RX_MISC_CLK_DUTY),
767   REG_RX_IF,            (RX_IF_USE_CLKOUT1
768                          | RX_IF_2S_COMP),
769   REG_RX_DIGITAL,       (RX_DIGITAL_2_CHAN)
770 };
771
772
773 usrp_basic_rx::usrp_basic_rx (int which_board, int fusb_block_size, int fusb_nblocks,
774                               const std::string fpga_filename,
775                               const std::string firmware_filename
776                               )
777   : usrp_basic (which_board, open_rx_interface, fpga_filename, firmware_filename),
778     d_devhandle (0), d_ephandle (0),
779     d_bytes_seen (0), d_first_read (true),
780     d_rx_enable (false)
781 {
782   // initialize rx specific registers
783
784   if (!usrp_9862_write_many_all (d_udh, rx_init_regs, sizeof (rx_init_regs))){
785     fprintf (stderr, "usrp_basic_rx: failed to init AD9862 RX regs\n");
786     throw std::runtime_error ("usrp_basic_rx/init_9862");
787   }
788
789   if (0){
790     // FIXME power down 2nd codec rx path
791     usrp_9862_write (d_udh, 1, REG_RX_PWR_DN, 0x1);     // power down everything
792   }
793
794   // Reset the rx path and leave it disabled.
795   set_rx_enable (false);
796   usrp_set_fpga_rx_reset (d_udh, true);
797   usrp_set_fpga_rx_reset (d_udh, false);
798
799   set_fpga_rx_sample_rate_divisor (2);  // usually correct
800
801   set_dc_offset_cl_enable(0xf, 0xf);    // enable DC offset removal control loops
802
803   probe_rx_slots (false);
804
805   //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
806   //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);
807
808   // check fusb buffering parameters
809
810   if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
811     throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
812
813   if (fusb_nblocks < 0)
814     throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
815   
816   if (fusb_block_size == 0)
817     fusb_block_size = fusb_sysconfig::default_block_size();
818
819   if (fusb_nblocks == 0)
820     fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
821
822   d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
823   d_ephandle = d_devhandle->make_ephandle (USRP_RX_ENDPOINT, true,
824                                            fusb_block_size, fusb_nblocks);
825
826   write_atr_mask(0, 0);         // zero Rx A Auto Transmit/Receive regs
827   write_atr_txval(0, 0);
828   write_atr_rxval(0, 0);
829   write_atr_mask(1, 0);         // zero Rx B Auto Transmit/Receive regs
830   write_atr_txval(1, 0);
831   write_atr_rxval(1, 0);
832 }
833
834 static unsigned char rx_fini_regs[] = {
835   REG_RX_PWR_DN,        0x1                             // power down everything
836 };
837
838 usrp_basic_rx::~usrp_basic_rx ()
839 {
840   if (!set_rx_enable (false)){
841     fprintf (stderr, "usrp_basic_rx: set_fpga_rx_enable failed\n");
842   }
843
844   d_ephandle->stop ();
845   delete d_ephandle;
846   delete d_devhandle;
847
848   if (!usrp_9862_write_many_all (d_udh, rx_fini_regs, sizeof (rx_fini_regs))){
849     fprintf (stderr, "usrp_basic_rx: failed to fini AD9862 RX regs\n");
850   }
851
852   shutdown_daughterboards();
853 }
854
855
856 bool
857 usrp_basic_rx::start ()
858 {
859   if (!usrp_basic::start ())    // invoke parent's method
860     return false;
861
862   // fire off reads before asserting rx_enable
863
864   if (!d_ephandle->start ()){
865     fprintf (stderr, "usrp_basic_rx: failed to start end point streaming");
866     return false;
867   }
868
869   if (!set_rx_enable (true)){
870     fprintf (stderr, "usrp_basic_rx: set_rx_enable failed\n");
871     return false;
872   }
873   
874   return true;
875 }
876
877 bool
878 usrp_basic_rx::stop ()
879 {
880   bool ok = usrp_basic::stop();
881
882   if (!set_rx_enable(false)){
883     fprintf (stderr, "usrp_basic_rx: set_rx_enable(false) failed\n");
884     ok = false;
885   }
886
887   if (!d_ephandle->stop()){
888     fprintf (stderr, "usrp_basic_rx: failed to stop end point streaming");
889     ok = false;
890   }
891
892   return ok;
893 }
894
895 usrp_basic_rx *
896 usrp_basic_rx::make (int which_board, int fusb_block_size, int fusb_nblocks,
897                      const std::string fpga_filename,
898                      const std::string firmware_filename)
899 {
900   usrp_basic_rx *u = 0;
901   
902   try {
903     u = new usrp_basic_rx (which_board, fusb_block_size, fusb_nblocks,
904                            fpga_filename, firmware_filename);
905     return u;
906   }
907   catch (...){
908     delete u;
909     return 0;
910   }
911
912   return u;
913 }
914
915 bool
916 usrp_basic_rx::set_fpga_rx_sample_rate_divisor (unsigned int div)
917 {
918   return _write_fpga_reg (FR_RX_SAMPLE_RATE_DIV, div - 1);
919 }
920
921
922 /*
923  * \brief read data from the D/A's via the FPGA.
924  * \p len must be a multiple of 512 bytes.
925  *
926  * \returns the number of bytes read, or -1 on error.
927  *
928  * If overrun is non-NULL it will be set true iff an RX overrun is detected.
929  */
930 int
931 usrp_basic_rx::read (void *buf, int len, bool *overrun)
932 {
933   int   r;
934   
935   if (overrun)
936     *overrun = false;
937   
938   if (len < 0 || (len % 512) != 0){
939     fprintf (stderr, "usrp_basic_rx::read: invalid length = %d\n", len);
940     return -1;
941   }
942
943   r = d_ephandle->read (buf, len);
944   if (r > 0)
945     d_bytes_seen += r;
946
947   /*
948    * In many cases, the FPGA reports an rx overrun right after we
949    * enable the Rx path.  If this is our first read, check for the
950    * overrun to clear the condition, then ignore the result.
951    */
952   if (0 && d_first_read){       // FIXME
953     d_first_read = false;
954     bool bogus_overrun;
955     usrp_check_rx_overrun (d_udh, &bogus_overrun);
956   }
957
958   if (overrun != 0 && d_bytes_seen >= d_bytes_per_poll){
959     d_bytes_seen = 0;
960     if (!usrp_check_rx_overrun (d_udh, overrun)){
961       fprintf (stderr, "usrp_basic_rx: usrp_check_rx_overrun failed\n");
962     }
963   }
964     
965   return r;
966 }
967
968 bool
969 usrp_basic_rx::set_rx_enable (bool on)
970 {
971   d_rx_enable = on;
972   return usrp_set_fpga_rx_enable (d_udh, on);
973 }
974
975 // conditional disable, return prev state
976 bool
977 usrp_basic_rx::disable_rx ()
978 {
979   bool enabled = rx_enable ();
980   if (enabled)
981     set_rx_enable (false);
982   return enabled;
983 }
984
985 // conditional set
986 void
987 usrp_basic_rx::restore_rx (bool on)
988 {
989   if (on != rx_enable ())
990     set_rx_enable (on);
991 }
992
993 void
994 usrp_basic_rx::probe_rx_slots (bool verbose)
995 {
996   struct usrp_dboard_eeprom     eeprom;
997   static int slot_id_map[2] = { SLOT_RX_A, SLOT_RX_B };
998   static const char *slot_name[2] = { "RX d'board A", "RX d'board B" };
999
1000   for (int i = 0; i < 2; i++){
1001     int slot_id = slot_id_map [i];
1002     const char *msg = 0;
1003     usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
1004
1005     switch (s){
1006     case UDBE_OK:
1007       d_dbid[i] = eeprom.id;
1008       msg = usrp_dbid_to_string (eeprom.id).c_str ();
1009       set_adc_offset (2*i+0, eeprom.offset[0]);
1010       set_adc_offset (2*i+1, eeprom.offset[1]);
1011       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
1012       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1013       break;
1014       
1015     case UDBE_NO_EEPROM:
1016       d_dbid[i] = -1;
1017       msg = "<none>";
1018       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1019       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1020       break;
1021       
1022     case UDBE_INVALID_EEPROM:
1023       d_dbid[i] = -2;
1024       msg = "Invalid EEPROM contents";
1025       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1026       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1027       break;
1028       
1029     case UDBE_BAD_SLOT:
1030     default:
1031       assert (0);
1032     }
1033
1034     if (verbose){
1035       fflush (stdout);
1036       fprintf (stderr, "%s: %s\n", slot_name[i], msg);
1037     }
1038   }
1039 }
1040
1041 bool
1042 usrp_basic_rx::set_pga (int which_amp, double gain)
1043 {
1044   return common_set_pga(C_RX, which_amp, gain);
1045 }
1046
1047 double
1048 usrp_basic_rx::pga(int which_amp) const
1049 {
1050   return common_pga(C_RX, which_amp);
1051 }
1052
1053 double
1054 usrp_basic_rx::pga_min() const
1055 {
1056   return common_pga_min(C_RX);
1057 }
1058
1059 double
1060 usrp_basic_rx::pga_max() const
1061 {
1062   return common_pga_max(C_RX);
1063 }
1064
1065 double
1066 usrp_basic_rx::pga_db_per_step() const
1067 {
1068   return common_pga_db_per_step(C_RX);
1069 }
1070
1071 bool
1072 usrp_basic_rx::_write_oe (int which_side, int value, int mask)
1073 {
1074   return _common_write_oe(C_RX, which_side, value, mask);
1075 }
1076
1077 bool
1078 usrp_basic_rx::write_io (int which_side, int value, int mask)
1079 {
1080   return common_write_io(C_RX, which_side, value, mask);
1081 }
1082
1083 bool
1084 usrp_basic_rx::read_io (int which_side, int *value)
1085 {
1086   return common_read_io(C_RX, which_side, value);
1087 }
1088
1089 int
1090 usrp_basic_rx::read_io (int which_side)
1091 {
1092   return common_read_io(C_RX, which_side);
1093 }
1094
1095 bool
1096 usrp_basic_rx::write_refclk(int which_side, int value)
1097 {
1098   return common_write_refclk(C_RX, which_side, value);
1099 }
1100
1101 bool
1102 usrp_basic_rx::write_atr_mask(int which_side, int value)
1103 {
1104   return common_write_atr_mask(C_RX, which_side, value);
1105 }
1106
1107 bool
1108 usrp_basic_rx::write_atr_txval(int which_side, int value)
1109 {
1110   return common_write_atr_txval(C_RX, which_side, value);
1111 }
1112
1113 bool
1114 usrp_basic_rx::write_atr_rxval(int which_side, int value)
1115 {
1116   return common_write_atr_rxval(C_RX, which_side, value);
1117 }
1118
1119 bool
1120 usrp_basic_rx::write_aux_dac (int which_side, int which_dac, int value)
1121 {
1122   return common_write_aux_dac(C_RX, which_side, which_dac, value);
1123 }
1124
1125 bool
1126 usrp_basic_rx::read_aux_adc (int which_side, int which_adc, int *value)
1127 {
1128   return common_read_aux_adc(C_RX, which_side, which_adc, value);
1129 }
1130
1131 int
1132 usrp_basic_rx::read_aux_adc (int which_side, int which_adc)
1133 {
1134   return common_read_aux_adc(C_RX, which_side, which_adc);
1135 }
1136
1137 int
1138 usrp_basic_rx::block_size () const { return d_ephandle->block_size(); }
1139
1140 ////////////////////////////////////////////////////////////////
1141 //
1142 //                         usrp_basic_tx
1143 //
1144 ////////////////////////////////////////////////////////////////
1145
1146
1147 //
1148 // DAC input rate 64 MHz interleaved for a total input rate of 128 MHz
1149 // DAC input is latched on rising edge of CLKOUT2
1150 // NCO is disabled
1151 // interpolate 2x
1152 // coarse modulator disabled
1153 //
1154
1155 static unsigned char tx_init_regs[] = {
1156   REG_TX_PWR_DN,        0,
1157   REG_TX_A_OFFSET_LO,   0,
1158   REG_TX_A_OFFSET_HI,   0,
1159   REG_TX_B_OFFSET_LO,   0,
1160   REG_TX_B_OFFSET_HI,   0,
1161   REG_TX_A_GAIN,        (TX_X_GAIN_COARSE_FULL | 0),
1162   REG_TX_B_GAIN,        (TX_X_GAIN_COARSE_FULL | 0),
1163   REG_TX_PGA,           0xff,                   // maximum gain (0 dB)
1164   REG_TX_MISC,          0,
1165   REG_TX_IF,            (TX_IF_USE_CLKOUT1
1166                          | TX_IF_I_FIRST
1167                          | TX_IF_INV_TX_SYNC
1168                          | TX_IF_2S_COMP
1169                          | TX_IF_INTERLEAVED),
1170   REG_TX_DIGITAL,       (TX_DIGITAL_2_DATA_PATHS
1171                          | TX_DIGITAL_INTERPOLATE_4X),
1172   REG_TX_MODULATOR,     (TX_MODULATOR_DISABLE_NCO
1173                          | TX_MODULATOR_COARSE_MODULATION_NONE),
1174   REG_TX_NCO_FTW_7_0,   0,
1175   REG_TX_NCO_FTW_15_8,  0,
1176   REG_TX_NCO_FTW_23_16, 0
1177 };
1178
1179 usrp_basic_tx::usrp_basic_tx (int which_board, int fusb_block_size, int fusb_nblocks,
1180                               const std::string fpga_filename,
1181                               const std::string firmware_filename)
1182   : usrp_basic (which_board, open_tx_interface, fpga_filename, firmware_filename),
1183     d_devhandle (0), d_ephandle (0),
1184     d_bytes_seen (0), d_first_write (true),
1185     d_tx_enable (false)
1186 {
1187   if (!usrp_9862_write_many_all (d_udh, tx_init_regs, sizeof (tx_init_regs))){
1188     fprintf (stderr, "usrp_basic_tx: failed to init AD9862 TX regs\n");
1189     throw std::runtime_error ("usrp_basic_tx/init_9862");
1190   }
1191
1192   if (0){
1193     // FIXME power down 2nd codec tx path
1194     usrp_9862_write (d_udh, 1, REG_TX_PWR_DN,
1195                      (TX_PWR_DN_TX_DIGITAL
1196                       | TX_PWR_DN_TX_ANALOG_BOTH));
1197   }
1198
1199   // Reset the tx path and leave it disabled.
1200   set_tx_enable (false);
1201   usrp_set_fpga_tx_reset (d_udh, true);
1202   usrp_set_fpga_tx_reset (d_udh, false);
1203
1204   set_fpga_tx_sample_rate_divisor (4);  // we're using interp x4
1205
1206   probe_tx_slots (false);
1207
1208   //d_db[0] = instantiate_dbs(d_dbid[0], this, 0);
1209   //d_db[1] = instantiate_dbs(d_dbid[1], this, 1);
1210
1211   // check fusb buffering parameters
1212
1213   if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
1214     throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
1215
1216   if (fusb_nblocks < 0)
1217     throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
1218   
1219   if (fusb_block_size == 0)
1220     fusb_block_size = FUSB_BLOCK_SIZE;
1221
1222   if (fusb_nblocks == 0)
1223     fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
1224
1225   d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
1226   d_ephandle = d_devhandle->make_ephandle (USRP_TX_ENDPOINT, false,
1227                                            fusb_block_size, fusb_nblocks);
1228
1229   write_atr_mask(0, 0);         // zero Tx A Auto Transmit/Receive regs
1230   write_atr_txval(0, 0);
1231   write_atr_rxval(0, 0);
1232   write_atr_mask(1, 0);         // zero Tx B Auto Transmit/Receive regs
1233   write_atr_txval(1, 0);
1234   write_atr_rxval(1, 0);
1235 }
1236
1237
1238 static unsigned char tx_fini_regs[] = {
1239   REG_TX_PWR_DN,        (TX_PWR_DN_TX_DIGITAL
1240                          | TX_PWR_DN_TX_ANALOG_BOTH),
1241   REG_TX_MODULATOR,     (TX_MODULATOR_DISABLE_NCO
1242                          | TX_MODULATOR_COARSE_MODULATION_NONE)
1243 };
1244
1245 usrp_basic_tx::~usrp_basic_tx ()
1246 {
1247   d_ephandle->stop ();
1248   delete d_ephandle;
1249   delete d_devhandle;
1250
1251   if (!usrp_9862_write_many_all (d_udh, tx_fini_regs, sizeof (tx_fini_regs))){
1252     fprintf (stderr, "usrp_basic_tx: failed to fini AD9862 TX regs\n");
1253   }
1254
1255   shutdown_daughterboards();
1256 }
1257
1258 bool
1259 usrp_basic_tx::start ()
1260 {
1261   if (!usrp_basic::start ())
1262     return false;
1263
1264   if (!set_tx_enable (true)){
1265     fprintf (stderr, "usrp_basic_tx: set_tx_enable failed\n");
1266     return false;
1267   }
1268   
1269   if (!d_ephandle->start ()){
1270     fprintf (stderr, "usrp_basic_tx: failed to start end point streaming");
1271     return false;
1272   }
1273
1274   return true;
1275 }
1276
1277 bool
1278 usrp_basic_tx::stop ()
1279 {
1280   bool ok = usrp_basic::stop ();
1281
1282   if (!d_ephandle->stop ()){
1283     fprintf (stderr, "usrp_basic_tx: failed to stop end point streaming");
1284     ok = false;
1285   }
1286
1287   if (!set_tx_enable (false)){
1288     fprintf (stderr, "usrp_basic_tx: set_tx_enable(false) failed\n");
1289     ok = false;
1290   }
1291
1292   return ok;
1293 }
1294
1295 usrp_basic_tx *
1296 usrp_basic_tx::make (int which_board, int fusb_block_size, int fusb_nblocks,
1297                      const std::string fpga_filename,
1298                      const std::string firmware_filename)
1299 {
1300   usrp_basic_tx *u = 0;
1301   
1302   try {
1303     u = new usrp_basic_tx (which_board, fusb_block_size, fusb_nblocks,
1304                            fpga_filename, firmware_filename);
1305     return u;
1306   }
1307   catch (...){
1308     delete u;
1309     return 0;
1310   }
1311
1312   return u;
1313 }
1314
1315 bool
1316 usrp_basic_tx::set_fpga_tx_sample_rate_divisor (unsigned int div)
1317 {
1318   return _write_fpga_reg (FR_TX_SAMPLE_RATE_DIV, div - 1);
1319 }
1320
1321 /*!
1322  * \brief Write data to the A/D's via the FPGA.
1323  *
1324  * \p len must be a multiple of 512 bytes.
1325  * \returns number of bytes written or -1 on error.
1326  *
1327  * if \p underrun is non-NULL, it will be set to true iff
1328  * a transmit underrun condition is detected.
1329  */
1330 int
1331 usrp_basic_tx::write (const void *buf, int len, bool *underrun)
1332 {
1333   int   r;
1334   
1335   if (underrun)
1336     *underrun = false;
1337   
1338   if (len < 0 || (len % 512) != 0){
1339     fprintf (stderr, "usrp_basic_tx::write: invalid length = %d\n", len);
1340     return -1;
1341   }
1342
1343   r = d_ephandle->write (buf, len);
1344   if (r > 0)
1345     d_bytes_seen += r;
1346     
1347   /*
1348    * In many cases, the FPGA reports an tx underrun right after we
1349    * enable the Tx path.  If this is our first write, check for the
1350    * underrun to clear the condition, then ignore the result.
1351    */
1352   if (d_first_write && d_bytes_seen >= 4 * FUSB_BLOCK_SIZE){
1353     d_first_write = false;
1354     bool bogus_underrun;
1355     usrp_check_tx_underrun (d_udh, &bogus_underrun);
1356   }
1357
1358   if (underrun != 0 && d_bytes_seen >= d_bytes_per_poll){
1359     d_bytes_seen = 0;
1360     if (!usrp_check_tx_underrun (d_udh, underrun)){
1361       fprintf (stderr, "usrp_basic_tx: usrp_check_tx_underrun failed\n");
1362     }
1363   }
1364
1365   return r;
1366 }
1367
1368 void
1369 usrp_basic_tx::wait_for_completion ()
1370 {
1371   d_ephandle->wait_for_completion ();
1372 }
1373
1374 bool
1375 usrp_basic_tx::set_tx_enable (bool on)
1376 {
1377   d_tx_enable = on;
1378   // fprintf (stderr, "set_tx_enable %d\n", on);
1379   return usrp_set_fpga_tx_enable (d_udh, on);
1380 }
1381
1382 // conditional disable, return prev state
1383 bool
1384 usrp_basic_tx::disable_tx ()
1385 {
1386   bool enabled = tx_enable ();
1387   if (enabled)
1388     set_tx_enable (false);
1389   return enabled;
1390 }
1391
1392 // conditional set
1393 void
1394 usrp_basic_tx::restore_tx (bool on)
1395 {
1396   if (on != tx_enable ())
1397     set_tx_enable (on);
1398 }
1399
1400 void
1401 usrp_basic_tx::probe_tx_slots (bool verbose)
1402 {
1403   struct usrp_dboard_eeprom     eeprom;
1404   static int slot_id_map[2] = { SLOT_TX_A, SLOT_TX_B };
1405   static const char *slot_name[2] = { "TX d'board A", "TX d'board B" };
1406
1407   for (int i = 0; i < 2; i++){
1408     int slot_id = slot_id_map [i];
1409     const char *msg = 0;
1410     usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
1411
1412     switch (s){
1413     case UDBE_OK:
1414       d_dbid[i] = eeprom.id;
1415       msg = usrp_dbid_to_string (eeprom.id).c_str ();
1416       // FIXME, figure out interpretation of dc offset for TX d'boards
1417       // offset = (eeprom.offset[1] << 16) | (eeprom.offset[0] & 0xffff);
1418       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
1419       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1420       break;
1421       
1422     case UDBE_NO_EEPROM:
1423       d_dbid[i] = -1;
1424       msg = "<none>";
1425       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1426       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1427       break;
1428       
1429     case UDBE_INVALID_EEPROM:
1430       d_dbid[i] = -2;
1431       msg = "Invalid EEPROM contents";
1432       _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1433       _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1434       break;
1435       
1436     case UDBE_BAD_SLOT:
1437     default:
1438       assert (0);
1439     }
1440
1441     if (verbose){
1442       fflush (stdout);
1443       fprintf (stderr, "%s: %s\n", slot_name[i], msg);
1444     }
1445   }
1446 }
1447
1448 bool
1449 usrp_basic_tx::set_pga (int which_amp, double gain)
1450 {
1451   return common_set_pga(C_TX, which_amp, gain);
1452 }
1453
1454 double
1455 usrp_basic_tx::pga (int which_amp) const
1456 {
1457   return common_pga(C_TX, which_amp);
1458 }
1459
1460 double
1461 usrp_basic_tx::pga_min() const
1462 {
1463   return common_pga_min(C_TX);
1464 }
1465
1466 double
1467 usrp_basic_tx::pga_max() const
1468 {
1469   return common_pga_max(C_TX);
1470 }
1471
1472 double
1473 usrp_basic_tx::pga_db_per_step() const
1474 {
1475   return common_pga_db_per_step(C_TX);
1476 }
1477
1478 bool
1479 usrp_basic_tx::_write_oe (int which_side, int value, int mask)
1480 {
1481   return _common_write_oe(C_TX, which_side, value, mask);
1482 }
1483
1484 bool
1485 usrp_basic_tx::write_io (int which_side, int value, int mask)
1486 {
1487   return common_write_io(C_TX, which_side, value, mask);
1488 }
1489
1490 bool
1491 usrp_basic_tx::read_io (int which_side, int *value)
1492 {
1493   return common_read_io(C_TX, which_side, value);
1494 }
1495
1496 int
1497 usrp_basic_tx::read_io (int which_side)
1498 {
1499   return common_read_io(C_TX, which_side);
1500 }
1501
1502 bool
1503 usrp_basic_tx::write_refclk(int which_side, int value)
1504 {
1505   return common_write_refclk(C_TX, which_side, value);
1506 }
1507
1508 bool
1509 usrp_basic_tx::write_atr_mask(int which_side, int value)
1510 {
1511   return common_write_atr_mask(C_TX, which_side, value);
1512 }
1513
1514 bool
1515 usrp_basic_tx::write_atr_txval(int which_side, int value)
1516 {
1517   return common_write_atr_txval(C_TX, which_side, value);
1518 }
1519
1520 bool
1521 usrp_basic_tx::write_atr_rxval(int which_side, int value)
1522 {
1523   return common_write_atr_rxval(C_TX, which_side, value);
1524 }
1525
1526 bool
1527 usrp_basic_tx::write_aux_dac (int which_side, int which_dac, int value)
1528 {
1529   return common_write_aux_dac(C_TX, which_side, which_dac, value);
1530 }
1531
1532 bool
1533 usrp_basic_tx::read_aux_adc (int which_side, int which_adc, int *value)
1534 {
1535   return common_read_aux_adc(C_TX, which_side, which_adc, value);
1536 }
1537
1538 int
1539 usrp_basic_tx::read_aux_adc (int which_side, int which_adc)
1540 {
1541   return common_read_aux_adc(C_TX, which_side, which_adc);
1542 }
1543
1544 int
1545 usrp_basic_tx::block_size () const { return d_ephandle->block_size(); }
1546