imx6ull development board environment configuration – libusb, libudev, eudev cross compilation

Directory

Zero. Foreword

1. libusb cross compilation

1.0 Preface

1.1 Cross Compilation

Two, usbutils cross compilation

2.0 Preface

2.1 Cross Compilation

3. libudev cross compilation

3.0 Preface

3.1 Cross Compilation

3.2 Error handling – usbutils not found

3.3 Error handling – pci.ids not found (pci.ids not found)

3.3.0 Preface

3.3.1 Find pci.ids

3.3.2 Continue to compile

3.4 Error handling – gperf not found (gperf not found)

3.5 Compile and install

4. eudev cross compilation

4.0 Preface

4.1 Cross Compilation

5. Test code

5.1 usb test code

5.2 Compile script Makefile

5.3 Running Results


Zero, Preface

Background: Because the punctual atom imx6ull needs to use usblib for some usb-related programming, and punctual does not provide the corresponding usblib-related library cross-compilation method. The following briefly introduces how to cross-compile the libraries required for usb development.

Dependency:libusb<-usbutils<-libudev Cross-compilation is recommended to follow the following order to prevent conflicts Phenomenon: When compiling with the test code, the following error occurs, because the corresponding libusb and libudev libraries are missing

alientek@ubuntu16:~/linuxProject/v4l2_test/usb_callback_test$ make
arm-linux-gnueabihf-gcc -o usb_callback_test usb_callback_test.c -lpthread -lusb-1.0 -I/home/alientek/tools/libusb-1.0.26/build/include -L/home/alientek/tools/libusb-1.0. 26/build/lib
/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.4/../../.. /../arm-linux-gnueabihf/bin/ld: warning: libudev.so.1, needed by /home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so, not found ( try using -rpath or -rpath-link)
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_filter_add_match_subsystem_devtype@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_enable_receiving@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_scan_devices@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_new@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_device_get_devnode@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_new@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_list_entry_get_name@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_add_match_subsystem@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_device_get_sysname@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_get_list_entry@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_add_match_property@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_device_new_from_syspath@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_new_from_netlink@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_receive_device@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_device_unref@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_enumerate_unref@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_get_fd@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_list_entry_get_next@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_monitor_unref@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_unref@LIBUDEV_183'
/home/alientek/tools/libusb-1.0.26/build/lib/libusb-1.0.so: undefined reference to 'udev_device_get_action@LIBUDEV_183'
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'usb_callback_test' failed
make: *** [usb_callback_test] Error 1

1. libusb cross compilation

1.0 Preface

Reference: Wei Dongshan Driver Encyclopedia-12USB-07_Using libusb to read mouse parameters

