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

📄 chap_9.c

📁 USB_D12开发评估板实现USB键盘的全部源代码
💻 C
字号:
#include "..\..\Driver\inc\includes.h"


#include "mainloop.h"
#include "usb100.h"
#include "chap_9.h"


#define NUM_ENDPOINTS	4

#define CONFIG_DESCRIPTOR_LENGTH    34

extern CONTROL_XFER ControlData;
extern IO_REQUEST idata ioRequest;
extern EPPFLAGS bEPPflags;

/*
unsigned char code Keyboard_DeviceDescriptor[] = {
	18, 1,					// Length, Type
	0x10, 1,				// USB Rev 1.1 (=0110H, low=10H, High=01H)
	0, 0, 0,				// Class, Subclass and Protocol
	EP0Size,				// EP0 size
	0x42, 0x42, 0, 2, 0, 0,	// Vendor ID, Product ID and Version
	1, 0, 0,				// Manufacturer, Product & Serial# Names
	1		    			// #Configs
};
unsigned char code Keyboard_ConfigurationDescriptor[] = {
	9, 2,					// Length, Type
	34, 0,
	1, 1, 0,				// #Interfaces, Configuration#, Config. Name
	0xa0,					// Attributes = Bus Powered & Remote Wakened
	50		 			// Max. Power is 50x2 = 100mA
};
unsigned char code Keyboard_InterfaceDescriptor[] = {
	9, 4,					// Length, Type
	0, 0, 1,				// No alternate setting, HID uses EP1
	3,	      				// Class = Human Interface Device
	1, 1,					// Subclass and Protocol
	0	      				// Interface Name
};
unsigned char code Keyboard_HIDDescriptor[] = {
	9, 0x21,				// Length, Type
	0, 1,					// HID Class Specification compliance
	0,		  			// Country localization (=none)
	1,		 			// Number of descriptors to follow
	0x22,					// And it's a Report descriptor
	63, 00
};
unsigned char code Keyboard_EndpointDescriptor[] = {
	7, 5,					// Length, Type
	0x81,					// Address = IN 1
	0x03,					// Interrupt
	EP0Size, 0,			 	// Maximum packet size
	10		     			// Poll every 10 msec (OS will round down to 8) 
};

unsigned char code Keyboard_ReportDescriptor[] = {	// Generated with HID Tool, copied to here
	5, 1,	 		// Usage_Page (Vendor Defined)
	9, 6,			// Usage (I/O Device)
	0xa1, 1,		// Collection (Application)
	0x05, 07,
	0x19, 0xe0,		//   Usage_Minimum
	0x29, 0xe7,		//   Usage_Maximum
	0x15, 0,		//   Logical_Minimum (0)
	0x25, 1,		//   Logical_Maximum (1)
	0x75, 1,		//   Report_Size (1)
	0x95, 8,		//   Report_Count (8)
	0x81, 2,		//   Input (Data,Var,Abs)

	0x95, 0x01,
	0x75, 0x08,
	0x81, 0x01,
	0x95, 0x05,
	0x75, 0x01,
	0x05, 0x08,
	0x19, 0x01,
	0x29, 0x05,
	0x91, 0x02,

	0x95, 0x01,
	0x75, 0x03,
	0x91, 0x01,
	
	0x95, 0x06,
	0x75, 0x08,
	0x15, 0x00,
	0x25, 0x65,
	0x05, 0x07,
	0x19, 0x00,		//   Usage_Minimum
	0x29, 0x65,		//   Usage_Maximum
	0x81, 0x00,		//   Output (Data,Var,Abs)
	0xc0			// End_Collection
};

*/


//设备描述符
code USB_DEVICE_DESCRIPTOR DeviceDescr =
{
	18, 1,					// Length, Type
	0x1001,			// USB Rev 1.1 (=0110H, low=10H, High=01H)
	0, 0, 0,				// Class, Subclass and Protocol
	16,				// EP0 size
	0x4242, 0x0002, 0x0000,	// Vendor ID, Product ID and Version
	0, 0, 0,				// Manufacturer, Product & Serial# Names
	1	
};

//配置描述符
code USB_CONFIGURATION_DESCRIPTOR ConfigDescr =
{
 	9, 2,					// Length, Type
	0x2200,
	1, 1, 0,				// #Interfaces, Configuration#, Config. Name
	0xa0,					// Attributes = Bus Powered & Remote Wakened
	150						//Current = 150*2mA 
};

//接口描述符
code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
{
 	9, 4,					// Length, Type
	0, 0, 1,				// No alternate setting, HID uses EP1
	3,	      				// Class = Human Interface Device
	0, 1,					// Subclass and Protocol
	0	      				// Interface Name
};

unsigned char code Keyboard_HIDDescriptor[] = {
	9, 0x21,				// Length, Type
	0, 1,					// HID Class Specification compliance
	0,		  			// Country localization (=none)
	1,		 			// Number of descriptors to follow
	0x22,					// And it's a Report descriptor
	63, 00
};

//端点描述符
code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),		//7
	USB_ENDPOINT_DESCRIPTOR_TYPE,
	0x81,
	USB_ENDPOINT_TYPE_INTERRUPT,
	SWAP(EP1_PACKET_SIZE),
	10
};


unsigned char code Keyboard_ReportDescriptor[] = {	// Generated with HID Tool, copied to here
	5, 1,	 		// Usage_Page (Vendor Defined)
	9, 6,			// Usage (I/O Device)
	0xa1, 1,		// Collection (Application)
	0x05, 07,
	0x19, 0xe0,		//   Usage_Minimum
	0x29, 0xe7,		//   Usage_Maximum
	0x15, 0,		//   Logical_Minimum (0)
	0x25, 1,		//   Logical_Maximum (1)
	0x75, 1,		//   Report_Size (1)
	0x95, 8,		//   Report_Count (8)
	0x81, 2,		//   Input (Data,Var,Abs)

	0x95, 0x01,
	0x75, 0x08,
	0x81, 0x01,
	0x95, 0x05,
	0x75, 0x01,
	0x05, 0x08,
	0x19, 0x01,
	0x29, 0x05,
	0x91, 0x02,

	0x95, 0x01,
	0x75, 0x03,
	0x91, 0x01,
	
	0x95, 0x06,
	0x75, 0x08,
	0x15, 0x00,
	0x25, 0x65,
	0x05, 0x07,
	0x19, 0x00,		//   Usage_Minimum
	0x29, 0x65,		//   Usage_Maximum
	0x81, 0x00,		//   Output (Data,Var,Abs)
	0xc0			// End_Collection
};


/*
   //*************************************************************************
   // USB Protocol Layer
   //*************************************************************************
*/
//----------------------usb标准设备请求用到的了程序 ----------------/
//返回stall应答
void stall_ep0(void)
{
	D12_SetEndpointStatus(0, 1);
	D12_SetEndpointStatus(1, 1);
}

//恢复到未配置状态
//禁止0除外的所有端点
void init_unconfig(void)
{
//	unsigned char i;

	D12_SetEndpointEnable(0);	/* Disable all endpoints but EPP0. */
}

//设置配置状态
//允许端点收发。
void init_config(void)
{
	D12_SetEndpointEnable(1);	/* Enable  generic/iso endpoints. */
}

//从端点号1发送数据
void single_transmit(unsigned char * buf, unsigned char len)
{
	if( len <= EP0_PACKET_SIZE) 
	{
		D12_WriteEndpoint(1, len, buf);
	}
}

//发送端点号1建立代码
//将第1包数据发送到主机,后继发送与处理由中断程序来完成。
void code_transmit(unsigned char code * pRomData, unsigned short len)
{
	ControlData.wCount = 0;			//发送字符计数器请零。

	if(ControlData.wLength > len)
		ControlData.wLength = len;

	ControlData.pData = pRomData;
	if( ControlData.wLength >= EP0_PACKET_SIZE) 
	{
		D12_WriteEndpoint(1, EP0_PACKET_SIZE, ControlData.pData);//发送16字节数据
		ControlData.wCount += EP0_PACKET_SIZE;
		DISABLE;
		bEPPflags.bits.control_state = USB_TRANSMIT;
		ENABLE;
	}
	else 
	{
		D12_WriteEndpoint(1, ControlData.wLength, pRomData);//发送16字节内数据
		ControlData.wCount += ControlData.wLength;
		DISABLE;
		bEPPflags.bits.control_state = USB_IDLE;
		ENABLE;
	}
}


void reserved(void)
{
	stall_ep0();
}

/*
   //*************************************************************************
   // USB standard device requests
//----------

⌨️ 快捷键说明

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