完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
你好,我正在搜索一些示例代码来帮助我开始创建一个能模拟USB HID键盘的设备,我发现一些代码浏览互联网,代码是Emil Enchev在LiBoStand上共享的。我在PIC18F47 J53上进行了测试,该代码对Windows 7很有效(微控制器被检测出来,我可以在屏幕上复制一些打字)。在Linux上,设备立即被检测为键盘,这很好,但是我不能在屏幕上复制打字,这很奇怪。所以我做了一些研究,偶然发现了一个能够在内核级侦听键盘事件的工具,这个工具叫做“EVTEST”,当我插入微控制器并且代码开始运行时,EVTest就能够拾取微控制器的“键盘”事件,但是屏幕上没有键入显示。(我对这个问题做了更多的研究,发现Linux的X.Org服务器(用于将内核级的关键代码映射到屏幕显示的实际ASCII)可能不正确地解释微控制器的关键代码。如何让微控制器发送Linux事件正确解释并在屏幕上显示的键盘事件?任何能帮助我实现与Linux兼容的帮助或例子都是值得赞赏的:我在两个Linux Destro(ARCH和Ubuntu)上测试过,但没有运气。下面的代码是我在链接上找到的USB描述符,可能丢失了什么东西吗?
以上来自于百度翻译 以下为原文 Hello, i was searching for some sample code to help me get started on creating a device that would emulate a USB HID Keyboard, i found some code browsing the Internet The code was shared by Emil Enchev on libstock. I tested it on a PIC18F47J53, the code works great for Windows7 (the microcontroller is detected and i'm able to reproduce some typing on screen). On Linux the device is detected as a Keyboard right away, which is great but i'm not able to reproduce typing on screen, which is odd. so i did some research and stumbled upon a tool that is able to listen for keyboard events at kernel level, the tool is called "evtest", when i plug the microcontroller in and the code begins to run, evtest is able to pick up the microcontroller's "keyboard" events but no typing is shown on screen :( I did some more research on the issue and found out that Linux's X.org server (which is used to map kernel level keycodes into actual ASCII for screen display) might not be interpreting the microcontrollers keycodes correctly. How can i get the microcontroller to send keyboard events that Linux will interpret correctly and display them on screen? Any help or example that would point me in the right way to achieve compatibility with Linux is appreciated :). I tested it on two Linux distros (Arch and Ubuntu) but no luck. The following code is the u*** descriptor i found on the link, maybe it's missing something? const unsigned int USB_VENDOR_ID = 0x4406; const unsigned int USB_PRODUCT_ID = 0x0007; const char USB_SELF_POWER = 0x80; // Self powered 0xC0, 0x80 bus powered const char USB_MAX_POWER = 50; // Bus power required in units of 2 mA const char HID_INPUT_REPORT_BYTES = 64; const char HID_OUTPUT_REPORT_BYTES = 64; const char USB_TRANSFER_TYPE = 0x03; //0x03 Interrupt const char EP_IN_INTERVAL = 1; const char EP_OUT_INTERVAL = 1; const char USB_INTERRUPT = 1; const char USB_HID_EP = 1; const char USB_HID_RPT_SIZE = 63; /* Device Descriptor */ const struct { char bLength; // bLength - Descriptor size in bytes (12h) char bDescriptorType; // bDescriptorType - The constant DEVICE (01h) unsigned int bcdUSB; // bcdUSB - USB specification release number (BCD) char bDeviceClass; // bDeviceClass - Class Code char bDeviceSubClass; // bDeviceSubClass - Subclass code char bDeviceProtocol; // bDeviceProtocol - Protocol code char bMaxPacketSize0; // bMaxPacketSize0 - Maximum packet size for endpoint 0 unsigned int idVendor; // idVendor - Vendor ID unsigned int idProduct; // idProduct - Product ID unsigned int bcdDevice; // bcdDevice - Device release number (BCD) char iManufacturer; // iManufacturer - Index of string descriptor for the manufacturer char iProduct; // iProduct - Index of string descriptor for the product. char iSerialNumber; // iSerialNumber - Index of string descriptor for the serial number. char bNumConfigurations; // bNumConfigurations - Number of possible configurations } device_dsc = { 0x12, // bLength 0x01, // bDescriptorType 0x0200, // bcdUSB 0x00, // bDeviceClass 0x00, // bDeviceSubClass 0x00, // bDeviceProtocol 8, // bMaxPacketSize0 USB_VENDOR_ID, // idVendor USB_PRODUCT_ID, // idProduct 0x0001, // bcdDevice 0x01, // iManufacturer 0x02, // iProduct 0x00, // iSerialNumber 0x01 // bNumConfigurations }; /* Configuration 1 Descriptor */ const char configDescriptor1[]= { // Configuration Descriptor 0x09, // bLength - Descriptor size in bytes 0x02, // bDescriptorType - The constant CONFIGURATION (02h) 0x29,0x00, // wTotalLength - The number of bytes in the configuration descriptor and all of its subordinate descriptors 1, // bNumInterfaces - Number of interfaces in the configuration 1, // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests 0, // iConfiguration - Index of string descriptor for the configuration USB_SELF_POWER, // bmAttributes - Self/bus power and remote wakeup settings USB_MAX_POWER, // bMaxPower - Bus power required in units of 2 mA // Interface Descriptor 0x09, // bLength - Descriptor size in bytes (09h) 0x04, // bDescriptorType - The constant Interface (04h) 0, // bInterfaceNumber - Number identifying this interface 0, // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber. 2, // bNumEndpoint - Number of endpoints supported not counting endpoint zero 0x03, // bInterfaceClass - Class code 0, // bInterfaceSubclass - Subclass code 0, // bInterfaceProtocol - Protocol code 0, // iInterface - Interface string index // HID Class-Specific Descriptor 0x09, // bLength - Descriptor size in bytes. 0x21, // bDescriptorType - This descriptor's type: 21h to indicate the HID class. 0x11,0x01, // bcdHID - HID specification release number (BCD). 0x00, // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h. 1, // bNumDescriptors - Number of subordinate report and physical descriptors. 0x22, // bDescriptorType - The type of a class-specific descriptor that follows USB_HID_RPT_SIZE,0x00, // wDescriptorLength - Total length of the descriptor identified above. // Endpoint Descriptor 0x07, // bLength - Descriptor size in bytes (07h) 0x05, // bDescriptorType - The constant Endpoint (05h) USB_HID_EP | 0x80, // bEndpointAddress - Endpoint number and direction USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information 0x40,0x00, // wMaxPacketSize - Maximum packet size supported EP_IN_INTERVAL, // bInterval - Service interval or NAK rate // Endpoint Descriptor 0x07, // bLength - Descriptor size in bytes (07h) 0x05, // bDescriptorType - The constant Endpoint (05h) USB_HID_EP, // bEndpointAddress - Endpoint number and direction USB_TRANSFER_TYPE, // bmAttributes - Transfer type and supplementary information 0x40,0x00, // wMaxPacketSize - Maximum packet size supported EP_OUT_INTERVAL // bInterval - Service interval or NAK rate }; const struct { char report[]; }hid_rpt_desc = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x06, // USAGE (Keyboard) 0xa1, 0x01, // COLLECTION (Application) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl) 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x01, // LOGICAL_MAXIMUM (1) 0x75, 0x01, // REPORT_SIZE (1) 0x95, 0x08, // REPORT_COUNT (8) 0x81, 0x02, // INPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x08, // REPORT_SIZE (8) 0x81, 0x03, // INPUT (Cnst,Var,Abs) 0x95, 0x05, // REPORT_COUNT (5) 0x75, 0x01, // REPORT_SIZE (1) 0x05, 0x08, // USAGE_PAGE (LEDs) 0x19, 0x01, // USAGE_MINIMUM (Num Lock) 0x29, 0x05, // USAGE_MAXIMUM (Kana) 0x91, 0x02, // OUTPUT (Data,Var,Abs) 0x95, 0x01, // REPORT_COUNT (1) 0x75, 0x03, // REPORT_SIZE (3) 0x91, 0x03, // OUTPUT (Cnst,Var,Abs) 0x95, 0x06, // REPORT_COUNT (6) 0x75, 0x08, // REPORT_SIZE (8) 0x15, 0x00, // LOGICAL_MINIMUM (0) 0x25, 0x65, // LOGICAL_MAXIMUM (101) 0x05, 0x07, // USAGE_PAGE (Keyboard) 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated)) 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) 0x81, 0x00, // INPUT (Data,Ary,Abs) 0xc0 // End Collection }; //Language code string descriptor const struct { char bLength; char bDscType; unsigned int string[1]; } strd1 = { 4, 0x03, {0x0409} }; //Manufacturer string descriptor const struct{ char bLength; char bDscType; unsigned int string[10]; }strd2={ 22, //sizeof this descriptor string 0x03, {'T','U',' ','G','a','b','r','o','v','o'} }; //Product string descriptor const struct{ char bLength; char bDscType; unsigned int string[15]; }strd3={ 32, //sizeof this descriptor string 0x03, {'U','S','B',' ','M','a','n','i','p','u','l','a','t','o','r'} }; //Array of configuration descriptors const char* USB_config_dsc_ptr[1]; //Array of string descriptors const char* USB_string_dsc_ptr[3]; void USB_Init_Desc(){ USB_config_dsc_ptr[0] = &configDescriptor1; USB_string_dsc_ptr[0] = (const char*)&strd1; USB_string_dsc_ptr[1] = (const char*)&strd2; USB_string_dsc_ptr[2] = (const char*)&strd3; } |
|
相关推荐
3个回答
|
|
|
LIBOSTS发布的代码主要使用MIKROC的编译器。你可能会在他们的论坛得到更好的支持。为什么你不能尝试MICHCHIPUSB键盘演示?
以上来自于百度翻译 以下为原文 The code published libstock are mostly using their compiler like mikroC. You are likely to get a better support in their forum. Why can't you try with Microchip USB Keypad Demonstration? |
|
|
|
|
|
|
|
|
|
|
|
不久前,只是玩,我在我的PIC18F45 J50 PIM测试MLA USB设备HID键盘应用程序,并发现它的工作方式,我预期它在我的Linux工作站。在MLA分发中也有一个PIC18F47 JP53 PIM的代码,我“非常肯定”它也应该工作。(MLA是用于应用的微芯片库的缩写。)我的PIM***入到我的老PDEDEM HPC Explorer板中,但是PIM可以自己工作,而不插进PICkit(或任何)和USB电缆以外的任何东西。如果你不使用微芯片PIM,你可能不得不。更改一些IO引脚设置。(例如,按钮,LED)。例如:连接到PIC RB2的PIM上的“按钮”用来发送字符。(按一下按钮,你得到下一个字符的序列“A”,“B”,…)当前的MLA下载可以在MLA家庭页面找到。这里是我如何测试应用程序:在我的CITOS 6.9 Linux工作站上,我打开了一个终端窗口(一个GNOME终端用于我的安装)。没有连接到PC,我执行以下操作:LSUBANK,我看到了一个已经列举的USB设备列表。接下来……我将一个USB电缆从PC插入到PIM上的Mi-IB连接器。PIM上的LED连接到Re0,闪烁得很快。(这是一件好事)如果没有,那么就没有必要再进一步了。它没有被列举。现在假设好的事情发生了…在终端窗口,我进入LxBI看到一些东西在我插入“46J50:ID04D8:00 55 Microchip Technology,Inc.,现在(鼓滚))…我按下PIM上的按钮。PC上的终端窗口(仍然是焦点)显示AI再次按下按钮(Heck),一旦我看到它工作,我就必须一直按这个按钮。这种东西在工作时很有趣。每次我看到序列中的另一个字符,现在我看到,在终端窗口ABCDEE,所以最下面一行:巴达宾,巴达繁荣!现在,如果它像提供的那样工作,那就是在AppIdEclipse键盘.c中的AppHyKBooDtasks-()函数中输入你自己的代码来实现你自己的功能来决定发送什么东西(何时)。(用MLA V2017 03-06,MPLABX版本4.05,XC8版本1.44,皮奇3)测试,Daveht。TP://www. McCux.com……白羊座的应用
以上来自于百度翻译 以下为原文 A little while ago, just playing around, I tested the MLA USB Device HID Keyboard app on my PIC18F45J50 PIM, and found that it worked the way I expected it to on my Linux workstation. There is also code for a PIC18F47J53 PIM in the MLA distribution, and I'm "pretty sure" it should work also. (MLA is an abbreviation for Microchip Libraries for Applications.) My PIM is plugged into my old PICDEM HPC Explorer board, but the PIM can work on its own without plugging into anything other than a PICkit (or whatever) and a USB cable. If you aren't working with a Microchip PIM, you might have to change some IO pin setups. (Button, LEDs). For example: The "button" on the PIM, connected to PIC RB2 is used to send characters. (Press the button and you get the next character in the sequence 'a', 'b', ...) The current MLA download can be found at the MLA Home Page Here's how I test the app: On my Centos 6.9 Linux workstation, I open up a terminal window (a Gnome terminal for my installation). And... First of all, with the PIM not connected to the PC, I execute the following: lsu*** And I see a list of USB devices that have been enumerated. Next... I Plug a USB cable from the PC into the Mini-B connector on the PIM. An LED on the PIM, connected to RE0, blinks rapidly. (That's a Good Thing.) If not, then there's no need to go further. It's not enumerated. Now, assuming the Good Thing happened... In the terminal window, I enter lsu*** I see something that wasn't there before I plugged in the '46J50: ID 04d8:0055 Microchip Technology, Inc. Now, (drum roll please)... I press the button on the PIM. The terminal window (still in focus) on the PC shows a I press the button again, and again (Heck, once I see it working, I just gotta keep pressing the button. This stuff is kind of fun when it works.) Each time I see another character in the sequence, so now I see, in the terminal window abcde And so forth Bottom line: Bada-bing, bada-boom! Now, if it works as supplied, it's a matter of entering your own code in the APP_KeyboardTasks() function in app_device_keyboard.c to implement your own functionality to decide what (and when) to send stuff. (Tested with MLA v2017-03-06, MPLABX version 4.05, XC8 version 1.44, PICkit 3) Regards, Dave http://www.microchip.com/...aries-for-applications |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
473 浏览 0 评论
5793 浏览 9 评论
2334 浏览 8 评论
2224 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3530 浏览 3 评论
1124浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1097浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
873浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
475浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-2 08:56 , Processed in 1.141593 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
3122