📄 usb_type.h
字号:
/*******************************/
/*******************************/
/**********描述符定义***********/
/*******************************/
/*******************************/
#define SWAP(x) ((((x) & 0x00FF) << 8) | (((x) >> 8) & 0x00FF))
#define CharToInt(x,y) (((x)&0x00ff) | (((y)&0x00ff)<<8))
#define MSB(x) (((x) >> 8) & 0x00FF)
#define LSB(x) ((x) & 0x00FF)
#define MAXIMUM_USB_STRING_LENGTH 255
#define FALSE 0
#define TRUE (!FALSE)
#define NUM_ENDPOINTS 4
#define CONFIG_DESCRIPTOR_LENGTH sizeof(USB_CONFIGURATION_DESCRIPTOR) \
+ sizeof(USB_INTERFACE_DESCRIPTOR) \
+ (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))
/*(18+9+4*7)=55 */
// values for the bits returned by the USB GET_STATUS command
#define USB_GETSTATUS_SELF_POWERED 0x01
#define USB_GETSTATUS_REMOTE_WAKEUP_ENABLED 0x02
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define USB_STRING_DESCRIPTOR_TYPE 0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
#define USB_POWER_DESCRIPTOR_TYPE 0x06
#define USB_DESCRIPTOR_MAKE_TYPE_AND_INDEX(d, i) ((USHORT)((USHORT)d<<8 | i))
//
// Values for bmAttributes field of an
// endpoint descriptor
//
#define USB_ENDPOINT_TYPE_MASK 0x03
#define USB_ENDPOINT_TYPE_CONTROL 0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
#define USB_ENDPOINT_TYPE_BULK 0x02
#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
//
// definitions for bits in the bmAttributes field of a
// configuration descriptor.
//
#define USB_CONFIG_POWERED_MASK 0xc0
#define USB_CONFIG_BUS_POWERED 0x80
#define USB_CONFIG_SELF_POWERED 0x40
#define USB_CONFIG_REMOTE_WAKEUP 0x20
//
// Endpoint direction bit, stored in address
//
#define USB_ENDPOINT_DIRECTION_MASK 0x80
// test direction bit in the bEndpointAddress field of
// an endpoint descriptor.
#define USB_ENDPOINT_DIRECTION_OUT(addr) (!((addr) & USB_ENDPOINT_DIRECTION_MASK))
#define USB_ENDPOINT_DIRECTION_IN(addr) ((addr) & USB_ENDPOINT_DIRECTION_MASK)
//
// USB defined request codes
// see chapter 9 of the USB 1.0 specifcation for
// more information.
//
// These are the correct values based on the USB 1.0
// specification
#define USB_REQUEST_GET_STATUS 0x00
#define USB_REQUEST_CLEAR_FEATURE 0x01
#define USB_REQUEST_SET_FEATURE 0x03
#define USB_REQUEST_SET_ADDRESS 0x05
#define USB_REQUEST_GET_DESCRIPTOR 0x06
#define USB_REQUEST_SET_DESCRIPTOR 0x07
#define USB_REQUEST_GET_CONFIGURATION 0x08
#define USB_REQUEST_SET_CONFIGURATION 0x09
#define USB_REQUEST_GET_INTERFACE 0x0A
#define USB_REQUEST_SET_INTERFACE 0x0B
#define USB_REQUEST_SYNC_FRAME 0x0C
//
// defined USB device classes
//
#define USB_DEVICE_CLASS_RESERVED 0x00
#define USB_DEVICE_CLASS_AUDIO 0x01
#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
#define USB_DEVICE_CLASS_MONITOR 0x04
#define USB_DEVICE_CLASS_PHYSICAL_INTERFACE 0x05
#define USB_DEVICE_CLASS_POWER 0x06
#define USB_DEVICE_CLASS_PRINTER 0x07
#define USB_DEVICE_CLASS_STORAGE 0x08
#define USB_DEVICE_CLASS_HUB 0x09
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
//
// USB defined Feature selectors
//
#define USB_FEATURE_ENDPOINT_STALL 0x0000
#define USB_FEATURE_REMOTE_WAKEUP 0x0001
#define USB_FEATURE_POWER_D0 0x0002
#define USB_FEATURE_POWER_D1 0x0003
#define USB_FEATURE_POWER_D2 0x0004
#define USB_FEATURE_POWER_D3 0x0005
/*************************************************************************
/ basic #defines
/*************************************************************************/
#define MAX_ENDPOINTS (unsigned char)0x3
#define EP0_TX_FIFO_SIZE 16
#define EP0_RX_FIFO_SIZE 16
#define EP0_PACKET_SIZE 16
#define EP1_TX_FIFO_SIZE 16
#define EP1_RX_FIFO_SIZE 16
#define EP1_PACKET_SIZE 16
#define EP2_TX_FIFO_SIZE 64
#define EP2_RX_FIFO_SIZE 64
#define EP2_PACKET_SIZE 64
#define USB_IDLE 0
#define USB_TRANSMIT 1
#define USB_RECEIVE 2
#define USB_CLASS_CODE_TEST_CLASS_DEVICE 0xdc
#define USB_SUBCLASS_CODE_TEST_CLASS_D12 0xA0
#define USB_PROTOCOL_CODE_TEST_CLASS_D12 0xB0
/*************************************************************************
/ masks
/*************************************************************************/
#define USB_RECIPIENT (unsigned char)0x1F
#define USB_RECIPIENT_DEVICE (unsigned char)0x00
#define USB_RECIPIENT_INTERFACE (unsigned char)0x01
#define USB_RECIPIENT_ENDPOINT (unsigned char)0x02
#define USB_REQUEST_TYPE_MASK (unsigned char)0x60
#define USB_STANDARD_REQUEST (unsigned char)0x00
#define USB_CLASS_REQUEST (unsigned char)0x20
#define USB_VENDOR_REQUEST (unsigned char)0x40
#define USB_REQUEST_MASK (unsigned char)0x0F
#define DEVICE_ADDRESS_MASK 0x7F
/*********设备描述符**********/
typedef struct _USB_DEVICE_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar bcdUSBl;
uchar bcdUSBh; /*ushort bcdUSB*/
uchar bDeviceClass;
uchar bDeviceSubClass;
uchar bDeviceProtocol;
uchar bMaxPacketSize0;
uchar idVendorl;
uchar idVendorh; /*ushort idVendor;*/
uchar idProductl;
uchar idProducth; /*ushort idProduct;*/
uchar bcdDevicel;
uchar bcdDeviceh; /*ushort bcdDevice;*/
uchar iManufacturer;
uchar iProduct;
uchar iSerialNumber;
uchar bNumConfigurations; /*The struct include 18 bytes*/
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;
/**********端点描述符***********/
typedef struct _USB_ENDPOINT_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar bEndpointAddress;
uchar bmAttributes;
uchar wMaxPacketSizel;
uchar wMaxPacketSizeh; /*ushort wMaxPacketSize;*/
uchar bInterval; /*The struct include 7 bytes*/
} USB_ENDPOINT_DESCRIPTOR, *PUSB_ENDPOINT_DESCRIPTOR;
//
// values for bmAttributes Field in
// USB_CONFIGURATION_DESCRIPTOR
//
#define BUS_POWERED 0x80
#define SELF_POWERED 0x40
#define REMOTE_WAKEUP 0x20
/**********配置描述符***********/
typedef struct _USB_CONFIGURATION_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar wTotalLengthl;
uchar wTotalLengthh; /*ushort wTotalLength;*/
uchar bNumInterfaces;
uchar bConfigurationValue;
uchar iConfiguration;
uchar bmAttributes;
uchar MaxPower; /*The struct include 9 bytes*/
} USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;
/**********接口描述符***********/
typedef struct _USB_INTERFACE_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar bInterfaceNumber;
uchar bAlternateSetting;
uchar bNumEndpoints;
uchar bInterfaceClass;
uchar bInterfaceSubClass;
uchar bInterfaceProtocol;
uchar iInterface; /*The struct include 9 bytes*/
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;
/**********字符串描述符***********/
typedef struct _USB_STRING_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar bString[1]; /*The struct include 3 bytes*/
} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
//
// USB power descriptor added to core specification
//
#define USB_SUPPORT_D0_COMMAND 0x01
#define USB_SUPPORT_D1_COMMAND 0x02
#define USB_SUPPORT_D2_COMMAND 0x04
#define USB_SUPPORT_D3_COMMAND 0x08
#define USB_SUPPORT_D1_WAKEUP 0x10
#define USB_SUPPORT_D2_WAKEUP 0x20
/**********电源描述符***********/
typedef struct _USB_POWER_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
uchar bCapabilitiesFlags;
ushort EventNotification;
ushort D1LatencyTime;
ushort D2LatencyTime;
ushort D3LatencyTime;
uchar PowerUnit;
ushort D0PowerConsumption;
ushort D1PowerConsumption;
ushort D2PowerConsumption;
} USB_POWER_DESCRIPTOR, *PUSB_POWER_DESCRIPTOR;
/**********命令描述符***********/
typedef struct _USB_COMMON_DESCRIPTOR {
uchar bLength;
uchar bDescriptorType;
} USB_COMMON_DESCRIPTOR, *PUSB_COMMON_DESCRIPTOR;
//
// Standard USB HUB definitions
//
// See Chapter 11
//
typedef struct _USB_HUB_DESCRIPTOR
{
uchar bDescriptorLength; // Length of this descriptor
uchar bDescriptorType; // Hub configuration type
uchar bNumberOfPorts; // number of ports on this hub
ushort wHubCharacteristics; // Hub Charateristics
uchar bPowerOnToPowerGood; // port power on till power good in 2ms
uchar bHubControlCurrent; // max current in mA
//
// room for 255 ports power control and removable bitmask
uchar bRemoveAndPowerMask[64];
} USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
/*******************************/
/*******描述符变量定义********/
/*******************************/
const USB_DEVICE_DESCRIPTOR DeviceDescr =/*设备描述符*/
{
sizeof(USB_DEVICE_DESCRIPTOR), /*1 byte 18*/
USB_DEVICE_DESCRIPTOR_TYPE, /*1 byte*/
0x00,0x01, /*2 byte* 0x0100*/
USB_CLASS_CODE_TEST_CLASS_DEVICE, /*1 byte*/
0, 0, /*2 byte*/
EP0_PACKET_SIZE, /*1 byte*/
0x71,0x04, /*2 byte 0x0471,*/
0x66,0x06, /*2 byte 0x0666,*/
0x00,0x01, /*2 byte 0x0100,*/
0, 0, 0, /*3 byte 0x0471,*/
1 /*1 byte 0x0471,*/
};
const USB_CONFIGURATION_DESCRIPTOR ConfigDescr =/*配置描述符*/
{
sizeof(USB_CONFIGURATION_DESCRIPTOR), /*1 byte 9,*/
USB_CONFIGURATION_DESCRIPTOR_TYPE, /*1 byte*/
0x2e,0x00, /*2 byte 55,SWAP(CONFIG_DESCRIPTOR_LENGTH)*/
1, /*1 byte*/
1, /*1 byte*/
0, /*1 byte*/
0x60, /*1 byte*/
0x32 /*1 byte*/
}; /*total 9 bytes */
const USB_INTERFACE_DESCRIPTOR InterfaceDescr =/*接口描述符*/
{
sizeof(USB_INTERFACE_DESCRIPTOR), /*1 byte 9,*/
USB_INTERFACE_DESCRIPTOR_TYPE, /*1 byte */
0, /*1 byte */
0, /*1 byte */
NUM_ENDPOINTS, /*1 byte */
USB_CLASS_CODE_TEST_CLASS_DEVICE, /*1 byte */
USB_SUBCLASS_CODE_TEST_CLASS_D12, /*1 byte */
USB_PROTOCOL_CODE_TEST_CLASS_D12, /*1 byte */
0 /*1 byte */
}; /*total 9 bytes */
const USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =/*端点1描述符*/
{
sizeof(USB_ENDPOINT_DESCRIPTOR), /*1 byte 7,*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*1 byte */
0x81, /*1 byte */
USB_ENDPOINT_TYPE_INTERRUPT, /*1 byte */
EP1_PACKET_SIZE,0x00, /*2 byte SWAP(EP1_PACKET_SIZE),*/
100 /*1 byte */
}; /*total 7 bytes */
const USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =/*端点1描述符*/
{
sizeof(USB_ENDPOINT_DESCRIPTOR), /*1 byte 7,*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*1 byte */
0x1, /*1 byte */
USB_ENDPOINT_TYPE_INTERRUPT, /*1 byte */
EP1_PACKET_SIZE,0x00, /*2 byte SWAP(EP1_PACKET_SIZE),*/
100 /*1 byte */
}; /*total 7 bytes */
const USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =/*端点2描述符*/
{
sizeof(USB_ENDPOINT_DESCRIPTOR), /*1 byte 7,*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*1 byte*/
0x82, /*1 byte*/
USB_ENDPOINT_TYPE_BULK, /*1 byte*/
EP2_PACKET_SIZE,0x00, /*2 byte SWAP(EP2_PACKET_SIZE),*/
10 /*1 byte*/
}; /*total 7 bytes */
const USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =/*端点2描述符*/
{
sizeof(USB_ENDPOINT_DESCRIPTOR), /*1 byte 7,*/
USB_ENDPOINT_DESCRIPTOR_TYPE, /*1 byte*/
0x2, /*1 byte*/
USB_ENDPOINT_TYPE_BULK, /*1 byte*/
EP2_PACKET_SIZE,0x00, /*2 byte SWAP(EP2_PACKET_SIZE),*/
10 /*1 byte*/
}; /*total 7 bytes */
//设备请求结构体
typedef struct _device_request
{
unsigned char bmRequestType; /*8 bytes*/
unsigned char bRequest;
unsigned char wValuel;
unsigned char wValueh; /*unsigned short wValue;*/
unsigned char wIndexl;
unsigned char wIndexh; /*unsigned short wIndex;*/
unsigned char wLengthl;
unsigned char wLengthh; /*unsigned short wLength;*/
} DEVICE_REQUEST;
//io请求结构体
typedef struct _IO_REQUEST
{
unsigned short uAddressL;
unsigned char bAddressH;
unsigned short uSize;
unsigned char bCommand;
} IO_REQUEST, *PIO_REQUEST;
#define MAX_CONTROLDATA_SIZE 8
typedef struct _control_xfer
{
DEVICE_REQUEST DeviceRequest; /*occupated 8 bytes*/
unsigned char wLengthl;
unsigned char wLengthh; /*unsigned short wLength;*/
unsigned char wCountl;
unsigned char wCounth; /*unsigned short wCount;*/
unsigned char * pData; /*occupated 16bits*/
unsigned char dataBuffer[MAX_CONTROLDATA_SIZE];/* 8 bytes */
} CONTROL_XFER; /*total 21 bytes*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -