⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb_descriptor.c

📁 IT projecotr reference design.
💻 C
📖 第 1 页 / 共 3 页
字号:
    USBDESC_CFG_ATTRIBUTES,
    USBDESC_CFG_MAX_POWER
};


/*Interface 0 Descriptor for Mouse. EP1*/
const USB_INTERFACE_DESC InterfaceDescData_IF0_UserMode = 
{
    USBDESC_INTR_LENGTH,
    USB_INTERFACE_DESC_TYPE,
    USB_INTERFACE_0,
    USB_ALT_SETTING_0,
    USBDESC_IF0_NUM_EPS_USERMODE,
    USBDESC_IF0_CLASS_USERMODE,     
    USBDESC_IF0_SUBCLASS_USERMODE,
    USBDESC_IF0_PROTOCOL_USERMODE,
    USBDESC_STRING_MOUSE_IDX
};


/*Endpoint Descriptor for Mouse. EP1 IN descriptor only.*/
const USB_ENDPOINT_DESC EndpointDescData_IF0_Ep1In_UserMode = 
{
    USBDESC_EP_LENGTH,
    USB_ENDPOINT_DESC_TYPE,
    USBDESC_EP1_IN_ADDR,
    USB_INTERRUPT_EP,
    USB_MAX_PACKET_8,
    15                          /* Polling interval in ms. */
};

/*Interface 1 Descriptor for Keyboard EP2*/
const USB_INTERFACE_DESC InterfaceDescData_IF1_UserMode = 
{
    USBDESC_INTR_LENGTH,
    USB_INTERFACE_DESC_TYPE,
    USB_INTERFACE_1,
    USB_ALT_SETTING_0,
    USBDESC_IF0_NUM_EPS_USERMODE,
    USBDESC_IF0_CLASS_USERMODE,     
    USBDESC_IF0_SUBCLASS_USERMODE,
    USBDESC_IF0_PROTOCOL_USERMODE,
    USBDESC_STRING_KEYBOARD_IDX
};


/*Endpoint Descriptor for Keyboard. EP2 IN descriptor only.*/
const USB_ENDPOINT_DESC EndpointDescData_IF1_Ep2In_UserMode = 
{
    USBDESC_EP_LENGTH,
    USB_ENDPOINT_DESC_TYPE,
    USBDESC_EP2_IN_ADDR,
    USB_INTERRUPT_EP,
    USB_MAX_PACKET_8,
    20                          /* Polling interval in ms. */
};

/*HID class descriptors*/

/*This HID must be declared as an array because it has an odd number of bytes.  Declaring it as a
  typedef struct lets the compiler add padding on the end that messes up the Report descriptor length.
  Besides, we would need new typedefs every time we added more optional HID descriptors on the end, so 
  this is a little more straightforward.
  
  __align(4) is used here because this data structure may be sent to the host individually, and
  so must be aligned on a four byte boundary.*/
__align(4) const uint08 HidDescData_Mouse[USBDESC_HID_LENGTH_MOUSE] =
{
    USBDESC_HID_LENGTH_MOUSE,              // Length of HID descriptor (variable depending on number of optional HID descriptors)
    USB_HID_DESC_TYPE,                   // HID descriptor type
    0x11, 0x01,                          // HID specification release number: 0x0111 in little endian (spec release 1.11) 
    0,                                   // Non-localized hardware for this HID
    1,                                   // Number of optional descriptors associated with this interface  
    USB_HID_REPORT_DESC_TYPE,            // Optional HID descriptor #1 is a report descriptor
    USBDESC_REPORT_LENGTH_MOUSE, 0x00  // Length of optional HID descriptor #1 is 0x0032 in little endian 
};

__align(4) const uint08 HidDescData_Keyboard[USBDESC_HID_LENGTH_KEYBOARD] =
{
    USBDESC_HID_LENGTH_KEYBOARD,         // Length of HID descriptor (variable depending on number of optional HID descriptors)
    USB_HID_DESC_TYPE,                   // HID descriptor type
    0x11, 0x01,                          // HID specification release number: 0x0111 in little endian (spec release 1.11) 
    0,                                   // Non-localized hardware for this HID
    1,                                   // Number of optional descriptors associated with this interface  
    USB_HID_REPORT_DESC_TYPE,            // Optional HID descriptor #1 is a report descriptor
    USBDESC_REPORT_LENGTH_KEYBOARD, 0x00 // Length of optional HID descriptor #1 is 0x0032 in little endian 
};

/*Report descriptor for HID interface 0: Mouse (courtesy of USB-IF HID Descriptor Tool)*/
__align(4) const uint08 ReportDescriptor_Mouse[USBDESC_REPORT_LENGTH_MOUSE] = 
{
    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    0x09, 0x02,                    // USAGE (Mouse)
    0xa1, 0x01,                    // COLLECTION (Application)
    0x09, 0x01,                    //   USAGE (Pointer)
    0xa1, 0x00,                    //   COLLECTION (Physical)
    0x05, 0x09,                    //     USAGE_PAGE (Button)
    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
    0x29, 0x02,                    //     USAGE_MAXIMUM (Button 2)
    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    0x95, 0x02,                    //     REPORT_COUNT (2)
    0x75, 0x01,                    //     REPORT_SIZE (1)
    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    0x95, 0x01,                    //     REPORT_COUNT (1)
    0x75, 0x06,                    //     REPORT_SIZE (6)
    0x81, 0x01,                    //     INPUT (Cnst,Ary,Abs)
    0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
    0x09, 0x30,                    //     USAGE (X)
    0x09, 0x31,                    //     USAGE (Y)
    0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
    0x25, 0x81,                    //     LOGICAL_MAXIMUM (-127)
    0x75, 0x08,                    //     REPORT_SIZE (8)
    0x95, 0x02,                    //     REPORT_COUNT (2)
    0x81, 0x06,                    //     INPUT (Data,Var,Rel)
    0xc0,                          //     END_COLLECTION
    0xc0                           // END_COLLECTION
};

/*Report descriptor for HID interface 1: Keyboard (courtesy of USB-IF HID Descriptor Tool)*/
__align(4) const uint08 ReportDescriptor_Keyboard[USBDESC_REPORT_LENGTH_KEYBOARD] = 
{
    0x05, 0x01,					// Usage Page (Generic Desktop Control)
    0x09, 0x06,					// Usage (Keyboard)
    0xA1, 0x01,					// Collection (Application)
    0x05, 0x07,					//   Usage Page (Keyboard/Keypad Keys)
    0x19, 0xE0,					//   Usage Minimum (224)
    0x29, 0xE7,					//   Usage Maximum (231)
    0x15, 0x00,					//   Logical Minimum (0)
    0x25, 0x01,					//   Logical Maximum (1)
    0x75, 0x01,					//   Report Size (1)
    0x95, 0x08,					//   Report Count (8)
    0x81, 0x02,					//   Input (Data, Variable, Absolute)
    0x95, 0x01,					//   Report Count (1)
    0x75, 0x08,					//   Report Size (8)
    0x81, 0x01,					//   Input (Constant)
    0x95, 0x05,					//   Report Count (5)
    0x75, 0x01,					//   Report Size (1)
    0x05, 0x08,					//   Usage Page (LED)
    0x19, 0x01,					//   Usage Minimum (1)
    0x29, 0x05,					//   Usage Maximum (5)
    0x91, 0x02,					//   Ouput (Data, Variable, Absolute)
    0x95, 0x01,					//   Report Count (1)
    0x75, 0x03,					//   Report Size (3)
    0x91, 0x01,					//   Output (Constant)
    0x95, 0x06,					//   Report Count (6)
    0x75, 0x08,					//   Report Size (8)
    0x15, 0x00,					//   Logical Minimum (0)
    0x25, 0x68,					//   Logical Maximum (104)
    0x05, 0x07,					//   Usage Page (Keyboard/Keypad Keys)
    0x19, 0x00,					//   Usage Minimum (0)
    0x29, 0x68,					//   Usage Maximum (104)
    0x81, 0x00,					//   Input (Data, Array, Absolute)
    0xC0						// End Collection
};

/*Global storage arrays for configuration and string descriptors.  These are setup 
  in USBDESC_InitDescriptors*/
__align(4) uint08 ConfigDesc_Array_FactoryMode[82];    //Contains configuration, interface, endpoint, and HID descriptor data.
__align(4) uint08 ConfigDesc_Array_UserMode[82];    //Contains configuration, interface, endpoint, and HID descriptor data.

