跳到主要内容
版本:Latest

主机功能使用指南

本文档介绍 CherryUSB Host Class Driver 的使用方法,涵盖 MSC 大容量存储、HID 键盘、CDC ACM 串口、USB Hub、UAC 音频、CDC ECM 虚拟网卡和 GSM/Cat1(EC801)模块的配置、编译和使用步骤。开发者只需通过 Kconfig 配置使能对应功能,系统即可自动识别插入的 USB 设备并加载相应驱动。

术语​

本文档使用的专业术语定义请参阅《CherryUSB 入门概述》的术语章节。

概述​

CherryUSB Host class driver 支持 Allwinner 开发板作为 USB Host,通过 EHCI + OHCI 控制器驱动自动识别插入的 USB 设备并加载对应 Class 驱动。

全速/低速设备插入后,EHCI 通过端口转交(port owner / companion controller)将其交给 OHCI 处理,开发者无需额外配置。

本文档收录了以下 USB Host Class Driver 的使用指南:

功能章节USB Class典型应用
MSC 大容量存储MSC 大容量存储功能使用USB MSC 1.0插入 U 盘后自动挂载,进行文件读写
HID 键盘HID 键盘功能使用USB HID 1.11接入 USB 键盘,获取按键事件
CDC ACM 串口CDC ACM 串口功能使用USB CDC 1.2接入 USB 串口设备,进行数据收发
USB HubUSB Hub 功能使用USB 2.0 Hub扩展 USB 端口,可级联(受 Host 资源上限约束)
UAC 音频UAC 音频功能使用USB Audio 1.0接入 USB 耳机、声卡、麦克风,播放/录音
CDC ECM 虚拟网卡CDC ECM 虚拟网卡功能使用USB CDC ECM接入 ECM 网卡/模块,通过 lwIP 联网
GSM / Cat1(EC801)GSM / Cat1(EC801)功能使用USB CDC ACM + AT接入 Quectel EC801 等 4G Cat1 模块

启用任意 Host Class Driver 的通用流程为:板级配置 → 使能 Host 模式 → 选择 Class Driver → 使能 Host 控制器 → 编译烧录 → 插入设备自动识别。以下各章分别说明每个驱动的操作步骤。

日志等级

本文日志示例默认按 CONFIG_USB_DBG_LEVEL=2(INFO)采集,各代码块会标注其所需等级。SDK 默认等级为 1(WARNING),只输出 [E/...] 错误和 [W/...] 警告,[I/...] 日志不会打印;需要查看枚举、类驱动加载等日志时,将 aw_usbconfig.h 中的 CONFIG_USB_DBG_LEVEL 改为 2(INFO)或 3(DEBUG)后重新编译。标注为 CONFIG_USB_DBG_LEVEL=1 的日志在默认等级下即可看到。

MSC 大容量存储功能使用​

功能介绍​

MSC (Mass Storage Class) 支持开发板作为 USB Host 识别 U 盘等大容量存储设备。U 盘插入后,系统自动完成设备枚举和 MSC 驱动加载,并将 U 盘分区挂载到文件系统,可进行文件读写操作。

配置使用​

板级配置​

在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。

进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:

备注

CherryUSB Port Sunxi Configuration 与 CherryUSB Configuration 在 thirdparty 菜单下为同级项(两者均直接挂在 > System components > thirdparty components 下),并不在 Enable usb host mode 内。需先进入 CherryUSB Configuration → Enable usb host mode 选择 Host IP 为 sunxi,再返回 > System components > thirdparty components 主菜单进入 CherryUSB Port Sunxi Configuration 使能 Host 控制器和测试选项。

步骤一:使能 Host 模式并选择 MSC 驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb msc driver # 使能 MSC 驱动

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration(选择 sunxi host IP 后此选项变为可见):

[*] CherryUSB Port Sunxi Configuration --->
--- CherryUSB Port Sunxi Configuration
[*] enable arch sunxi support
[*] enable arch sunxi host controller support
[*] enable arch sunxi host ohci driver
[*] enable arch sunxi usb0 ohci-hcd
[*] enable arch sunxi host ehci driver
[*] enable arch sunxi usb0 ehci-hcd
[*] enable cherryusb test
配置项路径必选/可选
CHERRYUSB_HOSTCherryUSB → Enable usb host mode必选
CHERRYUSB_HOST_SUNXIEnable usb host mode → Select usb host ip → sunxi必选
CHERRYUSB_HOST_MSCEnable usb host mode → Enable usb msc driver必选
CHERRYUSB_PORT_SUNXI> System components > thirdparty components → CherryUSB Port Sunxi Configuration必选
ARCH_SUNXI_USB0_OHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd必选
ARCH_SUNXI_USB0_EHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd必选
CHERRYUSB_TESTCherryUSB Port Sunxi Configuration → enable cherryusb test可选(仅在需要使用 usb_test 手动加载/卸载控制器时使能)

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

  1. 开发板上电,进入系统 Shell
  2. 确认 USB Host 已初始化:使能 Host 模式后系统启动阶段会自动初始化 USB Host;如需手动加载可执行 usb_test hci -i 0
  3. 将 U 盘插入开发板的 USB A 口(Host 口)
  4. 系统自动检测 U 盘,完成枚举和 MSC 驱动加载
  5. U 盘分区自动挂载到 /mnt/sdap0

启动日志如下(CONFIG_USB_DBG_LEVEL=2):

{sunxi-ohci0}usbh_hcd_add 525: add
{sunxi-ehci0}usbh_hcd_add 525: add
{sunxi-ehci0}ehci_start 3807: Start Asynchronous List Base Address: 40124a80
{sunxi-ehci0}ehci_start 3811: Start Periodic Frame List Base Address: 40124000

U 盘插入后:
uart>{sunxi-ehci0}ehci_irq_handler 3499: connect present
[I/usbh_hub] New high-speed device on Bus 0, Hub 1, Port 2 connected
[I/usbh_core] New device found,idVendor:0930,idProduct:6545,bcdDevice:0110
[I/usbh_core] The device has 1 bNumConfigurations
[I/usbh_core] The device has 1 interfaces
[I/usbh_core] Enumeration success, start loading class driver
[I/usbh_core] Loading msc class driver on interface 0
[I/usbh_msc] Get max LUN:1
[I/usbh_msc] Capacity info:
[I/usbh_msc] Block num:60481536,block size:512
mount usb mass storage successull!!
usbmsc blkdev:SCSI_DISK_sdap0 mount on /mnt/sdap0
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 290: blkinfo.hiddennum 0
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 291: blkinfo.headnum 2048
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 292: blkinfo.secnum 60477440
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 293: blkinfo.secsize 512
[I/USB] {SCSI_DISK_sda}usbh_msc_connect 398: Register MSC Class:/dev/sda

uart>ll
drwxrwxrwx root root 0 KB dev
drwxrwxrwx root root 0 KB res
drwxrwxrwx root root 0 KB data
drwxrwxrwx root root 0 KB mnt/sdap0
uart>

日志关键行说明:

日志内容含义
{sunxi-ohci0}usbh_hcd_add 525: addHost 控制器驱动添加成功(HCD 名称和行号随控制器/源码版本变化)
Enumeration success, start loading class driverU 盘枚举成功,开始加载 MSC 驱动
mount usb mass storage successullU 盘分区挂载成功(源码即为此拼写)
Register MSC Class:/dev/sdaMSC 块设备注册成功

MSC 缓存与带宽测试(可选)​

Host MSC 驱动默认带一层读预取 + 写回缓存(平台层 awusb_blk_cache),可显著提升顺序读写性能。在 menuconfig Enable usb host mode → Enable usb msc driver 下有如下子项:

配置项默认说明
CHERRYUSB_HOST_MSC_SUPPORT_CACHE开MSC 缓存总开关
CHERRYUSB_HOST_MSC_CACHE_SECTOR_NR256每组缓存扇区数
CHERRYUSB_HOST_MSC_CACHE_GROUP_NR8缓存组数
CHERRYUSB_HOST_MSC_SUPPORT_BWTEST关使能块读写带宽测试命令(msc_bwtest / msc_stats / msc_cache_test)

开启 CHERRYUSB_HOST_MSC_SUPPORT_BWTEST 并插入 U 盘后,可执行以下命令评估 MSC 读写性能:

msc_bwtest # 对 MSC 块设备做 RAW 顺序读写带宽测试(绕过缓存)
msc_stats # 查看缓存写回(WRITE10)统计
msc_cache_test # MSC 缓存几何配置(总量 x 分组)扫描
注意

msc_bwtest 和 msc_cache_test 的写测试会绕过文件系统与缓存,直接覆盖分区末尾 16MB 安全窗口内的真实扇区,该区域原有数据将被破坏。请勿在存放重要数据的分区上执行,测试前做好数据备份。


HID 键盘功能使用​

功能介绍​

HID (Human Interface Device) 键盘功能支持开发板作为 USB Host 接入 USB 键盘,获取按键事件。键盘通过 HID 协议上报按键码,系统可通过 usbhkbd 命令进行键盘打开、关闭和 LED 控制等操作。

配置使用​

板级配置​

在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。

进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:

备注

CherryUSB Port Sunxi Configuration 与 CherryUSB Configuration 在 thirdparty 菜单下为同级项(两者均直接挂在 > System components > thirdparty components 下),并不在 Enable usb host mode 内。需先进入 CherryUSB Configuration → Enable usb host mode 选择 Host IP 为 sunxi,再返回 > System components > thirdparty components 主菜单进入 CherryUSB Port Sunxi Configuration 使能 Host 控制器和测试选项。

步骤一:使能 Host 模式并选择 HID 键盘驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb hidkbd driver # 使能 HID 键盘驱动
Select USB host template, please select class driver first --->
[*] demo for test hidkbd # 使能 usbhkbd 测试命令

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration:

[*] CherryUSB Port Sunxi Configuration --->
--- CherryUSB Port Sunxi Configuration
[*] enable arch sunxi support
[*] enable arch sunxi host controller support
[*] enable arch sunxi host ohci driver
[*] enable arch sunxi usb0 ohci-hcd
[*] enable arch sunxi host ehci driver
[*] enable arch sunxi usb0 ehci-hcd
[*] enable cherryusb test
配置项路径必选/可选
CHERRYUSB_HOSTCherryUSB → Enable usb host mode必选
CHERRYUSB_HOST_SUNXIEnable usb host mode → Select usb host ip → sunxi必选
CHERRYUSB_HOST_HIDKBDEnable usb host mode → Enable usb hidkbd driver必选
TEST_USBH_HIDKBDEnable usb host mode → Select USB host template → demo for test hidkbd必选(提供 usbhkbd 命令)
CHERRYUSB_PORT_SUNXI> System components > thirdparty components → CherryUSB Port Sunxi Configuration必选
ARCH_SUNXI_USB0_OHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd必选
ARCH_SUNXI_USB0_EHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd必选
CHERRYUSB_TESTCherryUSB Port Sunxi Configuration → enable cherryusb test可选(仅在需要使用 usb_test 手动加载/卸载控制器时使能)

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

  1. 开发板上电,进入系统 Shell
  2. 确认 USB Host 已初始化:使能 Host 模式后系统启动阶段会自动初始化 USB Host;如需手动加载可执行 usb_test hci -i 0
  3. 将 USB 键盘插入开发板的 USB A 口
  4. 系统自动识别键盘

键盘插入后(全速设备由 EHCI 转交给 OHCI 处理),执行以下命令控制键盘:

# 查看可用命令
usbhkbd

# 打开键盘设备(id 为 0)
usbhkbd 0 open

# 关闭键盘设备
usbhkbd 0 close

# 设置键盘 LED(如 Caps Lock、Num Lock 等)
usbhkbd 0 evtled 0x1f

启动日志如下(CONFIG_USB_DBG_LEVEL=1):

{sunxi-ehci0}ehci_irq_handler 3499: connect present
[E/USB] {sunxi-ehci0}usbh_reset_port 226: reset timeout
{sunxi-ehci0}ehci_relinquish_port 3287: hwport_id 0 full/low speed --> companion
{sunxi-ehci0}ehci_irq_handler 3507: disconnect present
{sunxi-ohci0}ohci_irq_handler 2202: connect present
[W/usbh_hub] Failed to enable port 2
usbh_hidkbd_connect 331: Found USB HIDBP Keyboard 046d:c31d -> kbd0

> usbhkbd 0 open
find keyboard kbd0...
awusb_input_event 73: kbd0, type 1 code 029 val 0
awusb_input_event 73: kbd0, type 1 code 042 val 0
...

日志关键行说明:

日志内容含义
reset timeout + full/low speed --> companion全速设备由 EHCI 转交给 OHCI 处理,属于正常现象
[W/usbh_hub] Failed to enable port 2该警告不影响键盘使用,属于正常现象
Found USB HIDBP Keyboard ... -> kbd0键盘识别成功,设备名为 kbd0

usbhkbd 命令说明:

命令说明
usbhkbd查看可用命令及使用帮助
usbhkbd <id> open打开指定键盘设备
usbhkbd <id> close关闭指定键盘设备
usbhkbd <id> evtled <value>设置键盘 LED 状态

CDC ACM 串口功能使用​

功能介绍​

CDC ACM (Communication Device Class - Abstract Control Model) 使开发板作为 USB Host 能够接入符合 CDC ACM 标准的 USB 串口设备(如 STM32 虚拟串口),并进行数据收发。CH340、CP210x、PL2303、FTDI 等厂商专用转串口芯片属于厂商自定义类,不是 CDC ACM 设备,需要接入对应的厂商驱动,当前构建默认未编译这些驱动。系统自动识别串口设备后,可通过测试命令进行数据的读写验证。

配置使用​

板级配置​

在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。

进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:

备注

CherryUSB Port Sunxi Configuration 与 CherryUSB Configuration 在 thirdparty 菜单下为同级项(两者均直接挂在 > System components > thirdparty components 下),并不在 Enable usb host mode 内。需先进入 CherryUSB Configuration → Enable usb host mode 选择 Host IP 为 sunxi,再返回 > System components > thirdparty components 主菜单进入 CherryUSB Port Sunxi Configuration 使能 Host 控制器和测试选项。

步骤一:使能 Host 模式并选择 CDC ACM 驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb cdc acm driver # 使能 CDC ACM 驱动
...
Select USB host template, please select class driver first --->
[*] demo for test serial # 使能串口测试命令

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration:

[*] CherryUSB Port Sunxi Configuration --->
--- CherryUSB Port Sunxi Configuration
[*] enable arch sunxi support
[*] enable arch sunxi host controller support
[*] enable arch sunxi host ohci driver
[*] enable arch sunxi usb0 ohci-hcd
[*] enable arch sunxi host ehci driver
[*] enable arch sunxi usb0 ehci-hcd
[*] enable cherryusb test
配置项路径必选/可选
CHERRYUSB_HOSTCherryUSB → Enable usb host mode必选
CHERRYUSB_HOST_SUNXIEnable usb host mode → Select usb host ip → sunxi必选
CHERRYUSB_HOST_CDC_ACMEnable usb host mode → Enable usb cdc acm driver必选
TEST_USBH_SERIALEnable usb host mode → Select USB host template → demo for test serial可选(使能测试命令)
CHERRYUSB_PORT_SUNXI> System components > thirdparty components → CherryUSB Port Sunxi Configuration必选
ARCH_SUNXI_USB0_OHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd必选
ARCH_SUNXI_USB0_EHCICherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd必选
CHERRYUSB_TESTCherryUSB Port Sunxi Configuration → enable cherryusb test可选(仅在需要使用 usb_test 手动加载/卸载控制器时使能)

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

  1. 开发板上电,进入系统 Shell
  2. 确认 USB Host 已初始化:使能 Host 模式后系统启动阶段会自动初始化 USB Host;如需手动加载可执行 usb_test hci -i 0
  3. 将符合 CDC ACM 标准的 USB 串口设备插入开发板的 USB A 口
  4. 系统自动识别串口设备

设备识别后,执行以下命令进行串口通信测试:

# 打开串口设备
usbh_acm_test_open

# 发送数据
usbh_acm_test_write 111

# 读取数据
usbh_acm_test_read

启动日志如下(CONFIG_USB_DBG_LEVEL=2):

{sunxi-ehci0}ehci_irq_handler: connect present
[I/usbh_core] New device found,idVendor:0483,idProduct:5740,bcdDevice:0200
[I/usbh_core] Enumeration success, start loading class driver
[I/usbh_core] Loading cdc_acm class driver on interface 0

> usbh_acm_test_open
> usbh_acm_test_write 111
> usbh_acm_test_read
usbh acm read:(3)
31 31 31

日志关键行说明:

日志内容含义
Enumeration success, start loading class driver串口设备枚举成功
Loading cdc_acm class driver on interface 0CDC ACM 驱动加载成功
usbh acm read:(3) / 31 31 31读取到 3 字节数据(ASCII "111")

USB Hub 功能使用​

功能介绍​

