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

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