原创 STM32 USB joystick程序 -- usb_desc.c

2008-4-26 18:30 6767 5 5 分类: MCU/ 嵌入式

usb_desc.c  提供了设备、端点、接口、字符串、群组、制造商描述符。因为joystick是HID设备,所以多了报告描述符。我加了一些注释如下。我的目标是先搞懂其大致的结构,至于细节可以慢慢再学习。


/* USB Standard Device Descriptor 设备描述符*/
const u8 Joystick_DeviceDescriptor[JOYSTICK_SIZ_DEVICE_DESC] =
  {
    0x12,                       /*bLength Byte0: the length of bytes is 0x12*/
    USB_DEVICE_DESCRIPTOR_TYPE, /*Byte1: 0x01, type of descriptor:Device bDescriptorType*/
    0x00,                       /*bcdUSB compatible with USB2.0 */
    0x02,                       /* 0x00,0x01 means compatible with USB1.0 */
    0x00,                       /*bDeviceClass 群组码*/
    0x00,                       /*bDeviceSubClass 设备次群组*/
    0x00,                       /*bDeviceProtocol 0表示无特定设备协议*/
    0x40,                       /*bMaxPacketSize 最大封包0x40*/
    0x83,                       /*idVendor 制造商ID(0x0483)*/
    0x04,
    0x10,                       /*idProduct 产品ID 0x5710*/
    0x57,
    0x00,                       /*bcdDevice rel. 2.00 以BCD表示设备发行序号*/
    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 配置数目为1,接下来只有1个配置描述符*/
  }
  ; /* Joystick_DeviceDescriptor */


/* USB Configuration Descriptor 配置,接口,端点,群组,制作商描述符*/
/*   All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */
const u8 Joystick_ConfigDescriptor[JOYSTICK_SIZ_CONFIG_DESC] =
  {
    0x09, /* bLength: Configuation Descriptor size 配置描述符 长度为9字节*/
    USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
    JOYSTICK_SIZ_CONFIG_DESC,         //JOYSTICK_SIZ_CONFIG_DESC 34
    0x00,    //34=09+09+09+07
    /* 34 0x00 总长度为0x34字节,包括配置,接口,端点,bytes与群组描述符。
       注意低字节在前。wTotalLength: Bytes returned */
    0x01,         /*bNumInterfaces: 1 interface,因此接下来只有1个接口描述符*/
    0x01,         /*bConfigurationValue: Configuration value 接口配置值*/
    0x00,         /*iConfiguration: Index of string descriptor describing
                                 the configuration*/
    0xC0,         /*bmAttributes: self powered */
    0x32,         /*MaxPower 100 mA: this current is used for detecting Vbus*/


    /************** Descriptor of Joystick Mouse interface ****************/
    /* 09 接口描述符*/
    0x09,         /*bLength: Interface Descriptor size*/
    USB_INTERFACE_DESCRIPTOR_TYPE,/*0x04, 代表接口 bDescriptorType: Interface descriptor type*/
    0x00,         /*bInterfaceNumber: Number of Interface,接口数目以0为基值*/
    0x00,         /*bAlternateSetting: Alternate setting 交互设置值*/
    0x01,         /*bNumEndpoints 端点数目1*/
    0x03,         /*bInterfaceClass: 群组类型:HID*/
    0x01,         /*bInterfaceSubClass : 1=BOOT, 0=no boot*/
    0x02,         /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/
    0,            /*iInterface: Index of string descriptor 0个字符串描述符*/
   
    /******************** Descriptor of Joystick Mouse HID **********/
    /* 18 */         //群组描述符
    0x09,         /*bLength: HID Descriptor size*/
    HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/
    0x00,        
    0x01,         /*bcdHID: HID Class Spec release number*/
    0x00,         /*bCountryCode: Hardware target country,无区域为0*/
    0x01,         /*bNumDescriptors: Number of HID class descriptors to follow,至少为1*/
    0x22,         /*bDescriptorType,描述符类型为报告*/
    JOYSTICK_SIZ_REPORT_DESC,/*wItemLength: Total length of Report descriptor报告描述符的长度*/
    0x00,
   
    /******************** Descriptor of Joystick Mouse endpoint ********************/
    /* 27 */       //端点描述符
    0x07,          /*bLength: Endpoint Descriptor size*/
    USB_ENDPOINT_DESCRIPTOR_TYPE, /*bDescriptorType:*/


    0x81,          /*bEndpointAddress: Endpoint Address (IN) bit0 为1表示IN,端点地址*/
    0x03,          /*bmAttributes: Interrupt endpoint 中断传输*/
                   //0 控制;1 等时 2 批量 3 中断
    0x04,          /*wMaxPacketSize: 4 Byte max */
    0x00,
    0x20,          /*bInterval: Polling Interval (32 ms) 以ms为单位,查询间隔*/
    //USB的中断并非一般意义的中断,而是查询
    /* 34 */
  }
  ; /* MOUSE_ConfigDescriptor  报告描述符*/