CherryUSB 内置 USB 2.0 Hub 类驱动。开发板作为 Host 时,可通过外接 USB Hub 扩展端口,并支持 Hub 级联(级联层数受 Host 资源上限约束)。Hub 下游设备由 Host Core 自动枚举,并按 Class 自动加载 MSC、HID 键盘(HIDKBD)、CDC ACM、UVC、UAC 等驱动,使用方式与直接插入根端口一致。

暂不支持 siTD(split isochronous)通信

全速/低速设备接在高速 Hub 后面时,等时传输需要由 EHCI 通过 split transaction(siTD)经 Hub 转发。当前驱动未实现 siTD,ehci_iso_init_sitd() 直接返回 -USB_ERR_NOTSUPP,因此全速/低速的 UAC 音频、UVC 摄像头等使用等时传输的设备不能经 Hub 使用。

高速等时设备,以及经 Hub 转发的控制、批量、中断传输使用 QH 的 split transaction,已支持,不受影响;全速/低速设备直接插在根端口时由 OHCI 处理,也不受此限制。

配置使用​

板级配置​

在板级配置 sys_config.fex 中设置 usb_port_type = 1,将 USB 端口配置为 Host Only 模式。

CHERRYUSB_HOST_HUB(Enable usb hub driver)默认开启,通常无需额外配置;Host 控制器配置与 MSC 章节相同。

外接 Hub 的数量和端口数需要按实际拓扑配置,否则超出上限的 Hub 或端口无法使用。配置项位于 CherryUSB Port Sunxi Configuration 菜单:

[*] CherryUSB Port Sunxi Configuration --->
--- CherryUSB Port Sunxi Configuration
[*] enable arch sunxi support
[*] enable arch sunxi host controller support
...
(1) CherryUSB usbhost max export hubs
(4) CherryUSB usbhost max export hub ports
(4) CherryUSB usbhost max root hub ports
配置项menuconfig 提示语默认值说明
CHERRYUSB_USBHOST_MAX_EXTHUBSCherryUSB usbhost max export hubs1可同时接入的外接 Hub 数量(含级联)
CHERRYUSB_USBHOST_MAX_EHPORTSCherryUSB usbhost max export hub ports4单个外接 Hub 的最大端口数
CHERRYUSB_USBHOST_MAX_RHPORTSCherryUSB usbhost max root hub ports4根 Hub 的端口数
注意
  • CHERRYUSB_USBHOST_MAX_EXTHUBS 小于实际接入的 Hub 数量时,超出的 Hub 打印 Fail to alloc hub_class 并无法枚举;接入多个 Hub 或级联 Hub 时需相应调大。
  • CHERRYUSB_USBHOST_MAX_EHPORTS 小于 Hub 的实际端口数时,枚举该 Hub 时打印 Hub nports x overflow 并返回 -USB_ERR_NOMEM,整个 Hub 都不可用;应按所用 Hub 的最大端口数配置(如 7 口 Hub 至少配为 7)。
  • 级联层数超过 HUB_MAX_DEPTH(5)时打印 Hub depth(x) is overflow。
  • 以上日志均为 [E/...] 错误日志(CONFIG_USB_DBG_LEVEL=1)。

编译烧录​

完成配置后,编译烧录固件到开发板。

使用示例​

  1. 开发板上电,确认 USB Host 已初始化
  2. 将 USB Hub 插入开发板 USB A 口,再将 U 盘、键盘、串口等设备插入 Hub 端口
  3. 系统自动枚举 Hub 及其下游设备,日志出现 Hub 枚举和对应 Class 驱动加载信息(CONFIG_USB_DBG_LEVEL=2)
  4. 下游设备的使用方式与直接插入开发板一致(U 盘自动挂载、键盘事件、串口节点等)
提示

大功率设备(移动硬盘、部分 4G 模块)建议使用带外部供电的 Hub,避免根端口 VBUS 供电不足导致枚举失败或掉盘。

UAC 音频功能使用​

功能介绍​

UAC (USB Audio Class) Host 驱动支持 USB 音频设备的播放与录音,采用等时(isochronous)传输,枚举成功后注册 /dev/audioN 设备节点:

注意

Host 侧目前只支持 UAC 1.0。UAC 2.0 设备在枚举解析 Audio Control Header 时,驱动打印 NOT SUPPORT UAC2.0 NOW 并返回 -USB_ERR_NOTSUPP,设备无法使用。

  • speaker0:播放流,用于向 USB 耳机 / 声卡输出音频
  • mic0:录音流,用于从 USB 麦克风采集音频

配置使用​

板级配置​

