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

📄 usb._h

📁 单片机中的中断处理模块
💻 _H
字号:
#ifndef		__USB__H
#define		__USB__H

#include "stdtypes.h"
#include <iom128v.h>


#define USB_SP  PG4
#define D12_INT PE3              


//#define D12_DATA  (*(UNS8 *)0x3000)         //A13=1,A12=1  CS_USB=0,USB_A0=0
//#define D12_COMMAND (*(UNS8 *)0x2000)       //A13=1,A12=0  CS_USB=0,USB_A0=1

//D12访问地址
#define D12_DATA  0x3000         //A13=1,A12=1  CS_USB=0,USB_A0=0
#define D12_COMMAND 0x2000       //A13=1,A12=0  CS_USB=0,USB_A0=1

#define DISABLE  CLI()
#define ENABLE   SEI()

#define D12_NOLAZYCLOCK			0x02
#define D12_CLOCKRUNNING        0x04
#define D12_INTERRUPTMODE		0x08
#define D12_SOFTCONNECT			0x10
#define D12_ENDP_NONISO			0x00
#define D12_ENDP_ISOOUT			0x40
#define D12_ENDP_ISOIN			0x80
#define D12_ENDP_ISOIO			0xC0

#define D12_CLOCK_12M			0x03
#define D12_CLOCK_4M			0x0b
#define D12_SETTOONE            0x40
#define D12_SOFONLY				0x80

#define D12_DMASINGLE			0x00
#define D12_BURST_4				0x01
#define D12_BURST_8				0x02
#define D12_BURST_16			0x03
#define D12_DMAENABLE           0x04
#define D12_DMA_INTOKEN			0x08
#define D12_AUTOLOAD			0x10
#define D12_NORMALPLUSSOF		0x20
#define D12_ENDP4INTENABLE		0x40
#define D12_ENDP5INTENABLE		0x80	// bug fixed in V2.1

#define D12_INT_ENDP0OUT		0x01
#define D12_INT_ENDP0IN			0x02
#define D12_INT_ENDP1OUT		0x04
#define D12_INT_ENDP1IN			0x08
#define D12_INT_ENDP2OUT		0x10
#define D12_INT_ENDP2IN			0x20
#define D12_INT_BUSRESET		0x40
#define D12_INT_SUSPENDCHANGE	0x80
#define D12_INT_EOT				0x0100

#define D12_SETUPPACKET			0x20

#define D12_BUFFER0FULL			0x20
#define D12_BUFFER1FULL			0x40

#define D12_FULLEMPTY			0x01
#define D12_STALL				0x02


#define MAX_ENDPOINTS      (UNS8)0x3

#define EP0_TX_FIFO_SIZE   16
#define EP0_RX_FIFO_SIZE   16
#define EP0_PACKET_SIZE    16

#define EP1_TX_FIFO_SIZE   4
#define EP1_RX_FIFO_SIZE   4
#define EP1_PACKET_SIZE    4  
#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
#define USB_RECIPIENT            (UNS8)0x1F
#define USB_RECIPIENT_DEVICE     (UNS8)0x00
#define USB_RECIPIENT_INTERFACE  (UNS8)0x01
#define USB_RECIPIENT_ENDPOINT   (UNS8)0x02

#define USB_REQUEST_TYPE_MASK    (UNS8)0x60
#define USB_STANDARD_REQUEST     (UNS8)0x00
#define USB_REQUEST_MASK         (UNS8)0x0F
#define DEVICE_ADDRESS_MASK      0x7F

#define MAX_CONTROLDATA_SIZE	8

typedef struct _device_request
{
	UNS8 bmRequestType;           //请求类型
	UNS8 bRequest;                //USB请求
	UNS16 wValue;		  //USB请求值
	UNS16 wIndex;		  //USB请求索引
	UNS16 wLength;		  //计数长度
} DEVICE_REQUEST;

//定义USB事件标志数据类型
//低位开始存放
typedef union _epp_flags
{
	struct _flags
	{
		unsigned  bus_reset      : 1,    suspend   : 1;
		unsigned  setup_packet   : 1,    remote_wakeup	 : 1;
		unsigned  in_isr	     : 1,    configuration	: 1;
		unsigned  control_state	 : 2;
		unsigned  ep1_rxdone     : 1,   main_rxdone : 1;
		unsigned  setup_dma	     : 2; // V2.3
		unsigned  Reserve        : 2;
		unsigned  Reserve1        : 2;
	} bits;
	UNS16 value;
} EPPFLAGS;

