3 * Copyright 2004,2008 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #ifndef INCLUDED_USRP_BASE_H
22 #define INCLUDED_USRP_BASE_H
24 #include <gr_sync_block.h>
26 #include <boost/shared_ptr.hpp>
28 #include <usrp_subdev_spec.h>
33 * \brief base class for GNU Radio interface to the USRP
35 class usrp_base : public gr_sync_block {
37 boost::shared_ptr<usrp_basic> d_usrp_basic;
40 usrp_base(const std::string &name,
41 gr_io_signature_sptr input_signature,
42 gr_io_signature_sptr output_signature)
43 : gr_sync_block(name, input_signature, output_signature) {}
46 void set_usrp_basic(boost::shared_ptr<usrp_basic> u);
52 * Return a vector of vectors of daughterboard instances associated with
53 * the USRP source or sink. The first dimension of the returned vector
54 * corresponds to the side of the USRP, the second dimension, the subdevice
55 * on the particular daughterboard.
57 * N.B. To ensure proper lifetime management, the caller should
58 * continue to hold these as weak pointers, not shared pointers.
59 * As long as the caller does not attempt to directly use the weak
60 * pointers after this usrp object has been destroyed, everything
63 std::vector<std::vector<db_base_sptr> > db();
66 * Return a vector of size 1 or 2 that contains shared pointers
67 * to the daughterboard instance(s) associated with the specified side.
69 * \param which_side [0,1] which daughterboard
71 * N.B. To ensure proper lifetime management, the caller should
72 * continue to hold these as weak pointers, not shared pointers.
73 * As long as the caller does not attempt to directly use the weak
74 * pointers after this usrp object has been destroyed, everything
77 std::vector<db_base_sptr> db(int which_side);
80 * Return the daughterboard instance corresponding to the selected
81 * side of the USRP and selected daughterboard subdevice.
82 * N.B. To ensure proper lifetime management, the caller should
83 * continue to hold these as weak pointers, not shared pointers.
84 * As long as the caller does not attempt to directly use the weak
85 * pointers after this usrp object has been destroyed, everything
88 db_base_sptr db(int which_side, int which_dev);
91 * \brief given a usrp_subdev_spec, return the corresponding daughterboard object.
92 * \throws std::invalid_argument if ss is invalid.
94 * \param ss specifies the side and subdevice
96 db_base_sptr selected_subdev(usrp_subdev_spec ss);
99 * \brief return frequency of master oscillator on USRP
101 long fpga_master_clock_freq() const;
103 void set_verbose (bool on);
105 //! magic value used on alternate register read interfaces
106 static const int READ_FAILED = -99999;
109 * \brief Write EEPROM on motherboard or any daughterboard.
110 * \param i2c_addr I2C bus address of EEPROM
111 * \param eeprom_offset byte offset in EEPROM to begin writing
112 * \param buf the data to write
113 * \returns true iff sucessful
115 bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
118 * \brief Read EEPROM on motherboard or any daughterboard.
119 * \param i2c_addr I2C bus address of EEPROM
120 * \param eeprom_offset byte offset in EEPROM to begin reading
121 * \param len number of bytes to read
122 * \returns the data read if successful, else a zero length string.
124 std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
127 * \brief Write to I2C peripheral
128 * \param i2c_addr I2C bus address (7-bits)
129 * \param buf the data to write
130 * \returns true iff successful
131 * Writes are limited to a maximum of of 64 bytes.
133 bool write_i2c (int i2c_addr, const std::string buf);
136 * \brief Read from I2C peripheral
137 * \param i2c_addr I2C bus address (7-bits)
138 * \param len number of bytes to read
139 * \returns the data read if successful, else a zero length string.
140 * Reads are limited to a maximum of 64 bytes.
142 std::string read_i2c (int i2c_addr, int len);
145 * \brief Set ADC offset correction
146 * \param which which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
147 * \param offset 16-bit value to subtract from raw ADC input.
149 bool set_adc_offset (int which_adc, int offset);
152 * \brief Set DAC offset correction
153 * \param which which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
154 * \param offset 10-bit offset value (ambiguous format: See AD9862 datasheet).
155 * \param offset_pin 1-bit value. If 0 offset applied to -ve differential pin;
156 * If 1 offset applied to +ve differential pin.
158 bool set_dac_offset (int which_dac, int offset, int offset_pin);
161 * \brief Control ADC input buffer
162 * \param which_adc which ADC[0,3]
163 * \param bypass if non-zero, bypass input buffer and connect input
164 * directly to switched cap SHA input of RxPGA.
166 bool set_adc_buffer_bypass (int which_adc, bool bypass);
169 * \brief Enable/disable automatic DC offset removal control loop in FPGA
171 * \param bits which control loops to enable
172 * \param mask which \p bits to pay attention to
174 * If the corresponding bit is set, enable the automatic DC
175 * offset correction control loop.
178 * The 4 low bits are significant:
186 * By default the control loop is enabled on all ADC's.
188 bool set_dc_offset_cl_enable(int bits, int mask);
191 * \brief return the usrp's serial number.
193 * \returns non-zero length string iff successful.
195 std::string serial_number();
198 * \brief Return daughterboard ID for given side [0,1].
200 * \param which_side [0,1] which daughterboard
202 * \return daughterboard id >= 0 if successful
203 * \return -1 if no daugherboard
204 * \return -2 if invalid EEPROM on daughterboard
206 virtual int daughterboard_id (int which_side) const;
209 * \brief Clock ticks to delay rising of T/R signal
210 * \sa write_atr_mask, write_atr_txval, write_atr_rxval
212 bool write_atr_tx_delay(int value);
215 * \brief Clock ticks to delay falling edge of T/R signal
216 * \sa write_atr_mask, write_atr_txval, write_atr_rxval
218 bool write_atr_rx_delay(int value);
221 * \brief Set Programmable Gain Amplifier (PGA)
223 * \param which_amp which amp [0,3]
224 * \param gain_in_db gain value (linear in dB)
226 * gain is rounded to closest setting supported by hardware.
228 * \returns true iff sucessful.
230 * \sa pga_min(), pga_max(), pga_db_per_step()
232 bool set_pga (int which_amp, double gain_in_db);
235 * \brief Return programmable gain amplifier gain setting in dB.
237 * \param which_amp which amp [0,3]
239 double pga (int which_amp) const;
242 * \brief Return minimum legal PGA gain in dB.
244 double pga_min () const;
247 * \brief Return maximum legal PGA gain in dB.
249 double pga_max () const;
252 * \brief Return hardware step size of PGA (linear in dB).
254 double pga_db_per_step () const;
257 * \brief Write direction register (output enables) for pins that go to daughterboard.
259 * \param which_side [0,1] which size
260 * \param value value to write into register
261 * \param mask which bits of value to write into reg
263 * Each d'board has 16-bits of general purpose i/o.
264 * Setting the bit makes it an output from the FPGA to the d'board.
266 * This register is initialized based on a value stored in the
267 * d'board EEPROM. In general, you shouldn't be using this routine
268 * without a very good reason. Using this method incorrectly will
269 * kill your USRP motherboard and/or daughterboard.
271 bool _write_oe (int which_side, int value, int mask);
274 * \brief Write daughterboard i/o pin value
276 * \param which_side [0,1] which d'board
277 * \param value value to write into register
278 * \param mask which bits of value to write into reg
280 bool write_io (int which_side, int value, int mask);
283 * \brief Read daughterboard i/o pin value
285 * \param which_side [0,1] which d'board
286 * \returns register value if successful, else READ_FAILED
288 int read_io (int which_side);
291 * \brief Write daughterboard refclk config register
293 * \param which_side [0,1] which d'board
294 * \param value value to write into register, see below
297 * Control whether a reference clock is sent to the daughterboards,
298 * and what frequency. The refclk is sent on d'board i/o pin 0.
301 * 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
302 * +-----------------------------------------------+-+------------+
303 * | Reserved (Must be zero) |E| DIVISOR |
304 * +-----------------------------------------------+-+------------+
306 * Bit 7 -- 1 turns on refclk, 0 allows IO use
307 * Bits 6:0 Divider value
310 bool write_refclk(int which_side, int value);
312 bool write_atr_mask(int which_side, int value);
313 bool write_atr_txval(int which_side, int value);
314 bool write_atr_rxval(int which_side, int value);
317 * \brief Write auxiliary digital to analog converter.
319 * \param which_side [0,1] which d'board
320 * N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
321 * SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
322 * \param which_dac [2,3] TX slots must use only 2 and 3.
323 * \param value [0,4095]
324 * \returns true iff successful
326 bool write_aux_dac (int which_side, int which_dac, int value);
329 * \brief Read auxiliary analog to digital converter.
331 * \param which_side [0,1] which d'board
332 * \param which_adc [0,1]
333 * \returns value in the range [0,4095] if successful, else READ_FAILED.
335 int read_aux_adc (int which_side, int which_adc);
338 * \brief returns A/D or D/A converter rate in Hz
340 long converter_rate() const;
343 // ----------------------------------------------------------------
344 // Low level implementation routines.
345 // You probably shouldn't be using these...
348 bool _set_led (int which_led, bool on);
351 * \brief Write FPGA register.
352 * \param regno 7-bit register number
353 * \param value 32-bit value
354 * \returns true iff successful
356 bool _write_fpga_reg (int regno, int value); //< 7-bit regno, 32-bit value
359 * \brief Read FPGA register.
360 * \param regno 7-bit register number
361 * \param value 32-bit value
362 * \returns true iff successful
364 bool _read_fpga_reg (int regno, int *value); //< 7-bit regno, 32-bit value
367 * \brief Read FPGA register.
368 * \param regno 7-bit register number
369 * \returns register value if successful, else READ_FAILED
371 int _read_fpga_reg (int regno);
374 * \brief Write FPGA register with mask.
375 * \param regno 7-bit register number
376 * \param value 16-bit value
377 * \param mask 16-bit value
378 * \returns true if successful
379 * Only use this for registers who actually implement a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
381 bool _write_fpga_reg_masked (int regno, int value, int mask);
384 * \brief Write AD9862 register.
385 * \param which_codec 0 or 1
386 * \param regno 6-bit register number
387 * \param value 8-bit value
388 * \returns true iff successful
390 bool _write_9862 (int which_codec, int regno, unsigned char value);
393 * \brief Read AD9862 register.
394 * \param which_codec 0 or 1
395 * \param regno 6-bit register number
396 * \returns register value if successful, else READ_FAILED
398 int _read_9862 (int which_codec, int regno) const;
401 * \brief Write data to SPI bus peripheral.
403 * \param optional_header 0,1 or 2 bytes to write before buf.
404 * \param enables bitmask of peripherals to write. See usrp_spi_defs.h
405 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
406 * \param buf the data to write
407 * \returns true iff successful
408 * Writes are limited to a maximum of 64 bytes.
410 * If \p format specifies that optional_header bytes are present, they are
411 * written to the peripheral immediately prior to writing \p buf.
413 bool _write_spi (int optional_header, int enables, int format, std::string buf);
416 * \brief Read data from SPI bus peripheral.
418 * \param optional_header 0,1 or 2 bytes to write before buf.
419 * \param enables bitmask of peripheral to read. See usrp_spi_defs.h
420 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
421 * \param len number of bytes to read. Must be in [0,64].
422 * \returns the data read if sucessful, else a zero length string.
424 * Reads are limited to a maximum of 64 bytes.
426 * If \p format specifies that optional_header bytes are present, they
427 * are written to the peripheral first. Then \p len bytes are read from
428 * the peripheral and returned.
430 std::string _read_spi (int optional_header, int enables, int format, int len);
433 * Return an existing daughterboard from list of candidate dbids, or the first found
434 * on side A or side B.
436 * \param candidates Vector of candidate dbids
438 * Throws std::runtime_error if not found
440 usrp_subdev_spec pick_subdev(std::vector<int> candidates=std::vector<int>(0));
443 #endif /* INCLUDED_USRP_BASE_H */