USB Development
1. Introduction
This article demonstrates the process of using libusb for USB host application development on embedded platforms. Finally, a case study is shared, which involves real-time video stream collection through a custom protocol using bulk transfer.
Refer to other articles on this development board
https://mp.weixin.qq.com/s/5gDfX__U8-F4MVqn92KtLw Playing Board Series II: Setting Up Samba Service on BeagleBone Green Development Board
https://mp.weixin.qq.com/s/14_pt_OkyrNTEPGlB9gWBw Playing Board Series III: Serial Port Development on BeagleBone Green Development Board Based on Ibserialport
https://mp.weixin.qq.com/s/ygU1d60Dnj72HSlVnwtBw Playing Board Series I: BeagleBone Green as an Excellent Alternative to Raspberry Pi SBC
The following case study can be found at:
https://mp.weixin.qq.com/s/9yhMPGqsehdCypk7EtS91Q Reference Super Simplified Series XXI: Efficient and Robust Frame Reception Implementation Based on State Machine and FIFO – Practical Efficient Video Stream Processing
《STEP BY STEP Design a USB Debug Assistant》 series of articles.
2. Building the Library
Follow the steps below to build the library
Update the system
sudo apt update
sudo apt upgrade
Install dependencies
sudo apt-get install autoconf
sudo apt-get install libtool
sudo apt-get install libudev-dev
Download the code
git clone https://github.com/libusb/libusb.git
cd libusb
./autogen.sh
make
sudo make install
Install to /usr/local/lib
3. Compiling Applications Using the Library
By default, when building the library, all example programs are compiled,
cd examples/
./listdevs to run the example.
We can use the example program to manually compile
Compile the program using the library
cd libusb/examples/
gcc listdevs.c -lusb-1.0 -I/usr/local/include/libusb-1.0 -L/usr/local/lib
4. Compiling Applications from Source
Compiling from source is more convenient for development and debugging,
Prepare the source code
Located in the same directory as libusb
Copy the example routine over cp libusb/examples/listdevs.c .
./autogen.sh will automatically generate config.h
Copy the configuration file over cp libusb/config.h .
It is also possible to manually create config.h with the following content
Compile: Dependencies-ludev -lpthread
gcc libusb/libusb/*.c libusb/libusb/os/events_posix.c libusb/libusb/os/linux_udev.c libusb/libusb/os/linux_usbfs.c libusb/libusb/os/threads_posix.c listdevs.c -Ilibusb/libusb -Ilibusb/libusb/os -I. -lpthread -ludev -o listdevs
Run ./listdevs
debian@BeagleBone:~$ ./listdevs
1d6b:0002 (bus 1, device 1)
debian@BeagleBone:~$
Thus, developing applications using libusb requires dependencies on libudev and pthread, and it is necessary to check if these dependencies are supported on the embedded platform.
I have a project here that supports cross-platform development on Linux and Windows, with the following files, primarily for parsing and displaying video streams sent from devices through bulk transfer.
debian@BeagleBone:~/share/usb_tool$ tree . -L 3
.
├── Linux_CMD
│ ├── build.sh
│ ├── config.h
│ ├── main.c
│ └── usb_tool
├── Windows_QT
│ ├── build
│ │ └── build-usb_tool-Desktop_Qt_6_6_1_MinGW_64_bit-Profile
│ ├── config.h
│ ├── main.cpp
│ ├── mainwindow.cpp
│ ├── mainwindow.h
│ ├── mainwindow.ui
│ ├── usb_tool.pro
│ └── usb_tool.pro.user
├── fifo.c
├── fifo.h
├── fifo_pool.c
├── fifo_pool.h
├── frame.c
├── frame.h
├── libusb
│ ├── AUTHORS
│ ├── COPYING
│ ├── ChangeLog
│ ├── HACKING
│ ├── INSTALL_WIN.txt
│ ├── Makefile.am
│ ├── NEWS
│ ├── PORTING
│ ├── README
│ ├── README.git
│ ├── README.md
│ ├── TODO
│ ├── Xcode
│ │ ├── common.xcconfig
│ │ ├── config.h
│ │ ├── debug.xcconfig
│ │ ├── libusb.xcconfig
│ │ ├── libusb.xcodeproj
│ │ ├── libusb_debug.xcconfig
│ │ ├── libusb_release.xcconfig
│ │ └── release.xcconfig
│ ├── android
│ │ ├── README
│ │ ├── config.h
│ │ ├── examples
│ │ └── jni
│ ├── appveyor.yml
│ ├── autogen.sh
│ ├── bootstrap.sh
│ ├── configure.ac
│ ├── doc
│ │ ├── Makefile.in
│ │ ├── doxygen.cfg.in
│ │ └── libusb.png
│ ├── examples
│ │ ├── Makefile.am
│ │ ├── dpfp.c
│ │ ├── ezusb.c
│ │ ├── ezusb.h
│ │ ├── fxload.c
│ │ ├── hotplugtest.c
│ │ ├── listdevs.c
│ │ ├── sam3u_benchmark.c
│ │ ├── testlibusb.c
│ │ └── xusb.c
│ ├── libusb
│ │ ├── Makefile.am
│ │ ├── Makefile.am.extra
│ │ ├── core.c
│ │ ├── descriptor.c
│ │ ├── hotplug.c
│ │ ├── io.c
│ │ ├── libusb-1.0.def
│ │ ├── libusb-1.0.rc
│ │ ├── libusb.h
│ │ ├── libusbi.h
│ │ ├── os
│ │ ├── strerror.c
│ │ ├── sync.c
│ │ ├── version.h
│ │ └── version_nano.h
│ ├── libusb-1.0.pc.in
│ ├── msvc
│ │ ├── Base.props
│ │ ├── Configuration.Application.props
│ │ ├── Configuration.Base.props
│ │ ├── Configuration.DynamicLibrary.props
│ │ ├── Configuration.StaticLibrary.props
│ │ ├── ProjectConfigurations.Base.props
│ │ ├── build_all.ps1
│ │ ├── config.h
│ │ ├── dpfp.vcxproj
│ │ ├── dpfp_threaded.vcxproj
│ │ ├── fxload.vcxproj
│ │ ├── getopt
│ │ ├── getopt.vcxproj
│ │ ├── hotplugtest.vcxproj
│ │ ├── init_context.vcxproj
│ │ ├── libusb.sln
│ │ ├── libusb_dll.vcxproj
│ │ ├── libusb_static.vcxproj
│ │ ├── listdevs.vcxproj
│ │ ├── sam3u_benchmark.vcxproj
│ │ ├── set_option.vcxproj
│ │ ├── stress.vcxproj
│ │ ├── stress_mt.vcxproj
│ │ ├── testlibusb.vcxproj
│ │ └── xusb.vcxproj
│ └── tests
│ ├── Makefile.am
│ ├── init_context.c
│ ├── libusb_testlib.h
│ ├── macos.c
│ ├── set_option.c
│ ├── stress.c
│ ├── stress_mt.c
│ ├── testlib.c
│ ├── umockdev.c
│ └── webusb-test-shim
├── log.c
├── log.h
├── usbdev.c
├── usbdev.h
├── usbdev_cfg.h
├── usbdev_cmd.c
├── usbdev_cmd.h
├── usbdev_fifo.c
├── usbdev_fifo.h
├── usbdev_frame.c
├── usbdev_frame.h
├── usbdev_frame_converted.c
├── usbdev_frame_converted.h
├── usbdev_task.c
└── usbdev_task.h
18 directories, 118 files
debian@BeagleBone:~/share/usb_tool$
Compile with ./build.sh
Contents of build.sh are as follows
gcc *.c ../*.c ../libusb/libusb/*.c ../libusb/libusb/os/events_posix.c ../libusb/libusb/os/linux_udev.c ../libusb/libusb/os/linux_usbfs.c ../libusb/libusb/os/threads_posix.c -I../libusb/libusb -I../libusb/libusb/os -I. -I../ -lpthread -ludev -o usb_tool
Compiling on the board is also acceptable, and the time is not very long
Running as follows
5. Conclusion
This article shared the process of developing USB host applications using libusb on this development board, which is also suitable for general embedded platforms. A specific project case was also shared.