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 usrp_one_time_init (bool new_context)
71 static bool first = true;
72 libusb_context *ctx = NULL;
75 // On first call create default context in addition to any new requested
76 // context. The default context is probably useless in this form, but keep
77 // it for now due to compatibility reasons.
81 if ((ret = libusb_init (NULL)) < 0)
82 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
86 if ((ret = libusb_init (&ctx)) < 0)
87 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
99 // ----------------------------------------------------------------
102 * q must be a real USRP, not an FX2. Return its hardware rev number.
105 usrp_hw_rev (struct libusb_device *q)
107 struct libusb_device_descriptor desc;
108 if (libusb_get_device_descriptor(q, &desc) < 0)
109 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
111 return desc.bcdDevice & 0x00FF;
115 * q must be a real USRP, not an FX2. Return true if it's configured.
118 _usrp_configured_p (struct libusb_device *q)
120 struct libusb_device_descriptor desc;
121 if (libusb_get_device_descriptor(q, &desc) < 0)
122 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
124 return (desc.bcdDevice & 0xFF00) != 0;
128 usrp_usrp_p (struct libusb_device *q)
130 struct libusb_device_descriptor desc;
131 if (libusb_get_device_descriptor(q, &desc) < 0)
132 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
134 return (desc.idVendor == USB_VID_FSF
135 && desc.idProduct == USB_PID_FSF_USRP);
139 usrp_fx2_p (struct libusb_device *q)
141 struct libusb_device_descriptor desc;
142 if (libusb_get_device_descriptor(q, &desc) < 0)
143 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
145 return (desc.idVendor == USB_VID_CYPRESS
146 && desc.idProduct == USB_PID_CYPRESS_FX2);
150 // ----------------------------------------------------------------
152 struct libusb_device *
153 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
155 libusb_device **list;
157 struct libusb_device *q;
160 //usrp_one_time_init (false);
161 assert (ctx != NULL);
163 size_t cnt = libusb_get_device_list(ctx, &list);
167 fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
169 for (i = 0; i < cnt; i++) {
171 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
172 if (n_found == nth) // return this one
174 n_found++; // keep looking
179 * The list needs to be freed. Right now just release it if nothing is found.
182 libusb_free_device_list(list, 1);
184 return 0; // not found
187 struct libusb_device_handle *
188 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
190 struct libusb_device_handle *udh;
193 if (libusb_open (dev, &udh) < 0)
196 if (dev != libusb_get_device (udh)){
197 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
201 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
202 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
203 fprintf (stderr, "%d\n", ret);
208 if ((ret = libusb_set_interface_alt_setting (udh, interface,
209 altinterface)) < 0) {
210 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
211 fprintf (stderr, "%d\n", ret);
212 libusb_release_interface (udh, interface);
221 usrp_close_interface (libusb_device_handle *udh)
228 // ----------------------------------------------------------------
229 // write internal ram using Cypress vendor extension
232 write_internal_ram (struct libusb_device_handle *udh, unsigned char *buf,
233 int start_addr, size_t len)
238 int quanta = MAX_EP0_PKTSIZE;
240 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
241 n = len + start_addr - addr;
245 a = libusb_control_transfer (udh, 0x40, 0xA0,
246 addr, 0, (unsigned char *)(buf + (addr - start_addr)), n, 1000);
249 fprintf(stderr,"write_internal_ram failed: %u\n", a);
256 // ----------------------------------------------------------------
257 // write vendor extension command to USRP
260 write_cmd (struct libusb_device_handle *udh,
261 int request, int value, int index,
262 unsigned char *bytes, int len)
264 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
266 int r = libusb_control_transfer(udh, requesttype, request, value, index,
267 (unsigned char *) bytes, len, 1000);
270 // we get EPIPE if the firmware stalls the endpoint.
271 if (r != LIBUSB_ERROR_PIPE) {
272 fprintf (stderr, "libusb_control_transfer failed: %i\n", r);
280 usrp_set_hash (struct libusb_device_handle *udh, int which,
281 const unsigned char hash[USRP_HASH_SIZE])
285 // we use the Cypress firmware down load command to jam it in.
286 int r = libusb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
287 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
288 return r == USRP_HASH_SIZE;
292 usrp_get_hash (struct libusb_device_handle *udh, int which,
293 unsigned char hash[USRP_HASH_SIZE])
297 // we use the Cypress firmware upload command to fetch it.
298 int r = libusb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
299 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
300 return r == USRP_HASH_SIZE;
304 usrp_write_fpga_reg (struct libusb_device_handle *udh, int reg, int value)
306 switch (usrp_hw_rev (libusb_get_device (udh))){
307 case 0: // not supported ;)
311 return usrp1_fpga_write (udh, reg, value);
316 usrp_read_fpga_reg (struct libusb_device_handle *udh, int reg, int *value)
318 switch (usrp_hw_rev (libusb_get_device (udh))){
319 case 0: // not supported ;)
323 return usrp1_fpga_read (udh, reg, value);
328 static libusb_device_handle *
329 open_nth_cmd_interface (int nth, libusb_context *ctx)
332 struct libusb_device *udev = usrp_find_device (nth, false, ctx);
334 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
338 struct libusb_device_handle *udh;
340 udh = usrp_open_cmd_interface (udev);
342 // FIXME this could be because somebody else has it open.
343 // We should delay and retry...
344 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
352 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx)
354 struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
358 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
359 usrp_close_interface (udh);
363 case ULS_ALREADY_LOADED: // nothing changed...
364 return ULS_ALREADY_LOADED;
368 // we loaded firmware successfully.
370 // It's highly likely that the board will renumerate (simulate a
371 // disconnect/reconnect sequence), invalidating our current
374 // FIXME. Turn this into a loop that rescans until we refind ourselves
376 struct timespec t; // delay for 1 second
384 case ULS_ERROR: // some kind of problem
390 usrp_load_standard_bits (int nth, bool force,
391 const std::string fpga_filename,
392 const std::string firmware_filename,
395 usrp_load_status_t s;
396 const char *filename;
397 const char *proto_filename;
400 assert (ctx != NULL);
402 // first, figure out what hardware rev we're dealing with
404 struct libusb_device *udev = usrp_find_device (nth, false, ctx);
406 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
409 hw_rev = usrp_hw_rev (udev);
412 // start by loading the firmware
414 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
415 default_firmware_filename);
416 filename = find_file(proto_filename, hw_rev);
418 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
421 s = usrp_load_firmware_nth (nth, filename, force, ctx);
422 load_status_msg (s, "firmware", filename);
427 // if we actually loaded firmware, we must reload fpga ...
431 // now move on to the fpga configuration bitstream
433 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
434 default_fpga_filename);
435 filename = find_file (proto_filename, hw_rev);
437 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
440 struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
444 s = usrp_load_fpga (udh, filename, force);
445 usrp_close_interface (udh);
446 load_status_msg (s, "fpga bitstream", filename);
455 power_down_9862s (struct libusb_device_handle *udh)
457 static const unsigned char regs[] = {
458 REG_RX_PWR_DN, 0x01, // everything
459 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
460 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
463 switch (usrp_hw_rev (libusb_get_device (udh))){
468 usrp_9862_write_many_all (udh, regs, sizeof (regs));
474 // ----------------------------------------------------------------
477 usrp_serial_number(struct libusb_device_handle *udh)
479 struct libusb_device_descriptor desc;
480 if (libusb_get_device_descriptor(libusb_get_device(udh), &desc) < 0)
481 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
483 unsigned char iserial = desc.iSerialNumber;
487 unsigned char buf[1024];
488 if (libusb_get_string_descriptor_ascii(udh, iserial, buf, sizeof(buf)) < 0)