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

📄 sl811.c

📁 USB HOST ,赛普拉萨 SL811 对应的弹片机驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
			if(!usbXfer(usbaddr,0,PID_IN,payload,wLen,pData))
				return FALSE;
			payload=0;
			}
		else											// device-to-host : OUT token
   			{
			if(!usbXfer(usbaddr,0,PID_OUT,payload,wLen,pData))
				return FALSE;
			}
		}
	//----------------------------------------------------
	// Status stage IN or OUT zero-length data packet
	//----------------------------------------------------
	if(!usbXfer(usbaddr,0,pid,payload,0,NULL))
		return FALSE;
	return TRUE;
	}

bit epBulkSend(unsigned char *pBuffer,unsigned int len)
	{
//	usbstack.usbaddr=0x1;
//	usbstack.endpoint=usbstack.epbulkout;
//	usbstack.pid=PID_OUT;
//	usbstack.wPayload=64;
//	usbstack.wLen=len;
//	usbstack.buffer=pBuffer;
//	if(usbstack.wLen)
//	      while(!usbXfer());
//	return TRUE;

	if(usbXfer(1,uDev.epbulkout&0x0F,PID_OUT,uDev.wPayLoad[uDev.epbulkout&0x0F],len,pBuffer))	//64
//	if(usbXfer(1,2,PID_OUT,64,len,pBuffer))	//64
		return TRUE;
	return FALSE;
	}

bit 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;
//	while(usbstack.wLen)
//		while(!usbXfer());
//	return TRUE;

	if(usbXfer(1,uDev.epbulkin&0x0F,PID_IN,uDev.wPayLoad[uDev.epbulkin&0x0F],len,pBuffer))		//64
//	if(usbXfer(1,1,PID_IN,64,len,pBuffer))	//64
		return TRUE;
	return FALSE;
	}
//*****************************************************************************************
// Control endpoint
//*****************************************************************************************
bit VendorCmd(uchar usbaddr,uchar bReq,uchar bCmd,uint wValue,uint wIndex,uint wLen,uchar *pData)
	{
    	xdata SetupPKG setup;
    	setup.bmRequest=bReq;
	setup.bRequest=bCmd;
    	setup.wValue=wValue;
    	setup.wIndex=wIndex;
    	setup.wLength=SwapINT16(wLen);
   	return 	ep0Xfer(usbaddr,uDev.wPayLoad[0], &setup,pData);
	}
//*****************************************************************************************
// Set Device Address :
//*****************************************************************************************
//bit SetAddress(uint addr)
//	{
//	return VendorCmd(0,0,SET_ADDRESS,SwapINT16(addr),0,0,NULL);
//	}
//*****************************************************************************************
// Set Device Configuration :
//*****************************************************************************************
//bit Set_Configuration(uchar usbaddr,uint wVal)
//	{
//	return VendorCmd(usbaddr,0,SET_CONFIG,SwapINT16(wVal),0,0,NULL);
//	}
//*****************************************************************************************
// Get Device Descriptor : Device, Configuration, String
//*****************************************************************************************
//bit GetDesc(uchar usbaddr,uint wValue,uint wIndex,uint wLen,uchar *desc)
//	{
//	return VendorCmd(usbaddr,0x80,GET_DESCRIPTOR,wValue,wIndex,wLen,desc);
//	}
//*****************************************************************************************
// USB Data Endpoint Read/Write
// wLen is in low byte first format
//*****************************************************************************************

//bit DataRW(uchar usbaddr,uchar epaddr,uint wPayload,uint wLen,uchar *pData)
//	{
//	xdata uchar pid=PID_OUT;
//	if(epaddr&0x80)	// get direction of transfer
//		pid=PID_IN;
//	if(usbXfer(usbaddr,epaddr&0x0F,pid,wPayload,wLen,pData))
//		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
//*****************************************************************************************
bit EnumUsbDev(uchar usbaddr)
	{
	int i;											// always reset USB transfer address
	idata		uchar 	uAddr=0;							// for enumeration to Address #0
	idata		uchar 	epLen;
	idata uint 	strLang;
	//------------------------------------------------
	// 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
//	if (!GetDesc(uAddr,DEVICE,0,18,DBUF))		// and determine the wPayload size
	if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,DEVICE,0,18,DBUF))
		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(!VendorCmd(0,0,SET_ADDRESS,SwapINT16((uint)usbaddr),0,0,NULL))
