www.pudn.com > UDISK.rar > HOST_811.C


////////////////////////////////////////////////////////////////////////////// 
//author:dragon;www.8dragon.com 
//2004年完成于桃龙源 
// 
/////////////////////////////////////////////////////////////////////////// 
#include "AT89X52.H" 
#include "host_811.h" 
#include "fw.h" 
xdata BYTE SL811H_ADDR	  _at_	0x8000;		//A0=0 
xdata BYTE SL811H_DATA  _at_	0x8100;		//A0=1 
////////////////////////////////////////////////////////////////////////// 
BYTE  bBUF[256];			// at 0x2000 for general descriptors data 
USBDEV  idata	uDev;		// Multiple USB devices attributes, Max 5 devices 
////////////////////////////////////////////////////////////////////////// 
Control bdata USB_Control;  //全局命令控制变量 
BYTE  bData1;				//乒乓操作标志 
BYTE  d1,d2; 
//***************************************************************************************** 
// Byte Read from SL811H 
// a = register address 
// return = data in register 
//***************************************************************************************** 
BYTE SL811Read(BYTE idata a) 
{   
	SL811H_ADDR = a;			 
	return (SL811H_DATA); 
} 
//***************************************************************************************** 
// Byte Write to SL811H 
// a = register address 
// d = data to be written to this register address 
//***************************************************************************************** 
void SL811Write(BYTE idata a, BYTE idata d) 
{   
	SL811H_ADDR = a;	 
	SL811H_DATA = d; 
} 
//***************************************************************************************** 
// Buffer Read from SL811H 
// addr = buffer start address 
// s    = return buffer address where data are to be save/read 
// c	= buffer data length 
//***************************************************************************************** 
void SL811BufRead(BYTE addr, BYTE *s, BYTE c) 
{	 
	BYTE idata i; 
	//////////////////////////////////////////////////////////////////////// 
	i=c; 
	SL811H_ADDR = addr;	 
   	while (i--)  
		*s++ = SL811H_DATA; 
} 
 
//***************************************************************************************** 
// Buffer Write  to SL811H 
// addr = buffer start address 
// s    = buffer address where data are to be written 
// c	= buffer data length 
//***************************************************************************************** 
void SL811BufWrite(BYTE addr, BYTE *s, BYTE c) 
{	 
	SL811H_ADDR = addr;	 
   	while (c--)  
		SL811H_DATA = *s++; 
} 
void DelayMs(BYTE nFactor) 
{ 
	BYTE i; 
	BYTE j; 
	//////////////////////////////////////////////////////////////////////// 
	for(i=0; i>8)); 
} 
DWORD DWordSwap(DWORD dData) 
{ 
    dData = (dData&0xff)<<24|(dData&0xff00)<<8|(dData&0xff000000)>>24|(dData&0xff0000)>>8; 
	return dData; 
} 
//***************************************************************************************** 
// UsbReset during enumeration of device attached directly to SL811HS 
//***************************************************************************************** 
void USBReset() 
{ 
	BYTE temp; 
	BYTE i;	 
	//////////////////////////////////////////////////////////////////////// 
/*	SL811Write(cSOFcnt,0xae); 
	SL811Write(CtrlReg,0x48); 
	DelayMs(10); 
	SL811Write(CtrlReg,0x00); 
	DelayMs(10);*/ 
	i=SL811Read(IntStatus); 
	if(i&0x80)	 
	{ 	 
 		SL811Write(cDATASet,0xe0); 
		SL811Write(cSOFcnt,0xae); 
		SL811Write(CtrlReg,0x05); 
	}	 
	SL811Write(EP0Status,0x50); 
	SL811Write(EP0Counter,0x00); 
    SL811Write(EP0Control,0x01); 
    DelayMs(150);   
    temp=SL811Read(CtrlReg);    
 	SL811Write(CtrlReg,temp|0x08); 
	DelayMs(15);		 
	SL811Write(CtrlReg,temp | 0x18);  
	DelayMs(15);//Delay(200);	 
	SL811Write(CtrlReg,temp | 0x08);  
	DelayMs(15);//Delay(200); 
    SL811Write(CtrlReg,temp); 
    DelayMs(150); 
	//DelayMs(400);//Delay(2000);     
} 
 
