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"
71 usrp_one_time_init (false);
76 usrp_one_time_init (libusb_context **ctx)
80 if ((ret = libusb_init (ctx)) < 0)
81 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
86 usrp_one_time_init (bool new_context)
89 static bool first = true;
90 libusb_context *ctx = NULL;
93 // On first call create default context in addition to any new requested
94 // context. The default context is probably useless in this form, but keep
95 // it for now due to possible compatibility reasons.
99 if ((ret = libusb_init (NULL)) < 0)
100 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
104 if ((ret = libusb_init (&ctx)) < 0)
105 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
118 // ----------------------------------------------------------------
121 * q must be a real USRP, not an FX2. Return its hardware rev number.
124 usrp_hw_rev (struct libusb_device *q)
126 struct libusb_device_descriptor desc;
127 if (libusb_get_device_descriptor(q, &desc) < 0)
128 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
130 return desc.bcdDevice & 0x00FF;
134 * q must be a real USRP, not an FX2. Return true if it's configured.
137 _usrp_configured_p (struct libusb_device *q)
139 struct libusb_device_descriptor desc;
140 if (libusb_get_device_descriptor(q, &desc) < 0)
141 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
143 return (desc.bcdDevice & 0xFF00) != 0;
147 usrp_usrp_p (struct libusb_device *q)
149 struct libusb_device_descriptor desc;
150 if (libusb_get_device_descriptor(q, &desc) < 0)
151 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
153 return (desc.idVendor == USB_VID_FSF
154 && desc.idProduct == USB_PID_FSF_USRP);
158 usrp_fx2_p (struct libusb_device *q)
160 struct libusb_device_descriptor desc;
161 if (libusb_get_device_descriptor(q, &desc) < 0)
162 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
164 return (desc.idVendor == USB_VID_CYPRESS
165 && desc.idProduct == USB_PID_CYPRESS_FX2);
169 // ----------------------------------------------------------------
171 struct libusb_device *
172 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
174 libusb_device **list;
176 struct libusb_device *q;
179 //usrp_one_time_init (false);
180 assert (ctx != NULL);
182 size_t cnt = libusb_get_device_list(ctx, &list);
186 fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
188 for (i = 0; i < cnt; i++) {
190 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
191 if (n_found == nth) // return this one
193 n_found++; // keep looking
198 * The list needs to be freed. Right now just release it if nothing is found.
201 libusb_free_device_list(list, 1);
203 return 0; // not found
206 struct libusb_device_handle *
207 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
209 struct libusb_device_handle *udh;
212 if (libusb_open (dev, &udh) < 0)
215 if (dev != libusb_get_device (udh)){
216 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
220 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
221 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
222 fprintf (stderr, "%d\n", ret);
227 if ((ret = libusb_set_interface_alt_setting (udh, interface,
228 altinterface)) < 0) {
229 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
230 fprintf (stderr, "%d\n", ret);
231 libusb_release_interface (udh, interface);
240 usrp_close_interface (libusb_device_handle *udh)
247 // ----------------------------------------------------------------
248 // write internal ram using Cypress vendor extension
251 write_internal_ram (struct libusb_device_handle *udh, unsigned char *buf,
252 int start_addr, size_t len)
257 int quanta = MAX_EP0_PKTSIZE;
259 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
260 n = len + start_addr - addr;
264 a = libusb_control_transfer (udh, 0x40, 0xA0,
265 addr, 0, (unsigned char *)(buf + (addr - start_addr)), n, 1000);
268 fprintf(stderr,"write_internal_ram failed: %u\n", a);
275 // ----------------------------------------------------------------
276 // write vendor extension command to USRP
279 write_cmd (struct libusb_device_handle *udh,
280 int request, int value, int index,
281 unsigned char *bytes, int len)
283 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
285 int r = libusb_control_transfer(udh, requesttype, request, value, index,
286 (unsigned char *) bytes, len, 1000);
289 // we get EPIPE if the firmware stalls the endpoint.
290 if (r != LIBUSB_ERROR_PIPE) {
291 fprintf (stderr, "libusb_control_transfer failed: %i\n", r);
299 usrp_set_hash (struct libusb_device_handle *udh, int which,
300 const unsigned char hash[USRP_HASH_SIZE])
304 // we use the Cypress firmware down load command to jam it in.
305 int r = libusb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
306 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
307 return r == USRP_HASH_SIZE;
311 usrp_get_hash (struct libusb_device_handle *udh, int which,
312 unsigned char hash[USRP_HASH_SIZE])
316 // we use the Cypress firmware upload command to fetch it.
317 int r = libusb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
318 (unsigned char *) hash, USRP_HASH_SIZE, 1000);
319 return r == USRP_HASH_SIZE;
323 usrp_write_fpga_reg (struct libusb_device_handle *udh, int reg, int value)
325 switch (usrp_hw_rev (libusb_get_device (udh))){
326 case 0: // not supported ;)
330 return usrp1_fpga_write (udh, reg, value);
335 usrp_read_fpga_reg (struct libusb_device_handle *udh, int reg, int *value)
337 switch (usrp_hw_rev (libusb_get_device (udh))){
338 case 0: // not supported ;)
342 return usrp1_fpga_read (udh, reg, value);
347 static libusb_device_handle *
348 open_nth_cmd_interface (int nth, libusb_context *ctx)
351 struct libusb_device *udev = usrp_find_device (nth, false, ctx);
353 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
357 struct libusb_device_handle *udh;
359 udh = usrp_open_cmd_interface (udev);
361 // FIXME this could be because somebody else has it open.
362 // We should delay and retry...
363 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
371 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx)
373 struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
377 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
378 usrp_close_interface (udh);
382 case ULS_ALREADY_LOADED: // nothing changed...
383 return ULS_ALREADY_LOADED;
387 // we loaded firmware successfully.
389 // It's highly likely that the board will renumerate (simulate a
390 // disconnect/reconnect sequence), invalidating our current
393 // FIXME. Turn this into a loop that rescans until we refind ourselves
395 struct timespec t; // delay for 1 second
403 case ULS_ERROR: // some kind of problem
409 usrp_load_standard_bits (int nth, bool force,
410 const std::string fpga_filename,
411 const std::string firmware_filename,
414 usrp_load_status_t s;
415 const char *filename;
416 const char *proto_filename;
419 assert (ctx != NULL);
421 // first, figure out what hardware rev we're dealing with
423 struct libusb_device *udev = usrp_find_device (nth, false, ctx);
425 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
428 hw_rev = usrp_hw_rev (udev);
431 // start by loading the firmware
433 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
434 default_firmware_filename);
435 filename = find_file(proto_filename, hw_rev);
437 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
440 s = usrp_load_firmware_nth (nth, filename, force, ctx);
441 load_status_msg (s, "firmware", filename);
446 // if we actually loaded firmware, we must reload fpga ...
450 // now move on to the fpga configuration bitstream
452 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
453 default_fpga_filename);
454 filename = find_file (proto_filename, hw_rev);
456 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
459 struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
463 s = usrp_load_fpga (udh, filename, force);
464 usrp_close_interface (udh);
465 load_status_msg (s, "fpga bitstream", filename);
474 power_down_9862s (struct libusb_device_handle *udh)
476 static const unsigned char regs[] = {
477 REG_RX_PWR_DN, 0x01, // everything
478 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
479 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
482 switch (usrp_hw_rev (libusb_get_device (udh))){
487 usrp_9862_write_many_all (udh, regs, sizeof (regs));
493 // ----------------------------------------------------------------
496 usrp_serial_number(struct libusb_device_handle *udh)
498 struct libusb_device_descriptor desc;
499 if (libusb_get_device_descriptor(libusb_get_device(udh), &desc) < 0)
500 fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
502 unsigned char iserial = desc.iSerialNumber;
506 unsigned char buf[1024];
507 if (libusb_get_string_descriptor_ascii(udh, iserial, buf, sizeof(buf)) < 0)