typedef struct _control_xfer
{
	DEVICE_REQUEST DeviceRequest; 	  //USB请求结构体
	unsigned short wLength;			  //传输数据的总字节数
	unsigned short wCount;			  //传输字节数统计
	UNS8 * pData;  					  //传输数据指针
	UNS8 dataBuffer[MAX_CONTROLDATA_SIZE];	 //请求的数据
} CONTROL_XFER;						   

#define MAXIMUM_USB_STRING_LENGTH 255

// 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

typedef struct _USB_DEVICE_DESCRIPTOR {
    UNS8 bLength;
    UNS8 bDescriptorType;
    UNS16 bcdUSB;
    UNS8 bDeviceClass;
    UNS8 bDeviceSubClass;
    UNS8 bDeviceProtocol;
    UNS8 bMaxPacketSize0;
    UNS16 idVendor;
    UNS16 idProduct;
    UNS16 bcdDevice;
    UNS8 iManufacturer;
    UNS8 iProduct;
    UNS8 iSerialNumber;
    UNS8 bNumConfigurations;
} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR;

typedef struct _USB_ENDPOINT_DESCRIPTOR {
    UNS8 bLength;
    UNS8 bDescriptorType;
    UNS8 bEndpointAddress;
    UNS8 bmAttributes;
    UNS16 wMaxPacketSize;
    UNS8 bInterval;
} 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_STRING_DESCRIPTOR {
    const UNS8 bLength;
    const UNS8 bDescriptorType;
    const UNS8 bString[1];
} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;


typedef struct _USB_CONFIGURATION_DESCRIPTOR {
    UNS8 bLength;
    UNS8 bDescriptorType;
    UNS16 wTotalLength;
    UNS8 bNumInterfaces;
    UNS8 bConfigurationValue;
    UNS8 iConfiguration;
    UNS8 bmAttributes;
    UNS8 MaxPower;
} USB_CONFIGURATION_DESCRIPTOR, *PUSB_CONFIGURATION_DESCRIPTOR;

typedef struct _USB_INTERFACE_DESCRIPTOR {
    UNS8 bLength;
    UNS8 bDescriptorType;
    UNS8 bInterfaceNumber;
    UNS8 bAlternateSetting;
    UNS8 bNumEndpoints;
    UNS8 bInterfaceClass;
    UNS8 bInterfaceSubClass;
    UNS8 bInterfaceProtocol;
    UNS8 iInterface;
} USB_INTERFACE_DESCRIPTOR, *PUSB_INTERFACE_DESCRIPTOR;


#define NUM_ENDPOINTS	4
typedef struct _CON_INT_ENDP_DESCRIPTOR_STRUCT
{
 USB_CONFIGURATION_DESCRIPTOR configuration_descriptor;
 USB_INTERFACE_DESCRIPTOR  interface_descritor;
 USB_ENDPOINT_DESCRIPTOR  EP1_TXDescr;
 USB_ENDPOINT_DESCRIPTOR  EP1_RXDescr;
 USB_ENDPOINT_DESCRIPTOR  EP2_TXDescr;
 USB_ENDPOINT_DESCRIPTOR  EP2_RXDescr;
}CON_INT_ENDP_DESCRIPTOR_STRUCT;

#define CONFIG_DESCRIPTOR_LENGTH    sizeof(USB_CONFIGURATION_DESCRIPTOR) \
									+ sizeof(USB_INTERFACE_DESCRIPTOR) \
									+ (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))
#define MSB(x)    (((x) >> 8) & 0xFF) 		//取数据高8位
#define LSB(x)    ((x) & 0xFF)				//取数据低8位

/* Decoding received messages */
#define DECODE_USB_LENGTH(msg) \
	(((UNS32)(msg)[0] << 8) | (UNS32)(msg)[1])

extern UNS8 usb_flag;
extern UNS8 usb_first;
extern UNS16 usb_length;
extern UNS16 usb_counter;



unsigned short D12_ReadInterruptRegister(void);
void init_usb(void);
void usb_loop(void);



#endif


⌨️ 快捷键说明

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