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;
54 * Forward Declarations
57 bool our_nanosleep (const struct timespec *delay);
58 const char *get_proto_filename (const std::string user_filename,
61 char *find_file (const char *filename, int hw_rev);
62 void load_status_msg (usrp_load_status_t s, const char *type,
63 const char *filename);
64 bool usrp1_fpga_write (libusb_device_handle *udh, int regno, int value);
65 bool usrp1_fpga_read (libusb_device_handle *udh, int regno, int *value);
66 bool usrp_set_switch (libusb_device_handle *udh, int cmd_byte, bool on);
68 bool usrp_set_fpga_reset (struct usb_dev_handle *udh, bool on);
69 bool usrp_set_fpga_tx_enable (struct usb_dev_handle *udh, bool on);
70 bool usrp_set_fpga_rx_enable (struct usb_dev_handle *udh, bool on);
71 bool usrp_set_fpga_tx_reset (struct usb_dev_handle *udh, bool on);
72 bool usrp_set_fpga_rx_reset (struct usb_dev_handle *udh, bool on);
76 static const int FIRMWARE_HASH_SLOT = 0;
77 static const int FPGA_HASH_SLOT = 1;
79 static const int hash_slot_addr[2] = {
80 USRP_HASH_SLOT_0_ADDR,
84 static const char *default_firmware_filename = "std.ihx";
85 static const char *default_fpga_filename = "std_2rxhb_2tx.rbf";
87 #include "std_paths.h"
93 static bool first = true;
97 usb_init (); // usb library init
110 // ----------------------------------------------------------------
113 * q must be a real USRP, not an FX2. Return its hardware rev number.
116 usrp_hw_rev (struct usb_device *q)
118 return q->descriptor.bcdDevice & 0x00FF;
122 * q must be a real USRP, not an FX2. Return true if it's configured.
125 _usrp_configured_p (struct usb_device *q)
127 return (q->descriptor.bcdDevice & 0xFF00) != 0;
131 usrp_usrp_p (struct usb_device *q)
133 return (q->descriptor.idVendor == USB_VID_FSF
134 && q->descriptor.idProduct == USB_PID_FSF_USRP);
138 usrp_fx2_p (struct usb_device *q)
140 return (q->descriptor.idVendor == USB_VID_CYPRESS
141 && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);
144 // ----------------------------------------------------------------
147 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
150 struct usb_device *q;
153 usrp_one_time_init ();
155 p = usb_get_busses();
159 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
160 if (n_found == nth) // return this one
162 n_found++; // keep looking
168 return 0; // not found
171 struct usb_dev_handle *
172 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
174 struct usb_dev_handle *udh = usb_open (dev);
178 if (dev != usb_device (udh)){
179 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
183 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
184 // There's no get get_configuration function, and with some of the newer kernels
185 // setting the configuration, even if to the same value, hoses any other processes
186 // that have it open. Hence we opt to not set it at all (We've only
187 // got a single configuration anyway). This may hose the win32 stuff...
189 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
190 if (usb_set_configuration (udh, 1) < 0){
194 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
195 * it returns -EBUSY if _any_ of the interfaces of a device are open.
196 * We've only got a single configuration, so setting it doesn't even seem
197 * like it should be required.
202 if (usb_claim_interface (udh, interface) < 0){
203 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
204 fprintf (stderr, "%s\n", usb_strerror());
209 if (usb_set_altinterface (udh, altinterface) < 0){
210 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
211 fprintf (stderr, "%s\n", usb_strerror());
212 usb_release_interface (udh, interface);
221 usrp_close_interface (struct usb_dev_handle *udh)
223 // we're assuming that closing an interface automatically releases it.
224 return usb_close (udh) == 0;
227 // ----------------------------------------------------------------
228 // write internal ram using Cypress vendor extension
231 write_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,
232 int start_addr, size_t len)
237 int quanta = MAX_EP0_PKTSIZE;
239 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
240 n = len + start_addr - addr;
244 a = usb_control_msg (udh, 0x40, 0xA0,
245 addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);
248 fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());
256 // ----------------------------------------------------------------
257 // write vendor extension command to USRP
260 write_cmd (struct usb_dev_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 = usb_control_msg (udh, requesttype, request, value, index,
267 (char *) bytes, len, 1000);
269 // we get EPIPE if the firmware stalls the endpoint.
271 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
279 usrp_set_hash (struct usb_dev_handle *udh, int which,
280 const unsigned char hash[USRP_HASH_SIZE])
284 // we use the Cypress firmware down load command to jam it in.
285 int r = usb_control_msg (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
286 (char *) hash, USRP_HASH_SIZE, 1000);
287 return r == USRP_HASH_SIZE;
291 usrp_get_hash (struct usb_dev_handle *udh, int which,
292 unsigned char hash[USRP_HASH_SIZE])
296 // we use the Cypress firmware upload command to fetch it.
297 int r = usb_control_msg (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
298 (char *) hash, USRP_HASH_SIZE, 1000);
299 return r == USRP_HASH_SIZE;
303 usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value)
305 switch (usrp_hw_rev (usb_device (udh))){
306 case 0: // not supported ;)
310 return usrp1_fpga_write (udh, reg, value);
315 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
317 switch (usrp_hw_rev (usb_device (udh))){
318 case 0: // not supported ;)
322 return usrp1_fpga_read (udh, reg, value);
329 static usb_dev_handle *
330 open_nth_cmd_interface (int nth)
332 struct usb_device *udev = usrp_find_device (nth);
334 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
338 struct usb_dev_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");
354 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx){
355 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
359 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
360 usrp_close_interface (udh);
364 case ULS_ALREADY_LOADED: // nothing changed...
365 return ULS_ALREADY_LOADED;
369 // we loaded firmware successfully.
371 // It's highly likely that the board will renumerate (simulate a
372 // disconnect/reconnect sequence), invalidating our current
375 // FIXME. Turn this into a loop that rescans until we refind ourselves
377 struct timespec t; // delay for 1 second
382 usb_find_busses (); // rescan busses and devices
388 case ULS_ERROR: // some kind of problem
394 usrp_load_standard_bits (int nth, bool force,
395 const std::string fpga_filename,
396 const std::string firmware_filename,
399 usrp_load_status_t s;
400 const char *filename;
401 const char *proto_filename;
404 // first, figure out what hardware rev we're dealing with
406 struct usb_device *udev = usrp_find_device (nth);
408 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
411 hw_rev = usrp_hw_rev (udev);
414 // start by loading the firmware
416 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
417 default_firmware_filename);
418 filename = find_file(proto_filename, hw_rev);
420 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
424 s = usrp_load_firmware_nth (nth, filename, force);
425 load_status_msg (s, "firmware", filename);
430 // if we actually loaded firmware, we must reload fpga ...
434 // now move on to the fpga configuration bitstream
436 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
437 default_fpga_filename);
438 filename = find_file (proto_filename, hw_rev);
440 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
444 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
448 s = usrp_load_fpga (udh, filename, force);
449 usrp_close_interface (udh);
450 load_status_msg (s, "fpga bitstream", filename);
459 power_down_9862s (struct usb_dev_handle *udh)
461 static const unsigned char regs[] = {
462 REG_RX_PWR_DN, 0x01, // everything
463 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
464 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
467 switch (usrp_hw_rev (usb_device (udh))){
472 usrp_9862_write_many_all (udh, regs, sizeof (regs));
479 usrp_serial_number(struct usb_dev_handle *udh)
481 unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
486 if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)