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

📄 usb.c

📁 HardDiskUSBconnectsourcecode
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************
 *                         H E A D E R   F I L E S
 *************************************************************************/
#include "stdio.h"
#include<ABSACC.H>
#include <reg51.h>
#include <intrins.h>
#include "AT89C51SND.h"
#include "typedef.h"
#include "config.h"
#include "constant.h"
#include "usb.h"
#include "global.h"
#include "gpio.h"
#include "macro.h"
#include "external.h"

/********************************************
*
*GET_DESCRIPTOR(Device Descriptor = 18 byte)
*
********************************************/
code UINT8   USB_DeviceDescriptor[] =
{
		

		
		0x12,                   /*bLength:*/
		0x01,                   /*bDescriptorType*/
		0x10, 0x01,           /*bcdUSB*/
		0x00,                   /*bDeviceClass*/
		0x00,                   /*bDeviceSubClass*/
		0x00,                   /*bDeviceProtocol*/
		0x08,                   /*bMaxPacketSize0*/
		0x34, 0x22,           /*idVendor*/
		0x78, 0x56,           /*idProduct*/
		0x01, 0x00,           /*bcdDevice*/
		0x01,                   /*iManufacturer*/
		0x02,                   /*iProduct*/
		0x03,                   /*iSerialNumber*/
		0x01,                   /*bNumConfigurations*/
};

/********************************************
*
*GET_DESCRIPTOR(Configuration Descriptor = 34 byte)
*
********************************************/
code UINT8   USB_ConfigurationDescriptor[] =
{
		
		
		0x09,                   /*bLength*/
		0x02,                   /*bDescriptorType*/
		0x20, 0x00,           /*wTotalLength*/
		0x01,                   /*bNumInterfaces*/
		0x01,                   /*bConfigurationValue*/
		0x00,                   /*iConfiguration*/
		0x80,                   /*bmAttributes*/
		0xFA,                   /*bMaxPower*/


		/*Interface Descriptor(0x09 byte)*/
		0x09,                   /*bLength             */
		0x04,                   /*bDescriptorType  */
		0x00,                   /*bInterfaceNumber*/
		0x00,                   /*bAlternateSetting*/
		0x02,                   /*bNumEndpoints:*/
		0x08,                   /*bInterfaceClass*/
		0x06,                   /*bInterfaceSubClass:*/
		0x50,                   /*bInterfaceProtocol*/
		0x00,                   /*iInterface*/

		/*Endpoint Descriptor (0x07 byte)*/
		0x07,                   /*bLength:*/
		0x05,                   /*bDescriptorType:*/
		0x81,                   /*bEndpointAddress*/
		0x02,                   /*bmAttributes*/
		0x40, 0x00,           /*wMaxPacketSize*/
		0x00,                   /*bInterval: ignored*/
		                   

		/*Endpoint Descriptor (0x07 byte)*/
		0x07,                   /*bLength:*/
		0x05,                   /*bDescriptorType:*/
		0x02,                   /*bEndpointAddress*/
		0x02,                   /*bmAttributes*/
		0x40, 0x00,           /*wMaxPacketSize*/
		0x00,                   /*bInterval: ignored*/

};

/********************************************
*
*GET_DESCRIPTOR(STRING Descriptor = 4 byte)
*
********************************************/
code UINT8   USB_LanguageIdString[] =
{
		
		0x04,              /*bLength*/
		0x03,              /*bDescriptorType: STRING*/
		0x09, 0x04,     /*bString: English (US)*/
};

/********************************************
*
*GET_DESCRIPTOR(STRING Descriptor = 32 byte)
*
********************************************/
code UINT8   USB_ManufacturerString[] =
{

		0x20,           /*bLength*/
		0x03,           /*bDescriptorType: STRING*/
		'a', 0x00,     
		't', 0x00,
		'm', 0x00,
		'e', 0x00,
		'l', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
		' ', 0x00,
};

/********************************************
*
*GET_DESCRIPTOR(STRING Descriptor = 48 byte)
*
********************************************/
code UINT8   USB_ProductString[] =
{
		
		0x30,           /*bLength*/
		0x03,           /*bDescriptorType: STRING*/
		'a',0x00,     
		't',0x00,
		'm',0x00,
		'e',0x00,
		'l',0x00,
		'a',0x00,
		't',0x00,
		'8',0x00,
		'9',0x00,
		'c',0x00,
		'5',0x00,
		'1',0x00,
		's',0x00,
		'n',0x00,
		'd',0x00,
		'1',0x00,
		'c',0x00,
		' ',0x00,
		' ',0x00,
		' ',0x00,
		' ',0x00,
		' ',0x00,
		' ',0x00,
};

/********************************************
*
*GET_DESCRIPTOR(STRING Descriptor = 18 byte)
*
********************************************/
code UINT8   USB_SerialNumberString[] =
{
		
		0x12,           /*bLength*/
		0x03,           /*bDescriptorType: STRING*/
		'0', 0x00,     /*bString: 01.00.00*/
		'1', 0x00,
		'.', 0x00,
		'0', 0x00,
		'0', 0x00,
		'.', 0x00,
		'0', 0x00,
		'0', 0x00,
};

/**************************************************************************
 *                                                                                                                                 
 *  Function Name: usb_init                                                                               
 *                                                                                                                          
 *  Descriptions:                                                       
 *                                                                        
 *  Arguments:                                                           
 *                                                                       
 *  Returns: None                                                         
 *                                          
 **************************************************************************/

void usb_init(void ){

       Pll_ndiv(1);
       Pll_rdiv(11);
       Pll_enable();
       Set_Usb_clock(1);
       Usb_enable();      /*Enable usb controller*/	
       Reset_All_Ep();   
       Stop_Reset_All_Ep();
       Usb_sel_ep(0);   /*select ep0*/ 
	 UEPCONX = 0x80;
}

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



	if (Usb_reset_event())                      
	{
    		Clear_Usb_reset_event();	
      		printf("USB RESET detection\n ");
	}

	if (Usb_ep_int_event() == 0x01)
	{
		
		Usb_sel_ep(0);                
		if (Usb_setup_packet_receive())
		{
			
			usb_do_enumeration();
		}
    	}
}

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

	Usb_sel_ep(0x00);
	UsbCtrlCmd.bmRequestType = UEPDATX;
	UsbCtrlCmd.bRequest =  UEPDATX;
	UsbCtrlCmd.wValue = UEPDATX;
	UsbCtrlCmd.wValue |= UEPDATX<<8;
	UsbCtrlCmd.wIndex  =UEPDATX;
	UsbCtrlCmd.wIndex |=UEPDATX<<8;
	UsbCtrlCmd.wLength = UEPDATX;
	UsbCtrlCmd.wLength |= UEPDATX<<8;

	 #if 1
	printf("UsbCtrlCmd.bmRequestType = %x\n",UsbCtrlCmd.bmRequestType);
	printf("UsbCtrlCmd.bRequest=%x\n",UsbCtrlCmd.bRequest);
	printf("UsbCtrlCmd.wValue=%x\n",UsbCtrlCmd.wValue);
	printf("UsbCtrlCmd.wIndex=%x\n",UsbCtrlCmd.wIndex);
	printf("UsbCtrlCmd.wLength=%x\n",UsbCtrlCmd.wLength);
	#endif
	
	if((UsbCtrlCmd.bmRequestType&0x20) == 0x00){
		
		usb_standard_request();
		
	}
	else if((UsbCtrlCmd.bmRequestType&0x20) == 0x20){ 
		usb_class_request();

	}
}



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

	switch (UsbCtrlCmd.bRequest)                 
  	{

		case USB_GET_DESCRIPTOR:
			
			usb_GetDescriptor();
		break;
			
		case USB_GET_CONFIGURATION:
			
			usb_get_configuration();
		break;

 		case USB_SET_ADDRESS:
			usb_set_address();
		break;

  		case USB_SET_CONFIGURATION:
			usb_set_configuration();
		break;

		case USB_CLEAR_FEATURE:
			usb_clear_feature();
		break;

		case USB_SET_FEATURE:
			usb_set_feature();
		break;

		case USB_GET_STATUS:
			usb_get_status();
		break;

		
		default:	  
		break;
  }
 
}

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


	switch (UsbCtrlCmd.bRequest)                 
  	{

		case HID_SET_IDLE:
			hid_set_idle();
			break;
		case USB_GET_MAX_LUN:
			printf("USB_GET_MAX_LUN\n");
			usb_get_max_lun();
			break;
		default:
		break;
	}


}
/**************************************************************************
 *                                                                                                                                 
 *  Function Name:usb_GetDescriptor                                                                               
 *                                                                                                                          
 *  Descriptions:                                                       
 *                                                                        
 *  Arguments:                                                           
 *                                                                       
 *  Returns: None                                                         
 *                                          
 **************************************************************************/
void   usb_GetDescriptor(void){
	
	UINT8 data_to_send;
	UINT8 des_type,i;
	UINT8 desc_ptr = 0; 
	UINT8 zeroflag = 0;
	UINT8 ret;
	Usb_set_DIR_bit();                           

	des_type = UsbCtrlCmd.wValue>>8;/*read  wValue msb*/
	switch (des_type)
	{
		case USB_DEVICE_DESC:
		{

		
			G_pucCtrlDataPointer = (UINT8 *)USB_DeviceDescriptor;
			data_to_send = sizeof(USB_DeviceDescriptor);
			break;
		}

		case USB_CONFIGURATION_DESC:
		{
			
			G_pucCtrlDataPointer = (UINT8 *)USB_ConfigurationDescriptor;
			data_to_send = sizeof(USB_ConfigurationDescriptor);
			break;
		}

    		case USB_STRING_DESC:
    		{

			
			des_type = UsbCtrlCmd.wValue & 0xff;
			switch (des_type)
			{
				case USB_LANG_ID:
				{

					G_pucCtrlDataPointer = (UINT8 *)USB_LanguageIdString;
					data_to_send = sizeof(USB_LanguageIdString); 	 
					break;
				}

				case USB_MAN_INDEX:
				{

					G_pucCtrlDataPointer = (UINT8 *)USB_ManufacturerString;
					data_to_send = sizeof(USB_ManufacturerString);		  
					break;
				}
				case USB_PROD_INDEX:
				{

					G_pucCtrlDataPointer = (UINT8 *)USB_ProductString;
					data_to_send = sizeof(USB_ProductString);		  
					break;
				}
				case USB_SN_INDEX:
				{
					G_pucCtrlDataPointer = (UINT8 *)USB_SerialNumberString;
					data_to_send = sizeof(USB_SerialNumberString);
					break;
				}
				default:
				{
					return;
				}
      			}
			break;
    		}
		default:
		{

⌨️ 快捷键说明

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