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

📄 sl811hst.c

📁 关于arm的一些基础的入门东西希望对初学者有好处
💻 C
📖 第 1 页 / 共 4 页
字号:

		for(i=1; i<=pHub->bNbrPort; i++)					// GetPortStatus(wHubStatus,wHubChange)
		   if( !GetPortStatus(uAddr,i,STATUS) ) 			//
				return FALSE;								//

		for(i=2;i<=pNumPort+1;i++)							// clear port present status
		{													// address #2..#5 only
			uHub.bPortPresent[i] = 0;						// clear status
			uHub.bPortNumber[i] = 0;						//
		}
		HUB_DEVICE = TRUE;									// Set Hub flag, as long as a hub is attached 
	}
	*****/														// directly to the HUB_DEVICE will be set
	Uart_Printf("EnumUsbDev Return\n");
	return TRUE;
}

//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************
int speed_detect() 
{	
	volatile int i =0;
	pNumPort    = 0;			// zero no. of downstream ports
	SLAVE_FOUND = FALSE;		// Clear USB device found flag
	FULL_SPEED  = TRUE;			// Assume full speed device
	//HUB_DEVICE  = FALSE;		// not HUB device
	DATA_STOP   = FALSE;		//
 
	//SL811MemTest();	 			// Memory test
	/////SL811Write(IntEna,0x63);	// USBA/B, Insert/Remove,USBRest/Resume
	////////SL811Write(cSOFcnt,0xAE);	// Set SOF high counter, no change D+/D-, host mode
	//////SL811Write(CtrlReg,0x48);	//Clear Sl811H mode and setup normal operation
	//////Delay(100);
	//////SL811Write(CtrlReg,0x00);	//Disable USB transfer operation and SOF
	//////Delay(100);
	//////SL811Write(IntEna,0x61);      	// USB-A, Insert/Remove, USB_Resume.
	//////SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
	//////Delay(100);							// Delay for HW stablize
	i = SL811Read(IntStatus);			//Read Interrupt Status	
	/*	
	if(i & 0x40)
	{										// test for USB reset
		SL811Write(IntStatus,0x40);			// Clear Interrupt enable status
		Uart_Printf("NO device or No Power\n");
		return 0;						// exit speed_detect()
	}
	
	i = SL811Read(IntStatus);*/
	
		
	/////if(!(i & 0x40))
	if(i&0x20)//device is inserted or removed
	{
		if((i & USB_DPLUS)==0)		// Checking full or low speed	
		{									// ** Low Speed is detected ** //
			Uart_Printf("Low Speed is detected, status=%x\n",i);
			//Set up Master and low Speed direct and SOF cnt high = 0x2e
			SL811Write(cSOFcnt,0xee);   	// Set up host and low speed direct and SOF cnt
			//SOF Coonuter Low = 0xe0; 1ms interval
			SL811Write(cDATASet,0xe0); 	// SOF Counter Low = 0xE0; 1ms interval
			//Setup 6Mhz and EOP enable
			SL811Write(CtrlReg,0x21);  	// Setup 6MHz and EOP enable         
			//uHub.bPortSpeed[1] = 1;		// low speed for Device #1
			FULL_SPEED = FALSE;		// low speed device flag
		}
		else	
		{	
			Uart_Printf("Full Speed is detected, status=%x\n",i);								// ** Full Speed is detected ** //
			SL811Write(cSOFcnt,0xae);   	// Set up Master & low speed direct and SOF cnt high=0x2e
			SL811Write(cDATASet,0xe0);  	// SOF Counter Low = 0xE0; 1ms interval
			SL811Write(CtrlReg,0x05);   	// Setup 48MHz and SOF enable
			//uHub.bPortSpeed[1] = 0;		// full speed for Device #1	
	////		SL811Write(CtrlReg,0x05);	
		}
	}
	else 
	{
		SL811Write(IntStatus,INT_CLEAR);
		Uart_Printf("NO device detected or No Power.\n");
		return 0;
	}
			
	SLAVE_FOUND = TRUE;			// Set USB device found flag
	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
	Delay(250);					// Hub required approx. 24.1mS
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt status
	return 0;    						// exit speed_detect();
}

//*****************************************************************************************
// Detect USB Device
//*****************************************************************************************
int slave_detect(void)
{
	//int retDataRW = FALSE;
	
	Uart_Printf("OK\n" ); 
	//-------------------------------------------------------------------------
	// Wait for SL811HS enumeration
	//-------------------------------------------------------------------------
	if(!SLAVE_ENUMERATED)					// only if slave is not configured
	{
		speed_detect();						// wait for an USB device to be inserted to 
		Delay(100);
		if(SLAVE_FOUND)						// the SL811HST host
		{
		//	printf("slave found\n");
	  		if(EnumUsbDev(1))				// enumerate USB device, assign USB address = #1
			{
			   	SLAVE_ENUMERATED = TRUE;	// Set slave USB device enumerated flag
				
				Uart_Printf("%s Speed Device Attached\n", FULL_SPEED?"Full":"Low");
				//Uart_Printf("Press ESC key to Exit...\n");
			}	
		}
	}

	//-------------------------------------------------------------------------
	// SL811HS enumerated, proceed accordingly
	//-------------------------------------------------------------------------
	else									
	{													
//		
		if(Slave_Detach())					// test for slave device detach 
			return 0;						// exit now.
														
	} // end of else

	return 0;
}

//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************
int Slave_Detach(void)
{
	if( (SL811Read(IntStatus)&INSERT_REMOVE) || (SL811Read(IntStatus)&USB_RESET) )
	{												// Test USB detached?
		SLAVE_ENUMERATED = FALSE;					// return to un-enumeration
		//uHub.bPortPresent[1] = 0;					// Device #1 not present
		
		SL811Write(IntStatus,INT_CLEAR); 			// clear interrupt status
			Uart_Printf("Device Detached\n");
		return TRUE;								// exit now !!!		
	}

	return FALSE;
}

int SL811MemTest(void)
{
	int errors =0, i;
	//BYTE temp;
		
	////outportb(0x0d, SL811H_ADDR);
	////temp=inportb(SL811H_DATA);
	////Uart_Printf("Status Register: %x,\n",temp);
		
	for(i = 0x10/*EP0Buf*/; i< 0xff; i++)	//addr = data
		SL811Write((BYTE)i,(BYTE)i);
			
	for(i = 0x10/*EP0Buf*/; i<0xff; i++)		//verify data
	{
		if((BYTE)i != SL811Read((BYTE)i))
			errors++;
		SL811Write((BYTE)i,(BYTE)~i);
		if((BYTE)~i != SL811Read((BYTE)i))
			errors++;
	}	
	Uart_Printf("Memory test done errors: 0x%x\n",errors);				
	/*// auto increment: addr = data 
	for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
		outportb(i,SL811H_DATA);
	// auto: addr = data 
	for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
	{
		if((BYTE)i != (BYTE)inportb(SL811H_DATA))
			errors++;
	}
	// clear all sl811h Memory
	for(i = EP0Buf, outportb(EP0Buf,SL811H_ADDR); i < cMemEnd; i++)
		outportb(0,SL811H_DATA);
	Uart_Printf("Memory test done: 0x%x\n",errors);*/
	return errors;		//Return number of error
}

//-------------------------------------------------------------------------
//Find Full Speed USB Device
//------------------------------------------------------------------------
/***
int FindUsbDev(short DevNum)
{
	pDevDesc   pDev;
	pCfgDesc  pConf;
	pIntfDesc  pINtf;
	int        i,j;
	BYTE       *p,*ep;
	sEPDesc    sEP;
	
	if (DevNum==1)
		USBReset();		//root hub
	uDev.wEPxCRC[0] = crc5(0);	//address=0, endpoint =0
	uDev.wPayLoad[0] = 8;
	Uart_Printf("Get Device Descriptor: .. \n");
	pDev = (pDevDesc)SCMD;
	if( !GetDesc(DEVICE<<8,0x40,(BYTE*)SCMD))
		return FALSE;
	Uart_Printf("len=%02x ep0Len=%d\n", pDev->bLength, pDev->bMaxPacketSize0);
	uDev.wPayLoad[0] = (pDev->bMaxPacketSize0 & 0xff)? pDev->bMaxPacketSize 0:8;
	Uart_Printf("Get Device Descriptor: .. ");
	uDev.wEPxCRC[0] = crc5((WORD)(DevNum+(0<<7)));  //Endpoint 0 | assign new addr
	
	if(!GetDesc(DEVICE<<8,0x09,(BYTE*)SCMD))
		return FALSE;
	Uart_Printf("Vendor:%04x Product: %04x\n",pDev->idVendor,pDev->idProduct);
	uDev.wVID = pDev->idVendor;
	uDev.wPID = pDev->idProduct;
	
	Uart_Printf("Get Configuration\n");
	if(!GetDesc(CONFIGURATION<<8,0xff,(BYTE*)SCMD)) return false;
	if(!GetDesc(CONFIGURATION<<8,0x12,(BYTE*)SCMD)) return false;
	if(!GetDesc(CONFIGURATION<<8,0x09,(BYTE*)SCMD)) return false;
	if(!GetDesc(CONFIGURATION<<8,0x20,(BYTE*)SCMD)) return false;
	
	pConf = (pConfDesc)SCMD;
	//printf("Len=%d,type:%02x\n",pConf->wLength, pConf->bType);
	//printf("Set Configuration\n");
	if(!Set_Configuration(DEVICE)) return FALSE;
	p = (BYTE*)SCMD;
	i = *p;
	pIntf = (pINtfDesc)& p[i];		//point to Interface Desc
	
	p = (BYTE*)& p[i+pIntf->bLength];	//point to EP Desc
	Uart_Printf("Number of Endpoint = %d\n", pIntf->bEndPoints);
	uDev.bNumofEPs = pIntf -> bEndPoints;
	if(uDev.bNumofEPS > cMaxEP)  return FALSE;
	
	ep = (BYTE*) & sEP;
	for(i=1; i<= pIntf->bEndPoints; i++)
	{
		uDev.wEPxCRC[i] = crc5((WORD)(DevNum + (i<<7))); 	//Endpoint CRC
		for(j=0; j<7; j++)
			ep[j] = *p++;
		if(sEP.bEPAdd & 0x80)
			Rd_EP = sEP.bEPAdd & 0xf;	//index of Read EP
		else 
			Wr_EP = sEP.bEPAdd;
		uDev.bEPAddr[i] = sEP.bAttr;
		uDev.wPayLoad[i] = sEP.wPayLoad;
		
		Uart_Printf("Length %x Type %x EndPoint Addr %02x Attr %02x wLength = %04x\n",
		 	sEP.bLength, sEP.bType, sEP.bEPAdd, sEP.bAttr, sEP.wPayLoad);
		 DToggle[i] = 0;
	}
	Uart_Printf("Wrinte Endpoint %d, Read Endpoint %d\n", Wr_EP, Rd_EP);
	return TRUE;
}			
	****/
	
/************大批量传输**************************************************************/	
	
BYTE  BulkSend(WORD len,BYTE *buffer)
{   
	BYTE currentlen;
	while(len>0)
	{
		if (len >uDev[1].wPayLoad[epbulkout] )
			currentlen=uDev[1].wPayLoad[epbulkout];
		else				
			currentlen=len;	
		if(!usbXfer( 1, epbulkout , PID_OUT, 0,uDev[1].wPayLoad[epbulkout] , currentlen, buffer))
			return FALSE;
		len-=currentlen;
		buffer=buffer+currentlen;
		//EZUSB_Delay(50);
	}
	return TRUE;	
}

/*************************************************************************************************/
BYTE BulkReceive( WORD len,BYTE *buffer)
{
	if(len)
	{
		if(!usbXfer( 1, epbulkin , PID_IN, 0,uDev[1].wPayLoad[epbulkin] , len, buffer))
			return FALSE;
	}
	return TRUE;
 }

⌨️ 快捷键说明

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