在进行开发工作时,我们经常会遇到需要进行远程调试的情况,或者需要进行远程烧录。这时候,一个能够通过网络连接的USB接口将会变得非常有用。它不仅能够让我们方便地通过网络连接到设备进行调试,还可以通过网络进行远程烧录操作。这样的设计大大提高了开发效率,因为我们不再局限于物理距离的限制,可以随时随地对设备进行调试和更新,从而加速了开发和测试的过程。此外,通过网络连接的USB接口还能够提高工作灵活性,尤其是在多人协作开发环境中,远程调试和烧录可以让团队成员更方便地分享资源和信息。
我之前用过的方案就是利用开源的usbip实现这样的功能,现在的发行版基本就将usbip集成进内核了。不过对于本次用到的开发版,需要手动移植一个进Linux内核。
移植USBIP
这个在米尔官方的yocto的环境中是没有这个配方的,openembedded网站上倒是有,但是似乎比较老的版本了,用了老版本的yocto python语句编译过不了,所以我们为其写一个配方
SUMMARY = "userspace usbip from Linux kernel tools"
DESCRIPTION = " USB/IP protocol allows to pass USB device from server to \
client over the network. Server is a machine which provides (shares) a \
USB device. Client is a machine which uses USB device provided by server \
over the network. The USB device may be either physical device connected \
to a server or software entity created on a server using USB gadget subsystem."
LICENSE = "GPL-2.0"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/GPL-2.0;md5=801f80980d171dd6425610833a22dbe6"
DEPENDS = "virtual/kernel udev"
PROVIDES = "virtual/usbip-tools"
inherit kernelsrc autotools-brokensep
do_configure[depends] += "virtual/kernel:do_shared_workdir"
# We need to set S, for not being set to STAGING_KERNEL_DIR, and by that
# be wiped when we prune dest below. We just set it to usbip-tools-1.0
S = "${WORKDIR}/${BP}"
# Copy the source files from KERNEL/tools/usb/usbip to ${S}
do_configure[prefuncs] += "copy_usbip_source_from_kernel"
python copy_usbip_source_from_kernel() {
dir_in_kernel = "tools/usb/usbip"
src_dir = d.getVar("STAGING_KERNEL_DIR")
src = oe.path.join(src_dir, dir_in_kernel)
dest = d.getVar("S")
bb.utils.mkdirhier(dest)
bb.utils.prunedir(dest)
# copy src to dest folder
if not os.path.exists(src):
bb.fatal("Path does not exist: %s. Maybe dir_in_kernel does not match the kernel version." % src)
if os.path.isdir(src):
oe.path.copyhardlinktree(src, dest)
else:
src_path = os.path.dirname(src)
os.makedirs(os.path.join(dest,src_path),exist_ok=True)
bb.utils.copyfile(src, dest)
}
# Use local scripts before relying on inherited autotools
do_configure () {
# We are in ${B} - equal to ${S}, so just run the scripts
./cleanup.sh || bbnote "${PN} failed to cleanup.sh"
./autogen.sh || bbnote "${PN} failed to autogen.sh"
oe_runconf
}
# As usbip integrate with the kernel module, we set this package to be build specific for
# this machine, and not generally for the architecture
PACKAGE_ARCH = "${MACHINE_ARCH}"
# Even though the libusbip is set to version 0.0.1, set the package version to match kernel
# e.g. usbip-tools-5.14.21-r0.qemux86_64.rpm for qemu package using kernel 5.14.21
do_package[prepend] += "python () { d.setVar('PKGV', d.getVar("KERNEL_VERSION").split('-')[0]); }"
然后再将其添加进镜像配方去(原谅我懒,就不写个新的镜像配方了,直接改myir-image-core)

但是这还不够,我们需要相应内核模块支持。配置内核启用usbip相关模块,如下图。

之后再编译。
编译通过

如何检测编译出的镜像是否包含自己想要的软件
这个需求源自于开发时我们不能总是要等到烧录好了再输出命令检查软件是否存在,况且软件一多一个一个手动检查很是浪费时间。这里整理了一下我学习到的检测软件否包含在镜像的方法。源自stackexchange和yocto文档。
方法1:从根本出发
充分理解yocto工程的结构后,显然可知,我们只需检查对应的bblayer.conf中BBLAYERS变量是否包含所需的层,再检查对应镜像配方中IMAGE_INSTALL及其相关变量是否有有想要的软件包。
方法2:借助bitbake
就相当于代码版的方法1,使用如下命令可以列出全部配方,配合grep命令可以检查是否包含有想要的配方
bitbake-layers show-recipes
另一方面可以通过以下命令列出目标镜像将会安装的配方,进一步调整grep命令的正则表达式可以直接确定自己想要添加的配方是否会真的添加进镜像。
bitbake -e 目标镜像名 | grep ^IMAGE_INSTALL
方法3检查编译好的镜像
这个方法有点亡羊补牢,但也是个过程中检查的方法,简单来说就是把要用来烧写的镜像文件挂载在自己的电脑上,搜索是否有想要的软件。以usbip为例,具体操作如下:
首先,编译出来是没有烧写要用的.wic文件的,它被压缩在了同名的.wic.bz2中,另一方面.wic.bz2实质上被硬链接到了.rootfs.wic.bz2文件上,所以我们直接拷贝出来解压它即可
cp tmp/deploy/images/myd-y6ull14x14/myir-image-core-myd-y6ull14x14-20240613234526.rootfs.tar.bz2 .
bunzip2 -k myir-image-core-myd-y6ull14x14-20240613234526.rootfs.wic.bz2
然后我们只需将解压得到的wic镜像挂载即可
sudo losetup -fP myir-image-core-myd-y6ull14x14-20240613234526.rootfs.wic
这会为 .wic 文件创建一个环回设备,例如 /dev/loop0,并自动分配分区设备,例如 /dev/loop0p1,/dev/loop0p2 等。
找到你要挂载的分区设备并进行挂载。例如,如果我们要挂载第一分区 /dev/loop0p1:
sudo mount /dev/loop0p1 /mnt/wic
然后直接检查是否安装所需软件即可,这里我们是usbip,方法之一是检查安装目录sbin/只需
ls /mnt/wic/usr/sbin/ | grep usbip
检查完内容后,卸载挂载点并释放环回设备
sudo umount /mnt/wic
sudo losetup -d /dev/loop0
其实其他方法还有很多,我这里不再赘述,凭记忆写的可能难免与实际有些误差,请读者阅读时注意辨别。
如何使用USBIP
首先是了解自己的IP,先前我们已经使用systemd配置了静态IP,可以再看一下当前IP(内网,仅做测试用途)

先确保内核模块已加载
modprobe usbip-core
modprobe usbip-host
启动守护进程
usbipd -D
然后添加绑定
usbip bind --busid 1-1.1

设备插入时刚好有debug信息在调试串口打印,省去对应USB端口的功夫,当然也可以usbip list -l 然后记下BUSID。

最后启动USBIP服务即可。
使用时连接端是从机(注意打开防火墙对应端口,默认3240),可列出主机可用,再根据BUSID连接
usbip list -r 服务端IP
usbip attach -r 服务端 -b BUSID
如下图,成功连接开发板上1号USB接口的设备hackrf,不过由于开发板没有hackrf的驱动,无法正确识别,但是不妨碍我们使用usbip将USB接口转接到当前电脑使用

从设备管理器上也可以看到hackrf设备

可以正常使用设备

后台debug串口消息

用USBIP实现网络SDR效果展示

稍微有点卡,估计主要是因为USB2.0传输带宽不够

全部评论