Created
May 3, 2014 00:45
-
-
Save YuuichiAkagawa/495c5034d5892b46f68c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//gcc -o test_ez_read test_ez_read.c -lusb | |
#include <string.h> | |
#include <errno.h> | |
#include <stdio.h> | |
#include <stdarg.h> | |
#include <usb.h> | |
//Define | |
#define USB_VENDOR 0x1f00 | |
#define USB_PRODUCT 0x2012 | |
#define TIMEOUT (5*1000) | |
#define EP2IN 0x82 | |
#define EP2OUT 0x02 | |
#define IF_CDCACM 0 | |
#define IF_CDCDATA 1 | |
#define ACM_CTRL_DTR 0x01 | |
#define ACM_CTRL_RTS 0x02 | |
/* - set line encoding: here 9600 8N1 | |
* 9600 = 0x2580 ~> 0x80, 0x25 in little endian | |
*/ | |
unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }; | |
/* Init USB */ | |
struct usb_bus *USB_init() | |
{ | |
usb_init(); | |
usb_find_busses(); | |
usb_find_devices(); | |
return(usb_get_busses()); | |
} | |
/* Find USB device */ | |
struct usb_device *USB_find(struct usb_bus *busses, struct usb_device *dev) | |
{ | |
struct usb_bus *bus; | |
for(bus=busses; bus; bus=bus->next){ | |
for(dev=bus->devices; dev; dev=dev->next) { | |
if( (dev->descriptor.idVendor==USB_VENDOR) && (dev->descriptor.idProduct==USB_PRODUCT) ){ | |
return( dev ); | |
} | |
} | |
} | |
return( NULL ); | |
} | |
/* USB Open */ | |
struct usb_dev_handle *USB_open(struct usb_device *dev) | |
{ | |
struct usb_dev_handle *udev = NULL; | |
int i, bNumIf; | |
udev=usb_open(dev); | |
if( (udev=usb_open(dev))==NULL ){ | |
fprintf(stderr,"usb_open Error.(%s)\n",usb_strerror()); | |
exit(1); | |
} | |
/* | |
if( usb_set_configuration(udev,dev->config->bConfigurationValue)<0 ){ | |
if( usb_detach_kernel_driver_np(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){ | |
fprintf(stderr,"usb_set_configuration Error.\n"); | |
fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror()); | |
} | |
} | |
*/ | |
bNumIf = dev->config->bNumInterfaces; | |
for(i=0; i<bNumIf; i++){ | |
if( usb_claim_interface(udev, i)<0 ){ | |
if( usb_detach_kernel_driver_np(udev, i)<0 ){ | |
fprintf(stderr,"usb_claim_interface Error.\n"); | |
fprintf(stderr,"usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror()); | |
} | |
} | |
if( usb_claim_interface(udev, i)<0 ){ | |
fprintf(stderr,"usb_claim_interface Error.(%s)\n",usb_strerror()); | |
} | |
} | |
return(udev); | |
} | |
/* USB Close */ | |
void USB_close(struct usb_dev_handle *dh) | |
{ | |
if(usb_release_interface(dh, 0)){ | |
fprintf(stderr,"usb_release_interface() failed. (%s)\n",usb_strerror()); | |
usb_close(dh); | |
} | |
if( usb_close(dh)<0 ){ | |
fprintf(stderr,"usb_close Error.(%s)\n",usb_strerror()); | |
} | |
} | |
/* USB altinterface */ | |
void USB_altinterface(struct usb_dev_handle *dh,int tyep) | |
{ | |
if(usb_set_altinterface(dh,tyep)<0) | |
{ | |
fprintf(stderr,"Failed to set altinterface %d: %s\n", 1,usb_strerror()); | |
USB_close(dh); | |
} | |
} | |
main() | |
{ | |
struct usb_bus *bus; | |
struct usb_device *dev; | |
usb_dev_handle *dh; | |
unsigned char buf[512]; | |
int ret; | |
/* Initialize */ | |
printf("starting!\n"); | |
bus=USB_init(); | |
dev=USB_find(bus,dev); | |
if( dev==NULL ){ | |
printf("device not found\n"); | |
exit(1); } | |
printf("Initialize OK\n"); | |
/*-------------*/ | |
/* Device Open */ | |
/*-------------*/ | |
dh=USB_open(dev); | |
if( dh==NULL ){ exit(2); } | |
printf("Device Open OK\n"); | |
/* | |
Put command here | |
*/ | |
ret = usb_control_msg(dh, 0x21, 0x22, ACM_CTRL_DTR | ACM_CTRL_RTS, IF_CDCACM, NULL, 0, 0); | |
ret = usb_control_msg(dh, 0x21, 0x20, 0, IF_CDCACM, encoding, sizeof(encoding), 0); | |
printf("Start Read data from EP2\n"); | |
// USB_altinterface(dh,1); | |
ret = usb_bulk_read(dh,EP2IN, buf, sizeof(buf), TIMEOUT); | |
if(ret<0){ | |
fprintf(stderr,"usb_bulk_read() failed? buf(%d) usb(%d)\n",sizeof(buf), ret); | |
fprintf(stderr,"usb_bulk_read error.(%s)\n",usb_strerror()); | |
USB_close(dh); | |
return 1; | |
} | |
printf("usb_bulk_read() finished\n"); | |
//fwrite(buf, 1, sizeof(buf), stdout); | |
fwrite(buf, 1, ret, stdout); | |
USB_close(dh); | |
printf("\nUSB End\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pi@raspberrypi:
/src/usbtest$ cat /etc/debian_version/src/usbtest$ uname -a7.0
pi@raspberrypi:
Linux raspberrypi 3.6.11+ #371 PREEMPT Thu Feb 7 16:31:35 GMT 2013 armv6l GNU/Linux
pi@raspberrypi:
/src/usbtest$ dpkg -l | grep libusb/src/usbtest$ gcc -o usbtest usbtest.c -lusbii libusb-0.1-4:armhf 2:0.1.12-20+nmu1 armhf userspace USB programming library
ii libusb-1.0-0:armhf 2:1.0.11-1 armhf userspace USB programming library
ii libusb-dev 2:0.1.12-20+nmu1 armhf userspace USB programming library development files
ii libusbmuxd1 1.0.7-2 armhf USB multiplexor daemon for iPhone and iPod Touch devices - library
pi@raspberrypi:
pi@raspberrypi:~/src/usbtest$ sudo ./usbtest
starting!
Initialize OK
Device Open OK
Start Read data from EP2
usb_bulk_read() finished
a
USB End