天天看點

交叉編譯libusb和libusb-compat-0.1.5

1. 下載下傳libusb庫:

https://jaist.dl.sourceforge.net/project/libusb/libusb-1.0/libusb-1.0.22/libusb-1.0.22.tar.bz2

2. 編譯安裝libusb:

tar -xvf ./libusb-1.0.22.tar.bz2

cd ./libusb-1.0.22

mkdir build

./configure --prefix=/home/chy/Downloads/libusb-1.0.22/build CC=mips-linux-gnu-gcc --host=mips-linux-gnu --build=i686-linux --disable-udev

make -j4

make install

3. 下載下傳libusb-compat-0.1.5庫:

https://sourceforge.net/projects/libusb/files/libusb-compat-0.1/libusb-compat-0.1.5/libusb-compat-0.1.5.tar.bz2

4. 編譯安裝libusb-compat:

tar -xvf ./libusb-compat-0.1.5.tar.bz2

cd ./libusb-compat-0.1.5

mkdir build

./configure --build=i686-linux --host=mips-linux-gnu --prefix=/home/chy/work/project/libusb-1.0.0/libusb-compat-0.1.5/build CC=mips-linux-gnu-gcc PKG_CONFIG_PATH=/home/chy/work/project/libusb-1.0.0/libusb-1.0.0-mips/lib/pkgconfig

make -j4

make install

5. 測試程式

#include <stdio.h>
#include "usb.h"

#define VID 0x0525
#define PID 0xa4ac

int main(int argc, char *argv[])
{
	usb_init();
	usb_find_busses();
	usb_find_devices();
	usb_dev_handle *handle = NULL;
	struct usb_device *device = NULL;

	struct usb_bus *bus = NULL;
	for (bus = usb_busses; bus; bus = bus->next) {
		struct usb_device *dev = NULL;
		for (dev = bus->devices; dev; dev = dev->next) {
			if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID) {
				device = dev;
				break;
			}
		}
	}

	if (device == NULL) {
		printf("no found\n");
		return -1;
	}

	handle = usb_open(device);
	if (handle == NULL) {
		printf("open device err\n");
		return -1;
	}

	int ret = usb_detach_kernel_driver_np(handle, 0);
	if (ret != 0) {
		printf("set detach kernel driver failed ret = %d\n", ret);
		return -1;
	}

	ret = usb_set_configuration(handle, 1);
	if (ret != 0) {
		printf("set configuration failed ret = %d\n", ret);
		return -1;
	}

	usb_claim_interface(handle, 0);

#if 1
	unsigned char buff[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x00, 0x00, 0x00, 0x00};
	ret = usb_interrupt_write(handle, 0x01, buff, 8, 0);
	printf("%s %s %d ret = %d\n", __FILE__, __func__, __LINE__, ret);
	ret = usb_interrupt_write(handle, 0x01, &buff[8], 8, 0);
	printf("%s %s %d ret = %d\n", __FILE__, __func__, __LINE__, ret);
#else
	for (int index = 0; index < 10; index++) {
		unsigned char buff[16] = {0};
		int ret = usb_interrupt_read(handle, 0x81, buff, 8, 10000);
		printf("ret = %d\ndata:\n", ret);
		for (int i = 0; i < 8; i++)
			printf("%02x ", buff[i]);
		printf("\n");
	}
#endif

	ret = usb_release_interface(handle, 0);
	if (ret != 0) {
		printf("release failed\n");
		return -1;
	}

	ret = usb_close(handle);
	if (ret != 0) {
		printf("close failed\n");
		return -1;
	}

	return 0;
}
           

6. 編譯測試程式

mips-linux-gnu-gcc ./test.c -o test -I /home/chy/work/project/libusb-1.0.0/libusb-compat-0.1.5-mips/include -L /home/chy/work/project/libusb-1.0.0/libusb-compat-0.1.5-mips/lib -lusb -lusb-1.0 -std=gnu99