3 * Copyright 2003,2004,2006 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_prims.h"
28 #include "usrp_commands.h"
30 #include "usrp_i2c_addr.h"
31 #include "fpga_regs_common.h"
32 #include "fpga_regs_standard.h"
40 #include <time.h> // FIXME should check with autoconf (nanosleep)
51 using namespace ad9862;
53 static const int FIRMWARE_HASH_SLOT = 0;
54 static const int FPGA_HASH_SLOT = 1;
56 static const int hash_slot_addr[2] = {
57 USRP_HASH_SLOT_0_ADDR,
61 static const char *default_firmware_filename = "std.ihx";
62 static const char *default_fpga_filename = "std_2rxhb_2tx.rbf";
64 #include "std_paths.h"
68 find_file (const char *filename, int hw_rev)
70 const char **sp = std_paths;
71 static char path[1000];
74 s = getenv("USRP_PATH");
76 snprintf (path, sizeof (path), "%s/rev%d/%s", s, hw_rev, filename);
77 if (access (path, R_OK) == 0)
82 snprintf (path, sizeof (path), "%s/rev%d/%s", *sp, hw_rev, filename);
83 if (access (path, R_OK) == 0)
91 get_proto_filename(const std::string user_filename, const char *env_var, const char *def)
93 if (user_filename.length() != 0)
94 return user_filename.c_str();
96 char *s = getenv(env_var);
104 static void power_down_9862s (struct usb_dev_handle *udh);
107 usrp_one_time_init ()
109 static bool first = true;
113 usb_init (); // usb library init
127 // ----------------------------------------------------------------
128 // Danger, big, fragile KLUDGE. The problem is that we want to be
129 // able to get from a usb_dev_handle back to a usb_device, and the
130 // right way to do this is buried in a non-installed include file.
132 static struct usb_device *
133 dev_handle_to_dev (usb_dev_handle *udh)
135 struct usb_dev_handle_kludge {
138 struct usb_device *device;
141 return ((struct usb_dev_handle_kludge *) udh)->device;
144 // ----------------------------------------------------------------
147 * q must be a real USRP, not an FX2. Return its hardware rev number.
150 usrp_hw_rev (struct usb_device *q)
152 return q->descriptor.bcdDevice & 0x00FF;
156 * q must be a real USRP, not an FX2. Return true if it's configured.
159 _usrp_configured_p (struct usb_device *q)
161 return (q->descriptor.bcdDevice & 0xFF00) != 0;
165 usrp_usrp_p (struct usb_device *q)
167 return (q->descriptor.idVendor == USB_VID_FSF
168 && q->descriptor.idProduct == USB_PID_FSF_USRP);
172 usrp_fx2_p (struct usb_device *q)
174 return (q->descriptor.idVendor == USB_VID_CYPRESS
175 && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);
179 usrp_usrp0_p (struct usb_device *q)
181 return usrp_usrp_p (q) && usrp_hw_rev (q) == 0;
185 usrp_usrp1_p (struct usb_device *q)
187 return usrp_usrp_p (q) && usrp_hw_rev (q) == 1;
191 usrp_usrp2_p (struct usb_device *q)
193 return usrp_usrp_p (q) && usrp_hw_rev (q) == 2;
198 usrp_unconfigured_usrp_p (struct usb_device *q)
200 return usrp_usrp_p (q) && !_usrp_configured_p (q);
204 usrp_configured_usrp_p (struct usb_device *q)
206 return usrp_usrp_p (q) && _usrp_configured_p (q);
209 // ----------------------------------------------------------------
212 usrp_find_device (int nth, bool fx2_ok_p)
215 struct usb_device *q;
218 usrp_one_time_init ();
220 p = usb_get_busses();
224 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
225 if (n_found == nth) // return this one
227 n_found++; // keep looking
233 return 0; // not found
236 static struct usb_dev_handle *
237 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
239 struct usb_dev_handle *udh = usb_open (dev);
243 if (dev != dev_handle_to_dev (udh)){
244 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
248 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
249 // There's no get get_configuration function, and with some of the newer kernels
250 // setting the configuration, even if to the same value, hoses any other processes
251 // that have it open. Hence we opt to not set it at all (We've only
252 // got a single configuration anyway). This may hose the win32 stuff...
254 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
255 if (usb_set_configuration (udh, 1) < 0){
259 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
260 * it returns -EBUSY if _any_ of the interfaces of a device are open.
261 * We've only got a single configuration, so setting it doesn't even seem
262 * like it should be required.
267 if (usb_claim_interface (udh, interface) < 0){
268 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
269 fprintf (stderr, "%s\n", usb_strerror());
274 if (usb_set_altinterface (udh, altinterface) < 0){
275 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
276 fprintf (stderr, "%s\n", usb_strerror());
277 usb_release_interface (udh, interface);
285 struct usb_dev_handle *
286 usrp_open_cmd_interface (struct usb_device *dev)
288 return usrp_open_interface (dev, USRP_CMD_INTERFACE, USRP_CMD_ALTINTERFACE);
291 struct usb_dev_handle *
292 usrp_open_rx_interface (struct usb_device *dev)
294 return usrp_open_interface (dev, USRP_RX_INTERFACE, USRP_RX_ALTINTERFACE);
297 struct usb_dev_handle *
298 usrp_open_tx_interface (struct usb_device *dev)
300 return usrp_open_interface (dev, USRP_TX_INTERFACE, USRP_TX_ALTINTERFACE);
304 usrp_close_interface (struct usb_dev_handle *udh)
306 // we're assuming that closing an interface automatically releases it.
307 return usb_close (udh) == 0;
310 // ----------------------------------------------------------------
311 // write internal ram using Cypress vendor extension
314 write_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,
315 int start_addr, size_t len)
320 int quanta = MAX_EP0_PKTSIZE;
322 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
323 n = len + start_addr - addr;
327 a = usb_control_msg (udh, 0x40, 0xA0,
328 addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);
331 fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());
338 // ----------------------------------------------------------------
339 // whack the CPUCS register using the upload RAM vendor extension
342 reset_cpu (struct usb_dev_handle *udh, bool reset_p)
347 v = 1; // hold processor in reset
349 v = 0; // release reset
351 return write_internal_ram (udh, &v, 0xE600, 1);
354 // ----------------------------------------------------------------
355 // Load intel format file into cypress FX2 (8051)
358 _usrp_load_firmware (struct usb_dev_handle *udh, const char *filename,
359 unsigned char hash[USRP_HASH_SIZE])
361 FILE *f = fopen (filename, "ra");
367 if (!reset_cpu (udh, true)) // hold CPU in reset while loading firmware
375 unsigned char data[256];
376 unsigned char checksum, a;
381 fgets(s, sizeof (s), f); /* we should not use more than 263 bytes normally */
383 fprintf(stderr,"%s: invalid line: \"%s\"\n", filename, s);
386 sscanf(s+1, "%02x", &length);
387 sscanf(s+3, "%04x", &addr);
388 sscanf(s+7, "%02x", &type);
392 a=length+(addr &0xff)+(addr>>8)+type;
393 for(i=0;i<length;i++){
394 sscanf (s+9+i*2,"%02x", &b);
399 sscanf (s+9+length*2,"%02x", &b);
401 if (((a+checksum)&0xff)!=0x00){
402 fprintf (stderr, " ** Checksum failed: got 0x%02x versus 0x%02x\n", (-a)&0xff, checksum);
405 if (!write_internal_ram (udh, data, addr, length))
408 else if (type == 0x01){ // EOF
411 else if (type == 0x02){
412 fprintf(stderr, "Extended address: whatever I do with it?\n");
413 fprintf (stderr, "%s: invalid line: \"%s\"\n", filename, s);
418 // we jam the hash value into the FX2 memory before letting
419 // the cpu out of reset. When it comes out of reset it
420 // may renumerate which will invalidate udh.
422 if (!usrp_set_hash (udh, FIRMWARE_HASH_SLOT, hash))
423 fprintf (stderr, "usrp: failed to write firmware hash slot\n");
425 if (!reset_cpu (udh, false)) // take CPU out of reset
436 // ----------------------------------------------------------------
437 // write vendor extension command to USRP
440 write_cmd (struct usb_dev_handle *udh,
441 int request, int value, int index,
442 unsigned char *bytes, int len)
444 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
446 int r = usb_control_msg (udh, requesttype, request, value, index,
447 (char *) bytes, len, 1000);
449 // we get EPIPE if the firmware stalls the endpoint.
451 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
457 // ----------------------------------------------------------------
461 _usrp_load_fpga (struct usb_dev_handle *udh, const char *filename,
462 unsigned char hash[USRP_HASH_SIZE])
466 FILE *fp = fopen (filename, "rb");
472 unsigned char buf[MAX_EP0_PKTSIZE]; // 64 is max size of EP0 packet on FX2
475 usrp_set_led (udh, 1, 1); // led 1 on
478 // reset FPGA (and on rev1 both AD9862's, thus killing clock)
479 usrp_set_fpga_reset (udh, 1); // hold fpga in reset
481 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_BEGIN, 0, 0) != 0)
484 while ((n = fread (buf, 1, sizeof (buf), fp)) > 0){
485 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_XFER, buf, n) != n)
489 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_END, 0, 0) != 0)
494 if (!usrp_set_hash (udh, FPGA_HASH_SLOT, hash))
495 fprintf (stderr, "usrp: failed to write fpga hash slot\n");
497 // On the rev1 USRP, the {tx,rx}_{enable,reset} bits are
498 // controlled over the serial bus, and hence aren't observed until
499 // we've got a good fpga bitstream loaded.
501 usrp_set_fpga_reset (udh, 0); // fpga out of master reset
503 // now these commands will work
505 ok &= usrp_set_fpga_tx_enable (udh, 0);
506 ok &= usrp_set_fpga_rx_enable (udh, 0);
508 ok &= usrp_set_fpga_tx_reset (udh, 1); // reset tx and rx paths
509 ok &= usrp_set_fpga_rx_reset (udh, 1);
510 ok &= usrp_set_fpga_tx_reset (udh, 0); // reset tx and rx paths
511 ok &= usrp_set_fpga_rx_reset (udh, 0);
514 fprintf (stderr, "usrp: failed to reset tx and/or rx path\n");
516 // Manually reset all regs except master control to zero.
517 // FIXME may want to remove this when we rework FPGA reset strategy.
518 // In the mean while, this gets us reproducible behavior.
519 for (int i = 0; i < FR_USER_0; i++){
520 if (i == FR_MASTER_CTRL)
522 usrp_write_fpga_reg(udh, i, 0);
525 power_down_9862s (udh); // on the rev1, power these down!
526 usrp_set_led (udh, 1, 0); // led 1 off
531 power_down_9862s (udh); // on the rev1, power these down!
536 // ----------------------------------------------------------------
539 usrp_set_led (struct usb_dev_handle *udh, int which, bool on)
541 int r = write_cmd (udh, VRQ_SET_LED, on, which, 0, 0);
547 usrp_set_hash (struct usb_dev_handle *udh, int which,
548 const unsigned char hash[USRP_HASH_SIZE])
552 // we use the Cypress firmware down load command to jam it in.
553 int r = usb_control_msg (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
554 (char *) hash, USRP_HASH_SIZE, 1000);
555 return r == USRP_HASH_SIZE;
559 usrp_get_hash (struct usb_dev_handle *udh, int which,
560 unsigned char hash[USRP_HASH_SIZE])
564 // we use the Cypress firmware upload command to fetch it.
565 int r = usb_control_msg (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
566 (char *) hash, USRP_HASH_SIZE, 1000);
567 return r == USRP_HASH_SIZE;
571 usrp_set_switch (struct usb_dev_handle *udh, int cmd_byte, bool on)
573 return write_cmd (udh, cmd_byte, on, 0, 0, 0) == 0;
578 usrp1_fpga_write (struct usb_dev_handle *udh,
579 int regno, int value)
581 // on the rev1 usrp, we use the generic spi_write interface
583 unsigned char buf[4];
585 buf[0] = (value >> 24) & 0xff; // MSB first
586 buf[1] = (value >> 16) & 0xff;
587 buf[2] = (value >> 8) & 0xff;
588 buf[3] = (value >> 0) & 0xff;
590 return usrp_spi_write (udh, 0x00 | (regno & 0x7f),
592 SPI_FMT_MSB | SPI_FMT_HDR_1,
597 usrp1_fpga_read (struct usb_dev_handle *udh,
598 int regno, int *value)
601 unsigned char buf[4];
603 bool ok = usrp_spi_read (udh, 0x80 | (regno & 0x7f),
605 SPI_FMT_MSB | SPI_FMT_HDR_1,
609 *value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
616 usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value)
618 switch (usrp_hw_rev (dev_handle_to_dev (udh))){
619 case 0: // not supported ;)
623 return usrp1_fpga_write (udh, reg, value);
628 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
630 switch (usrp_hw_rev (dev_handle_to_dev (udh))){
631 case 0: // not supported ;)
635 return usrp1_fpga_read (udh, reg, value);
640 usrp_set_fpga_reset (struct usb_dev_handle *udh, bool on)
642 return usrp_set_switch (udh, VRQ_FPGA_SET_RESET, on);
646 usrp_set_fpga_tx_enable (struct usb_dev_handle *udh, bool on)
648 return usrp_set_switch (udh, VRQ_FPGA_SET_TX_ENABLE, on);
652 usrp_set_fpga_rx_enable (struct usb_dev_handle *udh, bool on)
654 return usrp_set_switch (udh, VRQ_FPGA_SET_RX_ENABLE, on);
658 usrp_set_fpga_tx_reset (struct usb_dev_handle *udh, bool on)
660 return usrp_set_switch (udh, VRQ_FPGA_SET_TX_RESET, on);
664 usrp_set_fpga_rx_reset (struct usb_dev_handle *udh, bool on)
666 return usrp_set_switch (udh, VRQ_FPGA_SET_RX_RESET, on);
670 // ----------------------------------------------------------------
671 // conditional load stuff
674 compute_hash (const char *filename, unsigned char hash[USRP_HASH_SIZE])
676 assert (USRP_HASH_SIZE == 16);
677 memset (hash, 0, USRP_HASH_SIZE);
679 FILE *fp = fopen (filename, "rb");
684 int r = md5_stream (fp, hash);
690 static usrp_load_status_t
691 usrp_conditionally_load_something (struct usb_dev_handle *udh,
692 const char *filename,
695 bool loader (struct usb_dev_handle *,
697 unsigned char [USRP_HASH_SIZE]))
699 unsigned char file_hash[USRP_HASH_SIZE];
700 unsigned char usrp_hash[USRP_HASH_SIZE];
702 if (access (filename, R_OK) != 0){
707 if (!compute_hash (filename, file_hash))
711 && usrp_get_hash (udh, slot, usrp_hash)
712 && memcmp (file_hash, usrp_hash, USRP_HASH_SIZE) == 0)
713 return ULS_ALREADY_LOADED;
715 bool r = loader (udh, filename, file_hash);
724 usrp_load_firmware (struct usb_dev_handle *udh,
725 const char *filename,
728 return usrp_conditionally_load_something (udh, filename, force,
730 _usrp_load_firmware);
734 usrp_load_fpga (struct usb_dev_handle *udh,
735 const char *filename,
738 return usrp_conditionally_load_something (udh, filename, force,
743 static usb_dev_handle *
744 open_nth_cmd_interface (int nth)
746 struct usb_device *udev = usrp_find_device (nth);
748 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
752 struct usb_dev_handle *udh;
754 udh = usrp_open_cmd_interface (udev);
756 // FIXME this could be because somebody else has it open.
757 // We should delay and retry...
758 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
767 our_nanosleep (const struct timespec *delay)
769 struct timespec new_delay = *delay;
770 struct timespec remainder;
773 int r = nanosleep (&new_delay, &remainder);
777 new_delay = remainder;
779 perror ("nanosleep");
786 mdelay (int millisecs)
789 ts.tv_sec = millisecs / 1000;
790 ts.tv_nsec = (millisecs - (1000 * ts.tv_sec)) * 1000000;
791 return our_nanosleep (&ts);
795 usrp_load_firmware_nth (int nth, const char *filename, bool force){
796 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
800 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
801 usrp_close_interface (udh);
805 case ULS_ALREADY_LOADED: // nothing changed...
806 return ULS_ALREADY_LOADED;
810 // we loaded firmware successfully.
812 // It's highly likely that the board will renumerate (simulate a
813 // disconnect/reconnect sequence), invalidating our current
816 // FIXME. Turn this into a loop that rescans until we refind ourselves
818 struct timespec t; // delay for 1 second
823 usb_find_busses (); // rescan busses and devices
829 case ULS_ERROR: // some kind of problem
835 load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
837 char *e = getenv("USRP_VERBOSE");
838 bool verbose = e != 0;
842 fprintf (stderr, "usrp: failed to load %s %s.\n", type, filename);
845 case ULS_ALREADY_LOADED:
847 fprintf (stderr, "usrp: %s %s already loaded.\n", type, filename);
852 fprintf (stderr, "usrp: %s %s loaded successfully.\n", type, filename);
858 usrp_load_standard_bits (int nth, bool force,
859 const std::string fpga_filename,
860 const std::string firmware_filename)
862 usrp_load_status_t s;
863 const char *filename;
864 const char *proto_filename;
867 // first, figure out what hardware rev we're dealing with
869 struct usb_device *udev = usrp_find_device (nth);
871 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
874 hw_rev = usrp_hw_rev (udev);
877 // start by loading the firmware
879 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
880 default_firmware_filename);
881 filename = find_file(proto_filename, hw_rev);
883 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
887 s = usrp_load_firmware_nth (nth, filename, force);
888 load_status_msg (s, "firmware", filename);
893 // if we actually loaded firmware, we must reload fpga ...
897 // now move on to the fpga configuration bitstream
899 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
900 default_fpga_filename);
901 filename = find_file (proto_filename, hw_rev);
903 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
907 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
911 s = usrp_load_fpga (udh, filename, force);
912 usrp_close_interface (udh);
913 load_status_msg (s, "fpga bitstream", filename);
922 _usrp_get_status (struct usb_dev_handle *udh, int which, bool *trouble)
924 unsigned char status;
927 if (write_cmd (udh, VRQ_GET_STATUS, 0, which,
928 &status, sizeof (status)) != sizeof (status))
936 usrp_check_rx_overrun (struct usb_dev_handle *udh, bool *overrun_p)
938 return _usrp_get_status (udh, GS_RX_OVERRUN, overrun_p);
942 usrp_check_tx_underrun (struct usb_dev_handle *udh, bool *underrun_p)
944 return _usrp_get_status (udh, GS_TX_UNDERRUN, underrun_p);
949 usrp_i2c_write (struct usb_dev_handle *udh, int i2c_addr,
950 const void *buf, int len)
952 if (len < 1 || len > MAX_EP0_PKTSIZE)
955 return write_cmd (udh, VRQ_I2C_WRITE, i2c_addr, 0,
956 (unsigned char *) buf, len) == len;
961 usrp_i2c_read (struct usb_dev_handle *udh, int i2c_addr,
964 if (len < 1 || len > MAX_EP0_PKTSIZE)
967 return write_cmd (udh, VRQ_I2C_READ, i2c_addr, 0,
968 (unsigned char *) buf, len) == len;
972 usrp_spi_write (struct usb_dev_handle *udh,
973 int optional_header, int enables, int format,
974 const void *buf, int len)
976 if (len < 0 || len > MAX_EP0_PKTSIZE)
979 return write_cmd (udh, VRQ_SPI_WRITE,
981 ((enables & 0xff) << 8) | (format & 0xff),
982 (unsigned char *) buf, len) == len;
987 usrp_spi_read (struct usb_dev_handle *udh,
988 int optional_header, int enables, int format,
991 if (len < 0 || len > MAX_EP0_PKTSIZE)
994 return write_cmd (udh, VRQ_SPI_READ,
996 ((enables & 0xff) << 8) | (format & 0xff),
997 (unsigned char *) buf, len) == len;
1001 usrp_9862_write (struct usb_dev_handle *udh, int which_codec,
1002 int regno, int value)
1005 fprintf (stderr, "usrp_9862_write which = %d, reg = %2d, val = %3d (0x%02x)\n",
1006 which_codec, regno, value, value);
1008 unsigned char buf[1];
1012 return usrp_spi_write (udh, 0x00 | (regno & 0x3f),
1013 which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
1014 SPI_FMT_MSB | SPI_FMT_HDR_1,
1019 usrp_9862_read (struct usb_dev_handle *udh, int which_codec,
1020 int regno, unsigned char *value)
1022 return usrp_spi_read (udh, 0x80 | (regno & 0x3f),
1023 which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
1024 SPI_FMT_MSB | SPI_FMT_HDR_1,
1029 usrp_9862_write_many (struct usb_dev_handle *udh,
1031 const unsigned char *buf,
1035 return false; // must be even
1040 result &= usrp_9862_write (udh, which_codec, buf[0], buf[1]);
1050 usrp_9862_write_many_all (struct usb_dev_handle *udh,
1051 const unsigned char *buf, int len)
1053 // FIXME handle 2/2 and 4/4 versions
1056 result = usrp_9862_write_many (udh, 0, buf, len);
1057 result &= usrp_9862_write_many (udh, 1, buf, len);
1062 power_down_9862s (struct usb_dev_handle *udh)
1064 static const unsigned char regs[] = {
1065 REG_RX_PWR_DN, 0x01, // everything
1066 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
1067 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
1070 switch (usrp_hw_rev (dev_handle_to_dev (udh))){
1075 usrp_9862_write_many_all (udh, regs, sizeof (regs));
1082 static const int EEPROM_PAGESIZE = 16;
1085 usrp_eeprom_write (struct usb_dev_handle *udh, int i2c_addr,
1086 int eeprom_offset, const void *buf, int len)
1088 unsigned char cmd[2];
1089 const unsigned char *p = (unsigned char *) buf;
1091 // The simplest thing that could possibly work:
1092 // all writes are single byte writes.
1094 // We could speed this up using the page write feature,
1095 // but we write so infrequently, why bother...
1098 cmd[0] = eeprom_offset++;
1100 bool r = usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd));
1101 mdelay (10); // delay 10ms worst case write time
1110 usrp_eeprom_read (struct usb_dev_handle *udh, int i2c_addr,
1111 int eeprom_offset, void *buf, int len)
1113 unsigned char *p = (unsigned char *) buf;
1115 // We setup a random read by first doing a "zero byte write".
1116 // Writes carry an address. Reads use an implicit address.
1118 unsigned char cmd[1];
1119 cmd[0] = eeprom_offset;
1120 if (!usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd)))
1124 int n = std::min (len, MAX_EP0_PKTSIZE);
1125 if (!usrp_i2c_read (udh, i2c_addr, p, n))
1133 // ----------------------------------------------------------------
1136 slot_to_codec (int slot, int *which_codec)
1152 fprintf (stderr, "usrp_prims:slot_to_codec: invalid slot = %d\n", slot);
1159 tx_slot_p (int slot)
1172 usrp_write_aux_dac (struct usb_dev_handle *udh, int slot,
1173 int which_dac, int value)
1177 if (!slot_to_codec (slot, &which_codec))
1180 if (!(0 <= which_dac && which_dac < 4)){
1181 fprintf (stderr, "usrp_write_aux_dac: invalid dac = %d\n", which_dac);
1185 value &= 0x0fff; // mask to 12-bits
1187 if (which_dac == 3){
1188 // dac 3 is really 12-bits. Use value as is.
1190 r &= usrp_9862_write (udh, which_codec, 43, (value >> 4)); // most sig
1191 r &= usrp_9862_write (udh, which_codec, 42, (value & 0xf) << 4); // least sig
1195 // dac 0, 1, and 2 are really 8 bits.
1196 value = value >> 4; // shift value appropriately
1197 return usrp_9862_write (udh, which_codec, 36 + which_dac, value);
1203 usrp_read_aux_adc (struct usb_dev_handle *udh, int slot,
1204 int which_adc, int *value)
1209 if (!slot_to_codec (slot, &which_codec))
1212 if (!(0 <= which_codec && which_codec < 2)){
1213 fprintf (stderr, "usrp_read_aux_adc: invalid adc = %d\n", which_adc);
1217 unsigned char aux_adc_control =
1218 AUX_ADC_CTRL_REFSEL_A // on chip reference
1219 | AUX_ADC_CTRL_REFSEL_B; // on chip reference
1221 int rd_reg = 26; // base address of two regs to read for result
1223 // program the ADC mux bits
1224 if (tx_slot_p (slot))
1225 aux_adc_control |= AUX_ADC_CTRL_SELECT_A2 | AUX_ADC_CTRL_SELECT_B2;
1228 aux_adc_control |= AUX_ADC_CTRL_SELECT_A1 | AUX_ADC_CTRL_SELECT_B1;
1231 // I'm not sure if we can set the mux and issue a start conversion
1232 // in the same cycle, so let's do them one at a time.
1234 usrp_9862_write (udh, which_codec, 34, aux_adc_control);
1237 aux_adc_control |= AUX_ADC_CTRL_START_A;
1240 aux_adc_control |= AUX_ADC_CTRL_START_B;
1243 // start the conversion
1244 usrp_9862_write (udh, which_codec, 34, aux_adc_control);
1246 // read the 10-bit result back
1247 unsigned char v_lo = 0;
1248 unsigned char v_hi = 0;
1249 bool r = usrp_9862_read (udh, which_codec, rd_reg, &v_lo);
1250 r &= usrp_9862_read (udh, which_codec, rd_reg + 1, &v_hi);
1253 *value = ((v_hi << 2) | ((v_lo >> 6) & 0x3)) << 2; // format as 12-bit
1258 // ----------------------------------------------------------------
1260 static int slot_to_i2c_addr (int slot)
1263 case SLOT_TX_A: return I2C_ADDR_TX_A;
1264 case SLOT_RX_A: return I2C_ADDR_RX_A;
1265 case SLOT_TX_B: return I2C_ADDR_TX_B;
1266 case SLOT_RX_B: return I2C_ADDR_RX_B;
1272 set_chksum (unsigned char *buf)
1276 for (i = 0; i < DB_EEPROM_CLEN - 1; i++)
1281 static usrp_dbeeprom_status_t
1282 read_dboard_eeprom (struct usb_dev_handle *udh,
1283 int slot_id, unsigned char *buf)
1285 int i2c_addr = slot_to_i2c_addr (slot_id);
1287 return UDBE_BAD_SLOT;
1289 if (!usrp_eeprom_read (udh, i2c_addr, 0, buf, DB_EEPROM_CLEN))
1290 return UDBE_NO_EEPROM;
1292 if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE)
1293 return UDBE_INVALID_EEPROM;
1296 for (unsigned int i = 0; i < DB_EEPROM_CLEN; i++)
1299 if ((sum & 0xff) != 0)
1300 return UDBE_INVALID_EEPROM;
1305 usrp_dbeeprom_status_t
1306 usrp_read_dboard_eeprom (struct usb_dev_handle *udh,
1307 int slot_id, usrp_dboard_eeprom *eeprom)
1309 unsigned char buf[DB_EEPROM_CLEN];
1311 memset (eeprom, 0, sizeof (*eeprom));
1313 usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
1317 eeprom->id = (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB];
1318 eeprom->oe = (buf[DB_EEPROM_OE_MSB] << 8) | buf[DB_EEPROM_OE_LSB];
1319 eeprom->offset[0] = (buf[DB_EEPROM_OFFSET_0_MSB] << 8) | buf[DB_EEPROM_OFFSET_0_LSB];
1320 eeprom->offset[1] = (buf[DB_EEPROM_OFFSET_1_MSB] << 8) | buf[DB_EEPROM_OFFSET_1_LSB];
1326 usrp_write_dboard_offsets (struct usb_dev_handle *udh, int slot_id,
1327 short offset0, short offset1)
1329 unsigned char buf[DB_EEPROM_CLEN];
1331 usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
1335 buf[DB_EEPROM_OFFSET_0_LSB] = (offset0 >> 0) & 0xff;
1336 buf[DB_EEPROM_OFFSET_0_MSB] = (offset0 >> 8) & 0xff;
1337 buf[DB_EEPROM_OFFSET_1_LSB] = (offset1 >> 0) & 0xff;
1338 buf[DB_EEPROM_OFFSET_1_MSB] = (offset1 >> 8) & 0xff;
1341 return usrp_eeprom_write (udh, slot_to_i2c_addr (slot_id),
1342 0, buf, sizeof (buf));
1346 usrp_serial_number(struct usb_dev_handle *udh)
1348 unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
1353 if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)