|
From: Michael M. <qm...@cl...> - 2009-10-29 11:57:14
|
"Michael Plante" <mic...@gm...> wrote: > > Feng wrote: > >> 1) What is procedures of reading data, especially, what do the > >> functions "usb_reap_async" and "usb_submit_async" do? > > Based on the question, this is someone else's code, not yours? If I > understand correctly, "submit" tells the OS to schedule the transfer, and > "reap" cleans up (which involves getting the requested data back on a read). > Evidently, usb_reap_async() can block for USB_TIMEOUT (a number you didn't > include) in your case. Take this with a grain of salt, as I've only used > synchronous interfaces, and am figuring what I can from your code and the > libusb source. If you want to see a simpler example, you might read the > source to _usb_transfer_sync(). It uses these functions. Hi! I've been lurking for some months and felt that a hello was long overdue. And also, I would like to say a big thank you to the developers of this important library :-) As for the asynchronous library functions mentioned above... I posted a little blog entry that some might find interesting... I found some clear-to-understand code developed by Kevin Kofler as part of the TI Calc project..... http://ee07m060.wordpress.com/2009/07/28/asynchronous-usb-transfers-using-libusb-win32/ Cheers, Michael Murphy -------------------------------- Asynchronous USB transfers using libusb-win32 The official documentation for the libusb-win32 library makes no mention of the asynchronous API that the library provides. Evidence of these functions is to be found in the prototypes defined in the header file usb.h that forms part of the source code for the win32 port of this important library. Those async functions are defined as follows: int usb_submit_async(void *context, char *bytes, int size); int usb_reap_async(void *context, int timeout); int usb_reap_async_nocancel(void *context, int timeout); int usb_cancel_async(void *context); int usb_free_async(void **context); What still remains absent in the async API is any documentation on how to use it! The clearest example of working async code that I have found was written by Kevin Kofler. Kevin developed it as part of the USB codebase for the Texas Instrument calculator project. Here is a small excerpt of Kevins code. It should get you going with async USB transfers in Windows using libusb-win32: [..] /* variables for slv_check and slv_bulk_read2 */ static int io_pending = 0; uint8_t rBuf[64]; uint8_t* rBufPtr; [..] static int slv_check(CableHandle *h, int *status) { int ret; if (!io_pending) { ret = usb_bulk_setup_async(uHdl, &context, TIGL_BULK_IN); if (ret < 0) return ERR_READ_ERROR; ret = usb_submit_async(context, (char*)rBuf, max_ps); if (ret < 0) { usb_free_async(&context); return ERR_READ_ERROR; } io_pending = TRUE; } ret = usb_reap_async_nocancel(context, 0); if (ret < 0 && ret != -ETIMEDOUT) { // Error, unlink URB and return failure. usb_cancel_async(context); usb_free_async(&context); io_pending = FALSE; if (ret > 0) { nBytesRead = ret; rBufPtr = rBuf; *status = STATUS_RX; // data available } } return 0; } |