主机功能使用指南
本文档介绍 CherryUSB Host Class Driver 的使用方法,涵盖 MSC 大容量存储、HID 键盘和 CDC ACM 串口三种主机功能的配置、编译和使用步骤。开发者只需通过 Kconfig 配置使能对应功能,系统即可自动识别插入的 USB 设备并加载相应驱动。
术语
本文档使用的专业术语定义请参阅《CherryUSB 入门概述》的术语章节。
概述
CherryUSB Host class driver 支持 Allwinner 开发板作为 USB Host,通过 EHCI + OHCI 控制器驱动自动识别插入的 USB 设备并加载对应 Class 驱动。
全速/低速设备插入后,EHCI 自动将其转交给 OHCI 处理(Transaction Translator 机制),开发者无需额外配置。
本文档收录了以下 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 串口设备,进行数据收发 |
启用任意 Host Class Driver 的通用流程为:板级配置 → 使能 Host 模式 → 选择 Class Driver → 使能 Host 控制器 → 编译烧录 → 插入设备自动识别。以下各章分别说明每个驱动的操作步骤。
MSC 大容量存储功能使用
功能介绍
MSC (Mass Storage Class) 支持开发板作为 USB Host 识别 U 盘等大容量存储设备。U 盘插入后,系统自动完成设备枚举和 MSC 驱动加载,并将 U 盘分区挂载到文件系统,可进行文件读写操作。
配置使用
板级配置
在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。
menuconfig 配置
进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:
CherryUSB Port Sunxi Configuration 与 Enable usb host mode 在菜单中为同级选项。需先进入 Enable usb host mode 选择 Host IP 为 sunxi,再返回上级菜单进入 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 控制器
返回上级菜单(CherryUSB Configuration),进入 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_HOST | CherryUSB → Enable usb host mode | 必选 |
CHERRYUSB_HOST_SUNXI | Enable usb host mode → Select usb host ip → sunxi | 必选 |
CHERRYUSB_HOST_MSC | Enable usb host mode → Enable usb msc driver | 必选 |
CHERRYUSB_PORT_SUNXI | CherryUSB → CherryUSB Port Sunxi Configuration | 必选 |
ARCH_SUNXI_USB0_OHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd | 必选 |
ARCH_SUNXI_USB0_EHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd | 必选 |
CHERRYUSB_TEST | CherryUSB Port Sunxi Configuration → enable cherryusb test | 必选(提供 usb_test 命令) |
编译烧录
完成上述配置后,编译烧录固件到开发板。
使用示例
- 开发板上电,进入系统 Shell
- 执行
usbh_init初始化 USB Host(如已集成到系统启动流程中则跳过此步骤) - 将 U 盘插入开发板的 USB A 口(Host 口)
- 系统自动检测 U 盘,完成枚举和 MSC 驱动加载
- U 盘分区自动挂载到
/mnt/sdap0
启动日志如下:
> usbh_init
[I/USB]{sunxi-ohci0}usbh_hcd_add 453: add
[I/USB]{sunxi-ehci0}usbh_hcd_add 453: add
[I/USB]{sunxi-ehci0}ehci_start 1745: Start Asynchronous List Base Address: 40124a80
[I/USB]{sunxi-ehci0}ehci_start 1749: Start Periodic Frame List Base Address: 40124000
U 盘插入后:
uart>{sunxi-ehci0}ehci_irq_handler 1655: 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] Ep=81 Attr=02 Mps=512 Interval=00 Mult=00
[I/usbh_msc] Ep=02 Attr=02 Mps=512 Interval=00 Mult=00
[I/usbh_msc] Capacity info:
[I/usbh_msc] Block num:60481536,block size:512
mount usb mass storage successful!!
usbmsc blkdev:SCSI_DISK_sdap0 mount on /mnt/sdap0
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 295: blkinfo.hiddennum 0
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 296: blkinfo.headnum 2048
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 297: blkinfo.secnum 60477440
[I/USB] {SCSI_DISK_sdap0}usbh_msc_partition_init 298: blkinfo.secsize 512
[I/usbh_msc] {SCSI_DISK_sda}usbh_msc_connect 400: 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>
日志关键行说明:
| 日志内容 | 含义 |
|---|---|
usbh_hcd_add: add | Host 控制器驱动添加成功 |
Enumeration success, start loading class driver | U 盘枚举成功,开始加载 MSC 驱动 |
mount usb mass storage successful | U 盘分区挂载成功 |
Register MSC Class:/dev/sda | MSC 块设备注册成功 |
HID 键盘功能使用
功能介绍
HID (Human Interface Device) 键盘功能支持开发板作为 USB Host 接入 USB 键盘,获取按键事件。键盘通过 HID 协议上报按键码,系统可通过 usbhkbd 命令进行键盘打开、关闭和 LED 控制等操作。
配置使用
板级配置
在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。
menuconfig 配置
进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:
CherryUSB Port Sunxi Configuration 与 Enable usb host mode 在菜单中为同级选项。需先进入 Enable usb host mode 选择 Host IP 为 sunxi,再返回上级菜单进入 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 键盘驱动
步骤二:使能 Host 控制器
返回上级菜单(CherryUSB Configuration),进入 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_HOST | CherryUSB → Enable usb host mode | 必选 |
CHERRYUSB_HOST_SUNXI | Enable usb host mode → Select usb host ip → sunxi | 必选 |
CHERRYUSB_HOST_HIDKBD | Enable usb host mode → Enable usb hidkbd driver | 必选 |
CHERRYUSB_PORT_SUNXI | CherryUSB → CherryUSB Port Sunxi Configuration | 必选 |
ARCH_SUNXI_USB0_OHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd | 必选 |
ARCH_SUNXI_USB0_EHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd | 必选 |
CHERRYUSB_TEST | CherryUSB Port Sunxi Configuration → enable cherryusb test | 必选(提供 usb_test 命令) |
编译烧录
完成上述配置后,编译烧录固件到开发板。
使用示例
- 开发板上电,进入系统 Shell
- 执行
usbh_init初始化 USB Host(如已集成到系统启动流程中则跳过此步骤) - 将 USB 键盘插入开发板的 USB A 口
- 系统自动识别键盘
键盘插入后(全速设备由 EHCI 转交给 OHCI 处理),执行以下命令控制键盘:
# 查看可用命令
usbhkbd
# 打开键盘设备(id 为 0)
usbhkbd 0 open
# 关闭键盘设备
usbhkbd 0 close
# 设置键盘 LED(如 Caps Lock、Num Lock 等)
usbhkbd 0 evtled 0x1f
启动日志如下:
{sunxi-ehci0}ehci_irq_handler 1571: connect present
[E/USB] {sunxi-ehci0}usbh_reset_port 125: reset timeout
{sunxi-ehci0}usbh_reset_port 126: port 0 full/low speed --> companion
{sunxi-ehci0}ehci_irq_handler 1573: disconnect present
{sunxi-ohci0}ohci_irq_handler 2150: connect present
[W/USB] Failed to enable port 2
usbh_hidkbd_connect 331: Found USB HIDBP Keyboard 046d:c31d -> kbd0
> usbhkbd 0 open
find keyboard kbd0...
usb_input_event 72: kbd0, type 1 code 029 val 0
usb_input_event 72: kbd0, type 1 code 042 val 0
...
日志关键行说明:
| 日志内容 | 含义 |
|---|---|
reset timeout + full/low speed --> companion | 全速设备由 EHCI 转交给 OHCI 处理,属于正常现象 |
[W/USB] 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 能够接入 USB 串口设备(如 USB 转串口模块),并进行数据收发。系统自动识别串口设备后,可通过测试命令进行数据的读写验证。
配置使用
板级配置
在板级配置 sys_config.fex 中使能 USB 功能,并设置 usb_port_type = 1 将 USB 端口配置为 Host Only 模式。
menuconfig 配置
进入 menuconfig,按以下路径配置 CherryUSB Host 功能和 Host 控制器驱动:
CherryUSB Port Sunxi Configuration 与 Enable usb host mode 在菜单中为同级选项。需先进入 Enable usb host mode 选择 Host IP 为 sunxi,再返回上级菜单进入 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 控制器
返回上级菜单(CherryUSB Configuration),进入 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_HOST | CherryUSB → Enable usb host mode | 必选 |
CHERRYUSB_HOST_SUNXI | Enable usb host mode → Select usb host ip → sunxi | 必选 |
CHERRYUSB_HOST_CDC_ACM | Enable usb host mode → Enable usb cdc acm driver | 必选 |
TEST_USBH_SERIAL | Enable usb host mode → Select USB host template → demo for test serial | 可选(使能测试命令) |
CHERRYUSB_PORT_SUNXI | CherryUSB → CherryUSB Port Sunxi Configuration | 必选 |
ARCH_SUNXI_USB0_OHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ohci-hcd | 必选 |
ARCH_SUNXI_USB0_EHCI | CherryUSB Port Sunxi Configuration → enable arch sunxi usb0 ehci-hcd | 必选 |
CHERRYUSB_TEST | CherryUSB Port Sunxi Configuration → enable cherryusb test | 必选(提供 usb_test 命令) |
编译烧录
完成上述配置后,编译烧录固件到开发板。
使用示例
- 开发板上电,进入系统 Shell
- 执行
usbh_init初始化 USB Host - 将 USB 串口设备(如 USB 转串口模块)插入开发板的 USB A 口
- 系统自动识别串口设备
设备识别后,执行以下命令进行串口通信测试:
# 打开串口设备
usbh_acm_test_open
# 发送数据
usbh_acm_test_write 111
# 读取数据
usbh_acm_test_read
启动日志如下:
{sunxi-ehci0}ehci_irq_handler 1571: connect present
[I/usbh_core] New device found,idVendor:067b,idProduct:2303,bcdDevice:0400
[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 0 | CDC ACM 驱动加载成功 |
usbh acm read:(3) / 31 31 31 | 读取到 3 字节数据(ASCII "111") |
FAQ
全速/低速设备插入后 EHCI 报 reset timeout 是否正常?
问题现象: 插入 USB 键盘后日志显示 usbh_reset_port: reset timeout,但设备最终能正常工作。
根本原因: 这是正常现象。全速/低速设备不支持高速复位,EHCI 复位超时后自动将设备转交给 OHCI 处理。
解决办法: 无需操作。日志中随后会出现 port 0 full/low speed --> companion 的提示,表示转交成功,设备可正常工作。
USB 设备插入后无任何日志输出?
问题现象: USB 设备插入后,系统无任何日志输出,设备未被识别。
根本原因: USB Host 未初始化、端口未配置为 Host 模式或 VBUS 供电异常。
解决办法:
- 确认已执行
usbh_init初始化 USB Host - 检查板级配置
usb_port_type = 1(Host Only 模式) - 检查 USB 设备是否供电(确认
usb_drv_vbus_gpio配置正确) - 检查 USB 线缆是否支持数据传输
U 盘插入后挂载失败?
问题现象: U 盘插入后枚举成功,但无法挂载或访问。
根本原因: 文件系统格式不支持、分区表异常或设备节点未正确创建。
解决办法:
- 确认 U 盘文件系统格式支持(FAT32、exFAT、NTFS)
- 检查分区表格式(MBR / GPT),MSC 驱动均支持
- 使用
ls /dev/sd*确认块设备节点已创建 - 确认挂载点
/mnt/sdap0已自动创建