关于UVC
UVC全称为USB Video Class,即:USB视频类,是一种为USB视频捕获设备定义的协议标准。是Microsoft与另外几家设备厂商联合推出的为USB视频捕获设备定义的协议标准之一。
UVC设备都是多Interface设备,这点同普通的u盘不同。UVC设备最起码有两个Interface,VideoControl(VC)Interface和VideoStream(VS) Interface; 这也是最常见的UVC设备。 Spec明确要求一个具有可用的,具有实际UVC功能的设备要有一个VC Interface,一个或多个VS Interface。
VCInterface用于进行配置,操控,设置UVC设备进入不同的功能状态,而VSInterface则负责视频数据流的传输;完整的UVC功能需依赖VS,VC Interfaces的配合才能实现。
Linux下的uvc驱动位于drivers/usb/gadget/目录下,需要的话可以仔细读一下源代码。
我这篇文章主要记录了一下大概的调试过程,时间有限,断断续续写了一星期才写完,可能有些细节没写到的,见谅。
参考链接
https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/2046001302/Building+Linux+usb+device+drivers+with+2021.1
开发环境
ubuntu20.04.1
zcu104或者zcu106,其他开发板根据硬件配置稍做修改
官方推荐petalinux2021.1
一、创建Petalinux工程并修改设备树
1) 创建一个基本的petalinux工程
参考以下链接
Petalinux快速入门向导 (4) 第三章.PetaLinux开发基本流程
先从www.xilinx.com官网下载zcu106对应的bsp文件xilinx-zcu106-v2021.1-final.bsp
然后用bsp创建petalinux工程
petalinux-create -t project -s xilinx-zcu106-v2021.1-final.bsp
2) 配置sstate和downloads
vim project-spec/meta-user/conf/petalinuxbsp.conf
增加以下几行,路径按自己的路径配置
DL_DIR = "/opt/xilinx/p211/downloads"
SSTATE_DIR = "/opt/xilinx/p211/sstate/aarch64"
RM_WORK_EXCLUDE += "linux-xlnx"
RM_WORK_EXCLUDE += "u-boot-xlnx"
3) 配置内核
petalinux-config -c kernel
Device Drivers ->USB support -> USB Gadget Support
Device Drivers ->USB support ->USB Gadget Support -> USB Gadget precomposed configurations
Device Drivers -> Multimedia support -> Media drivers->V4L test drivers
4) 修改设备树
vim project-spec/meta-user/recipes-bsp/device-tree/files/system-user.dtsi
/include/ "system-conf.dtsi"
&usb0 {
? status = "okay";
};
&dwc3_0 {
? status = "okay";
? dr_mode = "peripheral";
};
二、在petalinux下添加uvc-gadget测试程序
1) 创建一个空应用程序,并用hello world模板填充
这个命令会在project-spec/meta-user/recipes-apps/目录下生成一个uvc-gadget的文件夹,并创建对应的Makefile和空的c模板文件
petalinux-create-tapps-nuvc-gadget--enable
uvv-gadget是一个uvc测试的小程序
https://git.ideasonboard.org/uvc-gadget.git
我们把源码clone到uvc-gadget/files/目录,可能会提示files目录非空,这是git的问题。
直接先删除这个目录,git clone以后会自动重新生成
rm -rf project-spec/meta-user/recipes-apps/uvc-gadget/files
git clone https://github.com/wlhe/uvc-gadget.git? project-spec/meta-user/recipes-apps/uvc-gadget/files/
3) 编辑recipe文件,把应用加到petalinux最终生成的镜像中
这一步是petalinux独有的,目的是把生成的二进制文件加到rootfs的对应位置
这样不需要再手动把文件复制到rootfs
vim project-spec/meta-user/recipes-apps/uvc-gadget/uvc-gadget.bb
SUMMARY = "Simple uvc-gadget application"
SECTION = "PETALINUX/apps"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
SRC_URI = "file://uvc-gadget.c \
file://uvc.h \
file://Makefile \
"
#INHIBIT_PACKAGE_STRIP = 1
S = "${WORKDIR}"
do_compile() {
? ? oe_runmake
}
do_install() {
? ? install -d ${D}${bindir}
? ? install -m 0755 uvc-gadget ${D}${bindir}
}
4) 修改Makefile
github下载的源码是针对x86的,我们在嵌入式里面,需要修改对应 的Makefile文件
$vim project-spec/meta-user/recipes-apps/uvc-gadget/files/Makefile
APP = uvc-gadget
APP_OBJS = uvc-gadget.o
all: $(APP)
$(APP): $(APP_OBJS)
$(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS)
clean:
-rm -f $(APP) *.elf *.gdb *.o
5) 编译Petalinux,生成镜像文件
依次运行以下命令编译并打包
$petalinux-build
$cd images/linux
$petalinux-package --boot --fsbl zynqmp_fsbl.elf --u-boot u-boot.elf --pmufw pmufw.elf --fpga system.bit --force
6) 对sd卡分区,并把文件复制到sd卡
然后把images/linux目录下的BOOT.BIN、image.ub、boot.scr复制到sd卡的分区1
$cp boot.scr /media/xxx/BOOT/
$cp BOOT.BIN /media/xxx/BOOT/
$cp image.ub /media/xx/BOOT/
把rootfs复制到sd卡的分区2
$sudo dd if=rootfs.ext4 of=/dev/sdc2
三、板端测试
modprobe g_ffs.ko
modprobe g-webcam streaming_maxburst=15 streaming_maxpacket=3072
modprobe vivid
uvc-gadget -u /dev/video0 -v /dev/video1 -t 15 -r 0 -n 2 &
root@xilinx-zcu104-2021_1:~# modprobe g_ffs.ko
[? 40.744693] file system registered
root@xilinx-zcu104-2021_1:~# modprobe g-webcam streaming_maxburst=15 streaming_maxpacket=3072 streaming_interval=2
[? 43.871356] g_webcam gadget: uvc: uvc_function_bind()
[? 43.876605] g_webcam gadget: Webcam Video Gadget
[? 43.881234] g_webcam gadget: g_webcam ready
root@xilinx-zcu104-2021_1:~# modprobe vivid
[? 49.702111] vivid-000: using single planar format API
[? 49.709263] vivid-000: CEC adapter cec0 registered for HDMI input 0
[? 49.715639] vivid-000: V4L2 capture device registered as video1
[? 49.721753] vivid-000: CEC adapter cec1 registered for HDMI output 0
[? 49.728238] vivid-000: V4L2 output device registered as video2
[? 49.734256] vivid-000: V4L2 capture device registered as vbi0, supports raw and sliced VBI
[? 49.742645] vivid-000: V4L2 output device registered as vbi1, supports raw and sliced VBI
[? 49.750999] vivid-000: V4L2 capture device registered as swradio0
[? 49.757213] vivid-000: V4L2 receiver device registered as radio0
[? 49.763395] vivid-000: V4L2 transmitter device registered as radio1
[? 49.769790] vivid-000: V4L2 metadata capture device registered as video3
[? 49.776657] vivid-000: V4L2 metadata output device registered as video4
[? 49.783437] vivid-000: V4L2 touch capture device registered as v4l-touch0
root@xilinx-zcu104-2021_1:~# uvc-gadget -u /dev/video0 -v /dev/video1 -t 15 -r 0 -n 2 &
[1] 1111
Requested Burst value = 15
Number of buffers requested = 2
V4L2 device is vivid on bus platform:vivid-000
V4L2: Getting current format: YUYV 640x360
V4L2: Setting format to: YUYV 640x360
V4L2: Getting current format: YUYV 640x360
v4l2 open succeeded, file descriptor = 3
uvc device is dwc3-gadget on bus gadget
uvc open succeeded, file descriptor = 4
V4L2: Buffer 0 mapped at address 0xffffa4b38000.
V4L2: Buffer 1 mapped at address 0xffffa4ac7000.
V4L2: 2 buffers allocated.
打开设备管理器,可以看到在"照相机"下多了一个"UVC Camera"
安装potplayer就可以打开了
https://potplayer.daum.net/