uint32 StringDesc_Array[USBDESC_STRING_NUM_IDX]; //Contains pointers to string descriptor data

/*-----------------------------------------------------------------------------*/
/*            End of Descriptor Data                                           */
/*-----------------------------------------------------------------------------*/

int08 USBDESC_InitDescriptors( uint08 Mode ) 
/**
 * Initializes the data structures for the configuration and string descriptors.
 *
 * A USB host requesting a Configuration descriptor is expecting a single data structure
 * to be sent back containing the configuration, interface, endpoint, and any class descriptors
 * as required by USB protocol (HID descriptor in this application).  This function
 * initializes that single data structure with the individual descriptor data structures defined 
 * above. 
 *
 * An array of pointers is used to store the starting addresses of the various string
 * descriptors.  This is done to enable faster access to the individual string data 
 * structures, and allow those data structures to be declared as '__align(4)' so that they
 * are aligned on four byte boundaries.
 *
 * @return PASS
 */

{
    uint08 *tmp_send_ptr;
    
    switch( Mode )
    {
    	case USBMAIN_FACTORYMODE:
  
            /*Load configuration descriptor data structure for USB factory mode.*/
            memcpy(ConfigDesc_Array_FactoryMode, &ConfigDescData_FactoryMode, ConfigDescData_FactoryMode.bLength);
            tmp_send_ptr = ConfigDesc_Array_FactoryMode + ConfigDescData_FactoryMode.bLength;

            memcpy(tmp_send_ptr, &InterfaceDescData_IF0_FactoryMode, InterfaceDescData_IF0_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + InterfaceDescData_IF0_FactoryMode.bLength;
            
            memcpy(tmp_send_ptr, &EndpointDescData_IF0_Ep1In_FactoryMode, EndpointDescData_IF0_Ep1In_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF0_Ep1In_FactoryMode.bLength;
            
            memcpy(tmp_send_ptr, &EndpointDescData_IF0_Ep1Out_FactoryMode, EndpointDescData_IF0_Ep1Out_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF0_Ep1Out_FactoryMode.bLength;

/********************************************************************************************/

            memcpy(tmp_send_ptr, &EndpointDescData_IF0_Ep2In_FactoryMode, EndpointDescData_IF0_Ep2In_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF0_Ep2In_FactoryMode.bLength;
            
            memcpy(tmp_send_ptr, &EndpointDescData_IF0_Ep2Out_FactoryMode, EndpointDescData_IF0_Ep2Out_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF0_Ep2Out_FactoryMode.bLength;

/********************************************************************************************/

            memcpy(tmp_send_ptr, &InterfaceDescData_IF1_FactoryMode, InterfaceDescData_IF1_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + InterfaceDescData_IF1_FactoryMode.bLength;
            
            memcpy(tmp_send_ptr, &HidDescData_Mouse, USBDESC_HID_LENGTH_MOUSE);
            tmp_send_ptr = tmp_send_ptr + USBDESC_HID_LENGTH_MOUSE;
            
            memcpy(tmp_send_ptr, &EndpointDescData_IF1_Ep3In_FactoryMode, EndpointDescData_IF1_Ep3In_FactoryMode.bLength); 
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF1_Ep3In_FactoryMode.bLength;

            memcpy(tmp_send_ptr, &InterfaceDescData_IF2_FactoryMode, InterfaceDescData_IF2_FactoryMode.bLength);
            tmp_send_ptr = tmp_send_ptr + InterfaceDescData_IF2_FactoryMode.bLength;
            
            memcpy(tmp_send_ptr, &HidDescData_Keyboard, USBDESC_HID_LENGTH_KEYBOARD);
            tmp_send_ptr = tmp_send_ptr + USBDESC_HID_LENGTH_KEYBOARD;
            
            memcpy(tmp_send_ptr, &EndpointDescData_IF2_Ep4In_FactoryMode, EndpointDescData_IF2_Ep4In_FactoryMode.bLength);

            /*Load string descriptor data. */
            StringDesc_Array[0] = (uint32)&StringDescData_Index0;
            StringDesc_Array[1] = (uint32)&StringDescData_Index1;
            StringDesc_Array[2] = (uint32)&StringDescData_Index2;
            StringDesc_Array[3] = (uint32)&StringDescData_USBMAIN_FACTORYMODEIndex3;
            StringDesc_Array[4] = (uint32)&StringDescData_Index4;
            StringDesc_Array[5] = (uint32)&StringDescData_Index5;
            StringDesc_Array[6] = (uint32)&StringDescData_Index6;

            break;
		
		case USBMAIN_USERMODE:	
			
            /*Load configuration descriptor data structure for USB user mode.*/
            memcpy(ConfigDesc_Array_UserMode, &ConfigDescData_UserMode, ConfigDescData_UserMode.bLength);
            tmp_send_ptr = ConfigDesc_Array_UserMode + ConfigDescData_UserMode.bLength;

            memcpy(tmp_send_ptr, &InterfaceDescData_IF0_UserMode, InterfaceDescData_IF0_UserMode.bLength);
            tmp_send_ptr = tmp_send_ptr + InterfaceDescData_IF0_UserMode.bLength;
            memcpy(tmp_send_ptr, &HidDescData_Mouse, USBDESC_HID_LENGTH_MOUSE);
            tmp_send_ptr = tmp_send_ptr + USBDESC_HID_LENGTH_MOUSE;
            memcpy(tmp_send_ptr, &EndpointDescData_IF0_Ep1In_UserMode, EndpointDescData_IF0_Ep1In_UserMode.bLength); 
            tmp_send_ptr = tmp_send_ptr + EndpointDescData_IF0_Ep1In_UserMode.bLength;

            memcpy(tmp_send_ptr, &InterfaceDescData_IF1_UserMode, InterfaceDescData_IF1_UserMode.bLength);
            tmp_send_ptr = tmp_send_ptr + InterfaceDescData_IF1_UserMode.bLength;
            memcpy(tmp_send_ptr, &HidDescData_Keyboard, USBDESC_HID_LENGTH_KEYBOARD);
            tmp_send_ptr = tmp_send_ptr + USBDESC_HID_LENGTH_KEYBOARD;
            memcpy(tmp_send_ptr, &EndpointDescData_IF1_Ep2In_UserMode, EndpointDescData_IF1_Ep2In_UserMode.bLength);

            /*Load string descriptor data. */
            StringDesc_Array[0] = (uint32)&StringDescData_Index0;
            StringDesc_Array[1] = (uint32)&StringDescData_Index1;
            StringDesc_Array[2] = (uint32)&StringDescData_Index2;
            StringDesc_Array[3] = (uint32)&StringDescData_USBMAIN_USERMODEIndex3;
            StringDesc_Array[4] = (uint32)&StringDescData_Index4;
            StringDesc_Array[5] = (uint32)&StringDescData_Index5;
            StringDesc_Array[6] = (uint32)&StringDescData_Index6;

            break;
	};
  

    return(PASS);
}


int08 USBDESC_SendDescriptor(uint08 Ep, USB_DEV_REQUEST *Dev_Request, uint08 Max_Pkt, uint08 Mode)
/**
 * Sends the USB host the requested descriptor information (Dev_Request) over the specified 
 * Ep endpoint.
 *
 * Descriptor data is sent based on descriptor type and current USB Mode.
 * A USB device must never return more descriptor data than is requested by the host
 * in the setup request message (wLength). It may return less.  If the host does not
 * request enough data to get the entire descriptor, then it will send another setup
 * message with a new size based on the size in the previously returned descriptor data.
 *
 * @return PASS or FAIL
 */
{
  int08  result = FAIL;
  uint32 Req_Len = Dev_Request->wLength;           /*host requested descriptor length*/
  uint08 Desc_Type  = Dev_Request->wValue >> 8;    /*host requested descriptor type*/
  uint32 wIndex = Dev_Request->wIndex;             /*LANG_ID for String descriptors, Interface for HID descriptors*/
  uint08 Desc_Idx = Dev_Request->wValue & 0x00ff;  /*Descriptor Index*/
  uint08 send_length;
  uint32 zero_buffer;                              /*for sending zero byte packet if needed*/
  USB_STRING_DESC *stringPtr;                      /*pointer to string descriptor data structure*/     
  USBMAIN_EP_DATA *ep = &Ep_Data[Ep];
  
  switch (Desc_Type)
	{
	case USB_DEVICE_DESC_TYPE:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -