要实现带鼠标功能的键盘有两种方式
设备描述符,配置描述符,HID 描述符 (注意报告描述符的长度) 和端点描述符等保持不变,唯一不同的是报告描述符和报告描述符的长度。
端点 0 最大包长为 64,分包传输如下
0x5 0x1 0x9 0x6 0xa1 0x1 0x85 0x1 0x5 0x7 0x19 0xe0 0x29 0xe7 0x15 0x0 0x25 0x1 0x95 0x8 0x75 0x1 0x81 0x2 0x95 0x1 0x75 0x8 0x81 0x1 0x5 0x8 0x19 0x1 0x29 0x5 0x95 0x5 0x75 0x1 0x91 0x2 0x95 0x1 0x75 0x3 0x91 0x1 0x5 0x7 0x19 0x0 0x2a 0xff 0x0 0x15 0x0 0x26 0xff 0x0 0x95 0x6 0x75 0x8
0x81 0x0 0xc0 0x5 0x1 0x9 0x2 0xa1 0x1 0x85 0x2 0x9 0x1 0xa1 0x0 0x5 0x9 0x19 0x1 0x29 0x5 0x15 0x0 0x25 0x1 0x95 0x5 0x75 0x1 0x81 0x2 0x95 0x1 0x75 0x3 0x81 0x1 0x5 0x1 0x9 0x30 0x9 0x31 0x15 0x81 0x25 0x7f 0x95 0x2 0x75 0x8 0x81 0x6 0x9 0x38 0x15 0x81 0x25 0x7f 0x95 0x1 0x75 0x8 0x81
:0x6 0x5 0xc 0xa 0x38 0x2 0x15 0x81 0x25 0x7f 0x95 0x1 0x75 0x8 0x81 0x6 0xc0 0xc0 0x5 0xc 0x9 0x1 0xa1 0x1 0x85 0x3 0x15 0x0 0x26 0xff 0x3 0x19 0x0 0x2a 0xff 0x3 0x95 0x1 0x75 0x10 0x81 0x0 0xc0 0x5 0x1 0x9 0x5 0xa1 0x1 0x85 0x4 0x5 0x1 0x9 0x30 0x9 0x31 0x9 0x32 0x9 0x35 0x9 0x33 0x9
0x34 0x15 0x81 0x25 0x7f 0x95 0x6 0x75 0x8 0x81 0x2 0x5 0x1 0x9 0x39 0x15 0x1 0x25 0x8 0x35 0x0 0x46 0x3b 0x1 0x95 0x1 0x75 0x8 0x81 0x2 0x5 0x9 0x19 0x1 0x29 0x20 0x15 0x0 0x25 0x1 0x95 0x20 0x75 0x1 0x81 0x2 0xc0
上述报告描述符设置了四个报告
报告返回时,报告的最低位指定报告 ID 即可
例如键盘报告的返回, 最低位为 1
0x1 0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
a
按下鼠标报告的返回最低位为 2
0x2 0x0 0x5 0x5 0x0 0x0
其他报告的返回类似。
USB - 描述符之间的关系 中可以知道,多个接口实现时,一个接口实现鼠标,一个接口实现键盘,所以此时的配置描述符集合为
{
配置描述符,
接口 1 描述符
接口描述符 (键盘接口),
类特殊描述符 (HID 描述符 - 键盘),
端点描述符 (键盘输入端点),
接口 2 描述符
接口描述符 (鼠标接口),
类特殊描述符 (HID 描述符 - 鼠标),
端点描述符 (鼠标输入端点),
}
0x9 0x2 0x3b 0x0 0x2 0x1 0x0 0xa0 0x32 0x9 0x4 0x0 0x0 0x1 0x3 0x1 0x1 0x4 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x41 0x0 0x7 0x5 0x81 0x3 0x8 0x0 0xa 0x9 0x4 0x1 0x0 0x1 0x3 0x1 0x2 0x5 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x4d 0x0 0x7 0x5 0x82 0x3 0x8 0x0 0xa
配置描述符的 bNumInterfaces(第五个数据)
的值为 2,表明有两个接口
接口描述符的 bInterfaceNumber(第三个数据)
表示接口的编号,从 0 开始。
接口描述符的 iInterface(最后一个数据)
表示接口字符串的索引,主机在获取字符串描述符时,需要根据索引返回相应的字符串。
USB 控制端点收到的数据
0x80 0x6 0x4 0x3 0x9 0x4 0x4 0x0
设备返回给主机的数据
0x26 0x3 0x4b 0x0
主机从返回的 0x26
知道了接口 1 字符串的长度,所以下次就会请求全部的长度
0x80 0x6 0x4 0x3 0x9 0x4 0x26 0x0
从机返回 0x26
个接口 1 字符串描述符
0x26 0x3 0x4b 0x0 0x65 0x0 0x79 0x0 0x62 0x0 0x6f 0x0 0x61 0x0 0x72 0x0 0x64 0x0 0x20 0x0 0x49 0x0 0x6e 0x0 0x74 0x0 0x65 0x0 0x72 0x0 0x66 0x0 0x61 0x0 0x63 0x0 0x65 0x0
USB 控制端点收到的数据
0x80 0x6 0x5 0x3 0x9 0x4 0x4 0x0
设备返回给主机的数据
0x20 0x3 0x4d 0x0
0x20
知道了接口 2 字符串的长度,所以下次就会请求全部的长度0x80 0x6 0x5 0x3 0x9 0x4 0x20 0x0
从机返回 0x20
个接口 2 字符串描述符
0x20 0x3 0x4d 0x0 0x6f 0x0 0x75 0x0 0x73 0x0 0x65 0x0 0x20 0x0 0x49 0x0 0x6e 0x0 0x74 0x0 0x65 0x0 0x72 0x0 0x66 0x0 0x61 0x0 0x63 0x0 0x65 0x0
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
0x0 0x5 0x5 0x0 0x0
往对应的端点写数据即可。
suspend int
bus reset int
enum done int
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x1 0x0 0x0 0x40 0x0
dcd xfer 12
input ep int
d->h :0x12 0x1 0x0 0x2 0x0 0x0 0x0 0x40 0xfe 0xca 0x8 0x40 0x0 0x1 0x1 0x2 0x3 0x1
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
bus reset int
output ep int
send xfer complete event
enum done int
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x0 0x5 0x5 0x0 0x0 0x0 0x0 0x0
dcd xfer 0
input ep int
edpt int send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x1 0x0 0x0 0x12 0x0
dcd xfer 12
input ep int
d->h :0x12 0x1 0x0 0x2 0x0 0x0 0x0 0x40 0xfe 0xca 0x8 0x40 0x0 0x1 0x1 0x2 0x3 0x1
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
output ep int
send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x2 0x0 0x0 0xff 0x0
dcd xfer 3b
input ep int
d->h :0x9 0x2 0x3b 0x0 0x2 0x1 0x0 0xa0 0x32 0x9 0x4 0x0 0x0 0x1 0x3 0x1 0x1 0x4 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x41 0x0 0x7 0x5 0x81 0x3 0x8 0x0 0xa 0x9 0x4 0x1 0x0 0x1 0x3 0x1 0x2 0x5 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x4d 0x0 0x7 0x5 0x82 0x3 0x8 0x0 0xa
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x3 0x3 0x9 0x4 0xff 0x0
send xfer complete event
dcd xfer e
input ep int
d->h :0xe 0x3 0x31 0x0 0x32 0x0 0x33 0x0 0x34 0x0 0x35 0x0 0x36 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x3 0x0 0x0 0xff 0x0
send xfer complete event
dcd xfer 4
input ep int
d->h :0x4 0x3 0x9 0x4
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x2 0x3 0x9 0x4 0xff 0x0
send xfer complete event
dcd xfer 1e
input ep int
d->h :0x1e 0x3 0x54 0x0 0x69 0x0 0x6e 0x0 0x79 0x0 0x55 0x0 0x53 0x0 0x42 0x0 0x20 0x0 0x44 0x0 0x65 0x0 0x76 0x0 0x69 0x0 0x63 0x0 0x65 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x6 0x0 0x0 0xa 0x0
send xfer complete event
get device qualifier
stall ep0
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x1 0x0 0x0 0x12 0x0
dcd xfer 12
input ep int
d->h :0x12 0x1 0x0 0x2 0x0 0x0 0x0 0x40 0xfe 0xca 0x8 0x40 0x0 0x1 0x1 0x2 0x3 0x1
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x2 0x0 0x0 0x9 0x0
send xfer complete event
dcd xfer 9
input ep int
d->h :0x9 0x2 0x3b 0x0 0x2 0x1 0x0 0xa0 0x32
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x2 0x0 0x0 0x3b 0x0
send xfer complete event
dcd xfer 3b
input ep int
d->h :0x9 0x2 0x3b 0x0 0x2 0x1 0x0 0xa0 0x32 0x9 0x4 0x0 0x0 0x1 0x3 0x1 0x1 0x4 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x41 0x0 0x7 0x5 0x81 0x3 0x8 0x0 0xa 0x9 0x4 0x1 0x0 0x1 0x3 0x1 0x2 0x5 0x9 0x21 0x11 0x1 0x0 0x1 0x22 0x4d 0x0 0x7 0x5 0x82 0x3 0x8 0x0 0xa
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x0 0x9 0x1 0x0 0x0 0x0 0x0 0x0
send xfer complete event
edpt open
edpt open
dcd xfer 0
input ep int
edpt int send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x4 0x3 0x9 0x4 0x4 0x0
dcd xfer 4
input ep int
d->h :0x26 0x3 0x4b 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x3 0x0 0x0 0xff 0x0
send xfer complete event
dcd xfer 4
input ep int
d->h :0x4 0x3 0x9 0x4
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x4 0x3 0x9 0x4 0x26 0x0
send xfer complete event
dcd xfer 26
input ep int
d->h :0x26 0x3 0x4b 0x0 0x65 0x0 0x79 0x0 0x62 0x0 0x6f 0x0 0x61 0x0 0x72 0x0 0x64 0x0 0x20 0x0 0x49 0x0 0x6e 0x0 0x74 0x0 0x65 0x0 0x72 0x0 0x66 0x0 0x61 0x0 0x63 0x0 0x65 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x0 0x3 0x0 0x0 0xff 0x0
send xfer complete event
dcd xfer 4
input ep int
d->h :0x4 0x3 0x9 0x4
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x5 0x3 0x9 0x4 0x4 0x0
send xfer complete event
dcd xfer 4
input ep int
d->h :0x20 0x3 0x4d 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x1 0x3 0x9 0x4 0xff 0x0
send xfer complete event
dcd xfer 10
input ep int
d->h :0x10 0x3 0x54 0x0 0x69 0x0 0x6e 0x0 0x79 0x0 0x55 0x0 0x53 0x0 0x42 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x5 0x3 0x9 0x4 0x20 0x0
send xfer complete event
dcd xfer 20
input ep int
d->h :0x20 0x3 0x4d 0x0 0x6f 0x0 0x75 0x0 0x73 0x0 0x65 0x0 0x20 0x0 0x49 0x0 0x6e 0x0 0x74 0x0 0x65 0x0 0x72 0x0 0x66 0x0 0x61 0x0 0x63 0x0 0x65 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x1 0x3 0x9 0x4 0xff 0x0
send xfer complete event
dcd xfer 10
input ep int
d->h :0x10 0x3 0x54 0x0 0x69 0x0 0x6e 0x0 0x79 0x0 0x55 0x0 0x53 0x0 0x42 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x21 0xa 0x0 0x0 0x0 0x0 0x0 0x0
send xfer complete event
dcd xfer 0
input ep int
edpt int send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x2 0x3 0x9 0x4 0xff 0x0
dcd xfer 1e
input ep int
d->h :0x1e 0x3 0x54 0x0 0x69 0x0 0x6e 0x0 0x79 0x0 0x55 0x0 0x53 0x0 0x42 0x0 0x20 0x0 0x44 0x0 0x65 0x0 0x76 0x0 0x69 0x0 0x63 0x0 0x65 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x81 0x6 0x0 0x22 0x0 0x0 0x81 0x0
send xfer complete event
dcd xfer 40
input ep int
d->h :0x5 0x1 0x9 0x6 0xa1 0x1 0x5 0x7 0x19 0xe0 0x29 0xe7 0x15 0x0 0x25 0x1 0x95 0x8 0x75 0x1 0x81 0x2 0x95 0x1 0x75 0x8 0x81 0x1 0x5 0x8 0x19 0x1 0x29 0x5 0x95 0x5 0x75 0x1 0x91 0x2 0x95 0x1 0x75 0x3 0x91 0x1 0x5 0x7 0x19 0x0 0x2a 0xff 0x0 0x15 0x0 0x26 0xff 0x0 0x95 0x6 0x75 0x8 0x81 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 1
input ep int
d->h :0xc0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x80 0x6 0x2 0x3 0x9 0x4 0xff 0x0
send xfer complete event
dcd xfer 1e
input ep int
d->h :0x1e 0x3 0x54 0x0 0x69 0x0 0x6e 0x0 0x79 0x0 0x55 0x0 0x53 0x0 0x42 0x0 0x20 0x0 0x44 0x0 0x65 0x0 0x76 0x0 0x69 0x0 0x63 0x0 0x65 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
setup packed recvd
setup packed done
output ep int
h->d: 0x21 0xa 0x0 0x0 0x1 0x0 0x0 0x0
send xfer complete event
dcd xfer 0
input ep int
edpt int send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x81 0x6 0x0 0x22 0x1 0x0 0x8d 0x0
dcd xfer 40
input ep int
d->h :0x5 0x1 0x9 0x2 0xa1 0x1 0x9 0x1 0xa1 0x0 0x5 0x9 0x19 0x1 0x29 0x5 0x15 0x0 0x25 0x1 0x95 0x5 0x75 0x1 0x81 0x2 0x95 0x1 0x75 0x3 0x81 0x1 0x5 0x1 0x9 0x30 0x9 0x31 0x15 0x81 0x25 0x7f 0x95 0x2 0x75 0x8 0x81 0x6 0x9 0x38 0x15 0x81 0x25 0x7f 0x95 0x1 0x75 0x8 0x81 0x6 0x5 0xc 0xa 0x38
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer d
input ep int
d->h :0x2 0x15 0x81 0x25 0x7f 0x95 0x1 0x75 0x8 0x81 0x6 0xc0 0xc0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 0
rxflvl int
ep 0 out packet recvd:
output ep int
send xfer complete event
rxflvl int
setup packed recvd
setup packed done
output ep int
h->d: 0x21 0x9 0x0 0x2 0x0 0x0 0x1 0x0
dcd xfer 1
rxflvl int
ep 0 out packet recvd:
buffer: 0x15
output ep int
send xfer complete event
dcd xfer 0
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 5
0x0 0x5 0x5 0x0 0x0
input ep int
d->h :0x0 0x5 0x5 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 5
0x0 0x5 0x5 0x0 0x0
input ep int
d->h :0x0 0x5 0x5 0x0 0x0
turn off txfe
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
dcd xfer 5
0x0 0x5 0x5 0x0 0x0
input ep int
d->h :0x0 0x5 0x5 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
dcd xfer 5
0x0 0x5 0x5 0x0 0x0
input ep int
d->h :0x0 0x5 0x5 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 5
0x0 0x5 0x5 0x0 0x0
input ep int
d->h :0x0 0x5 0x5 0x0 0x0
turn off txfe
dcd xfer 8
0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x4 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event
dcd xfer 8
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
input ep int
d->h :0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
turn off txfe
input ep int
edpt int send xfer complete event