From 7a6f0bf04dd29333e63fb0824db3d0fa036f9584 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Mon, 11 Aug 2014 14:08:59 +0200 Subject: [PATCH] Reverting wrong commit --- drivers/gr-gn3s/include/CMakeLists.txt | 1 - drivers/gr-gn3s/include/fusb.h | 34 +- drivers/gr-gn3s/include/gn3s.h | 133 ++++- drivers/gr-gn3s/lib/CMakeLists.txt | 2 +- drivers/gr-gn3s/lib/gn3s.cc | 719 +++++++++++++++++-------- 5 files changed, 611 insertions(+), 278 deletions(-) diff --git a/drivers/gr-gn3s/include/CMakeLists.txt b/drivers/gr-gn3s/include/CMakeLists.txt index 9cd96f1dd..b682e8564 100644 --- a/drivers/gr-gn3s/include/CMakeLists.txt +++ b/drivers/gr-gn3s/include/CMakeLists.txt @@ -27,7 +27,6 @@ install(FILES gn3s_defines.h gn3s.h fusb.h - fx2.h libusb_types.h fusb_linux.h DESTINATION include/gn3s diff --git a/drivers/gr-gn3s/include/fusb.h b/drivers/gr-gn3s/include/fusb.h index 0f098de03..7112353a6 100644 --- a/drivers/gr-gn3s/include/fusb.h +++ b/drivers/gr-gn3s/include/fusb.h @@ -23,8 +23,9 @@ #ifndef _FUSB_H_ #define _FUSB_H_ +#include "libusb_types.h" -struct usb_dev_handle; +struct libusb_context; class fusb_ephandle; /*! @@ -33,15 +34,15 @@ class fusb_ephandle; class fusb_devhandle { private: // NOT IMPLEMENTED - fusb_devhandle (const fusb_devhandle &rhs); // no copy constructor + fusb_devhandle (const fusb_devhandle &rhs); // no copy constructor fusb_devhandle &operator= (const fusb_devhandle &rhs); // no assignment operator protected: - usb_dev_handle *d_udh; + libusb_device_handle *d_udh; public: // CREATORS - fusb_devhandle (usb_dev_handle *udh); + fusb_devhandle (libusb_device_handle *udh); virtual ~fusb_devhandle (); // MANIPULATORS @@ -50,10 +51,10 @@ public: * \brief return an ephandle of the correct subtype */ virtual fusb_ephandle *make_ephandle (int endpoint, bool input_p, - int block_size = 0, int nblocks = 0) = 0; + int block_size = 0, int nblocks = 0) = 0; // ACCESSORS - usb_dev_handle *get_usb_dev_handle () const { return d_udh; } + libusb_device_handle *get_usb_dev_handle () const { return d_udh; } }; @@ -63,23 +64,23 @@ public: class fusb_ephandle { private: // NOT IMPLEMENTED - fusb_ephandle (const fusb_ephandle &rhs); // no copy constructor + fusb_ephandle (const fusb_ephandle &rhs); // no copy constructor fusb_ephandle &operator= (const fusb_ephandle &rhs); // no assignment operator protected: - int d_endpoint; - bool d_input_p; - int d_block_size; - int d_nblocks; - bool d_started; + int d_endpoint; + bool d_input_p; + int d_block_size; + int d_nblocks; + bool d_started; public: fusb_ephandle (int endpoint, bool input_p, - int block_size = 0, int nblocks = 0); + int block_size = 0, int nblocks = 0); virtual ~fusb_ephandle (); - virtual bool start () = 0; //!< begin streaming i/o - virtual bool stop () = 0; //!< stop streaming i/o + virtual bool start () = 0; //!< begin streaming i/o + virtual bool stop () = 0; //!< stop streaming i/o /*! * \returns \p nbytes if write was successfully enqueued, else -1. @@ -114,7 +115,8 @@ public: /*! * \brief returns fusb_devhandle or throws if trouble */ - static fusb_devhandle *make_devhandle (usb_dev_handle *udh); + static fusb_devhandle *make_devhandle (libusb_device_handle *udh, + libusb_context *ctx = 0); /*! * \brief Returns max block size in bytes (hard limit). diff --git a/drivers/gr-gn3s/include/gn3s.h b/drivers/gr-gn3s/include/gn3s.h index da9de8bc9..b705f2844 100644 --- a/drivers/gr-gn3s/include/gn3s.h +++ b/drivers/gr-gn3s/include/gn3s.h @@ -30,30 +30,113 @@ #define GN3S_H_ -#define MODE_NARROW_16_I 32 /* SF:16.368MHz, IF:4.092MHz, 2bit, I */ -#define MODE_NARROW_8_IQ 36 /* SF: 8.184MHz, IF: 0MHz, 4bit, IQ */ -#define MODE_NARROW_5_I 38 /* SF: 5.456MHz, IF:4.092MHz, 2bit, I */ -#define MODE_NARROW_4_IQ 42 /* SF: 4.092MHz, IF: 0MHz, 4bit, IQ */ -#define MODE_WIDE_16_I 132 /* SF:16.368MHz, IF:4.092MHz, 2bit, I */ -#define MODE_WIDE_8_IQ 136 /* SF: 8.184MHz, IF: 0MHz, 4bit, IQ */ -#define MODE_WIDE_5_I 138 /* SF: 5.456MHz, IF:4.092MHz, 2bit, I */ -#define MODE_WIDE_4_IQ 142 /* SF: 4.092MHz, IF: 0MHz, 4bit, IQ */ - -#define GN3S_MODE MODE_NARROW_16_I /* GN3S operating mode */ -#define GN3S_DECIMATE 1 /* decimate mode */ -#define GN3S_PACK 1 /* pack mode */ -#define GN3S_BUFFSIZE 32*512 /* buffer size: 16 kB */ - -/* global functions */ -extern int gn3s_init(void); -extern void gn3s_quit(void); -extern void gn3s_exp_v2(unsigned char *buf, int n, char *expbuf); -extern void gn3s_getbuff_v2(uint64_t buffloc, int n, int dtype, char *expbuf); -extern void gn3s_exp_v3(unsigned char *buf, int n, int i_mode, char *expbuf); -extern void gn3s_getbuff_v3(uint64_t buffloc, int n, int dtype, char *expbuf); -extern int gn3s_pushtomembuf(void); -extern void fgn3s_pushtomembuf(void); -extern void fgn3s_getbuff(uint64_t buffloc, int n, int dtype, char *expbuf); +/* Includes */ +/*--------------------------------------------------------------*/ +#include +#include +#include +#include +#include +#include "fusb.h" +#include "fusb_linux.h" +//#include "usrp_bytesex.h" +//#include "usrp_prims.h" +/*--------------------------------------------------------------*/ -#endif +/* FX2 Configuration Structure */ +/*--------------------------------------------------------------*/ +struct fx2Config +{ + int interface; + int altinterface; + usb_dev_handle *udev; + fusb_ephandle *d_ephandle; + fusb_devhandle *d_devhandle; +}; +/*--------------------------------------------------------------*/ + + +/* FX2 Stuff */ +/*--------------------------------------------------------------*/ +#define RX_ENDPOINT (0x86) +#define VRT_VENDOR_IN (0xC0) +#define VRT_VENDOR_OUT (0x40) +#define RX_INTERFACE (2) +#define RX_ALTINTERFACE (0) +#define VRQ_GET_STATUS (0x80) +#define GS_RX_OVERRUN (1) //!< Returns 1 byte +#define VRQ_XFER (0x01) +/*--------------------------------------------------------------*/ + + +/* GN3S Stuff */ +/*--------------------------------------------------------------*/ +#define GN3S_VID (0x16C0) +#define GN3S_PID (0x072F) +#define VID_OLD (0x1781) +#define PID_OLD (0x0B39) +#define PROG_SET_CMD (0xE600) +#define FUSB_BUFFER_SIZE (16 * (1L << 20)) //!< 8 MB +#define FUSB_BLOCK_SIZE (16 * (1L << 10)) //!< 16KB is hard limit +#define FUSB_NBLOCKS (FUSB_BUFFER_SIZE / FUSB_BLOCK_SIZE) +/*--------------------------------------------------------------*/ + + +/* The firmware is embedded into the executable */ +/*--------------------------------------------------------------*/ +extern char _binary_usrp_gn3s_firmware_ihx_start[]; +/*--------------------------------------------------------------*/ + + +/*--------------------------------------------------------------*/ +/*! \ingroup CLASSES + * + */ +class gn3s +{ + + private: + + /* First or second board */ + int which; + + /* GN3S FX2 Stuff */ + struct fx2Config fx2_config; + struct usb_device *fx2_device; + struct usb_dev_handle *fx2_handle; + + /* USB IDs */ + unsigned int gn3s_vid, gn3s_pid; + + /* Pull in the binary firmware */ + int fstart; + int fsize; + //char *gn3s_firmware; + + public: + + gn3s(int _which); //!< Constructor + ~gn3s(); //!< Destructor + + /* FX2 functions */ + struct usb_device* usb_fx2_find(int vid, int pid, char info, int ignore); + bool usb_fx2_configure(struct usb_device *fx2, fx2Config *fx2c); + fusb_devhandle* make_devhandle (usb_dev_handle *udh); + int read(void *buff, int bytes); + int write_cmd(int request, int value, int index, unsigned char *bytes, int len); + bool _get_status(int which, bool *trouble); + bool check_rx_overrun(); + bool usrp_xfer(char VRQ_TYPE, bool start); + + /* Used to flash the GN3S */ + int atoz(char *s); + void upload_ram(char *buf, int start, int len); + void program_fx2(char *filename, char mem); + int prog_gn3s_board(); + +}; +/*--------------------------------------------------------------*/ + + +#endif /*GN3S_H_ */ diff --git a/drivers/gr-gn3s/lib/CMakeLists.txt b/drivers/gr-gn3s/lib/CMakeLists.txt index 25fe6ecc4..065736099 100644 --- a/drivers/gr-gn3s/lib/CMakeLists.txt +++ b/drivers/gr-gn3s/lib/CMakeLists.txt @@ -22,7 +22,7 @@ ######################################################################## include(GrPlatform) #define LIB_SUFFIX -add_library(gr-gn3s SHARED gn3s_source_cc.cc gn3s_source.cc gn3s.cc fusb.cc fx2.cc fusb_linux.cc) +add_library(gr-gn3s SHARED gn3s_source_cc.cc gn3s_source.cc gn3s.cc fusb.cc fusb_linux.cc) target_link_libraries(gr-gn3s ${Boost_LIBRARIES} ${GNURADIO_RUNTIME_LIBRARIES} "usb" ${LIBUSB_LIBRARIES}) set_target_properties(gr-gn3s PROPERTIES DEFINE_SYMBOL "gr_gn3s_EXPORTS") diff --git a/drivers/gr-gn3s/lib/gn3s.cc b/drivers/gr-gn3s/lib/gn3s.cc index 15cd0cf3c..372237b79 100644 --- a/drivers/gr-gn3s/lib/gn3s.cc +++ b/drivers/gr-gn3s/lib/gn3s.cc @@ -25,276 +25,525 @@ */ /*----------------------------------------------------------------------------------------------*/ -#include "sdr.h" -#include "fx2.h" +#include "gn3s.h" +#include +#include +#include +#include +#include "fusb_linux.h" -/* global variables ----------------------------------------------------------*/ -using namespace std; -Fx2_dev fx2_d; +static char debug = 1; //!< 1 = Verbose -/* GN3S initialization --------------------------------------------------------- -* search front end and initialization -* args : none -* return : int status 0:okay -1:failure -*-----------------------------------------------------------------------------*/ -extern int gn3s_init(void) +/*----------------------------------------------------------------------------------------------*/ +gn3s::gn3s(int _which) { - char version; /* version of modules */ - unsigned char uc_flags[5]; - /* Look for a device */ - fx2_d = Fx2_dev(1); /* PID 0x0b39 */ - if (fx2_d.usb_fx2_find() != 0) { - version = 2; - if (sdrini.fend!=FEND_GN3SV2) { - SDRPRINTF("error: wrong frontend type, GN3SV2 is found\n"); - return -1; - } - } else { - fx2_d = Fx2_dev(2); /* PID 0x0b3a */ - if (fx2_d.usb_fx2_find() != 0) { - version = 3; - if (sdrini.fend!=FEND_GN3SV3) { - SDRPRINTF("error: wrong frontend type, GN3SV3 is found\n"); - return -1; - } - } else { - fx2_d = Fx2_dev(3); /* PID 0x0b3f */ - if(fx2_d.usb_fx2_find()!=0) { - version = 3; - if (sdrini.fend!=FEND_GN3SV3) { - SDRPRINTF("error: wrong frontend type, GN3SV3 is found\n"); - return -1; - } - } else { - SDRPRINTF("error: no GN3S frontend found\n"); - return -1; - } - } - } - fx2_d.usb_fx2_init(); - /* version 2 */ - if (version==2) { - fx2_d.usrp_xfer(VRQ_XFER, 1); - } - /* version 3 */ - if (version==3) { - fx2_d.usrp_xfer (VRQ_AGC,0); /* AGC OFF */ - fx2_d.usrp_xfer (VRQ_CMODE,132 ); - fx2_d.usrp_xfer (VRQ_XFER, 0 ); - fx2_d.usrp_xfer (VRQ_XFER, 1); - fx2_d.usrp_xfer2(VRQ_FLAGS, 0, uc_flags, 5); - fx2_d.usrp_xfer (VRQ_XFER, 0 ); - fx2_d.usrp_xfer(VRQ_CMODE,GN3S_MODE); - fx2_d.usrp_xfer(VRQ_XFER, 1); - } - return 0; + //int fsize; + bool ret; + which = _which; + + fx2_device = NULL; + fx2_handle = NULL; + gn3s_vid = GN3S_VID; + gn3s_pid = GN3S_PID; + + /* Get the firmware embedded in the executable */ + //fstart = (int) &_binary_usrp_gn3s_firmware_ihx_start; + //fsize = strlen(_binary_usrp_gn3s_firmware_ihx_start); + //gn3s_firmware = new char[fsize + 10]; + //memcpy(&gn3s_firmware[0], (void *)fstart, fsize); + + // Load the firmware from external file (Javier) + + + //gn3s_firmware[fsize] = NULL; + + /* Search all USB busses for the device specified by VID/PID */ + fx2_device = usb_fx2_find(gn3s_vid, gn3s_pid, debug, 0); + if (!fx2_device) + { + /* Program the board */ + ret = prog_gn3s_board(); + if(ret) + { + fprintf(stdout, "Could not flash GN3S device\n"); + throw(1); + } + + /* Need to wait to catch change */ + sleep(2); + + /* Search all USB busses for the device specified by VID/PID */ + fx2_device = usb_fx2_find(gn3s_vid, gn3s_pid, debug, 0); + } + else + { + fprintf(stdout, "Found GN3S Device\n"); + } + + /* Open and configure FX2 device if found... */ + ret = usb_fx2_configure(fx2_device, &fx2_config); + if(ret) + { + fprintf(stdout, "Could not obtain a handle to the GN3S device\n"); + throw(1); + + } + } -/* stop front-end -------------------------------------------------------------- -* stop grabber of front end -* args : none -* return : none -*-----------------------------------------------------------------------------*/ -extern void gn3s_quit(void) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +gn3s::~gn3s() { - fx2_d.close(); - fx2_d.~Fx2_dev(); + + usrp_xfer(VRQ_XFER, 0); + + //delete gn3s_firmware; + delete fx2_config.d_ephandle; + delete fx2_config.d_devhandle; + + usb_release_interface(fx2_config.udev, fx2_config.interface); + usb_close(fx2_config.udev); + } -/* data expansion to binary (GN3S v2) ------------------------------------------ -* get current data buffer from memory buffer -* args : char *buf I memory buffer -* int n I number of grab data -* char *expbuff O extracted data buffer -* return : none -*-----------------------------------------------------------------------------*/ -extern void gn3s_exp_v2(unsigned char *buf, int n, char *expbuf) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +int gn3s::prog_gn3s_board() { - int i; - char LUT_1bit[2]={1,-1}; - char shift=0, endshift=0; - /* data shift (refer gn3s-full.cc) */ - /* http://ccar.colorado.edu/gnss/files/GN3Sv2.7z */ - if ((buf[ 0]&0x02)!=2) shift=1; - if ((buf[n-1]&0x02)!=0) endshift=1; + char a; + struct usb_bus *bus; + struct usb_device *dev; + //struct usb_dev_handle *han; + int vid, pid; - for (i=0;inext) + { + for(dev = bus->devices; dev; dev = dev->next) + { + if((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid)) + { + fx2_device = dev; + fprintf(stdout,"GN3S Device Found... awaiting firmware flash \n"); + break; + } + } + } + + if(fx2_device == NULL) + { + fprintf(stderr,"Cannot find vid 0x%x pid 0x%x \n", vid, pid); + return -1; + } + + printf("Using device vendor id 0x%04x product id 0x%04x\n", + fx2_device->descriptor.idVendor, fx2_device->descriptor.idProduct); + + fx2_handle = usb_open(fx2_device); + + /* Do the first set 0xE600 1 */ + char c[] = "1"; + char d[] = "0"; + + a = atoz(c); + + fprintf(stdout,"GN3S flashing ... \n"); + + upload_ram(&a, (PROG_SET_CMD),1); + + program_fx2(NULL, 1); + + a = atoz(d); + + upload_ram(&a, (PROG_SET_CMD),1); + + fprintf(stdout,"GN3S flash complete! \n"); + + usb_close(fx2_handle); + + return(0); } -/* get current data buffer (GN3S v2) ------------------------------------------- -* get current data buffer from memory buffer -* args : uint64_t buffloc I buffer location -* int n I number of grab data -* int dtype I data type (DTYPEI or DTYPEIQ) -* char *expbuff O extracted data buffer -* return : none -*-----------------------------------------------------------------------------*/ -extern void gn3s_getbuff_v2(uint64_t buffloc, int n, int dtype, char *expbuf) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +int gn3s::atoz(char *s) { - uint64_t membuffloc=2*buffloc%(MEMBUFFLEN*GN3S_BUFFSIZE); - int nout; - n=2*n; - nout=(int)((membuffloc+n)-(MEMBUFFLEN*GN3S_BUFFSIZE)); - - mlock(hbuffmtx); - if (nout>0) { - gn3s_exp_v2(&sdrstat.buff[membuffloc],n-nout,expbuf); - gn3s_exp_v2(&sdrstat.buff[0],nout,&expbuf[n-nout]); - } else { - gn3s_exp_v2(&sdrstat.buff[membuffloc],n,expbuf); + int a; + if(!strncasecmp("0x", s, 2)){ + sscanf(s, "%x", &a); + return a; } - unmlock(hbuffmtx); + return atoi(s); } -/* data expansion to binary (GN3S v3) ------------------------------------------ -* get current data buffer from memory buffer -* args : char *buf I memory buffer -* int n I number of grab data -* int i_mode I GN3S data grabber mode -* char *expbuff O extracted data buffer -* return : none -*-----------------------------------------------------------------------------*/ -extern void gn3s_exp_v3(unsigned char *buf, int n, int i_mode, char *expbuf) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +void gn3s::upload_ram(char *buf, int start, int len) { - int i=0; - int i_idx=0; - int i_dtype=0; - char LUT_2bit[4]={1,-1,3,-3}; - char LUT_I_4bit[16]={1,-1,0,0,3,-3,0,0,0,0,0,0,0,0,0,0}; - char LUT_Q_4bit[16]={1,0,-1,0,0,0,0,0,3,0,-3,0,0,0,0,0}; - char MASK_2bit=0x03; - char MASK_4bit_I=0x05; - char MASK_4bit_Q=0x0A; + int i; + int tlen; + int quanta = 16; + int a; - /* determine mode */ - if (((i_mode% 100)==32)|| ((i_mode% 100)==38)) { - i_dtype =2; - } - if (((i_mode% 100)==36)|| ((i_mode% 100)==42)) { - i_dtype =4; - } + for (i = start; i < start + len; i += quanta) { + tlen = len + start - i; - for (i=0;i quanta) + tlen = quanta; + + if (debug >= 3) + printf("i = %d, tlen = %d \n", i, tlen); + a = usb_control_msg(fx2_handle, 0x40, 0xa0, i, 0, + buf + (i - start), tlen, 1000); + + if (a < 0) { + fprintf(stderr, "Request to upload ram contents failed: %s\n", + usb_strerror()); + return; + } + } } -/* get current data buffer (GN3S v3) ------------------------------------------- -* get current data buffer from memory buffer -* args : uint64_t buffloc I buffer location -* int n I number of grab data -* int dtype I data type (DTYPEI or DTYPEIQ) -* char *expbuff O extracted data buffer -* return : none -*-----------------------------------------------------------------------------*/ -extern void gn3s_getbuff_v3(uint64_t buffloc, int n, int dtype, char *expbuf) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +void gn3s::program_fx2(char *filename, char mem) { - uint64_t membuffloc=buffloc%(MEMBUFFLEN*GN3S_BUFFSIZE); - int nout=(int)((membuffloc+n)-(MEMBUFFLEN*GN3S_BUFFSIZE)); + FILE *f; + char s[1024]; + char data[256]; + char checksum, a; + int length, addr, type, i; + unsigned int b; - mlock(hbuffmtx); - if (nout>0) { - gn3s_exp_v3(&sdrstat.buff[membuffloc],n-nout,GN3S_MODE,expbuf); - gn3s_exp_v3(&sdrstat.buff[0],nout,GN3S_MODE,&expbuf[dtype*(n-nout)]); - } else { - gn3s_exp_v3(&sdrstat.buff[membuffloc],n,GN3S_MODE,expbuf); - } - unmlock(hbuffmtx); + // *** mod javier: load firmware from external file *** + + //f = tmpfile(); + + /* Dump firmware into temp file */ + //fputs(gn3s_firmware, f); + //rewind(f); + + f = fopen ("gn3s_firmware.ihx","r"); + if (f!=NULL) + { + printf("GN3S firmware file found!\n"); + }else{ + printf("Could not open GN3S firmware file!\n"); + return; + } + + while (!feof(f)) { + fgets(s, 1024, f); /* we should not use more than 263 bytes normally */ + + if (s[0] != ':') { + fprintf(stderr, "%s: invalid string: \"%s\"\n", filename, s); + continue; + } + + sscanf(s + 1, "%02x", &length); + sscanf(s + 3, "%04x", &addr); + sscanf(s + 7, "%02x", &type); + + if (type == 0) { + // printf("Programming %3d byte%s starting at 0x%04x", + // length, length==1?" ":"s", addr); + a = length + (addr & 0xff) + (addr >> 8) + type; + + for (i = 0; i < length; i++) { + sscanf(s + 9 + i * 2, "%02x", &b); + data[i] = b; + a = a + data[i]; + } + + sscanf(s + 9 + length * 2, "%02x", &b); + checksum = b; + + if (((a + checksum) & 0xff) != 0x00) { + printf(" ** Checksum failed: got 0x%02x versus 0x%02x\n", (-a) + & 0xff, checksum); + continue; + } else { + //printf(", checksum ok\n"); + } + + upload_ram(data, addr, length); + + } else { + if (type == 0x01) { + printf("End of file\n"); + fclose(f); + + return; + } else { + if (type == 0x02) { + printf("Extended address: whatever I do with it ?\n"); + continue; + } + } + } + } + + fclose(f); } -/* push data to memory buffer -------------------------------------------------- -* push data to memory buffer from GN3S front end -* args : none -* return : none -*-----------------------------------------------------------------------------*/ -extern int gn3s_pushtomembuf(void) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +struct usb_device* gn3s::usb_fx2_find(int vid, int pid, char info, int ignore) { - bool b_overrun=false; - int nbuff; + struct usb_bus *bus; + struct usb_device *dev; + struct usb_device *fx2 = NULL; + usb_dev_handle *udev; + int count = 0; + int ret; + char str[256]; - /* check buffer overrun */ - fx2_d.check_rx_overrun(&b_overrun); - if (b_overrun) return -1; + usb_init(); + usb_find_busses(); + usb_find_devices(); - mlock(hbuffmtx); - nbuff=fx2_d.read_IF( - &sdrstat.buff[(sdrstat.buffcnt%MEMBUFFLEN)*GN3S_BUFFSIZE]); - unmlock(hbuffmtx); + for(bus = usb_busses; bus; bus = bus->next) + { + for(dev = bus->devices; dev; dev = dev->next) + { + if((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid)) + { + fx2 = dev; + } - if (nbuff!=GN3S_BUFFSIZE) { - SDRPRINTF("GN3S read IF error...\n"); - } + if(fx2 != NULL && info) + { + udev = usb_open(fx2); + if(udev && dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid && count < ignore) + { + if(fx2->descriptor.iManufacturer) + { + ret = usb_get_string_simple(udev, fx2->descriptor.iManufacturer, str, sizeof(str)); - mlock(hreadmtx); - sdrstat.buffcnt++; - unmlock(hreadmtx); + if(ret > 0) + printf("- Manufacturer : %s\n", str); + else + printf("- Unable to fetch manufacturer string\n"); + } + if(fx2->descriptor.iProduct) + { + ret = usb_get_string_simple(udev, fx2->descriptor.iProduct, str, sizeof(str)); - return 0; + if(ret > 0) + printf("- Product : %s\n", str); + else + printf("- Unable to fetch product string\n"); + } + + if(fx2->descriptor.iSerialNumber) + { + ret = usb_get_string_simple(udev, fx2->descriptor.iSerialNumber, str, sizeof(str)); + + if(ret > 0) + printf("- Serial Number: %s\n", str); + else + printf("- Unable to fetch serial number string\n"); + } + + usb_close (udev); + return fx2; + } + else if(udev && dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid && count >= ignore) + { + count++; + } + + if(!fx2->config) + { + printf(" Could not retrieve descriptors\n"); + continue; + } + + for(int i = 0; i < fx2->descriptor.bNumConfigurations; i++) + { + //print_configuration(&fx2->config[i]); + } + } + } + } + + return fx2; } -/* push data to memory buffer -------------------------------------------------- -* post-processing function: push data to memory buffer from GN3S binary IF file -* args : none -* return : none -*-----------------------------------------------------------------------------*/ -extern void fgn3s_pushtomembuf(void) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +bool gn3s::usb_fx2_configure(struct usb_device *fx2, fx2Config *fx2c) { - size_t nread; - mlock(hbuffmtx); + char status = 0; + int interface = RX_INTERFACE; + int altinterface = RX_ALTINTERFACE; + usb_dev_handle *udev; + fusb_ephandle *d_ephandle; + fusb_devhandle *d_devhandle; - nread=fread(&sdrstat.buff[(sdrstat.buffcnt%MEMBUFFLEN)*GN3S_BUFFSIZE], - 1,GN3S_BUFFSIZE,sdrini.fp1); + udev = usb_open(fx2); - unmlock(hbuffmtx); + if(!udev) + { + fprintf(stdout, "Could not obtain a handle to GNSS Front-End device \n"); + return -1; + } + else + { + if(debug) + printf("Received handle for GNSS Front-End device \n"); - if (nreadmake_ephandle(RX_ENDPOINT, true, FUSB_BLOCK_SIZE, FUSB_NBLOCKS); + + if(!d_ephandle->start()) + { + fprintf (stdout, "usrp0_rx: failed to start end point streaming"); + usb_strerror (); + status = -1; + } + + if(status == 0) + { + fx2c->interface = interface; + fx2c->altinterface = altinterface; + fx2c->udev = udev; + fx2c->d_devhandle = d_devhandle; + fx2c->d_ephandle = d_ephandle; + return 0; + } + else + { + return -1; + } + } } -/* get current data buffer from IF file ---------------------------------------- -* post-processing function: get current data buffer from memory buffer -* args : uint64_t buffloc I buffer location -* int n I number of grab data -* int dtype I data type (DTYPEI or DTYPEIQ) -* char *expbuff O extracted data buffer -* return : none -*-----------------------------------------------------------------------------*/ -extern void fgn3s_getbuff(uint64_t buffloc, int n, int dtype, char *expbuf) +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +fusb_devhandle* gn3s::make_devhandle(usb_dev_handle *udh) { - uint64_t membuffloc=dtype*buffloc%(MEMBUFFLEN*dtype*GN3S_BUFFSIZE); - int nout; - - n=dtype*n; - nout=(int)((membuffloc+n)-(MEMBUFFLEN*dtype*GN3S_BUFFSIZE)); - - mlock(hbuffmtx); - if (nout>0) { - memcpy(expbuf,&sdrstat.buff[membuffloc],n-nout); - memcpy(&expbuf[(n-nout)],&sdrstat.buff[0],nout); - } else { - memcpy(expbuf,&sdrstat.buff[membuffloc],n); - } - unmlock(hbuffmtx); + return new fusb_devhandle_linux(udh); } +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +int gn3s::read(void *buff, int bytes) +{ + return(fx2_config.d_ephandle->read(buff, bytes)); +} +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +bool gn3s::check_rx_overrun() +{ + bool overrun; + + _get_status(GS_RX_OVERRUN, &overrun); + + return(overrun); +} +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +bool gn3s::_get_status(int command, bool *trouble) +{ + unsigned char status; + + if(write_cmd(VRQ_GET_STATUS, 0, command, &status, sizeof(status)) != sizeof (status)) + return false; + + *trouble = status; + return true; +} +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +bool gn3s::usrp_xfer(char VRQ_TYPE, bool start) +{ + int r; + + r = write_cmd(VRQ_TYPE, start, 0, 0, 0); + + return(r == 0); +} +/*----------------------------------------------------------------------------------------------*/ + + +/*----------------------------------------------------------------------------------------------*/ +int gn3s::write_cmd(int request, int value, int index, unsigned char *bytes, int len) +{ + int requesttype; + int r; + + requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT; + r = usb_control_msg (fx2_config.udev, requesttype, request, value, index, (char *) bytes, len, 1000); + if(r < 0) + { + /* We get EPIPE if the firmware stalls the endpoint. */ + if(errno != EPIPE) + fprintf (stdout, "usb_control_msg failed: %s\n", usb_strerror()); + } + return r; +} +/*----------------------------------------------------------------------------------------------*/