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

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

(请保留-> 作者: 罗冰 )

YIE002USB开发板之Linux编程


上一篇使用hidapi的hidraw方式,实现了Linux上位机与USB HID设备通信的测试程序。不过,只实现了三种通信方式中的两种。本篇将使用hidapi的libusb方式,将三种通信方式全部实现。

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_CALL  hid_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;	/* Account for the report ID */	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 String: (77) My123Indexed String 1: RobinFeature Report   report number:0   a0 03 0b 0c 00 00 00 00 00 00 00 00 00 00 00 00 Input Report   report number:0   a0 02 0b 0c 00 00 00 00 00 00 00 00 00 00 00 00 Read data,Length=16   a0 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地址:https://gitee.com/luobing4365/uefi-exolorer

项目代码位于:/ 90 hidlibusb下


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

你可能感兴趣的文章
Mysql join原理
查看>>
MySQL Join算法与调优白皮书(二)
查看>>
Mysql order by与limit混用陷阱
查看>>
Mysql order by与limit混用陷阱
查看>>
mysql order by多个字段排序
查看>>
MySQL Order By实现原理分析和Filesort优化
查看>>
mysql problems
查看>>
mysql replace first,MySQL中处理各种重复的一些方法
查看>>
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>