//***************************************************************************************** 
// usbXfer: 
// successful transfer = return TRUE 
// fail transfer = return FALSE 
//***************************************************************************************** 
BYTE usbXfer(BYTE usbaddr, BYTE endpoint, BYTE pid,WORD wPayload, WORD wLen, BYTE *buffer) 
{   
	BYTE 	cmd, result, timeout, intr; 
    BYTE	xferLen, bufLen, data0, data1, dataX, addr,remainder;	 
    //////////////////////////////////////////////////////////////////////// 
	//------------------------------------------------ 
	// Default setting for usb trasnfer 
	//------------------------------------------------ 
	bufLen = dataX = timeout = 0;					//reset all 
	data0 = EP0_Buf;								// DATA0 buffer address 
	data1 = data0 + (BYTE)wPayload;					// DATA1 buffer address 
	USB_Control.DATA_STOP =	USB_Control.TIMEOUT_ERR = FALSE;				// set default conditions 
	//------------------------------------------------ 
	// Define data transfer payload 
	//------------------------------------------------ 
	if (wLen >= wPayload)  							// select proper data payload 
		xferLen = wPayload;							// limit to wPayload size  
	else											// else take < payload len 
		xferLen = wLen;								//	 
 
	//------------------------------------------------ 
	// For IN token 
	//------------------------------------------------ 
	if (pid==PID_IN)								// for current IN tokens 
	{ 
		cmd=sDATA0_RD; 
	} 
	//------------------------------------------------ 
	// For OUT token 
	//------------------------------------------------ 
	else if(pid==PID_OUT)							// for OUT tokens 
	{  	 
		if(xferLen)									// only when there are	 
			SL811BufWrite(data0,buffer,xferLen); 	// data to transfer on USB 
		cmd=sDATA0_WR; 
		bData1 = uDev.bData1[endpoint]; 
        uDev.bData1[endpoint] = (uDev.bData1[endpoint] ? 0 : 1); // DataToggle 
		if(bData1) 
			cmd |= 0x40;                             // Set Data1 bit in command 
	} 
	//------------------------------------------------ 
	// For SETUP/OUT token 
	//------------------------------------------------ 
	else											// for current SETUP/OUT tokens 
	{  	 
		if(xferLen)									// only when there are	 
			SL811BufWrite(data0,buffer,xferLen); 	// data to transfer on USB 
		cmd = sDATA0_WR; 
		 
	}	 
	//------------------------------------------------ 
	// For EP0's IN/OUT token data, start with DATA1 
	// Control Endpoint0's status stage. 
	// For data endpoint, IN/OUT data, start ???? 
	//------------------------------------------------ 
	if (endpoint == 0 && pid != PID_SETUP) 			// for Ep0's IN/OUT token 
		cmd |= 0x40; 								// always set DATA1 
		 
	//------------------------------------------------ 
	// Arming of USB data transfer for the first pkt 
	//------------------------------------------------ 
	SL811Write(EP0Status,((endpoint&0x0F)|pid));	// PID + EP address 
	SL811Write(EP0Counter,usbaddr);					// USB address 
	SL811Write(EP0Address,data0);					// buffer address, start with "data0" 
	SL811Write(EP0XferLen,xferLen);					// data transfer length 
	SL811Write(IntStatus,INT_CLEAR); 				// clear interrupt status 
	SL811Write(EP0Control,cmd);						// Enable ARM and USB transfer start here 
	//------------------------------------------------ 
	// Main loop for completing a wLen data trasnfer 
	//---------------------- -------------------------- 
	while(TRUE) 
	{    
		//---------------Wait for done interrupt------------------ 
		while(TRUE)												// always ensure requested device is 
		{														// inserted at all time, then you will 
			intr = SL811Read(IntStatus);						// wait for interrupt to be done, and  
			if((intr & USB_RESET) || (intr & INSERT_REMOVE))	// proceed to parse result from slave  
			{													// device. 
				USB_Control.DATA_STOP = TRUE;								// if dhevice is removed, set DATA_STOP 
				return FALSE;									// flag true, so that main loop will  
			}													// know tis condition and exit gracefully 
			if(intr & USB_A_DONE)								 
				break;											// interrupt done !!! 
		} 
 
		SL811Write(IntStatus,INT_CLEAR); 						// clear interrupt status 
		result 	  = SL811Read(EP0Status);						// read EP0status register 
		remainder = SL811Read(EP0Counter);						// remainder value in last pkt xfer 
 
		//-------------------------ACK---------------------------- 
		if (result & EP0_ACK)									// Transmission ACK 
		{	 
 
			// SETUP TOKEN 
			if(pid == PID_SETUP)								// do nothing for SETUP/OUT token  
				break;											// exit while(1) immediately 
 
			// OUT TOKEN				 
			else if(pid == PID_OUT) 
				break; 
 
			// IN TOKEN 
			else if(pid == PID_IN) 
			{													// for IN token only 
				wLen  -= (WORD)xferLen;							// update remainding wLen value 
				cmd   ^= 0x40;    								// toggle DATA0/DATA1 
				dataX++;										// point to next 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 
					addr    = (dataX & 1) ? data1:data0; 		// select next address for data 
					xferLen = (BYTE)(wLen>=wPayload) ? wPayload:wLen;	// get data length required 
				//	if (FULL_SPEED)								// sync with SOF transfer 
						cmd |= 0x20;							// 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; 
			}							 
		} 
		 
		//-------------------------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 = WordSwap(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     = wValue; 
    setup.wIndex     = wIndex; 
    setup.wLength    = 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); 
} 
//***************************************************************************************** 
// 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;											// 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) 
	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,WordSwap(pCfg->wLength),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(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] 	= pEnp->wPayLoad;		// Payload of Endpoint 
		uDev.bInterval[i] 	= pEnp->bInterval;		// Polling interval 
	    uDev.bData1[i] = 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(void) 
{	 
	SL811Write(IntEna,0x20); 
	SL811Write(IntStatus,INT_CLEAR); 
	SL811Write(cSOFcnt,0xae);  
	DelayMs(10);  
	SL811Write(IntStatus,INT_CLEAR); 
	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;	 
} 
//************************************************************************ 
//author:dragon 
//web:8dragon.com 
//2004.2.5完成于桃龙源 
//*************************************************************************