Sunteți pe pagina 1din 4

Understanding Isochronouos Transfer with Libusb-Win32

From: Xiaofan Chen <xiaofanc <at> gmail.com>


Subject: Understanding iIsochronous transfer with libusb-win32
Newsgroups: gmane.comp.lib.libusb.devel.windows
Date: 2007-07-31 01:56:46 GMT (7 years, 19 weeks, 5 days and 58 minutes ago)

I am in the process of writing a simple isochronous transfer firmware


for
Microchip PICkit 2 (PIC18F2550). I need a host application to test it.
http://forum.microchip.com/tm.aspx?m=270049&mpage=2
Based on some of the examples Stephan posted here, I come out the
following test program and it seems to work. However, I do not quite
understand the code since I do not quite understand isochronous
transfer.
Why we need three context? What is the benefit? Now three buffers
contain the same data.
I know the firmware is not that useful yet since it does not implement
double buffer.
<code>
#include <usb.h>
#define
#define
#define
#define
#define

VERSION "0.1.0"
VENDOR_ID 0x04D8
PRODUCT_ID 0x0080
INTERFACE 0
BUFFER_SIZE 65

usb_dev_handle *find_pickit2_isoc();
usb_dev_handle* setup_libusb_access() {
usb_dev_handle *pickit2_isoc;
usb_set_debug(255);
usb_init();
usb_find_busses();
usb_find_devices();
if(!(pickit2_isoc = find_pickit2_isoc())) {
printf("Couldn't find the mouse, Exiting\n");
return NULL;
}
if (usb_set_configuration(pickit2_isoc, 1) < 0) {
printf("Could not set configuration 1 : %s\n");
return NULL;
}
if (usb_claim_interface(pickit2_isoc, INTERFACE) < 0) {
printf("Could not claim interface: %s\n");
return NULL;
}
}

return pickit2_isoc;

usb_dev_handle *find_pickit2_isoc()

{
struct usb_bus *bus;
struct usb_device *dev;
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == VENDOR_ID &&
dev->descriptor.idProduct ==
PRODUCT_ID ) {
usb_dev_handle *handle;
printf("pickit2_isoc with Vendor Id: %x
and Product Id: %x
found.\n", VENDOR_ID, PRODUCT_ID);
if (!(handle = usb_open(dev))) {
printf("Could not open USB
device\n");
return NULL;
}
}

return handle;

}
}
return NULL;

void test_isochronous_async(usb_dev_handle *dev)


{
unsigned char buf0[640];
unsigned char buf1[640];
unsigned char buf2[640];
int i;
/* use three contexts for this example (more can be used) */
void *context0 = NULL;
void *context1 = NULL;
void *context2 = NULL;
/*

/* write transfer (stream) */


usb_isochronous_setup_async(dev, &context0, 0x01);
usb_isochronous_setup_async(dev, &context1, 0x01);
usb_isochronous_setup_async(dev, &context2, 0x01);
usb_submit_async(context0, buf0, sizeof(buf0));
usb_submit_async(context1, buf1, sizeof(buf1));
usb_submit_async(context2, buf2, sizeof(buf2));
for(i = 0; i < 10; i++)
{
usb_reap_async(context0, 5000);
usb_submit_async(context0, buf0, sizeof(buf0));
usb_reap_async(context1, 5000);
usb_submit_async(context1, buf1, sizeof(buf1));
usb_reap_async(context2, 5000);
usb_submit_async(context2, buf2, sizeof(buf2));

}
usb_reap_async(context0, 5000);
usb_reap_async(context1, 5000);
usb_reap_async(context2, 5000);
usb_free_async(&context0);
usb_free_async(&context1);
usb_free_async(&context2);
*/
/* read transfer (stream) */
usb_isochronous_setup_async(dev, &context0, 0x81,64);
usb_isochronous_setup_async(dev, &context1, 0x81,64);
usb_isochronous_setup_async(dev, &context2, 0x81,64);
usb_submit_async(context0, buf0, sizeof(buf0));
usb_submit_async(context1, buf1, sizeof(buf1));
usb_submit_async(context2, buf2, sizeof(buf2));
for(i = 0; i < 10; i++)
{
usb_reap_async(context0, 5000);
usb_submit_async(context0, buf0, sizeof(buf0));
usb_reap_async(context1, 5000);
usb_submit_async(context1, buf1, sizeof(buf1));
usb_reap_async(context2, 5000);
usb_submit_async(context2, buf2, sizeof(buf2));
}
for(i = 0; i < 640; i++) {
printf(" %02x, %02x, %02x ", buf0[i],buf1[i],buf2[i]);
//
printf(" %02x, %02x, %02x ", buf0[i]);
}
usb_reap_async(context0, 5000);
usb_reap_async(context1, 5000);
usb_reap_async(context2, 5000);
usb_free_async(&context0);
usb_free_async(&context1);
usb_free_async(&context2);

/* release interface */
usb_set_altinterface(dev, 0);
usb_release_interface(dev, 0);

int main(void)
{
usb_dev_handle *pickit2_isoc;
if ((pickit2_isoc = setup_libusb_access()) == NULL) {
exit(-1);
}
test_isochronous_async(pickit2_isoc);
usb_close(pickit2_isoc);
}

return 0;

<device info>
DLL version:
0.1.12.1
Driver version: 0.1.12.1
bus/device idVendor/idProduct
bus-0/\\.\libusb0-0001--0x04d8-0x0080
04D8/0080
- Manufacturer : Microchip Technology Inc.
- Product
: PICDEM FS USB Demo Board (C) 2004
wTotalLength:
32
bNumInterfaces:
1
bConfigurationValue: 1
iConfiguration:
0
bmAttributes:
80h
MaxPower:
50
bInterfaceNumber:
0
bAlternateSetting: 0
bNumEndpoints:
2
bInterfaceClass:
0
bInterfaceSubClass: 0
bInterfaceProtocol: 0
iInterface:
0
bEndpointAddress: 01h
bmAttributes:
0dh
wMaxPacketSize:
64
bInterval:
1
bRefresh:
0
bSynchAddress:
0
bEndpointAddress: 81h
bmAttributes:
0dh
wMaxPacketSize:
64
bInterval:
1
bRefresh:
0
bSynchAddress:
0
Regards,
Xiaofan
-----------------------------------------------------------------------This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a
browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/

Information taken from:


http://article.gmane.org/gmane.comp.lib.libusb.devel.windows/1439, on Monday
December 15, 2014.

S-ar putea să vă placă și