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