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

📄 sl811.c

📁 可以实现44B0的底层所有通信
💻 C
📖 第 1 页 / 共 2 页
字号:
				if(!remainder && usbstack.wLen)							// remainder==0 when last xferLen
				{														// was all completed or wLen!=0
					addr    = (dataX & 1) ? data1:data0; 				// select next address for data
					xferLen = (BYTE)(usbstack.wLen>=usbstack.wPayload) ? usbstack.wPayload:usbstack.wLen;	// get data length required
					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), usbstack.buffer, bufLen);
					usbstack.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 || !usbstack.wLen)
					break;
			}// PID IN	
			else if((usbstack.pid == PID_OUT)&&(bEnd))
                break;
			else if(usbstack.pid == PID_SETUP)								// do nothing for SETUP/OUT token
				break;						
		}
		
		//-------------------------NAK----------------------------
		if (result & EP0_NAK)									// NAK Detected
		{														
			offset=0;
				
				SL811Write(IntStatus,INT_CLEAR);				// clear interrupt status, need to
				SL811Write(EP0Control+offset,cmd);						// re-arm and request for last cmd, IN token
		}
		//-----------------------TIMEOUT--------------------------
		if (result & EP0_TIMEOUT)								// TIMEOUT Detected
		{														
			if(usbstack.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 
				bTJHFlags.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
			//result=result;
			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
//*****************************************************************************************/
U8 ep0Xfer(void)
{
	
	usbstack.endpoint=0;
	//----------------------------------------------------
	// SETUP token with 8-byte request on endpoint 0
	//----------------------------------------------------
	usbstack.pid=PID_SETUP;
	usbstack.wLen=8;
	if (!usbXfer()) 
   		return FALSE;
	usbstack.pid  = PID_IN;
	//----------------------------------------------------
	// IN or OUT data stage on endpoint 0	
	//----------------------------------------------------
	usbstack.wLen=usbstack.setup.wLength;
   	if (usbstack.wLen)											// if there are data for transfer
	{
		if (usbstack.setup.bmRequest & 0x80)		// host-to-device : IN token
		{
			usbstack.pid  = PID_IN;	
			
			if(!usbXfer())
				return FALSE;
			//usbstack.wPayload = 0;
			usbstack.pid  = PID_OUT;
		}
		else						// device-to-host : OUT token
   		{							
			usbstack.pid  = PID_OUT;
				
			if(!usbXfer())
				return FALSE;
			usbstack.pid  = PID_IN;
		}
	}
	//----------------------------------------------------
	// Status stage IN or OUT zero-length data packet
	//----------------------------------------------------
	usbstack.wLen=0;
	if(!usbXfer())
		return FALSE;

	return TRUE;											
					
}

U8 epBulkSend(U8 *pBuffer,U16 len)
{
	//usbstack.usbaddr=0x1;
	usbstack.endpoint=usbstack.epbulkout;
	usbstack.pid=PID_OUT;
	usbstack.wPayload=64;
	usbstack.wLen=len;
	usbstack.buffer=pBuffer;
	
	if(usbstack.wLen)
		return usbXfer();
	
	return TRUE;	
}

U8 epBulkRcv(U8 *pBuffer,U16 len)
{
	//usbstack.usbaddr=0x1;
	usbstack.endpoint=usbstack.epbulkin;
	usbstack.pid=PID_IN;
	usbstack.wPayload=64;
	usbstack.wLen=len;
	usbstack.buffer=pBuffer;
	if(usbstack.wLen)
		while(!usbXfer());
	return TRUE;
}


//*****************************************************************************************/
// Set Device Address : 
//*****************************************************************************************/
U8 SetAddress(U8 addr)
{
	usbstack.usbaddr=0;
	usbstack.setup.bmRequest=0;
	usbstack.setup.bRequest=SET_ADDRESS;
	usbstack.setup.wValue=WordSwap(addr);
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=0;
	return ep0Xfer();

}

//*****************************************************************************************/
// Set Device Configuration : 
//*****************************************************************************************/
U8 Set_Configuration(void)
{
	usbstack.setup.bmRequest=0;
	usbstack.setup.bRequest=SET_CONFIG;
	usbstack.setup.wValue=WordSwap(DEVICE);
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=0;
	usbstack.buffer=NULL;
	return ep0Xfer();

}

//*****************************************************************************************/
// Set Device Interface : 
//*****************************************************************************************/
U8 Set_Interface(void)
{
	usbstack.setup.bmRequest=1;
	usbstack.setup.bRequest=SET_INTERFACE;
	usbstack.setup.wValue=0;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=0;
	usbstack.buffer=NULL;
	return ep0Xfer();

}
//*****************************************************************************************/
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************/

U8 GetDesc(void)
{ 
	
	usbstack.setup.bmRequest=0x80;
	usbstack.setup.bRequest=GET_DESCRIPTOR;
	usbstack.wPayload=uDev.wPayLoad[0];
	return ep0Xfer();
}
//*****************************************************************************************/
// 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
//*****************************************************************************************/

U8 EnumUsbDev(U8 usbaddr)
{  
	U8 i,epLen;							// always reset USB transfer address 
	U8 uAddr = 0;							// for enumeration to Address #0
	U8 BUF[512];
	
	pDevDesc  pDev;	
	pCfgDesc pCfg;
	pIntfDesc pIfc;
	pEPDesc pEnp;
	
	//------------------------------------------------
	// Reset only Slave device attached directly
	//------------------------------------------------
	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)
    	
    	Delay1(2500);
	
	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr 0
	// with default 64-byte payload
	//------------------------------------------------
	pDev =(pDevDesc)BUF;					// ask for 64 bytes on Addr #0
	
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=18;
	usbstack.buffer=BUF;
	
	if (!GetDesc())			// and determine the wPayload size
		return FALSE;								// get correct wPayload of Endpoint 0
	uDev.wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address

	//------------------------------------------------
	// Set Slave USB Device Address
	//------------------------------------------------
	if (!SetAddress(usbaddr)) 						// set to specific USB address
		return FALSE;						//
	uAddr = usbaddr;							// transfer using this new address
	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr X
	//------------------------------------------------
	pDev =(pDevDesc)BUF;
	usbstack.usbaddr=uAddr;
	
	usbstack.setup.wLength=pDev->bLength;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	usbstack.buffer=BUF;
	
	if (!GetDesc()) 	
		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

	//------------------------------------------------
	// Get String Descriptors
	//------------------------------------------------
	//------------------------------------------------
	// Get Slave USB Configuration Descriptors
	//------------------------------------------------
	
	pCfg = (pCfgDesc)BUF;	
	
	//usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=CONFIGURATION;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=64;
	usbstack.buffer=BUF;	
	if (!GetDesc()) 		
		return FALSE;	
	
	pIfc = (pIntfDesc)(BUF + 9);			// point to Interface Descp
	uDev.bClass 	= pIfc->iClass;			// update to class type
	uDev.bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
	
	if(uDev.bClass==8) //mass storage device
		bTJHFlags.bMassDevice=TRUE;
	else
		return FALSE;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	
	//if (uDev[usbaddr].bClass!=HUBCLASS)	// enumerating a FS/LS non-hub device
	if (!Set_Configuration())		// connected directly to SL811HS
		return FALSE;
	
	//usbstack.usbaddr=uAddr;
	//usbstack.setup.wValue=0;
	if (!Set_Interface())		
		return FALSE;
	//------------------------------------------------
	// For each slave endpoints, get its attributes
	// Excluding endpoint0, only data endpoints
	//------------------------------------------------
	
	epLen = 0;
	for (i=1; i<=uDev.bNumOfEPs; i++)				// For each data endpoint
	{
		pEnp = (pEPDesc)(BUF + 18+ 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.wPayLoad[i] 	= MakeU16(*(BUF+18+epLen+5),*(BUF+18+epLen+4));
		uDev.bInterval[i] = pEnp->bInterval;		// Polling interval
	    	uDev.bData1[i] = 0;			            // init data toggle
		epLen += 7;
		//////////////////////////////
		bTJHFlags.bData1=0;
		if(uDev.bAttr[i]==0x2)
		{
		    if(uDev.bEPAddr[i]&0x80)
		    	usbstack.epbulkin=uDev.bEPAddr[i];
		    else
		    	usbstack.epbulkout=uDev.bEPAddr[i];
		}
		//////////////////////////////
	}
	
	return TRUE;
}

⌨️ 快捷键说明

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