目录
下面是一篇以 CSDN 文章形式记录的内容总结,补充了一些细节供参考:
Zephyr 在 Linux 下编译 native_sim 目标及蓝牙调试详解
近期在开发过程中,遇到在 Linux 环境下编译 Zephyr 的 native_sim 目标板程序的问题。本文将对编译、生成程序、初始化时可能遇到的错误及其解决方案,以及常用蓝牙操作命令进行详细记录和补充,希望对大家在使用 Zephyr 项目时有所帮助。
1. 编译 native_sim 目标板程序
在 Linux 环境下编译 Zephyr 的 native_sim 目标程序时,需要注意目标板的架构与系统环境的对应关系:
- 当 Linux 系统为 64 位时,目标板需选用:
native_sim/native/64
。
编译成功之后,会生成 zephyr.exe
可执行程序。
2. 初始化蓝牙时的错误及排查
在执行 bt_enable
初始化过程中,常通过以下代码段来启动蓝牙模块:
err = bt_enable(NULL);
if (err) {
printk("Bluetooth init failed (err %d)\n", err);
return 0;
}
在实际操作中,根据返回的错误码可以大致判断出错位置。以下是一些常见错误码及其含义(部分错误码定义如下):
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
在测试过程中还遇到如下错误情况:
-
错误码 -19
“没有指定设备”。该错误说明在执行蓝牙初始化时,没有指定蓝牙驱动设备。解决方案是通过参数--bt-dev=hciN
来指定,其中hciN
代表需要使用的具体电脑端驱动设备编号。 -
错误码 -1
“没有操作权限”。这是由于当前用户权限不足,导致无法执行蓝牙相关操作。解决方案为使用管理员权限:
sudo ./build/zephyr/zephyr.exe --bt-dev=hci0
。 -
错误码 -16
“蓝牙设备被占用”。此错误主要由系统中的蓝牙服务(bluetooth.service)占用了蓝牙资源造成。需要先关闭蓝牙服务才能正常初始化蓝牙模块。
3. 常用蓝牙操作命令
为了更好地调试和管理蓝牙设备,在 Linux 系统上可以使用以下常用命令:
-
管理 Bluetooth 服务
停止蓝牙服务:sudo systemctl stop bluetooth.service
启动蓝牙服务:
sudo systemctl start bluetooth.service
检查当前蓝牙服务状态:
systemctl status bluetooth.service
若发现蓝牙服务配置为开机自启,则需要禁用自动启动来避免服务自动激活:
禁用自动启动:sudo systemctl disable bluetooth.service
使能自动启动(如果需要):
sudo systemctl enable bluetooth.service
-
控制蓝牙硬件
关闭蓝牙硬件:sudo hciconfig hci0 down
开启蓝牙硬件:
sudo hciconfig hci0 up
-
管理用户与蓝牙组
将当前用户添加到 bluetooth 组中,以便拥有蓝牙操作权限:sudo usermod -aG bluetooth $USER
若要将用户从 bluetooth 组中移除:
sudo gpasswd -d $USER bluetooth
注意:修改用户组后可以使用如下命令使设置立即生效,或者直接重启系统:
newgrp bluetooth
-管理蓝牙开关
查询无线设备开关状态:
如果 bluetooth soft blocked 选项为yes,则需要将蓝牙设备打开。对应ubuntu上的开关蓝牙操作。
sudo rfkill list
0: ideapad_wlan: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
2: ideapad_bluetooth: Bluetooth
Soft blocked: no
Hard blocked: no
3: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
开启蓝牙功能
sudo rfkill unblock bluetooth
关闭蓝牙功能
sudo rfkill block bluetooth
4. 使用native_sim 运行蓝牙功能的配置
-配置用户group 权限
sudo usermod -aG bluetooth $USER
newgrp bluetooth
-配置蓝牙开关
sudo rfkill unblock bluetooth
也可以直接在ubuntu 面板上打开拉那样开关
-关闭蓝牙服务
sudo systemctl disable bluetooth.service #禁止蓝牙服务开机启动
sudo systemctl stop bluetooth.service #关闭蓝牙相关的服务
sudo hciconfig down #确保zephyr 独占蓝牙硬件
5.网卡驱动安装
当使用较新的网卡时,当前的ubuntu 可能没有支持对应的蓝牙驱动、wifi驱动。
更新网卡驱动的三个核心点:
-linux kernel 版本
查看当前的linxu kernel 版本,确认当前版本是否包含该网卡驱动。 如果包含理论上网卡是可以正常工作的。
-linux firmware
由于系统自带的 linux-firmware 包可能版本较旧,需要手动克隆官方固件仓库,并获取最新的固件文件。
执行以下命令下载最新固件仓库:
git clone https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git
-
备份当前固件
为防止出现问题,先备份现有的固件目录:sudo cp -r /lib/firmware /lib/firmware.backup
-
替换系统固件
将指定提交的的linux-firmware 复制到 /lib/firmware/:sudo cp -r /path//firmware/* /lib/firmware/
-
更新 initramfs
更新系统的 initramfs,以便在下次启动时包含新的固件:sudo update-initramfs -u
-
重启系统
重启系统以加载新的固件:sudo reboot
-网卡的固件版本
部分咸鱼上流出的网卡,固件版本可能较低。 无法打上windows 版本的官方驱动,只能使用指定的驱动才能正常工作。
如果在linux 上始终,不要是用该网卡
5. 总结与补充
本文详细介绍了 Zephyr 在 Linux 下编译 native_sim 目标板程序时的编译要求、生成程序执行过程中可能遇到的蓝牙初始化错误及解决方案,同时列举了在 Linux 上配置和管理蓝牙服务、硬件以及用户组的常用命令。
关于配置过程中的一些潜在问题,建议大家在编译前确认系统环境与目标板的匹配,并注意蓝牙服务是否占用设备,以免影响调试。同时,使用管理员权限进行调试和添加用户进组都是必不可少的步骤。
希望本文能帮助大家在使用 Zephyr 开发过程中更好地排查问题和调试蓝牙模块。如果有任何疑问或改进建议,欢迎留言讨论!