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