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

📄 my_sl811.c

📁 USB for TMS320C5402
💻 C
📖 第 1 页 / 共 2 页
字号:
        
    return ep0Xfer();//(usbaddr, uDev[usbaddr].wPayLoad[0], &setup, pData);
}



//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
unsigned char GetDesc(void)
{ 
	
	usbstack.setup.bmRequest=0x80;
	usbstack.setup.bRequest=GET_DESCRIPTOR;
	//usbstack.setup.wValue=WordSwap(usbstack.setup.wValue);	
	//usbstack.setup.wValue=usbstack.setup.wValue;	
	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
//*****************************************************************************************
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;
		
	//------------------------------------------------
	// 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(100);
	
	//------------------------------------------------
	// Get USB Device Descriptors on EP0 & Addr 0
	// with default 64-byte payload
	//------------------------------------------------
		
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=18;
	usbstack.buffer=DBUF;
	
	if (!GetDesc())			// and determine the wPayload size
		return FALSE;								// get correct wPayload of Endpoint 0
	uDev.wPayLoad[0]=DBUF[7];	// 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
	//------------------------------------------------
	

	usbstack.usbaddr=uAddr;	
	usbstack.setup.wLength=DBUF[0];	
	usbstack.setup.wValue=DEVICE;
	usbstack.setup.wIndex=0;	
	usbstack.buffer=DBUF;	
	if (!GetDesc()) 	
		return FALSE;								// For this current device:
	uDev.iMfg  = DBUF[14];	
	uDev.iPdt  = DBUF[15];

	//------------------------------------------------
	// Get Slave USB Configuration Descriptors
	//------------------------------------------------
	
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=CONFIGURATION;
	usbstack.setup.wIndex=0;
	usbstack.setup.wLength=64;
	usbstack.buffer=DBUF;	
	if (!GetDesc()) 		
		return FALSE;	

	uDev.bClass 	= DBUF[9+5];	// update to class type
	uDev.bNumOfEPs = (DBUF[9+4] <= MAX_EP) ? DBUF[9+4] : MAX_EP;
	if(uDev.bClass==8) //mass storage device
		bFlags.bits.bMassDevice=TRUE;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	usbstack.usbaddr=uAddr;
	usbstack.setup.wValue=DEVICE;
					// enumerating a FS/LS non-hub device
		if (!Set_Configuration())		// connected directly to SL811HS
				return FALSE;
		//the Addition of my		
        //if(!Set_Interferface())
        //        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
	{
		uDev.bEPAddr[i]		= DBUF[9 + 9 + epLen+2];	// Ep address and direction				
		uDev.bAttr[i]		= DBUF[9 + 9 + epLen+3];	// Attribute of Endpoint			
		uDev.wPayLoad[i] 	= LSwapINT16(DBUF[9 + 9 + epLen+4],DBUF[9 + 9 + epLen+5]);	// Payload of Endpoint
		uDev.bInterval[i] 	= DBUF[9 + 9 + epLen+6];	// Polling interval
	    uDev.bData1[i] = 0;			            // init data toggle
		epLen += 7;
		//////////////////////////////
		if(uDev.bAttr[i]==0x2)     //bulk transfer
		{
		    if(uDev.bEPAddr[i]&0x80)
		    	usbstack.epbulkin=uDev.bEPAddr[i];
		    else
		    	usbstack.epbulkout=uDev.bEPAddr[i];
		}
		//////////////////////////////
	}
	
	return TRUE;
}



//*****************************************************************************************
// Byte Read from SL811H
// a = register address
// return = data in register
//*****************************************************************************************
unsigned char SL811Read(unsigned char a)
{  
	//unsigned char nVal;
	//unsigned int xdata *exAddress;
	//exAddress = SL811_ADDR_PORT;
	////SL811_CS=1;
	//*exAddress=a;
	//exAddress=SL811_DATA_PORT;
	//nVal = *exAddress;
	////SL811_CS=0;
	//return nVal;
	SL811H_ADDR = a;
	DelayMs(10);			
	return (SL811H_DATA&0xFF);
}

//*****************************************************************************************
// Byte Write to SL811H
// a = register address
// d = data to be written to this register address
//*****************************************************************************************
void SL811Write(unsigned char a, unsigned char d)
{  
	//xdata *exAddress;
	//exAddress = SL811_ADDR_PORT;
	////SL811_CS=1;
	//*exAddress=a;
	//exAddress=SL811_DATA_PORT;
	//*exAddress = d;
	////SL811_CS=0;
	SL811H_ADDR = a;
	DelayMs(10);	
	SL811H_DATA = d&0xFF;
}

//*****************************************************************************************
// 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)//(unsigned char addr, unsigned char *s, unsigned char c)
{	
	//unsigned char i;
	//unsigned char xdata *exAddress;
	//exAddress=SL811_ADDR_PORT;
	////SL811_CS=1;
	//*exAddress = addr;
	//exAddress=SL811_DATA_PORT;
	//for(i=0;i<c;i++)
	//	{
	//	*s++ = *exAddress;
	//	}
	////SL811_CS=0;
	char tmp_char;
	
	SL811H_ADDR = addr;	
   	while (c--) 
   	   {
   	    DelayMs(5);
   		tmp_char = SL811H_DATA;
		*s++ = (BYTE)tmp_char&0xFF;
		
       }
}

//*****************************************************************************************
// 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)//(unsigned char addr, unsigned char *s, unsigned char c)
{	
	//unsigned char xdata *exAddress;
	//exAddress = SL811_ADDR_PORT;
	
	////SL811_CS=1;
	//*exAddress = addr;
	//exAddress=SL811_DATA_PORT;
	//while (c--) 
	//	{
	//	*exAddress = *s++;
	//	}
	////SL811_CS=0;
		SL811H_ADDR = addr;	
   	    while (c--) 
   	      {
   	        DelayMs(5);
		    SL811H_DATA = *s++;
		  }
	
}

