libusb移植与v4l2使用--Applecai的学习笔记 前言 问题 正确的libusb交叉编译步骤 API调用及功能测试通过 小插曲

前言

之前把家里的库存小模块都玩了一遍,这些主要是配在单片机玩的,但是我放到linux上,目的就是学习linux自己写驱动。至于usb等驱动包括v4l2内核配置下就有了。然后网上找的简单的框架API调用下,家里的usb camera就用起来了。2年前我就已经玩过v4l2了,arm-VS2017 opencv远程人脸识别--APPLE的学习笔记,至于libusb我去移植它还是第一次,我想后面做些usb相关的应用,所以看了下libusb是常用库,所以就交叉编译移植下。代码已上传我的gitee,15_usbdev工程。

问题

包括交叉编译问题的临时解决,及测试代码编译后segment fault的调试解决
(如下6个步骤不要参考,参考正确的libusb交叉编译步骤)
一,libusb交叉编译
1.交叉编译先./autogen.sh。
2.配置
./configure --build=i686-linux --host=arm-linux --prefix=/home/applecai/tools/install CC=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-gcc CXX=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-g++
结果报错

  1. 打开config.log查看是-V的问题,改成-v。
  2. 又报错4447,就是如下2句,我就删除了。
    /LT_INIT/
    /LT_LANG(Windows Resource)/
  3. 又报错error: cannot find input file: `Makefile.in',按网上搜索的结果
    就在configure 之前执行如下命令
    aclocal
    libtoolize --force
    automake --add-missing
    autoconf
    autoheader
    make clean
  4. 重新再运行步骤2的配置命令,成功生成makefile
  5. make
  6. make install
    然后拿libusb中的example code来使用,功能就是遍历usb设备。
    静态编译由于缺少udev库报错。于是动态编译,如下命令是错误的,-fPIC -shared是编译so动态库的。但是我一开始没反应过来
    arm-linux-gnueabihf-gcc -g -fPIC -shared usbtest.c -o usbtest -I/home/applecai/tools/install/include/libusb-1.0/ -L/home/applecai/tools/install/lib/ -lusb-1.0
    直接运行编译后的代码则segment fault

通过开发板上gdb调试,由于当前是buildroot uclib编译的,我之前的gdb是用的自己通过busybox glib编译的,所以换了挂载的文件系统,然后里面有我之前准备的gdb bin文件。gdb运行后提示缺少python。查看报错是有个路径也要copy gdb文件夹。copy完成后gdb正常运行。发现原因是0x00000000 in ?? ()

[   15.766487] random: gdb: uninitialized urandom read (24 bytes read)
GNU gdb (GDB) 8.2
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./usbtest...done.
(gdb) b main
Breakpoint 1 at 0x8dc: file usbtest.c, line 39.
(gdb) r
Starting program: /usr/study/usbtest 

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) 

这时候反应到我添加了-fPIC -shared,重新改成动态编译,依然报错,缺少udev。网上搜索了下libusb默认找设备是从udev,可以通过配置--disable udev来禁止调用udev相关函数。所以我重新编译libusb,结果同样的命令再运行一次,configure无法通过。于是重新unzip libusb,本次编译就非常顺利。之前可能是一开始编译不正确,后来没有用make clean。所以如下才是正确的

正确的libusb交叉编译步骤

  1. ./autogen.sh
  2. ./configure --build=i686-linux --host=arm-linux --prefix=/home/applecai/tools/install CC=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-gcc CXX=/home/applecai/bbb/ti-processor-sdk-linux-am335x-evm-06.01.00.08/linux-devkit/sysroots/x86_64-arago-linux/usr/bin/arm-linux-gnueabihf-g++ --disable-udev --enable-system-log
  3. make
  4. make install
    然后对c文件进行编译,链接动态库的方式。
    arm-linux-gnueabihf-gcc usbtest.c -o usbtest -I/home/applecai/tools/install/include/libusb-1.0/ -L/home/applecai/tools/install/lib/ -lusb-1.0 -lpthread

API调用及功能测试通过

一开始插入usb camera后,libusb扫描到2个设备,拔走camera后,libusb扫描到一个usb设备。至于camg里面是调用了v4l2的API显示camera基本信息的。至于camg就是用-static静态编译的,这是很顺利的。

Please press Enter to activate this console. 
[root@apple335 ]# cd /usr/study/
[root@apple335 study]# [   20.194856] usb 1-1: new high-speed USB device number 2 using musb-hdrc
[   20.387122] usb 1-1: New USB device found, idVendor=1871, idProduct=0142, bcdDevice= 0.0c
[   20.395387] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[   20.402561] usb 1-1: Product: USB2.0 Camera
[   20.406793] usb 1-1: Manufacturer: AVEO Technology Corp.
[   20.414666] uvcvideo: Found UVC 1.00 device USB2.0 Camera (1871:0142)
[   20.423928] uvcvideo 1-1:1.0: Entity type for entity Extension 4 was not initialized!
[   20.431894] uvcvideo 1-1:1.0: Entity type for entity Processing 3 was not initialized!
[   20.439886] uvcvideo 1-1:1.0: Entity type for entity Camera 1 was not initialized!
[   20.448030] input: USB2.0 Camera: USB2.0 Camera as /devices/platform/ocp/47400000.usb/47401c00.usb/musb-hdrc.1/usb1/1-1/1-1:1.0/input/input0

[root@apple335 study]# cd /usr/study/
[root@apple335 study]# ls
camg     core     t        usbtest
[root@apple335 study]# ./usbtest 
Hello, world!1
121871:0142 (bus 1, device 2) path: 1
1d6b:0002 (bus 1, device 1)
[root@apple335 study]# ./camg 
init camera
driver:         uvcvideo
card:           USB2.0 Camera: USB2.0 Camera
bus_info:       usb-musb-hdrc.1-1
version:        328765
capabilities:   84a00001
Device supports capture.
Device supports streaming.
Support format:
        1.YUYV 4:2:2
support format RGB32
set fmt...
fmt.type:               1
pix.pixelformat:        RGB4
pix.height:             480
pix.width:              640
pix.field:              4
get fmt...
fmt.type:               1
pix.pixelformat:        YUYV
pix.height:             480
pix.width:              640
pix.field:              1
numerator:1
denominator:30
[root@apple335 study]# [   59.204985] usb 1-1: USB disconnect, device number 2

[root@apple335 study]# ./usbtest 
Hello, world!1
121d6b:0002 (bus 1, device 1)

小插曲

我把挂载文件系统环境又换成在buildroot用uclib编译的,结果

./usbtest
-sh: ./usbtest: not found
我猜测是缺少库文件。因为之前我自己编译的busybox文件系统环境中很多库我我从ti包中copy的。查看需要的库。

root@applecaiHP:/home/applecai/mydriver/usbdev# arm-linux-gnueabihf-objdump -x usbtest |grep NEEDED
  NEEDED               libusb-1.0.so.0
  NEEDED               libpthread.so.0
  NEEDED               libc.so.6
root@applecaiHP:/home/applecai/mydriver/usbdev# 

uClibc是独立的,为了应用于嵌入式系统中,完全重新实现出来的。和glibc在源码结构和二进制上,都不兼容。我直接copy了libpthread和libc,依然无法解决。由于buildroot之前用uclib编译的,若改用glibc非常花费时间,而当前文件系统已经不是我的重点,所以我准备将文件系统环境切换到之前用glibc编译的busybox最下文件系统下,而且,库文件可以用ti制作的库直接替换了。或者之后再尝试用uclib来编译libusb,就怕编译不过。
参考网址:如下好像uclib编译通过了
https://blog.csdn.net/weixin_33841722/article/details/91735798

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章