哪些公司需要网站建设,个人站长做什么网站好,电子商务网站建设案例教程,939网站建设文章目录 1、声明2、HID协议2.1、描述符2.2、鼠标数据格式 3、应用程序4、编译应用程序5、测试 1、声明
本文是在学习韦东山《驱动大全》USB子系统时#xff0c;为梳理知识点和自己回看而记录#xff0c;全部内容高度复制粘贴。
韦老师的《驱动大全》#xff1a;商品详情 … 文章目录 1、声明2、HID协议2.1、描述符2.2、鼠标数据格式 3、应用程序4、编译应用程序5、测试 1、声明
本文是在学习韦东山《驱动大全》USB子系统时为梳理知识点和自己回看而记录全部内容高度复制粘贴。
韦老师的《驱动大全》商品详情
其对应的讲义资料https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
libusb apihttps://libusb.sourceforge.io/api-1.0/libusb_api.html
2、HID协议
HIDHuman Interface Devices, 人类用来跟计算机交互的设备。就是鼠标、键盘、游戏手柄等设备。对于USB接口的HID设备有一套协议。
2.1、描述符
HID设备有如下描述符 HID设备的设备描述符并无实际意义没有使用设备描述符来表示自己是HID设备。HID设备只有一个配置所以只有一个配置描述符。接口描述符 bInterfaceClass为3表示它是HID设备。bInterfaceSubClass是0或11表示它支持Boot Interface(表示PC的BIOS能识别、使用它)0表示必须等操作系统启动后通过驱动程序来使用它。bInterfaceProtocol0-None, 1-键盘, 2-鼠标。 端点描述符HID设备有一个控制端点、一个中断端点。 对于鼠标HOST可以通过中断端点读到数据。
2.2、鼠标数据格式
通过中断传输可以读到鼠标数据它是8字节的数据格式如下
偏移大小描述01字节11字节按键状态22字节X位移42字节Y位移61字节或2字节滚轮
按键状态里每一位对应鼠标的一个按键等1时表示对应按键被点击了格式如下
位长度描述01鼠标的左键11鼠标的右键21鼠标的中间键35保留设备自己定义bit3: 鼠标的侧边按键bit4:
X位移、Y位移都是8位的有符号数。对于X位移负数表示鼠标向左移动正数表示鼠标向右移动移动的幅度就使用这个8位数据表示。对于Y位移负数表示鼠标向上移动正数表示鼠标向下移动移动的幅度就使用这个8位数据表示。
3、应用程序
本次应用程序是使用同步接口读取鼠标数据。
#include errno.h
#include signal.h
#include stdio.h
#include stdlib.h
#include string.h#include libusb-1.0/libusb.hint main(int argc, char **argv)
{int err;libusb_device *dev, **devs;int num_devices;int endpoint;int interface_num;int transferred;int count 0;unsigned char buffer[8];struct libusb_config_descriptor *config_desc;struct libusb_device_handle *dev_handle NULL;int found 0;/* libusb init */err libusb_init(NULL);if (err 0) {fprintf(stderr, failed to initialise libusb %d - %s\n, err, libusb_strerror(err));exit(1);}/* get device list */if ((num_devices libusb_get_device_list(NULL, devs)) 0) // 获取设备描述符列表函数返回设备描述符数量{fprintf(stderr, libusb_get_device_list() failed\n);libusb_exit(NULL);exit(1);} fprintf(stdout, libusb_get_device_list() ok\n);/* for each device, get config descriptor */for (int i 0; i num_devices; i){dev devs[i];err libusb_get_config_descriptor(dev, 0, config_desc); // 获取配置描述符if (err) {fprintf(stderr, could not get configuration descriptor\n);continue;}fprintf(stdout, libusb_get_config_descriptor() ok\n);/* parse interface descriptor, find usb mouse */for (int interface 0; interface config_desc-bNumInterfaces; interface) // 枚举所有接口描述符{const struct libusb_interface_descriptor *intf_desc config_desc-interface[interface].altsetting[0]; // 获取配置描述符里的第一个接口描述符interface_num intf_desc-bInterfaceNumber; // 记录该接口描述符的编号编号是从0开始if (intf_desc-bInterfaceClass ! 3 || intf_desc-bInterfaceProtocol ! 2) // 判断是否是HID设备和是否是鼠标协议continue;/* 找到了USB鼠标 */fprintf(stdout, find usb mouse ok\n);for (int ep 0; ep intf_desc-bNumEndpoints; ep) // 枚举所有端点描述符{// 判断是否是中断传输是否是输入端点输入输出都是以USB Host来讨论所以该端点是USB Device输出到USB Hostif ((intf_desc-endpoint[ep].bmAttributes 3) LIBUSB_TRANSFER_TYPE_INTERRUPT || (intf_desc-endpoint[ep].bEndpointAddress 0x80) LIBUSB_ENDPOINT_IN){/* 找到了输入的中断端点 */fprintf(stdout, find in int endpoint\n);endpoint intf_desc-endpoint[ep].bEndpointAddress;found 1;break;}}if (found)break;}libusb_free_config_descriptor(config_desc);if (found)break; }if (!found){/* free device list */libusb_free_device_list(devs, 1);libusb_exit(NULL);exit(1);}/* libusb open */if (found){err libusb_open(dev, dev_handle);if (err){fprintf(stderr, failed to open usb mouse\n);exit(1);}fprintf(stdout, libusb_open ok\n);}/* free device list */libusb_free_device_list(devs, 1);/* claim interface */libusb_set_auto_detach_kernel_driver(dev_handle, 1); err libusb_claim_interface(dev_handle, interface_num);if (err){fprintf(stderr, failed to libusb_claim_interface\n);exit(1);}fprintf(stdout, libusb_claim_interface ok\n);/* libusb interrupt transfer */while (1){err libusb_interrupt_transfer(dev_handle, endpoint, buffer, 8, transferred, 5000); // 发起中断传输阻塞等待5s超时时间if (!err) {/* parser data */printf(%04d datas: , count);printf(recv datas len %d\n, transferred);for (int i 0; i transferred; i){printf(%02x , buffer[i]);}printf(\n);} else if (err LIBUSB_ERROR_TIMEOUT){fprintf(stderr, libusb_interrupt_transfer timout\n);} else {const char *errname libusb_error_name(err);fprintf(stderr, libusb_interrupt_transfer err : %d, %s\n, err, errname);//exit(1);}}/* libusb close */libusb_release_interface(dev_handle, interface_num);libusb_close(dev_handle);libusb_exit(NULL);
}4、编译应用程序
假设你的开发板是ubuntu系统
# 安装libusb库
$ sudo apt install libusb-1.0-0-dev# 编译程序
$ gcc -o readmouse readmouse.c -lusb-1.05、测试
将usb鼠标插入开发板 执行程序
$ sudo ./readmouse移动鼠标 滚轮滑动 按键状态 另外每个鼠标的数据格式是不一样的。以上测试结果只是我使用的鼠标。 文章转载自: http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.kndt.cn.gov.cn.kndt.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.xlmpj.cn.gov.cn.xlmpj.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.nrlsg.cn.gov.cn.nrlsg.cn http://www.morning.djxnn.cn.gov.cn.djxnn.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.hqwcd.cn.gov.cn.hqwcd.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.ygpdm.cn.gov.cn.ygpdm.cn http://www.morning.prhqn.cn.gov.cn.prhqn.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.wtdhm.cn.gov.cn.wtdhm.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.liyixun.com.gov.cn.liyixun.com http://www.morning.cljmx.cn.gov.cn.cljmx.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.xykst.cn.gov.cn.xykst.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn