GNU Radio 3.4.2 C++ API
usrp_prims.h
Go to the documentation of this file.
00001 /*  -*- c++ -*- */
00002 /*
00003  * Copyright 2005,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
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_USRP_PRIMS_H
00024 #define INCLUDED_USRP_PRIMS_H
00025 
00026 #include <usrp/usrp_slots.h>
00027 #include <usrp/libusb_types.h>
00028 #include <string>
00029 
00030 struct libusb_context;
00031 
00032 static const int USRP_HASH_SIZE = 16;
00033 
00034 enum usrp_load_status_t { ULS_ERROR = 0, ULS_OK, ULS_ALREADY_LOADED };
00035 
00036 /*!
00037  * \brief initialize libusb; Behavior differs depending on libusb version
00038  *
00039  * libusb-0.12
00040  *
00041  * Probe busses and devices. The argument is ignored and defaults to NULL.
00042  * Safe to call more than once.
00043  *
00044  * libusb-1.0
00045  *
00046  * If an location to a libusb_context is passed in, create and write in the new
00047  * context. If no argument is provided, initialize libusb with the default
00048  * (NULL) context.
00049  *
00050  * Generally _not_ safe to call more than once with non-NULL argument since a
00051  * new context will be created each time.
00052  */
00053 
00054 void usrp_one_time_init (libusb_context **ctx = NULL);
00055 
00056 /*!
00057  * \brief deinitialize libusb
00058  *
00059  * libusb-0.1: No effect
00060  *
00061  * libusb-1.0: Deinitialize context ctx 
00062  */
00063 void usrp_deinit (libusb_context *ctx);
00064 
00065 /*
00066  * force a rescan of the buses and devices
00067  */
00068 void usrp_rescan ();
00069 
00070 /*!
00071  * \brief locate Nth (zero based) USRP device in system.
00072  * Return pointer or 0 if not found.
00073  *
00074  * The following kinds of devices are considered USRPs:
00075  *
00076  *   unconfigured USRP (no firwmare loaded)
00077  *   configured USRP (firmware loaded)
00078  *   unconfigured Cypress FX2 (only if fx2_ok_p is true)
00079  */
00080 libusb_device *usrp_find_device (int nth, bool fx2_ok_p = false, libusb_context *ctx = NULL);
00081 
00082 bool usrp_usrp_p (libusb_device *q);            //< is this a USRP
00083 bool usrp_usrp0_p (libusb_device *q);           //< is this a USRP Rev 0
00084 bool usrp_usrp1_p (libusb_device *q);           //< is this a USRP Rev 1
00085 bool usrp_usrp2_p (libusb_device *q);           //< is this a USRP Rev 2
00086 int  usrp_hw_rev (libusb_device *q);            //< return h/w rev code
00087 
00088 bool usrp_fx2_p (libusb_device *q);                     //< is this an unconfigured Cypress FX2
00089 
00090 bool usrp_unconfigured_usrp_p (libusb_device *q);       //< some kind of unconfigured USRP
00091 bool usrp_configured_usrp_p (libusb_device *q); //< some kind of configured USRP
00092 
00093 /*!
00094  * \brief given a libusb_device return an instance of the appropriate libusb_device_handle
00095  *
00096  * These routines claim the specified interface and select the
00097  * correct alternate interface.  (USB nomenclature is totally screwed!)
00098  *
00099  * If interface can't be opened, or is already claimed by some other
00100  * process, 0 is returned.
00101  */
00102 libusb_device_handle *usrp_open_cmd_interface (libusb_device *dev);
00103 libusb_device_handle *usrp_open_rx_interface (libusb_device *dev);
00104 libusb_device_handle *usrp_open_tx_interface (libusb_device *dev);
00105 
00106 /*!
00107  * \brief close interface.
00108  */
00109 bool usrp_close_interface (libusb_device_handle *udh);
00110 
00111 /*!
00112  * \brief load intel hex format file into USRP/Cypress FX2 (8051).
00113  *
00114  * The filename extension is typically *.ihx
00115  *
00116  * Note that loading firmware may cause the device to renumerate.  I.e.,
00117  * change its configuration, invalidating the current device handle.
00118  */
00119 
00120 usrp_load_status_t 
00121 usrp_load_firmware (libusb_device_handle *udh, const char *filename, bool force);
00122 
00123 /*!
00124  * \brief load intel hex format file into USRP FX2 (8051).
00125  *
00126  * The filename extension is typically *.ihx
00127  *
00128  * Note that loading firmware may cause the device to renumerate.  I.e.,
00129  * change its configuration, invalidating the current device handle.
00130  * If the result is ULS_OK, usrp_load_firmware_nth delays 1 second
00131  * then rescans the busses and devices.
00132  */
00133 usrp_load_status_t
00134 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx = NULL);
00135 
00136 /*!
00137  * \brief load fpga configuration bitstream
00138  */
00139 usrp_load_status_t
00140 usrp_load_fpga (libusb_device_handle *udh, const char *filename, bool force);
00141 
00142 /*!
00143  * \brief load the regular firmware and fpga bitstream in the Nth USRP.
00144  *
00145  * This is the normal starting point...
00146  */
00147 bool usrp_load_standard_bits (int nth, bool force,
00148                               const std::string fpga_filename = "",
00149                               const std::string firmware_filename = "",
00150                               libusb_context *ctx = NULL);
00151 
00152 /*!
00153  * \brief copy the given \p hash into the USRP hash slot \p which.
00154  * The usrp implements two hash slots, 0 and 1.
00155  */
00156 bool usrp_set_hash (libusb_device_handle *udh, int which,
00157                     const unsigned char hash[USRP_HASH_SIZE]);
00158 
00159 /*!
00160  * \brief retrieve the \p hash from the USRP hash slot \p which.
00161  * The usrp implements two hash slots, 0 and 1.
00162  */
00163 bool usrp_get_hash (libusb_device_handle *udh, int which,
00164                     unsigned char hash[USRP_HASH_SIZE]);
00165 
00166 bool usrp_write_fpga_reg (libusb_device_handle *udh, int reg, int value);
00167 bool usrp_read_fpga_reg (libusb_device_handle *udh, int reg, int *value);
00168 bool usrp_set_fpga_reset (libusb_device_handle *udh, bool on);
00169 bool usrp_set_fpga_tx_enable (libusb_device_handle *udh, bool on);
00170 bool usrp_set_fpga_rx_enable (libusb_device_handle *udh, bool on);
00171 bool usrp_set_fpga_tx_reset (libusb_device_handle *udh, bool on);
00172 bool usrp_set_fpga_rx_reset (libusb_device_handle *udh, bool on);
00173 bool usrp_set_led (libusb_device_handle *udh, int which, bool on);
00174 
00175 bool usrp_check_rx_overrun (libusb_device_handle *udh, bool *overrun_p);
00176 bool usrp_check_tx_underrun (libusb_device_handle *udh, bool *underrun_p);
00177 
00178 // i2c_read and i2c_write are limited to a maximum len of 64 bytes.
00179 
00180 bool usrp_i2c_write (libusb_device_handle *udh, int i2c_addr,
00181                      const void *buf, int len);
00182 
00183 bool usrp_i2c_read (libusb_device_handle *udh, int i2c_addr,
00184                     void *buf, int len);
00185 
00186 // spi_read and spi_write are limited to a maximum of 64 bytes
00187 // See usrp_spi_defs.h for more info
00188 
00189 bool usrp_spi_write (libusb_device_handle *udh,
00190                      int optional_header, int enables, int format,
00191                      const void *buf, int len);
00192 
00193 bool usrp_spi_read (libusb_device_handle *udh,
00194                      int optional_header, int enables, int format,
00195                      void *buf, int len);
00196 
00197 
00198 bool usrp_9862_write (libusb_device_handle *udh,
00199                       int which_codec,                  // [0,  1]
00200                       int regno,                        // [0, 63]
00201                       int value);                       // [0, 255]     
00202 
00203 bool usrp_9862_read (libusb_device_handle *udh,
00204                      int which_codec,                   // [0,  1]
00205                      int regno,                         // [0, 63]
00206                      unsigned char *value);             // [0, 255]
00207 
00208 /*!
00209  * \brief Write multiple 9862 regs at once.
00210  *
00211  * \p buf contains alternating register_number, register_value pairs.
00212  * \p len must be even and is the length of buf in bytes.
00213  */
00214 bool usrp_9862_write_many (libusb_device_handle *udh, int which_codec,
00215                            const unsigned char *buf, int len);
00216                            
00217 
00218 /*!
00219  * \brief write specified regs to all 9862's in the system
00220  */
00221 bool usrp_9862_write_many_all (libusb_device_handle *udh,
00222                                const unsigned char *buf, int len);
00223                            
00224 
00225 // Write 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
00226 // Which EEPROM is determined by i2c_addr.  See i2c_addr.h
00227 
00228 bool usrp_eeprom_write (libusb_device_handle *udh, int i2c_addr,
00229                         int eeprom_offset, const void *buf, int len);
00230 
00231 
00232 // Read 24LC024 / 24LC025 EEPROM on motherboard or daughterboard.
00233 // Which EEPROM is determined by i2c_addr.  See i2c_addr.h
00234 
00235 bool usrp_eeprom_read (libusb_device_handle *udh, int i2c_addr,
00236                        int eeprom_offset, void *buf, int len);
00237 
00238 
00239 // Slot specific i/o routines
00240 
00241 /*!
00242  * \brief write to the specified aux dac.
00243  *
00244  * \p slot: which Tx or Rx slot to write.
00245  *    N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's
00246  *          SLOT_TX_B and SLOT_RX_B share the same AUX DAC's
00247  *
00248  * \p which_dac: [0,3]  RX slots must use only 0 and 1.
00249  *                      TX slots must use only 2 and 3.
00250  *
00251  * AUX DAC 3 is really the 9862 sigma delta output.
00252  *
00253  * \p value to write to aux dac.  All dacs take straight
00254  * binary values.  Although dacs 0, 1 and 2 are 8-bit and dac 3 is 12-bit,
00255  * the interface is in terms of 12-bit values [0,4095]
00256  */
00257 bool usrp_write_aux_dac (libusb_device_handle *uhd, int slot,
00258                          int which_dac, int value);
00259 
00260 /*!
00261  * \brief Read the specified aux adc
00262  *
00263  * \p slot: which Tx or Rx slot to read aux dac
00264  * \p which_adc: [0,1]  which of the two adcs to read
00265  * \p *value: return value, 12-bit straight binary.
00266  */
00267 bool usrp_read_aux_adc (libusb_device_handle *udh, int slot,
00268                         int which_adc, int *value);
00269 
00270 
00271 /*!
00272  * \brief usrp daughterboard id to name mapping
00273  */
00274 const std::string usrp_dbid_to_string (int dbid);
00275 
00276 
00277 enum usrp_dbeeprom_status_t { UDBE_OK, UDBE_BAD_SLOT, UDBE_NO_EEPROM, UDBE_INVALID_EEPROM };
00278 
00279 struct usrp_dboard_eeprom {
00280   unsigned short        id;             // d'board identifier code
00281   unsigned short        oe;             // fpga output enables:
00282                                         //   If bit set, i/o pin is an output from FPGA.
00283   short                 offset[2];      // ADC/DAC offset correction
00284 };
00285 
00286 /*!
00287  * \brief Read and return parsed daughterboard eeprom
00288  */
00289 usrp_dbeeprom_status_t
00290 usrp_read_dboard_eeprom (libusb_device_handle *udh,
00291                          int slot_id, usrp_dboard_eeprom *eeprom);
00292 
00293 /*!
00294  * \brief write ADC/DAC offset calibration constants to d'board eeprom
00295  */
00296 bool usrp_write_dboard_offsets (libusb_device_handle *udh, int slot_id,
00297                                 short offset0, short offset1);
00298 
00299 /*!
00300  * \brief return a usrp's serial number.
00301  *
00302  * Note that this only works on a configured usrp.
00303  * \returns non-zero length string iff successful.
00304  */
00305 std::string usrp_serial_number(libusb_device_handle *udh);
00306 
00307 #endif /* INCLUDED_USRP_PRIMS_H */