2430ff5c4c32bd735e2dfd9a115b615506028381
[debian/gnuradio] / usrp / host / include / usrp / usrp_basic.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2004,2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /*
24  * ----------------------------------------------------------------------
25  * Mid level interface to the Universal Software Radio Peripheral (Rev 1)
26  *
27  * These classes implement the basic functionality for talking to the
28  * USRP.  They try to be as independent of the signal processing code
29  * in FPGA as possible.  They implement access to the low level
30  * peripherals on the board, provide a common way for reading and
31  * writing registers in the FPGA, and provide the high speed interface
32  * to streaming data across the USB.
33  *
34  * It is expected that subclasses will be derived that provide
35  * access to the functionality to a particular FPGA configuration.
36  * ----------------------------------------------------------------------
37  */
38
39 #ifndef INCLUDED_USRP_BASIC_H
40 #define INCLUDED_USRP_BASIC_H
41
42 #include <usrp/db_base.h>
43 #include <usrp/usrp_slots.h>
44 #include <string>
45 #include <vector>
46 #include <boost/utility.hpp>
47 #include <usrp/usrp_subdev_spec.h>
48
49 struct libusb_device_handle;
50 class  fusb_devhandle;
51 class  fusb_ephandle;
52
53 enum txrx_t {
54   C_RX = 0,
55   C_TX = 1
56 };
57
58 /*!
59  * \brief abstract base class for usrp operations
60  * \ingroup usrp
61  */
62 class usrp_basic : boost::noncopyable
63 {
64 protected:
65   void shutdown_daughterboards();
66
67 protected:
68   struct libusb_device_handle   *d_udh;
69   struct libusb_context         *d_ctx;
70   int                            d_usb_data_rate;       // bytes/sec
71   int                            d_bytes_per_poll;      // how often to poll for overruns
72   bool                           d_verbose;
73   long                           d_fpga_master_clock_freq;
74
75   static const int               MAX_REGS = 128;
76   unsigned int                   d_fpga_shadows[MAX_REGS];
77
78   int                            d_dbid[2];             // daughterboard ID's (side A, side B)
79
80   /*!
81    * Shared pointers to subclasses of db_base.
82    *
83    * The outer vector is of length 2 (0 = side A, 1 = side B).  The
84    * inner vectors are of length 1, 2 or 3 depending on the number of
85    * subdevices implemented by the daugherboard.  At this time, only
86    * the Basic Rx and LF Rx implement more than 1 subdevice.
87    */
88   std::vector< std::vector<db_base_sptr> > d_db;
89
90   //! One time call, made only only from usrp_standard_*::make after shared_ptr is created.
91   void init_db(usrp_basic_sptr u);
92
93
94   usrp_basic (int which_board,
95               struct libusb_device_handle *open_interface (struct libusb_device *dev),
96               const std::string fpga_filename = "",
97               const std::string firmware_filename = "");
98
99   /*!
100    * \brief advise usrp_basic of usb data rate (bytes/sec)
101    *
102    * N.B., this doesn't tweak any hardware.  Derived classes
103    * should call this to inform us of the data rate whenever it's
104    * first set or if it changes.
105    *
106    * \param usb_data_rate       bytes/sec
107    */
108   void set_usb_data_rate (int usb_data_rate);
109   
110   /*!
111    * \brief Write auxiliary digital to analog converter.
112    *
113    * \param slot        Which Tx or Rx slot to write.
114    *                    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
115    *                    SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
116    * \param which_dac   [0,3] RX slots must use only 0 and 1.  TX slots must use only 2 and 3.
117    * \param value       [0,4095]
118    * \returns true iff successful
119    */
120   bool _write_aux_dac (int slot, int which_dac, int value);
121
122   /*!
123    * \brief Read auxiliary analog to digital converter.
124    *
125    * \param slot        2-bit slot number. E.g., SLOT_TX_A
126    * \param which_adc   [0,1]
127    * \param value       return 12-bit value [0,4095]
128    * \returns true iff successful
129    */
130   bool _read_aux_adc (int slot, int which_adc, int *value);
131
132   /*!
133    * \brief Read auxiliary analog to digital converter.
134    *
135    * \param slot        2-bit slot number. E.g., SLOT_TX_A
136    * \param which_adc   [0,1]
137    * \returns value in the range [0,4095] if successful, else READ_FAILED.
138    */
139   int _read_aux_adc (int slot, int which_adc);
140
141
142 public:
143   virtual ~usrp_basic ();
144
145
146   /*!
147    * Return a vector of vectors that contain shared pointers
148    * to the daughterboard instance(s) associated with the specified side.
149    *
150    * It is an error to use the returned objects after the usrp_basic
151    * object has been destroyed.
152    */
153   std::vector<std::vector<db_base_sptr> > db() const { return d_db; }
154
155   /*!
156    * Return a vector of size >= 1 that contains shared pointers
157    * to the daughterboard instance(s) associated with the specified side.
158    *
159    * \param which_side  [0,1] which daughterboard
160    *
161    * It is an error to use the returned objects after the usrp_basic
162    * object has been destroyed.
163    */
164   std::vector<db_base_sptr> db(int which_side);
165  
166   /*!
167    * \brief is the subdev_spec valid?
168    */
169   bool is_valid(const usrp_subdev_spec &ss);
170
171   /*!
172    * \brief given a subdev_spec, return the corresponding daughterboard object.
173    * \throws std::invalid_ argument if ss is invalid.
174    *
175    * \param ss specifies the side and subdevice
176    */
177   db_base_sptr selected_subdev(const usrp_subdev_spec &ss);
178
179   /*!
180    * \brief return frequency of master oscillator on USRP
181    */
182   long fpga_master_clock_freq () const { return d_fpga_master_clock_freq; }
183
184   /*!
185    * Tell API that the master oscillator on the USRP is operating at a non-standard 
186    * fixed frequency. This is only needed for custom USRP hardware modified to 
187    * operate at a different frequency from the default factory configuration. This
188    * function must be called prior to any other API function.
189    * \param master_clock USRP2 FPGA master clock frequency in Hz (10..64 MHz)
190    */
191   void set_fpga_master_clock_freq (long master_clock) { d_fpga_master_clock_freq = master_clock; }
192
193   /*!
194    * \returns usb data rate in bytes/sec
195    */
196   int usb_data_rate () const { return d_usb_data_rate; }
197
198   void set_verbose (bool on) { d_verbose = on; }
199
200   //! magic value used on alternate register read interfaces
201   static const int READ_FAILED = -99999;
202
203   /*!
204    * \brief Write EEPROM on motherboard or any daughterboard.
205    * \param i2c_addr            I2C bus address of EEPROM
206    * \param eeprom_offset       byte offset in EEPROM to begin writing
207    * \param buf                 the data to write
208    * \returns true iff sucessful
209    */
210   bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
211
212   /*!
213    * \brief Read EEPROM on motherboard or any daughterboard.
214    * \param i2c_addr            I2C bus address of EEPROM
215    * \param eeprom_offset       byte offset in EEPROM to begin reading
216    * \param len                 number of bytes to read
217    * \returns the data read if successful, else a zero length string.
218    */
219   std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
220
221   /*!
222    * \brief Write to I2C peripheral
223    * \param i2c_addr            I2C bus address (7-bits)
224    * \param buf                 the data to write
225    * \returns true iff successful
226    * Writes are limited to a maximum of of 64 bytes.
227    */
228   bool write_i2c (int i2c_addr, const std::string buf);
229
230   /*!
231    * \brief Read from I2C peripheral
232    * \param i2c_addr            I2C bus address (7-bits)
233    * \param len                 number of bytes to read
234    * \returns the data read if successful, else a zero length string.
235    * Reads are limited to a maximum of 64 bytes.
236    */
237   std::string read_i2c (int i2c_addr, int len);
238
239   /*!
240    * \brief Set ADC offset correction
241    * \param which_adc   which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
242    * \param offset      16-bit value to subtract from raw ADC input.
243    */
244   bool set_adc_offset (int which_adc, int offset);
245
246   /*!
247    * \brief Set DAC offset correction
248    * \param which_dac   which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
249    * \param offset      10-bit offset value (ambiguous format:  See AD9862 datasheet).
250    * \param offset_pin  1-bit value.  If 0 offset applied to -ve differential pin;
251    *                                  If 1 offset applied to +ve differential pin.
252    */
253   bool set_dac_offset (int which_dac, int offset, int offset_pin);
254
255   /*!
256    * \brief Control ADC input buffer
257    * \param which_adc   which ADC[0,3]
258    * \param bypass      if non-zero, bypass input buffer and connect input
259    *                    directly to switched cap SHA input of RxPGA.
260    */
261   bool set_adc_buffer_bypass (int which_adc, bool bypass);
262
263   /*!
264    * \brief Enable/disable automatic DC offset removal control loop in FPGA
265    *
266    * \param bits  which control loops to enable
267    * \param mask  which \p bits to pay attention to
268    *
269    * If the corresponding bit is set, enable the automatic DC
270    * offset correction control loop.
271    *
272    * <pre>
273    * The 4 low bits are significant:
274    *
275    *   ADC0 = (1 << 0)
276    *   ADC1 = (1 << 1)
277    *   ADC2 = (1 << 2)
278    *   ADC3 = (1 << 3)
279    * </pre>
280    *
281    * By default the control loop is enabled on all ADC's.
282    */
283   bool set_dc_offset_cl_enable(int bits, int mask);
284
285   /*!
286    * \brief return the usrp's serial number.
287    *
288    * \returns non-zero length string iff successful.
289    */
290   std::string serial_number();
291
292   /*!
293    * \brief Return daughterboard ID for given side [0,1].
294    *
295    * \param which_side  [0,1] which daughterboard
296    *
297    * \return daughterboard id >= 0 if successful
298    * \return -1 if no daugherboard
299    * \return -2 if invalid EEPROM on daughterboard
300    */
301   virtual int daughterboard_id (int which_side) const = 0;
302
303   /*!
304    * \brief Clock ticks to delay rising of T/R signal
305    * \sa write_atr_mask, write_atr_txval, write_atr_rxval
306    */
307   bool write_atr_tx_delay(int value);
308
309   /*!
310    * \brief Clock ticks to delay falling edge of T/R signal
311    * \sa write_atr_mask, write_atr_txval, write_atr_rxval
312    */
313   bool write_atr_rx_delay(int value);
314
315
316 \f  // ================================================================
317   // Routines to access and control daughterboard specific i/o
318   //
319   // Those with a common_ prefix access either the Tx or Rx side depending
320   // on the txrx parameter.  Those without the common_ prefix are virtual
321   // and are overriden in usrp_basic_rx and usrp_basic_tx to access the
322   // the Rx or Tx sides automatically.  We provide the common_ versions
323   // for those daughterboards such as the WBX and XCVR2450 that share
324   // h/w resources (such as the LO) between the Tx and Rx sides.
325
326   // ----------------------------------------------------------------
327   // BEGIN common_  daughterboard control functions
328
329   /*!
330    * \brief Set Programmable Gain Amplifier(PGA)
331    *
332    * \param txrx        Tx or Rx?
333    * \param which_amp   which amp [0,3]
334    * \param gain_in_db  gain value(linear in dB)
335    *
336    * gain is rounded to closest setting supported by hardware.
337    *
338    * \returns true iff sucessful.
339    *
340    * \sa pga_min(), pga_max(), pga_db_per_step()
341    */
342   bool common_set_pga(txrx_t txrx, int which_amp, double gain_in_db);
343
344   /*!
345    * \brief Return programmable gain amplifier gain setting in dB.
346    *
347    * \param txrx        Tx or Rx?
348    * \param which_amp   which amp [0,3]
349    */
350   double common_pga(txrx_t txrx, int which_amp) const;
351
352   /*!
353    * \brief Return minimum legal PGA gain in dB.
354    * \param txrx        Tx or Rx?
355    */
356   double common_pga_min(txrx_t txrx) const;
357
358   /*!
359    * \brief Return maximum legal PGA gain in dB.
360    * \param txrx        Tx or Rx?
361    */
362   double common_pga_max(txrx_t txrx) const;
363
364   /*!
365    * \brief Return hardware step size of PGA(linear in dB).
366    * \param txrx        Tx or Rx?
367    */
368   double common_pga_db_per_step(txrx_t txrx) const;
369
370   /*!
371    * \brief Write direction register(output enables) for pins that go to daughterboard.
372    *
373    * \param txrx        Tx or Rx?
374    * \param which_side  [0,1] which size
375    * \param value       value to write into register
376    * \param mask        which bits of value to write into reg
377    *
378    * Each d'board has 16-bits of general purpose i/o.
379    * Setting the bit makes it an output from the FPGA to the d'board.
380    *
381    * This register is initialized based on a value stored in the
382    * d'board EEPROM.  In general, you shouldn't be using this routine
383    * without a very good reason.  Using this method incorrectly will
384    * kill your USRP motherboard and/or daughterboard.
385    */
386   bool _common_write_oe(txrx_t txrx, int which_side, int value, int mask);
387
388   /*!
389    * \brief Write daughterboard i/o pin value
390    *
391    * \param txrx        Tx or Rx?
392    * \param which_side  [0,1] which d'board
393    * \param value       value to write into register
394    * \param mask        which bits of value to write into reg
395    */
396   bool common_write_io(txrx_t txrx, int which_side, int value, int mask);
397
398   /*!
399    * \brief Read daughterboard i/o pin value
400    *
401    * \param txrx        Tx or Rx?
402    * \param which_side  [0,1] which d'board
403    * \param value       output
404    */
405   bool common_read_io(txrx_t txrx, int which_side, int *value);
406
407   /*!
408    * \brief Read daughterboard i/o pin value
409    *
410    * \param txrx        Tx or Rx?
411    * \param which_side  [0,1] which d'board
412    * \returns register value if successful, else READ_FAILED
413    */
414   int common_read_io(txrx_t txrx, int which_side);
415
416   /*!
417    * \brief Write daughterboard refclk config register
418    *
419    * \param txrx        Tx or Rx?
420    * \param which_side  [0,1] which d'board
421    * \param value       value to write into register, see below
422    *
423    * <pre>
424    * Control whether a reference clock is sent to the daughterboards,
425    * and what frequency.  The refclk is sent on d'board i/o pin 0.
426    * 
427    *     3                   2                   1                       
428    *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
429    *  +-----------------------------------------------+-+------------+
430    *  |             Reserved (Must be zero)           |E|   DIVISOR  |
431    *  +-----------------------------------------------+-+------------+
432    * 
433    *  Bit 7  -- 1 turns on refclk, 0 allows IO use
434    *  Bits 6:0 Divider value
435    * </pre>
436    */
437   bool common_write_refclk(txrx_t txrx, int which_side, int value);
438
439   /*!
440    * \brief Automatic Transmit/Receive switching
441    * <pre>
442    *
443    * If automatic transmit/receive (ATR) switching is enabled in the
444    * FR_ATR_CTL register, the presence or absence of data in the FPGA
445    * transmit fifo selects between two sets of values for each of the 4
446    * banks of daughterboard i/o pins.
447    *
448    * Each daughterboard slot has 3 16-bit registers associated with it:
449    *   FR_ATR_MASK_*, FR_ATR_TXVAL_* and FR_ATR_RXVAL_*
450    *
451    * FR_ATR_MASK_{0,1,2,3}: 
452    *
453    *   These registers determine which of the daugherboard i/o pins are
454    *   affected by ATR switching.  If a bit in the mask is set, the
455    *   corresponding i/o bit is controlled by ATR, else it's output
456    *   value comes from the normal i/o pin output register:
457    *   FR_IO_{0,1,2,3}.
458    *
459    * FR_ATR_TXVAL_{0,1,2,3}:
460    * FR_ATR_RXVAL_{0,1,2,3}:
461    *
462    *   If the Tx fifo contains data, then the bits from TXVAL that are
463    *   selected by MASK are output.  Otherwise, the bits from RXVAL that
464    *   are selected by MASK are output.
465    * </pre>
466    */
467   bool common_write_atr_mask(txrx_t txrx, int which_side, int value);
468   bool common_write_atr_txval(txrx_t txrx, int which_side, int value);
469   bool common_write_atr_rxval(txrx_t txrx, int which_side, int value);
470
471   /*!
472    * \brief Write auxiliary digital to analog converter.
473    *
474    * \param txrx        Tx or Rx?
475    * \param which_side  [0,1] which d'board
476    *                    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
477    *                    SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
478    * \param which_dac   [2,3] TX slots must use only 2 and 3.
479    * \param value       [0,4095]
480    * \returns true iff successful
481    */
482   bool common_write_aux_dac(txrx_t txrx, int which_side, int which_dac, int value);
483
484   /*!
485    * \brief Read auxiliary analog to digital converter.
486    *
487    * \param txrx        Tx or Rx?
488    * \param which_side  [0,1] which d'board
489    * \param which_adc   [0,1]
490    * \param value       return 12-bit value [0,4095]
491    * \returns true iff successful
492    */
493   bool common_read_aux_adc(txrx_t txrx, int which_side, int which_adc, int *value);
494
495   /*!
496    * \brief Read auxiliary analog to digital converter.
497    *
498    * \param txrx        Tx or Rx?
499    * \param which_side  [0,1] which d'board
500    * \param which_adc   [0,1]
501    * \returns value in the range [0,4095] if successful, else READ_FAILED.
502    */
503   int common_read_aux_adc(txrx_t txrx, int which_side, int which_adc);
504
505   // END common_ daughterboard control functions\f
506   // ----------------------------------------------------------------
507   // BEGIN virtual daughterboard control functions
508
509   /*!
510    * \brief Set Programmable Gain Amplifier (PGA)
511    *
512    * \param which_amp   which amp [0,3]
513    * \param gain_in_db  gain value (linear in dB)
514    *
515    * gain is rounded to closest setting supported by hardware.
516    *
517    * \returns true iff sucessful.
518    *
519    * \sa pga_min(), pga_max(), pga_db_per_step()
520    */
521   virtual bool set_pga (int which_amp, double gain_in_db) = 0;
522
523   /*!
524    * \brief Return programmable gain amplifier gain setting in dB.
525    *
526    * \param which_amp   which amp [0,3]
527    */
528   virtual double pga (int which_amp) const = 0;
529
530   /*!
531    * \brief Return minimum legal PGA gain in dB.
532    */
533   virtual double pga_min () const = 0;
534
535   /*!
536    * \brief Return maximum legal PGA gain in dB.
537    */
538   virtual double pga_max () const = 0;
539
540   /*!
541    * \brief Return hardware step size of PGA (linear in dB).
542    */
543   virtual double pga_db_per_step () const = 0;
544
545   /*!
546    * \brief Write direction register (output enables) for pins that go to daughterboard.
547    *
548    * \param which_side  [0,1] which size
549    * \param value       value to write into register
550    * \param mask        which bits of value to write into reg
551    *
552    * Each d'board has 16-bits of general purpose i/o.
553    * Setting the bit makes it an output from the FPGA to the d'board.
554    *
555    * This register is initialized based on a value stored in the
556    * d'board EEPROM.  In general, you shouldn't be using this routine
557    * without a very good reason.  Using this method incorrectly will
558    * kill your USRP motherboard and/or daughterboard.
559    */
560   virtual bool _write_oe (int which_side, int value, int mask) = 0;
561
562   /*!
563    * \brief Write daughterboard i/o pin value
564    *
565    * \param which_side  [0,1] which d'board
566    * \param value       value to write into register
567    * \param mask        which bits of value to write into reg
568    */
569   virtual bool write_io (int which_side, int value, int mask) = 0;
570
571   /*!
572    * \brief Read daughterboard i/o pin value
573    *
574    * \param which_side  [0,1] which d'board
575    * \param value       output
576    */
577   virtual bool read_io (int which_side, int *value) = 0;
578
579   /*!
580    * \brief Read daughterboard i/o pin value
581    *
582    * \param which_side  [0,1] which d'board
583    * \returns register value if successful, else READ_FAILED
584    */
585   virtual int read_io (int which_side) = 0;
586
587   /*!
588    * \brief Write daughterboard refclk config register
589    *
590    * \param which_side  [0,1] which d'board
591    * \param value       value to write into register, see below
592    *
593    * <pre>
594    * Control whether a reference clock is sent to the daughterboards,
595    * and what frequency.  The refclk is sent on d'board i/o pin 0.
596    * 
597    *     3                   2                   1                       
598    *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
599    *  +-----------------------------------------------+-+------------+
600    *  |             Reserved (Must be zero)           |E|   DIVISOR  |
601    *  +-----------------------------------------------+-+------------+
602    * 
603    *  Bit 7  -- 1 turns on refclk, 0 allows IO use
604    *  Bits 6:0 Divider value
605    * </pre>
606    */
607   virtual bool write_refclk(int which_side, int value) = 0;
608
609   virtual bool write_atr_mask(int which_side, int value) = 0;
610   virtual bool write_atr_txval(int which_side, int value) = 0;
611   virtual bool write_atr_rxval(int which_side, int value) = 0;
612
613   /*!
614    * \brief Write auxiliary digital to analog converter.
615    *
616    * \param which_side  [0,1] which d'board
617    *                    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
618    *                    SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
619    * \param which_dac   [2,3] TX slots must use only 2 and 3.
620    * \param value       [0,4095]
621    * \returns true iff successful
622    */
623   virtual bool write_aux_dac (int which_side, int which_dac, int value) = 0;
624
625   /*!
626    * \brief Read auxiliary analog to digital converter.
627    *
628    * \param which_side  [0,1] which d'board
629    * \param which_adc   [0,1]
630    * \param value       return 12-bit value [0,4095]
631    * \returns true iff successful
632    */
633   virtual bool read_aux_adc (int which_side, int which_adc, int *value) = 0;
634
635   /*!
636    * \brief Read auxiliary analog to digital converter.
637    *
638    * \param which_side  [0,1] which d'board
639    * \param which_adc   [0,1]
640    * \returns value in the range [0,4095] if successful, else READ_FAILED.
641    */
642   virtual int read_aux_adc (int which_side, int which_adc) = 0;
643
644   /*!
645    * \brief returns current fusb block size
646    */
647   virtual int block_size() const = 0;
648
649   /*!
650    * \brief returns A/D or D/A converter rate in Hz
651    */
652   virtual long converter_rate() const = 0;
653
654   // END virtual daughterboard control functions\f
655
656   // ----------------------------------------------------------------
657   // Low level implementation routines.
658   // You probably shouldn't be using these...
659   //
660
661   bool _set_led (int which_led, bool on);
662
663   /*!
664    * \brief Write FPGA register.
665    * \param regno       7-bit register number
666    * \param value       32-bit value
667    * \returns true iff successful
668    */
669   bool _write_fpga_reg (int regno, int value);  //< 7-bit regno, 32-bit value
670
671   /*!
672    * \brief Read FPGA register.
673    * \param regno       7-bit register number
674    * \param value       32-bit value
675    * \returns true iff successful
676    */
677   bool _read_fpga_reg (int regno, int *value);  //< 7-bit regno, 32-bit value
678
679   /*!
680    * \brief Read FPGA register.
681    * \param regno       7-bit register number
682    * \returns register value if successful, else READ_FAILED
683    */
684   int  _read_fpga_reg (int regno);
685
686   /*!
687    * \brief Write FPGA register with mask.
688    * \param regno       7-bit register number
689    * \param value       16-bit value
690    * \param mask        16-bit value
691    * \returns true if successful
692    * Only use this for registers who actually implement a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
693    */
694   bool _write_fpga_reg_masked (int regno, int value, int mask);
695
696   /*!
697    * \brief Write AD9862 register.
698    * \param which_codec 0 or 1
699    * \param regno       6-bit register number
700    * \param value       8-bit value
701    * \returns true iff successful
702    */
703   bool _write_9862 (int which_codec, int regno, unsigned char value);
704
705   /*!
706    * \brief Read AD9862 register.
707    * \param which_codec 0 or 1
708    * \param regno       6-bit register number
709    * \param value       8-bit value
710    * \returns true iff successful
711    */
712   bool _read_9862 (int which_codec, int regno, unsigned char *value) const;
713
714   /*!
715    * \brief Read AD9862 register.
716    * \param which_codec 0 or 1
717    * \param regno       6-bit register number
718    * \returns register value if successful, else READ_FAILED
719    */
720   int  _read_9862 (int which_codec, int regno) const;
721
722   /*!
723    * \brief Write data to SPI bus peripheral.
724    *
725    * \param optional_header     0,1 or 2 bytes to write before buf.
726    * \param enables             bitmask of peripherals to write. See usrp_spi_defs.h
727    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
728    * \param buf                 the data to write
729    * \returns true iff successful
730    * Writes are limited to a maximum of 64 bytes.
731    *
732    * If \p format specifies that optional_header bytes are present, they are
733    * written to the peripheral immediately prior to writing \p buf.
734    */
735   bool _write_spi (int optional_header, int enables, int format, std::string buf);
736
737   /*
738    * \brief Read data from SPI bus peripheral.
739    *
740    * \param optional_header     0,1 or 2 bytes to write before buf.
741    * \param enables             bitmask of peripheral to read. See usrp_spi_defs.h
742    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
743    * \param len                 number of bytes to read.  Must be in [0,64].
744    * \returns the data read if sucessful, else a zero length string.
745    *
746    * Reads are limited to a maximum of 64 bytes.
747    *
748    * If \p format specifies that optional_header bytes are present, they
749    * are written to the peripheral first.  Then \p len bytes are read from
750    * the peripheral and returned.
751    */
752   std::string _read_spi (int optional_header, int enables, int format, int len);
753
754   /*!
755    * \brief Start data transfers.
756    * Called in base class to derived class order.
757    */
758   bool start ();
759
760   /*!
761    * \brief Stop data transfers.
762    * Called in base class to derived class order.
763    */
764   bool stop ();
765 };
766
767 \f/*!
768  * \brief class for accessing the receive side of the USRP
769  * \ingroup usrp
770  */
771 class usrp_basic_rx : public usrp_basic 
772 {
773 private:
774   fusb_devhandle        *d_devhandle;
775   fusb_ephandle         *d_ephandle;
776   int                    d_bytes_seen;          // how many bytes we've seen
777   bool                   d_first_read;
778   bool                   d_rx_enable;
779
780 protected:
781   /*!
782    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
783    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512. 
784    *                         Use zero for a reasonable default.
785    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default. 
786    * \param fpga_filename    name of the rbf file to load
787    * \param firmware_filename name of ihx file to load
788    */
789   usrp_basic_rx (int which_board,
790                  int fusb_block_size=0,
791                  int fusb_nblocks=0,
792                  const std::string fpga_filename = "",
793                  const std::string firmware_filename = ""
794                  );  // throws if trouble
795
796   bool set_rx_enable (bool on);
797   bool rx_enable () const { return d_rx_enable; }
798
799   bool disable_rx ();           // conditional disable, return prev state
800   void restore_rx (bool on);    // conditional set
801
802   void probe_rx_slots (bool verbose);
803
804 public:
805   ~usrp_basic_rx ();
806
807   /*!
808    * \brief invokes constructor, returns instance or 0 if trouble
809    *
810    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
811    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512. 
812    *                         Use zero for a reasonable default.
813    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default. 
814    * \param fpga_filename    name of file that contains image to load into FPGA
815    * \param firmware_filename   name of file that contains image to load into FX2
816    */
817   static usrp_basic_rx *make (int which_board,
818                               int fusb_block_size=0,
819                               int fusb_nblocks=0,
820                               const std::string fpga_filename = "",
821                               const std::string firmware_filename = ""
822                               );
823
824   /*!
825    * \brief tell the fpga the rate rx samples are coming from the A/D's
826    *
827    * div = fpga_master_clock_freq () / sample_rate
828    *
829    * sample_rate is determined by a myriad of registers
830    * in the 9862.  That's why you have to tell us, so
831    * we can tell the fpga.
832    */
833   bool set_fpga_rx_sample_rate_divisor (unsigned int div);
834
835   /*!
836    * \brief read data from the D/A's via the FPGA.
837    * \p len must be a multiple of 512 bytes.
838    *
839    * \returns the number of bytes read, or -1 on error.
840    *
841    * If overrun is non-NULL it will be set true iff an RX overrun is detected.
842    */
843   int read (void *buf, int len, bool *overrun);
844
845
846   //! sampling rate of A/D converter
847   virtual long converter_rate() const { return fpga_master_clock_freq(); } // 64M
848   long adc_rate() const { return converter_rate(); }
849   int daughterboard_id (int which_side) const { return d_dbid[which_side & 0x1]; }
850
851   bool set_pga (int which_amp, double gain_in_db);
852   double pga (int which_amp) const;
853   double pga_min () const;
854   double pga_max () const;
855   double pga_db_per_step () const;
856
857   bool _write_oe (int which_side, int value, int mask);
858   bool write_io (int which_side, int value, int mask);
859   bool read_io (int which_side, int *value);
860   int read_io (int which_side);
861   bool write_refclk(int which_side, int value);
862   bool write_atr_mask(int which_side, int value);
863   bool write_atr_txval(int which_side, int value);
864   bool write_atr_rxval(int which_side, int value);
865
866   bool write_aux_dac (int which_side, int which_dac, int value);
867   bool read_aux_adc (int which_side, int which_adc, int *value);
868   int  read_aux_adc (int which_side, int which_adc);
869
870   int block_size() const;
871
872   // called in base class to derived class order
873   bool start ();
874   bool stop ();
875 };
876
877 \f/*!
878  * \brief class for accessing the transmit side of the USRP
879  * \ingroup usrp
880  */
881 class usrp_basic_tx : public usrp_basic 
882 {
883 private:
884   fusb_devhandle        *d_devhandle;
885   fusb_ephandle         *d_ephandle;
886   int                    d_bytes_seen;          // how many bytes we've seen
887   bool                   d_first_write;
888   bool                   d_tx_enable;
889
890  protected:
891   /*!
892    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
893    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512.
894    *                         Use zero for a reasonable default.
895    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default.
896    * \param fpga_filename    name of file that contains image to load into FPGA
897    * \param firmware_filename   name of file that contains image to load into FX2
898    */
899   usrp_basic_tx (int which_board,
900                  int fusb_block_size=0,
901                  int fusb_nblocks=0,
902                  const std::string fpga_filename = "",
903                  const std::string firmware_filename = ""
904                  );             // throws if trouble
905
906   bool set_tx_enable (bool on);
907   bool tx_enable () const { return d_tx_enable; }
908
909   bool disable_tx ();           // conditional disable, return prev state
910   void restore_tx (bool on);    // conditional set
911
912   void probe_tx_slots (bool verbose);
913
914 public:
915
916   ~usrp_basic_tx ();
917
918   /*!
919    * \brief invokes constructor, returns instance or 0 if trouble
920    *
921    * \param which_board      Which USRP board on usb (not particularly useful; use 0)
922    * \param fusb_block_size  fast usb xfer block size.  Must be a multiple of 512. 
923    *                         Use zero for a reasonable default.
924    * \param fusb_nblocks     number of fast usb URBs to allocate.  Use zero for a reasonable default. 
925    * \param fpga_filename    name of file that contains image to load into FPGA
926    * \param firmware_filename   name of file that contains image to load into FX2
927    */
928   static usrp_basic_tx *make (int which_board, int fusb_block_size=0, int fusb_nblocks=0,
929                               const std::string fpga_filename = "",
930                               const std::string firmware_filename = ""
931                               );
932
933   /*!
934    * \brief tell the fpga the rate tx samples are going to the D/A's
935    *
936    * div = fpga_master_clock_freq () * 2
937    *
938    * sample_rate is determined by a myriad of registers
939    * in the 9862.  That's why you have to tell us, so
940    * we can tell the fpga.
941    */
942   bool set_fpga_tx_sample_rate_divisor (unsigned int div);
943
944   /*!
945    * \brief Write data to the A/D's via the FPGA.
946    *
947    * \p len must be a multiple of 512 bytes.
948    * \returns number of bytes written or -1 on error.
949    *
950    * if \p underrun is non-NULL, it will be set to true iff
951    * a transmit underrun condition is detected.
952    */
953   int write (const void *buf, int len, bool *underrun);
954
955   /*
956    * Block until all outstanding writes have completed.
957    * This is typically used to assist with benchmarking
958    */
959   void wait_for_completion ();
960
961   //! sampling rate of D/A converter
962   virtual long converter_rate() const { return fpga_master_clock_freq () * 2; } // 128M
963   long dac_rate() const { return converter_rate(); }
964   int daughterboard_id (int which_side) const { return d_dbid[which_side & 0x1]; }
965
966   bool set_pga (int which_amp, double gain_in_db);
967   double pga (int which_amp) const;
968   double pga_min () const;
969   double pga_max () const;
970   double pga_db_per_step () const;
971
972   bool _write_oe (int which_side, int value, int mask);
973   bool write_io (int which_side, int value, int mask);
974   bool read_io (int which_side, int *value);
975   int read_io (int which_side);
976   bool write_refclk(int which_side, int value);
977   bool write_atr_mask(int which_side, int value);
978   bool write_atr_txval(int which_side, int value);
979   bool write_atr_rxval(int which_side, int value);
980
981   bool write_aux_dac (int which_side, int which_dac, int value);
982   bool read_aux_adc (int which_side, int which_adc, int *value);
983   int read_aux_adc (int which_side, int which_adc);
984
985   int block_size() const;
986
987   // called in base class to derived class order
988   bool start ();
989   bool stop ();
990 };
991
992 #endif