From: hackbard Date: Tue, 13 Feb 2007 00:40:18 +0000 (+0000) Subject: initial checkin of usb_bulk_test code X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b1768b107b2744dedac49b246fcec81e8d307af;p=my-code%2Ffpga.git initial checkin of usb_bulk_test code --- diff --git a/fx2/usb_bulk_test.c b/fx2/usb_bulk_test.c new file mode 100644 index 0000000..f68309a --- /dev/null +++ b/fx2/usb_bulk_test.c @@ -0,0 +1,145 @@ +/* + * usb_bulk_test.c - test fx2 bulktransfer + * + * usage: ./usb_bulk_test device iface alt_setting ep_out ep_in + * build: gcc -Wall -O3 usb_bulk_test.c -o usb_bulk_test + * + * author:hackbard@hackdaworld.org + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define BUFLEN (1024*10) +#define BULKMAX 256 +#define TIMEOUT 1 + + +int main(int argc,char **argv) { + + int usb_fd; + int random_fd; + int left; + int ret,rx,tmp; + unsigned char *tbuf; + struct usbdevfs_bulktransfer tbulk; + unsigned char *rbuf; + struct usbdevfs_bulktransfer rbulk; + struct usbdevfs_setinterface iface; + + + if(argc!=6) { + printf("usage: %s device iface alt_setting ep_out ep_in\n", + argv[0]); + return -1; + } + + tbuf=(unsigned char *)malloc(BUFLEN); + rbuf=(unsigned char *)malloc(BUFLEN); + if((rbuf==NULL)||(tbuf==NULL)) { + perror("malloc test buffer"); + return -1; + } + + usb_fd=open(argv[1],O_RDWR); + if(usb_fd<0) { + perror("open usb device"); + return usb_fd; + } + + random_fd=open("/dev/urandom",O_RDONLY); + if(random_fd<0) { + perror("open random device"); + return random_fd; + } + printf("\npoke random data into buffer ..."); + fflush(stdout); + left=BUFLEN; + while(left) + left-=read(random_fd,&(tbuf[BUFLEN-left]),left); + printf(" done\n"); + close(random_fd); + + iface.interface=atoi(argv[2]); + iface.altsetting=atoi(argv[3]); + + tbulk.ep=strtoul(argv[4],0,16); + tbulk.timeout=TIMEOUT; + tbulk.data=&tbuf; + + rbulk.ep=strtoul(argv[5],0,16); + rbulk.timeout=TIMEOUT; + rbulk.data=&rbuf; + + printf("\n"); + printf("summary:\n"); + printf("device %s\n",argv[1]); + printf("interface %d\n",iface.interface); + printf("alternate setting %d\n",iface.altsetting); + printf("bulk ep (out) %0x2\n",tbulk.ep); + printf("bulk ep (in) %0x2\n",rbulk.ep); + printf("\n"); + + printf("setting interface %d, alternate setting %d\n", + iface.interface, + iface.altsetting); + + ret=ioctl(usb_fd,USBDEVFS_SETINTERFACE,&iface); + if(ret<0) { + perror("set interface"); + return ret; + } + + left=BUFLEN; + while(left) { + tbulk.data=&(tbuf[BUFLEN-left]); + rbulk.data=&(rbuf[BUFLEN-left]); + tbulk.len=BULKMAX; + printf("transmit "); + fflush(stdout); + ret=ioctl(usb_fd,USBDEVFS_BULK,&tbulk); + if(ret<0) { + perror("transmitting data"); + return ret; + } + printf("done (%d) - receive ",ret); + fflush(stdout); + + rx=0; + while(ret) { + rbulk.len=ret; + rbulk.data=&(rbuf[BUFLEN-left+rx]); + tmp=ioctl(usb_fd,USBDEVFS_BULK,&rbulk); + if(tmp<0) { + perror("receiving data"); + return tmp; + } + printf("[%d] ",tmp); + rx+=tmp; + ret-=tmp; + } + printf("done (%d)",rx); + + if(!memcmp(&tbuf[BUFLEN-left],&rbuf[BUFLEN-left],rx)) + printf(" <--- equal!\n"); + else + printf(" <--- differ! :(\n"); + + left-=rx; + } + + close(usb_fd); + + return 0; +}