Created
December 17, 2023 02:05
-
-
Save mrchipset/869047c98ba2afe97aac78b571eae66c to your computer and use it in GitHub Desktop.
Async and sync bulk read with libusb
This file contains 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
#include "libusb-1.0/libusb.h" | |
#include <stdio.h> | |
#include <iostream> | |
#include <ctime> | |
static const int USB_VID = 0x0547; | |
static const int USB_PID = 0x1002; | |
static const int USB_EP_WRITE = 0x01; | |
static const int USB_EP_READ = 0x81; | |
static int r = 0; | |
static libusb_device_handle* dev = NULL; | |
static struct libusb_transfer* transferIn = NULL; | |
static int ERROR_LIBUSB_NO_ERROR = 0; | |
static int ERROR_LIBUSB_ERROR = -1; | |
static clock_t T1 = 0; | |
static clock_t T2 = 0; | |
static uint32_t packetLen = 0; | |
static uint32_t packetCount = 0; | |
static int CloseDevice(libusb_device_handle* handle) | |
{ | |
if (dev != NULL) { | |
libusb_close(dev); | |
dev = NULL; | |
} | |
return ERROR_LIBUSB_NO_ERROR; | |
} | |
static int OpenDevice(int vid, int pid) | |
{ | |
dev = libusb_open_device_with_vid_pid(NULL, vid, pid); | |
if (dev == NULL) { | |
fprintf(stderr, "Open device failed\n"); | |
return ERROR_LIBUSB_ERROR; | |
} | |
else { | |
if (libusb_kernel_driver_active(dev, 0) == 1) | |
{ | |
//fprintf(stderr, "Kernel Driver Active\n"); | |
if (libusb_detach_kernel_driver(dev, 0) != 0) | |
{ | |
fprintf(stderr, "Couldn't detach kernel driver!\n"); | |
CloseDevice(dev); | |
return ERROR_LIBUSB_ERROR; | |
} | |
} | |
r = libusb_claim_interface(dev, 0); | |
if (r < 0) | |
{ | |
fprintf(stderr, "Cannot Claim Interface\n"); | |
CloseDevice(dev); | |
return ERROR_LIBUSB_ERROR; | |
} | |
return ERROR_LIBUSB_NO_ERROR; | |
} | |
} | |
static int SyncRead() | |
{ | |
uint32_t buffer[128]; | |
int nRead = 0; | |
while (true) { | |
uint32_t start = clock(); | |
int nRet = libusb_bulk_transfer(dev, 0x81, reinterpret_cast<uint8_t*>(buffer), 128 * sizeof(uint32_t), &nRead, 500); | |
if (nRet == 0) { | |
uint32_t end = clock(); | |
std::cout << "Cmd:" << buffer[0] << "\tPacket Id:" << buffer[2] << "\tRead count:" << nRead << "\t Used:" << (end - start) << " ms" << std::endl; | |
} | |
} | |
return 0; | |
} | |
void DataReceivedCallback(struct libusb_transfer* transfer) | |
{ | |
T2 = clock(); | |
libusb_submit_transfer(transferIn); | |
uint32_t* buffer = reinterpret_cast<uint32_t*>(transfer->buffer); | |
std::cout << "Cmd:" << buffer[0] << "\tPacket Id:" << buffer[2] << "\tRead count:" << transfer->actual_length << "\t Used:" << (T2 - T1) << " ms" << std::endl; | |
T1 = T2; | |
} | |
static int AsyncRead() | |
{ | |
transferIn = libusb_alloc_transfer(0); | |
uint32_t buffer[128 * 64]; | |
libusb_fill_bulk_transfer(transferIn, dev, 0x81, reinterpret_cast<uint8_t*>(buffer), 128 * sizeof(uint32_t) * 64, DataReceivedCallback, NULL, 500); | |
r = libusb_submit_transfer(transferIn); | |
while (1) { | |
r = libusb_handle_events_completed(NULL, NULL); | |
if (r < 0) { | |
break; | |
} | |
} | |
return 0; | |
} | |
int main() | |
{ | |
r = libusb_init(/*ctx=*/NULL); | |
int ret = OpenDevice(USB_VID, USB_PID); | |
//SyncRead(); | |
AsyncRead(); | |
CloseDevice(dev); | |
libusb_exit(NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment