From 1cec0fc89af6cb7a21d453788b3dd5737f716008 Mon Sep 17 00:00:00 2001 From: ray <2954701669@qq.com> Date: Thu, 11 Apr 2024 03:22:54 +0000 Subject: [PATCH] init upload --- readme | 5 +++ usb_read_data.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++++ usb_test_show.cpp | 42 ++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 readme create mode 100644 usb_read_data.cpp create mode 100644 usb_test_show.cpp diff --git a/readme b/readme new file mode 100644 index 0000000..63d8acb --- /dev/null +++ b/readme @@ -0,0 +1,5 @@ +usb-hid +1. build the sample read usb-hid device gcc usb_test_show.cpp -o hid_test_show -lusb-1.0, this is have been test in +rk3588 and read Tracker sucessful. +2. build cmd gcc usb_read_data.cpp -o hid -lusb-1.0 and run "sudo ./hid" or you will cannot open dev + diff --git a/usb_read_data.cpp b/usb_read_data.cpp new file mode 100644 index 0000000..e43276d --- /dev/null +++ b/usb_read_data.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include + +static void print_devs(libusb_device **devs) +{ + libusb_device *dev; + int i = 0; + + while ((dev = devs[i++]) != NULL) { + struct libusb_device_descriptor desc; + int r = libusb_get_device_descriptor(dev, &desc); + if (r < 0) { + fprintf(stderr, "failed to get device descriptor"); + return; + } + + printf("%04x:%04x (bus %d, device %d)\n", + desc.idVendor, desc.idProduct, + libusb_get_bus_number(dev), libusb_get_device_address(dev)); + } +} + +int main() +{ + int r; + ssize_t cnt; + libusb_device_handle *dev_handle; //a device handle + libusb_device **devs; //devices + //libusb_context **ctx=NULL; + r=libusb_init(NULL); //init 初始化libusb + if(r<0) { + printf("failed to init libusb\n"); + return 1; + } + cnt = libusb_get_device_list(NULL,&devs); //获取设备列表 + if (cnt < 0) { + printf("failed to get device list\n"); + return 1; + } + //print_devs(devs); + dev_handle = libusb_open_device_with_vid_pid(NULL, 0x2833, 0x0002); + if(dev_handle == NULL){ + printf("Cannot open device\n"); + return 1; + }else + printf("Device Opened\n"); + + libusb_free_device_list(devs, 1); //free the list, unref the devices in it + + if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached + printf("Kernel Driver Active\n"); + if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it + printf("Kernel Driver Detached!\n"); + } + r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1) + if(r < 0) { + printf("Cannot Claim Interface\n"); + return 1; + } + printf("Claimed Interface\n"); + sleep(1); + unsigned char data[2]; + + unsigned char tmp_char[64]; + data[0]=0x02;data[1]=0x64; + int transferred; + int actual; //used to find out how many bytes were written + while(1){ + //r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_OUT), data, 2, &actual, 0); //my device's out endpoint was 1, found with trial- the device had 2 endpoints: 2 and 129 + //if(r == 0 && actual == 2) //we wrote the 2 bytes successfully + //printf("Writing Successful\n"); + //else + // printf("Write Error\n"); + + //r = libusb_interrupt_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN),tmp_char,64,&actual, 1000);//pay attion + + r = libusb_bulk_transfer(dev_handle, (0x01 | LIBUSB_ENDPOINT_IN), tmp_char, 64, &transferred,1000); + if(r == 0 && actual == 64) //we read the 64 bytes successfully + printf("Read Successful\n"); + else + printf("Read Error\n"); + printf("%i,%i\n",r,actual); + + int16_t mx = *(int16_t *)(tmp_char+56); + int16_t my = *(int16_t *)(tmp_char+58); + int16_t mz = *(int16_t *)(tmp_char+60); + printf("mag:%d,%d,%d\n",mx,my,mz); + usleep(1000*500); + //printf("%s","\033[1H\033[2J");//clear display + + } + + r = libusb_release_interface(dev_handle, 0); //release the claimed interface + if(r!=0) { + printf("Cannot Release Interface\n"); + return 1; + } + printf("Released Interface\n"); + + libusb_close(dev_handle); //close the device we opened + libusb_exit(NULL); //needs to be called to end the + + return 0; +} + + diff --git a/usb_test_show.cpp b/usb_test_show.cpp new file mode 100644 index 0000000..c4368e8 --- /dev/null +++ b/usb_test_show.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +static void print_devs(libusb_device **devs) +{ + libusb_device *dev; + int i = 0; + + while ((dev = devs[i++]) != NULL) { + struct libusb_device_descriptor desc; + int r = libusb_get_device_descriptor(dev, &desc); + if (r < 0) { + fprintf(stderr, "failed to get device descriptor"); + return; + } + + printf("%04x:%04x (bus %d, device %d)\n", + desc.idVendor, desc.idProduct, + libusb_get_bus_number(dev), libusb_get_device_address(dev)); + } +} + +int main() +{ + int r; + ssize_t cnt; + libusb_device **devs; //devices + r=libusb_init(NULL); //init 初始化libusb + if(r<0) { + printf("failed to init libusb\n"); + return 1; + } + cnt = libusb_get_device_list(NULL,&devs); //获取设备列表 + if (cnt < 0) { + printf("failed to get device list\n"); + return 1; + } + print_devs(devs); + return 0; +} +