在板级配置 sys_config.fex 中设置 usb_port_type = 1,将 USB 端口配置为 Host Only 模式。

步骤一:使能 Host 模式并选择 UAC 驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb audio driver, it is commercial charge # 使能 UAC 驱动
[*] Enable usb audio demo console commands (uac_list/uac_record/uac_play/uac_vol)

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration:

[*] CherryUSB Port Sunxi Configuration --->
--- CherryUSB Port Sunxi Configuration
[*] enable arch sunxi support
[*] enable arch sunxi host controller support
[*] enable arch sunxi host ohci driver
[*] enable arch sunxi usb0 ohci-hcd
[*] enable arch sunxi host ehci driver
[*] enable arch sunxi usb0 ehci-hcd
配置项路径必选/可选
CHERRYUSB_HOST_AUDIOEnable usb host mode → Enable usb audio driver, it is commercial charge必选
CHERRYUSB_HOST_AUDIO_DEMOEnable usb host mode → Enable usb audio demo console commands可选(提供 uac_* 命令)

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

  1. 将 USB 音频设备(耳机、声卡或麦克风)插入开发板 USB A 口
  2. 系统枚举成功后生成 /dev/audio0 设备节点
  3. 通过以下命令验证播放、录音和音量控制:
# 列出音频设备及其音频流
uac_list
uac_list /dev/audio1

# 录音:默认 5 秒,16 kHz / 16 bit,保存到 /mnt/F/record.wav
uac_record
uac_record 10 # 录制 10 秒
uac_record 10 /mnt/F/test.wav # 指定时长和文件

# 播放 WAV 文件(采样率/位深/声道取自 WAV 头)
uac_play /mnt/F/test.wav

# 查询 / 设置音量,或静音
uac_vol speaker0
uac_vol speaker0 60
uac_vol speaker0 mute
uac_vol speaker0 unmute

uac_list 输出示例:

uac_list: 2 stream(s) on /dev/audio0
idx name direction ch type
--- ---- --------- -- ----
0 speaker0 playback 2 0x0301
1 mic0 capture 1 0x0201
备注

uac_record 的默认保存路径为 /mnt/F/record.wav,使用前需确认该目录所在文件系统已挂载且可写;也可在命令中指定其他可写路径。

CDC ECM 虚拟网卡功能使用​

功能介绍​

CDC ECM (Ethernet Control Model) Host 驱动将 USB ECM 设备(USB 网卡、部分 4G/5G 模块的 ECM 口)注册为 lwIP 网卡,网卡名为 EX0。驱动内置 Allwinner lwIP 适配层(platform/lwip/usbh_aw_cdc_ecm_lwip.c),设备插入后自动完成 netif 注册:

  • 默认使用 DHCP 获取 IP
  • 也可通过 Kconfig 选择静态 IPv4 配置
  • 网卡链路状态由设备通知和收发数据共同确认,链路可用后自动启动 DHCP

配置使用​

板级配置​

在板级配置 sys_config.fex 中设置 usb_port_type = 1,将 USB 端口配置为 Host Only 模式。

步骤一:使能 Host 模式并选择 CDC ECM 驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb cdc ecm driver # 使能 CDC ECM 驱动
(32) CDC ECM tx pbuf queue depth ... # 默认 32
(1514) CDC ECM max ethernet frame size ... # 默认 1514
[ ] Use static IPv4 config instead of DHCP # 默认使用 DHCP

选择 Use static IPv4 config instead of DHCP 后,可继续配置:

[*] Use static IPv4 config instead of DHCP
(192.168.123.88) Static IPv4 address
(255.255.255.0) Static IPv4 netmask
(192.168.123.1) Static IPv4 gateway

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration,配置方式与 MSC 章节相同。

配置项路径必选/可选
CHERRYUSB_HOST_CDC_ECMEnable usb host mode → Enable usb cdc ecm driver必选
CHERRYUSB_HOST_CDC_ECM_TX_QUEUE_SIZEEnable usb cdc ecm driver → tx pbuf queue depth可选(默认 32)
CHERRYUSB_HOST_CDC_ECM_ETH_MAX_SIZEEnable usb cdc ecm driver → max ethernet frame size可选(默认 1514)
CHERRYUSB_HOST_CDC_ECM_STATIC_IPEnable usb cdc ecm driver → Use static IPv4 config instead of DHCP可选(默认关)
CHERRYUSB_HOST_CDC_ECM_STATIC_IPADDR / ..._NETMASK / ..._GATEWAY静态 IP 子选项选择静态 IP 时配置
备注

ECM 依赖 lwIP 协议栈。工程需已使能 lwIP,并为 PBUF_POOL_SIZE 预留足够 pbuf;CHERRYUSB_HOST_CDC_ECM_TX_QUEUE_SIZE 建议保持约为 PBUF_POOL_SIZE 的一半。适配层会在设备接入时调用 tcpip_stack_init() 初始化 lwIP 线程。

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

  1. 将 USB ECM 设备插入开发板 USB A 口
  2. 系统自动枚举并注册网卡,串口日志出现(CONFIG_USB_DBG_LEVEL=2):
[I/usbh_ecma] ecm tx mq depth: 32
[I/usbh_ecma] usb ecm netif up
  1. 使用平台提供的网络命令查看 EX0 网卡状态(DHCP 模式下等待获取到 IP):
ifconfig # 或工程中使能的等效网络查看命令
  1. 获取 IP 后,ping 网关或外网验证数据通路:
ping <gateway_ip> # 静态 IP 模式下默认为 192.168.123.1
ping 8.8.8.8 # 验证外网连通性(需对端已提供路由)
提示

若使用静态 IP 模式,请确保开发板 EX0 的地址与对端 ECM 设备处于同一网段,且网关指向对端;Kconfig 中的静态 IP 配置会在 netif 注册时解析,格式非法将拒绝拉起网卡并打印 invalid static ip config(CONFIG_USB_DBG_LEVEL=1)。

GSM / Cat1(EC801)功能使用​

功能介绍​

CherryUSB 通过 CDC ACM 串口承载 4G 模块的 AT 通道:CHERRYUSB_HOST_GSM 提供 GSM 设备识别,CHERRYUSB_HOST_GSM_EC801 在通用 AT 框架 at_frame 之上实现 Quectel EC801(Cat1)的初始化、注网、查询和原始 AT 收发。

  • EC801 默认 AT 设备节点:/dev/ttyUSB1(由 class/net/cat1/ec801.c 中的 acm_device_name 指定,枚举顺序不同时需要调整)

配置使用​

板级配置​

在板级配置 sys_config.fex 中设置 usb_port_type = 1,将 USB 端口配置为 Host Only 模式。

步骤一:使能 Host 模式并选择 GSM / EC801 驱动

> System components > thirdparty components
[*] CherryUSB Configuration --->
[*] Enable usb host mode --->
--- Enable usb host mode
Select usb host ip, and some ip need config in usb_config.h, please check (sunxi) --->
[*] Enable usb cdc ecm driver # 使能 CDC ECM 驱动
(32) CDC ECM tx pbuf queue depth ... # 默认 32
(1514) CDC ECM max ethernet frame size ... # 默认 1514
[ ] Use static IPv4 config instead of DHCP # 默认使用 DHCP
[*] Enable usb cdc acm driver # 提供 usbh_serial 核心与 /dev/ttyUSB*
[*] Enable usb gsm driver for 4g module # 使能 GSM 驱动
[*] Enable Quectel EC801 (Cat1) module AT support # 使能 EC801,自动 select COMPONENTS_AT_FRAME
(8) CherryUSB usbhost max interfaces # 最大接口数需要大于5

步骤二:使能 Host 控制器

返回 > System components > thirdparty components 主菜单,进入 CherryUSB Port Sunxi Configuration,配置方式与 MSC 章节相同。

配置项路径必选/可选
CONFIG_USBHOST_MAX_INTERFACESCherryUSB usbhost max interfaces必选 (EC801默认接口数为5)
CHERRYUSB_HOST_CDC_ACMEnable usb host mode → Enable usb cdc acm driver必选(提供串口核心与设备节点)
CHERRYUSB_HOST_GSMEnable usb host mode → Enable usb gsm driver for 4g module必选
CHERRYUSB_HOST_GSM_EC801Enable usb host mode → Enable Quectel EC801 (Cat1) module AT supportEC801 模块必选
CHERRYUSB_HOST_CDC_ECMEnable usb host mode → Enable usb cdc ecm driver必选
CHERRYUSB_HOST_CDC_ECM_TX_QUEUE_SIZEEnable usb cdc ecm driver → tx pbuf queue depth可选(默认 32)
CHERRYUSB_HOST_CDC_ECM_ETH_MAX_SIZEEnable usb cdc ecm driver → max ethernet frame size可选(默认 1514)
CHERRYUSB_HOST_CDC_ECM_STATIC_IPEnable usb cdc ecm driver → Use static IPv4 config instead of DHCP可选(默认关)
CHERRYUSB_HOST_CDC_ECM_STATIC_IPADDR / ..._NETMASK / ..._GATEWAY静态 IP 子选项选择静态 IP 时配置

