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