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

📄 usb.c

📁 ATMEL AT89C51SND1 IDE TO USB READER
💻 C
📖 第 1 页 / 共 2 页
字号:
			return;
		}
	}


	if (UsbCtrlCmd.wLength > data_to_send)
	{
		if ((data_to_send % 8) == 0)
		{
			zeroflag = TRUE;                          
		}
		else
		{
			zeroflag = FALSE;                         
		}
	}
	else
	{
		data_to_send = UsbCtrlCmd.wLength;       
	}
	Usb_clr_rxsetup_bit() ;  

	while (data_to_send > 8)
	{

		Usb_sel_ep(0);
		for(i=0;i<8;i++){
			UEPDATX = G_pucCtrlDataPointer[desc_ptr] ;
			desc_ptr ++;
		}

		data_to_send = data_to_send - 8;
		ret = usb_send_data();
		if (ret == 1)return;

	}

	Usb_sel_ep(0); 
	for(i=0;i<data_to_send;i++){

		UEPDATX = G_pucCtrlDataPointer[desc_ptr] ;
		desc_ptr ++;
	}	

	data_to_send = 0;
	ret = usb_send_data();
	if (ret == 1)return;

	if (zeroflag == TRUE)
	{
		ret = usb_send_data();
		if (ret == 1)return;
	}
	while ((!(Usb_receive_out_data())) && (!(Usb_setup_packet_receive())));

	if (Usb_setup_packet_receive())
	{
		return;
	}

	if (Usb_receive_out_data())
	{
		Usb_clear_DIR_bit();                  
		Usb_clear_RXOUTB0_bit();
	}

}
/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_send_data                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
UINT8 usb_send_data (void)
{


	Usb_set_TXRDY_bit(); /*send zero packet*/ 
	while ((!(Usb_receive_out_data())) && (!(Usb_send_data_complete())));
	if ((Usb_receive_out_data()))             
	{
		Usb_clear_RXOUTB0_bit();
		return 1;
	}
	if (Usb_send_data_complete())
	{
		Usb_clear_TXCMPL_bit();
	}
	return 0;
}
/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_set_address                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_set_address (void)
{

	UINT8 add;

	Usb_clear_DIR_bit();
	add = UsbCtrlCmd.wValue&0x00ff;                    
	Usb_set_EP0RST();	
	Usb_clr_EP0RST();	
	Usb_clr_rxsetup_bit();                    
	Usb_set_FADDEN_bit();

	Usb_set_TXRDY_bit();    
	while (!(Usb_send_data_complete()));
	Usb_clear_TXCMPL_bit();
	Usb_set_USBADDR(add);

}
/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_get_configuration                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_get_configuration (void)
{


	Usb_clr_rxsetup_bit();
	Usb_set_DIR_bit();


	if (USBCON & 0x02)
	{
		UEPDATX = 1;
	}
	else
	{
		UEPDATX = 0;
	}

	Usb_set_TXRDY_bit();
	while (!(Usb_send_data_complete()));
	Usb_clear_TXCMPL_bit();
	while (!(Usb_receive_out_data()));
	Usb_clear_RXOUTB0_bit();
	Usb_clear_DIR_bit();

}

/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_set_configuration                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_set_configuration (void)
{
	UINT8 configuration_number;

	configuration_number = UsbCtrlCmd.wValue&0x00ff;
	Usb_clear_DIR_bit();
	Usb_clr_rxsetup_bit();
	Usb_set_TXRDY_bit();                          /* send a ZLP for STATUS phase */
	if (configuration_number == 0)
	{
		Usb_clear_CONFG();
	}
	else
	{
		Usb_set_CONFG();
	}
	while (!Usb_send_data_complete());
	Usb_clear_TXCMPL_bit();
	Usb_sel_ep(0x01);                     /* endpoints configuration */
	UEPCONX = 0x86 ;
	Usb_sel_ep(0x02);
	UEPCONX = 0x82;

}

/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_clear_feature                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_clear_feature (void)
{



	if (UsbCtrlCmd.bmRequestType == 0x00)
	{
		Usb_clr_rxsetup_bit();
		Usb_set_STALLRQ_bit();
		while (!(Usb_stall_sent_complete()));
		Usb_clear_STALLRQ_bit();
	}

	if (UsbCtrlCmd.bmRequestType == 0x01)
	{
		Usb_clr_rxsetup_bit();
		Usb_set_STALLRQ_bit();
		while (!(Usb_stall_sent_complete()));
		Usb_clear_STALLRQ_bit();
	}

	if (UsbCtrlCmd.bmRequestType == 0x02)
	{

		if ((UsbCtrlCmd.wValue&0xff) == 0x00)/*feature selector*/
		{

			switch ((UsbCtrlCmd.wIndex&0xff)&0x7f)           /*endpoint selector*/
			{

			case 0x00:
				{
					Usb_clr_rxsetup_bit();
					Usb_set_TXRDY_bit();
					while (!(Usb_send_data_complete()));
					Usb_clear_TXCMPL_bit();
					break;
				}
			case 0x01:
				{

					Usb_sel_ep(1);
					Usb_clear_STALLRQ_bit();
					Usb_set_EP1RST();
					Usb_clr_EP1RST();
					ep_status[1] =0x00; 
					Usb_sel_ep(0);
					Usb_clr_rxsetup_bit();
					Usb_set_TXRDY_bit();    
					while (!(Usb_send_data_complete()));
					Usb_clear_TXCMPL_bit();
					break;
				}
			case 0x02:
				{

					Usb_sel_ep(2);
					Usb_clear_STALLRQ_bit();
					Usb_set_EP2RST();
					Usb_clr_EP2RST();
					ep_status[2] = 0x00;
					Usb_sel_ep(0);
					Usb_clr_rxsetup_bit();
					Usb_set_TXRDY_bit();    
					while (!(Usb_send_data_complete()));
					Usb_clear_TXCMPL_bit();
					break;
				}
			default:
				{
					Usb_clr_rxsetup_bit();
					Usb_set_STALLRQ_bit();
					while (!(Usb_stall_sent_complete()));
					Usb_clear_STALLRQ_bit();
					break;
				}
			}
		}
	}

}

/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_set_feature                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_set_feature (void)
{


	if (UsbCtrlCmd.bmRequestType == 0x00)
	{
		Usb_clr_rxsetup_bit();
		Usb_set_STALLRQ_bit();
		while (!(Usb_stall_sent_complete()));
		Usb_clear_STALLRQ_bit();
	}
	if (UsbCtrlCmd.bmRequestType == 0x01)
	{

		Usb_clr_rxsetup_bit();
		Usb_set_STALLRQ_bit();
		while (!(Usb_stall_sent_complete()));
		Usb_clear_STALLRQ_bit();
	}


	if (UsbCtrlCmd.bmRequestType == 0x02)
	{

		if ((UsbCtrlCmd.wValue&0xff) == 0x00)/*feature selector*/
		{


			switch (UsbCtrlCmd.wIndex & 0x7f) // check wIndex 
			{
			case 0x01:
				{
					Usb_sel_ep(1);
					Usb_set_STALLRQ_bit();
					Usb_sel_ep(0);
					ep_status[1] = 0x01;
					Usb_clr_rxsetup_bit();
					Usb_set_TXRDY_bit();
					while (!(Usb_send_data_complete()));
					Usb_clear_TXCMPL_bit();
					break;
				}
			case 0x02:
				{
					Usb_sel_ep(2);
					Usb_set_STALLRQ_bit();
					Usb_sel_ep(0);
					ep_status[2] = 0x01;
					Usb_clr_rxsetup_bit();
					Usb_set_TXRDY_bit();
					while (!(Usb_send_data_complete()));
					Usb_clear_TXCMPL_bit();
					break;
				}
			default:
				{

					break;
				}
			}
		}
	}
}

/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_get_status                                                                               
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_get_status (void)
{


	UINT8 wIndex;
	Usb_clr_rxsetup_bit();
	Usb_set_DIR_bit();
	switch(UsbCtrlCmd.bmRequestType) 
	{
	case 0x80:
		{
			UEPDATX = 0x01;
			break;
		}
	case 0x81:
		{

			UEPDATX = 0x00;
			break;
		}
	case 0x82:
		{
			wIndex = UsbCtrlCmd.wIndex & 0x7f;
			UEPDATX = ep_status[wIndex];
			break;
		}
	}
	UEPDATX = 0x00;
	Usb_set_TXRDY_bit();
	while ((!(Usb_send_data_complete())) || (Usb_setup_packet_receive()));
	Usb_clear_TXCMPL_bit();
	while ((!(Usb_receive_out_data())) || (Usb_setup_packet_receive()));
	Usb_clear_DIR_bit();                  
	Usb_clear_RXOUTB0_bit();

}

/**************************************************************************
*                                                                                                                                 
*  Function Name:hid_set_idle                                                                              
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void hid_set_idle(void)
{


	Usb_clear_DIR_bit();
	Usb_clr_rxsetup_bit();
	Usb_set_TXRDY_bit();
	while (!(Usb_send_data_complete()));
	Usb_clear_TXCMPL_bit();
}

/**************************************************************************
*                                                                                                                                 
*  Function Name:usb_get_max_lun                                                                              
*                                                                                                                          
*  Descriptions:                                                       
*                                                                        
*  Arguments:                                                           
*                                                                       
*  Returns: None                                                         
*                                          
**************************************************************************/
void usb_get_max_lun(void)
{

	Usb_clr_rxsetup_bit();
	Usb_set_DIR_bit();
	UEPDATX = 0x00;
	Usb_set_TXRDY_bit();
	while ((!(Usb_send_data_complete())) || (Usb_setup_packet_receive()));
	Usb_clear_TXCMPL_bit();
	while ((!(Usb_receive_out_data())) || (Usb_setup_packet_receive()));
	Usb_clear_DIR_bit();                  
	Usb_clear_RXOUTB0_bit();

}

⌨️ 快捷键说明

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