GNU Radio 3.4.0 C++ API
|
00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2008,2009 Free Software Foundation, Inc. 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation, either version 3 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #ifndef INCLUDED_USRP2_H 00020 #define INCLUDED_USRP2_H 00021 00022 #include <boost/shared_ptr.hpp> 00023 #include <boost/utility.hpp> 00024 #include <boost/bind.hpp> 00025 #include <vector> 00026 #include <complex> 00027 #include <usrp2/rx_sample_handler.h> 00028 #include <usrp2/tune_result.h> 00029 #include <usrp2/mimo_config.h> 00030 00031 /* 00032 * N.B., The interfaces described here are still in flux. 00033 * 00034 * We will keep all the code in the tree up-to-date with regard to changes 00035 * here, but reserve the right to change this on a whim. 00036 */ 00037 00038 namespace usrp2 { 00039 00040 /*! 00041 * Structure to hold properties of USRP2 hardware devices. 00042 * 00043 */ 00044 struct props 00045 { 00046 std::string addr; 00047 uint16_t hw_rev; 00048 uint8_t fpga_md5sum[16]; 00049 uint8_t sw_md5sum[16]; 00050 }; 00051 00052 typedef std::vector<props> props_vector_t; 00053 00054 /*! 00055 * \brief Search the ethernet for all USRP2s or for a specific USRP2. 00056 * 00057 * \param ifc is the name of the OS ethernet interface (e.g., "eth0") 00058 * \param mac_addr is the MAC address of the desired USRP2, or "" to search for all. 00059 * mac_addr must be either a zero length string, "", or must be of the form 00060 * "01:02:03:04:05:06" or "05:06". 00061 * 00062 * \returns a vector of properties, 1 entry for each matching USRP2 found. 00063 */ 00064 props_vector_t find(const std::string &ifc, const std::string &mac_addr=""); 00065 00066 class tune_result; 00067 00068 // FIXME: get from firmware include 00069 static const int GPIO_TX_BANK = 0; 00070 static const int GPIO_RX_BANK = 1; 00071 00072 /*! 00073 * \brief standard C++ interface to USRP2 00074 * \ingroup usrp2 00075 */ 00076 class usrp2 : boost::noncopyable 00077 { 00078 public: 00079 static const unsigned int MAX_CHAN = 30; 00080 00081 /*! 00082 * Shared pointer to this class 00083 */ 00084 typedef boost::shared_ptr<usrp2> sptr; 00085 00086 /*! 00087 * Static function to return an instance of usrp2 as a shared pointer 00088 * 00089 * \param ifc Network interface name, e.g., "eth0" 00090 * \param addr Network mac address, e.g., "01:23:45:67:89:ab", "89:ab" or "". 00091 * If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH 00092 * "" will autoselect a USRP2 if there is only a single one on the local ethernet. 00093 * \param rx_bufsize is the length in bytes of the kernel networking buffer to allocate. 00094 */ 00095 static sptr make(const std::string &ifc, const std::string &addr="", size_t rx_bufsize=0); 00096 00097 /*! 00098 * Class destructor 00099 */ 00100 ~usrp2(); 00101 00102 /*! 00103 * Returns the MAC address associated with this USRP 00104 */ 00105 std::string mac_addr(); 00106 00107 /*! 00108 * Returns the GbE interface name associated with this USRP 00109 */ 00110 std::string interface_name(); 00111 00112 /* 00113 * ---------------------------------------------------------------- 00114 * Rx configuration and control 00115 * ---------------------------------------------------------------- 00116 */ 00117 00118 /*! 00119 * Set the rx antenna 00120 */ 00121 bool set_rx_antenna(int ant); 00122 00123 /*! 00124 * Set receiver gain 00125 * \param gain in dB (more or less) 00126 */ 00127 bool set_rx_gain(double gain); 00128 00129 //! return minimum Rx gain 00130 double rx_gain_min(); 00131 00132 //! return maximum Rx gain 00133 double rx_gain_max(); 00134 00135 //! return Rx gain db_per_step 00136 double rx_gain_db_per_step(); 00137 00138 /*! 00139 * \brief Set receive daughterboard LO offset frequency 00140 */ 00141 bool set_rx_lo_offset(double frequency); 00142 00143 /*! 00144 * Set receiver center frequency 00145 */ 00146 bool set_rx_center_freq(double frequency, tune_result *result); 00147 00148 //! return minimum Rx center frequency 00149 double rx_freq_min(); 00150 00151 //! return maximum Rx center frequency 00152 double rx_freq_max(); 00153 00154 /*! 00155 * Set receiver sample rate decimation 00156 */ 00157 bool set_rx_decim(int decimation_factor); 00158 00159 //! Return current decimation factor 00160 int rx_decim(); 00161 00162 /*! 00163 * Set receiver IQ magnitude scaling 00164 */ 00165 bool set_rx_scale_iq(int scale_i, int scale_q); 00166 00167 /*! 00168 * Set received sample format 00169 * 00170 * domain: complex or real 00171 * type: floating, fixed point, or raw 00172 * depth: bits per sample 00173 * 00174 * Sets format over the wire for samples from USRP2. 00175 */ 00176 // bool set_rx_format(...); 00177 00178 /*! 00179 * Start streaming receive mode. USRP2 will send a continuous stream of 00180 * DSP pipeline samples to host. Call rx_samples(...) to access. 00181 * 00182 * \param channel Stream channel number (0-30) 00183 * \param items_per_frame Number of 32-bit items per frame. 00184 */ 00185 bool start_rx_streaming(unsigned int channel=0, unsigned int items_per_frame=0); 00186 00187 /*! 00188 * Start streaming receive mode at specified timestamp. USRP2 will send a 00189 * continuous stream of DSP pipeline samples to host. Call rx_samples(...) 00190 * to access. 00191 * 00192 * \param channel Stream channel number (0-30) 00193 * \param items_per_frame Number of 32-bit items per frame. 00194 * \param time Timestamp to start streaming at 00195 */ 00196 bool start_rx_streaming_at(unsigned int channel=0, unsigned int items_per_frame=0, unsigned int time=0); 00197 00198 /*! 00199 * Sync to PPS and start streaming receive mode at specified timestamp. 00200 * Just like calling sync_to_pps() and start_rx_streaming_at(). 00201 * 00202 * \param channel Stream channel number (0-30) 00203 * \param items_per_frame Number of 32-bit items per frame. 00204 * \param time Timestamp to start streaming at 00205 */ 00206 bool sync_and_start_rx_streaming_at(unsigned int channel=0, unsigned int items_per_frame=0, uint32_t time=0); 00207 00208 /*! 00209 * Stop streaming receive mode. 00210 */ 00211 bool stop_rx_streaming(unsigned int channel=0); 00212 00213 /*! 00214 * \brief Receive data from the specified channel 00215 * This method is used to receive all data: streaming or discrete. 00216 */ 00217 bool rx_samples(unsigned int channel, rx_sample_handler *handler); 00218 00219 /*! 00220 * Returns number of times receive overruns have occurred 00221 */ 00222 unsigned int rx_overruns(); 00223 00224 /*! 00225 * Returns total number of missing frames from overruns. 00226 */ 00227 unsigned int rx_missing(); 00228 00229 /* 00230 * ---------------------------------------------------------------- 00231 * Tx configuration and control 00232 * ---------------------------------------------------------------- 00233 */ 00234 00235 /*! 00236 * Set the tx antenna 00237 */ 00238 bool set_tx_antenna(int ant); 00239 00240 /*! 00241 * Set transmitter gain 00242 */ 00243 bool set_tx_gain(double gain); 00244 00245 //! return minimum Tx gain 00246 double tx_gain_min(); 00247 00248 //! return maximum Tx gain 00249 double tx_gain_max(); 00250 00251 //! return Tx gain db_per_step 00252 double tx_gain_db_per_step(); 00253 00254 /* 00255 * \brief Set transmit daughterboard LO offset frequency 00256 */ 00257 bool set_tx_lo_offset(double frequency); 00258 00259 /*! 00260 * Set transmitter center frequency 00261 */ 00262 bool set_tx_center_freq(double frequency, tune_result *result); 00263 00264 //! return minimum Tx center frequency 00265 double tx_freq_min(); 00266 00267 //! return maximum Tx center frequency 00268 double tx_freq_max(); 00269 00270 /*! 00271 * Set transmitter sample rate interpolation 00272 */ 00273 bool set_tx_interp(int interpolation_factor); 00274 00275 //! Return current interpolation factor 00276 int tx_interp(); 00277 00278 /* 00279 * \brief Calculate default scale_iq for given interpolation rate 00280 */ 00281 void default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q); 00282 00283 /*! 00284 * Set transmit IQ magnitude scaling 00285 */ 00286 bool set_tx_scale_iq(int scale_i, int scale_q); 00287 00288 /*! 00289 * Set transmit sample format 00290 * 00291 * domain: complex or real 00292 * type: floating, fixed point, or raw 00293 * depth: bits per sample 00294 * 00295 * Sets format over the wire for samples to USRP2. 00296 */ 00297 // bool set_tx_format(...); 00298 00299 /*! 00300 * \brief transmit complex<float> samples to USRP2 00301 * 00302 * \param channel specifies the channel to send them to 00303 * \param samples are the samples to transmit. They should be in the range [-1.0, +1.0] 00304 * \param nsamples is the number of samples to transmit 00305 * \param metadata provides the timestamp and flags 00306 * 00307 * The complex<float> samples are converted to the appropriate 00308 * "on the wire" representation, depending on the current USRP2 00309 * configuration. Typically, this is big-endian 16-bit I & Q. 00310 */ 00311 bool tx_32fc(unsigned int channel, 00312 const std::complex<float> *samples, 00313 size_t nsamples, 00314 const tx_metadata *metadata); 00315 00316 /*! 00317 * \brief transmit complex<int16_t> samples to USRP2 00318 * 00319 * \param channel specifies the channel to send them to 00320 * \param samples are the samples to transmit 00321 * \param nsamples is the number of samples to transmit 00322 * \param metadata provides the timestamp and flags 00323 * 00324 * The complex<int16_t> samples are converted to the appropriate 00325 * "on the wire" representation, depending on the current USRP2 00326 * configuration. Typically, this is big-endian 16-bit I & Q. 00327 */ 00328 bool tx_16sc(unsigned int channel, 00329 const std::complex<int16_t> *samples, 00330 size_t nsamples, 00331 const tx_metadata *metadata); 00332 00333 /*! 00334 * \brief transmit raw uint32_t data items to USRP2 00335 * 00336 * The caller is responsible for ensuring that the items are 00337 * formatted appropriately for the USRP2 and its configuration. 00338 * This method is used primarily by the system itself. Users 00339 * should call tx_32fc or tx_16sc instead. 00340 * 00341 * \param channel specifies the channel to send them to 00342 * \param items are the data items to transmit 00343 * \param nitems is the number of items to transmit 00344 * \param metadata provides the timestamp and flags 00345 */ 00346 bool tx_raw(unsigned int channel, 00347 const uint32_t *items, 00348 size_t nitems, 00349 const tx_metadata *metadata); 00350 00351 /* 00352 * ---------------------------------------------------------------- 00353 * miscellaneous methods 00354 * ---------------------------------------------------------------- 00355 */ 00356 00357 /*! 00358 * \brief MIMO configuration 00359 * 00360 * \param flags from usrp2_mimo_config.h 00361 * 00362 * <pre> 00363 * one of these: 00364 * 00365 * MC_WE_DONT_LOCK 00366 * MC_WE_LOCK_TO_SMA 00367 * MC_WE_LOCK_TO_MIMO 00368 * 00369 * and optionally this: 00370 * 00371 * MC_PROVIDE_CLK_TO_MIMO 00372 * </pre> 00373 */ 00374 bool config_mimo(int flags); 00375 00376 00377 //! Get frequency of master oscillator in Hz 00378 bool fpga_master_clock_freq(long *freq); 00379 00380 // Get Sampling rate of A/D converter in Hz 00381 bool adc_rate(long *rate); 00382 00383 // Get Sampling rate of D/A converter in Hz 00384 bool dac_rate(long *rate); 00385 00386 /*! 00387 * \brief Get Tx daughterboard ID 00388 * 00389 * \param[out] dbid returns the daughterboard id. 00390 * 00391 * daughterboard id >= 0 if successful, -1 if no daugherboard installed, 00392 * -2 if invalid EEPROM on daughterboard. 00393 */ 00394 bool tx_daughterboard_id(int *dbid); 00395 00396 /*! 00397 * \brief Get Rx daughterboard ID 00398 * 00399 * \param[out] dbid returns the daughterboard id. 00400 * 00401 * daughterboard id >= 0 if successful, -1 if no daugherboard installed, 00402 * -2 if invalid EEPROM on daughterboard. 00403 */ 00404 bool rx_daughterboard_id(int *dbid); 00405 00406 /* 00407 * ---------------------------------------------------------------- 00408 * Low level methods 00409 * ---------------------------------------------------------------- 00410 */ 00411 00412 /*! 00413 * Burn new mac address into EEPROM on USRP2 00414 * 00415 * \param new_addr Network mac address, e.g., "01:23:45:67:89:ab" or "89:ab". 00416 * If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH 00417 */ 00418 bool burn_mac_addr(const std::string &new_addr); 00419 00420 /*! 00421 * Reset master time to 0 at next PPS edge 00422 */ 00423 bool sync_to_pps(); 00424 00425 /*! 00426 * Reset master time to 0 at every PPS edge 00427 */ 00428 bool sync_every_pps(bool enable); 00429 00430 /*! 00431 * Read memory from Wishbone bus as 32-bit words. Handles endian swapping if needed. 00432 * 00433 * \param addr 32-bit aligned address. Only the lower 16-bits are significant. 00434 * \param words Number of 32-bit words 00435 * 00436 * \returns Vector of 32-bit read values 00437 * 00438 * WARNING: Attempts to read memory from addresses that do not correspond to RAM or 00439 * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle. 00440 * 00441 */ 00442 std::vector<uint32_t> peek32(uint32_t addr, uint32_t words); 00443 00444 /*! 00445 * Write memory to Wishbone bus as 32-bit words. Handles endian swapping if needed. 00446 * 00447 * \param addr 32-bit aligned address. Only the lower 16-bits are significant 00448 * \param data Vector of 32-bit values to write. 00449 * 00450 * \returns true iff successful 00451 * 00452 * WARNING: Attempts to read memory from addresses that do not correspond to RAM or 00453 * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle. 00454 * 00455 */ 00456 bool poke32(uint32_t addr, const std::vector<uint32_t> &data); 00457 00458 /*! 00459 * Set daughterboard GPIO data direction register. 00460 * 00461 * \param bank GPIO_TX_BANK or GPIO_RX_BANK 00462 * \param value 16-bits, 0=FPGA input, 1=FPGA output 00463 * \param mask 16-bits, 0=ignore, 1=set 00464 * 00465 * \returns true iff successful 00466 * 00467 * WARNING: Improper usage of this function may result in damage to the USRP2 00468 * 00469 */ 00470 bool set_gpio_ddr(int bank, uint16_t value, uint16_t mask); 00471 00472 /*! 00473 * Set daughterboard GPIO output selection register. For those GPIO pins that 00474 * are configured as outputs in the DDR, this settings configures the source 00475 * of the pin value. 00476 * 00477 * \param bank GPIO_TX_BANK or GPIO_RX_BANK 00478 * \param sels Exactly 16 character MSB->LSB string. For each position: 00479 * '.' = ignore this bit, i.e., leave current value 00480 * 'a' = Output ATR value 00481 * 's' = Output host software controlled value 00482 * '0' = Output FPGA debug bus 0 value 00483 * '1' = Output FPGA debug bus 1 value 00484 * 00485 * \returns true iff successful 00486 * 00487 * WARNING: Improper usage of this function may result in damage to the USRP2 00488 * 00489 */ 00490 bool set_gpio_sels(int bank, std::string sels); 00491 00492 /*! 00493 * Set daughterboard GPIO pin values. 00494 * 00495 * \param bank GPIO_TX_BANK or GPIO_RX_BANK 00496 * \param value 16 bits, 0=low, 1=high 00497 * \param mask 16 bits, 0=ignore, 1=set 00498 * 00499 * \returns true iff successful 00500 * 00501 * WARNING: Improper usage of this function may result in damage to the USRP2 00502 * 00503 */ 00504 bool write_gpio(int bank, uint16_t value, uint16_t mask); 00505 00506 /*! 00507 * Read daughterboard GPIO pin values 00508 * 00509 * \param bank GPIO_TX_BANK or GPIO_RX_BANK 00510 * \param value pointer to uint16_t to hold read results 00511 * 00512 * \returns true iff successful 00513 * 00514 */ 00515 bool read_gpio(int bank, uint16_t *value); 00516 00517 /*! 00518 * Set GPIO streaming mode 00519 * 00520 * Individually enables streaming GPIO pins through LSBs of DSP 00521 * samples. 00522 * 00523 * On receive, io_rx[15] replaces I[0], io_rx[14] replaces Q[0] 00524 * On transmit, I[0] maps to io_tx[15], Q[0] maps to io_tx[14] 00525 * (Transmit streaming is not yet implemented.) 00526 * 00527 * The selected GPIO pins must have been set as inputs or outputs 00528 * and, for transmit, set to software control. 00529 * 00530 * When enabled, the replaced DSP sample LSBs become 0. 00531 * 00532 * \param bank GPIO_TX_BANK or GPIO_RX_BANK 00533 * \param enable enable[0] controls I channel LSB 00534 * enable[1] controls Q channel LSB 00535 * 00536 * \returns true iff successful 00537 * 00538 * WARNING: Improper usage of this function may result in damage to the USRP2 00539 * 00540 */ 00541 bool enable_gpio_streaming(int bank, int enable); 00542 00543 #if 0 // not yet implemented 00544 /*! 00545 * \brief Write EEPROM on motherboard or any daughterboard. 00546 * \param i2c_addr I2C bus address of EEPROM 00547 * \param eeprom_offset byte offset in EEPROM to begin writing 00548 * \param buf the data to write 00549 * \returns true iff sucessful 00550 */ 00551 bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string &buf); 00552 00553 /*! 00554 * \brief Read EEPROM on motherboard or any daughterboard. 00555 * \param i2c_addr I2C bus address of EEPROM 00556 * \param eeprom_offset byte offset in EEPROM to begin reading 00557 * \param len number of bytes to read 00558 * \returns the data read if successful, else a zero length string. 00559 */ 00560 std::string read_eeprom (int i2c_addr, int eeprom_offset, int len); 00561 00562 /*! 00563 * \brief Write to I2C peripheral 00564 * \param i2c_addr I2C bus address (7-bits) 00565 * \param buf the data to write 00566 * \returns true iff successful 00567 * Writes are limited to a maximum of of 64 bytes. 00568 */ 00569 bool write_i2c (int i2c_addr, const std::string &buf); 00570 00571 /*! 00572 * \brief Read from I2C peripheral 00573 * \param i2c_addr I2C bus address (7-bits) 00574 * \param len number of bytes to read 00575 * \returns the data read if successful, else a zero length string. 00576 * Reads are limited to a maximum of 64 bytes. 00577 */ 00578 std::string read_i2c (int i2c_addr, int len); 00579 00580 /*! 00581 * \brief Write data to SPI bus peripheral. 00582 * 00583 * \param optional_header 0,1 or 2 bytes to write before buf. 00584 * \param enables bitmask of peripherals to write. See usrp_spi_defs.h 00585 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_* 00586 * \param buf the data to write 00587 * \returns true iff successful 00588 * Writes are limited to a maximum of 64 bytes. 00589 * 00590 * If \p format specifies that optional_header bytes are present, they are 00591 * written to the peripheral immediately prior to writing \p buf. 00592 */ 00593 bool write_spi (int optional_header, int enables, int format, const std::string &buf); 00594 00595 /* 00596 * \brief Read data from SPI bus peripheral. 00597 * 00598 * \param optional_header 0,1 or 2 bytes to write before buf. 00599 * \param enables bitmask of peripheral to read. See usrp_spi_defs.h 00600 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_* 00601 * \param len number of bytes to read. Must be in [0,64]. 00602 * \returns the data read if sucessful, else a zero length string. 00603 * 00604 * Reads are limited to a maximum of 64 bytes. 00605 * 00606 * If \p format specifies that optional_header bytes are present, they 00607 * are written to the peripheral first. Then \p len bytes are read from 00608 * the peripheral and returned. 00609 */ 00610 std::string read_spi (int optional_header, int enables, int format, int len); 00611 #endif 00612 00613 00614 class impl; // implementation details 00615 00616 private: 00617 // Static function to retrieve or create usrp2 instance 00618 static sptr find_existing_or_make_new(const std::string &ifc, props *p, size_t rx_bufsize); 00619 00620 // Only class members can instantiate this class 00621 usrp2(const std::string &ifc, props *p, size_t rx_bufsize); 00622 00623 // All private state is held in opaque pointer 00624 std::auto_ptr<impl> d_impl; 00625 }; 00626 00627 }; 00628 00629 std::ostream& operator<<(std::ostream &os, const usrp2::props &x); 00630 00631 00632 #endif /* INCLUDED_USRP2_H */