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"
70 static bool first = true;
74 usb_init (); // usb library init
87 // ----------------------------------------------------------------
90 * q must be a real USRP, not an FX2. Return its hardware rev number.
93 usrp_hw_rev (struct usb_device *q)
95 return q->descriptor.bcdDevice & 0x00FF;
99 * q must be a real USRP, not an FX2. Return true if it's configured.
102 _usrp_configured_p (struct usb_device *q)
104 return (q->descriptor.bcdDevice & 0xFF00) != 0;
108 usrp_usrp_p (struct usb_device *q)
110 return (q->descriptor.idVendor == USB_VID_FSF
111 && q->descriptor.idProduct == USB_PID_FSF_USRP);
115 usrp_fx2_p (struct usb_device *q)
117 return (q->descriptor.idVendor == USB_VID_CYPRESS
118 && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);
121 // ----------------------------------------------------------------
124 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
127 struct usb_device *q;
130 usrp_one_time_init ();
132 p = usb_get_busses();
136 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
137 if (n_found == nth) // return this one
139 n_found++; // keep looking
145 return 0; // not found
148 struct usb_dev_handle *
149 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
151 struct usb_dev_handle *udh = usb_open (dev);
155 if (dev != usb_device (udh)){
156 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
160 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
161 // There's no get get_configuration function, and with some of the newer kernels
162 // setting the configuration, even if to the same value, hoses any other processes
163 // that have it open. Hence we opt to not set it at all (We've only
164 // got a single configuration anyway). This may hose the win32 stuff...
166 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
167 if (usb_set_configuration (udh, 1) < 0){
171 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
172 * it returns -EBUSY if _any_ of the interfaces of a device are open.
173 * We've only got a single configuration, so setting it doesn't even seem
174 * like it should be required.
179 if (usb_claim_interface (udh, interface) < 0){
180 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
181 fprintf (stderr, "%s\n", usb_strerror());
186 if (usb_set_altinterface (udh, altinterface) < 0){
187 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
188 fprintf (stderr, "%s\n", usb_strerror());
189 usb_release_interface (udh, interface);
198 usrp_close_interface (struct usb_dev_handle *udh)
200 // we're assuming that closing an interface automatically releases it.
201 return usb_close (udh) == 0;
204 // ----------------------------------------------------------------
205 // write internal ram using Cypress vendor extension
208 write_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,
209 int start_addr, size_t len)
214 int quanta = MAX_EP0_PKTSIZE;
216 for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
217 n = len + start_addr - addr;
221 a = usb_control_msg (udh, 0x40, 0xA0,
222 addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);
225 fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());
233 // ----------------------------------------------------------------
234 // write vendor extension command to USRP
237 write_cmd (struct usb_dev_handle *udh,
238 int request, int value, int index,
239 unsigned char *bytes, int len)
241 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
243 int r = usb_control_msg (udh, requesttype, request, value, index,
244 (char *) bytes, len, 1000);
246 // we get EPIPE if the firmware stalls the endpoint.
248 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
256 usrp_set_hash (struct usb_dev_handle *udh, int which,
257 const unsigned char hash[USRP_HASH_SIZE])
261 // we use the Cypress firmware down load command to jam it in.
262 int r = usb_control_msg (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
263 (char *) hash, USRP_HASH_SIZE, 1000);
264 return r == USRP_HASH_SIZE;
268 usrp_get_hash (struct usb_dev_handle *udh, int which,
269 unsigned char hash[USRP_HASH_SIZE])
273 // we use the Cypress firmware upload command to fetch it.
274 int r = usb_control_msg (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
275 (char *) hash, USRP_HASH_SIZE, 1000);
276 return r == USRP_HASH_SIZE;
280 usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value)
282 switch (usrp_hw_rev (usb_device (udh))){
283 case 0: // not supported ;)
287 return usrp1_fpga_write (udh, reg, value);
292 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
294 switch (usrp_hw_rev (usb_device (udh))){
295 case 0: // not supported ;)
299 return usrp1_fpga_read (udh, reg, value);
306 static usb_dev_handle *
307 open_nth_cmd_interface (int nth)
309 struct usb_device *udev = usrp_find_device (nth);
311 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
315 struct usb_dev_handle *udh;
317 udh = usrp_open_cmd_interface (udev);
319 // FIXME this could be because somebody else has it open.
320 // We should delay and retry...
321 fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
331 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx){
332 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
336 usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
337 usrp_close_interface (udh);
341 case ULS_ALREADY_LOADED: // nothing changed...
342 return ULS_ALREADY_LOADED;
346 // we loaded firmware successfully.
348 // It's highly likely that the board will renumerate (simulate a
349 // disconnect/reconnect sequence), invalidating our current
352 // FIXME. Turn this into a loop that rescans until we refind ourselves
354 struct timespec t; // delay for 1 second
359 usb_find_busses (); // rescan busses and devices
365 case ULS_ERROR: // some kind of problem
371 usrp_load_standard_bits (int nth, bool force,
372 const std::string fpga_filename,
373 const std::string firmware_filename,
376 usrp_load_status_t s;
377 const char *filename;
378 const char *proto_filename;
381 // first, figure out what hardware rev we're dealing with
383 struct usb_device *udev = usrp_find_device (nth);
385 fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
388 hw_rev = usrp_hw_rev (udev);
391 // start by loading the firmware
393 proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
394 default_firmware_filename);
395 filename = find_file(proto_filename, hw_rev);
397 fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
401 s = usrp_load_firmware_nth (nth, filename, force);
402 load_status_msg (s, "firmware", filename);
407 // if we actually loaded firmware, we must reload fpga ...
411 // now move on to the fpga configuration bitstream
413 proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
414 default_fpga_filename);
415 filename = find_file (proto_filename, hw_rev);
417 fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
421 struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
425 s = usrp_load_fpga (udh, filename, force);
426 usrp_close_interface (udh);
427 load_status_msg (s, "fpga bitstream", filename);
436 power_down_9862s (struct usb_dev_handle *udh)
438 static const unsigned char regs[] = {
439 REG_RX_PWR_DN, 0x01, // everything
440 REG_TX_PWR_DN, 0x0f, // pwr dn digital and analog_both
441 REG_TX_MODULATOR, 0x00 // coarse & fine modulators disabled
444 switch (usrp_hw_rev (usb_device (udh))){
449 usrp_9862_write_many_all (udh, regs, sizeof (regs));
456 usrp_serial_number(struct usb_dev_handle *udh)
458 unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
463 if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)