在万利学习板自带的演示例程中,有几个usb的例程。如果我们想实现一个usb功能,可以拿里面的例子来改。
那么具体要改哪些地方呢?首先要改各种描述符,然后是具体的数据处理。我们拿usb摇杆鼠标范例来修改,把它改成usb键盘。该范例在目录manleyekboardekstm32fusbdemo(8mosc)usbdemousblibdemosjoystickmous下,将joystickmouse复制一份,改名为usbkeyboard,以用来修改。
描述符修改
描述符在文件usb_desc.c中。第一个要改的是设备描述符。设备描述符的结构都标准的,长度也是固定的。范例中的usb设备描述符如下:
/* usb standard device descriptor */
const u8 joystick_devicedescriptor[joystick_siz_device_desc]=
{
0x12, /*blength */
usb_device_descriptor_type, /*bdescriptortype*/
0x00, /*bcdusb */
0x02,
0x00, /*bdeviceclass*/
0x00, /*bdevicesubclass*/
0x00, /*bdeviceprotocol*/
0x40, /*bmaxpacketsize40*/
0x83, /*idvendor (0x0483)*/
0x04,
0x10, /*idproduct = 0x5710*/
0x57,
0x00, /*bcddevice rel. 2.00*/
0x02,
1, /*index of string descriptor describing
manufacturer */
2,/*index of string descriptor describing
product*/
3, /*index of string descriptor describing the
device serial number */
0x01 /*bnumconfigurations*/
}; /* joystick_devicedescriptor */
我们只需要修改这里的idvendor(即vid)和idproduct(即pid)即可。它们是用来供电脑端识别设备以加载驱动用的,所以必须不能跟现有的设备相冲突。vid和pid都是两字节,低字节在前,高字节在后。例如这里的vid为0x0483,写在里面就是0x83,0x04。我们将vid改成0x1234,将pid改成0x4321,即: 0x34, 0x12, 0x21, 0x43。
然后再修改配置描述符集合。配置描述符集合包括配置描述符、接口描述符、类特殊描述符(这里是hid描述符)、以及端点描述符。如果你需要增加端点,那么在最后增加就行了,注意要记得修改joystick_siz_config_desc的值为配置描述符集合的长度。第一部分为配置描述符。通常这里不需要修改,除非你要改成该配置有多个接口(usb复合设备),那么应该修改bnuminterfaces,需要多少个就改成多少个,这里只有一个接口,所以值为1。第二部分为接口描述符,在接口描述符中决定该接口所实现的功能,例如hid设备,或者是大容量存储设备等等。其中binterfacenumber为该接口的编号,从0开始。这里只有一个接口,所以它的值为0,如果又更多的接口,则依次编号。注意一个接口完整结束(包括该接口下的类特殊描述符和端点描述符)后,才开始一个新的接口。bnumendpoints为该接口所使用的端点数目(不包括端点0),原来的程序是实现鼠标功能的,所以只有一个输入端点。我们这里增加一个输出端点,用来控制led(键盘上有大写字母锁定、小键盘数字键锁定等指示灯),因此将bnumendpoints改为2。binterfaceclass为接口所使用的类,这里指定为hid设备,usb键盘和鼠标都是hid设备,这里不用修改,如果你要实现其它设备,请根据usb协议所规定的类来修改。binterfacesubclass为接口所使用的子类,在hid设备类下规定了两种子类,系统引导时能用的和不能用的,这里为1,表示系统引导时能使用。binterfaceprotocol为接口的协议,原来为鼠标,这里改为1,键盘。第三部分为hid描述符,只有hid设备才有,如果你要修改成其它设备,则用其它设备的类特殊描述符代替或者没有,在这里不用做修改。第四部分为输入端点1的端点描述符,原来代码中,设置的端点最大包长度(wmaxpacketsize)为4字节,我们将其改成8字节。另外,我们再增加一个输出端点1,将最后的输入端点1描述符复制一份,然后修改地址(bendpointaddress)为0x01,这表示该端点为输出端点,地址为1。由bendpointaddress的最高位表示方向,1为输入,0为输出,最后4位表示地址。最后,要记得在usb_desc.h文件中修改joystick_siz_config_desc的长度为41,因为我们增加了7字节。实际修改好的配置描述符集合如下:
/* usb configuration descriptor */
/* all descriptors (configuration, interface, endpoint, class, vendor */
const u8 joystick_configdescriptor[joystick_siz_config_desc] =
{
//以下为配置描述符
0x09, /* blength: configuation descriptor size */
usb_configuration_descriptor_type, /* bdescriptortype: configuration */
joystick_siz_config_desc,
/* wtotallength: bytes returned */
0x00,
0x01, /*bnuminterfaces: 1 interface*/
0x01, /*bconfigurationvalue: configuration value*/
0x00, /*iconfiguration: index of string descriptor describing
the configuration*/
0xc0, /*bmattributes: self powered */
0x32, /*maxpower 100 ma: this current is used for detecting vbus*/
//以下为接口描述符
/************** descriptor of joystick mouse interface ****************/
/* 09 */
0x09, /*blength: interface descriptor size*/
usb_interface_descriptor_type,/*bdescriptortype: interface descriptor type*/
0x00, /*binterfacenumber: number of interface*/
0x00, /*balternatesetting: alternate setting*/
0x02, /*bnumendpoints*/
0x03, /*binterfaceclass: hid*/
0x01, /*binterfacesubclass : 1=boot, 0=no boot*/
0x01, /*binterfaceprotocol : 0=none, 1=keyboard, 2=mouse*/
0, /*iinterface: index of string descriptor*/
//以下为hid描述符
/******************** descriptor of joystick mouse hid ********************/
/* 18 */
0x09, /*blength: hid descriptor size*/
hid_descriptor_type, /*bdescriptortype: hid*/
0x00, /*bcdhid: hid class spec release number*/
0x01,
0x00, /*bcountrycode: hardware target country*/
0x01, /*bnumdescriptors: number of hid class descriptors to follow*/
0x22, /*bdescriptortype*/
joystick_siz_report_desc,/*witemlength: total length of report descriptor*/
0x00,
//以下为输入端点1描述符
/******************** descriptor of joystick mouse endpoint ********************/
/* 27 */
0x07, /*blength: endpoint descriptor size*/
usb_endpoint_descriptor_type, /*bdescriptortype:*/
0x81, /*bendpointaddress: endpoint address (in)*/
0x03, /*bmattributes: interrupt endpoint*/
0x08, /*wmaxpacketsize: 8 byte max */
0x00,
0x20, /*binterval: polling interval (32 ms)*/
//以下为输出端但1描述符
/* 34 */
0x07, /*blength: endpoint descriptor size*/
usb_endpoint_descriptor_type, /*bdescriptortype:*/
0x01, /*bendpointaddress: endpoint address (out)*/
0x03, /*bmattributes: interrupt endpoint*/
0x08, /*wmaxpacketsize: 8 byte max */
0x00,
0x20, /*binterval: polling interval (32 ms)*/
如何去掉batch normalization层来加速神经网络
揭秘中国西部硅谷:物流产业提速经济发展
有没有又便宜又好的蓝牙耳机?便宜性价比高的蓝牙耳机推荐
关注电池系统安全问题是企业的社会责任
英国研发可进行发动机内部维修的“蟑螂”机器人
基于万利EK-STM32开发板的简易USB键盘实现
瞬态抑制(TVS)二极管厂家,好的如何选择?
无人叉车市场的发展趋势是如何
英特尔与美光试制50纳米NAND闪存
正式发布 | 极海APM32F411系列高适配型MCU,均衡功耗、性能与成本
海尔联手以色列顶尖创新资源擦出创意新火花
红魔3 | 2899起,6.65英寸AMOLED面板,DC调光,屏刷新率90Hz,还支持HDR
光线传感器有哪些分类
凌云光携多款高端产品解决方案和明星产品亮相ACP 2023
13.56MHz非接触式读卡器芯片DP5321+
dfrobot2.7英寸12864 OLED液晶显示模块介绍
#深入浅出学习eTs#(十)蓝药丸还是红药丸
宝克力®模塑料助保点推出“隐形”防损门:优雅光效无缝融入店内陈设
RFX2401C 2.4GHz功放PA前端模块
“雷军号”亮相:上次是特斯拉 这次是蔚来汽车