📄 usb.h
字号:
//=========================================================================================
//usb.h
//=========================================================================================
#ifndef __USB_H_
#define __USB_H_
#include "option.h"
#if _DEBUG
#define DbgPrintf Uart0_Printf
#else
void DoNone( S8 *fmt, ... ){ }
#define DbgPrintf DoNone
#endif
//********************//
//Endpoint 0 defines //
//********************//
// Standard bmRequestTyje (Direction)
#define DIR_HOST_TO_DEVICE ( 0x00 )
#define DIR_DEVICE_TO_HOST ( 0x80 )
// Standard bmRequestType (Type)
#define TYPE_STANDARD ( 0x00 )
#define TYPE_CLASS ( 0x20 )
#define TYPE_VENDOR ( 0x40 )
#define TYPE_RESERVED ( 0x60 )
// Standard bmRequestType (Recipient)
#define RECIPIENT_DEVICE ( 0 )
#define RECIPIENT_INTERFACE ( 1 )
#define RECIPIENT_ENDPOINT ( 2 )
#define RECIPIENT_OTHER ( 3 )
// Feature Selectors
#define DEVICE_REMOTE_WAKEUP ( 1 )
#define EP_STALL ( 0 )
// Standard Request Codes
#define GET_STATUS ( 0 )
#define CLEAR_FEATURE ( 1 )
#define SET_FEATURE ( 3 )
#define SET_ADDRESS ( 5 )
#define GET_DESCRIPTOR ( 6 )
#define SET_DESCRIPTOR ( 7 )
#define GET_CONFIGURATION ( 8 )
#define SET_CONFIGURATION ( 9 )
#define GET_INTERFACE ( 10 )
#define SET_INTERFACE ( 11 )
#define SYNCH_FRAME ( 12 )
// Descriptor Types
#define TYPE_DEVICE ( 1 )
#define TYPE_CONFIGURATION ( 2 )
#define TYPE_STRING ( 3 )
#define TYPE_INTERFACE ( 4 )
#define TYPE_ENDPOINT ( 5 )
// Class-specific Request Codes
#define GET_DEVICE_ID ( 0 )
#define GET_PORT_STATUS ( 1 )
#define SOFT_RESET ( 2 )
//configuration descriptor: bmAttributes
#define CONF_ATTR_REMOTE_WAKEUP ( 0x20 )
#define CONF_ATTR_SELFPOWERED ( 0x40 )
#define CONF_ATTR_DEFAULT ( 0x80 ) //Spec 1.0 it was BUSPOWERED bit.
//endpoint descriptor
#define EP_ADDR_IN ( 0x80 )
#define EP_ADDR_OUT ( 0x00 )
#define EP_ATTR_CONTROL ( 0x0 )
#define EP_ATTR_ISOCHRONOUS ( 0x1 )
#define EP_ATTR_BULK ( 0x2 )
#define EP_ATTR_INTERRUPT ( 0x3 )
//string descriptor
#define LANGID_US_L ( 0x09 )
#define LANGID_US_H ( 0x04 )
// rEP_INT_REG, rEP_INT_EN_REG
#define BIT_USBD_EP0_INT ( 1 << 0 )
#define BIT_USBD_EP1_INT ( 1 << 1 )
#define BIT_USBD_EP2_INT ( 1 << 2 )
#define BIT_USBD_EP3_INT ( 1 << 3 )
#define BIT_USBD_EP4_INT ( 1 << 4 )
// rUSB_INT_REG, rUSB_INT_EN_REG
#define BIT_USBD_SUSPEND_INT ( 1 << 0 )
#define BIT_USBD_RESUME_INT ( 1 << 1 )
#define BIT_USBD_RESET_INT ( 1 << 2 )
// rEP0_CSR
#define BIT_EP0_OUT_PKT_RDY ( 1 << 0 ) //Indicate received data, if it is setted
#define BIT_EP0_IN_PKT_RDY ( 1 << 1 ) //Set to tell usb controller to send the data to host
#define BIT_EP0_SENT_STALL ( 1 << 2 ) //Indicate the STALL packet was sent to host
#define BIT_EP0_DATA_END ( 1 << 3 ) //Set when received the last packet or sending the last packet to host
#define BIT_EP0_SETUP_END ( 1 << 4 ) //Indicate control transfer ends before DATA_END is set
#define BIT_EP0_SEND_STALL ( 1 << 5 ) //Set & stop send STALL packet
#define BIT_EP0_CSR_SERVICED_OUT_PKT_RDY ( 1 << 6 ) //Set it to clear OUT_PKT_RDY, and lets the control transfer go on
#define BIT_EP0_CSR_SERVICED_SETUP_END ( 1 << 7 ) //Set to clear SETUP_END
#define BIT_EP0_WR_BITS ( BIT_EP0_CSR_SERVICED_OUT_PKT_RDY | BIT_EP0_CSR_SERVICED_SETUP_END )
// rIN_CSR1_REG
#define BIT_EPIN1_IN_PKT_READY ( 1 << 0 ) //Set to tell usb controller to send the data to host
#define BIT_EPIN1_FIFO_FLUSH ( 1 << 3 ) //Set by MCU, cleared by USB when finished flush
#define BIT_EPIN1_SEND_STALL ( 1 << 4 ) //Set & stop send STALL packet
#define BIT_EPIN1_SENT_STALL ( 1 << 5 ) //Indicate issues a STALL handshake
#define BIT_EPIN1_CLR_DATA_TOGGLE ( 1 << 6 ) //Set 0 to set data toggle; set 1 to clear, and the PID will be DATA0
#define BIT_EPIN1_WR_BITS ( BIT_EPIN1_FIFO_FLUSH | BIT_EPIN1_IN_PKT_READY | BIT_EPIN1_CLR_DATA_TOGGLE )
// rIN_CSR2_REG
#define BIT_EPIN2_IN_DMA_INT_MASK ( 1 << 4 ) //IN DMA interrupt mask, when IN_PKT_RDY setted and this bit is 0, interrupt happens
#define BIT_EPIN2_MODE_IN ( 1 << 5 ) //Endpoint Direction set to IN
#define BIT_EPIN2_MODE_OUT ( 0 << 5 ) //Endpoint Direction set to OUT
#define BIT_EPIN2_ISO ( 1 << 6 ) //Reserved endpoints transfer type
#define BIT_EPIN2_BULK ( 0 << 6 ) //Endpoints transfer type set to BULK
#define BIT_EPIN2_AUTO_SET ( 1 << 7 ) //Set when the MCU writes MAXP data, IN_PKT_RDY will automatically be set
// rOUT_CSR1_REG
#define BIT_EPOUT1_OUT_PKT_READY ( 1 << 0 ) //Indicate received data, if it is setted
#define BIT_EPOUT1_FIFO_FLUSH ( 1 << 4 ) //Set by MCU, cleared by USB when finished flush
#define BIT_EPOUT1_SEND_STALL ( 1 << 5 ) //Set & stop send STALL packet
#define BIT_EPOUT1_SENT_STALL ( 1 << 6 ) //Indicate issues a STALL handshake
#define BIT_EPOUT1_CLR_DATA_TOGGLE ( 1 << 7 ) //Set 0 to set data toggle; set 1 to clear, and the PID will be DATA0
#define BIT_EPOUT1_WR_BITS ( BIT_EPOUT1_FIFO_FLUSH | BIT_EPOUT1_SEND_STALL | BIT_EPOUT1_CLR_DATA_TOGGLE )
// rOUT_CSR2_REG
#define BIT_EPOUT2_OUT_DMA_INT_MASK ( 1 << 5 ) //OUT DMA interrupt mask, when OUT_PKT_RDY setted and this bit is 0, interrupt happens
#define BIT_EPOUT2_ISO ( 1 << 6 ) //Reserved endpoints transfer type
#define BIT_EPOUT2_BULK ( 0 << 6 ) //Endpoints transfer type set to BULK
#define BIT_EPOUT2_AUTO_CLR ( 1 << 7 ) //Set when the MCU Read MAXP data, OUT_PKT_RDY will automatically be cleared
//
// MAXP Register
#define FIFO_SIZE_0 ( 0 << 0 ) //0x00 * 8 = 0
#define FIFO_SIZE_8 ( 1 << 1 ) //0x01 * 8 = 8
#define FIFO_SIZE_16 ( 1 << 2 ) //0x02 * 8 = 16
#define FIFO_SIZE_32 ( 1 << 3 ) //0x04 * 8 = 32
#define FIFO_SIZE_64 ( 1 << 4 ) //0x08 * 8 = 64
#define EP0_PKT_SIZE 8
#define EP1_PKT_SIZE 32
#define EP2_PKT_SIZE 32
#define EP3_PKT_SIZE 32
#define EP4_PKT_SIZE 32
struct USB_SETUP_DATA{
U8 bmRequestType;
U8 bRequest;
U8 bValueL;
U8 bValueH;
U8 bIndexL;
U8 bIndexH;
U8 bLengthL;
U8 bLengthH;
};
struct USB_DEVICE_DESCRIPTOR{
U8 bLength;
U8 bDescriptorType;
U8 bcdUSBL;
U8 bcdUSBH;
U8 bDeviceClass;
U8 bDeviceSubClass;
U8 bDeviceProtocol;
U8 bMaxPacketSize0;
U8 idVendorL;
U8 idVendorH;
U8 idProductL;
U8 idProductH;
U8 bcdDeviceL;
U8 bcdDeviceH;
U8 iManufacturer;
U8 iProduct;
U8 iSerialNumber;
U8 bNumConfigurations;
};
struct USB_CONFIGURATION_DESCRIPTOR{
U8 bLength;
U8 bDescriptorType;
U8 wTotalLengthL;
U8 wTotalLengthH;
U8 bNumInterfaces;
U8 bConfigurationValue;
U8 iConfiguration;
U8 bmAttributes;
U8 maxPower;
};
struct USB_INTERFACE_DESCRIPTOR{
U8 bLength;
U8 bDescriptorType;
U8 bInterfaceNumber;
U8 bAlternateSetting;
U8 bNumEndpoints;
U8 bInterfaceClass;
U8 bInterfaceSubClass;
U8 bInterfaceProtocol;
U8 iInterface;
};
struct USB_ENDPOINT_DESCRIPTOR{
U8 bLength;
U8 bDescriptorType;
U8 bEndpointAddress;
U8 bmAttributes;
U8 wMaxPacketSizeL;
U8 wMaxPacketSizeH;
U8 bInterval;
};
struct USB_GET_STATUS{
U8 Device;
U8 Interface;
U8 Endpoint0;
U8 Endpoint1;
U8 Endpoint3;
};
void __irq USBDIsr( void );
void UsbTest( void );
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -