3 * Copyright 2003,2004,2006,2009 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/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"
33 #include <libusb-1.0/libusb.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 libusb_device_handle *udh);
107 usrp_one_time_init ()
109 static bool first = true;
113 libusb_init (NULL); // usb library init
123 // ----------------------------------------------------------------
126 * q must be a real USRP, not an FX2. Return its hardware rev number.
129 usrp_hw_rev (struct libusb_device *q)
131 struct libusb_device_descriptor desc;
132 if (libusb_get_device_descriptor(q, &desc) < 0)
133 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
135 return desc.bcdDevice & 0x00FF;
139 * q must be a real USRP, not an FX2. Return true if it's configured.
142 _usrp_configured_p (struct libusb_device *q)
144 struct libusb_device_descriptor desc;
145 if (libusb_get_device_descriptor(q, &desc) < 0)
146 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
148 return (desc.bcdDevice & 0xFF00) != 0;
152 usrp_usrp_p (struct libusb_device *q)
154 struct libusb_device_descriptor desc;
155 if (libusb_get_device_descriptor(q, &desc) < 0)
156 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
158 return (desc.idVendor == USB_VID_FSF
159 && desc.idProduct == USB_PID_FSF_USRP);
163 usrp_fx2_p (struct libusb_device *q)
165 struct libusb_device_descriptor desc;
166 if (libusb_get_device_descriptor(q, &desc) < 0)
167 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
169 return (desc.idVendor == USB_VID_CYPRESS
170 && desc.idProduct == USB_PID_CYPRESS_FX2);
174 usrp_usrp0_p (struct libusb_device *q)
176 return usrp_usrp_p (q) && usrp_hw_rev (q) == 0;
180 usrp_usrp1_p (struct libusb_device *q)
182 return usrp_usrp_p (q) && usrp_hw_rev (q) == 1;
186 usrp_usrp2_p (struct libusb_device *q)
188 return usrp_usrp_p (q) && usrp_hw_rev (q) == 2;
193 usrp_unconfigured_usrp_p (struct libusb_device *q)
195 return usrp_usrp_p (q) && !_usrp_configured_p (q);
199 usrp_configured_usrp_p (struct libusb_device *q)
201 return usrp_usrp_p (q) && _usrp_configured_p (q);
204 // ----------------------------------------------------------------
206 struct libusb_device *
207 usrp_find_device (int nth, bool fx2_ok_p)
209 libusb_device **list;
211 struct libusb_device *q;
214 usrp_one_time_init ();
216 size_t cnt = libusb_get_device_list(NULL, &list);
220 fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
222 for (i = 0; i < cnt; i++) {
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
231 libusb_free_device_list(list, 1);
232 return 0; // not found
235 static struct libusb_device_handle *
236 usrp_open_interface (struct libusb_device *dev, int interface, int altinterface)
238 struct libusb_device_handle *udh;
241 if (libusb_open (dev, &udh) < 0)
244 if (dev != libusb_get_device (udh)){
245 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
249 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
250 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
251 fprintf (stderr, "%d\n", ret);
256 if ((ret = libusb_set_interface_alt_setting (udh, interface,
257 altinterface)) < 0) {
258 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
259 fprintf (stderr, "%d\n", ret);
260 libusb_release_interface (udh, interface);
268 struct libusb_device_handle *
269 usrp_open_cmd_interface (struct libusb_device *dev)
271 return usrp_open_interface (dev, USRP_CMD_INTERFACE, USRP_CMD_ALTINTERFACE);
274 struct libusb_device_handle *
275 usrp_open_rx_interface (struct libusb_device *dev)
277 return usrp_open_interface (dev, USRP_RX_INTERFACE, USRP_RX_ALTINTERFACE);
280 struct libusb_device_handle *
281 usrp_open_tx_interface (struct libusb_device *dev)
283 return usrp_open_interface (dev, USRP_TX_INTERFACE, USRP_TX_ALTINTERFACE);
287 usrp_close_interface (struct libusb_device_handle *udh)
294 // ----------------------------------------------------------------
295 // write internal ram using Cypress vendor extension
298 write_internal_ram (struct libusb_device_handle *udh, unsigned char *buf,
299 int start_addr, size_t len)
304 int quanta = MAX_EP0_PKTSIZE;
306 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
307 n = len + start_addr - addr;
311 a = libusb_control_transfer (udh, 0x40, 0xA0,
312 addr, 0, (unsigned char *)(buf + (addr - start_addr)), n, 1000);
315 fprintf(stderr,"write_internal_ram failed: %u\n", a);
322 // ----------------------------------------------------------------
323 // whack the CPUCS register using the upload RAM vendor extension
326 reset_cpu (struct libusb_device_handle *udh, bool reset_p)
331 v = 1; // hold processor in reset
333 v = 0; // release reset
335 return write_internal_ram (udh, &v, 0xE600, 1);
338 // ----------------------------------------------------------------
339 // Load intel format file into cypress FX2 (8051)
342 _usrp_load_firmware (struct libusb_device_handle *udh, const char *filename,
343 unsigned char hash[USRP_HASH_SIZE])
345 FILE *f = fopen (filename, "ra");
351 if (!reset_cpu (udh, true)) // hold CPU in reset while loading firmware
359 unsigned char data[256];
360 unsigned char checksum, a;
365 fgets(s, sizeof (s), f); /* we should not use more than 263 bytes normally */
367 fprintf(stderr,"%s: invalid line: \"%s\"\n", filename, s);
370 sscanf(s+1, "%02x", &length);
371 sscanf(s+3, "%04x", &addr);
372 sscanf(s+7, "%02x", &type);
376 a=length+(addr &0xff)+(addr>>8)+type;
377 for(i=0;i<length;i++){
378 sscanf (s+9+i*2,"%02x", &b);
383 sscanf (s+9+length*2,"%02x", &b);
385 if (((a+checksum)&0xff)!=0x00){
386 fprintf (stderr, " ** Checksum failed: got 0x%02x versus 0x%02x\n", (-a)&0xff, checksum);
389 if (!write_internal_ram (udh, data, addr, length))
392 else if (type == 0x01){ // EOF
395 else if (type == 0x02){
396 fprintf(stderr, "Extended address: whatever I do with it?\n");
397 fprintf (stderr, "%s: invalid line: \"%s\"\n", filename, s);
402 // we jam the hash value into the FX2 memory before letting
403 // the cpu out of reset. When it comes out of reset it
404 // may renumerate which will invalidate udh.
406 if (!usrp_set_hash (udh, FIRMWARE_HASH_SLOT, hash))
407 fprintf (stderr, "usrp: failed to write firmware hash slot\n");
409 if (!reset_cpu (udh, false)) // take CPU out of reset
420 // ----------------------------------------------------------------
421 // write vendor extension command to USRP
424 write_cmd (struct libusb_device_handle *udh,
425 int request, int value, int index,
426 unsigned char *bytes, int len)
428 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
430 int r = libusb_control_transfer(udh, requesttype, request, value, index,
431 (unsigned char *) bytes, len, 1000);
434 // we get EPIPE if the firmware stalls the endpoint.
435 if (r != LIBUSB_ERROR_PIPE) {
436 fprintf (stderr, "libusb_control_transfer failed: %i\n", r);
443 // ----------------------------------------------------------------
447 _usrp_load_fpga (struct libusb_device_handle *udh, const char *filename,
448 unsigned char hash[USRP_HASH_SIZE])
452 FILE *fp = fopen (filename, "rb");
458 unsigned char buf[MAX_EP0_PKTSIZE]; // 64 is max size of EP0 packet on FX2
461 usrp_set_led (udh, 1, 1); // led 1 on
464 // reset FPGA (and on rev1 both AD9862's, thus killing clock)
465 usrp_set_fpga_reset (udh, 1); // hold fpga in reset
467 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_BEGIN, 0, 0) != 0)
470 while ((n = fread (buf, 1, sizeof (buf), fp)) > 0){
471 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_XFER, buf, n) != n)
475 if (write_cmd (udh, VRQ_FPGA_LOAD, 0, FL_END, 0, 0) != 0)
480 if (!usrp_set_hash (udh, FPGA_HASH_SLOT, hash))
481 fprintf (stderr, "usrp: failed to write fpga hash slot\n");
483 // On the rev1 USRP, the {tx,rx}_{enable,reset} bits are
484 // controlled over the serial bus, and hence aren't observed until
485 // we've got a good fpga bitstream loaded.
487 usrp_set_fpga_reset (udh, 0); // fpga out of master reset
489 // now these commands will work
491 ok &= usrp_set_fpga_tx_enable (udh, 0);
492 ok &= usrp_set_fpga_rx_enable (udh, 0);
494 ok &= usrp_set_fpga_tx_reset (udh, 1); // reset tx and rx paths
495 ok &= usrp_set_fpga_rx_reset (udh, 1);
496 ok &= usrp_set_fpga_tx_reset (udh, 0); // reset tx and rx paths
497 ok &= usrp_set_fpga_rx_reset (udh, 0);
500 fprintf (stderr, "usrp: failed to reset tx and/or rx path\n");
502 // Manually reset all regs except master control to zero.
503 // FIXME may want to remove this when we rework FPGA reset strategy.
504 // In the mean while, this gets us reproducible behavior.
505 for (int i = 0; i < FR_USER_0; i++){
506 if (i == FR_MASTER_CTRL)
508 usrp_write_fpga_reg(udh, i, 0);
511 power_down_9862s (udh); // on the rev1, power these down!
512 usrp_set_led (udh, 1, 0); // led 1 off
517 power_down_9862s (udh); // on the rev1, power these down!
522 // ----------------------------------------------------------------
525 usrp_set_led (struct libusb_device_handle *udh, int which, bool on)
527 int r = write_cmd (udh, VRQ_SET_LED, on, which, 0, 0);
533 usrp_set_hash (struct libusb_device_handle *udh, int which,
534 const unsigned char hash[USRP_HASH_SIZE])
538 // we use the Cypress firmware down load command to jam it in.
539 int r = libusb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
540 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
541 return r == USRP_HASH_SIZE;
545 usrp_get_hash (struct libusb_device_handle *udh, int which,
546 unsigned char hash[USRP_HASH_SIZE])
550 // we use the Cypress firmware upload command to fetch it.
551 int r = libusb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
552 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
553 return r == USRP_HASH_SIZE;
557 usrp_set_switch (struct libusb_device_handle *udh, int cmd_byte, bool on)
559 return write_cmd (udh, cmd_byte, on, 0, 0, 0) == 0;
564 usrp1_fpga_write (struct libusb_device_handle *udh,
565 int regno, int value)
567 // on the rev1 usrp, we use the generic spi_write interface
569 unsigned char buf[4];
571 buf[0] = (value >> 24) & 0xff; // MSB first
572 buf[1] = (value >> 16) & 0xff;
573 buf[2] = (value >> 8) & 0xff;
574 buf[3] = (value >> 0) & 0xff;
576 return usrp_spi_write (udh, 0x00 | (regno & 0x7f),
578 SPI_FMT_MSB | SPI_FMT_HDR_1,
583 usrp1_fpga_read (struct libusb_device_handle *udh,
584 int regno, int *value)
587 unsigned char buf[4];
589 bool ok = usrp_spi_read (udh, 0x80 | (regno & 0x7f),
591 SPI_FMT_MSB | SPI_FMT_HDR_1,
595 *value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
602 usrp_write_fpga_reg (struct libusb_device_handle *udh, int reg, int value)
604 switch (usrp_hw_rev (libusb_get_device (udh))){
605 case 0: // not supported ;)
609 return usrp1_fpga_write (udh, reg, value);
614 usrp_read_fpga_reg (struct libusb_device_handle *udh, int reg, int *value)
616 switch (usrp_hw_rev (libusb_get_device (udh))){
617 case 0: // not supported ;)
621 return usrp1_fpga_read (udh, reg, value);
626 usrp_set_fpga_reset (struct libusb_device_handle *udh, bool on)
628 return usrp_set_switch (udh, VRQ_FPGA_SET_RESET, on);
632 usrp_set_fpga_tx_enable (struct libusb_device_handle *udh, bool on)
634 return usrp_set_switch (udh, VRQ_FPGA_SET_TX_ENABLE, on);
638 usrp_set_fpga_rx_enable (struct libusb_device_handle *udh, bool on)
640 return usrp_set_switch (udh, VRQ_FPGA_SET_RX_ENABLE, on);
644 usrp_set_fpga_tx_reset (struct libusb_device_handle *udh, bool on)
646 return usrp_set_switch (udh, VRQ_FPGA_SET_TX_RESET, on);
650 usrp_set_fpga_rx_reset (struct libusb_device_handle *udh, bool on)
652 return usrp_set_switch (udh, VRQ_FPGA_SET_RX_RESET, on);
656 // ----------------------------------------------------------------
657 // conditional load stuff
660 compute_hash (const char *filename, unsigned char hash[USRP_HASH_SIZE])
662 assert (USRP_HASH_SIZE == 16);
663 memset (hash, 0, USRP_HASH_SIZE);
665 FILE *fp = fopen (filename, "rb");
670 int r = md5_stream (fp, hash);
676 static usrp_load_status_t
677 usrp_conditionally_load_something (struct libusb_device_handle *udh,
678 const char *filename,
681 bool loader (struct libusb_device_handle *,
683 unsigned char [USRP_HASH_SIZE]))
685 unsigned char file_hash[USRP_HASH_SIZE];
686 unsigned char usrp_hash[USRP_HASH_SIZE];
688 if (access (filename, R_OK) != 0){
693 if (!compute_hash (filename, file_hash))
697 && usrp_get_hash (udh, slot, usrp_hash)
698 && memcmp (file_hash, usrp_hash, USRP_HASH_SIZE) == 0)
699 return ULS_ALREADY_LOADED;
701 bool r = loader (udh, filename, file_hash);
710 usrp_load_firmware (struct libusb_device_handle *udh,
711 const char *filename,
714 return usrp_conditionally_load_something (udh, filename, force,
716 _usrp_load_firmware);
720 usrp_load_fpga (struct libusb_device_handle *udh,
721 const char *filename,
724 return usrp_conditionally_load_something (udh, filename, force,
729 static libusb_device_handle *
730 open_nth_cmd_interface (int nth)
732 struct libusb_device *udev = usrp_find_device (nth);
734 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
738 struct libusb_device_handle *udh;
740 udh = usrp_open_cmd_interface (udev);
742 // FIXME this could be because somebody else has it open.
743 // We should delay and retry...
744 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
752 our_nanosleep (const struct timespec *delay)
754 struct timespec new_delay = *delay;
755 struct timespec remainder;
758 int r = nanosleep (&new_delay, &remainder);
762 new_delay = remainder;
764 perror ("nanosleep");
771 mdelay (int millisecs)
774 ts.tv_sec = millisecs / 1000;
775 ts.tv_nsec = (millisecs - (1000 * ts.tv_sec)) * 1000000;
776 return our_nanosleep (&ts);
780 usrp_load_firmware_nth (int nth, const char *filename, bool force){
781 struct libusb_device_handle *udh = open_nth_cmd_interface (nth);
785 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
786 usrp_close_interface (udh);
790 case ULS_ALREADY_LOADED: // nothing changed...
791 return ULS_ALREADY_LOADED;
795 // we loaded firmware successfully.
797 // It's highly likely that the board will renumerate (simulate a
798 // disconnect/reconnect sequence), invalidating our current
801 // FIXME. Turn this into a loop that rescans until we refind ourselves
803 struct timespec t; // delay for 1 second
811 case ULS_ERROR: // some kind of problem
817 load_status_msg (usrp_load_status_t s, const char *type, const char *filename)
819 char *e = getenv("USRP_VERBOSE");
820 bool verbose = e != 0;
824 fprintf (stderr, "usrp: failed to load %s %s.\n", type, filename);
827 case ULS_ALREADY_LOADED:
829 fprintf (stderr, "usrp: %s %s already loaded.\n", type, filename);
834 fprintf (stderr, "usrp: %s %s loaded successfully.\n", type, filename);
840 usrp_load_standard_bits (int nth, bool force,
841 const std::string fpga_filename,
842 const std::string firmware_filename)
844 usrp_load_status_t s;
845 const char *filename;
846 const char *proto_filename;
849 // first, figure out what hardware rev we're dealing with
851 struct libusb_device *udev = usrp_find_device (nth);
853 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
856 hw_rev = usrp_hw_rev (udev);
859 // start by loading the firmware
861 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
862 default_firmware_filename);
863 filename = find_file(proto_filename, hw_rev);
865 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
869 s = usrp_load_firmware_nth (nth, filename, force);
870 load_status_msg (s, "firmware", filename);
875 // if we actually loaded firmware, we must reload fpga ...
879 // now move on to the fpga configuration bitstream
881 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
882 default_fpga_filename);
883 filename = find_file (proto_filename, hw_rev);
885 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
889 struct libusb_device_handle *udh = open_nth_cmd_interface (nth);
893 s = usrp_load_fpga (udh, filename, force);
894 usrp_close_interface (udh);
895 load_status_msg (s, "fpga bitstream", filename);
904 _usrp_get_status (struct libusb_device_handle *udh, int which, bool *trouble)
906 unsigned char status;
909 if (write_cmd (udh, VRQ_GET_STATUS, 0, which,
910 &status, sizeof (status)) != sizeof (status))
918 usrp_check_rx_overrun (struct libusb_device_handle *udh, bool *overrun_p)
920 return _usrp_get_status (udh, GS_RX_OVERRUN, overrun_p);
924 usrp_check_tx_underrun (struct libusb_device_handle *udh, bool *underrun_p)
926 return _usrp_get_status (udh, GS_TX_UNDERRUN, underrun_p);
931 usrp_i2c_write (struct libusb_device_handle *udh, int i2c_addr,
932 const void *buf, int len)
934 if (len < 1 || len > MAX_EP0_PKTSIZE)
937 return write_cmd (udh, VRQ_I2C_WRITE, i2c_addr, 0,
938 (unsigned char *) buf, len) == len;
943 usrp_i2c_read (struct libusb_device_handle *udh, int i2c_addr,
946 if (len < 1 || len > MAX_EP0_PKTSIZE)
949 return write_cmd (udh, VRQ_I2C_READ, i2c_addr, 0,
950 (unsigned char *) buf, len) == len;
954 usrp_spi_write (struct libusb_device_handle *udh,
955 int optional_header, int enables, int format,
956 const void *buf, int len)
958 if (len < 0 || len > MAX_EP0_PKTSIZE)
961 return write_cmd (udh, VRQ_SPI_WRITE,
963 ((enables & 0xff) << 8) | (format & 0xff),
964 (unsigned char *) buf, len) == len;
969 usrp_spi_read (struct libusb_device_handle *udh,
970 int optional_header, int enables, int format,
973 if (len < 0 || len > MAX_EP0_PKTSIZE)
976 return write_cmd (udh, VRQ_SPI_READ,
978 ((enables & 0xff) << 8) | (format & 0xff),
979 (unsigned char *) buf, len) == len;
983 usrp_9862_write (struct libusb_device_handle *udh, int which_codec,
984 int regno, int value)
987 fprintf (stderr, "usrp_9862_write which = %d, reg = %2d, val = %3d (0x%02x)\n",
988 which_codec, regno, value, value);
990 unsigned char buf[1];
994 return usrp_spi_write (udh, 0x00 | (regno & 0x3f),
995 which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
996 SPI_FMT_MSB | SPI_FMT_HDR_1,
1001 usrp_9862_read (struct libusb_device_handle *udh, int which_codec,
1002 int regno, unsigned char *value)
1004 return usrp_spi_read (udh, 0x80 | (regno & 0x3f),
1005 which_codec == 0 ? SPI_ENABLE_CODEC_A : SPI_ENABLE_CODEC_B,
1006 SPI_FMT_MSB | SPI_FMT_HDR_1,
1011 usrp_9862_write_many (struct libusb_device_handle *udh,
1013 const unsigned char *buf,
1017 return false; // must be even
1022 result &= usrp_9862_write (udh, which_codec, buf[0], buf[1]);
1032 usrp_9862_write_many_all (struct libusb_device_handle *udh,
1033 const unsigned char *buf, int len)
1035 // FIXME handle 2/2 and 4/4 versions
1038 result = usrp_9862_write_many (udh, 0, buf, len);
1039 result &= usrp_9862_write_many (udh, 1, buf, len);
1044 power_down_9862s (struct libusb_device_handle *udh)
1046 static const unsigned char regs[] = {
1047 REG_RX_PWR_DN, 0x01, // everything
1048 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
1049 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
1052 switch (usrp_hw_rev (libusb_get_device (udh))){
1057 usrp_9862_write_many_all (udh, regs, sizeof (regs));
1064 static const int EEPROM_PAGESIZE = 16;
1067 usrp_eeprom_write (struct libusb_device_handle *udh, int i2c_addr,
1068 int eeprom_offset, const void *buf, int len)
1070 unsigned char cmd[2];
1071 const unsigned char *p = (unsigned char *) buf;
1073 // The simplest thing that could possibly work:
1074 // all writes are single byte writes.
1076 // We could speed this up using the page write feature,
1077 // but we write so infrequently, why bother...
1080 cmd[0] = eeprom_offset++;
1082 bool r = usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd));
1083 mdelay (10); // delay 10ms worst case write time
1092 usrp_eeprom_read (struct libusb_device_handle *udh, int i2c_addr,
1093 int eeprom_offset, void *buf, int len)
1095 unsigned char *p = (unsigned char *) buf;
1097 // We setup a random read by first doing a "zero byte write".
1098 // Writes carry an address. Reads use an implicit address.
1100 unsigned char cmd[1];
1101 cmd[0] = eeprom_offset;
1102 if (!usrp_i2c_write (udh, i2c_addr, cmd, sizeof (cmd)))
1106 int n = std::min (len, MAX_EP0_PKTSIZE);
1107 if (!usrp_i2c_read (udh, i2c_addr, p, n))
1115 // ----------------------------------------------------------------
1118 slot_to_codec (int slot, int *which_codec)
1134 fprintf (stderr, "usrp_prims:slot_to_codec: invalid slot = %d\n", slot);
1141 tx_slot_p (int slot)
1154 usrp_write_aux_dac (struct libusb_device_handle *udh, int slot,
1155 int which_dac, int value)
1159 if (!slot_to_codec (slot, &which_codec))
1162 if (!(0 <= which_dac && which_dac < 4)){
1163 fprintf (stderr, "usrp_write_aux_dac: invalid dac = %d\n", which_dac);
1167 value &= 0x0fff; // mask to 12-bits
1169 if (which_dac == 3){
1170 // dac 3 is really 12-bits. Use value as is.
1172 r &= usrp_9862_write (udh, which_codec, 43, (value >> 4)); // most sig
1173 r &= usrp_9862_write (udh, which_codec, 42, (value & 0xf) << 4); // least sig
1177 // dac 0, 1, and 2 are really 8 bits.
1178 value = value >> 4; // shift value appropriately
1179 return usrp_9862_write (udh, which_codec, 36 + which_dac, value);
1185 usrp_read_aux_adc (struct libusb_device_handle *udh, int slot,
1186 int which_adc, int *value)
1191 if (!slot_to_codec (slot, &which_codec))
1194 if (!(0 <= which_codec && which_codec < 2)){
1195 fprintf (stderr, "usrp_read_aux_adc: invalid adc = %d\n", which_adc);
1199 unsigned char aux_adc_control =
1200 AUX_ADC_CTRL_REFSEL_A // on chip reference
1201 | AUX_ADC_CTRL_REFSEL_B; // on chip reference
1203 int rd_reg = 26; // base address of two regs to read for result
1205 // program the ADC mux bits
1206 if (tx_slot_p (slot))
1207 aux_adc_control |= AUX_ADC_CTRL_SELECT_A2 | AUX_ADC_CTRL_SELECT_B2;
1210 aux_adc_control |= AUX_ADC_CTRL_SELECT_A1 | AUX_ADC_CTRL_SELECT_B1;
1213 // I'm not sure if we can set the mux and issue a start conversion
1214 // in the same cycle, so let's do them one at a time.
1216 usrp_9862_write (udh, which_codec, 34, aux_adc_control);
1219 aux_adc_control |= AUX_ADC_CTRL_START_A;
1222 aux_adc_control |= AUX_ADC_CTRL_START_B;
1225 // start the conversion
1226 usrp_9862_write (udh, which_codec, 34, aux_adc_control);
1228 // read the 10-bit result back
1229 unsigned char v_lo = 0;
1230 unsigned char v_hi = 0;
1231 bool r = usrp_9862_read (udh, which_codec, rd_reg, &v_lo);
1232 r &= usrp_9862_read (udh, which_codec, rd_reg + 1, &v_hi);
1235 *value = ((v_hi << 2) | ((v_lo >> 6) & 0x3)) << 2; // format as 12-bit
1240 // ----------------------------------------------------------------
1242 static int slot_to_i2c_addr (int slot)
1245 case SLOT_TX_A: return I2C_ADDR_TX_A;
1246 case SLOT_RX_A: return I2C_ADDR_RX_A;
1247 case SLOT_TX_B: return I2C_ADDR_TX_B;
1248 case SLOT_RX_B: return I2C_ADDR_RX_B;
1254 set_chksum (unsigned char *buf)
1258 for (i = 0; i < DB_EEPROM_CLEN - 1; i++)
1263 static usrp_dbeeprom_status_t
1264 read_dboard_eeprom (struct libusb_device_handle *udh,
1265 int slot_id, unsigned char *buf)
1267 int i2c_addr = slot_to_i2c_addr (slot_id);
1269 return UDBE_BAD_SLOT;
1271 if (!usrp_eeprom_read (udh, i2c_addr, 0, buf, DB_EEPROM_CLEN))
1272 return UDBE_NO_EEPROM;
1274 if (buf[DB_EEPROM_MAGIC] != DB_EEPROM_MAGIC_VALUE)
1275 return UDBE_INVALID_EEPROM;
1278 for (unsigned int i = 0; i < DB_EEPROM_CLEN; i++)
1281 if ((sum & 0xff) != 0)
1282 return UDBE_INVALID_EEPROM;
1287 usrp_dbeeprom_status_t
1288 usrp_read_dboard_eeprom (struct libusb_device_handle *udh,
1289 int slot_id, usrp_dboard_eeprom *eeprom)
1291 unsigned char buf[DB_EEPROM_CLEN];
1293 memset (eeprom, 0, sizeof (*eeprom));
1295 usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
1299 eeprom->id = (buf[DB_EEPROM_ID_MSB] << 8) | buf[DB_EEPROM_ID_LSB];
1300 eeprom->oe = (buf[DB_EEPROM_OE_MSB] << 8) | buf[DB_EEPROM_OE_LSB];
1301 eeprom->offset[0] = (buf[DB_EEPROM_OFFSET_0_MSB] << 8) | buf[DB_EEPROM_OFFSET_0_LSB];
1302 eeprom->offset[1] = (buf[DB_EEPROM_OFFSET_1_MSB] << 8) | buf[DB_EEPROM_OFFSET_1_LSB];
1308 usrp_write_dboard_offsets (struct libusb_device_handle *udh, int slot_id,
1309 short offset0, short offset1)
1311 unsigned char buf[DB_EEPROM_CLEN];
1313 usrp_dbeeprom_status_t s = read_dboard_eeprom (udh, slot_id, buf);
1317 buf[DB_EEPROM_OFFSET_0_LSB] = (offset0 >> 0) & 0xff;
1318 buf[DB_EEPROM_OFFSET_0_MSB] = (offset0 >> 8) & 0xff;
1319 buf[DB_EEPROM_OFFSET_1_LSB] = (offset1 >> 0) & 0xff;
1320 buf[DB_EEPROM_OFFSET_1_MSB] = (offset1 >> 8) & 0xff;
1323 return usrp_eeprom_write (udh, slot_to_i2c_addr (slot_id),
1324 0, buf, sizeof (buf));
1328 usrp_serial_number(struct libusb_device_handle *udh)
1330 struct libusb_device_descriptor desc;
1331 if (libusb_get_device_descriptor(libusb_get_device(udh), &desc) < 0)
1332 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
1334 unsigned char iserial = desc.iSerialNumber;
1338 unsigned char buf[1024];
1339 if (libusb_get_string_descriptor_ascii(udh, iserial, buf, sizeof(buf)) < 0)