|
From: Xiaofan C. <xia...@gm...> - 2007-07-31 01:56:48
|
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 VERSION "0.1.0" #define VENDOR_ID 0x04D8 #define PRODUCT_ID 0x0080 #define INTERFACE 0 #define 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 |
|
From: Xiaofan C. <xia...@gm...> - 2007-07-31 14:22:12
|
On 7/30/07, Xiaofan Chen <xia...@gm...> wrote: > 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. > Part of the problem is the lack of documentation for these functions. People like Staphano in the Microchip forum thread did not know that libusb-win32 supports isoc transfer. It would be nice that the libusb-win32 website to be updated to reflect the changes. Xiaofan |