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

📄 d12int.h

📁 44b0+PDIUSBD12开发的USB鼠标源代码
💻 H
字号:
 
#include "D12Def.h" 
#include "UsbDesc.h"
/////////////////////// 
#include "Terminal.h"
#include "..\INC\KeyLed.h"
//////////////////////

#define	STATUS_ERR	1
#define	STATUS_OK	0

static U8 usb_remote_wake;
static U8 usb_config;
static U8 UsbSendDescF;

static int send_remain;
static U32 send_ptr;

//void Delay(U32 Time);

U8 UsbInit()
{
	UsbSendDescF = 0;

	if(D12RdChipId()!=0x1210 ){
		return STATUS_ERR;
	}

	D12SetDma(MyD12DmaCfg);
	if(D12GetDma()!=MyD12DmaCfg){
		Tmn_StatusPrintStr("ERROR in D12Int!");
		return STATUS_ERR;
	}

	D12SetMode(MyD12EpCfgOff, D12Pll24M);
	UDelay(100);/********!!!!!!!!!!!!!!!!!!!!!!*********/
	D12SetMode(MyD12EpCfgOn, D12Pll24M);

	return STATUS_OK;
}

///////////////////////////////

#define	UsbDevDescType	1
#define	UsbCfgDescType	2
#define	UsbStrDescType	3
#define	UsbItfDescType	4
#define	UsbEndpDescType	5
#define	UsbPwrDescType	6

#define	UsbDevDescSize	18
#define	UsbCfgDescSize	9
#define	UsbItfDescSize	9
#define	UsbEndpDescSize	7

#define	UsbEndpControl		0
#define	UsbEndpIsochronous	1
#define	UsbEndpBulk			2
#define	UsbEndpInterrupt	3	

////////////////////////////////////////

#define	UsbClassMassStorage	8

#define	UsbSubClassRbc		1
#define	UsbSubClassSff8020i	2
#define	UsbSubClassQic157	3
#define	UsbSubClassUfi		4
#define	UsbSubClassSff8070i	5
#define	UsbSubClassScsi		6

#define	UsbProtocolCbi0	0
#define	UsbProtocolCbi1	1
#define	UsbProtocolBulk	0x50
////////////////////////////////////////

#define	Ep0PacketSize	16	//Ep0一次最大可发送16字节
#define	Ep2PacketSize	64  //Ep2一次最大可发送64字节
#define	EndpCnt	4
#define	UsbTotalCfgDescSize	UsbCfgDescSize+UsbItfDescSize+EndpCnt*UsbEndpDescSize
static __packed struct{		
	UsbDevDesc DevDesc;
	U8 CfgDesc[9];
	U8 ItfDesc[9];
	
	U8 HIDDesc[9];
	U8 EndpDesc[7];
	U8 ReportDesc[72];

}ThisDevDesc =
				{
					{					//设备描述符
					    UsbDevDescSize,
						UsbDevDescType,
						0x0101,			//bcdUSB(USB版本号)**************
						0, 0, 0,		//bDeviceClass,bDeviceSubClass,bDeviceProtocol
						Ep0PacketSize,//*****************
						0x708,			//idVendor
						0x223,			//idProduct
						0x113,			//bcdDevice(设备出场编号)
						1, 2, 0,		//iMannufacture,iProduct,iSerialNumber,
						1				//bNumConfiguration
					},
					{					//配置描述符
						 0x09, 		/* length of descriptor (9 bytes) */
						 0x02, 		/* descriptor type (CONFIGURATION) */
						 0x22, 0x00,/* total length of descriptor (33 bytes) */
						 0x01, 		/* number of interfaces to configure (1) */
						 0x01, 		/* configuration value (1) */
						 0x04, 		/* configuration string index (4) */
						 0xA0, 		/* configuration attributes (bus powered, remote wakeup) */
						 0x64 		/* maximum power (set at 100, change this value for your specific optics)*/
					},
					{					//接口描述符
						 0x09, 		/* length of descriptor (9 bytes) */
						 0x04, 		/* descriptor type (INTERFACE) */
						 0x00, 		/* interface number (0) */
						 0x00, 		/* alternate setting (0) */
						 0x01, 		/* number of endpoints (1) */
						 0x03, 		/* interface class (3..defined by USB spec) */
						 0x01, 		/* interface sub-class (1..defined by USB spec) */
						 0x02, 		/* interface protocol (2..defined by USB spec) */
						 0x00  		/* interface string index (not supported) */
					},
					{
					 0x09, 		/* descriptor size (9 bytes) */
					 0x21, 		/* descriptor type (HID) */
					 0x10, 0x01,/* class specification (1.10) */
					 0x00, 		/* hardware target country */
					 0x01, 		/* number of hid class desriptors to follow (1) */
					 0x22, 		/* report descriptor type (2) */
					 72,//sizeof(hid_report_desc_table),
					 0x00
					},
					{					//端点描述符
						 0x07, 		/* descriptor length (7 bytes) */
						 0x05, 		/* descriptor type (ENDPOINT) */
						 0x81, 		/* endpoint address (IN endpoint, endpoint 1) */
						 0x03, 		/* endpoint attributes (interrupt) */
						 64, 0x00, /* maximum packet size (4 bytes) */
						 0x06  		/* polling interval (10ms) */
					},
					
					{
						 0x05, 0x01, /*usage page (generic desktop) */
						 0x09, 0x02, /*usage (mouse) */
						 0xA1, 0x01, /*collection (application) */
						 0x05, 0x09, /*usage page (buttons) */
						 0x19, 0x01, /*usage minimum (1) */
						 0x29, 0x03, /*usage maximum (3) */
						 0x15, 0x00, /*logical minimum (0) */
						 0x25, 0x01, /*logical maximum (1) */
						 0x95, 0x03, /*report count (3 bytes) */
						 0x75, 0x01, /*report size (1) */
						 0x81, 0x02, /*input (3 button bits) */
						 0x95, 0x01, /*report count (1) */
						 0x75, 0x05, /*report size (5) */
						 0x81, 0x03, /*input (constant 5 bit padding) */
						 0x05, 0x01, /*usage page (generic desktop) */
						 0x09, 0x01, /*pointer*/
						 0xA1, 0x00, /*linked*/
						 0x09, 0x30, /*X*/
						 0x09, 0x31, /*Y*/
						 0x15, 0x81, /*logical minimum (-127) */
						 0x25, 0x7F, /*logical maximum (127) */
						 0x75, 0x08, /*report size (8) */
						 0x95, 0x02, /*report count (2) */
						 0x81, 0x06, /*input (2 position bytes X & Y) */
						 0xC0,		 /*end collection*/
						 0x09,0x38,  /*wheel*/
						 0x95,0x01,	 /*wheel size = 1 byte*/
						 0x81,0x06,  /*variable data bit field with relative position*/
						 0x09,0x3c,  /*motion wakeup*/
						 0x15,0x00,  /*0 no movement*/
						 0x25,0x01,  /*1 movement*/
						 0x75,0x01,  /*1st bit represents movement*/
						 0x95,0x01,  /*1 report*/
						 0xb1,0x22,  /*	Variable Data Bit Field with  absolute positioning and no preferred state*/
						 0x95,0x07,  /*	7 reports for reversing, upper 7 bits of byte 3*/
						 0xb1,0x01,  /*Constant Array Bit Field with absolute positioning*/
						 0xC0        /* end collection, end collection */		
					}
									
				};

/****************** standard device request ****************/
U8 UsbGetStatus(U8 *SetupPkt)
{
	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[5]|SetupPkt[7])
		return 1;
	if(SetupPkt[6]!=2)
		return 1;
	if(SetupPkt[0]==0x80)
	{
		if(SetupPkt[4])
			return 1;
		SetupPkt[0] = usb_remote_wake?2:1;//猜想1:0是措的;
		SetupPkt[1] = 0;
	}
	else
	if(SetupPkt[0]==0x81)
	{
		if(SetupPkt[4])
			return 1;
		SetupPkt[0] = 0;
		SetupPkt[1] = 0;		
	}
	else
	if(SetupPkt[0]==0x82)
	{
		D12SelEp((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0)); 
		SetupPkt[1] = (D12Dat&2)?1:0;
		SetupPkt[0] = 0;
	}
	else
		return 1;

	D12WrEp(1, SetupPkt, 2);//Ep1控制输入
	return 0;		
}

U8 UsbClrFeature(U8 *SetupPkt)
{

	if(SetupPkt[0]&0x80)
		return STATUS_ERR;
	if(SetupPkt[3]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])	
		return STATUS_ERR;
		
	if(SetupPkt[0]&0x1f)
	{
		if((SetupPkt[0]&0x1f)!=2)
			return STATUS_ERR;	
		D12SetEpStat((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0),0); 
	}							
	else
	{
		if(SetupPkt[4])
			return STATUS_ERR;
		usb_remote_wake = 0;
	}
	
	D12WrEp(1, SetupPkt, 0);//清空Ep1缓冲区
	return STATUS_OK;
}

U8 UsbSetFeature(U8 *SetupPkt)
{
	if(!(SetupPkt[0]&0x80))
		return STATUS_ERR;
	if(SetupPkt[3]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
		
	if(SetupPkt[0]&0x1f)
	{
		if((SetupPkt[0]&0x1f)!=2)
			return STATUS_ERR;
		D12SetEpStat((SetupPkt[4]*2)|((SetupPkt[4]&0x80)?1:0),1); 
	}								
	else
	{
		if(SetupPkt[4])
			return STATUS_ERR;
		usb_remote_wake = 1;
	}
	
	D12WrEp(1, SetupPkt, 0);//清空Ep1缓冲区
	return STATUS_OK;
}

U8 UsbSetAddress(U8 *SetupPkt)
{

	if(SetupPkt[0]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
		
	D12SetAddr(SetupPkt[2]|0x80);//最高位为使能位
	D12WrEp(1, SetupPkt, 0);
	return STATUS_OK;	
}

void UsbSendDesc()
{
 

	UsbSendDescF = send_remain>=Ep0PacketSize;	
	D12WrEp(1, (U8 *)send_ptr, (send_remain>Ep0PacketSize)?Ep0PacketSize:send_remain);
	

		
	send_remain -= Ep0PacketSize;
	send_ptr    += Ep0PacketSize;
	
}

U8 UsbGetDescriptor(U8 *SetupPkt)
{
	
	if(!(SetupPkt[0]==0x80||SetupPkt[0]==0x81))
		return STATUS_ERR;
		
	switch	(SetupPkt[3])
	{
	case UsbDevDescType:
		send_ptr = (U32)&ThisDevDesc;
		send_remain = UsbDevDescSize;
		break;
	case UsbCfgDescType:
		send_ptr = (U32)&ThisDevDesc.CfgDesc;
		send_remain = UsbTotalCfgDescSize;
		break;
	case 34:
		send_ptr = (U32)&ThisDevDesc.ReportDesc;//获取报告描述符
		send_remain = 72;
		break;
	case UsbStrDescType:		
		return STATUS_ERR;	
	default:
		return STATUS_ERR;	
	}

	if(!SetupPkt[7]&&(SetupPkt[6]<send_remain))//当描述符大小大于WLength字段值时,只发送主机所要求的大小
		send_remain = SetupPkt[6];

	UsbSendDesc();	
	return STATUS_OK;	
}

U8 UsbGetConfiguration(U8 *SetupPkt)
{
	if(SetupPkt[0]!=0x80)
		return STATUS_ERR;
	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[6]!=1)
		return STATUS_ERR;
		
	SetupPkt[0] = usb_config?1:0;
	D12WrEp(1, SetupPkt, 1);	
	return STATUS_OK;	
}

U8 UsbSetConfiguration(U8 *SetupPkt)
{
	if(SetupPkt[0]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[6]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[2]&0xfe)//即wValue值只能为0或1
		return STATUS_ERR;
		
	usb_config = SetupPkt[2]&1;
	D12SetEp(usb_config);
	D12SetEpStat(4, usb_config^1);
	D12SetEpStat(5, usb_config^1);
	D12WrEp(1, SetupPkt, 0);
	
	return STATUS_OK;
}

U8 UsbGetInterface(U8 *SetupPkt)
{
	if(SetupPkt[0]!=0x81)
		return STATUS_ERR;
	if(SetupPkt[2]|SetupPkt[3]|SetupPkt[4]|SetupPkt[5]|SetupPkt[7])
		return STATUS_ERR;
	if(SetupPkt[6]!=1)
		return STATUS_ERR;
		
	SetupPkt[0] = 0;
	D12WrEp(1, SetupPkt, 1);
	return STATUS_OK;
}

U8 UsbSetInterface(U8 *SetupPkt)
{
	return STATUS_ERR;
}

U8 UsbReserved(U8 *SetupPkt)
{
	return STATUS_ERR;	
}


U8 (*StdDevReq[])(U8 *SetupPkt) = {
	UsbGetStatus,
	UsbClrFeature,
	UsbReserved,
	UsbSetFeature,
	UsbReserved,
	UsbSetAddress,
	UsbGetDescriptor,
	UsbReserved,
	UsbGetConfiguration,
	UsbSetConfiguration,
	UsbGetInterface,
	UsbSetInterface,
	UsbReserved,
	UsbReserved,
	UsbReserved,
	UsbReserved
}; 
/******************HID特定类命令请求****************************/
U8	UsbGetReport(U8 *SetupPkt){
	//if(SetupPkt[1]==1)
	
	Tmn_StatusPrintNum(SetupPkt[1]);
	Tmn_StatusPrintStr("	ClassRequest!");
	return 0;
}
/***************************************************************/
/********************** D12 interrupt process ******************/
void D12Ep0IntProc()
{
	U8 SetupPkt[8];
	
	UsbSendDescF = 0;
	
	D12RdLastTrStat(0);//读最后处理状态寄存器
	if(D12Cmd&0x20)		 //为何不能用 !! D12Dat !! ????	
						 //表示最后成功接收的信息包有一个SETUP 标志
		if(D12RdEp(0, SetupPkt, 8)==8)
		{
			D12AckEp(0);
			D12AckEp(1);
			if(SetupPkt[1]==6){
			    Tmn_StatusPrintNum(SetupPkt[3]);
			    }
			 if(SetupPkt[0]&6!=0x00){
				Tmn_StatusPrintNum(SetupPkt[0]);
			    Tmn_StatusPrintNum(SetupPkt[1]);
			    }
			//if(SetupPkt[0]&0x60==0x00){	
				if(!StdDevReq[SetupPkt[1]&0xf](SetupPkt));//函数分支处理技巧
					return;	//返回值为0,成功处理,若为1,则出错!
			//}else{
				//UsbGetReport(SetupPkt);
				 
			//}
		}
					
	//D12SetEpStat(0, D12EpStall);
	//D12SetEpStat(1, D12EpStall);		
}

void D12Ep1IntProc()//Ep1控制输入,最大信息包规格16字节
{
	U8 i;

	i = D12RdLastTrStat(1);
	if(UsbSendDescF){	//信息描述大于16字节,需再发一次
		UsbSendDesc();
	}else{
		D12WrEp(1, &i, 0);//!!!极其关键,控制传输的状态信息步确认传输!!!
	}
		
}

void D12Ep2IntProc()
{
	//D12SetEpStat(2, D12EpStall);
}

void D12Ep3IntProc()
{
	BYTE SdData[4];
	
	SdData[0]=0;
	SdData[1]=0;
	SdData[2]=0;
	SdData[3]=0;
	switch(Key_Scan()){
		case 1:SdData[0]=1;break;
		case 2:SdData[0]=2;break;
		//case 3:SdData[2]=129;break;
		//case 4:SdData[2]=1;break;
	}
	
	D12WrEp(3, SdData, 4);
	//Tmn_StatusPrintStr("READ Ep3");
	//D12SetEpStat(3, D12EpStall);
}

void D12Ep4IntProc()//Ep4普通输出,最大信息包规格64字节
{
	U8 size;
	U8 Ep2OutBuf[Ep2PacketSize];

	D12RdLastTrStat(4);	
	size = D12RdEp(4, Ep2OutBuf, Ep2PacketSize);
	Tmn_StatusPrintNum(Ep2OutBuf[0]);
	Tmn_StatusPrintNum(Ep2OutBuf[1]);
	Tmn_StatusPrintNum(Ep2OutBuf[2]);
	Tmn_StatusPrintNum(Ep2OutBuf[3]);
	Tmn_StatusPrintNum(Ep2OutBuf[4]);
	Tmn_StatusPrintNum(Ep2OutBuf[5]);
	Tmn_StatusPrintNum(Ep2OutBuf[6]);
	
	if((size==6)&&(*(U32 *)Ep2OutBuf==0x12345678)) {
		//LedSet(Ep2OutBuf[4]);
		Tmn_StatusPrintStr("LedON");
		//printf("%x\n", Ep2OutBuf[4]+(Ep2OutBuf[5]<<8));
	}
}

void D12Ep5IntProc(){
	
	U8 SdData[4];
	
	SdData[0]=0x12;
	SdData[1]=0x34;
	SdData[2]=0x56;
	SdData[3]=0x78;
	
	D12WrEp(5, SdData, 4);
}

void D12BusRstProc()
{
}

void D12SuspChgProc()
{
}

⌨️ 快捷键说明

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