博客
关于我
UEFI开发探索90- YIE002USB开发板(13 Linux编程)
阅读量:620 次
发布时间:2019-03-12

本文共 3385 字,大约阅读时间需要 11 分钟。

YIE002USB开发板之Linux编程

1 建立开发目录

与上一篇文章类似,首先创建开发目录,新建文件夹hidlibusb并将以下文件复制进去:

libusb/hid.chidapi/hidapi.hhidtest/test.c

为当前代码编写Makefile,内容如下:

all: hidtest-libusb libslibs: libhidapi-libusb.soCC      ?= gccCFLAGS  ?= -Wall -g -fpicLDFLAGS ?= -Wall -gCOBJS_LIBUSB = hid.oCOBJS = $(COBJS_LIBUSB) test.oOBJS      = $(COBJS)LIBS_USB  = `pkg-config libusb-1.0 --libs` -lrt -lpthreadLIBS      = $(LIBS_USB)INCLUDES ?= `pkg-config libusb-1.0 --cflags`# Console Test Programhidtest-libusb: $(COBJS) $(CC) $(LDFLAGS) $^ $(LIBS_USB) -o $@# Shared Libslibhidapi-libusb.so: $(COBJS_LIBUSB) $(CC) $(LDFLAGS) $(LIBS_USB) -shared -fpic -Wl,-soname,$@.0 $^ -o $@# Objects$(COBJS): %.o: %.c $(CC) $(CFLAGS) -c $(INCLUDES) $< -o $@clean: rm -f $(OBJS) hidtest-libusb libhidapi-libusb.so hidtest.o.PHONY: clean libs

2 代码编写

完成目录创建后,对代码进行修改。USB HID的三种通信方式——hid_read()hid_write()通信方式,以及Feature report的通信方式——在上一篇文章中已经实现。由于现在libusb下的hid.c中提供了统一的函数接口,这两种方式的代码不需要修改,我们只需添加支持Input report和Output report的代码即可。

2.1 添加Output report的处理代码

奇怪的是,libusb的源文件hid.c中已经提供了处理Input report的函数hid_get_input_report(),但是却没有提供处理Output report的函数。因此,实现Output report的代码需要做一些修改。

hid_send_feature_report()函数中,修改以下代码:

int HID_API_EXPORT HID_API_CALLhid_set_output_report(hid_device *dev, const unsigned char *data, size_t length) {    int res = -1;    int skipped_report_id = 0;    int report_number = data[0];    if (report_number == 0x0) {        data++;        length--;        skipped_report_id = 1;    }    res = libusb_control_transfer(dev->device_handle,        LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT,        0x09/*HID set_report*/,        (2/*HID Output*/ << 8) | report_number,        dev->interface,        (unsigned char *)data,        length,        1000/*timeout millis*/);    if (res < 0) {        return -1;    }    if (skipped_report_id) {        length++;    }    return length;}

2.2 Input report与Output report通信方式

其他两种通信方式在上一篇文章中已经实现,无需修改代码。现在需要添加的,是发送Output report和接收Input report的代码,如下所示:

memset(yie_buf, 0, sizeof(yie_buf));yie_buf[0] = 0x00;yie_buf[1] = 0xA0;yie_buf[2] = 0x0A;yie_buf[3] = 0x0B;yie_buf[4] = 0x0C;res = hid_set_output_report(handle, yie_buf, 17);if (res < 0) {    printf("Unable to send a output report.\n");}memset(yie_buf, 0, sizeof(yie_buf));res = hid_get_input_report(handle, yie_buf, sizeof(yie_buf));if (res < 0) {    printf("Unable to get a input report.\n");    printf("%ls", hid_error(handle));} else {    // Print out the returned buffer.    printf("Input Report\n   ");    printf("report number:%d\n   ", yie_buf[0]);    for (i = 1; i < res; i++) {        printf("%02x ", yie_buf[i]);    }    printf("\n");}

##启动命令行,使用make命令编译,得到执行文件hidtest-libusb

##插入自制的USB HID设备,运行hidtest-libusb,输出信息如下:

robin@NUC6CAYHC:~/luotest/hidapi$ sudo ./ hidtest-libusb……Manufacturer String: RobinProduct String: Robin's UEFI ExplorerSerial Number: (77) My123Indexed String 1: RobinFeature Report   report number:0a0 03 0b 0c 00 00 00 00 00 00 00 00 00 00 00 00Input Report   report number:0a0 02 0b 0c 00 00 00 00 00 00 00 00 00 00 00 00Read data,Length=16a0 01 0b 0c 00 00 00 00 00 00 00 00 00 00 00 00

对照以前制作USB HID设备的博客,可知三种通信模式,已经都实现了。

至此,我们在YIE002开发板的开篇中,规划的所有代码,都已经完成。从开发板的USB HID设备制作,到上位机的Windows软件、UEFI软件和Linux软件,全部介绍完毕。

YIE002开发板,与UEFI相关的学习,到此篇为止,全部结束。关于此开发板的其他嵌入式编程,请移步到我的“嵌入式开发”专栏中查阅,我会不定期更新的。

项目代码位置

项目代码位于:gitee.com/luobing4365/uefi-explorer

代码存储路径:/90/hidlibusb下

转载地址:http://skxxz.baihongyu.com/

你可能感兴趣的文章
openlayers 入门教程(十三):动画
查看>>
openlayers 入门教程(十二):定位与轨迹
查看>>
openlayers 入门教程(十五):与 canvas、echart,turf 等交互
查看>>
openlayers 入门教程(十四):第三方插件
查看>>
openlayers 入门教程(四):layers 篇
查看>>
OpenLayers 项目分析(三)-OpenLayers中定制JavaScript内置类
查看>>
Openlayers下载与加载geoserver的wms服务显示地图
查看>>
Openlayers中使用Cluster+Overlay实现点击单个要素和聚合要素时显示不同弹窗
查看>>
Openlayers中使用Cluster实现点位元素重合时动态聚合与取消聚合
查看>>
Openlayers中使用Cluster实现缩放地图时图层聚合与取消聚合
查看>>
Openlayers中使用Image的rotation实现车辆定位导航带转角(判断车辆图片旋转角度)
查看>>
Openlayers中加载Geoserver切割的EPSG:900913离线瓦片图层组
查看>>
Openlayers中多图层遮挡时调整图层上下顺序
查看>>
Openlayers中将某个feature置于最上层
查看>>
Openlayers中点击地图获取坐标并输出
查看>>
Openlayers中设置定时绘制和清理直线图层
查看>>
Openlayers图文版实战,vue项目从0到1做基础配置
查看>>
Openlayers实战:modifystart、modifyend互动示例
查看>>
Openlayers实战:判断共享单车是否在电子围栏内
查看>>
Openlayers实战:加载Bing地图
查看>>