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

📄 host_811.c

📁 arm(s3c440b)读写u盘源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
			}else if((pid == PID_OUT)&&(bEnd))
                                break;	
			else if(pid == PID_SETUP)								// do nothing for SETUP/OUT token
				break;
		}
		
		//-------------------------NAK----------------------------
		if (result & EP0_NAK)									// NAK Detected
		{														
			if(endpoint==0)										// on ep0 during enumeration of LS device
			{													// happen when slave is not fast enough,
				SL811Write(IntStatus,INT_CLEAR);				// clear interrupt status, need to
				SL811Write(EP0Control,cmd);						// re-arm and request for last cmd, IN token
                result = 0;                                     // respond to NAK status only
			}
			else												// normal data endpoint, exit now !!! , non-zero ep
				break;											// main loop control the interval polling
		}
		
		//-----------------------TIMEOUT--------------------------
		if (result & EP0_TIMEOUT)								// TIMEOUT Detected
		{														
			if(endpoint==0)										// happens when hub enumeration
			{
				if(++timeout >= TIMEOUT_RETRY)
				{	
				    timeout--;
					break;										// exit on the timeout detected	
				}
				SL811Write(IntStatus,INT_CLEAR);				// clear interrupt status, need to
				SL811Write(EP0Control,cmd);						// re-arm and request for last cmd again
			}
			else												
			{													// all other data endpoint, data transfer 
				USB_Control.TIMEOUT_ERR = TRUE;					// failed, set flag to terminate transfer
				break;											// happens when data transfer on a device
			}													// through the hub
		}

		//-----------------------STALL----------------------------
		if (result & EP0_STALL)  								// STALL detected
			return TRUE;										// for unsupported request.
																		
		//----------------------OVEFLOW---------------------------
		if (result & EP0_OVERFLOW)  							// OVERFLOW detected
			break;
		//-----------------------ERROR----------------------------
		if (result & EP0_ERROR)  								// ERROR detected
			break;
	}	// end of While(1)
   
	if (result & EP0_ACK) 										// on ACK transmission
		return TRUE;											// return OK

	return FALSE;												// fail transmission
}

//*****************************************************************************************
// Control Endpoint 0's USB Data Xfer
// ep0Xfer, endpoint 0 data transfer
//*****************************************************************************************
BYTE ep0Xfer(BYTE usbaddr, WORD payload, pSetupPKG setup, BYTE *pData)
{
	 BYTE	pid  = PID_IN;
	 WORD	wLen = setup->wLength;					// swap back for correct length
	 BYTE	ep0 = 0;											// always endpoint zero
	////////////////////////////////////////////////////////////////////////
	//----------------------------------------------------
	// SETUP token with 8-byte request on endpoint 0
	//----------------------------------------------------
	if (!usbXfer(usbaddr, ep0, PID_SETUP,payload, 8, (BYTE*)setup)) 
   		return FALSE;

	//----------------------------------------------------
	// IN or OUT data stage on endpoint 0	
	//----------------------------------------------------
   	if (wLen)													// if there are data for transfer
	{
		if (setup->bmRequest & 0x80)							// host-to-device : IN token
		{
			pid  = PID_OUT;	
			if(!usbXfer(usbaddr, ep0, PID_IN,payload, wLen, pData))
				return FALSE;
		//	payload = 0;
		}
		else													// device-to-host : OUT token
   		{							
			if(!usbXfer(usbaddr, ep0, PID_OUT,payload, wLen, pData))
				return FALSE;
		}
	}

	//----------------------------------------------------
	// Status stage IN or OUT zero-length data packet
	//----------------------------------------------------
	if(!usbXfer(usbaddr, ep0, pid,payload, 0, NULL))
		return FALSE;

	return TRUE;											
}

//*****************************************************************************************
// Control endpoint
//*****************************************************************************************
BYTE VendorCmd(BYTE usbaddr,BYTE bReq,BYTE bCmd,WORD wValue,WORD wIndex,WORD wLen,BYTE *pData)
{ 
    SetupPKG   setup;
    setup.bmRequest  = bReq;
    setup.bRequest   = bCmd;
    setup.wValue     = WordSwap(wValue);
    setup.wIndex     = 0;//WordSwap(wIndex);
    setup.wLength    = wLen;//WordSwap(wLen);
   	return ep0Xfer(usbaddr, uDev.wPayLoad[0], &setup, pData);
}

//*****************************************************************************************
// Set Device Address : 
//*****************************************************************************************
BYTE SetAddress(WORD addr)
{
	return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
}

//*****************************************************************************************
// Set Device Configuration : 
//*****************************************************************************************
BYTE SetConfiguration(BYTE usbaddr, WORD wVal)
{
	return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
}

//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
BYTE GetDesc(BYTE usbaddr, WORD wValue, 	WORD wIndex, WORD wLen, BYTE *desc)
{ 
	return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, wIndex, wLen, desc);
}
U8 Set_Interface(U8 usbaddr,WORD wVal)
{
	
	return VendorCmd(usbaddr, 01, SET_INTERFACE, 0, 0, 0, NULL);

}
//*****************************************************************************************
// USB Device Enumeration Process
// Support 1 confguration and interface #0 and alternate setting #0 only
// Support up to 1 control endpoint + 4 data endpoint only
//*****************************************************************************************
BYTE EnumUsbDev(BYTE usbaddr)
{  
	BYTE i,bBUF[256];											// always reset USB transfer address 
	BYTE uAddr = 0;									// for enumeration to Address #0
	BYTE epLen;
	pDevDesc  pDev;	
	pCfgDesc pCfg;
	pIntfDesc pIfc;
	pEPDesc pEnp;
	////////////////////////////////////////////////////////////////////////
	uDev.wPayLoad[0] = 64;							// default 64-byte payload of Endpoint 0, address #0
	if(usbaddr == 1)								// bus reset for the device attached to SL811HS only
		USBReset();									// that will always have the USB address = 0x01 (for a hub)
	Delay(2500);
	pDev =(pDevDesc)bBUF;							// ask for 64 bytes on Addr #0
	if (!GetDesc(uAddr,DEVICE,0,18,bBUF))			// and determine the wPayload size
		return FALSE;								// get correct wPayload of Endpoint 0
	uDev.wPayLoad[0]=pDev->bMaxPacketSize0;			// on current non-zero USB address
	if (!SetAddress(usbaddr)) 						// set to specific USB address
		return FALSE;								//
	uAddr = usbaddr;								// transfer using this new address
	if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),bBUF)) 	
		return FALSE;								// For this current device:
	uDev.wVID	 = pDev->idVendor;					// save VID
	uDev.wPID 	 = pDev->idProduct;					// save PID
	uDev.iMfg 	 = pDev->iManufacturer;				// save Mfg Index
	uDev.iPdt 	 = pDev->iProduct;					// save Product Index
	pCfg = (pCfgDesc)bBUF;									
	if (!GetDesc(uAddr,CONFIGURATION,0,8,bBUF)) 		
		return FALSE;										
	if (!GetDesc(uAddr,CONFIGURATION,0,64,bBUF)) 	
		return FALSE;		

	pIfc = (pIntfDesc)(bBUF + 9);					// point to Interface Descp
	uDev.bClass 	= pIfc->iClass;					// update to class type
	uDev.bNumOfEPs = (pIfc->bEndPoints<=MAX_EP) ? pIfc->bEndPoints : MAX_EP;
	/////////////////////////////////////////////
		if (!SetConfiguration(uAddr,DEVICE))		// connected directly to SL811HS
				return FALSE;
	////////////////////////////////////////////
		if(!Set_Interface(uAddr,0))
			return FALSE;
	if(uDev.bClass==8)
	{	
		USB_Control.bMassDevice=TRUE;
	}
	epLen = 0;
	for (i=1; i<=uDev.bNumOfEPs; i++)				// For each data endpoint
	{
		pEnp = (pEPDesc)(bBUF + 9 + 9 + epLen);	   	// point to Endpoint Descp(non-HID)
		uDev.bEPAddr[i]  	= pEnp->bEPAdd;			// Ep address and direction
		uDev.bAttr[i]		= pEnp->bAttr;			// Attribute of Endpoint
		uDev.wPayLoad[i] 	=MakeU16(*(bBUF+18+epLen+5),*(bBUF+18+epLen+4));//WordSwap(pEnp->wPayLoad) ;		// Payload of Endpoint
		uDev.bInterval[i] 	= pEnp->bInterval;		// Polling interval
	    uDev.bData1[0] = 0;			                // init data toggleu
		epLen += 7;
		if(uDev.bAttr[i]==0x2)
		{
		    if(uDev.bEPAddr[i]&0x80)
		    uDev.bEpin=uDev.bEPAddr[i];
		    else
		    uDev.bEpOut=uDev.bEPAddr[i];
		}
	}
	return TRUE;
}
//--------------------------------------------------------------------------
//SL811H variables initialization
//--------------------------------------------------------------------------
void sl811h_init()
{	
/* 	SL811Write(cSOFcnt,0xAE);
	SL811Write(CtrlReg,0x08);
	Delay(500);
	SL811Write(CtrlReg,0x00);
	SL811Write(IntEna,0x20);
	SL811Write(IntStatus,INT_CLEAR);
	SL811Write(IntEna,0x20);
	SL811Write(IntStatus,INT_CLEAR);
	SL811Write(cSOFcnt,0xae); 
//	DelayMs(10); 
	SL811Write(IntStatus,INT_CLEAR);*/
/**/
	SL811Write(cDATASet,0xe0);
	//SL811Write(cDATASet,0xb0);
	SL811Write(cSOFcnt,0xae);
	//SL811Write(cSOFcnt,0x84);
	
	SL811Write(CtrlReg,0x5);
			
	SL811Write(EP0Status,0x50);
	SL811Write(EP0Counter,0);
	SL811Write(EP0Control,0x01);
				
	SL811Write(IntEna,0x20);      		// USB-A, Insert/Remove, USB_Resume.
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status

	USB_Control.SLAVE_FOUND = FALSE;
	USB_Control.SLAVE_ENUMERATED = FALSE;
	USB_Control.SLAVE_ONLINE = FALSE;
	USB_Control.SLAVE_REMOVED=FALSE;
	USB_Control.DATA_STOP=FALSE;
	USB_Control.TIMEOUT_ERR=FALSE;
	USB_Control.bMassDevice = FALSE;	
}
BYTE check_usbdevice()
{
	U8 intr;
	
	intr=SL811Read(IntStatus);
	SL811Write(IntStatus,INT_CLEAR);
	SL811Write(IntStatus,INSERT_REMOVE);
	if(intr & 0x40)	// This bit is "1" -> Device not present 
		   {
		   if(USB_Control.SLAVE_ONLINE ==1)
		   	{
		    Led_Display(4,0);
			Led_Display(3,0);
			printf("\nMass storage device is removed.\n");
			prompt();
			USB_Control.UDiskOK = FALSE;
		   	USB_Control.SLAVE_ONLINE =0;
			}
		   }
	else	// This bit is "0" -> Device is present 
	{
		if(USB_Control.SLAVE_ONLINE == 0)
		   	{
			Delay(6000000);
			if(!EnumUsbDev(1))						//step 1: enumerate USB device, assign USB address = #1
				return FALSE;
			printf("\n Found USB DEVICE. Now begin to enuerate......\n");
			Delay(500000);
			if(PollDisk())					//step 2: emumerate U-Disk
					{
					USB_Control.UDiskOK = TRUE;
					printf("Mass storage device mounted successfully. It's ready to use\n");				
					if(USB_Control.bIsFat32)
						printf("File system on the disk is FAT32\n");
					else
						printf("File system on the disk is FAT16\n");
					printf("\n/************************************\n");
					printf("查看命令,请输入 help 或则 ? 后按回车\n");
					printf("/*************************************\n");
					printf("\n");
					prompt();
			   		}
				else
					{
				/*	Led_Display(3,0);
					USB_Control.UDiskOK = FALSE;*/
					printf("Enumerate device FAILD!\n");
					prompt();
					}	
					
		   	USB_Control.SLAVE_ONLINE =1;
			return 1;
			}	
	}
	
	return 1;
	
}

⌨️ 快捷键说明

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