您的位置:首页 > 科技 > 能源 > USB-HID 键盘描述符简介

USB-HID 键盘描述符简介

2024/12/23 9:36:24 来源:https://blog.csdn.net/pleboyzha/article/details/139086147  浏览:    关键词:USB-HID 键盘描述符简介

USB-HID 键盘描述符简介

USB-HID键盘设备描述符

#define DEVICE_DESCRIPTOR_SIZE	                0x12
#define USB_CTRL_TEST_SZIE      8
#define CONFIG_DESCRIPTOR_SIZE_DUSB				0x0029 //0x0022//0x0029
#define HID_REPORT_DESCRIPTOR_SIZE_DUSB		 	0x0041 //0x0041//0x007Cconst u8 DEVICEDESC_DUSB[18] = {DEVICE_DESCRIPTOR_SIZE,     // bLength0x01,						// bDescriptorType	= DEVICE0x10,0x01,					// bcdUSB(02.00 full-speed)0x00,						// bDeviceClass0x00,						// bDeviceSubClass0x00,//0x00					// bDeviceProtocolUSB_CTRL_TEST_SZIE,			     // bMaxPacketSize - 64bytes0x93,0x29,					// idVendOR      	= 00790x02,0x60,					// idProduct		= 181C0x00,0x01,					// bcdDevice		= 01.000x02,						// iManufacturer0x01,						// iProduct0x00,						// iSerialNumber0x01,
};

USB-HID键盘配置和其他描述符

const u8 HIDCONFIGDESC_DUSB[]={// Configuration Descriptor0x09,						// length of descriptor (9 bytes)0x02,						// descriptor type (CONFIGURATION)BYTE0(CONFIG_DESCRIPTOR_SIZE_DUSB),// Total length bytes (9 + 9 + 9 + 7 + 7)BYTE1(CONFIG_DESCRIPTOR_SIZE_DUSB),0x01,						// bNumInterfaces (1)0x01,						// bConfigurationValue (1)0x00,						// iConfiguration0x80,						// bmAttributes (bus powered)0xc8,						// MaxPower (400mA)// Configuration Descriptor End// Interface 0// InterfaceDescriptor0x09,						// bLength (9 bytes)0x04,						// bDescriptorType (INTERFACE)0x00,						// bInterfaceNumber (0)0x00,						// bAlternateSetting (0)0x02,//0X01						// bNumEndpoints (2)0x03,						// bInterfaceClass0x01,						// bInterfaceSubClass ()0x01,						// bInterfaceProtocol ()0x00,						// iInterface (not supported)// InterfaceDescriptor end// class descriptor0x09,						// bLength0x21,						// bDescritptorType0x10,0x01,					// bcdHID(01.11)0x00,						// bCountryCode0x01,						// bNumDescriptors0x22,						// bDescriptorTypeBYTE0(HID_REPORT_DESCRIPTOR_SIZE_DUSB),// wItemLength (tot. len. of report descriptor )BYTE1(HID_REPORT_DESCRIPTOR_SIZE_DUSB),// Endpoint1Descriptor0x07,						// descriptor length (7 bytes)0x05,						// descriptor type (ENDPOINT)0x81,						// endpoint address (IN endpoint, endpoint 1)0x03,						// endpoint attributes (interrupt)USB_CTRL_TEST_SZIE,0x00,                  //EP1_PACKET_SIZE_LE,			// maximum packet size0x0A,//0x0A,				// 10ms// Endpoint1Descriptor end// Endpoint2Descriptor0x07,						// bLength0x05,						// bDescriptorType0x82,						// bEndpointAddress (ep1, OUT)0x03,						// bmAttributes (INT)USB_CTRL_TEST_SZIE,0x00,  //EP2_PACKET_SIZE_LE,			// wMaxPacketSize (lsb first)0x0A,						// bInterval - 10ms// Endpoint2Descriptor End
}

USB-HID键盘报告描述符

