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

📄 ggpdiusbd12.c

📁 自己写的usb啦
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	if(i>EP0_PACKET_SIZE)
		{
			D12_WriteEndpoint(1,EP0_PACKET_SIZE,CtrData.pData+CtrData.wCount);
			CtrData.wCount+=EP0_PACKET_SIZE;
			pc_flag.bits.control_in_endp=1;
			}
		else if(i!=0)
			{
				D12_WriteEndpoint(1,i,CtrData.pData+CtrData.wCount);
				CtrData.wCount+=i;
				pc_flag.bits.control_in_endp=0;
				}
				else if(i==0)
					{
						D12_WriteEndpoint(1,0,0);
						pc_flag.bits.control_in_endp=0;
					}
*/
	else if(pc_flag.bits.control_in_endp)dispatch();
	}
/******************************************************************************/
/******************************************************************************/
void endp1_txin()//unsigned char len,unsigned char* pData)
{
	D12_ReadLastTransactionStatus(3);//clear int flags
	#ifdef debug
	//	D12_WriteEndpoint(1,len,pData);
	#endif
	}
void endp1_rxout(void)
{
//	unsigned char len;
	D12_ReadLastTransactionStatus(2);//clear int flags
/*	len=D12_ReadEndpoint(2,16,Ep1Buf);//read data	sizeof(EpBuf)
	if(len!=0)
		pc_flag.bits.control_out_endp=1;//magnify recieve	ep1_rxdone
*/	
	}
void main_txin(void)
{
	D12_ReadLastTransactionStatus(5);//clear int flags
	}
void main_rxout(void)
{
//	unsigned char len,epstatus;
	D12_ReadLastTransactionStatus(4);//clear int flags
/*	epstatus&=0x60;
	//recieve data
	len=D12_ReadEndpoint(4,64,Ep2Buf);//read buffer status	64
	if(epstatus==0x60)
		len=D12_ReadEndpoint(4,64,Ep2Buf);//read dual buffer data	64
*/	
	}
 

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

/////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************************************/
/******************************************************************************/
void enum_transmit(unsigned char * buf,unsigned char len)
{
	if(len<=EP0_PACKET_SIZE)
		{
			D12_WriteEndpoint(1,len,buf);
		}
}
void init_unconfig(void)
{
	D12_SetEndpointEnable(0);
}
void init_config(void)
{
	D12_SetEndpointEnable(1);
}
/******************************************************************************/
void NAK_ep0()
{
	D12_SetEndpointStatus(0,1);
	D12_SetEndpointStatus(1,1);
}
void reserved(void)
{	
	NAK_ep0();
}

void code_transmit(unsigned char code * pRomData,unsigned short len)
{
	CtrData.wCount=0;		
	if(CtrData.wLength>len)
		CtrData.wLength=len;
		CtrData.pData=pRomData;
	if(CtrData.wLength>=EP0_PACKET_SIZE)
		{
			D12_WriteEndpoint(1,EP0_PACKET_SIZE,CtrData.pData);
			CtrData.wCount+=EP0_PACKET_SIZE;
			//#asm  cli #endasm
			pc_flag.bits.control_in_endp=1;//state=USB_TRANSMIT;
			//ENABLE;
		}
	else
		{
			D12_WriteEndpoint(1,CtrData.wLength,pRomData);	
			CtrData.wCount+=CtrData.wLength;
			//#asm cli #endasm;
			pc_flag.bits.usb_idle=1;
			pc_flag.bits.control_in_endp=0;
			//ENABLE;
		}
	
}
/**/
/////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////
/******************************************************************************/
//STANDARD SERVICE 
void get_status(void)
{
	unsigned char endp,txdat[2];
	unsigned char bRecipient=CtrData.DeviceRequest.bmRequestType&0x1f;  //USB_RECIPIENT
	unsigned char c;
	if(bRecipient==0x00)//return device state	USB_RECIPIENT_DEVICE
		{
		/*	if(pc_flag.bits.remote_wakeup==1)
				txdat[0]=2;//1-0:1,REMOTE_WAKEUP
			else
		*/		txdat[0]=0;//0,BUS-POWER-SUPPORTED
			txdat[1]=0;
			enum_transmit(txdat,2);							
			}
	else if(bRecipient==0x01)//return interface state	USB_RECIPIENT_INTERFACE
		{
			txdat[0]=0;
			txdat[1]=0;
			enum_transmit(txdat,2);
			
			}
	else if(bRecipient==0x02)//					     USB_RECIPIENT_ENDPOINT
		{
			endp=(unsigned char)(CtrData.DeviceRequest.wIndex&MAX_ENDPOINTS);
				if(CtrData.DeviceRequest.wIndex&(unsigned char )USB_ENDPOINT_DIRECTION_MASK)
					c=D12_SelectEndpoint(endp*2+1);
					else
						c=D12_SelectEndpoint(endp*2);
				if(c&0x02)//D12_STALL==0x02
					txdat[0]=1;
					else
						txdat[0]=0;
						txdat[1]=0;
						enum_transmit(txdat,2);
			}
		else NAK_ep0();
}

void clear_feature(void)
{
	unsigned char endp;//read to know recipient
	unsigned char bRecipient=CtrData.DeviceRequest.bmRequestType&0x1f;			  //USB_RECIPIENT
	if(bRecipient==0x00&&CtrData.DeviceRequest.wValue==0x01)//clear remote	USB_RECIPIENT_DEVICE	USB_FEATURE_REMOTE_WAKEUP
		{
			//#asm cli #endasm;
			//pc_flag.bits.remote_wakeup=0;
			//ENABLE;
			enum_transmit(0,0);//return ack			
	/*	*/	}
	else if(bRecipient==0x02&&CtrData.DeviceRequest.wValue==0x00) //USB_RECIPIENT_ENDPOINT	USB_FEATURE_ENDPOINT_STALL
		{
			endp=(unsigned char )(CtrData.DeviceRequest.wIndex&MAX_ENDPOINTS);
			if(CtrData.DeviceRequest.wIndex&(unsigned char)USB_ENDPOINT_DIRECTION_MASK)
				D12_SetEndpointStatus(endp*2+1,0);
				else D12_SetEndpointStatus(endp*2,0);
			enum_transmit(0,0);
			}
		else	NAK_ep0();
			
	}
void set_feature(void)
{
	unsigned char endp;
	unsigned char bRecipient=CtrData.DeviceRequest.bmRequestType&0x1f;			   //USB_RECIPIENT
	if(bRecipient==0x00&&CtrData.DeviceRequest.wValue==0x01)			   //USB_RECIPIENT_DEVICE	USB_FEATURE_REMOTE_WAKEUP
		{
			//#asm cli #endasm;
			//pc_flag.bits.remote_wakeup=1;
			//ENABLE;
			//enum_transmit(0,0);//return ack
	/*	*/	}
		else if(bRecipient==0x02&&CtrData.DeviceRequest.wValue==0x00)	//USB_RECIPIENT_ENDPOINT  USB_FEATURE_ENDPOINT_STALL
				{
					endp=(unsigned char )(CtrData.DeviceRequest.wIndex&MAX_ENDPOINTS);
					if(CtrData.DeviceRequest.wIndex&(unsigned char)USB_ENDPOINT_DIRECTION_MASK)
						D12_SetEndpointStatus(endp*2+1,1);
						else D12_SetEndpointStatus(endp*2,1);
					enum_transmit(0,0);
					}
		else	NAK_ep0();
		
	}
	
void set_address(void)
{
	D12_SetAddressEnable((unsigned char)(CtrData.DeviceRequest.wValue&0x7f),1);///////DEVICE_ADDRESS_MASK
	enum_transmit(0,0);
}

void get_descriptor(void)
{
	unsigned char bDescriptor=MSB(CtrData.DeviceRequest.wValue);
	//pc_flag.bits.setup_packet=0;
	if(bDescriptor==USB_DEVICE_DESCRIPTOR_TYPE)
		{
			code_transmit((unsigned char code *)&DeviceDescr,sizeof(USB_DEVICE_DESCRIPTOR));//USB_DEVICE_DEScon_int_endp_descriptorCRIPTOR
		}
	else if(bDescriptor==USB_CONFIGURATION_DESCRIPTOR_TYPE)
			{
				if (CtrData.DeviceRequest.wLength>0x2e)	//   CONFIG_DESCRIPTOR_LENGTH
					{
						CtrData.DeviceRequest.wLength=0x2e;//CONFIG_DESCRIPTOR_LENGTH;
					}
				code_transmit((unsigned char code *)&ConfigDescr,CtrData.DeviceRequest.wLength);//configuration_descriptor
			 }
	else	NAK_ep0();
	//pc_flag.bits.setup_packet=1;	
}
/**/
void get_configuration(void)
{
	unsigned char c=pc_flag.bits.configuration;
	enum_transmit(&c,1);//
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void set_configuration(void)
{
	if(CtrData.DeviceRequest.wValue==0)
		{
			enum_transmit(0,0);
			//#asm cli #endasm;
			pc_flag.bits.configuration=0;
			//ENABLE;
			init_unconfig();
		}
		else if(CtrData.DeviceRequest.wValue==1)
			{
				enum_transmit(0,0);
				init_unconfig();
				init_config();
				//#asm cli #endasm;
				pc_flag.bits.configuration=1;
				//ENABLE;
			}
			else	NAK_ep0();
			
}
/////////////////////////////////////////////////////////////////////////////////////////////////

void get_interface(void)
{
	unsigned char txdat=0;
	enum_transmit(&txdat,1);
}
	
void set_interface(void)
{
	if(CtrData.DeviceRequest.wValue==0&&CtrData.DeviceRequest.wIndex==0)
		enum_transmit(0,0);
	else	NAK_ep0();
}
/**code****************************************************************************/
 void (* StandardDeviceRequest[])(void)=
{
	get_status,
	clear_feature,
	reserved,
	set_feature,
	reserved,
	set_address,
	get_descriptor,//reserved,
	reserved,//set_descriptor,51's rom can't refresh itself,otherwise,new or renew descriptors should be added
	get_configuration,
	set_configuration,
	get_interface,
	set_interface,
	reserved,//synch frame,
	reserved,
	reserved,
	reserved	
};
/******************************************************************************/
void dispatch()
{
 	unsigned char type,req;//read request type ,store bit5 bit6
 	type=CtrData.DeviceRequest.bRequest&0x0f;//read request code store low 4 bits
 	req=CtrData.DeviceRequest.bmRequestType&0x60;
 	if(req==USB_STANDARD_REQUEST)
 		(* StandardDeviceRequest[type])();//standard handle
 				else pc_flag.bits.setup_packet=0;
 		}
/******************************************************************************/
/******************************************************************************/
void ISR_CONSULT()
{
	//while(1)
	while(!ISR)
		{
		   read_int_register();
			 if(int_flag.bits.bus_reset			){	busRST(); 	    UART51schar('1');	//continue;
		 											}									
			 if(int_flag.bits.suspend			){	busSUS();       UART51schar('2'); 	//continue;	
			 										}
		     if(int_flag.bits.control_out_endp  ){	endp0_rxout();  UART51schar('3');	//continue;	
			 									    }
			 if(int_flag.bits.control_in_endp	 
			   	&pc_flag.bits.control_in_endp   ){  endp0_txin();   UART51schar('4');	//continue;      		
													}
			 if(int_flag.bits.endp_1_out		){	endp1_rxout();	UART51schar('5'); 		}
			 if(int_flag.bits.endp_1_in 		){	endp1_txin();	UART51schar('6');		}
		     if(int_flag.bits.main_out_endp		){	main_rxout();	UART51schar('7');		}//
			 if(int_flag.bits.main_in_endp		){	main_txin();	UART51schar('8');		}
	/**///	 if( pc_flag.bits.setup_packet		){	dispatch();	   UART51schar(0x0c);				}
		 }
}
/******************************************************************************/
/******************************************************************************/

⌨️ 快捷键说明

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