3 * Copyright 2003,2004 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
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
24 * ----------------------------------------------------------------------
25 * Mid level interface to the Universal Software Radio Peripheral (Rev 1)
27 * These classes implement the basic functionality for talking to the
28 * USRP. They try to be as independent of the signal processing code
29 * in FPGA as possible. They implement access to the low level
30 * peripherals on the board, provide a common way for reading and
31 * writing registers in the FPGA, and provide the high speed interface
32 * to streaming data across the USB.
34 * It is expected that subclasses will be derived that provide
35 * access to the functionality to a particular FPGA configuration.
36 * ----------------------------------------------------------------------
39 #ifndef INCLUDED_USRP_BASIC_H
40 #define INCLUDED_USRP_BASIC_H
42 #include <usrp_slots.h>
45 struct usb_dev_handle;
50 * \brief base class for usrp operations
56 usrp_basic (const usrp_basic &rhs); // no copy constructor
57 usrp_basic &operator= (const usrp_basic &rhs); // no assignment operator
61 struct usb_dev_handle *d_udh;
62 int d_usb_data_rate; // bytes/sec
63 int d_bytes_per_poll; // how often to poll for overruns
66 static const int MAX_REGS = 128;
67 unsigned int d_fpga_shadows[MAX_REGS];
69 usrp_basic (int which_board,
70 struct usb_dev_handle *open_interface (struct usb_device *dev),
71 const std::string fpga_filename = "",
72 const std::string firmware_filename = "");
75 * \brief advise usrp_basic of usb data rate (bytes/sec)
77 * N.B., this doesn't tweak any hardware. Derived classes
78 * should call this to inform us of the data rate whenever it's
79 * first set or if it changes.
81 * \param usb_data_rate bytes/sec
83 void set_usb_data_rate (int usb_data_rate);
86 * \brief Write auxiliary digital to analog converter.
88 * \param slot Which Tx or Rx slot to write.
89 * N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
90 * SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
91 * \param which_dac [0,3] RX slots must use only 0 and 1. TX slots must use only 2 and 3.
92 * \param value [0,4095]
93 * \returns true iff successful
95 bool write_aux_dac (int slot, int which_dac, int value);
98 * \brief Read auxiliary analog to digital converter.
100 * \param slot 2-bit slot number. E.g., SLOT_TX_A
101 * \param which_adc [0,1]
102 * \param value return 12-bit value [0,4095]
103 * \returns true iff successful
105 bool read_aux_adc (int slot, int which_adc, int *value);
108 * \brief Read auxiliary analog to digital converter.
110 * \param slot 2-bit slot number. E.g., SLOT_TX_A
111 * \param which_adc [0,1]
112 * \returns value in the range [0,4095] if successful, else READ_FAILED.
114 int read_aux_adc (int slot, int which_adc);
117 virtual ~usrp_basic ();
120 * \brief return frequency of master oscillator on USRP
122 long fpga_master_clock_freq () const { return 64000000; }
125 * \returns usb data rate in bytes/sec
127 int usb_data_rate () const { return d_usb_data_rate; }
129 void set_verbose (bool on) { d_verbose = on; }
131 //! magic value used on alternate register read interfaces
132 static const int READ_FAILED = -99999;
135 * \brief Write EEPROM on motherboard or any daughterboard.
136 * \param i2c_addr I2C bus address of EEPROM
137 * \param eeprom_offset byte offset in EEPROM to begin writing
138 * \param buf the data to write
139 * \returns true iff sucessful
141 bool write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf);
144 * \brief Read EEPROM on motherboard or any daughterboard.
145 * \param i2c_addr I2C bus address of EEPROM
146 * \param eeprom_offset byte offset in EEPROM to begin reading
147 * \param len number of bytes to read
148 * \returns the data read if successful, else a zero length string.
150 std::string read_eeprom (int i2c_addr, int eeprom_offset, int len);
153 * \brief Write to I2C peripheral
154 * \param i2c_addr I2C bus address (7-bits)
155 * \param buf the data to write
156 * \returns true iff successful
157 * Writes are limited to a maximum of of 64 bytes.
159 bool write_i2c (int i2c_addr, const std::string buf);
162 * \brief Read from I2C peripheral
163 * \param i2c_addr I2C bus address (7-bits)
164 * \param len number of bytes to read
165 * \returns the data read if successful, else a zero length string.
166 * Reads are limited to a maximum of 64 bytes.
168 std::string read_i2c (int i2c_addr, int len);
171 * \brief Set ADC offset correction
172 * \param which which ADC[0,3]: 0 = RX_A I, 1 = RX_A Q...
173 * \param offset 16-bit value to subtract from raw ADC input.
175 bool set_adc_offset (int which, int offset);
178 * \brief Set DAC offset correction
179 * \param which which DAC[0,3]: 0 = TX_A I, 1 = TX_A Q...
180 * \param offset 10-bit offset value (ambiguous format: See AD9862 datasheet).
181 * \param offset_pin 1-bit value. If 0 offset applied to -ve differential pin;
182 * If 1 offset applied to +ve differential pin.
184 bool set_dac_offset (int which, int offset, int offset_pin);
187 * \brief Control ADC input buffer
188 * \param which which ADC[0,3]
189 * \param bypass if non-zero, bypass input buffer and connect input
190 * directly to switched cap SHA input of RxPGA.
192 bool set_adc_buffer_bypass (int which, bool bypass);
196 * \brief return the usrp's serial number.
198 * \returns non-zero length string iff successful.
200 std::string serial_number();
202 // ----------------------------------------------------------------
203 // Low level implementation routines.
204 // You probably shouldn't be using these...
207 bool _set_led (int which, bool on);
210 * \brief Write FPGA register.
211 * \param regno 7-bit register number
212 * \param value 32-bit value
213 * \returns true iff successful
215 bool _write_fpga_reg (int regno, int value); //< 7-bit regno, 32-bit value
218 * \brief Read FPGA register.
219 * \param regno 7-bit register number
220 * \param value 32-bit value
221 * \returns true iff successful
223 bool _read_fpga_reg (int regno, int *value); //< 7-bit regno, 32-bit value
226 * \brief Read FPGA register.
227 * \param regno 7-bit register number
228 * \returns register value if successful, else READ_FAILED
230 int _read_fpga_reg (int regno);
234 * \brief Write FPGA register with mask.
235 * \param regno 7-bit register number
236 * \param value 16-bit value
237 * \param mask 16-bit value
238 * \returns true if successful
239 * Only use this for registers who actually implement a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
241 bool _write_fpga_reg_masked (int regno, int value, int mask);
244 * \brief Write AD9862 register.
245 * \param which_codec 0 or 1
246 * \param regno 6-bit register number
247 * \param value 8-bit value
248 * \returns true iff successful
250 bool _write_9862 (int which_codec, int regno, unsigned char value);
253 * \brief Read AD9862 register.
254 * \param which_codec 0 or 1
255 * \param regno 6-bit register number
256 * \param value 8-bit value
257 * \returns true iff successful
259 bool _read_9862 (int which_codec, int regno, unsigned char *value) const;
262 * \brief Read AD9862 register.
263 * \param which_codec 0 or 1
264 * \param regno 6-bit register number
265 * \returns register value if successful, else READ_FAILED
267 int _read_9862 (int which_codec, int regno) const;
270 * \brief Write data to SPI bus peripheral.
272 * \param optional_header 0,1 or 2 bytes to write before buf.
273 * \param enables bitmask of peripherals to write. See usrp_spi_defs.h
274 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
275 * \param buf the data to write
276 * \returns true iff successful
277 * Writes are limited to a maximum of 64 bytes.
279 * If \p format specifies that optional_header bytes are present, they are
280 * written to the peripheral immediately prior to writing \p buf.
282 bool _write_spi (int optional_header, int enables, int format, std::string buf);
285 * \brief Read data from SPI bus peripheral.
287 * \param optional_header 0,1 or 2 bytes to write before buf.
288 * \param enables bitmask of peripheral to read. See usrp_spi_defs.h
289 * \param format transaction format. See usrp_spi_defs.h SPI_FMT_*
290 * \param len number of bytes to read. Must be in [0,64].
291 * \returns the data read if sucessful, else a zero length string.
293 * Reads are limited to a maximum of 64 bytes.
295 * If \p format specifies that optional_header bytes are present, they
296 * are written to the peripheral first. Then \p len bytes are read from
297 * the peripheral and returned.
299 std::string _read_spi (int optional_header, int enables, int format, int len);
302 * \brief Start data transfers.
303 * Called in base class to derived class order.
308 * \brief Stop data transfers.
309 * Called in base class to derived class order.
315 * \brief class for accessing the receive side of the USRP
317 class usrp_basic_rx : public usrp_basic
320 fusb_devhandle *d_devhandle;
321 fusb_ephandle *d_ephandle;
322 int d_bytes_seen; // how many bytes we've seen
327 int d_dbid[2]; // Rx daughterboard ID's
330 * \param which_board Which USRP board on usb (not particularly useful; use 0)
331 * \param fusb_block_size fast usb xfer block size. Must be a multiple of 512.
332 * Use zero for a reasonable default.
333 * \param fusb_nblocks number of fast usb URBs to allocate. Use zero for a reasonable default.
335 usrp_basic_rx (int which_board,
336 int fusb_block_size=0,
338 const std::string fpga_filename = "",
339 const std::string firmware_filename = ""
340 ); // throws if trouble
342 bool set_rx_enable (bool on);
343 bool rx_enable () const { return d_rx_enable; }
345 bool disable_rx (); // conditional disable, return prev state
346 void restore_rx (bool on); // conditional set
348 void probe_rx_slots (bool verbose);
349 int dboard_to_slot (int dboard) { return (dboard << 1) | 1; }
355 * \brief invokes constructor, returns instance or 0 if trouble
357 * \param which_board Which USRP board on usb (not particularly useful; use 0)
358 * \param fusb_block_size fast usb xfer block size. Must be a multiple of 512.
359 * Use zero for a reasonable default.
360 * \param fusb_nblocks number of fast usb URBs to allocate. Use zero for a reasonable default.
362 static usrp_basic_rx *make (int which_board,
363 int fusb_block_size=0,
365 const std::string fpga_filename = "",
366 const std::string firmware_filename = ""
372 * \brief tell the fpga the rate rx samples are coming from the A/D's
374 * div = fpga_master_clock_freq () / sample_rate
376 * sample_rate is determined by a myriad of registers
377 * in the 9862. That's why you have to tell us, so
378 * we can tell the fpga.
380 bool set_fpga_rx_sample_rate_divisor (unsigned int div);
383 * \brief read data from the D/A's via the FPGA.
384 * \p len must be a multiple of 512 bytes.
386 * \returns the number of bytes read, or -1 on error.
388 * If overrun is non-NULL it will be set true iff an RX overrun is detected.
390 int read (void *buf, int len, bool *overrun);
394 //! sampling rate of A/D converter
395 virtual long converter_rate() const { return fpga_master_clock_freq(); } // 64M
396 long adc_rate() const { return converter_rate(); }
397 long adc_freq() const { return converter_rate(); } //!< deprecated method name
400 * \brief Return daughterboard ID for given Rx daughterboard slot [0,1].
402 * \param which_dboard [0,1] which Rx daughterboard
404 * \return daughterboard id >= 0 if successful
405 * \return -1 if no daugherboard
406 * \return -2 if invalid EEPROM on daughterboard
408 int daughterboard_id (int which_dboard) const { return d_dbid[which_dboard & 0x1]; }
410 // ----------------------------------------------------------------
411 // routines for controlling the Programmable Gain Amplifier
413 * \brief Set Programmable Gain Amplifier (PGA)
415 * \param which which A/D [0,3]
416 * \param gain_in_db gain value (linear in dB)
418 * gain is rounded to closest setting supported by hardware.
420 * \returns true iff sucessful.
422 * \sa pga_min(), pga_max(), pga_db_per_step()
424 bool set_pga (int which, double gain_in_db);
427 * \brief Return programmable gain amplifier gain setting in dB.
429 * \param which which A/D [0,3]
431 double pga (int which) const;
434 * \brief Return minimum legal PGA gain in dB.
436 double pga_min () const { return 0.0; }
439 * \brief Return maximum legal PGA gain in dB.
441 double pga_max () const { return 20.0; }
444 * \brief Return hardware step size of PGA (linear in dB).
446 double pga_db_per_step () const { return 20.0 / 20; }
449 * \brief Write direction register (output enables) for pins that go to daughterboard.
451 * \param which_dboard [0,1] which d'board
452 * \param value value to write into register
453 * \param mask which bits of value to write into reg
455 * Each d'board has 16-bits of general purpose i/o.
456 * Setting the bit makes it an output from the FPGA to the d'board.
458 * This register is initialized based on a value stored in the
459 * d'board EEPROM. In general, you shouldn't be using this routine
460 * without a very good reason. Using this method incorrectly will
461 * kill your USRP motherboard and/or daughterboard.
463 bool _write_oe (int which_dboard, int value, int mask);
466 * \brief Write daughterboard i/o pin value
468 * \param which_dboard [0,1] which d'board
469 * \param value value to write into register
470 * \param mask which bits of value to write into reg
472 bool write_io (int which_dboard, int value, int mask);
475 * \brief Read daughterboard i/o pin value
477 * \param which_dboard [0,1] which d'board
478 * \param value output
480 bool read_io (int which_dboard, int *value);
483 * \brief Read daughterboard i/o pin value
485 * \param which_dboard [0,1] which d'board
486 * \returns register value if successful, else READ_FAILED
488 int read_io (int which_dboard);
491 * \brief Write auxiliary digital to analog converter.
493 * \param which_dboard [0,1] which d'board
494 * N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
495 * SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
496 * \param which_dac [2,3] TX slots must use only 2 and 3.
497 * \param value [0,4095]
498 * \returns true iff successful
500 bool write_aux_dac (int which_board, int which_dac, int value);
503 * \brief Read auxiliary analog to digital converter.
505 * \param which_dboard [0,1] which d'board
506 * \param which_adc [0,1]
507 * \param value return 12-bit value [0,4095]
508 * \returns true iff successful
510 bool read_aux_adc (int which_dboard, int which_adc, int *value);
513 * \brief Read auxiliary analog to digital converter.
515 * \param which_dboard [0,1] which d'board
516 * \param which_adc [0,1]
517 * \returns value in the range [0,4095] if successful, else READ_FAILED.
519 int read_aux_adc (int which_dboard, int which_adc);
522 * \brief returns current fusb block size
524 int block_size() const;
527 * \brief Enable/disable automatic DC offset removal control loop in FPGA
529 * \param bits which control loops to enable
530 * \param mask which \p bits to pay attention to
532 * If the corresponding bit is set, enable the automatic DC
533 * offset correction control loop.
536 * The 4 low bits are significant:
544 * By default the control loop is enabled on all ADC's.
546 bool set_dc_offset_cl_enable(int bits, int mask);
548 // called in base class to derived class order
554 * \brief class for accessing the transmit side of the USRP
556 class usrp_basic_tx : public usrp_basic
559 fusb_devhandle *d_devhandle;
560 fusb_ephandle *d_ephandle;
561 int d_bytes_seen; // how many bytes we've seen
566 int d_dbid[2]; // Tx daughterboard ID's
569 * \param which_board Which USRP board on usb (not particularly useful; use 0)
570 * \param fusb_block_size fast usb xfer block size. Must be a multiple of 512.
571 * Use zero for a reasonable default.
572 * \param fusb_nblocks number of fast usb URBs to allocate. Use zero for a reasonable default.
574 usrp_basic_tx (int which_board,
575 int fusb_block_size=0,
577 const std::string fpga_filename = "",
578 const std::string firmware_filename = ""
579 ); // throws if trouble
581 bool set_tx_enable (bool on);
582 bool tx_enable () const { return d_tx_enable; }
584 bool disable_tx (); // conditional disable, return prev state
585 void restore_tx (bool on); // conditional set
587 void probe_tx_slots (bool verbose);
588 int dboard_to_slot (int dboard) { return (dboard << 1) | 0; }
595 * \brief invokes constructor, returns instance or 0 if trouble
597 * \param which_board Which USRP board on usb (not particularly useful; use 0)
598 * \param fusb_block_size fast usb xfer block size. Must be a multiple of 512.
599 * Use zero for a reasonable default.
600 * \param fusb_nblocks number of fast usb URBs to allocate. Use zero for a reasonable default.
602 static usrp_basic_tx *make (int which_board, int fusb_block_size=0, int fusb_nblocks=0,
603 const std::string fpga_filename = "",
604 const std::string firmware_filename = ""
610 * \brief tell the fpga the rate tx samples are going to the D/A's
612 * div = fpga_master_clock_freq () * 2
614 * sample_rate is determined by a myriad of registers
615 * in the 9862. That's why you have to tell us, so
616 * we can tell the fpga.
618 bool set_fpga_tx_sample_rate_divisor (unsigned int div);
621 * \brief Write data to the A/D's via the FPGA.
623 * \p len must be a multiple of 512 bytes.
624 * \returns number of bytes written or -1 on error.
626 * if \p underrun is non-NULL, it will be set to true iff
627 * a transmit underrun condition is detected.
629 int write (const void *buf, int len, bool *underrun);
632 * Block until all outstanding writes have completed.
633 * This is typically used to assist with benchmarking
635 void wait_for_completion ();
639 //! sampling rate of D/A converter
640 virtual long converter_rate() const { return fpga_master_clock_freq () * 2; } // 128M
641 long dac_rate() const { return converter_rate(); }
642 long dac_freq() const { return converter_rate(); } //!< deprecated method name
645 * \brief Return daughterboard ID for given Tx daughterboard slot [0,1].
647 * \return daughterboard id >= 0 if successful
648 * \return -1 if no daugherboard
649 * \return -2 if invalid EEPROM on daughterboard
651 int daughterboard_id (int which_dboard) const { return d_dbid[which_dboard & 0x1]; }
653 // ----------------------------------------------------------------
654 // routines for controlling the Programmable Gain Amplifier
656 * \brief Set Programmable Gain Amplifier (PGA)
658 * \param which which D/A [0,3]
659 * \param gain_in_db gain value (linear in dB)
661 * gain is rounded to closest setting supported by hardware.
662 * Note that DAC 0 and DAC 1 share a gain setting as do DAC 2 and DAC 3.
663 * Setting DAC 0 affects DAC 1 and vice versa. Same with DAC 2 and DAC 3.
665 * \returns true iff sucessful.
667 * \sa pga_min(), pga_max(), pga_db_per_step()
669 bool set_pga (int which, double gain_in_db);
672 * \brief Return programmable gain amplifier gain in dB.
674 * \param which which D/A [0,3]
676 double pga (int which) const;
679 * \brief Return minimum legal PGA gain in dB.
681 double pga_min () const { return -20.0; }
684 * \brief Return maximum legal PGA gain in dB.
686 double pga_max () const { return 0.0; }
689 * \brief Return hardware step size of PGA (linear in dB).
691 double pga_db_per_step () const { return 20.0/255; }
694 * \brief Write direction register (output enables) for pins that go to daughterboard.
696 * \param which_dboard [0,1] which d'board
697 * \param value value to write into register
698 * \param mask which bits of value to write into reg
700 * Each d'board has 16-bits of general purpose i/o.
701 * Setting the bit makes it an output from the FPGA to the d'board.
703 * This register is initialized based on a value stored in the
704 * d'board EEPROM. In general, you shouldn't be using this routine
705 * without a very good reason. Using this method incorrectly will
706 * kill your USRP motherboard and/or daughterboard.
708 bool _write_oe (int which_dboard, int value, int mask);
711 * \brief Write daughterboard i/o pin value
713 * \param which_dboard [0,1] which d'board
714 * \param value value to write into register
715 * \param mask which bits of value to write into reg
717 bool write_io (int which_dboard, int value, int mask);
720 * \brief Read daughterboard i/o pin value
722 * \param which_dboard [0,1] which d'board
723 * \param value return value
725 bool read_io (int which_dboard, int *value);
728 * \brief Read daughterboard i/o pin value
730 * \param which_dboard [0,1] which d'board
731 * \returns register value if successful, else READ_FAILED
733 int read_io (int which_dboard);
736 * \brief Write auxiliary digital to analog converter.
738 * \param which_dboard [0,1] which d'board
739 * N.B., SLOT_TX_A and SLOT_RX_A share the same AUX DAC's.
740 * SLOT_TX_B and SLOT_RX_B share the same AUX DAC's.
741 * \param which_dac [2,3] TX slots must use only 2 and 3.
742 * \param value [0,4095]
743 * \returns true iff successful
745 bool write_aux_dac (int which_board, int which_dac, int value);
748 * \brief Read auxiliary analog to digital converter.
750 * \param which_dboard [0,1] which d'board
751 * \param which_adc [0,1]
752 * \param value return 12-bit value [0,4095]
753 * \returns true iff successful
755 bool read_aux_adc (int which_dboard, int which_adc, int *value);
758 * \brief Read auxiliary analog to digital converter.
760 * \param which_dboard [0,1] which d'board
761 * \param which_adc [0,1]
762 * \returns value in the range [0,4095] if successful, else READ_FAILED.
764 int read_aux_adc (int which_dboard, int which_adc);
767 * \brief returns current fusb block size
769 int block_size() const;
771 // called in base class to derived class order