const u8 HIDREPORTDESC_DUSB[] =
{0x05,0x01,// Global Generic Desktop0x09,0x06,// Local KeyBoard0xA1,0x01,// Main app collection0x05,0x07,// Global KeyBoard//第1字节0x19,0xe0,// Local Usage Min (KeyBoard LeftControl)0x29,0xe7,// Local Usage Max (KeyBoard Right GUI)0x15,0x00,// Global Logical Min0x25,0x01,// Global Logical Max0x95,0x08,// Global ReportCount0x75,0x01,// Global ReportSize0x81,0x02,// Main Input(Data,Var,Abs)//第2字节0x95,0x01,// Global ReportCount0x75,0x08,// Global ReportSize0x81,0x01,// Main Input(Cnst,Var,Abs)//第3-8字节0x95,0x03,// Global ReportCount0x75,0x01,// Global ReportSize0x05,0x08,// Global Logical Min0x19,0x01,0x29,0x03,// Local Usage Max0x91,0x02,// Main Output(Data,Ary,Abs)//1字节输出报告0x95,0x05,// Global Logical Min0x75,0x01,// Global Logical Max0x91,0x01,// Global ReportCount0x95,0x06,// Global ReportSize0x75,0x08,// Global LED0x15,0x00,// Local Usage Min0x26,0x0f,0x00,// Local Usage Max0x05,0x07,// Main Output(Data,Var,Abs)//补足上面变成1个字节0x19,0x00,// Global ReportCount0x2a,0xff,0x00,// Global ReportSize0x81,0x00,// Main Output(Cnst,Var,Abs)0xc0      // Main End collection
}

在设置地址的时候,也需要将数据长度改了

//Set Address
AT(.usbdev.com)
static bool do_set_address(ude_t *ude)
{
//    printf("%s\n", __func__);spb_wrap_t *spb = &ude->spb;if(Read_USB_Mode==USB_Xbox_Mode){ude->ep.epsize = USB_CTRL_SIZE;             //默认为8,收到SetAddress后,将MAXEPSIZE改为64。}else{ude->ep.epsize = USB_CTRL_TEST_SZIE;//这个是键盘的}if (ude->cfgval) {return false;                           //已经进行配置,不支持设置地址}ude->devaddr = BYTE0(spb->val);ude->set_faddr = 1;return true;
}

在发送数据的时候,同时需要将len改成8

void ude_hid_tx_process(void)
{//USB - HID 发送数据u8 len=9;epcb_t *epcb=udp_1.int_in;if(bXUsbIntInput_sync==1){if(USB_SendData_Cnt<200) USB_SendData_Cnt++;if(Read_USB_Mode==USB_Switch_Mode){len=64;memcpy(epcb->buf, &PS3InBuffer[1], len);}else{len = 8;//USB_CTRL_TEST_SZIE//printf("ude_hid_tx_process.. send:%d\r\n",len);memcpy(epcb->buf, Send_USB_DataBuf, len);}epcb->xptr=epcb->buf;epcb->xlen = len;epcb->xcnt = 0;epcb->first_pkt=1;}psfr_t sfr =  epcb->sfr;usb_set_cur_ep(epcb);if (0 == (sfr[TXCSR1] & BIT(0)))     //TX OK{if (epcb->first_pkt == 0 && epcb->xlen == 0){
//            if(Read_USB_Mode==USB_Switch_Mode)Send_Switch_ct=5;bXUsbIntInput_sync=0;work_cheak_ct=0;return;     //ok}epcb->first_pkt = 0;len = (epcb->xlen >= epcb->epsize) ? epcb->epsize : epcb->xlen;USB_ENTER_CRITICAL();USBEP1TXADR=DMA_ADR(epcb->xptr);USBCON2 = BIT(16+1) | len;             //设置FIFOsfr[TXCSR1] = BIT(0);                  //Kick TxRdydelay_us(2);                           //等待TxRdy同步USB_EXIT_CRITICAL();epcb->xptr += len;epcb->xlen -= len;epcb->xcnt += len;}bXUsbIntInput_sync=2;
}

最后根据键盘键值发送数据即可

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com