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

📄 sl811.lst

📁 一个用来实现单片机USB接口的程序
💻 LST
📖 第 1 页 / 共 3 页
字号:
 499          	
 500          	if(epaddr & 0x80)	// get direction of transfer
 501          		usbstack.pid = PID_IN;				
 502          	
 503          	if(usbXfer())
 504          		return TRUE;
 505          
 506          	return FALSE;
 507          }
 508          */
 509          
 510          //*****************************************************************************************
 511          // USB Device Enumeration Process
 512          // Support 1 confguration and interface #0 and alternate setting #0 only
 513          // Support up to 1 control endpoint + 4 data endpoint only
 514          //*****************************************************************************************
 515          unsigned char EnumUsbDev(BYTE usbaddr)
 516          {  
 517   1      	unsigned char i;											// always reset USB transfer address 
 518   1      	unsigned char uAddr = 0;							// for enumeration to Address #0
 519   1      	unsigned char epLen;
 520   1      	//unsigned short strLang;
 521   1      	
 522   1      	pDevDesc  pDev;	
 523   1      	pCfgDesc pCfg;
 524   1      	pIntfDesc pIfc;
 525   1      	pEPDesc pEnp;
 526   1      	//------------------------------------------------
 527   1      	// Reset only Slave device attached directly
 528   1      	//------------------------------------------------
 529   1      	uDev.wPayLoad[0] = 64;	// default 64-byte payload of Endpoint 0, address #0
 530   1      	if(usbaddr == 1)		// bus reset for the device attached to SL811HS only
 531   1      		USBReset();		// that will always have the USB address = 0x01 (for a hub)
 532   1          	
 533   1          	DelayMs(25);
 534   1      	
 535   1      	//i = SL811Read(EP0Status);	
 536   1      	//i=SL811Read(IntStatus);
 537   1      	//------------------------------------------------
 538   1      	// Get USB Device Descriptors on EP0 & Addr 0
 539   1      	// with default 64-byte payload
 540   1      	//------------------------------------------------
 541   1      	pDev =(pDevDesc)DBUF;					// ask for 64 bytes on Addr #0
 542   1      	
 543   1      	usbstack.usbaddr=uAddr;
 544   1      	usbstack.setup.wValue=DEVICE;
 545   1      	usbstack.setup.wIndex=0;
 546   1      	usbstack.setup.wLength=18;
 547   1      	//usbstack.setup.wLength=sbstack.setup.wLength);
 548   1      	usbstack.buffer=DBUF;
 549   1      	
 550   1      	if (!GetDesc())			// and determine the wPayload size
C51 COMPILER V6.20c  SL811                                                                 10/09/2005 10:50:56 PAGE 10  

 551   1      		return FALSE;								// get correct wPayload of Endpoint 0
 552   1      	uDev.wPayLoad[0]=pDev->bMaxPacketSize0;// on current non-zero USB address
 553   1      
 554   1      	//------------------------------------------------
 555   1      	// Set Slave USB Device Address
 556   1      	//------------------------------------------------
 557   1      	if (!SetAddress(usbaddr)) 						// set to specific USB address
 558   1      		return FALSE;								//
 559   1      	uAddr = usbaddr;								// transfer using this new address
 560   1      
 561   1      	//------------------------------------------------
 562   1      	// Get USB Device Descriptors on EP0 & Addr X
 563   1      	//------------------------------------------------
 564   1      	pDev =(pDevDesc)DBUF;
 565   1      	usbstack.usbaddr=uAddr;
 566   1      	
 567   1      	usbstack.setup.wLength=pDev->bLength;
 568   1      	usbstack.setup.wValue=DEVICE;
 569   1      	usbstack.setup.wIndex=0;
 570   1      	
 571   1      	//usbstack.setup.wLength=0x12;//(unsigned short)DBUF[0];//pDev->bLength;
 572   1      	usbstack.buffer=DBUF;
 573   1      	
 574   1      	if (!GetDesc()) 	
 575   1      		return FALSE;								// For this current device:
 576   1      	uDev.wVID  = pDev->idVendor;			// save VID
 577   1      	uDev.wPID  = pDev->idProduct;			// save PID
 578   1      	uDev.iMfg  = pDev->iManufacturer;		// save Mfg Index
 579   1      	uDev.iPdt  = pDev->iProduct;			// save Product Index
 580   1      
 581   1      	//------------------------------------------------
 582   1      	// Get String Descriptors
 583   1      	//------------------------------------------------
 584   1      	//------------------------------------------------
 585   1      	// Get Slave USB Configuration Descriptors
 586   1      	//------------------------------------------------
 587   1      	
 588   1      	pCfg = (pCfgDesc)DBUF;	
 589   1      	
 590   1      	usbstack.usbaddr=uAddr;
 591   1      	usbstack.setup.wValue=CONFIGURATION;
 592   1      	usbstack.setup.wIndex=0;
 593   1      	usbstack.setup.wLength=64;
 594   1      	usbstack.buffer=DBUF;	
 595   1      	if (!GetDesc()) 		
 596   1      		return FALSE;	
 597   1      	
 598   1      	pIfc = (pIntfDesc)(DBUF + 9);					// point to Interface Descp
 599   1      	uDev.bClass 	= pIfc->iClass;			// update to class type
 600   1      	uDev.bNumOfEPs = (pIfc->bEndPoints <= MAX_EP) ? pIfc->bEndPoints : MAX_EP;
 601   1      	
 602   1      	if(uDev.bClass==8) //mass storage device
 603   1      		bXXGFlags.bits.bMassDevice=TRUE;
 604   1      	//------------------------------------------------
 605   1      	// Set configuration (except for HUB device)
 606   1      	//------------------------------------------------
 607   1      	usbstack.usbaddr=uAddr;
 608   1      	usbstack.setup.wValue=DEVICE;
 609   1      	//if (uDev[usbaddr].bClass!=HUBCLASS)				// enumerating a FS/LS non-hub device
 610   1      		if (!Set_Configuration())		// connected directly to SL811HS
 611   1      				return FALSE;
 612   1      
C51 COMPILER V6.20c  SL811                                                                 10/09/2005 10:50:56 PAGE 11  

 613   1      	//------------------------------------------------
 614   1      	// For each slave endpoints, get its attributes
 615   1      	// Excluding endpoint0, only data endpoints
 616   1      	//------------------------------------------------
 617   1      	
 618   1      	epLen = 0;
 619   1      	for (i=1; i<=uDev.bNumOfEPs; i++)				// For each data endpoint
 620   1      	{
 621   2      		pEnp = (pEPDesc)(DBUF + 9 + 9 + epLen);	   			// point to Endpoint Descp(non-HID)
 622   2      		//if(pIfc->iClass == HIDCLASS)	
 623   2      		//	pEnp = (pEPDesc)(DBUF + 9 + 9 + 9 + epLen);		// update pointer to Endpoint(HID)
 624   2      		uDev.bEPAddr[i]  	= pEnp->bEPAdd;			// Ep address and direction
 625   2      		uDev.bAttr[i]		= pEnp->bAttr;			// Attribute of Endpoint
 626   2      		uDev.wPayLoad[i] 	= WordSwap(pEnp->wPayLoad);		// Payload of Endpoint
 627   2      		uDev.bInterval[i] 	= pEnp->bInterval;		// Polling interval
 628   2      	    	uDev.bData1[i] = 0;			            // init data toggle
 629   2      		epLen += 7;
 630   2      		//////////////////////////////
 631   2      		if(uDev.bAttr[i]==0x2)
 632   2      		{
 633   3      		    if(uDev.bEPAddr[i]&0x80)
 634   3      		    	usbstack.epbulkin=uDev.bEPAddr[i];
 635   3      		    else
 636   3      		    	usbstack.epbulkout=uDev.bEPAddr[i];
 637   3      		}
 638   2      		//////////////////////////////
 639   2      	}
 640   1      	
 641   1      	return TRUE;
 642   1      }
 643          
 644          ///////////////////////////////////////////////////////////////////////////////////////////
 645          void SL811_Init(void)
 646          {	
 647   1      	bXXGFlags.bits.SLAVE_ONLINE = FALSE;
 648   1      	bXXGFlags.bits.SLAVE_FOUND = FALSE;
 649   1      	bXXGFlags.bits.SLAVE_REMOVED=FALSE;
 650   1      	
 651   1      	bXXGFlags.bits.SLAVE_ENUMERATED = FALSE;
 652   1      	bXXGFlags.bits.SLAVE_IS_ATTACHED = FALSE;
 653   1      	
 654   1      	SL811_CS=0;
 655   1      	///////////////////////////////////////////////////////
 656   1      	
 657   1      	SL811Write(cDATASet,0xe0);
 658   1      	SL811Write(cSOFcnt,0xae);
 659   1      	SL811Write(CtrlReg,0x5);
 660   1      			
 661   1      	SL811Write(EP0Status,0x50);
 662   1      	SL811Write(EP0Counter,0);
 663   1      	SL811Write(EP0Control,0x01);
 664   1      			
 665   1      	
 666   1      	SL811Write(IntEna,0x20);      		// USB-A, Insert/Remove, USB_Resume.
 667   1      	SL811Write(IntStatus,INT_CLEAR);	// Clear Interrupt enable status
 668   1      }
 669          
 670          void check_key_LED(void)
 671          {
 672   1      	static unsigned char last_key=0x7;
 673   1      	unsigned char *pBuf;//=(unsigned char *)&UartCmdBlock;
 674   1      	unsigned int *wpBuf;
C51 COMPILER V6.20c  SL811                                                                 10/09/2005 10:50:56 PAGE 12  

 675   1      	unsigned char intr;
 676   1      	unsigned int i,len;
 677   1      	//SL811Write(IntStatus,INSERT_REMOVE);
 678   1      	intr=SL811Read(IntStatus);
 679   1      	if(intr & USB_RESET)
 680   1      		   {
 681   2      		   		//bXXGFlags.bits.SLAVE_IS_ATTACHED = FALSE;	// Set USB device found flag
 682   2      		   if(bXXGFlags.bits.SLAVE_ONLINE ==TRUE)
 683   2      		   	{bXXGFlags.bits.SLAVE_REMOVED=TRUE;
 684   3      		   	bXXGFlags.bits.SLAVE_ONLINE =FALSE;}
 685   2      		   }
 686   1      	else	{
 687   2      		   		//bXXGFlags.bits.SLAVE_IS_ATTACHED = TRUE;
 688   2      		   if(bXXGFlags.bits.SLAVE_ONLINE == FALSE)
 689   2      		   	{bXXGFlags.bits.SLAVE_FOUND=TRUE;
 690   3      		   	bXXGFlags.bits.SLAVE_ONLINE =TRUE;}
 691   2      		   }
 692   1      	//bXXGFlags.bits.SLAVE_FOUND;
 693   1      	
 694   1      	//bXXGFlags.bits.SLAVE_REMOVED=0;
 695   1      	SL811Write(IntStatus,INT_CLEAR);
 696   1      	SL811Write(IntStatus,INSERT_REMOVE);
 697   1      	
 698   1      	/////// for test only /////////////////////
 699   1      	//intr=SWM0;
 700   1      	intr&=0x1;
 701   1      	if(intr!=last_key)
 702   1      		{
 703   2      		if(intr==0)
 704   2      			{
 705   3      			pBuf=(unsigned char *)&UartCmdBlock;
 706   3      			for(i=0;i<64;i++)
 707   3      				*(pBuf+i)=*(cmd_test_createfile+i);
 708   3      			bXXGFlags.bits.bUartInDone=1;
 709   3      			last_key=0;
 710   3      			}
 711   2      		else
 712   2      			last_key=1;
 713   2      		}
 714   1      	////////////////////////////////////////////
 715   1      	//intr=SWM1;
 716   1      	intr&=0x1;
 717   1      	if(intr!=last_key)
 718   1      		{
 719   2      		if(intr==0)
 720   2      			{
 721   3      			pBuf=(unsigned char *)&UartCmdBlock;
 722   3      			for(i=0;i<64;i++)
 723   3      				*(pBuf+i)=*(cmd_test_writefile+i);
 724   3      			wpBuf=(unsigned int *)UARTBUF;
 725   3      			len=SwapINT16(UartCmdBlock.CmdBlock.Cmd_WriteFile.writeLength)/2;
 726   3      			for(i=0;i<len;i++)
 727   3      				*(wpBuf+i)=i;
 728   3      			bXXGFlags.bits.bUartInDone=1;
 729   3      			last_key=0;
 730   3      			}
 731   2      		else
 732   2      			last_key=1;
 733   2      		}
 734   1      	
 735   1      	/////////////////////////////////////////////		
 736   1      }
C51 COMPILER V6.20c  SL811                                                                 10/09/2005 10:50:56 PAGE 13  



MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   2519    ----
   CONSTANT SIZE    =    124    ----
   XDATA SIZE       =   1533      36
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =     45    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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