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"
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 static bool first = true;
75 usb_init (); // usb library init
82 usrp_one_time_init (bool new_context)
84 usrp_one_time_init ();
90 usrp_one_time_init (libusb_context **ctx)
92 static bool first = true;
96 usb_init (); // usb library init
109 // ----------------------------------------------------------------
112 * q must be a real USRP, not an FX2. Return its hardware rev number.
115 usrp_hw_rev (struct usb_device *q)
117 return q->descriptor.bcdDevice & 0x00FF;
121 * q must be a real USRP, not an FX2. Return true if it's configured.
124 _usrp_configured_p (struct usb_device *q)
126 return (q->descriptor.bcdDevice & 0xFF00) != 0;
130 usrp_usrp_p (struct usb_device *q)
132 return (q->descriptor.idVendor == USB_VID_FSF
133 && q->descriptor.idProduct == USB_PID_FSF_USRP);
137 usrp_fx2_p (struct usb_device *q)
139 return (q->descriptor.idVendor == USB_VID_CYPRESS
140 && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);
143 // ----------------------------------------------------------------
146 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
149 struct usb_device *q;
152 usrp_one_time_init ();
154 p = usb_get_busses();
158 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
159 if (n_found == nth) // return this one
161 n_found++; // keep looking
167 return 0; // not found
170 struct usb_dev_handle *
171 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
173 struct usb_dev_handle *udh = usb_open (dev);
177 if (dev != usb_device (udh)){
178 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
182 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
183 // There's no get get_configuration function, and with some of the newer kernels
184 // setting the configuration, even if to the same value, hoses any other processes
185 // that have it open. Hence we opt to not set it at all (We've only
186 // got a single configuration anyway). This may hose the win32 stuff...
188 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
189 if (usb_set_configuration (udh, 1) < 0){
193 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
194 * it returns -EBUSY if _any_ of the interfaces of a device are open.
195 * We've only got a single configuration, so setting it doesn't even seem
196 * like it should be required.
201 if (usb_claim_interface (udh, interface) < 0){
202 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
203 fprintf (stderr, "%s\n", usb_strerror());
208 if (usb_set_altinterface (udh, altinterface) < 0){
209 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
210 fprintf (stderr, "%s\n", usb_strerror());
211 usb_release_interface (udh, interface);
220 usrp_close_interface (struct usb_dev_handle *udh)
222 // we're assuming that closing an interface automatically releases it.
223 return usb_close (udh) == 0;
226 // ----------------------------------------------------------------
227 // write internal ram using Cypress vendor extension
230 write_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,
231 int start_addr, size_t len)
236 int quanta = MAX_EP0_PKTSIZE;
238 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
239 n = len + start_addr - addr;
243 a = usb_control_msg (udh, 0x40, 0xA0,
244 addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);
247 fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());
255 // ----------------------------------------------------------------
256 // write vendor extension command to USRP
259 write_cmd (struct usb_dev_handle *udh,
260 int request, int value, int index,
261 unsigned char *bytes, int len)
263 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
265 int r = usb_control_msg (udh, requesttype, request, value, index,
266 (char *) bytes, len, 1000);
268 // we get EPIPE if the firmware stalls the endpoint.
270 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
278 usrp_set_hash (struct usb_dev_handle *udh, int which,
279 const unsigned char hash[USRP_HASH_SIZE])
283 // we use the Cypress firmware down load command to jam it in.
284 int r = usb_control_msg (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
285 (char *) hash, USRP_HASH_SIZE, 1000);
286 return r == USRP_HASH_SIZE;
290 usrp_get_hash (struct usb_dev_handle *udh, int which,
291 unsigned char hash[USRP_HASH_SIZE])
295 // we use the Cypress firmware upload command to fetch it.
296 int r = usb_control_msg (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
297 (char *) hash, USRP_HASH_SIZE, 1000);
298 return r == USRP_HASH_SIZE;
302 usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value)
304 switch (usrp_hw_rev (usb_device (udh))){
305 case 0: // not supported ;)
309 return usrp1_fpga_write (udh, reg, value);
314 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
316 switch (usrp_hw_rev (usb_device (udh))){
317 case 0: // not supported ;)
321 return usrp1_fpga_read (udh, reg, value);
328 static usb_dev_handle *
329 open_nth_cmd_interface (int nth)
331 struct usb_device *udev = usrp_find_device (nth);
333 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
337 struct usb_dev_handle *udh;
339 udh = usrp_open_cmd_interface (udev);
341 // FIXME this could be because somebody else has it open.
342 // We should delay and retry...
343 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
353 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx){
354 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
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
381 usb_find_busses (); // rescan busses and devices
387 case ULS_ERROR: // some kind of problem
393 usrp_load_standard_bits (int nth, bool force,
394 const std::string fpga_filename,
395 const std::string firmware_filename,
398 usrp_load_status_t s;
399 const char *filename;
400 const char *proto_filename;
403 // first, figure out what hardware rev we're dealing with
405 struct usb_device *udev = usrp_find_device (nth);
407 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
410 hw_rev = usrp_hw_rev (udev);
413 // start by loading the firmware
415 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
416 default_firmware_filename);
417 filename = find_file(proto_filename, hw_rev);
419 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
423 s = usrp_load_firmware_nth (nth, filename, force);
424 load_status_msg (s, "firmware", filename);
429 // if we actually loaded firmware, we must reload fpga ...
433 // now move on to the fpga configuration bitstream
435 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
436 default_fpga_filename);
437 filename = find_file (proto_filename, hw_rev);
439 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
443 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
447 s = usrp_load_fpga (udh, filename, force);
448 usrp_close_interface (udh);
449 load_status_msg (s, "fpga bitstream", filename);
458 power_down_9862s (struct usb_dev_handle *udh)
460 static const unsigned char regs[] = {
461 REG_RX_PWR_DN, 0x01, // everything
462 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
463 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
466 switch (usrp_hw_rev (usb_device (udh))){
471 usrp_9862_write_many_all (udh, regs, sizeof (regs));
478 usrp_serial_number(struct usb_dev_handle *udh)
480 unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
485 if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)