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