Build USB HID under BeagleBone

  • Tutorial


In one of our publications, we wrote about configuring Chinese USB-WiFi for Beagledone. Today we want to provide a way to build our own device control class via the HID protocol. The hidapi library was taken as a basis , and then a cross-compilation method for beaglebone was made and a test program was built for working with a USB device.

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

To get started, we downloaded the library and unpacked it into the working directory, for assembly and testing we used the Linux Mint distribution. In the unpacked folder (in our case it is hidapi-0.7.0), readme.txt instructions should be read. Since BeagleBone uses Linux, we go to the hidapi-0.7.0 / linux folder and there we see the next readme.txt file with instructions for building under Linux. In short, the library offers us two build options: using hid-libusb.c or hid.c. In the first case, the libusb library is used, which must be installed on the system. In the second case, hidraw is used, requiring libudev-devel.

Just note that we tried to collect both options; only hid-libusb.c successfully assembled, and hid.c threw various kinds of errors. Further, 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 assembled, and it is going just to 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
lines
LIBS = `pkg-config libusb-1.0 libudev --libs` -lpthread
INCLUDES? = -I ../ hidapi` pkg-config libusb-1.0 --cflags`

These lines add library paths (includes), and set 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 for BeagleBone, we did not change the path to include (there are only .h header files, moreover, cross-platform ones), and we registered directly without pkg-config. Perhaps this is not the most successful solution, but still working.

In general, it looks
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 mail file by directly turning on the libraries, prescribe the cross-compiler and assemble the program under BeagleBone. That's all, the program for working with a USB HID device is ready.

Also popular now: