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

📄 host_811.lst

📁 51+sl811读写U盘的源程序+原理图
💻 LST
📖 第 1 页 / 共 4 页
字号:
 489          		else											// device-to-host : OUT token
 490             		{							
 491          			if(!usbXfer(usbaddr, ep0, PID_OUT, 0, payload, wLen, pData))
 492          				return FALSE;
 493          		}
 494          	}
 495          
 496          	//----------------------------------------------------
 497          	// Status stage IN or OUT zero-length data packet
 498          	//----------------------------------------------------
 499          	if(!usbXfer(usbaddr, ep0, pid, 0, payload, 0, NULL))
 500          		return FALSE;
 501          
 502          	return TRUE;											
 503          }
 504          
 505          //*****************************************************************************************
 506          // Control endpoint
 507          //*****************************************************************************************
 508          int VendorCmd(BYTE usbaddr,BYTE bReq,BYTE bCmd,WORD wValue,WORD wIndex,WORD wLen,BYTE *pData)
 509          { 
 510              xdata SetupPKG setup;
 511          
 512              setup.bmRequest  = bReq;
 513              setup.bRequest   = bCmd;
 514              setup.wValue     = wValue;
 515              setup.wIndex     = wIndex;
 516              setup.wLength    = WordSwap(wLen);
 517          
 518             	return ep0Xfer(usbaddr, uDev[usbaddr].wPayLoad[0], &setup, pData);
 519          }
 520          
 521          //*****************************************************************************************
 522          // Set Device Address : 
 523          //*****************************************************************************************
 524          int SetAddress(WORD addr)
 525          {
 526          	return VendorCmd(0,0,SET_ADDRESS, WordSwap(addr), 0, 0, NULL);
 527          }
 528          
 529          //*****************************************************************************************
 530          // Set Device Configuration : 
 531          //*****************************************************************************************
 532          int Set_Configuration(BYTE usbaddr, WORD wVal)
 533          {
 534          	return VendorCmd(usbaddr, 0, SET_CONFIG, WordSwap(wVal), 0, 0, NULL);
 535          }
 536          
 537          //*****************************************************************************************
 538          // Get Device Descriptor : Device, Configuration, String
 539          //*****************************************************************************************
 540          int GetDesc(BYTE usbaddr, WORD wValue, 	WORD wIndex, WORD wLen, BYTE *desc)
 541          { 
 542          	return VendorCmd(usbaddr, 0x80, GET_DESCRIPTOR, wValue, wIndex, wLen, desc);
 543          }
 544          
C51 COMPILER V6.10  HOST_811                                                               09/04/2002 22:22:07 PAGE 10  

 545          //*****************************************************************************************
 546          // HID Get_Desc :
 547          //*****************************************************************************************
 548          int GetHid_Desc(BYTE usbaddr, WORD wValue, WORD wLen, BYTE *desc)
 549          { 
 550          	return VendorCmd(usbaddr, 0x81, GET_DESCRIPTOR, wValue, 0, wLen, desc);
 551          }
 552          
 553          //*****************************************************************************************
 554          // GetHUBDesc :
 555          //*****************************************************************************************
 556          int GetHubDesc(BYTE usbaddr, WORD wValue, WORD wLen, BYTE *desc)
 557          { 
 558          	return VendorCmd(usbaddr, 0xA0, GET_DESCRIPTOR, wValue, 0, wLen, desc);
 559          }
 560          
 561          //*****************************************************************************************
 562          // Get Status : (HUB)
 563          //*****************************************************************************************
 564          int GetStatus(BYTE usbaddr, BYTE *desc)
 565          { 
 566          	return VendorCmd(usbaddr, 0x80, GET_STATUS, 0, 0, 2, desc);		 
 567          }
 568          
 569          //*****************************************************************************************
 570          // PortFeature : (SET_FEATURE, CLEAR_FEATURE)
 571          //*****************************************************************************************
 572          int PortFeature(BYTE usbaddr, BYTE bReq, WORD wValue, BYTE cPort)
 573          { 
 574          	return VendorCmd(usbaddr, 0x23, bReq, WordSwap(wValue), WordSwap((WORD)cPort), 0, NULL);		  
 575          }
 576          
 577          //*****************************************************************************************
 578          // GetPortStatus :
 579          //*****************************************************************************************
 580          int GetPortStatus(BYTE usbaddr, BYTE cPort, BYTE *desc)
 581          { 
 582          	return VendorCmd(usbaddr, 0xA3, GET_STATUS, 0, WordSwap((WORD)cPort), 0x04, desc);
 583          }
 584          
 585          //*****************************************************************************************
 586          // GetDevInfo :
 587          //*****************************************************************************************
 588          WORD GetDevInfo(BYTE *DevInfo)
 589          { 
 590            BYTE EpAddr;
 591            int i;
 592            BYTE DescBufLen = 0;
 593          
 594          		for(EpAddr = 1; (EpAddr < MAX_DEV); EpAddr++)
 595          		{
 596          			if(uHub.bPortPresent[EpAddr])
 597          			{
 598          					 	DevInfo[DescBufLen++] = EpAddr;							// USB Address
 599           						DevInfo[DescBufLen++]	= uHub.bPortNumber[EpAddr];			// Port Number
 600          						DevInfo[DescBufLen++] = uHub.bPortSpeed[EpAddr];			// Device Speed (from enum)
 601          						DevInfo[DescBufLen++] = uDev[EpAddr].bClass;				// Class Type (from enum)
 602          						(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wVID;			// VID
 603          						DescBufLen++;
 604          						(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wPID;			// PID
 605          						DescBufLen++;
 606          						DevInfo[DescBufLen++] = (BYTE)uDev[EpAddr].wPayLoad[0];	// Ep0 MaxPktSize (max 64 bytes)
C51 COMPILER V6.10  HOST_811                                                               09/04/2002 22:22:07 PAGE 11  

 607          						DevInfo[DescBufLen++] = uDev[EpAddr].bNumOfEPs;			// Number of data endpoints
 608          						for(i=0;i<uDev[EpAddr].bNumOfEPs;i++)		// save all data endpoints info
 609          						{												
 610          							DevInfo[DescBufLen++] 	= uDev[EpAddr].bEPAddr[i+1];	// ep address/direction
 611          							DevInfo[DescBufLen++] 	= uDev[EpAddr].bAttr[i+1];		// transfer type
 612          							(WORD)DevInfo[DescBufLen++] = uDev[EpAddr].wPayLoad[i+1];	// max data payload
 613          							DescBufLen++;                                       // 2 byte payload
 614          							DevInfo[DescBufLen++] 	= uDev[EpAddr].bInterval[i+1];	// polling interval
 615          						}	
 616          			}
 617          		}
 618          		return(DescBufLen);
 619          }
 620          
 621          //*****************************************************************************************
 622          // USB Data Endpoint Read/Write
 623          // wLen is in low byte first format
 624          //*****************************************************************************************
 625          int DataRW(BYTE usbaddr, BYTE epaddr, WORD wPayload, WORD wLen, BYTE *pData)
 626          {
 627          	xdata BYTE pid = PID_OUT;
 628          
 629          	if(epaddr & 0x80)	// get direction of transfer
 630          		pid = PID_IN;				
 631          
 632          	if(usbXfer(usbaddr,epaddr&0x0F,pid,0,wPayload,wLen,pData))
 633          		return TRUE;
 634          
 635          	return FALSE;
 636          }
 637          
 638          //*****************************************************************************************
 639          // USB Device Enumeration Process
 640          // Support 1 confguration and interface #0 and alternate setting #0 only
 641          // Support up to 1 control endpoint + 4 data endpoint only
 642          //*****************************************************************************************
 643          int EnumUsbDev(BYTE usbaddr)
 644          {  
 645          	int i;											// always reset USB transfer address 
 646          	xdata BYTE uAddr = 0;							// for enumeration to Address #0
 647          	xdata BYTE epLen;
 648          	xdata WORD strLang;
 649          
 650          	//------------------------------------------------
 651          	// Reset only Slave device attached directly
 652          	//------------------------------------------------
 653          	uDev[0].wPayLoad[0] = 64;						// default 64-byte payload of Endpoint 0, address #0
 654          	if(usbaddr == 1)								// bus reset for the device attached to SL811HS only
 655          		USBReset();									// that will always have the USB address = 0x01 (for a hub)
 656              EZUSB_Delay(25);
 657          
 658          	//------------------------------------------------
 659          	// Get USB Device Descriptors on EP0 & Addr 0
 660          	// with default 64-byte payload
 661          	//------------------------------------------------
 662          	pDev =(pDevDesc)DBUF;							// ask for 64 bytes on Addr #0
 663          	if (!GetDesc(uAddr,DEVICE,0,18,DBUF))			// and determine the wPayload size
 664          		return FALSE;								// get correct wPayload of Endpoint 0
 665          	uDev[usbaddr].wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address
 666          
 667          	//------------------------------------------------
 668          	// Set Slave USB Device Address
C51 COMPILER V6.10  HOST_811                                                               09/04/2002 22:22:07 PAGE 12  

 669          	//------------------------------------------------
 670          	if (!SetAddress(usbaddr)) 						// set to specific USB address
 671          		return FALSE;								//
 672          	uAddr = usbaddr;								// transfer using this new address
 673          
 674          	//------------------------------------------------
 675          	// Get USB Device Descriptors on EP0 & Addr X
 676          	//------------------------------------------------
 677          	if (!GetDesc(uAddr,DEVICE,0,(pDev->bLength),DBUF)) 	
 678          		return FALSE;								// For this current device:
 679          	uDev[usbaddr].wVID 	 = pDev->idVendor;			// save VID
 680          	uDev[usbaddr].wPID 	 = pDev->idProduct;			// save PID
 681          	uDev[usbaddr].iMfg 	 = pDev->iManufacturer;		// save Mfg Index
 682          	uDev[usbaddr].iPdt 	 = pDev->iProduct;			// save Product Index
 683          
 684          	//------------------------------------------------
 685          	// Get String Descriptors
 686          	//------------------------------------------------
 687          	pStr = (pStrDesc)DBUF;	
 688          	if (!GetDesc(uAddr,STRING,0,4,DBUF)) 			// Get string language
 689          		return FALSE;								
 690          	strLang = pStr->wLang;							// get iManufacturer String length
 691          	if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,4,DBUF)) 		
 692          		return FALSE;								// get iManufacturer String descriptors
 693          	if (!GetDesc(uAddr,(WORD)(uDev[usbaddr].iMfg<<8)|STRING,strLang,pStr->bLength,DBUF)) 		
 694          		return FALSE;			
 695          
 696          	//------------------------------------------------
 697          	// Get Slave USB Configuration Descriptors
 698          	//------------------------------------------------
 699          	pCfg = (pCfgDesc)DBUF;									
 700          	if (!GetDesc(uAddr,CONFIGURATION,0,8,DBUF)) 		
 701          		return FALSE;										
 702          	if (!GetDesc(uAddr,CONFIGURATION,0,WordSwap(pCfg->wLength),DBUF)) 	
 703          		return FALSE;		
 704          
 705          	pIfc = (pIntfDesc)(DBUF + 9);					// point to Interface Descp
 706          	uDev[usbaddr].bClass 	= pIfc->iClass;			// update to class type
 707          	uDev[usbaddr].bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
 708          
 709          	//------------------------------------------------
 710          	// Set configuration (except for HUB device)
 711          	//------------------------------------------------
 712          	if (uDev[usbaddr].bClass!=HUBCLASS)				// enumerating a FS/LS non-hub device
 713          		if (!Set_Configuration(uAddr,DEVICE))		// connected directly to SL811HS
 714          				return FALSE;
 715          
 716          	//------------------------------------------------
 717          	// For each slave endpoints, get its attributes
 718          	// Excluding endpoint0, only data endpoints
 719          	//------------------------------------------------
 720          	epLen = 0;
 721          	for (i=1; i<=uDev[usbaddr].bNumOfEPs; i++)				// For each data endpoint
 722          	{
 723          		pEnp = (pEPDesc)(DBUF + 9 + 9 + epLen);	   			// point to Endpoint Descp(non-HID)
 724          		if(pIfc->iClass == HIDCLASS)	
 725          			pEnp = (pEPDesc)(DBUF + 9 + 9 + 9 + epLen);		// update pointer to Endpoint(HID)
 726          		uDev[usbaddr].bEPAddr[i]  	= pEnp->bEPAdd;			// Ep address and direction
 727          		uDev[usbaddr].bAttr[i]		= pEnp->bAttr;			// Attribute of Endpoint
 728          		uDev[usbaddr].wPayLoad[i] 	= pEnp->wPayLoad;		// Payload of Endpoint
 729          		uDev[usbaddr].bInterval[i] 	= pEnp->bInterval;		// Polling interval
 730          	    uDev[usbaddr].bData1[i] = 0;			            // init data toggle
C51 COMPILER V6.10  HOST_811                                                               09/04/2002 22:22:07 PAGE 13  

 731          		epLen += 7;
 732          	}
 733          
 734          	//------------------------------------------------
 735          	// Get Hid Report Descriptors
 736          	//------------------------------------------------
 737          	if(pIfc->iClass == HIDCLASS)
 738          	{	
 739          		pHid = (pHidDesc)(DBUF + 9 + 9);	   				// point to HID-CLASS descp
 740          		if (!GetHid_Desc(uAddr,HID_REPORT,pHid->wItemLength,DBUF))

⌨️ 快捷键说明

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