const u8 Joystick_ReportDescriptor[JOYSTICK_SIZ_REPORT_DESC] =
  {
    0x05,          /*Usage Page(Generic Desktop)*/
    0x01,
    0x09,          /*Usage(Mouse)*/
    0x02,
    0xA1,          /*Collection(Logical)*/
    0x01,
    0x09,          /*Usage(Pointer)*/
    0x01,
    /* 8 */
    0xA1,          /*Collection(Linked)*/
    0x00,
    0x05,          /*Usage Page(Buttons)*/
    0x09,
    0x19,          /*Usage Minimum(1)*/
    0x01,
    0x29,          /*Usage Maximum(3)*/
    0x03,
    /* 16 */
    0x15,          /*Logical Minimum(0)*/
    0x00,
    0x25,          /*Logical Maximum(1)*/
    0x01,
    0x95,          /*Report Count(3)*/
    0x03,
    0x75,          /*Report Size(1)*/
    0x01,
    /* 24 */
    0x81,          /*Input(Variable)*/
    0x02,
    0x95,          /*Report Count(1)*/
    0x01,
    0x75,          /*Report Size(5)*/
    0x05,
    0x81,          /*Input(Constant,Array)*/
    0x01,
    /* 32 */
    0x05,          /*Usage Page(Generic Desktop)*/
    0x01,
    0x09,          /*Usage(X axis)*/
    0x30,
    0x09,          /*Usage(Y axis)*/
    0x31,
    0x09,          /*Usage(Wheel)*/
    0x38,
    /* 40 */
    0x15,          /*Logical Minimum(-127)*/
    0x81,
    0x25,          /*Logical Maximum(127)*/
    0x7F,
    0x75,          /*Report Size(8)*/
    0x08,
    0x95,          /*Report Count(3)*/
    0x03,
    /* 48 */
    0x81,          /*Input(Variable, Relative)*/
    0x06,
    0xC0,          /*End Collection*/
    0x09,
    0x3c,
    0x05,
    0xff,
    0x09,
    /* 56 */
    0x01,
    0x15,
    0x00,
    0x25,
    0x01,
    0x75,
    0x01,
    0x95,
    /* 64 */
    0x02,
    0xb1,
    0x22,
    0x75,
    0x06,
    0x95,
    0x01,
    0xb1,
    /* 72 */
    0x01,
    0xc0
  }
  ; /* Joystick_ReportDescriptor */


//字符串描述符
/* USB String Descriptors (optional) */
const u8 Joystick_StringLangID[JOYSTICK_SIZ_STRING_LANGID] =
  {
    JOYSTICK_SIZ_STRING_LANGID,
    USB_STRING_DESCRIPTOR_TYPE,
    0x09,
    0x04
  }
  ; /* LangID = 0x0409: U.S. English */


最后还有几个字符描述符,这里就不copy了。

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
5
关闭 站长推荐上一条 /3 下一条