//	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
	//------------------------------------------------
//	if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),DBUF))
	if (!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,DEVICE,0,(pDev->bLength),DBUF))
		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
	//------------------------------------------------
	pStr=(pStrDesc)DBUF;
	if(VendorCmd(uAddr,0x80,GET_DESCRIPTOR,STRING,0,4,DBUF))
		{
		strLang=pStr->wLang;							// get iManufacturer String length
		if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,(uint)(uDev.iMfg<<8)|STRING,strLang,4,DBUF))
			return FALSE;								// get iManufacturer String descriptors
		if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,(uint)(uDev.iMfg<<8)|STRING,strLang,pStr->bLength,DBUF))
			return FALSE;
		}
	//------------------------------------------------
	// Get Slave USB Configuration Descriptors
	//------------------------------------------------
	pCfg=(pCfgDesc)DBUF;
//	if(!GetDesc(uAddr,CONFIGURATION,0,8,DBUF))
	if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,CONFIGURATION,0,8,DBUF))
		return FALSE;
//	if(!GetDesc(uAddr,CONFIGURATION,0,SwapINT16(pCfg->wLength),DBUF))
	if(!VendorCmd(uAddr,0x80,GET_DESCRIPTOR,CONFIGURATION,0,SwapINT16(pCfg->wLength),DBUF))
		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
		bMassDevice=TRUE;
	//------------------------------------------------
	// Set configuration (except for HUB device)
	//------------------------------------------------
	if (uDev.bClass!=HUBCLASS)				// enumerating a FS/LS non-hub device
		{
		if(!VendorCmd(uAddr,0,SET_CONFIG,SwapINT16(DEVICE),0,0,NULL))
//		if (!Set_Configuration(uAddr,DEVICE))			// 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)
		uDev.bEPAddr[i]=pEnp->bEPAdd;			// Ep address and direction
		uDev.bAttr[i]=pEnp->bAttr;			// Attribute of Endpoint
		uDev.wPayLoad[i]=SwapINT16(pEnp->wPayLoad);		// Payload of Endpoint
		uDev.bInterval[i]=pEnp->bInterval;		// Polling interval
	    	uDev.bData1[i]=0;			            // init data toggle
		epLen+=7;
		if(uDev.bAttr[i]==0x2)
			{
		    	if(uDev.bEPAddr[i]&0x80)
		    		uDev.epbulkin=uDev.bEPAddr[i];
		    	else
		    		uDev.epbulkout=uDev.bEPAddr[i];
			}
		}
	return TRUE;
	}

///////////////////////////////////////////////////////////////////////////////////////////
void SL811_Init(void)
	{
	SLAVE_IS_ATTACHED=SLAVE_ENUMERATED=SLAVE_REMOVED=SLAVE_FOUND=SLAVE_ONLINE=FALSE;
	SEL_OTHERS=1;
	///////////////////////////////////////////////////////
	SL811Write(cDATASet,0xe0);
	SL811Write(cSOFcnt,0xae);
	SL811Write(CtrlReg,0x5);		//自动产生SOF

	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_key_LED(void)
	{
	uchar intr;
	intr=SL811Read(IntStatus);
	if(intr&USB_RESET)
		{
		if(SLAVE_ONLINE==TRUE)
			{
			SLAVE_REMOVED=TRUE;
		   	SLAVE_ONLINE=FALSE;
		   	}
		}
	else	
		{
		if(SLAVE_ONLINE==FALSE)
			SLAVE_ONLINE=SLAVE_FOUND=TRUE;
		}
	SL811Write(IntStatus,INT_CLEAR);
	SL811Write(IntStatus,INSERT_REMOVE);
	}

⌨️ 快捷键说明

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