//*****************************************************************************************
// Word(high) swap to low 
// input = the word wait to swap
// 
// 
//*****************************************************************************************
unsigned int WordSwap(unsigned int input)
{
    unsigned int temp1,temp2;
    temp1=(input & 0x00FF)<<8;
    temp2=(input & 0xFF00)>>8;
    return (temp1|temp2);
	//return(((input&0x00FF)<<8)|((input&0xFF00)>>8));
	//return(((input&0x00FF)<<16)|((input&0x00FF0000)>>16));
}

void DelayMs(unsigned char nFactor)
{
	unsigned char i;
	unsigned int j;

	for(i=0; i<nFactor; i++)
		{
		for(j=0;j<100;j++)
		         j=j;
		}
}

unsigned int LSwapINT16(unsigned short dData1,unsigned short dData2)
{
    //unsigned int xdata dData;
    //dData = ((dData2<<8)&0xff00)|(dData1&0x00ff);
	return ((dData2<<8)&0xff00)|(dData1&0x00ff);  //dData;
}

unsigned long LSwapINT32(unsigned long dData1,unsigned long dData2,unsigned long dData3,unsigned long dData4)
{
    unsigned long xdata dData;
    dData = ((dData4<<24)&0xff000000)|((dData3<<16)&0xff0000)|((dData2<<8)&0xff00)|(dData1&0xff);
	return dData;
}

///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
{	
	bFlags.bits.SLAVE_ONLINE = FALSE;
	bFlags.bits.SLAVE_FOUND = FALSE;
	bFlags.bits.SLAVE_REMOVED=FALSE;
	
	
	bFlags.bits.SLAVE_ENUMERATED = FALSE;
	bFlags.bits.SLAVE_IS_ATTACHED = FALSE;
	///////////////////////////////////////////////////////
	//允许硬件产生SOF(Start of Frame)
	//SL811Write(CtrlReg,0x04);
	//DelayMs(100);
	//SL811Write(CtrlReg,0x01);
	SL811Write(CtrlReg,0x08);      		 //Reset USB engine, full-speed setup, suspend disable
	DelayMs(100);					   //Delay for HW stablize
	SL811Write(CtrlReg,0x00);      		// Set to normal operation
	DelayMs(10);
	//设置SOF,1 ms中断(低8位值)
	SL811Write(cDATASet,0xe0);
	DelayMs(10);
	//设置为主机工作模式和SOF 1ms中断(高6位值)
	SL811Write(cSOFcnt,0xae);
	DelayMs(10);
	//设置发送SOF包(5)、结点地址(EP)设为0(0),通道地址(Addr)设为0,共有127个地址
	SL811Write(EP0Status,0x50);
	DelayMs(10);
	SL811Write(EP0Counter,0);
	DelayMs(10);
	//允许数据发送
	SL811Write(EP0Control,0x01);
	DelayMs(10);
	SL811Write(IntEna,0x61);      		// USB-A, Insert/Remove, USB_Resume.
	DelayMs(10);
	//SL811Write(IntEna,0x20);      		// USB-A, Insert/Remove, USB_Resume.
	//清中断
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
}

/////////////////////////////////////////////
int speed_detect() 
{
	bFlags.bits.SLAVE_FOUND	= FALSE;				 //Clear USB device found flag
	bFlags.bits.FULL_SPEED  = TRUE;					 //Assume full speed device
	bFlags.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(100);					   //Delay for HW stablize
	SL811Write(CtrlReg,0x00);      		// Set to normal operation
	SL811Write(IntEna,0x61);      		// USB-A, InsertRemove, USB_Resume.
	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
	DelayMs(100);					// Delay for HW stablize
*/
    //MyUSBReset();
	//if(SL811Read(IntStatus)&USB_RESET)
	//{									 //test for USB reset
	//	SL811Write(IntStatus,INT_CLEAR); //Clear Interrupt enable status
	//	DelayMs(100);				// 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         
		bFlags.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
	}

	bFlags.bits.SLAVE_FOUND = TRUE;					 //Set USB device found flag
	bFlags.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(100);					// Hub required approx. 24.1mS
	SL811Write(IntStatus,INT_CLEAR);	 //Clear Interrupt status
	DelayMs(100);
	//LCD_DispHexData(7,2,SL811Read(IntStatus));
	return 0;    						// exit speed_detect();
}


 //Detect USB Device

int slave_detect(void)
{
	/*-------------------------------------------------------------------------
	 Wait for SL811HS enumeration
	-------------------------------------------------------------------------*/
	if(!bFlags.bits.SLAVE_ENUMERATED)					 //only if slave is not configured
	{
		speed_detect();						 //wait for an USB device to be inserted to 
		if(bFlags.bits.SLAVE_FOUND)						 //the SL811HST host
		{
	  		if(EnumUsbDev(1))				 //enumerate USB device, assign USB address = #1
			{
			   	bFlags.bits.SLAVE_ENUMERATED = TRUE;	 //Set slave USB device enumerated flag
			}	
		}
	}
	
	/*-------------------------------------------------------------------------
	 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
		bFlags.bits.SLAVE_ENUMERATED = FALSE;		 //return to un-enumeration
		SL811Write(IntStatus,INT_CLEAR); 			// clear interrupt status
		return TRUE;								// exit now !!!
	}
	return FALSE;
}

⌨️ 快捷键说明

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