USB-JoyStickMouse移植到HID

1.       修改usb_conf.h
文件
#define EP_NUM     (3)  
改爲3個端點
#define ENDP2_RXADDR        (0x128)  
分配端點2輸出ram偏移
    /* #define EP1_IN_Callback   NOP_Process*/
#define  EP2_IN_Callback   NOP_Process
#define  EP3_IN_Callback   NOP_Process
#define  EP4_IN_Callback   NOP_Process
#define  EP5_IN_Callback   NOP_Process
#define  EP6_IN_Callback   NOP_Process
#define  EP7_IN_Callback   NOP_Process
 
#define  EP1_OUT_Callback   NOP_Process
//#define EP2_OUT_Callback   NOP_Process
#define  EP3_OUT_Callback   NOP_Process
#define  EP4_OUT_Callback   NOP_Process
#define  EP5_OUT_Callback   NOP_Process
#define  EP6_OUT_Callback   NOP_Process
#define  EP7_OUT_Callback   NOP_Process
允許端點1輸入回調,允許端點2輸出回調處理
2.       在main.c文件或usb_endp.c中定義回調函數
voidEP2_OUT_Callback(void)
{
 USB_ReceiveFlg = TRUE;
 PMAToUserBufferCopy(Receive_Buffer,ENDP2_RXADDR,16);
 MsgCmd =Receive_Buffer[0];
 switch(MsgCmd)
 {
      case 1:
                   LED1_ON;
                             break;
             case 2:
                   LED2_ON;
                             break;
             default:
                   LED1_OFF; LED2_OFF;
 }
     SetEPRxStatus(ENDP2,EP_RX_VALID);
}
3.      修改VID和PID
方便區別
 constuint8_t Joystick_DeviceDescriptor[JOYSTICK_SIZ_DEVICE_DESC] =
  {
   0x12,                      /*bLength */
   USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
   0x00,                       /*bcdUSB*/
   0x02,
   0x00,                      /*bDeviceClass*/
   0x00,                      /*bDeviceSubClass*/
   0x00,                      /*bDeviceProtocol*/
   0x40,                      /*bMaxPacketSize 64*/
    0x16,                       /*idVendor (0x0483)*/
    0x20,
    0x09,                       /*idProduct = 0x5710*/
    0x05,
   0x00,                      /*bcdDevice rel. 2.00*/
   0x02,
   1,                         /*Index of string descriptor describing
                                                 manufacturer */
   2,                         /*Index of string descriptor describing
                                                product*/
   3,                         /*Index of string descriptor describing the
                                                device serial number */
   0x01                       /*bNumConfigurations*/
}
4.       修改配置描述符
constuint8_t Joystick_ConfigDescriptor[JOYSTICK_SIZ_CONFIG_DESC] =
  {
    0x09, /* bLength: Configuration Descriptorsize */
    USB_CONFIGURATION_DESCRIPTOR_TYPE, /*bDescriptorType: Configuration */
    JOYSTICK_SIZ_CONFIG_DESC,
    /* wTotalLength: Bytes returned */
    0x00,
    0x01,         /*bNumInterfaces: 1 interface*/
    0x01,         /*bConfigurationValue: Configurationvalue*/
    0x00,         /*iConfiguration: Index of stringdescriptor describing
                                     theconfiguration*/
    0xE0,         /*bmAttributes: Self powered */
    0x32,         /*MaxPower 100 mA: this current isused for detecting Vbus*/
 
    /************** Descriptor of JoystickMouse interface ****************/
    /* 09 */
    0x09,         /*bLength: Interface Descriptor size*/
   USB_INTERFACE_DESCRIPTOR_TYPE,/*bDescriptorType: Interface descriptortype*/
    0x00,         /*bInterfaceNumber: Number ofInterface*/
    0x00,         /*bAlternateSetting: Alternatesetting*/
    0x02,         /*bNumEndpoints*/      
         //變爲兩個端口 
 ep1 IN ep2 OUT
    0x03,         /*bInterfaceClass: HID*/
    0x00,        /*bInterfaceSubClass : 1=BOOT, 0=no boot*/      
//自定義hid
    0x00,    /*nInterfaceProtocol : 0=none, 1=keyboard,2=mouse*/ 
//自定義hid
    0,            /*iInterface: Index of stringdescriptor*/
    /*********** Descriptor of Joystick MouseHID ********************/
    /* 18 */
    0x09,         /*bLength: HID Descriptor size*/
    HID_DESCRIPTOR_TYPE, /*bDescriptorType:HID*/
    0x00,         /*bcdHID: HID Class Spec releasenumber*/
    0x01,
    0x00,         /*bCountryCode: Hardware target country*/
    0x01,         /*bNumDescriptors: Number of HID classdescriptors to follow*/
    0x22,         /*bDescriptorType*/
    JOYSTICK_SIZ_REPORT_DESC,/*wItemLength:Total length of Report descriptor*/
    0x00,
    /************* Descriptor of Joystick Mouseendpoint ********************/
    /* 27 */
    0x07,          /*bLength: Endpoint Descriptor size*/
    USB_ENDPOINT_DESCRIPTOR_TYPE,/*bDescriptorType:*/
 
    0x81,          /*bEndpointAddress: Endpoint Address(IN)*/
              // bit 3...0 : the endpoint number
                   // bit 6...4 : reserved
                    // bit 7     : 0(OUT), 1(IN)
    0x03,          /*bmAttributes: Interrupt endpoint*/
    0x04,          /*wMaxPacketSize: 4 Byte max */  //包長爲4byte
    0x00,
    0x20,          /*bInterval: Polling Interval (32ms)*/
    /* 34 */
 
0x07,        /* bLength: Endpoint Descriptor size */
   USB_ENDPOINT_DESCRIPTOR_TYPE,      /*bDescriptorType: */
                    /*     Endpoint descriptor type */
    0x02,          /* bEndpointAddress: */                    //追加一個端點2作爲輸出
                    /*     Endpoint Address (OUT) */
    0x03,          /* bmAttributes: Interrupt endpoint */
    0x08,//0x02,     /* wMaxPacketSize: 20 Bytes max  */ //包長爲8byte
    0x00,
    0x10,          /* bInterval: Polling Interval (32 ms)*/
 
/* 41 */
  }
; /* MOUSE_ConfigDescriptor */
別忘了修改JOYSTICK_SIZ_CONFIG_DESC數組的大小爲41。
 
5.      最後修改報告描述符,同樣JOYSTICK_SIZ_REPORT_DESC修改爲27
constuint8_t Joystick_ReportDescriptor[JOYSTICK_SIZ_REPORT_DESC] =

 

 0x05,0x01, // USAGE_PAGE (Generic Desktop)

 
 0x09,0x00, // USAGE (0) 

 
 
 0xa1,0x01, // COLLECTION (Application)

  
 0x15,0x00, //     LOGICAL_MINIMUM (0)

  
 0x25,0xff, //     LOGICAL_MAXIMUM (255)

 
 0x19,0x01, //     USAGE_MINIMUM (1)

 
 0x29,0x08, //     USAGE_MAXIMUM (8) 

 0x95,0x08, //     REPORT_COUNT (8)

 
 0x75,0x08, //     REPORT_SIZE (8)

  
 0x81,0x02, //     INPUT (Data,Var,Abs)


 //--------------------------------輸出---------------------
 0x19,0x01,
 0x29,0x08,
 0x91,0x02,
 0xc0       // END_COLLECTION

 
}; /* CustomHID_ReportDescriptor */

 

https://blog.csdn.net/GZZXBCXM2005/article/details/51348395

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章