📄 usb.h
字号:
//USB
/* USB Driver */
#define USB_PU_CTRL FALSE /* no pull-up control */
#define VENDOR_ID 0xEB03 /* Atmel vendor ID = 03EBh */
#define PRODUCT_ID 0x0220 /* Atmel Product ID: 2002h */
#define RELEASE_NUMBER 0x0001
/* USB Mass Storage ! unicode char */
#define USB_MANUFACTURER_NAME {'A'<<8,'T'<<8,'M'<<8,'E'<<8,'L'<<8}
#define USB_MN_LENGTH 5
#define USB_PRODUCT_NAME {'S'<<8,'N'<<8,'D'<<8,'1'<<8,' '<<8,'S'<<8,'t'<<8,\
'o'<<8,'r'<<8,'a'<<8,'g'<<8,'e'<<8}
#define USB_PN_LENGTH 12
#define USB_SERIAL_NUMBER {'4'<<8,'7'<<8,'1'<<8,'0'<<8,'7'<<8,'6'<<8,'5'<<8,\
'0'<<8,'6'<<8,'6'<<8,'4'<<8,'5'<<8,'1'<<8}
#define USB_SN_LENGTH 13
/* SCSI Mass Storage */
#define SBC_VENDOR_ID {'G','e','n','e','r','i','c',' '}
#define SBC_PRODUCT_ID {'S','N','D','1',' ','M','P','3',' ','P','l','a','y','e','r',' '}
#define SBC_REVISION_ID {'0','.','0','0'}
#define CONTROL 0x80
#define BULK_IN 0x86
#define BULK_OUT 0x82
#define INTERRUPT_IN 0x87
#define INTERRUPT_OUT 0x83
#define ISOCHRONOUS_IN 0x85
#define ISOCHRONOUS_OUT 0x81
#define BULK_MAX_SIZE ((Byte)64)
#define GET_DEVICE_DESCRIPTOR 1
#define GET_CONFIGURATION_DESCRIPTOR 4
/*_____ S T A N D A R D R E Q U E S T S __________________________________*/
#define GET_STATUS 0x00
#define GET_DEVICE 0x01
#define CLEAR_FEATURE 0x01 /* see FEATURES below */
#define GET_STRING 0x03
#define SET_FEATURE 0x03 /* see FEATURES below */
#define SET_ADDRESS 0x05
#define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09
#define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C
#define GET_MAX_LUN 0xFE
#define MASS_STORAGE_RESET 0xFF
/*_____ D E S C R I P T O R T Y P E S ____________________________________*/
#define DEVICE 0x01
#define CONFIGURATION 0x02
#define STRING 0x03
#define INTERFACE 0x04
#define ENDPOINT 0x05
#define LANG_ID 0x00
#define MAN_INDEX 0x01
#define PROD_INDEX 0x02
#define SN_INDEX 0x03
/*_____ S T A N D A R D F E A T U R E S __________________________________*/
#define DEVICE_REMOTE_WAKEUP_FEATURE 0x01
#define ENDPOINT_HALT_FEATURE 0x00
/*_____ D E V I C E S T A T U S ___________________________________________*/
#define SELF_POWERED 1
/*_____ D E V I C E S T A T E _____________________________________________*/
#define ATTACHED 0
#define POWERED 1
#define DEFAULT 2
#define ADDRESSED 3
#define CONFIGURED 4
#define SUSPENDED 5
#define USB_CONFIG_BUSPOWERED 0x80
#define USB_CONFIG_SELFPOWERED 0x40
#define USB_CONFIG_REMOTEWAKEUP 0x20
/*_____ M A S S S T O R A G E E N D P O I N T S _________________________*/
#define EP_CONTROL 0x00
#define EP_IN 0x01
#define EP_OUT 0x02
#define EP_CONTROL_LENGTH 8
#define EP_IN_LENGTH 64
#define EP_OUT_LENGTH 64
/*_____ U S B L I T T L E E N D I A N <--> B I G E N D I A N _________*/
extern bdata bit bmCBWFlag7;
extern bdata bit status_ready;
extern data Byte status;
extern bdata bit rx_bank;
/*_____ D E C L A R A T I O N ______________________________________________*/
void usb_init(void);
void usb_enumeration_process(void);
/* Standard define */
#define Usb_enable() (USBCON |= MSK_USBE)
#define Usb_disable() (USBCON &= ~MSK_USBE)
#define Usb_enable_int() (IEN1 |= MSK_EUSB)
#define Usb_disable_int() (IEN1 &= ~MSK_EUSB)
#define Usb_set_clock(c) (USBCLK = c)
#define Usb_clear_EORINT() (USBINT &= ~MSK_EORINT)
#define Usb_clear_WUPCPU() (USBINT &= ~MSK_WUPCPU)
#define Usb_clear_SOFINT() (USBINT &= ~MSK_SOFINT)
#define Usb_clear_SPINT() (USBINT &= ~MSK_SPINT)
#define Usb_enable_SPINT() (USBIEN |= MSK_SPINT)
#define Usb_suspend() (USBINT & MSK_SPINT)
#define Usb_resume() (USBINT & MSK_WUPCPU)
#define Usb_reset() (USBINT & MSK_EORINT)
#define Usb_clear_RXOUT() (UEPSTAX &= ~MSK_RXOUT)
#define Usb_clear_RXOUT_PP() {if(rx_bank){UEPSTAX&=~MSK_RXOUTB1;}else{UEPSTAX&=(~MSK_RXOUTB0);}rx_bank=~rx_bank;}
#define Usb_rx_complete() (UEPSTAX & MSK_RXOUTB0B1)
#define Usb_clear_RXSETUP() (UEPSTAX &= ~MSK_RXSETUP)
#define Usb_setup_received() (UEPSTAX & MSK_RXSETUP)
#define Usb_set_TXRDY() (UEPSTAX |= MSK_TXRDY)
#define Usb_clear_DIR() (UEPSTAX &= ~MSK_DIR)
#define Usb_set_DIR() (UEPSTAX |= MSK_DIR)
#define Usb_clear_TXCMPL() (UEPSTAX &= ~MSK_TXCMPL)
#define Usb_tx_complete() (UEPSTAX & MSK_TXCMPL)
#define Usb_configure_address(x) (USBADDR = (0x80 | x))
#define Usb_set_CONFG() (USBCON |= MSK_CONFG)
#define Usb_clear_CONFG() (USBCON &= ~MSK_CONFG)
#define Usb_set_FADDEN() (USBCON |= MSK_FADDEN)
#define Usb_clear_FADDEN() (USBCON &= ~MSK_FADDEN)
#define Usb_select_ep(e) (UEPNUM = e)
#define Usb_clear_rx_bank() (rx_bank = FALSE)
#define MSK_EP_DIR 0x7F
#define REQUEST_DEVICE_STATUS 0x80
#define REQUEST_INTERFACE_STATUS 0x81
#define REQUEST_ENDPOINT_STATUS 0x82
#define ENDPOINT_0 0x00
#define ENDPOINT_1 0x81
#define ENDPOINT_2 0x02
#define ZERO_TYPE 0x00
#define INTERFACE_TYPE 0x01
#define ENDPOINT_TYPE 0x02
/* mass storage define */
#define FREE 0x00
#define Usb_set_STALLRQ() (UEPSTAX |= MSK_STALLRQ)
#define Usb_clear_STALLRQ() (UEPSTAX &= ~MSK_STALLRQ)
#define Usb_clear_STALLED() (UEPSTAX &= ~MSK_STALLED)
#define Usb_STALL_requested() (UEPSTAX & MSK_STALLRQ)
#define Usb_STALL_sent() (UEPSTAX & MSK_STALLED)
#define Usb_set_no_data_phase() (no_data_phase = 1)
#define Usb_clear_no_data_phase() (no_data_phase = 0)
#define Usb_read_byte() (UEPDATX)
#define Usb_write_byte(x) (UEPDATX = x)
#define Usb_set_no_data_phase() (no_data_phase = 1)
#define Usb_clear_no_data_phase() (no_data_phase = 0)
#define Usb_set_status_ready() (status_ready = 1)
#define Usb_clear_status_ready() (status_ready = 0)
#define Usb_set_status(x) (status = x); (status_ready = 1)
#define Usb_CBWFlag_out() (bmCBWFlag7 = 0)
#define Usb_CBWFlag_in() (bmCBWFlag7 = 1)
#define Usb_get_CBWFlag() (bmCBWFlag7)
/*_________________________________________________________ S T R U C T _____*/
/*_____ U S B D E V I C E R E Q U E S T _________________________________*/
struct Endpoint_information_st
{
Uint16 fifo_size ; /* size of the endpoint FIFO */
Uint16 fifo_left ;
};
struct USB_request_st
{
Byte bmRequestType; /* Characteristics of the request */
Byte bRequest; /* Specific request */
Uint16 wValue; /* field that varies according to request */
Uint16 wIndex; /* field that varies according to request */
Uint16 wLength; /* Number of bytes to transfer if Data */
};
/*_____ U S B D E V I C E D E S C R I P T O R ___________________________*/
struct usb_st_device_descriptor
{
Byte bLength; /* Size of this descriptor in bytes */
Byte bDescriptorType; /* DEVICE descriptor type */
Uint16 bscUSB; /* Binay Coded Decimal Spec. release */
Byte bDeviceClass; /* Class code assigned by the USB */
Byte bDeviceSubClass; /* Sub-class code assigned by the USB */
Byte bDeviceProtocol; /* Protocol code assigned by the USB */
Byte bMaxPacketSize0; /* Max packet size for EP0 */
Uint16 idVendor; /* Vendor ID. ATMEL = 0x03EB */
Uint16 idProduct; /* Product ID assigned by the manufacturer */
Uint16 bcdDevice; /* Device release number */
Byte iManufacturer; /* Index of manu. string descriptor */
Byte iProduct; /* Index of prod. string descriptor */
Byte iSerialNumber; /* Index of S.N. string descriptor */
Byte bNumConfigurations; /* Number of possible configurations */
};
/*_____ U S B C O N F I G U R A T I O N D E S C R I P T O R _____________*/
struct usb_st_configuration_descriptor
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* CONFIGURATION descriptor type */
Uint16 wTotalLength; /* total length of data returned */
Byte bNumInterfaces; /* number of interfaces for this conf. */
Byte bConfigurationValue; /* value for SetConfiguration resquest */
Byte iConfiguration; /* index of string descriptor */
Byte bmAttibutes; /* Configuration characteristics */
Byte MaxPower; /* maximum power consumption */
};
/*_____ U S B M A N U F A C T U R E R D E S C R I P T O R _______________*/
struct usb_st_manufacturer
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* STRING descriptor type */
Uint16 wstring[USB_MN_LENGTH];/* unicode characters */
};
/*_____ U S B P R O D U C T D E S C R I P T O R _________________________*/
struct usb_st_product
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* STRING descriptor type */
Uint16 wstring[USB_PN_LENGTH];/* unicode characters */
};
/*_____ U S B S E R I A L N U M B E R D E S C R I P T O R _____________*/
struct usb_st_serial_number
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* STRING descriptor type */
Uint16 wstring[USB_SN_LENGTH];/* unicode characters */
};
/*_____ U S B L A N G U A G E D E S C R I P T O R ______________________*/
struct usb_st_language_descriptor
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* STRING descriptor type */
Uint16 wlangid; /* language id */
};
/*_____ U S B I N T E R F A C E D E S C R I P T O R _____________________*/
struct usb_st_interface_descriptor
{
Byte bLength; /* size of this descriptor in bytes */
Byte bDescriptorType; /* INTERFACE descriptor type */
Byte bInterfaceNumber; /* Number of interface */
Byte bAlternateSetting; /* value to select alternate setting */
Byte bNumEndpoints; /* Number of EP except EP 0 */
Byte bInterfaceClass; /* Class code assigned by the USB */
Byte bInterfaceSubClass; /* Sub-class code assigned by the USB */
Byte bInterfaceProtocol; /* Protocol code assigned by the USB */
Byte iInterface; /* Index of string descriptor */
};
/*_____ U S B E N D P O I N T D E S C R I P T O R _______________________*/
struct usb_st_endpoint_descriptor
{
Byte bLength; /* Size of this descriptor in bytes */
Byte bDescriptorType; /* ENDPOINT descriptor type */
Byte bEndpointAddress; /* Address of the endpoint */
Byte bmAttributes; /* Endpoint's attributes */
Uint16 wMaxPacketSize; /* Maximum packet size for this EP */
Byte bInterval; /* Interval for polling EP in ms */
};
/*_____ D E F I N I T I O N ________________________________________________*/
code struct usb_st_device_descriptor usb_device_descriptor =
{
sizeof(usb_device_descriptor), DEVICE, 0x1001, 0, 0, 0, EP_CONTROL_LENGTH,
VENDOR_ID, PRODUCT_ID, RELEASE_NUMBER, MAN_INDEX, PROD_INDEX, SN_INDEX, 1
};
code struct usb_st_manufacturer usb_manufacturer =
{
sizeof(usb_manufacturer), STRING,
USB_MANUFACTURER_NAME
};
code struct usb_st_product usb_product =
{
sizeof(usb_product), STRING,
USB_PRODUCT_NAME
};
code struct usb_st_serial_number usb_serial_number =
{
sizeof(usb_serial_number), STRING,
USB_SERIAL_NUMBER
};
code struct usb_st_language_descriptor usb_language =
{
sizeof(usb_language), STRING, 0x0904
};
code struct
{
struct usb_st_configuration_descriptor cfg;
struct usb_st_interface_descriptor ifc;
struct usb_st_endpoint_descriptor ep1;
struct usb_st_endpoint_descriptor ep2;
}
usb_configuration =
{
{ 9, CONFIGURATION, sizeof(usb_configuration) << 8, 1, 1, 0, USB_CONFIG_BUSPOWERED, 0x32},
{ 9, INTERFACE, 0, 0, 2, 0x08, 0x06, 0x50, 0 },
{ 7, ENDPOINT, 0x81, 0x02, EP_IN_LENGTH << 8, 0 },
{ 7, ENDPOINT, 0x02, 0x02, EP_OUT_LENGTH << 8, 0 }
};
extern void usb_mass_storage_reset (void);
extern void usb_mass_storage_get_lun (void);
static void usb_get_descriptor (void);
static Byte* send_ep0_packet (Byte *, Byte);
static void usb_read_request (void);
static void usb_set_address (void);
static void usb_set_configuration (void);
static void usb_clear_feature (void);
static void usb_set_feature (void);
static void usb_get_status (void);
static void usb_get_configuration (void);
static bdata bit zlp;
static idata Byte endpoint_status[3];
static idata Byte *pbuffer;
static idata Byte bmRequestType;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -