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

📄 sl811.c

📁 基于Embest开发环境
💻 C
📖 第 1 页 / 共 2 页
字号:
			return FALSE;
		len-=usbstack.wLen;
		usbstack.buffer=usbstack.buffer+usbstack.wLen;
		//DelayUs(10);
	}
	return TRUE;	
}

unsigned char epBulkRcv(unsigned char *pBuffer,unsigned int len)
{
	usbstack.usbaddr=0x1;
	usbstack.endpoint=usbstack.epbulkin;
	usbstack.pid=PID_IN;
	usbstack.wPayload=64;
	usbstack.wLen=len;
	usbstack.buffer=pBuffer;
	if(usbstack.wLen)
	{
		if(!usbXfer())
			return FALSE;
	}
	return TRUE;
}
//*****************************************************************************************
// Control endpoint
//*****************************************************************************************
//void VendorCmd(void)
//{ 
//      ep0Xfer();
//}

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

}

//*****************************************************************************************
// Set Device Configuration : 
//*****************************************************************************************
unsigned char Set_Configuration(void)
{
	//usbstack.usbaddr=usbaddr;
	//usbstack.
	usbstack.setup.bmRequest=0;
	usbstack.setup.bRequest=SET_CONFIG;
	//usbstack.setup.wValue=wVal;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=0;
	usbstack.buffer=NULL;
	return ep0Xfer();

}

//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
unsigned char GetDesc(void)
{ 
	
	usbstack.setup.bmRequest=0x80;
	usbstack.setup.bRequest=GET_DESCRIPTOR;
//	usbstack.setup.wValue=0x0100;//WordSwap(usbstack.setup.wValue);
	
	usbstack.wPayload=uDev.wPayLoad[0];
	//usbstack.buffer=&usbstack.setup;
	return ep0Xfer();
}

//*****************************************************************************************
// USB Data Endpoint Read/Write
// wLen is in low byte first format
//*****************************************************************************************
/*
unsigned char DataRW(BYTE epaddr)
{
	//xdata BYTE pid = PID_OUT;

	usbstack.pid=PID_OUT;
	usbstack.endpoint=epaddr&0x0F;
	
	if(epaddr & 0x80)	// get direction of transfer
		usbstack.pid = PID_IN;				
	
	if(usbXfer())
		return TRUE;

	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
//*****************************************************************************************
unsigned char EnumUsbDev(BYTE usbaddr)
{  
	unsigned char i;											// always reset USB transfer address 
	unsigned char uAddr = 0;							// for enumeration to Address #0
	unsigned char epLen;
	//unsigned short strLang;
	
	pDevDesc  pDev;	
	pCfgDesc pCfg;
	pIntfDesc pIfc;
	pEPDesc pEnp;
	
	Uart_Printf("Enumerate USB Device...");
	//------------------------------------------------
	// 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)
    	
    	DelayMs(25);
	

	pDev =(pDevDesc)DBUF;					// ask for 64 bytes on Addr #0
	
	usbstack.usbaddr = uAddr;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=18;
	//usbstack.setup.wLength=sbstack.setup.wLength);
	usbstack.buffer=DBUF;
	
	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)DBUF;
	usbstack.usbaddr=uAddr;
	
	usbstack.setup.wLength=pDev->bLength;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	
	//usbstack.setup.wLength=0x12;//(unsigned short)DBUF[0];//pDev->bLength;
	usbstack.buffer=DBUF;
	
	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 Slave USB Configuration Descriptors
	//------------------------------------------------
	
	pCfg = (pCfgDesc)DBUF;	
	
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=CONFIGURATION;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=64;
	usbstack.buffer=DBUF;	
	if (!GetDesc()) 		
		return FALSE;	


	
	pIfc = (pIntfDesc)(DBUF + 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
		bXXGFlags.bits.bMassDevice=TRUE;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
	//if (uDev[usbaddr].bClass!=HUBCLASS)				// enumerating a FS/LS non-hub device
		if (!Set_Configuration())		// connected directly to SL811HS
				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)(DBUF + 9 + 9 + epLen);	   			// point to Endpoint Descp(non-HID)
		//if(pIfc->iClass == HIDCLASS)	
		//	pEnp = (pEPDesc)(DBUF + 9 + 9 + 9 + epLen);		// update pointer to Endpoint(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 去掉WORDSWAP()
		uDev.bInterval[i] 	= pEnp->bInterval;		// Polling interval
	    	uDev.bData1[i] = 0;			            // init data toggle
		epLen += 7;
		
		if(i == 2)
			uDev.wPayLoad[i] 	= 0x40;
		else
			uDev.wPayLoad[i] 	= pEnp->wPayLoad;
		//////////////////////////////
		if(uDev.bAttr[i]==0x2)
		{
		    if(uDev.bEPAddr[i]&0x80)
		    	usbstack.epbulkin=uDev.bEPAddr[i];
		    else
		    	usbstack.epbulkout=uDev.bEPAddr[i];
		}
		//////////////////////////////
	}
	
	return TRUE;
}


//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************


///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
{	
	//int i;

	//for(i=0;i<MAX_DEV;i++)
	//{
	//	uHub.bPortPresent[i] = 0;
	//	uHub.bPortNumber[i] = 0;
	//}

	//SL811H_DATA = 0x00;
	//SL811H_ADDR = 0x00;
	//pNumPort	= 0x00;			

	//bXXGFlags.bits.FULL_SPEED = TRUE;	
	//HUB_DEVICE = FALSE;
	bXXGFlags.bits.SLAVE_ONLINE = FALSE;
	bXXGFlags.bits.SLAVE_FOUND = FALSE;
	bXXGFlags.bits.SLAVE_REMOVED=FALSE;
	
	bXXGFlags.bits.SLAVE_ENUMERATED = FALSE;
	bXXGFlags.bits.SLAVE_IS_ATTACHED = FALSE;
	


	
	SL811Write(cDATASet,0xe0);
	SL811Write(cSOFcnt,0xae);
	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
}

void check_usb_device(void)
{
	unsigned char intr;
	//SL811Write(IntStatus,INSERT_REMOVE);
	intr=SL811Read(IntStatus);
	if(intr & USB_RESET)
		   {
		   		//bXXGFlags.bits.SLAVE_IS_ATTACHED = FALSE;	// Set USB device found flag
		   if(bXXGFlags.bits.SLAVE_ONLINE ==TRUE)
		   	{bXXGFlags.bits.SLAVE_REMOVED=TRUE;
		   	bXXGFlags.bits.SLAVE_ONLINE =FALSE;}
		   }
	else	{
		   		//bXXGFlags.bits.SLAVE_IS_ATTACHED = TRUE;
		   if(bXXGFlags.bits.SLAVE_ONLINE == FALSE)
		   	{bXXGFlags.bits.SLAVE_FOUND=TRUE;
		   	bXXGFlags.bits.SLAVE_ONLINE =TRUE;}
		   }
	//bXXGFlags.bits.SLAVE_FOUND;
	
	//bXXGFlags.bits.SLAVE_REMOVED=0;
	SL811Write(IntStatus,INT_CLEAR);        //清除所有中断标志
   	SL811Write(IntStatus,INSERT_REMOVE);    //清除移开USB中断
		 
}


WORD WordSwap(WORD input)
{
	return (((input&0x00FF)<<8)|((input&0xFF00)>>8));
	//return input;
}

/*void DelayMs(WORD ms_time)		//~~从arm程序中copy过来的
{
	LWORD i;
	for( i = 0 ; i < 1000*ms_time ; i++ )
		;
}*/

INT32 SwapINT32(INT32 dData)
{
    dData = (dData&0xff)<<24|(dData&0xff00)<<8|(dData&0xff000000)>>24|(dData&0xff0000)>>8;
	return dData;
}

INT16 SwapINT16(INT32 dData)
{
    //dData = (dData&0xff00)>>8|(dData&0x00ff)<<8;
	return dData;
}

void ComSendByte(unsigned char c)
{
	//SBUF=c;
	//while(!TI);
	//TI=0;
}


int speed_detect() 
{
	bXXGFlags.bits.SLAVE_FOUND	= FALSE;				// Clear USB device found flag
	bXXGFlags.bits.FULL_SPEED  = TRUE;					// Assume full speed device
	bXXGFlags.bits.DATA_STOP	= FALSE;				//
 
	SL811Write(cSOFcnt,0xAE);      		// Set SOF high counter, no change D+/D-, host mode
	SL811Write(CtrlReg,0x08);      		// Reset USB engine, full-speed setup, suspend disable
	DelayMs(10);					// Delay for HW stablize
	SL811Write(CtrlReg,0x00);      		// Set to normal operation
	SL811Write(IntEna,0x61);      		// USB-A, Insert/Remove, USB_Resume.
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
	DelayMs(10);					// Delay for HW stablize

	if(SL811Read(IntStatus)&USB_RESET)
	{									// test for USB reset
		SL811Write(IntStatus,INT_CLEAR);// Clear Interrupt enable status
		DelayMs(30);				// Blink LED - waiting for slave USB plug-in
		return 0;						// exit speed_detect()
	}

	if((SL811Read(IntStatus)&USB_DPLUS)==0)	// Checking full or low speed	
	{									// ** Low Speed is detected ** //
		SL811Write(cSOFcnt,0xEE);   	// Set up host and low speed direct and SOF cnt
		SL811Write(cDATASet,0xE0); 		// SOF Counter Low = 0xE0; 1ms interval
		SL811Write(CtrlReg,0x21);  		// Setup 6MHz and EOP enable         
		bXXGFlags.bits.FULL_SPEED = FALSE;				// low speed device flag
	}
	else	
	{									// ** Full Speed is detected ** //
		SL811Write(cSOFcnt,0xAE);   	// Set up host & full speed direct and SOF cnt
		SL811Write(cDATASet,0xE0);  	// SOF Counter Low = 0xE0; 1ms interval
		SL811Write(CtrlReg,0x05);   	// Setup 48MHz and SOF enable
	}

	bXXGFlags.bits.SLAVE_FOUND = TRUE;					// Set USB device found flag
	bXXGFlags.bits.SLAVE_ENUMERATED = FALSE;			// no slave device enumeration

	SL811Write(EP0Status,0x50);   		// Setup SOF Token, EP0
	SL811Write(EP0Counter,0x00);		// reset to zero count
	SL811Write(EP0Control,0x01);   		// start generate SOF or EOP

	DelayMs(25);					// Hub required approx. 24.1mS
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt status
	return 0;    						// exit speed_detect();
}

⌨️ 快捷键说明

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