📜 ⬆️ ⬇️

Build USB HID under BeagleBone



In one of the publications we wrote about setting up a Chinese USB-WiFi for Beagledone. Today we want to give a way to build your own device management class via the HID protocol. The hidapi library was taken as a basis , and then a method of cross-compiling under beaglebone and building a test program for working with a USB device was made.

One of the easiest and most reliable ways to make USB control over Linux is to use the libusb-1.0 library. Hidapi is a “add-on” above libusb, and is used to easily connect hid devices to a programmable device. If you build on a “clean” hidapi system, you will have to compile libusb separately and only then build hidapi. In this article we will consider the assembly exactly hidapi. Looking ahead, let's say that android ndk refused to collect hidapi without intervention.

To get started, we downloaded the library and unpacked it into the working directory; we used the Linux Mint distribution for building and testing. In the unpacked folder (in our case it is hidapi-0.7.0), read the instructions readme.txt. Since BeagleBone uses Linux, we go to the hidapi-0.7.0 / linux folder and see there another readme.txt file with instructions for building under Linux. In short, the library offers us two options for building: using hid-libusb.c or with hid.c. In the first case, the library libusb is used, which must be installed in the system. In the second case, hidraw is used, requiring libudev-devel.
')
We note right away that we tried to collect both options; only hid-libusb.c was successfully assembled, and hid.c produced various kinds of errors. Next, the developers offer us to put the package libusb-1.0-0-dev or libudev-devel. After installing the packages, you can build the project in the linux folder. We execute make, the hidapi-0.7.0 / hidtest / project is being built, and it is being built in our hidapi-0.7.0 / linux folder. At the command prompt, you can see the compiler commands. Further, the essence of the assembly under BeagleBone is to substitute the cross-compiler instead of the regular gcc and g ++, respectively. But there is one subtlety, if you look inside the makefile, then you can see there
lines
LIBS = `pkg-config libusb-1.0 libudev --libs` -lpthread
INCLUDES? = -I ../ hidapi `pkg-config libusb-1.0 --cflags`

These lines add the paths to the libraries (includes), and set the library assembly keys (libs) dynamically. To understand what loads the script into the assembly, you can run it in the console: `pkg-config libusb-1.0 libudev --libs` and` pkg-config libusb-1.0 --cflags`. When building software under BeagleBone, we did not change the path to include (there are only header files .h, and cross-platform ones), and either we wrote it directly without pkg-config. Perhaps this is not the best solution, but still working.

In general, it looks like
in the following way
INCLUDEPATH + = / usr / include / libusb-1.0
Libs + = -lusb-1.0 -ludev -g
hidapi.h add to the source folder.
gcc -Wall -g -c -I / usr / include / libusb-1.0 hid-libusb.c -o hid-libusb.o`

We fix the filefile by directly turning on the libraries, prescribe the cross-compiler and build the program under BeagleBone. That's all, the program for working with USB HID device is ready.

Source: https://habr.com/ru/post/264391/


All Articles