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//0x007C

const u8 DEVICEDESC_DUSB[18] = {
    DEVICE_DESCRIPTOR_SIZE,     // bLength
	0x01,						// bDescriptorType	= DEVICE
	0x10,0x01,					// bcdUSB(02.00 full-speed)
	0x00,						// bDeviceClass
	0x00,						// bDeviceSubClass
	0x00,//0x00					// bDeviceProtocol
	USB_CTRL_TEST_SZIE,			     // bMaxPacketSize - 64bytes
	0x93,0x29,					// idVendOR      	= 0079
	0x02,0x60,					// idProduct		= 181C
	0x00,0x01,					// bcdDevice		= 01.00
	0x02,						// iManufacturer
	0x01,						// iProduct
	0x00,						// iSerialNumber
	0x01,
};

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

const u8 HIDCONFIGDESC_DUSB[]={
 // Configuration Descriptor
    0x09,						// 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,						// iConfiguration
    0x80,						// bmAttributes (bus powered)
    0xc8,						// MaxPower (400mA)
    // Configuration Descriptor End

    // Interface 0
    // InterfaceDescriptor
    0x09,						// bLength (9 bytes)
    0x04,						// bDescriptorType (INTERFACE)
    0x00,						// bInterfaceNumber (0)
    0x00,						// bAlternateSetting (0)
    0x02,//0X01						// bNumEndpoints (2)
    0x03,						// bInterfaceClass
    0x01,						// bInterfaceSubClass ()
    0x01,						// bInterfaceProtocol ()
    0x00,						// iInterface (not supported)
    // InterfaceDescriptor end

    // class descriptor
    0x09,						// bLength
    0x21,						// bDescritptorType
    0x10,0x01,					// bcdHID(01.11)
    0x00,						// bCountryCode
    0x01,						// bNumDescriptors
    0x22,						// bDescriptorType
    BYTE0(HID_REPORT_DESCRIPTOR_SIZE_DUSB),// wItemLength (tot. len. of report descriptor )
    BYTE1(HID_REPORT_DESCRIPTOR_SIZE_DUSB),


    // Endpoint1Descriptor
    0x07,						// 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 size
    0x0A,//0x0A,				// 10ms
    // Endpoint1Descriptor end

    // Endpoint2Descriptor
    0x07,						// bLength
    0x05,						// bDescriptorType
    0x82,						// 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 Desktop
    0x09,0x06,// Local KeyBoard
    0xA1,0x01,// Main app collection
    0x05,0x07,// Global KeyBoard
    //第1字节
    0x19,0xe0,// Local Usage Min (KeyBoard LeftControl)
    0x29,0xe7,// Local Usage Max (KeyBoard Right GUI)
    0x15,0x00,// Global Logical Min
    0x25,0x01,// Global Logical Max
    0x95,0x08,// Global ReportCount
    0x75,0x01,// Global ReportSize
    0x81,0x02,// Main Input(Data,Var,Abs)
    //第2字节
    0x95,0x01,// Global ReportCount
    0x75,0x08,// Global ReportSize
    0x81,0x01,// Main Input(Cnst,Var,Abs)
    //第3-8字节
    0x95,0x03,// Global ReportCount
    0x75,0x01,// Global ReportSize
    0x05,0x08,// Global Logical Min
    0x19,0x01,
    0x29,0x03,// Local Usage Max
    0x91,0x02,// Main Output(Data,Ary,Abs)
    //1字节输出报告
    0x95,0x05,// Global Logical Min
    0x75,0x01,// Global Logical Max
    0x91,0x01,// Global ReportCount
    0x95,0x06,// Global ReportSize
    0x75,0x08,// Global LED
    0x15,0x00,// Local Usage Min
    0x26,0x0f,0x00,// Local Usage Max
    0x05,0x07,// Main Output(Data,Var,Abs)
    //补足上面变成1个字节
    0x19,0x00,// Global ReportCount
    0x2a,0xff,0x00,// Global ReportSize
    0x81,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;             //设置FIFO
        sfr[TXCSR1] = BIT(0);                  //Kick TxRdy

        delay_us(2);                           //等待TxRdy同步
        USB_EXIT_CRITICAL();

        epcb->xptr += len;
        epcb->xlen -= len;
        epcb->xcnt += len;
    }

    bXUsbIntInput_sync=2;
}

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农-老七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值