📄 usb.h
字号:
#ifndef _USBDESCRIPTOR_H_
#define _USBDESCRIPTOR_H_
#include <typedef.h>
#define _HID_DEVICE //need define the macro if want to write a hid device firmware
#define SWAP(x) ((((x)&0xFF)<<8)|(((x)>>8)&0xFF))
#define MSB(x) (((x) >> 8) & 0xFF)
#define LSB(x) ((x) & 0xFF)
#define USB_DEVICE_DESCRIPTOR_TYPE 0x1
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x2
#define USB_STRING_DESCRIPTOR_TYPE 0X3
#define USB_INTERFACE_DESCRIPTOR_TYPE 0X4
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0X5
#define USB_HID_DESCRIPTOR_TYPE 0X21
#define USB_REPORT_DESCRIPTOR_TYPE 0X22
typedef struct _tagDEVICE_DESCRIPTOR
{
u8 bLength; //len of descriptor
u8 bDescriptorType; //fix device (01h)
u16 bcdUSB; //usb specification number (BCD code, specification 1.0->0100h, 1.1->0110h)
u8 bDeviceClass; //class code
u8 bDeviceSubClass; //sub class code
u8 bDeviceProtocol; //protocol code
u8 bMaxPacketSize0; //max packet size of endpoint 0
//for full speed device, the value may be 8, 16, 32 & 64
//for low speed device, the value must be 8
u16 wIDVendor; //ID of vendor
u16 wIDProduct; //ID of product
u16 bcdDevice; //release number of device (BCD code)
u8 iManufacturer; //point to manufacturer descriptor string
u8 iProduct; //pointer to product descriptor string
u8 iSerialNumber; //point to product serial number string
u8 bNumConfigurations; //config number
}DEVICE_DESCRIPTOR;
typedef struct _tagCONFIG_DESCRIPTOR
{
u8 bLength; //len of descriptor
u8 bDescriptorType; //fix config (02h)
u16 wTotalLength; //return len of data bytes
u8 bNumInterfaces; //support interface number of the configuration
u8 bConfigurationValue; //Set_configuration & get_configuration request id
u8 bConfiguration; //point to descriptor string of this configuration
u8 bmttributes; //self power/bus power & remote wakeup config
//if bus power: bit7->1
//if self power: bit6->1
//if support remote wakeup: bit5->1
//bit0~bit4->0
u8 bMaxPower; //max power need (max:mA/2)
}CONFIG_DESCRIPTOR;
typedef struct _tagINTERFACE_DESCRIPTOR
{
u8 bLength; //len of descriptor
u8 bDescriptorType; //fix interface (04h)
u8 bInterfaceNumber; //interface id
u8 bAlternateSetting; //a alternate setting value
u8 bNumEndpoints; //support endpoint num, except endpoint 0
u8 bInterfaceClass; //class code, the value = 3 if it's HID device
u8 bInterfaceSubClass; //sub class code
u8 bInterfaceProtocol; //protocol code
u8 bInterface; //point to descriptor string of interface
}INTERFACE_DESCRIPTOR;
typedef struct _tagENDPOINT_DESCRIPTOR
{
u8 bLength; //len of descriptor
u8 bDescriptorType; //fix endpoint (05h)
u8 bEndpointAddress; //endpoint num & direction
//bit0~3 is endpoint num, low speed device have max 3 endpoints(0-2),
// full speed device have max 16 endpoints(0-15)
//bit7 set direction: Out->0, In->1
//bit4~6->0
u8 bmAttributes; //support transport type
//control mode->0
//time limit mode->1
//batch mode->2
//interrup mode -> 3
u16 wMaxPacketSize; //max packet size
//for full speed device, the value may be 8, 16, 32 & 64
//for low speed device, the value must be 8
u8 bInterval; //interval of inquire(ms)
//interrupt mode:
//full speed device value may be 1~255
//low speed dvice value may be 10~255
//time limit mode: value must be 1
}ENDPOINT_DESCRIPTOR;
typedef struct _tagHID_DESCRIPTOR
{
u8 bLength; //len of descriptor
u8 bDescriptorType; //fix value (21h)
u16 bcdHID; //HID specification number (BCD)
u8 bCountryCode; //country code (BCD)
u8 bNumDescriptors; //number of sub class descriptor
u8 bDescriptorClassType; //type of class descriptor
//the value=22h if it is HID device
u16 wDescriptorClassLen; //len of report descriptor
//.... there may be more wDescriptorType & wDescriptorLen field
}HID_DESCRIPTOR;
typedef struct _tagDEVICE_REQUEST
{
u8 bmRequestType; //request type(direction, type, receive/send)
u8 bRequest; //usb request
u16 wValue; //usb request value
u16 wIndex; //usb request index
u16 wLength; //count len
}DEVICE_REQUEST;
#define MAX_CONTROLDATA_SIZE 16
typedef struct _tagCONTROL_XFER
{
DEVICE_REQUEST DevRequest; //usb request struct, 8B
u16 wLength; //xfer data len
u16 wCount; //xfer byte count
u8 *pData; //xfer data pointer
u8 buffer[MAX_CONTROLDATA_SIZE];
}CONTROL_XFER;
#define USB_IDLE 0
#define USB_TRANSMIT 1
#define USB_RECEIVE 2
typedef union _EPP_FLAGS
{
struct
{
u8 bus_reset:1; //bus reset
u8 suspend:1;
// u8 remote_wakeup:1;
u8 setup_packet:1; //receive setup packet
u8 in_isr:1; //in isr
u8 control_state:2;//0:IDEL
//1:TRANSMIT send data
//2:RECEIVE receive data
u8 config:1; //0:unconfig, 1:configed
u8 ep1_out:1; //ep1 receive data
u8 ep2_out:1;
u8 ep1_full:1; //ep1 send buf full
u8 ep2_full:2;
}flags;
u16 value;
}EPPFLAGS;
#define DISABLE_INTERRUPT EA = 0 //close int
#define ENABLE_INTERRUPT EA = 1 //open int
extern EPPFLAGS bEppflags;
//RECEIVE TYPE
#define USB_RECIPIENT 0X1F //MASK OF request type
#define USB_RECIPIENT_DEVICE 0X0 //DEVICE RECEIVE
#define USB_RECIPIENT_INTERFACE 0X1 //INTERFACE RECEIVE
#define USB_RECIPIENT_ENDPOINT 0X2 //ENDPOINT RECEIVE
#define USB_ENDPOINT_DIRECTION_MASK 0x80 //device send data to pc
#define MAX_ENDPOINTS 0x0f //bit0~3 is ep number
//request type
#define USB_REQUEST_TYPE_MASK 0x60
#define USB_STANDARD_REQUEST 0x0 //standard request
#define USB_CLASS_REQUEST 0X20 //class request, eg: HID
#define USB_VENDOR_REQUEST 0X40 //VENDOR REQUEST
#define USB_REQUEST_MASK 0X0F
#define USB_FEATURE_REMOTE_WAKEUP 1 //device feature remote_wake
#define USB_FEATURE_ENDPOINT_STALL 0 //ep feature stall
#define DEVICE_ADDRESS_MASK 0x7f //set address mask
void control_handler();
void reserved(void);
//standard usb rountine
void get_status(void);
void clear_feature(void);
void set_feature(void);
void set_address(void);
void get_descriptor(void);
void get_configuration(void);
void set_configuration(void);
void get_interface(void);
void set_interface(void);
//custom request rountine
#define GET_FIRMWARE_VERSION 0x0472
#define GET_BUFFER_SIZE 0x0474
#define TRANS_REQUEST 0x0471
void read_write_register(void);
//standard HID rountine
#ifdef _HID_DEVICE
void get_report();
void get_idle();
void get_protocol();
void set_report();
void set_idle();
void set_protocol();
#endif //_HID_DEVICE
#endif //_USBDESCRIPTOR_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -