3 * Copyright 2008,2009 Free Software Foundation, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef INCLUDED_USRP2_H
20 #define INCLUDED_USRP2_H
22 #include <boost/shared_ptr.hpp>
23 #include <boost/utility.hpp>
26 #include <usrp2/rx_sample_handler.h>
27 #include <usrp2/tune_result.h>
28 #include <usrp2/mimo_config.h>
31 * N.B., The interfaces described here are still in flux.
33 * We will keep all the code in the tree up-to-date with regard to changes
34 * here, but reserve the right to change this on a whim.
40 * Structure to hold properties of USRP2 hardware devices.
47 uint8_t fpga_md5sum[16];
48 uint8_t sw_md5sum[16];
51 typedef std::vector<props> props_vector_t;
54 * \brief Search the ethernet for all USRP2s or for a specific USRP2.
56 * \param ifc is the name of the OS ethernet interface (e.g., "eth0")
57 * \param mac_addr is the MAC address of the desired USRP2, or "" to search for all.
58 * mac_addr must be either a zero length string, "", or must be of the form
59 * "01:02:03:04:05:06" or "05:06".
61 * \returns a vector of properties, 1 entry for each matching USRP2 found.
63 props_vector_t find(const std::string &ifc, const std::string &mac_addr="");
67 // FIXME: get from firmware include
68 static const int GPIO_TX_BANK = 0;
69 static const int GPIO_RX_BANK = 1;
72 * \brief standard C++ interface to USRP2
75 class usrp2 : boost::noncopyable
78 static const unsigned int MAX_CHAN = 30;
81 * Shared pointer to this class
83 typedef boost::shared_ptr<usrp2> sptr;
86 * Static function to return an instance of usrp2 as a shared pointer
88 * \param ifc Network interface name, e.g., "eth0"
89 * \param addr Network mac address, e.g., "01:23:45:67:89:ab", "89:ab" or "".
90 * If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
91 * "" will autoselect a USRP2 if there is only a single one on the local ethernet.
92 * \param rx_bufsize is the length in bytes of the kernel networking buffer to allocate.
94 static sptr make(const std::string &ifc, const std::string &addr="", size_t rx_bufsize=0);
102 * Returns the MAC address associated with this USRP
104 std::string mac_addr();
107 * Returns the GbE interface name associated with this USRP
109 std::string interface_name();
112 * ----------------------------------------------------------------
113 * Rx configuration and control
114 * ----------------------------------------------------------------
119 * \param gain in dB (more or less)
121 bool set_rx_gain(double gain);
123 //! return minimum Rx gain
124 double rx_gain_min();
126 //! return maximum Rx gain
127 double rx_gain_max();
129 //! return Rx gain db_per_step
130 double rx_gain_db_per_step();
133 * \brief Set receive daughterboard LO offset frequency
135 bool set_rx_lo_offset(double frequency);
138 * Set receiver center frequency
140 bool set_rx_center_freq(double frequency, tune_result *result);
142 //! return minimum Rx center frequency
143 double rx_freq_min();
145 //! return maximum Rx center frequency
146 double rx_freq_max();
149 * Set receiver sample rate decimation
151 bool set_rx_decim(int decimation_factor);
153 //! Return current decimation factor
157 * Set receiver IQ magnitude scaling
159 bool set_rx_scale_iq(int scale_i, int scale_q);
162 * Set received sample format
164 * domain: complex or real
165 * type: floating, fixed point, or raw
166 * depth: bits per sample
168 * Sets format over the wire for samples from USRP2.
170 // bool set_rx_format(...);
173 * Start streaming receive mode. USRP2 will send a continuous stream of
174 * DSP pipeline samples to host. Call rx_samples(...) to access.
176 * \param channel Stream channel number (0-30)
177 * \param items_per_frame Number of 32-bit items per frame.
179 bool start_rx_streaming(unsigned int channel=0, unsigned int items_per_frame=0);
182 * Start streaming receive mode at specified timestamp. USRP2 will send a
183 * continuous stream of DSP pipeline samples to host. Call rx_samples(...)
186 * \param channel Stream channel number (0-30)
187 * \param items_per_frame Number of 32-bit items per frame.
188 * \param time Timestamp to start streaming at
190 bool start_rx_streaming_at(unsigned int channel=0, unsigned int items_per_frame=0, unsigned int time=0);
193 * Sync to PPS and start streaming receive mode at specified timestamp.
194 * Just like calling sync_to_pps() and start_rx_streaming_at().
196 * \param channel Stream channel number (0-30)
197 * \param items_per_frame Number of 32-bit items per frame.
198 * \param time Timestamp to start streaming at
200 bool sync_and_start_rx_streaming_at(unsigned int channel=0, unsigned int items_per_frame=0, uint32_t time=0);
203 * Stop streaming receive mode.
205 bool stop_rx_streaming(unsigned int channel=0);
208 * \brief Receive data from the specified channel
209 * This method is used to receive all data: streaming or discrete.
211 bool rx_samples(unsigned int channel, rx_sample_handler *handler);
214 * Returns number of times receive overruns have occurred
216 unsigned int rx_overruns();
219 * Returns total number of missing frames from overruns.
221 unsigned int rx_missing();
224 * ----------------------------------------------------------------
225 * Tx configuration and control
226 * ----------------------------------------------------------------
230 * Set transmitter gain
232 bool set_tx_gain(double gain);
234 //! return minimum Tx gain
235 double tx_gain_min();
237 //! return maximum Tx gain
238 double tx_gain_max();
240 //! return Tx gain db_per_step
241 double tx_gain_db_per_step();
244 * \brief Set transmit daughterboard LO offset frequency
246 bool set_tx_lo_offset(double frequency);
249 * Set transmitter center frequency
251 bool set_tx_center_freq(double frequency, tune_result *result);
253 //! return minimum Tx center frequency
254 double tx_freq_min();
256 //! return maximum Tx center frequency
257 double tx_freq_max();
260 * Set transmitter sample rate interpolation
262 bool set_tx_interp(int interpolation_factor);
264 //! Return current interpolation factor
268 * \brief Calculate default scale_iq for given interpolation rate
270 void default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q);
273 * Set transmit IQ magnitude scaling
275 bool set_tx_scale_iq(int scale_i, int scale_q);
278 * Set transmit sample format
280 * domain: complex or real
281 * type: floating, fixed point, or raw
282 * depth: bits per sample
284 * Sets format over the wire for samples to USRP2.
286 // bool set_tx_format(...);
289 * \brief transmit complex<float> samples to USRP2
291 * \param channel specifies the channel to send them to
292 * \param samples are the samples to transmit. They should be in the range [-1.0, +1.0]
293 * \param nsamples is the number of samples to transmit
294 * \param metadata provides the timestamp and flags
296 * The complex<float> samples are converted to the appropriate
297 * "on the wire" representation, depending on the current USRP2
298 * configuration. Typically, this is big-endian 16-bit I & Q.
300 bool tx_32fc(unsigned int channel,
301 const std::complex<float> *samples,
303 const tx_metadata *metadata);
306 * \brief transmit complex<int16_t> samples to USRP2
308 * \param channel specifies the channel to send them to
309 * \param samples are the samples to transmit
310 * \param nsamples is the number of samples to transmit
311 * \param metadata provides the timestamp and flags
313 * The complex<int16_t> samples are converted to the appropriate
314 * "on the wire" representation, depending on the current USRP2
315 * configuration. Typically, this is big-endian 16-bit I & Q.
317 bool tx_16sc(unsigned int channel,
318 const std::complex<int16_t> *samples,
320 const tx_metadata *metadata);
323 * \brief transmit raw uint32_t data items to USRP2
325 * The caller is responsible for ensuring that the items are
326 * formatted appropriately for the USRP2 and its configuration.
327 * This method is used primarily by the system itself. Users
328 * should call tx_32fc or tx_16sc instead.
330 * \param channel specifies the channel to send them to
331 * \param items are the data items to transmit
332 * \param nitems is the number of items to transmit
333 * \param metadata provides the timestamp and flags
335 bool tx_raw(unsigned int channel,
336 const uint32_t *items,
338 const tx_metadata *metadata);
341 * ----------------------------------------------------------------
342 * miscellaneous methods
343 * ----------------------------------------------------------------
347 * \brief MIMO configuration
349 * \param flags from usrp2_mimo_config.h
358 * and optionally this:
360 * MC_PROVIDE_CLK_TO_MIMO
363 bool config_mimo(int flags);
366 //! Get frequency of master oscillator in Hz
367 bool fpga_master_clock_freq(long *freq);
369 // Get Sampling rate of A/D converter in Hz
370 bool adc_rate(long *rate);
372 // Get Sampling rate of D/A converter in Hz
373 bool dac_rate(long *rate);
376 * \brief Get Tx daughterboard ID
378 * \param[out] dbid returns the daughterboard id.
380 * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
381 * -2 if invalid EEPROM on daughterboard.
383 bool tx_daughterboard_id(int *dbid);
386 * \brief Get Rx daughterboard ID
388 * \param[out] dbid returns the daughterboard id.
390 * daughterboard id >= 0 if successful, -1 if no daugherboard installed,
391 * -2 if invalid EEPROM on daughterboard.
393 bool rx_daughterboard_id(int *dbid);
396 * ----------------------------------------------------------------
398 * ----------------------------------------------------------------
402 * Burn new mac address into EEPROM on USRP2
404 * \param new_addr Network mac address, e.g., "01:23:45:67:89:ab" or "89:ab".
405 * If \p addr is HH:HH, it's treated as if it were 00:50:c2:85:HH:HH
407 bool burn_mac_addr(const std::string &new_addr);
410 * Reset master time to 0 at next PPS edge
415 * Reset master time to 0 at every PPS edge
417 bool sync_every_pps(bool enable);
420 * Read memory from Wishbone bus as 32-bit words. Handles endian swapping if needed.
422 * \param addr 32-bit aligned address. Only the lower 16-bits are significant.
423 * \param words Number of 32-bit words
425 * \returns Vector of 32-bit read values
427 * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
428 * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
431 std::vector<uint32_t> peek32(uint32_t addr, uint32_t words);
434 * Write memory to Wishbone bus as 32-bit words. Handles endian swapping if needed.
436 * \param addr 32-bit aligned address. Only the lower 16-bits are significant
437 * \param data Vector of 32-bit values to write.
439 * \returns true iff successful
441 * WARNING: Attempts to read memory from addresses that do not correspond to RAM or
442 * memory-mapped peripherals may cause the USRP2 to hang, requiring a power cycle.
445 bool poke32(uint32_t addr, const std::vector<uint32_t> &data);
448 * Set daughterboard GPIO data direction register.
450 * \param bank GPIO_TX_BANK or GPIO_RX_BANK
451 * \param value 16-bits, 0=FPGA input, 1=FPGA output
452 * \param mask 16-bits, 0=ignore, 1=set
454 * \returns true iff successful
456 * WARNING: Improper usage of this function may result in damage to the USRP2
459 bool set_gpio_ddr(int bank, uint16_t value, uint16_t mask);
462 * Set daughterboard GPIO output selection register. For those GPIO pins that
463 * are configured as outputs in the DDR, this settings configures the source
466 * \param bank GPIO_TX_BANK or GPIO_RX_BANK
467 * \param sels Exactly 16 character MSB->LSB string. For each position:
468 * '.' = ignore this bit, i.e., leave current value
469 * 'a' = Output ATR value
470 * 's' = Output host software controlled value
471 * '0' = Output FPGA debug bus 0 value
472 * '1' = Output FPGA debug bus 1 value
474 * \returns true iff successful
476 * WARNING: Improper usage of this function may result in damage to the USRP2
479 bool set_gpio_sels(int bank, std::string sels);
482 * Set daughterboard GPIO pin values.
484 * \param bank GPIO_TX_BANK or GPIO_RX_BANK
485 * \param value 16 bits, 0=low, 1=high
486 * \param mask 16 bits, 0=ignore, 1=set
488 * \returns true iff successful
490 * WARNING: Improper usage of this function may result in damage to the USRP2
493 bool write_gpio(int bank, uint16_t value, uint16_t mask);
496 * Read daughterboard GPIO pin values
498 * \param bank GPIO_TX_BANK or GPIO_RX_BANK
499 * \param value pointer to uint16_t to hold read results
501 * \returns true iff successful
504 bool read_gpio(int bank, uint16_t *value);
507 * Set GPIO streaming mode
509 * Individually enables streaming GPIO pins through LSBs of DSP
512 * On receive, io_rx[15] replaces I[0], io_rx[14] replaces Q[0]
513 * On transmit, I[0] maps to io_tx[15], Q[0] maps to io_tx[14]
514 * (Transmit streaming is not yet implemented.)
516 * The selected GPIO pins must have been set as inputs or outputs
517 * and, for transmit, set to software control.
519 * When enabled, the replaced DSP sample LSBs become 0.
521 * \param bank GPIO_TX_BANK or GPIO_RX_BANK
522 * \param enable enable[0] controls I channel LSB
523 * enable[1] controls Q channel LSB
525 * \returns true iff successful
527 * WARNING: Improper usage of this function may result in damage to the USRP2
530 bool enable_gpio_streaming(int bank, int enable);
532 #if 0 // not yet implemented
534 * \brief Write EEPROM on motherboard or any daughterboard.
535 * \param i2c_addr I2C bus address of EEPROM
536 * \param eeprom_offset byte offset in EEPROM to begin writing
537 * \param buf the data to write
538 * \returns true iff sucessful
540 bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string &buf);
543 * \brief Read EEPROM on motherboard or any daughterboard.
544 * \param i2c_addr I2C bus address of EEPROM
545 * \param eeprom_offset byte offset in EEPROM to begin reading
546 * \param len number of bytes to read
547 * \returns the data read if successful, else a zero length string.
549 std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
552 * \brief Write to I2C peripheral
553 * \param i2c_addr I2C bus address (7-bits)
554 * \param buf the data to write
555 * \returns true iff successful
556 * Writes are limited to a maximum of of 64 bytes.
558 bool write_i2c (int i2c_addr, const std::string &buf);
561 * \brief Read from I2C peripheral
562 * \param i2c_addr I2C bus address (7-bits)
563 * \param len number of bytes to read
564 * \returns the data read if successful, else a zero length string.
565 * Reads are limited to a maximum of 64 bytes.
567 std::string read_i2c (int i2c_addr, int len);
570 * \brief Write data to SPI bus peripheral.
572 * \param optional_header 0,1 or 2 bytes to write before buf.
573 * \param enables bitmask of peripherals to write. See usrp_spi_defs.h
574 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
575 * \param buf the data to write
576 * \returns true iff successful
577 * Writes are limited to a maximum of 64 bytes.
579 * If \p format specifies that optional_header bytes are present, they are
580 * written to the peripheral immediately prior to writing \p buf.
582 bool write_spi (int optional_header, int enables, int format, const std::string &buf);
585 * \brief Read data from SPI bus peripheral.
587 * \param optional_header 0,1 or 2 bytes to write before buf.
588 * \param enables bitmask of peripheral to read. See usrp_spi_defs.h
589 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
590 * \param len number of bytes to read. Must be in [0,64].
591 * \returns the data read if sucessful, else a zero length string.
593 * Reads are limited to a maximum of 64 bytes.
595 * If \p format specifies that optional_header bytes are present, they
596 * are written to the peripheral first. Then \p len bytes are read from
597 * the peripheral and returned.
599 std::string read_spi (int optional_header, int enables, int format, int len);
603 class impl; // implementation details
606 // Static function to retrieve or create usrp2 instance
607 static sptr find_existing_or_make_new(const std::string &ifc, props *p, size_t rx_bufsize);
609 // Only class members can instantiate this class
610 usrp2(const std::string &ifc, props *p, size_t rx_bufsize);
612 // All private state is held in opaque pointer
613 std::auto_ptr<impl> d_impl;
618 std::ostream& operator<<(std::ostream &os, const usrp2::props &x);
621 #endif /* INCLUDED_USRP2_H */