编译烧录​

完成上述配置后,编译烧录固件到开发板。

使用示例​

插入 EC801 模块后,判断是否获取到IP地址,即使无法连入公网也会有IP地址:

uart>ifconfig
network interface: EX (Default)
MTU: 1500
MAC: 30 89 84 6a 96 ab
FLAGS: UP LINK_UP ETHARP BROADCAST
ip address: 192.168.43.100
gw address: 192.168.43.1
net mask : 255.255.255.0
network interface: lo
MTU: 0
MAC:
FLAGS: UP LINK_UP
ip address: 127.0.0.1
gw address: 127.0.0.1
net mask : 255.0.0.0
dns server #0: 192.168.43.2
dns server #1: 192.168.43.3

也可以使用ec801_test命令发送指定AT指令:

ls /dev/ttyUSB* # 确认 AT 端口(EC801 默认 /dev/ttyUSB1)

ec801_test # 初始化并上电(ec801_init + ec801_power_on)
ec801_test -q # 上电后查询 IMEI / ICCID / IMSI / CSQ
ec801_test -s "AT+CPIN?" # 发送原始 AT 命令
ec801_test -s "AT+QNETDEVCTL=1,1,1" # 打开数据通道
ec801_test -o # 下电(ec801_power_off)

FAQ​

全速/低速设备插入后 EHCI 报 reset timeout 是否正常?​

问题现象: 插入 USB 键盘后日志显示 usbh_reset_port: reset timeout,但设备最终能正常工作。

根本原因: 这是正常现象。全速/低速设备不支持高速复位,EHCI 复位超时后自动将设备转交给 OHCI 处理。

解决办法: 无需操作。日志中随后会出现 port 0 full/low speed --> companion 的提示,表示转交成功,设备可正常工作。

USB 设备插入后无任何日志输出?​

问题现象: USB 设备插入后,系统无任何日志输出,设备未被识别。

根本原因: USB Host 未初始化、端口未配置为 Host 模式或 VBUS 供电异常。

解决办法:

  1. 确认 USB Host 已初始化:启动日志应出现 {sunxi-ohci0}usbh_hcd_add 525: add(CONFIG_USB_DBG_LEVEL=1;HCD 名称和行号随控制器/源码版本变化);若未自动加载,可执行 usb_test hci -i 0
  2. 检查板级配置 usb_port_type = 1(Host Only 模式)
  3. 检查 USB 设备是否供电(确认 usb_drv_vbus_gpio 配置正确)
  4. 检查 USB 线缆是否支持数据传输

U 盘插入后挂载失败?​

问题现象: U 盘插入后枚举成功,但无法挂载或访问。

根本原因: 文件系统格式不支持、分区表异常或设备节点未正确创建。

解决办法:

  1. 确认 U 盘使用 FAT32 或 exFAT(板端 elmfat 已支持);NTFS 无内置驱动,需额外文件系统组件
  2. 检查分区表格式(MBR / GPT),MSC 驱动均支持
  3. 使用 ls /dev/sd* 确认块设备节点已创建
  4. 确认挂载点 /mnt/sdap0 已自动创建

UAC录音/播放失败?​

问题现象: UAC设备插入后枚举成功,声卡节点也存在,但录音或播放失败。

根本原因: 采样率不匹配。

解决办法:

  1. 通过枚举成功后的log确认UAC支持的采样率
bcdADC :0100
Num of audio stream :2
stream name :speaker0
stream intf :1
Num of altsetting :2
Ingore altsetting 0
Altsetting :1
bNrChannels :2
bBitResolution :16
bSamFreqType :2
SampleFreq :8000
SampleFreq :48000
stream name :mic0
stream intf :2
Num of altsetting :2
Ingore altsetting 0
Altsetting :1
bNrChannels :2
bBitResolution :16
bSamFreqType :1
SampleFreq :48000
  1. 手动修改AUDIO_RECORD_SAMPLE_RATE宏的数值为匹配的采样率。