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

📄 sl811.c

📁 本程序是基于单片机C8051F020上使用SL811 USB 总线接口实现检测U 盘的功能。插上U 盘
💻 C
📖 第 1 页 / 共 3 页
字号:
		//////////////////////////////
		if(uDev.bAttr[i]==0x2)
		{
		    if(uDev.bEPAddr[i]&0x80)
		    	usbstack.epbulkin=uDev.bEPAddr[i];
		    else
		    	usbstack.epbulkout=uDev.bEPAddr[i];
		}
		//////////////////////////////
	}
	
	return TRUE;
}

//*****************************************************************************************
// Full-speed and low-speed detect - Device atttached directly to SL811HS
//*****************************************************************************************

void speed_detect(void) 
{
	
 
	SL811Write(cSOFcnt,0xAE);      		// Set SOF high counter, no change D+/D-, host mode
	SL811Write(CtrlReg,0x08);      		// Reset USB engine, full-speed setup, suspend disable
	Delay_ms(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
	Delay_ms(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
		//OUTB ^= ACTIVE_BLINK;			// Blink Active LED
		//OUTA |= PORTX_LED;				// clear debug LEDs
		return ;						// 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         
		//uHub.bPortSpeed[1] = 1;			// low speed for Device #1
	//	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
		//uHub.bPortSpeed[1] = 0;			// full speed for Device #1
	}

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

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

//*****************************************************************************************
// Detect USB Device
//*****************************************************************************************
/*
void slave_detect(void)
{
	//int retDataRW = FALSE;
	
	//-------------------------------------------------------------------------
	// Wait for EZUSB enumeration
	//-------------------------------------------------------------------------
	//if(!CONFIG_DONE)			// start SL811H after EZ-USB is configured
	//	return;

	//-------------------------------------------------------------------------
	// Wait for SL811HS enumeration
	//-------------------------------------------------------------------------
	if(!bXXGFlags.bits.SLAVE_ENUMERATED)					// only if slave is not configured
	{
		speed_detect();						// wait for an USB device to be inserted to 
		if(bXXGFlags.bits.SLAVE_FOUND)						// the SL811HST host
		{
	  		if(EnumUsbDev(1))				// enumerate USB device, assign USB address = #1
			{
			   	bXXGFlags.bits.SLAVE_ENUMERATED = TRUE;	// Set slave USB device enumerated flag
				//uHub.bPortPresent[1] = 1;	// set device addr #1 present
				Set_ezDEV(1);				// inform master of new attach/detach
			}	
		}
	}

	//-------------------------------------------------------------------------
	// SL811HS enumerated, proceed accordingly
	//-------------------------------------------------------------------------
	else									
	{													
		//OUTB &= ~ACTIVE_BLINK;				// Turn on Active LED, indicate successful slave enum
		if(Slave_Detach())					// test for slave device detach ???
			return;						// exit now.
		//----------------------------------------------
		// HUB DEVICE Polling (Addr #1, EndPt #1)		
		//----------------------------------------------
		// Polling of Hub deivce Endpoint #1 for any Port Attachement
		// for onboard HUB device, after enumeration, start to
		// transfer IN token to check for ports attachment
		// wLen = wPayload = 1 byte, always use USB address #1
		// if return is TRUE, a data pkt was ACK, data in HubChange
		// else is a NAK, no data was received 
	} // end of else

	return ;
}
*/
//*****************************************************************************************
// Slave_Detach
//*****************************************************************************************
/*
unsigned char Slave_Detach(void)
{
	if( (SL811Read(IntStatus)&INSERT_REMOVE) || (SL811Read(IntStatus)&USB_RESET) )
	{												// Test USB detached?
		bXXGFlags.bits.SLAVE_ENUMERATED = FALSE;					// return to un-enumeration
		//uHub.bPortPresent[1] = 0;					// Device #1 not present

		Set_ezDEV(1);								// inform master of slave detach
		SL811Write(IntStatus,INT_CLEAR); 			// clear interrupt status
		return TRUE;								// exit now !!!
	}

	return FALSE;
}
*/
//*****************************************************************************************
// Indicate to EZUSB's endpoint #2 IN of a new device attach/detach
//*****************************************************************************************
/*
void Set_ezDEV(BYTE chg)
{
	//if( (dsPoll) && (!(IN2CS & bmEPBUSY)) )
	//{
	//	IN2BUF[0] = chg;	// Arm endpoint #2, inform EZUSB host of attach/detatch
	//	IN2BC = 1;
	//}
}
*/
///////////////////////////////////////////////////////////////////////////////////////////
int hw_sof(void)
{
	SL811Write(cDATASet,0xe0);
	SL811Write(cSOFcnt,0xae);
 	SL811Write(CtrlReg,05);
	SL811Write(EP0Control,0x01);
}
unsigned char mem_test(void)
{
	unsigned char data temp,i;

	for (i=0;i<255;i++)
	{	
		SL811Write(i,i);
		temp=SL811Read(i);
	}

	for (i=0;i<255;i++)
	{	
		temp=SL811Read(i);
		if (temp!=i)
			return 0;
	}
	return 1;
} 

void sl811_init(void)
{	
	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
	
	//EnumUsbDev(1);
}


void sl811_disp(void)
{
	unsigned char data temp;

//	for (temp=0;temp<16;temp++)
//		SL811Write(temp,0x55);
 
	Uart0_SendString("***********************\r\n");
	Uart0_SendString("SL811 reg list:\r\n");

	temp=SL811Read(IntStatus);
	Uart0_SendString("IntStatus[0d] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(EP0Control);
	Uart0_SendString("EP0Control[00] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(EP0Address);
	Uart0_SendString("EP0Address[01] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");


	temp=SL811Read(EP0XferLen);
	Uart0_SendString("EP0XferLen[02] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");
		
	temp=SL811Read(EP0Status);
	Uart0_SendString("EP0Status[03] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(EP0Counter);
	Uart0_SendString("EP0Counter[04] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");



	temp=SL811Read(EP1Control);
	Uart0_SendString("EP1Control[08] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(EP1Address);
	Uart0_SendString("EP1Address[09] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");


	temp=SL811Read(EP1XferLen);
	Uart0_SendString("EP1XferLen[0a] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");
		
	temp=SL811Read(EP1Status);
	Uart0_SendString("EP1Status[0b] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(EP1Counter);
	Uart0_SendString("EP1Counter[0c] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");
	

	Uart0_SendString("\r\n");
	Uart0_SendString("\r\n");
	Uart0_SendString("\r\n");

	temp=SL811Read(CtrlReg);
	Uart0_SendString("CtrlReg[05] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");	

	temp=SL811Read(IntEna);
	Uart0_SendString("IntEna[06] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	
	temp=SL811Read(IntStatus);
	Uart0_SendString("IntStatus[0d] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(cDATASet);
	Uart0_SendString("cDATASet[0e] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

	temp=SL811Read(cSOFcnt);
	Uart0_SendString("cSOFcnt[0f] = ");
	Uart0_SendByte(ASSII[temp/16]);
	Uart0_SendByte(ASSII[temp%16]);
	Uart0_SendString("\r\n");

                               // 0x07 is reserved
}

⌨️ 快捷键说明

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