Library version: libusb-1.0.26 (https://sourceforge.net/projects/libusb/)

1.1 cross compilation

sudo apt-get install libtool

unzip libusb-1.0.26.zip
cd libusb-1.0.26
mkdir build
./autogen.sh

./configure CC={compiler directory} --host=arm-linux-gnueabihf --prefix=$PWD/build

make

make install #Install in the build directory

When compiling, you need to specify the header file path /home/alientek/tools/libusb-1.0.26/build/include

Library file path /home/alientek/tools/libusb-1.0.26/build/lib

Second, usbutils cross compilation

2.0 Preface

Reference:
Cross compilation of libusb and usbutils (ngui.cc)

Library Version:usbutils-005 (
Index of /pub/linux/utils/usb/usbutils/ (kernel.org))

2.1 Cross compilation

#unzip
cd usbutils-005
mkdir build
./configure CC={compiler directory} --host=arm-linux-gnueabihf --prefix=$PWD/build
make
make install

Third, libudev cross compilation

3.0 Preface

Reference:
Cross compile libudev_libudev cross compile_xqhrs232’s blog – CSDN blog

Library version: udev-182

3.1 Cross compilation

#unzip
cd udev-182
mkdir build
./configure CC={compiler directory} --host=arm-linux-gnueabihf --prefix=$PWD/build
make
make install

If there is no problem with the environment, it can be compiled successfully at one time, as shown below. But in fact, some files will be missing. For details, refer to chapters after 3.2.

3.2 Error handling-usbutils not found

If the configure configuration fails and an error occurs that usbutils is not found, you need to check whether there is a problem with the usbutils installation

By viewing the above printed information, you can see that the pkg-config path used is in the /opt/fsl-imx-x11/4.1.15-2.1.0/ directory. Find the actual stored pkgconfig path through the following command

cd /opt/fsl-imx-x11/4.1.15-2.1.0/ #Modify according to the prompt information
find ./ -name "pkgconfig" #Get path /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/lib/pkgconfig

Put the files generated by cross compilation in the usbutils-005 directory into the above pkgconfig directory

cp {usbutils-005}/build/share/pkgconfig/usbutils.pc /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/ lib/pkgconfig

After completing the above operations, configure udev-182 again

./configure --host=arm-linux-gnueabihf --prefix=$PWD/build #Configure udev cross compilation

Prompt the following error:

3.3 Error handling – pci.ids not found (pci.ids not found)

3.3.0 Preface

Reference: Cross compile udev library_pci.ids not found_huihuiwith’s blog-CSDN blog

3.3.1 Find pci.ids

Could not find a valid address using whereis pci.ids in the reference connection. Here use the find command to find: sudo find / -name “pci.ids”

Two paths are found. Question: We need cross-compilation, can it just be the ids of x86?

3.3.2 Continue compiling

Specify the pci path address and configure again

./configure --host=arm-linux-gnueabihf --prefix=$PWD/build --with-pci-ids-path=/usr/share/misc/pci.ids

Prompt gperf not found

3.4 Error handling – gperf not found (gperf not found)

Reference: configure: error: gperf is needed_tool gperf is required._Liu Kunpeng’s Blog-CSDN Blog

Install gperf via apt:

sudo apt install gperf

Configure again to succeed

3.5 Compile and install

make
make install

Note: During the make process, I encountered problems such as /bin/grep: /usr/lib/libglib-2.0.la: No such file or director. According to other people’s blog posts, udev has not been updated for a long time, and there is also eudev as a better alternative. Therefore, the subsequent modification uses eudev for cross-compilation.

4. eudev cross compilation

4.0 Preface

Reference: Hisilicon Hi3536 cross-compile eudev and libusb library_eudev 3.2.9 compilation_mkelehk’s blog-CSDN blog

eudev version:3.2.11 (Archived eudev tarballs (gentoo.org))

4.1 Cross compilation

sudo apt install gperf
# unzip
cd eudev-3.2.11
mkdir build
./configure --host=arm-linux-gnueabihf --prefix=$PWD/build --disable-blkid --disable-kmod
make
make install

Put the cross-compiled header files and dynamic library files into the cross-tool chain

cd {eudev directory}/build
cp bin/udevadm ~/linux/nfs/rootfs-alientek/bin/

sudo cp lib/* /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi/lib/

sudo cp include/* /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/cortexa7hf-neon-poky-linux-gnueabi/usr/include/

5. Test code

5.1 usb test code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libudev.h>
#include <sys/select.h>

// print device information
void print_device_info(struct udev_device *dev) {
    // Get device information
    const char *devpath = udev_device_get_devpath(dev);
    const char *subsystem = udev_device_get_subsystem(dev);
    const char *devtype = udev_device_get_devtype(dev);
    const char *syspath = udev_device_get_syspath(dev);
    const char *sysname = udev_device_get_sysname(dev);
    const char *sysnum = udev_device_get_sysnum(dev);
    const char *devnode = udev_device_get_devnode(dev);
   

    // print device information
    printf("Device info:\
");
    printf(" devpath : %s\
", devpath );
    printf(" subsystem : %s\
", subsystem);
    printf(" devtype : %s\
", devtype );
    printf(" syspath : %s\
", syspath );
    printf(" sysname : %s\
", sysname );
    printf(" sysnum : %s\
", sysnum );
    printf(" devnode : %s\
", devnode );
    printf("\
");
}

// Loop through USB devices
void enumerate_usb_devices(struct udev *udev) {
    // create enumerator
    struct udev_enumerate *enumerate = udev_enumerate_new(udev);
    // set matching conditions
    udev_enumerate_add_match_subsystem(enumerate, "usb");
    // Scan for devices
    udev_enumerate_scan_devices(enumerate);

    // get device list
    struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
    struct udev_list_entry *dev_list_entry;

    // traverse the device list
    udev_list_entry_foreach(dev_list_entry, devices) {
        // get device path
        const char *path = udev_list_entry_get_name(dev_list_entry);
        // create device object
        struct udev_device *dev = udev_device_new_from_syspath(udev, path);

        if (dev) {
            // print device information
            print_device_info(dev);
            // Release the device object
            udev_device_unref(dev);
        }
    }

    // release the enumerator
    udev_enumerate_unref(enumerate);
}


int main() {
    // Create a udev object
    struct udev *udev = udev_new();

    if (!udev) {
        // failed to create
        fprintf(stderr, "Failed to create udev context\
");
        return 1;
    }

    // Enumerate all USB devices and print their information
    enumerate_usb_devices(udev);

    // Create a udev monitor object and set the device type to be monitored to usb
    struct udev_monitor *monitor = udev_monitor_new_from_netlink(udev, "udev");
    udev_monitor_filter_add_match_subsystem_devtype(monitor, "usb", NULL);

    // Enable udev monitor to receive events
    udev_monitor_enable_receiving(monitor);

    // Get the file descriptor of the monitor
    int fd = udev_monitor_get_fd(monitor);
    fd_set fds;

    // Enter the loop and wait for the event to occur
    while (1) {
        // Clear the file descriptor collection and add the monitor's file descriptor to the collection
        FD_ZERO( &fds);
        FD_SET(fd, &fds);

        // Call the select function to wait for the file descriptor to be ready
        int ret = select(fd + 1, & fds, NULL, NULL, NULL);

        if (ret > 0 & amp; & amp; FD_ISSET(fd, & amp; fds)) {
            // Receive an event object from monitor
            struct udev_device *dev = udev_monitor_receive_device(monitor);

            if (dev) {
                // Get the event action (add or remove)
                const char *action = udev_device_get_action(dev);

                if (strcmp(action, "add") == 0) {
                    // If it is an add event, print a prompt message and enumerate all USB devices
                    printf("USB device added\
");
                    // enumerate_usb_devices(udev);
                }
                else if (strcmp(action, "remove") == 0) {
                    // If it is a remove event, just print the prompt message
                    printf("USB device removed\
");
                }

                // Release the event object
                udev_device_unref(dev);
            }
        }
    }

    // Close monitor and udev objects
    udev_monitor_unref(monitor);
    udev_unref(udev);
    return 0;
}

5.2 Compilation script Makefile

CROSS=arm-linux-gnueabihf-
CC = $(CROSS)gcc
CFLAGS = -Wall -Wextra -pedantic -std=c99 \
-lpthread -lusb-1.0 -ludev \
-I /home/alientek/tools/libusb-1.0.26/build/include \
-I/home/alientek/tools/eudev-3.2.11/build/include

LDFLAGS=-L/home/alientek/tools/libusb-1.0.26/build/lib \
-L/home/alientek/tools/eudev-3.2.11/build/lib


TARGET = usb_callback_test
OBJS = $(TARGET).o

$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
cp -f ${TARGET} ~/linux/nfs/rootfs-alientek/

%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

clean:
rm -f $(OBJS) $(TARGET)

5.3 Running Results

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge