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

📄 host_811.c

📁 usb2.0驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
				//------------------------------------------------	
				if(!remainder && wLen)							// remainder==0 when last xferLen
				{												// was all completed or wLen!=0
                                   //short_delay(10);
					addr    = (dataX & 1) ? data1:data0; 		// select next address for data
					xferLen = (BYTE)(wLen>=wPayload) ? wPayload:wLen;	// get data length required
					SL811BufWrite(addr,buffer,xferLen); 	// data to transfer on USB
					buffer += xferLen;								
					SL811Write(EP0XferLen, xferLen); 			// select next xfer length
					SL811Write(EP0Address, addr);           	// data buffer addr 
					SL811Write(IntStatus,INT_CLEAR);			// is a LS is on Hub.
					SL811Write(EP0Control,cmd);					// Enable USB transfer and re-arm
				}				
				//------------------------------------------------
				// Copy last IN token data pkt from prev transfer
				// Check if there was data available during the
				// last data transfer
				//------------------------------------------------
		
									//------------------------------------------------
				// Terminate on short packets, i.e. remainder!=0
				// a short packet or empty data packet OR when 
				// requested data len have completed, i.e.wLen=0
				// For a LOWSPEED device, the 1st device descp,
				// wPayload is default to 64-byte, LS device will
				// only send back a max of 8-byte device descp,
				// and host detect this as a short packet, and 
				// terminate with OUT status stage
				//------------------------------------------------
				if(remainder || !wLen)
					break;
				// implement data toggle.added by ricky
				bData1 = uDev[usbaddr].bData1[endpoint];
			}
			
			// IN TOKEN
			else if(pid == PID_IN)
			{	
//				if(((bData1)&&(result&EP0_SEQUENCE))||((!(bData1))&&(!(result&EP0_SEQUENCE))))
//				{	
					uDev[usbaddr].bData1[endpoint] = (uDev[usbaddr].bData1[endpoint] ? 0 : 1); // DataToggle										// for IN token only
					wLen  -= (WORD)xferLen;							// update remainding wLen value
					cmd   ^= 0x40;   				// toggle DATA0/DATA1
					dataX++;	
				
				

					//------------------------------------------------	
					// If host requested for more data than the slave 
					// have, and if the slave's data len is a multiple
					// of its endpoint payload size/last xferLen. Do 
					// not overwrite data in previous buffer.
					//------------------------------------------------	
					if(remainder==xferLen)							// empty data detected
						bufLen = 0;									// do not overwriten previous data
					else											// reset bufLen to zero
						bufLen = xferLen;							// update previous buffer length
					
					//------------------------------------------------	
					// Arm for next data transfer when requested data 
					// length have not reach zero, i.e. wLen!=0, and
					// last xferlen of data was completed, i.e.
					// remainder is equal to zero, not a short pkt
					//------------------------------------------------	
					if(!remainder && wLen)							// remainder==0 when last xferLen
					{												// was all completed or wLen!=0
                                          //short_delay(5);
						addr    = (dataX & 1) ? data1:data0; 		// select next address for data
						xferLen = (BYTE)(wLen>=wPayload) ? wPayload:wLen;	// get data length required
							// always sync SOF when FS, regardless 
						SL811Write(EP0XferLen, xferLen); 			// select next xfer length
						SL811Write(EP0Address, addr);           	// data buffer addr 
						SL811Write(IntStatus,INT_CLEAR);			// is a LS is on Hub.
						SL811Write(EP0Control,cmd);					// Enable USB transfer and re-arm
					}				

					//------------------------------------------------
					// Copy last IN token data pkt from prev transfer
					// Check if there was data available during the
					// last data transfer
					//------------------------------------------------
					if(bufLen)										
					{	
						SL811BufRead(((dataX&1)?data0:data1), buffer, bufLen);
						buffer += bufLen;								
					}
					
				
					//------------------------------------------------
					// Terminate on short packets, i.e. remainder!=0
					// a short packet or empty data packet OR when 
					// requested data len have completed, i.e.wLen=0
					// For a LOWSPEED device, the 1st device descp,
					// wPayload is default to 64-byte, LS device will
					// only send back a max of 8-byte device descp,
					// and host detect this as a short packet, and 
					// terminate with OUT status stage
					//------------------------------------------------
					if(remainder || !wLen)
						break;
					// implement data toggle.added by ricky
					bData1 = uDev[usbaddr].bData1[endpoint];
 			}
		}
		
		//-------------------------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 
				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
			break;										// 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

	TIMEOUT_ERR=2;
	return FALSE;			// fail transmission
}

//*****************************************************************************************
// Control Endpoint 0's USB Data Xfer
// ep0Xfer, endpoint 0 data transfer
//*****************************************************************************************
int ep0Xfer(BYTE usbaddr, WORD payload, pSetupPKG setup, BYTE *pData)
{
	xdata BYTE	pid  = PID_IN;
	xdata WORD	wLen = setup->wLength;		// swap back for correct length
	xdata BYTE	ep0 = 0;								// always endpoint zero

	//----------------------------------------------------
	// SETUP token with 8-byte request on endpoint 0
	//----------------------------------------------------
	if (!usbXfer(usbaddr, ep0, PID_SETUP, 0, payload, 8, (BYTE*)setup)) 
	{
		TIMEOUT_ERR=3;
   		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, 0, payload, wLen, pData))
			{
				TIMEOUT_ERR=4;
				return FALSE;
			}
			payload = 0;
		}
		else											// device-to-host : OUT token
   		{							
			if(!usbXfer(usbaddr, ep0, PID_OUT, 0, payload, wLen, pData))
			{
				TIMEOUT_ERR=5;
				return FALSE;
			}
		}
	}

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

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

//*****************************************************************************************
// Set Device Address : 
//*****************************************************************************************
int SetAddress(WORD addr)
{
	return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
}
//*****************************************************************************************
// Set Device Configuration : 
//*****************************************************************************************
int Set_Configuration(BYTE usbaddr, WORD wVal)
{
	return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
int GetDesc(BYTE usbaddr, WORD wValue, 	WORD wIndex, WORD wLen, BYTE *desc)
{ 
	return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, wIndex, wLen, desc);
}
//****************************
//Get  endpoint status
//***************************
int GetEndpStatus(BYTE endpoint,BYTE *pdata)
{    	
    return VendorCmd(2,0x82, 0, 0, endpoint, 2, pdata);
}
//***************************************************************************************
//Bulk only Mass Storage Reset (class specific request)
//this request is used to reset the mass storage device and its associated interface
//edit by ricky
//***************************************************************************************
int MassStoReset(BYTE Interface)
{
	return VendorCmd(2,0x21,0xff,0,Interface,0,NULL);
}
//*****************************************************************************************
// USB Data Endpoint Read/Write
// wLen is in low byte first format
//*****************************************************************************************
int DataRW(BYTE usbaddr, BYTE epaddr, WORD wPayload, WORD wLen, BYTE *pData)
{
	xdata BYTE pid = PID_OUT;

	if(epaddr & 0x80)	// get direction of transfer
		pid = PID_IN;				

	if(usbXfer(usbaddr,epaddr&0x0F,pid,0,wPayload,wLen,pData))
		return TRUE;
	
	TIMEOUT_ERR=7;
	return FALSE;
}

//*****************************************************************************************
// 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
//*****************************************************************************************
int EnumUsbDev(BYTE usbaddr)
{  
	int i;											// always reset USB transfer address 
	xdata BYTE uAddr = 0;							// for enumeration to Address #0
	xdata BYTE epLen;
	xdata WORD strLang;

	//------------------------------------------------
	// Reset only Slave device attached directly
	//------------------------------------------------
	uDev[0].wPayLoad[0] = 64;						// default 64-byte payload of Endpoint 0, address #0
	if(usbaddr == 2)								// bus reset for the device attached to SL811HS only
		USBReset();									// that will always have the USB address = 0x01 (for a hub)
    	usb_Delay(25);

	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr 0
	// with default 64-byte payload
	//------------------------------------------------
	pDev =(pDevDesc)DBUF;							// ask for 64 bytes on Addr #0
	if (!GetDesc(uAddr,DEVICE,0,0x12,DBUF))			// and determine the wPayload size,edit by ricky,0x40 orignal is 18.
	{
		TIMEOUT_ERR=8;
		return FALSE;
	}
									// get correct wPayload of Endpoint 0
	uDev[usbaddr].wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address

	//------------------------------------------------
	// Set Slave USB Device Address
	//------------------------------------------------
	if (!SetAddress(usbaddr)) 						// set to specific USB address
	{
		TIMEOUT_ERR=9;
		return FALSE;
	}
	uAddr = usbaddr;								// transfer using this new address
	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr X
	//------------------------------------------------
	if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),DBUF)) 
	{
		TIMEOUT_ERR=10;	
		return FALSE;								// For this current device:
	}
	uDev[uAddr].wVID 	 = pDev->idVendor;			// save VID
	uDev[uAddr].wPID 	 = pDev->idProduct;			// save PID
	uDev[uAddr].iMfg 	 = pDev->iManufacturer;		// save Mfg Index
	uDev[uAddr].iPdt 	 = pDev->iProduct;			// save Product Index

	//------------------------------------------------
	// Get Slave USB Configuration Descriptors
	//------------------------------------------------
	pCfg = (pCfgDesc)DBUF;									
	if (!GetDesc(uAddr,CONFIGURATION,0,8,DBUF)) 
	{
		TIMEOUT_ERR=12;		
		return FALSE;	
	}
										
	if (!GetDesc(uAddr,CONFIGURATION,0,pCfg->wLength,DBUF)) 	
	{
		TIMEOUT_ERR=13;
		return FALSE;	
	}
	
	pIfc = (pIntfDesc)(DBUF + 9);					// point to Interface Descp
	uDev[uAddr].bClass 	= pIfc->iClass;			// update to class type
	uDev[uAddr].bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
	uDev[uAddr].bSub	=pIfc->iSub;			//this two para is added by ricky
	uDev[uAddr].bProto	=pIfc->iProto;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	if ((uDev[uAddr].bClass==8)&&(uDev[uAddr].bSub==6)&&(uDev[uAddr].bProto==0x50))
		// enumerating a mass storage class,SCSI Sub,bulk only proto
	{
			if (!Set_Configuration(uAddr,DEVICE))		// connected directly to SL811HS
			{
				TIMEOUT_ERR=14;
				return FALSE;

⌨️ 快捷键说明

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