[原创] 飞凌OK3568-C开发板基于libusb的usb应用开发

qinyunti   2023-10-27 09:40 楼主

一.前言

本文介绍OK3568-C开发板基于libusb的应用开发,分别介绍了构建libusb库,应用链接libusb库和直接libusb源码和应用源码一起编译两种方式。并介绍了使用GDB进行调试。

 

二.构建库的方式

2.1构件库

下载源码

git clone https://github.com/libusb/libusb.git

进入源码路径

cd libusb/

自动生成

./autogen.sh

配置

./configure

构建和安装

make && make install

 

2.2测试

使用源码的样例代码测试

进入样例代码路径

cd examples/

编译

make

运行测试程序

make编译了很多测试程序,比如运行listdevs

./listdevs

 

打印如下

root@ok3568:~/Desktop/libusb/examples# ./listdevs

1d6b:0003 (bus 8, device 1)

1d6b:0002 (bus 7, device 1)

1d6b:0003 (bus 6, device 1)

1d6b:0002 (bus 5, device 1)

1d6b:0001 (bus 4, device 1)

1d6b:0002 (bus 2, device 1)

1d6b:0001 (bus 3, device 1)

1d6b:0002 (bus 1, device 1)

 

2.3手动编译

上面examples目录下提供了Makefile进行所有的样例的编译,我们也可以手动编译我们自己的应用或者某个样例程序,比如-lusb-1.0链接libusb库,-I指定头文件路径,-L指定库文件路径。

gcc listdevs.c -I../libusb -lusb-1.0 -L../libusb

 

2.4调试

安装gdb

apt install gdb

-g编译

gcc listdevs.c -I../libusb -lusb-1.0 -L../libusb -g -o listdevs

使用gdb调试

gdb listdevs

 

 

三.直接使用源码

以上先构建库然后链接库的方式DEBUG时看不到libusb的源码,我们可以直接使用libusb的源码和自己的应用代码一起编译,这样方便调试阅读源码。

 

复制一下内容到自己的工程目录下

复制libusb下的如下内容

core.c

descriptor.c

hotplug.c

Io.c

libusb.h

libusbi.h

sync.c

version.h

version_nano.h

strerror.c

 

复制libusb/os下的如下内容

events_posix.c

events_posix.h

linux_udev.c

linux_usbfs.c

linux_usbfs.h

threads_posix.c

threads_posix.h

 

examples下的listdevs.c

同时复制config.h文件过来

 

最终目录如下

.

|-- examples

|   |-- config.h

|   `-- listdevs.c

`-- libusb

    |-- core.c

    |-- descriptor.c

    |-- hotplug.c

    |-- io.c

    |-- libusb.h

    |-- libusbi.h

    |-- os

    |   |-- events_posix.c

    |   |-- events_posix.h

    |   |-- linux_udev.c

    |   |-- linux_usbfs.c

    |   |-- linux_usbfs.h

    |   |-- threads_posix.c

    |   `-- threads_posix.h

    |-- strerror.c

    |-- sync.c

    |-- version.h

    `-- version_nano.h

 

3 directories, 19 files

 

编译命令如下

cd examples

gcc ../libusb/*.c ../libusb/os/events_posix.c ../libusb/os/linux_udev.c ../libusb/os/linux_usbfs.c ../libusb/os/threads_posix.c ./listdevs.c -I../libusb -I../libusb/os -I./ -lpthread -ludev -g -o listdevs

 

gcc libusb/*.c libusb/os/events_posix.c libusb/os/linux_udev.c libusb/os/linux_usbfs.c libusb/os/threads_posix.c ./examples/listdevs.c -Ilibusb -Ilibusb/os -Iexamples -lpthread -ludev -g -o listdevs

 

 

使用GDB调试

gdb listdevs

打印如下

root@ok3568:~/Desktop/libusbtest/examples# gdb listdevs

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2                                                                                                                                                                       

Copyright (C) 2020 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 "aarch64-linux-gnu".                                                                                                                                                                 

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 listdevs...                                                                                                                                                                                

(gdb)          

 

layout src打开源码视图

 

b main在main函数处打断点

(gdb) b main

Breakpoint 1 at 0x178cc: file ./listdevs.c, line 53.

(gdb)

 

输入run回车,执行到断点处

image-20231027093828-1.png  

按s,单步运行可以进入libusb的源码中进行调试

image-20231027093828-2.png  

 

  • 总结

Linux下进行libusb比较比较简单,libusb的源码不多,开发阶段建议直接和应用代码一起编译,方便调试。依赖libpthread libudev。

从上可以看出得益于OK3568-C强劲的性能和完善的环境,可以方便的进行USB应用的开发。

 

 

 

 

 

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复