Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.96 KB | None | 0 0
  1. const unsigned int USB_VENDOR_ID = 0x1234;
  2. const unsigned int USB_PRODUCT_ID = 0x0001;
  3. const char USB_SELF_POWER = 0x80;            // Self powered 0xC0,  0x80 bus powered
  4. const char USB_MAX_POWER = 50;               // Bus power required in units of 2 mA
  5. const char HID_INPUT_REPORT_BYTES = 64;
  6. const char HID_OUTPUT_REPORT_BYTES = 64;
  7. const char USB_TRANSFER_TYPE = 0x03;         //0x03 Interrupt
  8. const char EP_IN_INTERVAL = 1;
  9. const char EP_OUT_INTERVAL = 1;
  10.  
  11. const char USB_INTERRUPT = 1;
  12. const char USB_HID_EP = 1;
  13. const char USB_HID_RPT_SIZE = 33;
  14.  
  15. /* Device Descriptor */
  16. const struct {
  17.     char bLength;               // bLength         - Descriptor size in bytes (12h)
  18.     char bDescriptorType;       // bDescriptorType - The constant DEVICE (01h)
  19.     unsigned int bcdUSB;        // bcdUSB          - USB specification release number (BCD)
  20.     char bDeviceClass;          // bDeviceClass    - Class Code
  21.     char bDeviceSubClass;       // bDeviceSubClass - Subclass code
  22.     char bDeviceProtocol;       // bDeviceProtocol - Protocol code
  23.     char bMaxPacketSize0;       // bMaxPacketSize0 - Maximum packet size for endpoint 0
  24.     unsigned int idVendor;      // idVendor        - Vendor ID
  25.     unsigned int idProduct;     // idProduct       - Product ID
  26.     unsigned int bcdDevice;     // bcdDevice       - Device release number (BCD)
  27.     char iManufacturer;         // iManufacturer   - Index of string descriptor for the manufacturer
  28.     char iProduct;              // iProduct        - Index of string descriptor for the product.
  29.     char iSerialNumber;         // iSerialNumber   - Index of string descriptor for the serial number.
  30.     char bNumConfigurations;    // bNumConfigurations - Number of possible configurations
  31. } device_dsc = {
  32.       0x12,                   // bLength
  33.       0x01,                   // bDescriptorType
  34.       0x0200,                 // bcdUSB
  35.       0x00,                   // bDeviceClass
  36.       0x00,                   // bDeviceSubClass
  37.       0x00,                   // bDeviceProtocol
  38.       8,                      // bMaxPacketSize0
  39.       USB_VENDOR_ID,          // idVendor
  40.       USB_PRODUCT_ID,         // idProduct
  41.       0x0001,                 // bcdDevice
  42.       0x01,                   // iManufacturer
  43.       0x02,                   // iProduct
  44.       0x00,                   // iSerialNumber
  45.       0x01                    // bNumConfigurations
  46.   };
  47.  
  48. /* Configuration 1 Descriptor */
  49. const char configDescriptor1[]= {
  50.     // Configuration Descriptor
  51.     0x09,                   // bLength             - Descriptor size in bytes
  52.     0x02,                   // bDescriptorType     - The constant CONFIGURATION (02h)
  53.     0x29,0x00,              // wTotalLength        - The number of bytes in the configuration descriptor and all of its subordinate descriptors
  54.     1,                      // bNumInterfaces      - Number of interfaces in the configuration
  55.     1,                      // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests
  56.     0,                      // iConfiguration      - Index of string descriptor for the configuration
  57.     USB_SELF_POWER,         // bmAttributes        - Self/bus power and remote wakeup settings
  58.     USB_MAX_POWER,          // bMaxPower           - Bus power required in units of 2 mA
  59.  
  60.     // Interface Descriptor
  61.     0x09,                   // bLength - Descriptor size in bytes (09h)
  62.     0x04,                   // bDescriptorType - The constant Interface (04h)
  63.     0,                      // bInterfaceNumber - Number identifying this interface
  64.     0,                      // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber.
  65.     2,                      // bNumEndpoint - Number of endpoints supported not counting endpoint zero
  66.     0x03,                   // bInterfaceClass - Class code
  67.     0,                      // bInterfaceSubclass - Subclass code
  68.     0,                      // bInterfaceProtocol - Protocol code
  69.     0,                      // iInterface - Interface string index
  70.  
  71.     // HID Class-Specific Descriptor
  72.     0x09,                   // bLength - Descriptor size in bytes.
  73.     0x21,                   // bDescriptorType - This descriptor's type: 21h to indicate the HID class.
  74.     0x01,0x01,              // bcdHID - HID specification release number (BCD).
  75.     0x00,                   // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h.
  76.     1,                      // bNumDescriptors - Number of subordinate report and physical descriptors.
  77.     0x22,                   // bDescriptorType - The type of a class-specific descriptor that follows
  78.     USB_HID_RPT_SIZE,0x00,  // wDescriptorLength - Total length of the descriptor identified above.
  79.  
  80.     // Endpoint Descriptor
  81.     0x07,                   // bLength - Descriptor size in bytes (07h)
  82.     0x05,                   // bDescriptorType - The constant Endpoint (05h)
  83.     USB_HID_EP | 0x80,      // bEndpointAddress - Endpoint number and direction
  84.     USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information    
  85.     0x40,0x00,              // wMaxPacketSize - Maximum packet size supported
  86.     EP_IN_INTERVAL,         // bInterval - Service interval or NAK rate
  87.  
  88.     // Endpoint Descriptor
  89.     0x07,                   // bLength - Descriptor size in bytes (07h)
  90.     0x05,                   // bDescriptorType - The constant Endpoint (05h)
  91.     USB_HID_EP,             // bEndpointAddress - Endpoint number and direction
  92.     USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information
  93.     0x40,0x00,              // wMaxPacketSize - Maximum packet size supported    
  94.     EP_OUT_INTERVAL         // bInterval - Service interval or NAK rate
  95. };
  96.  
  97. const struct {
  98.   char report[USB_HID_RPT_SIZE];
  99. }hid_rpt_desc =
  100.   {
  101.      {0x06, 0x00, 0xFF,       // Usage Page = 0xFF00 (Vendor Defined Page 1)
  102.       0x09, 0x01,             // Usage (Vendor Usage 1)
  103.       0xA1, 0x01,             // Collection (Application)
  104.   // Input report
  105.       0x19, 0x01,             // Usage Minimum
  106.       0x29, 0x40,             // Usage Maximum
  107.       0x15, 0x00,             // Logical Minimum (data bytes in the report may have minimum value = 0x00)
  108.       0x26, 0xFF, 0x00,       // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
  109.       0x75, 0x08,             // Report Size: 8-bit field size
  110.       0x95, HID_INPUT_REPORT_BYTES,// Report Count
  111.       0x81, 0x02,             // Input (Data, Array, Abs)
  112.   // Output report
  113.       0x19, 0x01,             // Usage Minimum
  114.       0x29, 0x40,             // Usage Maximum
  115.       0x75, 0x08,             // Report Size: 8-bit field size
  116.       0x95, HID_OUTPUT_REPORT_BYTES,// Report Count
  117.       0x91, 0x02,             // Output (Data, Array, Abs)
  118.       0xC0}                   // End Collection
  119.   };
  120.  
  121. //Language code string descriptor
  122. const struct {
  123.   char bLength;
  124.   char bDscType;
  125.   unsigned int string[1];
  126.   } strd1 = {
  127.       4,
  128.       0x03,
  129.       {0x0409}
  130.     };
  131.  
  132.  
  133. //Manufacturer string descriptor
  134. const struct{
  135.   char bLength;
  136.   char bDscType;
  137.   unsigned int string[20];
  138.   }strd2={
  139.     42,           //sizeof this descriptor string
  140.     0x03,
  141.     {'S','t','u','d','e','n','t',' ','C','o','m','p','a','n','i','o','n',' ','S','A'}
  142.   };
  143.  
  144. //Product string descriptor
  145. const struct{
  146.   char bLength;
  147.   char bDscType;
  148.   unsigned int string[29];
  149. }strd3={
  150.     60,          //sizeof this descriptor string
  151.     0x03,
  152.     {'U','S','B',' ','B','u','s',' ','C','o','m','m','u','n','i','c','a','t','i','o','n',' ','p','r','o','j','e','c','t'}
  153.  };
  154.  
  155. //Array of configuration descriptors
  156. const char* USB_config_dsc_ptr[1];
  157.  
  158. //Array of string descriptors
  159. const char* USB_string_dsc_ptr[3];
  160.  
  161. void USB_Init_Desc(){
  162.   USB_config_dsc_ptr[0] = &configDescriptor1;
  163.   USB_string_dsc_ptr[0] = (const char*)&strd1;
  164.   USB_string_dsc_ptr[1] = (const char*)&strd2;
  165.   USB_string_dsc_ptr[2] = (const char*)&strd3;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement