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

📄 usbimpl.c

📁 C8051FTest.rar 是C8051F系列处理器的基本测试程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <d12interface.h>
#include <usb.h>
#include <uart0.h>

EPPFLAGS	bEppflags;
#define NUM_ENDPOINTS 4

#ifdef 	_HID_DEVICE
#define CONFIG_DESCRIPTOR_LENGTH sizeof(CONFIG_DESCRIPTOR) \
	+ sizeof(INTERFACE_DESCRIPTOR) \
	+ sizeof(HID_DESCRIPTOR)	\
	+ (NUM_ENDPOINTS *sizeof(ENDPOINT_DESCRIPTOR))
#else
#define CONFIG_DESCRIPTOR_LENGTH sizeof(CONFIG_DESCRIPTOR) \
	+ sizeof(INTERFACE_DESCRIPTOR) \
	+ (NUM_ENDPOINTS *sizeof(ENDPOINT_DESCRIPTOR))
#endif //_HID_DEVICE

extern CONTROL_XFER ControlData;

code DEVICE_DESCRIPTOR DeviceDescr =
{
	sizeof(DEVICE_DESCRIPTOR),
		USB_DEVICE_DESCRIPTOR_TYPE,
		SWAP(0x0110),
		0, //CLASS CODE
		0, //SUBCLASS code
		0,	//protocol
		EP0_PACKET_SIZE,	//max ep0 packet size
		SWAP(0x045e),	//vendor id
		SWAP(0x930a),	//product id
		SWAP(0x0100),	//device version
		1, //manufacturer 
		2, //product
		3,	//serial number
		1			//config num
};

code CONFIG_DESCRIPTOR ConfigDescr =
{
	sizeof(CONFIG_DESCRIPTOR),
		USB_CONFIGURATION_DESCRIPTOR_TYPE,
		SWAP(CONFIG_DESCRIPTOR_LENGTH),
		1,	//support interrupt
		1,	//config value
		4,	//configuration string
		0x80,	//config bus power
		0x32	//max power x*2mV
};
code INTERFACE_DESCRIPTOR InterfaceDescr =
{
	sizeof(INTERFACE_DESCRIPTOR),
		USB_INTERFACE_DESCRIPTOR_TYPE,
		0,	//interrupt num
		0,	//alter setup
		NUM_ENDPOINTS,	
#ifdef _HID_DEVICE
		0x3, //class code, 03 for hid device
#else
		0x0,
#endif //_HID_DEVICE
		0, //subclass code
		0, //protocol code
		5	//interface string
};

#ifdef _HID_DEVICE
code HID_DESCRIPTOR HidDescr = 
{
	sizeof(HID_DESCRIPTOR),
	USB_HID_DESCRIPTOR_TYPE,
	SWAP(0x101),			//bcd hid class specification release
	0,		//country code
	1,		//number of hid class descr to follow
	0x22,	//report descr type
	SWAP(0X34)		//total length of report desc
};
#endif //_HID_DEVICE

code ENDPOINT_DESCRIPTOR EP1_TXDescr =
{
	sizeof(ENDPOINT_DESCRIPTOR),
		USB_ENDPOINT_DESCRIPTOR_TYPE,
		0x81,	//ep1 in
#ifdef _HID_DEVICE
		0x2, //USB_ENDPOINT_TYPE_BULK
#else
		0x3, //USB_ENDPOINT_TYPE_INTERRUPT,
#endif //_HID_DEVICE
		SWAP(EP1_PACKET_SIZE),	//MAX packet size
		10	//interval 10ms
};
code ENDPOINT_DESCRIPTOR EP1_RXDescr =
{
	sizeof(ENDPOINT_DESCRIPTOR),
		USB_ENDPOINT_DESCRIPTOR_TYPE,
		0x1,	//ep1 out
#ifdef _HID_DEVICE
		0x2, //USB_ENDPOINT_TYPE_BULK
#else
		0x3, //USB_ENDPOINT_TYPE_INTERRUPT,
#endif //_HID_DEVICE
		SWAP(EP1_PACKET_SIZE),
		10
};

code ENDPOINT_DESCRIPTOR EP2_TXDescr =
{
	sizeof(ENDPOINT_DESCRIPTOR),
		USB_ENDPOINT_DESCRIPTOR_TYPE,
		0x82,
#ifdef _HID_DEVICE
		0x3, //USB_ENDPOINT_TYPE_INTERRUPT
#else
		0x2, //USB_ENDPOINT_TYPE_BULK
#endif //_HID_DEVICE
		SWAP(EP2_PACKET_SIZE),
		1
};
code ENDPOINT_DESCRIPTOR EP2_RXDescr =
{
	sizeof(ENDPOINT_DESCRIPTOR),
		USB_ENDPOINT_DESCRIPTOR_TYPE,
		0x2,
#ifdef _HID_DEVICE
		0x3, //USB_ENDPOINT_TYPE_INTERRUPT
#else
		0x2, //USB_ENDPOINT_TYPE_BULK,
#endif //_HID_DEVICE
		SWAP(EP2_PACKET_SIZE),
		1
};

code u8 StringLANGID[] =         //字符描述符
{
	4,							// bLength
	USB_STRING_DESCRIPTOR_TYPE,	// bDescriptorType  0x03
	0x09, 0x04//SWAP(0x0409)	// wLANGID
};

code u8 szManufacturer[] =       //厂商描述符字符串
{
	46,										// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType  0x03
	// String... http://www.njtu.edu.cn
	'h', 0, 't', 0, 't', 0, 'p', 0, ':', 0, 
	'/', 0, '/', 0, 'w', 0, 'w', 0, 'w', 0,
	'.', 0, 'n', 0, 'j', 0, 't', 0, 'u', 0, 
	'.', 0, 'e', 0, 'd', 0, 'u', 0, '.', 0,
	'c', 0, 'n', 0
};


code u8 szProduct[] =           //产品描述符字符串
{
	36,										// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType
	// String... USB Develop Board
	'U', 0, 'S', 0, 'B', 0, ' ', 0, 'D', 0, 
	'e', 0, 'v', 0, 'e', 0, 'l', 0, 'o', 0,
	'p', 0, ' ', 0, 'B', 0, 'o', 0, 'a', 0,
	'r', 0,	'd', 0
};

code u8 szSerialNumber[] =       //设备序列号字符串
{
	22,										// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType
	// String... 0001-10000
	'0', 0, '0', 0, '0', 0, '1', 0, '-', 0, 
	'1', 0, '0', 0, '0', 0, '0', 0, '0', 0
};

code u8 szConfiguration[] =      //用于描述该配置字符串
{
	72,										// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType
	// String...This configuration is used for Test
	'T', 0, 'h', 0, 'i', 0, 's', 0, ' ', 0, 
	'c', 0, 'o', 0, 'n', 0, 'f', 0, 'i', 0,
	'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 
	'i', 0, 'o', 0, 'n', 0, ' ', 0, 'i', 0,
	's', 0, ' ', 0, 'u', 0, 's', 0, 'e', 0, 
	'd', 0, ' ', 0, 'f', 0, 'o', 0, 'r', 0,
	' ', 0, 'T', 0, 'e', 0, 's', 0, 't', 0
};

code u8 szInterface[] =          //用于描述该接口字符串
{
	50,										// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType
	// String...Interface to the AStart UDK12 T00L
	'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 
	'f', 0, 'a', 0, 'c', 0, 'e', 0, ' ', 0,
	't', 0, 'o', 0, ' ', 0, 't', 0, 'h', 0, 
	'e', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0,
	' ', 0,	'K', 0, 'i', 0, 't', 0
};

code u8 szUserInfo[] =
{
	48,									// bLength
	USB_STRING_DESCRIPTOR_TYPE,				// bDescriptorType
	// String...   Created by Tu xiaoqiang
	'C', 0, 'r', 0, 'e', 0, 'a', 0, 't', 0, 
	'e', 0, 'd', 0, ' ', 0, 'b', 0, 'y', 0,
	' ', 0, 'T', 0,	'u', 0, ' ', 0, 'x', 0, 
	'i', 0, 'a', 0, 'o', 0, 'q', 0, 'i', 0,
	'a', 0,	'n', 0,	'g', 0
};

// used for HID 
#ifdef _HID_DEVICE
code u8 szHidReport[] =
{
	 0x06,  0xA0,  0xFF,//    Usage Page (FFA0h, vendor defined)
	 0x09, 0x01,     	//       Usage (vendor defined)
	  0xA1, 0x01,    	//       Collection (Application)
	 0x09, 0x02 ,    	//       Usage (vendor defined)
	  0xA1, 0x00,   	//       Collection (Physical)
	 0x06,  0xA1,  0xFF, //    Usage Page (vendor defined)

/// The Input report
	 0x09, 0x03 ,    //       Usage (vendor defined)
	 0x09, 0x04,     //       Usage (vendor defined)
	 0x15, 0x80,	//	Logical minimum  0x80 or -128)
	 0x25, 0x7F,	//	Logical maximum  0x7F or 127)
	 0x35, 0x00,	// 	Physical minimum (0)
	 0x45,  0xFF,	// 	Physical maximum (255)
	 0x75, 0x08,	//	Report size (8 bits)
	 0x95, 0x40,	//	Report count (64 fields)
	 0x81, 0x02,	//	Input (data, variable, absolute)

/// The Output report
	 0x09, 0x05,     //       Usage (vendor defined)
	 0x09, 0x06,     //       Usage (vendor defined)
	 0x15, 0x80,	//	Logical minimum  (0x80 or -128)
	 0x25, 0x7F,	//	Logical maximum  (0x7F or 127)
	 0x35, 0x00,	//	Physical minimum (0)
	 0x45,  0xFF,	//	Physical maximum (255)
	 0x75, 0x08,	//	Report size (8 bits)
	 0x95, 0x40,	//	Report count (64 fields)
	 0x91, 0x02,	//	Output (data, variable, absolute)

	  0xC0,         //       End Collection 0x(Pysical)
	  0xC0         //       End Collection (Application)
};
#endif //_HID_DEVICE

code void (*StandardDeviceRequest[])(void) =
{
	get_status,
	clear_feature,
	reserved,
	set_feature,
	reserved,
	set_address,
	get_descriptor,
	reserved,
	get_configuration,
	set_configuration,
	get_interface,
	set_interface,
	reserved,
	reserved,
	reserved,
	reserved
};
code void (*VendorDeviceRequest[])(void) =
{
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	reserved,
	read_write_register,
	reserved,
	reserved,
	reserved
};

#ifdef _HID_DEVICE
code void (*HidDeviceReuest[])(void) = 
{
	reserved,
	get_report,	//0x1	mandatory request and must be support by all devices
	get_idle,
	get_protocol,	//required only for boot devices
	reserved,	//0x4
	reserved,
	reserved,
	reserved,
	reserved,	//0x8
	set_report,
	set_idle,
	set_protocol
};
#endif //_HID_DEVICE

/*
//*************************************************************************
// USB Protocol Layer
//*************************************************************************
*/
void init_unconfig(void)
{
//	u8 i;
	D12_SetEndpointEnable(false); /* DISABLE all endpoints but EPP0. */
}
void init_config(void)
{
	D12_SetEndpointEnable(true); /* ENABLE generic/iso endpoints. */
}
void single_transmit(u8 * buf, u8 len)
{
	if( len <= EP0_PACKET_SIZE) {
		D12_WriteEndpointBuffer(1, buf, len);
	}
}

void code_transmit(u8 code * pRomData, u16 len)
{
	ControlData.wCount = 0;
	if(ControlData.wLength > len)
		ControlData.wLength = len;
	ControlData.pData = pRomData;
	if( ControlData.wLength >= EP0_PACKET_SIZE) {
		D12_WriteEndpointBuffer(1, ControlData.pData, EP0_PACKET_SIZE);
		ControlData.wCount += EP0_PACKET_SIZE;
		DISABLE_INTERRUPT;
		bEppflags.flags.control_state = USB_TRANSMIT;
		ENABLE_INTERRUPT;
	}
	else {
		D12_WriteEndpointBuffer(1, pRomData, ControlData.wLength);
		ControlData.wCount += ControlData.wLength;
		DISABLE_INTERRUPT;
		bEppflags.flags.control_state = USB_IDLE;
		ENABLE_INTERRUPT;
	}
}

void stall_ep0(void)
{
	D12_SetEndpointStatus(0, 1);
	D12_SetEndpointStatus(1, 1);
}

void reserved(void)
{
	stall_ep0();
}

void control_handler()
{
	u8 type, req;
	type = ControlData.DevRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
	req = ControlData.DevRequest.bRequest & USB_REQUEST_MASK;

⌨️ 快捷键说明

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