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.
27 #include "usrp_basic.h"
28 #include "usrp_prims.h"
29 #include "usrp_interfaces.h"
30 #include "fpga_regs_common.h"
38 using namespace ad9862;
40 #define NELEM(x) (sizeof (x) / sizeof (x[0]))
42 // These set the buffer size used for each end point using the fast
43 // usb interface. The kernel ends up locking down this much memory.
45 static const int FUSB_BUFFER_SIZE = fusb_sysconfig::default_buffer_size();
46 static const int FUSB_BLOCK_SIZE = fusb_sysconfig::max_block_size();
47 static const int FUSB_NBLOCKS = FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE;
50 static const double POLLING_INTERVAL = 0.1; // seconds
52 ////////////////////////////////////////////////////////////////
54 static struct usb_dev_handle *
55 open_rx_interface (struct usb_device *dev)
57 struct usb_dev_handle *udh = usrp_open_rx_interface (dev);
59 fprintf (stderr, "usrp_basic_rx: can't open rx interface\n");
65 static struct usb_dev_handle *
66 open_tx_interface (struct usb_device *dev)
68 struct usb_dev_handle *udh = usrp_open_tx_interface (dev);
70 fprintf (stderr, "usrp_basic_tx: can't open tx interface\n");
77 //////////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////////
88 // These settings give us:
89 // CLKOUT1 = CLKIN = 64 MHz
90 // CLKOUT2 = CLKIN = 64 MHz
91 // ADC is clocked at 64 MHz
92 // DAC is clocked at 128 MHz
94 static unsigned char common_regs[] = {
96 REG_DLL, (DLL_DISABLE_INTERNAL_XTAL_OSC
99 REG_CLKOUT, CLKOUT2_EQ_DLL_OVER_2,
100 REG_AUX_ADC_CLK, AUX_ADC_CLK_CLK_OVER_4
104 usrp_basic::usrp_basic (int which_board,
105 struct usb_dev_handle *
106 open_interface (struct usb_device *dev),
107 const std::string fpga_filename,
108 const std::string firmware_filename)
110 d_usb_data_rate (16000000), // SWAG, see below
111 d_bytes_per_poll ((int) (POLLING_INTERVAL * d_usb_data_rate)),
115 * SWAG: Scientific Wild Ass Guess.
117 * d_usb_data_rate is used only to determine how often to poll for over- and under-runs.
118 * We defualt it to 1/2 of our best case. Classes derived from usrp_basic (e.g.,
119 * usrp_standard_tx and usrp_standard_rx) call set_usb_data_rate() to tell us the
120 * actual rate. This doesn't change our throughput, that's determined by the signal
121 * processing code in the FPGA (which we know nothing about), and the system limits
122 * determined by libusb, fusb_*, and the underlying drivers.
124 memset (d_fpga_shadows, 0, sizeof (d_fpga_shadows));
126 usrp_one_time_init ();
128 if (!usrp_load_standard_bits (which_board, false, fpga_filename, firmware_filename))
129 throw std::runtime_error ("usrp_basic/usrp_load_standard_bits");
131 struct usb_device *dev = usrp_find_device (which_board);
133 fprintf (stderr, "usrp_basic: can't find usrp[%d]\n", which_board);
134 throw std::runtime_error ("usrp_basic/usrp_find_device");
137 if (!(usrp_usrp_p(dev) && usrp_hw_rev(dev) >= 1)){
138 fprintf (stderr, "usrp_basic: sorry, this code only works with USRP revs >= 1\n");
139 throw std::runtime_error ("usrp_basic/bad_rev");
142 if ((d_udh = open_interface (dev)) == 0)
143 throw std::runtime_error ("usrp_basic/open_interface");
145 // initialize registers that are common to rx and tx
147 if (!usrp_9862_write_many_all (d_udh, common_regs, sizeof (common_regs))){
148 fprintf (stderr, "usrp_basic: failed to init common AD9862 regs\n");
149 throw std::runtime_error ("usrp_basic/init_9862");
152 _write_fpga_reg (FR_MODE, 0); // ensure we're in normal mode
153 _write_fpga_reg (FR_DEBUG_EN, 0); // disable debug outputs
156 usrp_basic::~usrp_basic ()
175 usrp_basic::set_usb_data_rate (int usb_data_rate)
177 d_usb_data_rate = usb_data_rate;
178 d_bytes_per_poll = (int) (usb_data_rate * POLLING_INTERVAL);
182 usrp_basic::write_aux_dac (int slot, int which_dac, int value)
184 return usrp_write_aux_dac (d_udh, slot, which_dac, value);
188 usrp_basic::read_aux_adc (int slot, int which_adc, int *value)
190 return usrp_read_aux_adc (d_udh, slot, which_adc, value);
194 usrp_basic::read_aux_adc (int slot, int which_adc)
197 if (!read_aux_adc (slot, which_adc, &value))
204 usrp_basic::write_eeprom (int i2c_addr, int eeprom_offset, const std::string buf)
206 return usrp_eeprom_write (d_udh, i2c_addr, eeprom_offset, buf.data (), buf.size ());
210 usrp_basic::read_eeprom (int i2c_addr, int eeprom_offset, int len)
217 if (!usrp_eeprom_read (d_udh, i2c_addr, eeprom_offset, buf, len))
220 return std::string (buf, len);
224 usrp_basic::write_i2c (int i2c_addr, const std::string buf)
226 return usrp_i2c_write (d_udh, i2c_addr, buf.data (), buf.size ());
230 usrp_basic::read_i2c (int i2c_addr, int len)
237 if (!usrp_i2c_read (d_udh, i2c_addr, buf, len))
240 return std::string (buf, len);
244 usrp_basic::serial_number()
246 return usrp_serial_number(d_udh);
249 // ----------------------------------------------------------------
252 usrp_basic::set_adc_offset (int which, int offset)
254 if (which < 0 || which > 3)
257 return _write_fpga_reg (FR_ADC_OFFSET_0 + which, offset);
261 usrp_basic::set_dac_offset (int which, int offset, int offset_pin)
263 if (which < 0 || which > 3)
266 int which_codec = which >> 1;
267 int tx_a = (which & 0x1) == 0;
268 int lo = ((offset & 0x3) << 6) | (offset_pin & 0x1);
269 int hi = (offset >> 2);
273 ok = _write_9862 (which_codec, REG_TX_A_OFFSET_LO, lo);
274 ok &= _write_9862 (which_codec, REG_TX_A_OFFSET_HI, hi);
277 ok = _write_9862 (which_codec, REG_TX_B_OFFSET_LO, lo);
278 ok &= _write_9862 (which_codec, REG_TX_B_OFFSET_HI, hi);
284 usrp_basic::set_adc_buffer_bypass (int which, bool bypass)
286 if (which < 0 || which > 3)
289 int codec = which >> 1;
290 int reg = (which & 1) == 0 ? REG_RX_A : REG_RX_B;
292 unsigned char cur_rx;
293 unsigned char cur_pwr_dn;
295 // If the input buffer is bypassed, we need to power it down too.
297 bool ok = _read_9862 (codec, reg, &cur_rx);
298 ok &= _read_9862 (codec, REG_RX_PWR_DN, &cur_pwr_dn);
303 cur_rx |= RX_X_BYPASS_INPUT_BUFFER;
304 cur_pwr_dn |= ((which & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B;
307 cur_rx &= ~RX_X_BYPASS_INPUT_BUFFER;
308 cur_pwr_dn &= ~(((which & 1) == 0) ? RX_PWR_DN_BUF_A : RX_PWR_DN_BUF_B);
311 ok &= _write_9862 (codec, reg, cur_rx);
312 ok &= _write_9862 (codec, REG_RX_PWR_DN, cur_pwr_dn);
316 // ----------------------------------------------------------------
319 usrp_basic::_write_fpga_reg (int regno, int value)
322 fprintf (stdout, "_write_fpga_reg(%3d, 0x%08x)\n", regno, value);
326 if (regno >= 0 && regno < MAX_REGS)
327 d_fpga_shadows[regno] = value;
329 return usrp_write_fpga_reg (d_udh, regno, value);
333 usrp_basic::_write_fpga_reg_masked (int regno, int value, int mask)
335 //Only use this for registers who actually use a mask in the verilog firmware, like FR_RX_MASTER_SLAVE
336 //value is a 16 bits value and mask is a 16 bits mask
338 fprintf (stdout, "_write_fpga_reg_masked(%3d, 0x%04x,0x%04x)\n", regno, value, mask);
342 if (regno >= 0 && regno < MAX_REGS)
343 d_fpga_shadows[regno] = value;
345 return usrp_write_fpga_reg (d_udh, regno, (value & 0xffff) | ((mask & 0xffff)<<16));
350 usrp_basic::_read_fpga_reg (int regno, int *value)
352 return usrp_read_fpga_reg (d_udh, regno, value);
356 usrp_basic::_read_fpga_reg (int regno)
359 if (!_read_fpga_reg (regno, &value))
365 usrp_basic::_write_9862 (int which_codec, int regno, unsigned char value)
368 // FIXME really want to enable logging in usrp_prims:usrp_9862_write
369 fprintf(stdout, "_write_9862(codec = %d, regno = %2d, val = 0x%02x)\n", which_codec, regno, value);
373 return usrp_9862_write (d_udh, which_codec, regno, value);
378 usrp_basic::_read_9862 (int which_codec, int regno, unsigned char *value) const
380 return usrp_9862_read (d_udh, which_codec, regno, value);
384 usrp_basic::_read_9862 (int which_codec, int regno) const
387 if (!_read_9862 (which_codec, regno, &value))
393 usrp_basic::_write_spi (int optional_header, int enables, int format, std::string buf)
395 return usrp_spi_write (d_udh, optional_header, enables, format,
396 buf.data(), buf.size());
400 usrp_basic::_read_spi (int optional_header, int enables, int format, int len)
407 if (!usrp_spi_read (d_udh, optional_header, enables, format, buf, len))
410 return std::string (buf, len);
415 usrp_basic::_set_led (int which, bool on)
417 return usrp_set_led (d_udh, which, on);
420 ////////////////////////////////////////////////////////////////
424 ////////////////////////////////////////////////////////////////
426 static unsigned char rx_init_regs[] = {
428 REG_RX_A, 0, // minimum gain = 0x00 (max gain = 0x14)
429 REG_RX_B, 0, // minimum gain = 0x00 (max gain = 0x14)
430 REG_RX_MISC, (RX_MISC_HS_DUTY_CYCLE | RX_MISC_CLK_DUTY),
431 REG_RX_IF, (RX_IF_USE_CLKOUT1
433 REG_RX_DIGITAL, (RX_DIGITAL_2_CHAN)
437 usrp_basic_rx::usrp_basic_rx (int which_board, int fusb_block_size, int fusb_nblocks,
438 const std::string fpga_filename,
439 const std::string firmware_filename
441 : usrp_basic (which_board, open_rx_interface, fpga_filename, firmware_filename),
442 d_devhandle (0), d_ephandle (0),
443 d_bytes_seen (0), d_first_read (true),
446 // initialize rx specific registers
448 if (!usrp_9862_write_many_all (d_udh, rx_init_regs, sizeof (rx_init_regs))){
449 fprintf (stderr, "usrp_basic_rx: failed to init AD9862 RX regs\n");
450 throw std::runtime_error ("usrp_basic_rx/init_9862");
454 // FIXME power down 2nd codec rx path
455 usrp_9862_write (d_udh, 1, REG_RX_PWR_DN, 0x1); // power down everything
458 // Reset the rx path and leave it disabled.
459 set_rx_enable (false);
460 usrp_set_fpga_rx_reset (d_udh, true);
461 usrp_set_fpga_rx_reset (d_udh, false);
463 set_fpga_rx_sample_rate_divisor (2); // usually correct
465 set_dc_offset_cl_enable(0xf, 0xf); // enable DC offset removal control loops
467 probe_rx_slots (false);
469 // check fusb buffering parameters
471 if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
472 throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
474 if (fusb_nblocks < 0)
475 throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
477 if (fusb_block_size == 0)
478 fusb_block_size = fusb_sysconfig::default_block_size();
480 if (fusb_nblocks == 0)
481 fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
483 d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
484 d_ephandle = d_devhandle->make_ephandle (USRP_RX_ENDPOINT, true,
485 fusb_block_size, fusb_nblocks);
487 _write_fpga_reg(FR_ATR_MASK_1, 0); // zero Rx side Auto Transmit/Receive regs
488 _write_fpga_reg(FR_ATR_TXVAL_1, 0);
489 _write_fpga_reg(FR_ATR_RXVAL_1, 0);
490 _write_fpga_reg(FR_ATR_MASK_3, 0);
491 _write_fpga_reg(FR_ATR_TXVAL_3, 0);
492 _write_fpga_reg(FR_ATR_RXVAL_3, 0);
495 static unsigned char rx_fini_regs[] = {
496 REG_RX_PWR_DN, 0x1 // power down everything
499 usrp_basic_rx::~usrp_basic_rx ()
501 if (!set_rx_enable (false)){
502 fprintf (stderr, "usrp_basic_rx: set_fpga_rx_enable failed\n");
510 if (!usrp_9862_write_many_all (d_udh, rx_fini_regs, sizeof (rx_fini_regs))){
511 fprintf (stderr, "usrp_basic_rx: failed to fini AD9862 RX regs\n");
517 usrp_basic_rx::start ()
519 if (!usrp_basic::start ()) // invoke parent's method
522 // fire off reads before asserting rx_enable
524 if (!d_ephandle->start ()){
525 fprintf (stderr, "usrp_basic_rx: failed to start end point streaming");
530 if (!set_rx_enable (true)){
531 fprintf (stderr, "usrp_basic_rx: set_rx_enable failed\n");
540 usrp_basic_rx::stop ()
542 bool ok = usrp_basic::stop();
544 if (!set_rx_enable(false)){
545 fprintf (stderr, "usrp_basic_rx: set_rx_enable(false) failed\n");
550 if (!d_ephandle->stop()){
551 fprintf (stderr, "usrp_basic_rx: failed to stop end point streaming");
560 usrp_basic_rx::make (int which_board, int fusb_block_size, int fusb_nblocks,
561 const std::string fpga_filename,
562 const std::string firmware_filename)
564 usrp_basic_rx *u = 0;
567 u = new usrp_basic_rx (which_board, fusb_block_size, fusb_nblocks,
568 fpga_filename, firmware_filename);
580 usrp_basic_rx::set_fpga_rx_sample_rate_divisor (unsigned int div)
582 return _write_fpga_reg (FR_RX_SAMPLE_RATE_DIV, div - 1);
587 * \brief read data from the D/A's via the FPGA.
588 * \p len must be a multiple of 512 bytes.
590 * \returns the number of bytes read, or -1 on error.
592 * If overrun is non-NULL it will be set true iff an RX overrun is detected.
595 usrp_basic_rx::read (void *buf, int len, bool *overrun)
602 if (len < 0 || (len % 512) != 0){
603 fprintf (stderr, "usrp_basic_rx::read: invalid length = %d\n", len);
607 r = d_ephandle->read (buf, len);
612 * In many cases, the FPGA reports an rx overrun right after we
613 * enable the Rx path. If this is our first read, check for the
614 * overrun to clear the condition, then ignore the result.
616 if (0 && d_first_read){ // FIXME
617 d_first_read = false;
619 usrp_check_rx_overrun (d_udh, &bogus_overrun);
622 if (overrun != 0 && d_bytes_seen >= d_bytes_per_poll){
624 if (!usrp_check_rx_overrun (d_udh, overrun)){
625 fprintf (stderr, "usrp_basic_rx: usrp_check_rx_overrun failed\n");
634 usrp_basic_rx::set_rx_enable (bool on)
637 return usrp_set_fpga_rx_enable (d_udh, on);
640 // conditional disable, return prev state
642 usrp_basic_rx::disable_rx ()
644 bool enabled = rx_enable ();
646 set_rx_enable (false);
652 usrp_basic_rx::restore_rx (bool on)
654 if (on != rx_enable ())
659 usrp_basic_rx::set_pga (int which, double gain)
661 if (which < 0 || which > 3)
664 gain = std::max (pga_min (), gain);
665 gain = std::min (pga_max (), gain);
667 int codec = which >> 1;
668 int reg = (which & 1) == 0 ? REG_RX_A : REG_RX_B;
670 // read current value to get input buffer bypass flag.
671 unsigned char cur_rx;
672 if (!_read_9862 (codec, reg, &cur_rx))
675 int int_gain = (int) rint ((gain - pga_min ()) / pga_db_per_step());
677 cur_rx = (cur_rx & RX_X_BYPASS_INPUT_BUFFER) | (int_gain & 0x7f);
678 return _write_9862 (codec, reg, cur_rx);
682 usrp_basic_rx::pga (int which) const
684 if (which < 0 || which > 3)
687 int codec = which >> 1;
688 int reg = (which & 1) == 0 ? REG_RX_A : REG_RX_B;
690 bool ok = _read_9862 (codec, reg, &v);
694 return (pga_db_per_step() * (v & 0x1f)) + pga_min();
698 slot_id_to_oe_reg (int slot_id)
700 static int reg[4] = { FR_OE_0, FR_OE_1, FR_OE_2, FR_OE_3 };
701 assert (0 <= slot_id && slot_id < 4);
706 slot_id_to_io_reg (int slot_id)
708 static int reg[4] = { FR_IO_0, FR_IO_1, FR_IO_2, FR_IO_3 };
709 assert (0 <= slot_id && slot_id < 4);
714 usrp_basic_rx::probe_rx_slots (bool verbose)
716 struct usrp_dboard_eeprom eeprom;
717 static int slot_id_map[2] = { SLOT_RX_A, SLOT_RX_B };
718 static const char *slot_name[2] = { "RX d'board A", "RX d'board B" };
720 for (int i = 0; i < 2; i++){
721 int slot_id = slot_id_map [i];
723 usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
727 d_dbid[i] = eeprom.id;
728 msg = usrp_dbid_to_string (eeprom.id).c_str ();
729 set_adc_offset (2*i+0, eeprom.offset[0]);
730 set_adc_offset (2*i+1, eeprom.offset[1]);
731 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
732 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
738 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
739 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
742 case UDBE_INVALID_EEPROM:
744 msg = "Invalid EEPROM contents";
745 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
746 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
756 fprintf (stderr, "%s: %s\n", slot_name[i], msg);
762 usrp_basic_rx::_write_oe (int which_dboard, int value, int mask)
764 if (! (0 <= which_dboard && which_dboard <= 1))
767 return _write_fpga_reg (slot_id_to_oe_reg (dboard_to_slot (which_dboard)),
768 (mask << 16) | (value & 0xffff));
772 usrp_basic_rx::write_io (int which_dboard, int value, int mask)
774 if (! (0 <= which_dboard && which_dboard <= 1))
777 return _write_fpga_reg (slot_id_to_io_reg (dboard_to_slot (which_dboard)),
778 (mask << 16) | (value & 0xffff));
782 usrp_basic_rx::read_io (int which_dboard, int *value)
784 if (! (0 <= which_dboard && which_dboard <= 1))
788 int reg = which_dboard + 1; // FIXME, *very* magic number (fix in serial_io.v)
789 bool ok = _read_fpga_reg (reg, &t);
793 *value = (t >> 16) & 0xffff; // FIXME, more magic
798 usrp_basic_rx::read_io (int which_dboard)
801 if (!read_io (which_dboard, &value))
807 usrp_basic_rx::write_aux_dac (int which_dboard, int which_dac, int value)
809 return usrp_basic::write_aux_dac (dboard_to_slot (which_dboard),
814 usrp_basic_rx::read_aux_adc (int which_dboard, int which_adc, int *value)
816 return usrp_basic::read_aux_adc (dboard_to_slot (which_dboard),
821 usrp_basic_rx::read_aux_adc (int which_dboard, int which_adc)
823 return usrp_basic::read_aux_adc (dboard_to_slot (which_dboard), which_adc);
827 usrp_basic_rx::block_size () const { return d_ephandle->block_size(); }
830 usrp_basic_rx::set_dc_offset_cl_enable(int bits, int mask)
832 return _write_fpga_reg(FR_DC_OFFSET_CL_EN,
833 (d_fpga_shadows[FR_DC_OFFSET_CL_EN] & ~mask) | (bits & mask));
836 ////////////////////////////////////////////////////////////////
840 ////////////////////////////////////////////////////////////////
844 // DAC input rate 64 MHz interleaved for a total input rate of 128 MHz
845 // DAC input is latched on rising edge of CLKOUT2
848 // coarse modulator disabled
851 static unsigned char tx_init_regs[] = {
853 REG_TX_A_OFFSET_LO, 0,
854 REG_TX_A_OFFSET_HI, 0,
855 REG_TX_B_OFFSET_LO, 0,
856 REG_TX_B_OFFSET_HI, 0,
857 REG_TX_A_GAIN, (TX_X_GAIN_COARSE_FULL | 0),
858 REG_TX_B_GAIN, (TX_X_GAIN_COARSE_FULL | 0),
859 REG_TX_PGA, 0xff, // maximum gain (0 dB)
861 REG_TX_IF, (TX_IF_USE_CLKOUT1
865 | TX_IF_INTERLEAVED),
866 REG_TX_DIGITAL, (TX_DIGITAL_2_DATA_PATHS
867 | TX_DIGITAL_INTERPOLATE_4X),
868 REG_TX_MODULATOR, (TX_MODULATOR_DISABLE_NCO
869 | TX_MODULATOR_COARSE_MODULATION_NONE),
870 REG_TX_NCO_FTW_7_0, 0,
871 REG_TX_NCO_FTW_15_8, 0,
872 REG_TX_NCO_FTW_23_16, 0
875 usrp_basic_tx::usrp_basic_tx (int which_board, int fusb_block_size, int fusb_nblocks,
876 const std::string fpga_filename,
877 const std::string firmware_filename)
878 : usrp_basic (which_board, open_tx_interface, fpga_filename, firmware_filename),
879 d_devhandle (0), d_ephandle (0),
880 d_bytes_seen (0), d_first_write (true),
883 if (!usrp_9862_write_many_all (d_udh, tx_init_regs, sizeof (tx_init_regs))){
884 fprintf (stderr, "usrp_basic_tx: failed to init AD9862 TX regs\n");
885 throw std::runtime_error ("usrp_basic_tx/init_9862");
889 // FIXME power down 2nd codec tx path
890 usrp_9862_write (d_udh, 1, REG_TX_PWR_DN,
891 (TX_PWR_DN_TX_DIGITAL
892 | TX_PWR_DN_TX_ANALOG_BOTH));
895 // Reset the tx path and leave it disabled.
896 set_tx_enable (false);
897 usrp_set_fpga_tx_reset (d_udh, true);
898 usrp_set_fpga_tx_reset (d_udh, false);
900 set_fpga_tx_sample_rate_divisor (4); // we're using interp x4
902 probe_tx_slots (false);
904 // check fusb buffering parameters
906 if (fusb_block_size < 0 || fusb_block_size > FUSB_BLOCK_SIZE)
907 throw std::out_of_range ("usrp_basic_rx: invalid fusb_block_size");
909 if (fusb_nblocks < 0)
910 throw std::out_of_range ("usrp_basic_rx: invalid fusb_nblocks");
912 if (fusb_block_size == 0)
913 fusb_block_size = FUSB_BLOCK_SIZE;
915 if (fusb_nblocks == 0)
916 fusb_nblocks = std::max (1, FUSB_BUFFER_SIZE / fusb_block_size);
918 d_devhandle = fusb_sysconfig::make_devhandle (d_udh);
919 d_ephandle = d_devhandle->make_ephandle (USRP_TX_ENDPOINT, false,
920 fusb_block_size, fusb_nblocks);
922 _write_fpga_reg(FR_ATR_MASK_0, 0); // zero Tx side Auto Transmit/Receive regs
923 _write_fpga_reg(FR_ATR_TXVAL_0, 0);
924 _write_fpga_reg(FR_ATR_RXVAL_0, 0);
925 _write_fpga_reg(FR_ATR_MASK_2, 0);
926 _write_fpga_reg(FR_ATR_TXVAL_2, 0);
927 _write_fpga_reg(FR_ATR_RXVAL_2, 0);
931 static unsigned char tx_fini_regs[] = {
932 REG_TX_PWR_DN, (TX_PWR_DN_TX_DIGITAL
933 | TX_PWR_DN_TX_ANALOG_BOTH),
934 REG_TX_MODULATOR, (TX_MODULATOR_DISABLE_NCO
935 | TX_MODULATOR_COARSE_MODULATION_NONE)
938 usrp_basic_tx::~usrp_basic_tx ()
944 if (!usrp_9862_write_many_all (d_udh, tx_fini_regs, sizeof (tx_fini_regs))){
945 fprintf (stderr, "usrp_basic_tx: failed to fini AD9862 TX regs\n");
950 usrp_basic_tx::start ()
952 if (!usrp_basic::start ())
955 if (!set_tx_enable (true)){
956 fprintf (stderr, "usrp_basic_tx: set_tx_enable failed\n");
961 if (!d_ephandle->start ()){
962 fprintf (stderr, "usrp_basic_tx: failed to start end point streaming");
971 usrp_basic_tx::stop ()
973 bool ok = usrp_basic::stop ();
975 if (!d_ephandle->stop ()){
976 fprintf (stderr, "usrp_basic_tx: failed to stop end point streaming");
981 if (!set_tx_enable (false)){
982 fprintf (stderr, "usrp_basic_tx: set_tx_enable(false) failed\n");
991 usrp_basic_tx::make (int which_board, int fusb_block_size, int fusb_nblocks,
992 const std::string fpga_filename,
993 const std::string firmware_filename)
995 usrp_basic_tx *u = 0;
998 u = new usrp_basic_tx (which_board, fusb_block_size, fusb_nblocks,
999 fpga_filename, firmware_filename);
1011 usrp_basic_tx::set_fpga_tx_sample_rate_divisor (unsigned int div)
1013 return _write_fpga_reg (FR_TX_SAMPLE_RATE_DIV, div - 1);
1017 * \brief Write data to the A/D's via the FPGA.
1019 * \p len must be a multiple of 512 bytes.
1020 * \returns number of bytes written or -1 on error.
1022 * if \p underrun is non-NULL, it will be set to true iff
1023 * a transmit underrun condition is detected.
1026 usrp_basic_tx::write (const void *buf, int len, bool *underrun)
1033 if (len < 0 || (len % 512) != 0){
1034 fprintf (stderr, "usrp_basic_tx::write: invalid length = %d\n", len);
1038 r = d_ephandle->write (buf, len);
1043 * In many cases, the FPGA reports an tx underrun right after we
1044 * enable the Tx path. If this is our first write, check for the
1045 * underrun to clear the condition, then ignore the result.
1047 if (d_first_write && d_bytes_seen >= 4 * FUSB_BLOCK_SIZE){
1048 d_first_write = false;
1049 bool bogus_underrun;
1050 usrp_check_tx_underrun (d_udh, &bogus_underrun);
1053 if (underrun != 0 && d_bytes_seen >= d_bytes_per_poll){
1055 if (!usrp_check_tx_underrun (d_udh, underrun)){
1056 fprintf (stderr, "usrp_basic_tx: usrp_check_tx_underrun failed\n");
1065 usrp_basic_tx::wait_for_completion ()
1067 d_ephandle->wait_for_completion ();
1071 usrp_basic_tx::set_tx_enable (bool on)
1074 // fprintf (stderr, "set_tx_enable %d\n", on);
1075 return usrp_set_fpga_tx_enable (d_udh, on);
1078 // conditional disable, return prev state
1080 usrp_basic_tx::disable_tx ()
1082 bool enabled = tx_enable ();
1084 set_tx_enable (false);
1090 usrp_basic_tx::restore_tx (bool on)
1092 if (on != tx_enable ())
1097 usrp_basic_tx::set_pga (int which, double gain)
1099 if (which < 0 || which > 3)
1102 gain = std::max (pga_min (), gain);
1103 gain = std::min (pga_max (), gain);
1105 int codec = which >> 1; // 0 and 1 are same, as are 2 and 3
1107 int int_gain = (int) rint ((gain - pga_min ()) / pga_db_per_step());
1109 return _write_9862 (codec, REG_TX_PGA, int_gain);
1113 usrp_basic_tx::pga (int which) const
1115 if (which < 0 || which > 3)
1118 int codec = which >> 1;
1120 bool ok = _read_9862 (codec, REG_TX_PGA, &v);
1124 return (pga_db_per_step() * v) + pga_min();
1128 usrp_basic_tx::probe_tx_slots (bool verbose)
1130 struct usrp_dboard_eeprom eeprom;
1131 static int slot_id_map[2] = { SLOT_TX_A, SLOT_TX_B };
1132 static const char *slot_name[2] = { "TX d'board A", "TX d'board B" };
1134 for (int i = 0; i < 2; i++){
1135 int slot_id = slot_id_map [i];
1136 const char *msg = 0;
1137 usrp_dbeeprom_status_t s = usrp_read_dboard_eeprom (d_udh, slot_id, &eeprom);
1141 d_dbid[i] = eeprom.id;
1142 msg = usrp_dbid_to_string (eeprom.id).c_str ();
1143 // FIXME, figure out interpretation of dc offset for TX d'boards
1144 // offset = (eeprom.offset[1] << 16) | (eeprom.offset[0] & 0xffff);
1145 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | eeprom.oe);
1146 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1149 case UDBE_NO_EEPROM:
1152 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1153 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1156 case UDBE_INVALID_EEPROM:
1158 msg = "Invalid EEPROM contents";
1159 _write_fpga_reg (slot_id_to_oe_reg(slot_id), (0xffff << 16) | 0x0000);
1160 _write_fpga_reg (slot_id_to_io_reg(slot_id), (0xffff << 16) | 0x0000);
1170 fprintf (stderr, "%s: %s\n", slot_name[i], msg);
1176 usrp_basic_tx::_write_oe (int which_dboard, int value, int mask)
1178 if (! (0 <= which_dboard && which_dboard <= 1))
1181 return _write_fpga_reg (slot_id_to_oe_reg (dboard_to_slot (which_dboard)),
1182 (mask << 16) | (value & 0xffff));
1186 usrp_basic_tx::write_io (int which_dboard, int value, int mask)
1188 if (! (0 <= which_dboard && which_dboard <= 1))
1191 return _write_fpga_reg (slot_id_to_io_reg (dboard_to_slot (which_dboard)),
1192 (mask << 16) | (value & 0xffff));
1196 usrp_basic_tx::read_io (int which_dboard, int *value)
1198 if (! (0 <= which_dboard && which_dboard <= 1))
1202 int reg = which_dboard + 1; // FIXME, *very* magic number (fix in serial_io.v)
1203 bool ok = _read_fpga_reg (reg, &t);
1207 *value = t & 0xffff; // FIXME, more magic
1212 usrp_basic_tx::read_io (int which_dboard)
1215 if (!read_io (which_dboard, &value))
1221 usrp_basic_tx::write_aux_dac (int which_dboard, int which_dac, int value)
1223 return usrp_basic::write_aux_dac (dboard_to_slot (which_dboard),
1228 usrp_basic_tx::read_aux_adc (int which_dboard, int which_adc, int *value)
1230 return usrp_basic::read_aux_adc (dboard_to_slot (which_dboard),
1235 usrp_basic_tx::read_aux_adc (int which_dboard, int which_adc)
1237 return usrp_basic::read_aux_adc (dboard_to_slot (which_dboard), which_adc);
1241 usrp_basic_tx::block_size () const { return d_ephandle->block_size(); }