GNU Radio 3.3.0 C++ API
usrp_base.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2008,2009 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 #ifndef INCLUDED_USRP_BASE_H
00022 #define INCLUDED_USRP_BASE_H
00023 
00024 #include <gr_sync_block.h>
00025 #include <stdexcept>
00026 #include <boost/shared_ptr.hpp>
00027 #include <usrp/db_base.h>
00028 #include <usrp/usrp_subdev_spec.h>
00029 
00030 class usrp_basic;
00031 
00032 /*!
00033  * \brief base class for GNU Radio interface to the USRP
00034  */
00035 class usrp_base : public gr_sync_block {
00036 private:
00037   boost::shared_ptr<usrp_basic> d_usrp_basic;
00038 
00039 protected:
00040   usrp_base(const std::string &name,
00041             gr_io_signature_sptr input_signature,
00042             gr_io_signature_sptr output_signature)
00043     : gr_sync_block(name, input_signature, output_signature) {}
00044     
00045 
00046   void set_usrp_basic(boost::shared_ptr<usrp_basic> u);
00047 
00048 public:
00049   virtual ~usrp_base();
00050 
00051   /* !
00052    * Return a vector of vectors of daughterboard instances associated with
00053    * the USRP source or sink.  The first dimension of the returned vector
00054    * corresponds to the side of the USRP, the second dimension, the subdevice
00055    * on the particular daughterboard.
00056    *
00057    * N.B. To ensure proper lifetime management, the caller should
00058    * continue to hold these as weak pointers, not shared pointers.  
00059    * As long as the caller does not attempt to directly use the weak
00060    * pointers after this usrp object has been destroyed, everything
00061    * will work out fine.
00062    */
00063    std::vector<std::vector<db_base_sptr> > db();
00064 
00065   /*!
00066    * Return a vector of size 1 or 2 that contains shared pointers
00067    * to the daughterboard instance(s) associated with the specified side.
00068    *
00069    * \param which_side  [0,1] which daughterboard
00070    *
00071    * N.B. To ensure proper lifetime management, the caller should
00072    * continue to hold these as weak pointers, not shared pointers.  
00073    * As long as the caller does not attempt to directly use the weak
00074    * pointers after this usrp object has been destroyed, everything
00075    * will work out fine.
00076    */
00077   std::vector<db_base_sptr> db(int which_side);
00078 
00079   /*!
00080    * Return the daughterboard instance corresponding to the selected
00081    * side of the USRP and selected daughterboard subdevice.
00082    * N.B. To ensure proper lifetime management, the caller should
00083    * continue to hold these as weak pointers, not shared pointers.  
00084    * As long as the caller does not attempt to directly use the weak
00085    * pointers after this usrp object has been destroyed, everything
00086    * will work out fine.
00087    */
00088   db_base_sptr db(int which_side, int which_dev);
00089 
00090   /*!
00091    * \brief given a usrp_subdev_spec, return the corresponding daughterboard object.
00092    * \throws std::invalid_argument if ss is invalid.
00093    *
00094    * \param ss specifies the side and subdevice
00095    */
00096   db_base_sptr selected_subdev(usrp_subdev_spec ss);
00097 
00098   /*!
00099    * \brief return frequency of master oscillator on USRP
00100    */
00101   long fpga_master_clock_freq() const;
00102 
00103   /*!
00104    * Tell API that the master oscillator on the USRP is operating at a non-standard 
00105    * fixed frequency. This is only needed for custom USRP hardware modified to 
00106    * operate at a different frequency from the default factory configuration. This
00107    * function must be called prior to any other API function.
00108    * \param master_clock USRP2 FPGA master clock frequency in Hz (10..64 MHz)
00109    */
00110   void set_fpga_master_clock_freq (long master_clock);
00111 
00112   void set_verbose (bool on);
00113 
00114   //! magic value used on alternate register read interfaces
00115   static const int READ_FAILED = -99999;
00116 
00117   /*!
00118    * \brief Write EEPROM on motherboard or any daughterboard.
00119    * \param i2c_addr            I2C bus address of EEPROM
00120    * \param eeprom_offset       byte offset in EEPROM to begin writing
00121    * \param buf                 the data to write
00122    * \returns true iff sucessful
00123    */
00124   bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
00125 
00126   /*!
00127    * \brief Read EEPROM on motherboard or any daughterboard.
00128    * \param i2c_addr            I2C bus address of EEPROM
00129    * \param eeprom_offset       byte offset in EEPROM to begin reading
00130    * \param len                 number of bytes to read
00131    * \returns the data read if successful, else a zero length string.
00132    */
00133   std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
00134 
00135   /*!
00136    * \brief Write to I2C peripheral
00137    * \param i2c_addr            I2C bus address (7-bits)
00138    * \param buf                 the data to write
00139    * \returns true iff successful
00140    * Writes are limited to a maximum of of 64 bytes.
00141    */
00142   bool write_i2c (int i2c_addr, const std::string buf);
00143 
00144   /*!
00145    * \brief Read from I2C peripheral
00146    * \param i2c_addr            I2C bus address (7-bits)
00147    * \param len                 number of bytes to read
00148    * \returns the data read if successful, else a zero length string.
00149    * Reads are limited to a maximum of 64 bytes.
00150    */
00151   std::string read_i2c (int i2c_addr, int len);
00152 
00153   /*!
00154    * \brief Set ADC offset correction
00155    * \param which_adc   which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
00156    * \param offset      16-bit value to subtract from raw ADC input.
00157    */
00158   bool set_adc_offset (int which_adc, int offset);
00159 
00160   /*!
00161    * \brief Set DAC offset correction
00162    * \param which_dac   which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
00163    * \param offset      10-bit offset value (ambiguous format:  See AD9862 datasheet).
00164    * \param offset_pin  1-bit value.  If 0 offset applied to -ve differential pin;
00165    *                                  If 1 offset applied to +ve differential pin.
00166    */
00167   bool set_dac_offset (int which_dac, int offset, int offset_pin);
00168 
00169   /*!
00170    * \brief Control ADC input buffer
00171    * \param which_adc   which ADC[0,3]
00172    * \param bypass      if non-zero, bypass input buffer and connect input
00173    *                    directly to switched cap SHA input of RxPGA.
00174    */
00175   bool set_adc_buffer_bypass (int which_adc, bool bypass);
00176 
00177   /*!
00178    * \brief Enable/disable automatic DC offset removal control loop in FPGA
00179    *
00180    * \param bits  which control loops to enable
00181    * \param mask  which \p bits to pay attention to
00182    *
00183    * If the corresponding bit is set, enable the automatic DC
00184    * offset correction control loop.
00185    *
00186    * <pre>
00187    * The 4 low bits are significant:
00188    *
00189    *   ADC0 = (1 << 0)
00190    *   ADC1 = (1 << 1)
00191    *   ADC2 = (1 << 2)
00192    *   ADC3 = (1 << 3)
00193    * </pre>
00194    *
00195    * By default the control loop is enabled on all ADC's.
00196    */
00197   bool set_dc_offset_cl_enable(int bits, int mask);
00198 
00199   /*!
00200    * \brief return the usrp's serial number.
00201    *
00202    * \returns non-zero length string iff successful.
00203    */
00204   std::string serial_number();
00205 
00206   /*!
00207    * \brief Return daughterboard ID for given side [0,1].
00208    *
00209    * \param which_side  [0,1] which daughterboard
00210    *
00211    * \return daughterboard id >= 0 if successful
00212    * \return -1 if no daugherboard
00213    * \return -2 if invalid EEPROM on daughterboard
00214    */
00215   virtual int daughterboard_id (int which_side) const;
00216 
00217   /*!
00218    * \brief Clock ticks to delay rising of T/R signal
00219    * \sa write_atr_mask, write_atr_txval, write_atr_rxval
00220    */
00221   bool write_atr_tx_delay(int value);
00222 
00223   /*!
00224    * \brief Clock ticks to delay falling edge of T/R signal
00225    * \sa write_atr_mask, write_atr_txval, write_atr_rxval
00226    */
00227   bool write_atr_rx_delay(int value);
00228 
00229   /*!
00230    * \brief Set Programmable Gain Amplifier (PGA)
00231    *
00232    * \param which_amp   which amp [0,3]
00233    * \param gain_in_db  gain value (linear in dB)
00234    *
00235    * gain is rounded to closest setting supported by hardware.
00236    *
00237    * \returns true iff sucessful.
00238    *
00239    * \sa pga_min(), pga_max(), pga_db_per_step()
00240    */
00241   bool set_pga (int which_amp, double gain_in_db);
00242 
00243   /*!
00244    * \brief Return programmable gain amplifier gain setting in dB.
00245    *
00246    * \param which_amp   which amp [0,3]
00247    */
00248   double pga (int which_amp) const;
00249 
00250   /*!
00251    * \brief Return minimum legal PGA gain in dB.
00252    */
00253   double pga_min () const;
00254 
00255   /*!
00256    * \brief Return maximum legal PGA gain in dB.
00257    */
00258   double pga_max () const;
00259 
00260   /*!
00261    * \brief Return hardware step size of PGA (linear in dB).
00262    */
00263   double pga_db_per_step () const;
00264 
00265   /*!
00266    * \brief Write direction register (output enables) for pins that go to daughterboard.
00267    *
00268    * \param which_side  [0,1] which size
00269    * \param value       value to write into register
00270    * \param mask        which bits of value to write into reg
00271    *
00272    * Each d'board has 16-bits of general purpose i/o.
00273    * Setting the bit makes it an output from the FPGA to the d'board.
00274    *
00275    * This register is initialized based on a value stored in the
00276    * d'board EEPROM.  In general, you shouldn't be using this routine
00277    * without a very good reason.  Using this method incorrectly will
00278    * kill your USRP motherboard and/or daughterboard.
00279    */
00280   bool _write_oe (int which_side, int value, int mask);
00281 
00282   /*!
00283    * \brief Write daughterboard i/o pin value
00284    *
00285    * \param which_side  [0,1] which d'board
00286    * \param value       value to write into register
00287    * \param mask        which bits of value to write into reg
00288    */
00289   bool write_io (int which_side, int value, int mask);
00290 
00291   /*!
00292    * \brief Read daughterboard i/o pin value
00293    *
00294    * \param which_side  [0,1] which d'board
00295    * \returns register value if successful, else READ_FAILED
00296    */
00297   int read_io (int which_side);
00298 
00299   /*!
00300    * \brief Write daughterboard refclk config register
00301    *
00302    * \param which_side  [0,1] which d'board
00303    * \param value       value to write into register, see below
00304    *
00305    * <pre>
00306    * Control whether a reference clock is sent to the daughterboards,
00307    * and what frequency.  The refclk is sent on d'board i/o pin 0.
00308    * 
00309    *     3                   2                   1                       
00310    *   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
00311    *  +-----------------------------------------------+-+------------+
00312    *  |             Reserved (Must be zero)           |E|   DIVISOR  |
00313    *  +-----------------------------------------------+-+------------+
00314    * 
00315    *  Bit 7  -- 1 turns on refclk, 0 allows IO use
00316    *  Bits 6:0 Divider value
00317    * </pre>
00318    */
00319   bool write_refclk(int which_side, int value);
00320 
00321   bool write_atr_mask(int which_side, int value);
00322   bool write_atr_txval(int which_side, int value);
00323   bool write_atr_rxval(int which_side, int value);
00324 
00325   /*!
00326    * \brief Write auxiliary digital to analog converter.
00327    *
00328    * \param which_side  [0,1] which d'board
00329    *                    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
00330    *                    SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
00331    * \param which_dac   [2,3] TX slots must use only 2 and 3.
00332    * \param value       [0,4095]
00333    * \returns true iff successful
00334    */
00335   bool write_aux_dac (int which_side, int which_dac, int value);
00336 
00337   /*!
00338    * \brief Read auxiliary analog to digital converter.
00339    *
00340    * \param which_side  [0,1] which d'board
00341    * \param which_adc   [0,1]
00342    * \returns value in the range [0,4095] if successful, else READ_FAILED.
00343    */
00344   int read_aux_adc (int which_side, int which_adc);
00345 
00346   /*!
00347    * \brief returns A/D or D/A converter rate in Hz
00348    */
00349   long converter_rate() const;
00350 
00351 
00352   // ----------------------------------------------------------------
00353   // Low level implementation routines.
00354   // You probably shouldn't be using these...
00355   //
00356 
00357   bool _set_led (int which_led, bool on);
00358 
00359   /*!
00360    * \brief Write FPGA register.
00361    * \param regno       7-bit register number
00362    * \param value       32-bit value
00363    * \returns true iff successful
00364    */
00365   bool _write_fpga_reg (int regno, int value);  //< 7-bit regno, 32-bit value
00366 
00367   /*!
00368    * \brief Read FPGA register.
00369    * \param regno       7-bit register number
00370    * \param value       32-bit value
00371    * \returns true iff successful
00372    */
00373   bool _read_fpga_reg (int regno, int *value);  //< 7-bit regno, 32-bit value
00374 
00375   /*!
00376    * \brief Read FPGA register.
00377    * \param regno       7-bit register number
00378    * \returns register value if successful, else READ_FAILED
00379    */
00380   int  _read_fpga_reg (int regno);
00381 
00382   /*!
00383    * \brief Write FPGA register with mask.
00384    * \param regno       7-bit register number
00385    * \param value       16-bit value
00386    * \param mask        16-bit value
00387    * \returns true if successful
00388    * Only use this for registers who actually implement a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
00389    */
00390   bool _write_fpga_reg_masked (int regno, int value, int mask);
00391 
00392   /*!
00393    * \brief Write AD9862 register.
00394    * \param which_codec 0 or 1
00395    * \param regno       6-bit register number
00396    * \param value       8-bit value
00397    * \returns true iff successful
00398    */
00399   bool _write_9862 (int which_codec, int regno, unsigned char value);
00400 
00401   /*!
00402    * \brief Read AD9862 register.
00403    * \param which_codec 0 or 1
00404    * \param regno       6-bit register number
00405    * \returns register value if successful, else READ_FAILED
00406    */
00407   int  _read_9862 (int which_codec, int regno) const;
00408 
00409   /*!
00410    * \brief Write data to SPI bus peripheral.
00411    *
00412    * \param optional_header     0,1 or 2 bytes to write before buf.
00413    * \param enables             bitmask of peripherals to write. See usrp_spi_defs.h
00414    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
00415    * \param buf                 the data to write
00416    * \returns true iff successful
00417    * Writes are limited to a maximum of 64 bytes.
00418    *
00419    * If \p format specifies that optional_header bytes are present, they are
00420    * written to the peripheral immediately prior to writing \p buf.
00421    */
00422   bool _write_spi (int optional_header, int enables, int format, std::string buf);
00423 
00424   /*
00425    * \brief Read data from SPI bus peripheral.
00426    *
00427    * \param optional_header     0,1 or 2 bytes to write before buf.
00428    * \param enables             bitmask of peripheral to read. See usrp_spi_defs.h
00429    * \param format              transaction format.  See usrp_spi_defs.h SPI_FMT_*
00430    * \param len                 number of bytes to read.  Must be in [0,64].
00431    * \returns the data read if sucessful, else a zero length string.
00432    *
00433    * Reads are limited to a maximum of 64 bytes.
00434    *
00435    * If \p format specifies that optional_header bytes are present, they
00436    * are written to the peripheral first.  Then \p len bytes are read from
00437    * the peripheral and returned.
00438    */
00439   std::string _read_spi (int optional_header, int enables, int format, int len);
00440 
00441   /*!
00442    * Return an existing daughterboard from list of candidate dbids, or the first found
00443    * on side A or side B.
00444    *
00445    * \param candidates          Vector of candidate dbids
00446    * 
00447    * Throws std::runtime_error if not found
00448    */
00449   usrp_subdev_spec pick_subdev(std::vector<int> candidates=std::vector<int>(0));
00450 };
00451 
00452 #endif /* INCLUDED_USRP_BASE_H */