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

📄 hid_fw.lst

📁 USB to Serial的Keil C固件程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 1   


C51 COMPILER V6.12, COMPILATION OF MODULE HID_FW
OBJECT MODULE PLACED IN .\hid_fw.OBJ
COMPILER INVOKED BY: e:\Keil\C51\BIN\C51.EXE .\hid_fw.c REGFILE(.\usb2ser.ORC) BROWSE DEBUG OBJECTEXTEND CODE

stmt level    source

   1          //-----------------------------------------------------------------------------
   2          //	File:		fw.c
   3          //	Contents:	Firmware frameworks task dispatcher and device request parser
   4          //				source.
   5          //
   6          //	Copyright (c) 1997 AnchorChips, Inc. All rights reserved
   7          //-----------------------------------------------------------------------------
   8          #include "ezusb.h"
   9          #include "ezregs.h"
  10          
  11          
  12          //-----------------------------------------------------------------------------
  13          // Random Macros
  14          //-----------------------------------------------------------------------------
  15          #define	min(a,b) (((a)<(b))?(a):(b))
  16          #define	max(a,b) (((a)>(b))?(a):(b))
  17          
  18          //-----------------------------------------------------------------------------
  19          // Constants
  20          //-----------------------------------------------------------------------------
  21          #define	DELAY_COUNT				0x9248*8L		// Delay for 8 sec at 24Mhz, 4 sec at 48
  22          
  23          //-----------------------------------------------------------------------------
  24          // Global Variables
  25          //-----------------------------------------------------------------------------
  26          volatile BOOL	GotSUD;
  27          BOOL		Rwuen;
  28          BOOL		Selfpwr;
  29          volatile BOOL	Sleep;						// Sleep mode enable flag
  30          
  31          WORD	pDeviceDscr;	// Pointer to Device Descriptor; Descriptors may be moved
  32          WORD	pConfigDscr;	
  33          WORD	pStringDscr;	
  34          
  35          //-----------------------------------------------------------------------------
  36          // Prototypes
  37          //-----------------------------------------------------------------------------
  38          void SetupCommand(void);
  39          void TD_Init(void);
  40          void TD_Poll(void);
  41          BOOL TD_Suspend(void);
  42          BOOL TD_Resume(void);
  43          
  44          BOOL DR_GetDescriptor(void);
  45          BOOL DR_SetConfiguration(void);
  46          BOOL DR_GetConfiguration(void);
  47          BOOL DR_SetInterface(void);
  48          BOOL DR_GetInterface(void);
  49          BOOL DR_GetStatus(void);
  50          BOOL DR_ClearFeature(void);
  51          BOOL DR_SetFeature(void);
  52          BOOL DR_VendorCmnd(void);
  53          BOOL DR_ClassRequest(void);
  54          
  55          //-----------------------------------------------------------------------------
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 2   

  56          // Code
  57          //-----------------------------------------------------------------------------
  58          
  59          // Task dispatcher
  60          void main(void)
  61          {
  62   1      	DWORD	i;
  63   1      	WORD	offset;
  64   1      	DWORD	DevDescrLen;
  65   1      	DWORD	j=0;
  66   1      	WORD	IntDescrAddr;
  67   1      	WORD	ExtDescrAddr;
  68   1      
  69   1      	// Initialize Global States
  70   1      	Sleep = FALSE;					// Disable sleep mode
  71   1      	Rwuen = FALSE;					// Disable remote wakeup
  72   1      	Selfpwr = FALSE;				// Disable self powered
  73   1      	GotSUD = FALSE;					// Clear "Got setup data" flag
  74   1      
  75   1      	// Initialize user device
  76   1      	TD_Init();
  77   1      
  78   1      	// The following section of code is used to relocate the descriptor table. 
  79   1      	// Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor 
  80   1      	// table, the descriptor table must be located in on-part memory.
  81   1      	// The 4K demo tools locate all code sections in external memory.
  82   1      	// The descriptor table is relocated by the frameworks ONLY if it is found 
  83   1      	// to be located in external memory.
  84   1      	pDeviceDscr = (WORD)&DeviceDscr;
  85   1      	pConfigDscr = (WORD)&ConfigDscr;
  86   1      	pStringDscr = (WORD)&StringDscr;
  87   1      	if ((WORD)&DeviceDscr & 0xe000)
  88   1      	{
  89   2      		IntDescrAddr = INTERNAL_DSCR_ADDR;
  90   2      		ExtDescrAddr = (WORD)&DeviceDscr;
  91   2      		DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2;
  92   2      		for (i = 0; i < DevDescrLen; i++)
  93   2      			*((BYTE xdata *)IntDescrAddr+i) = 0xCD;
  94   2      		for (i = 0; i < DevDescrLen; i++)
  95   2      			*((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i);
  96   2      		pDeviceDscr = IntDescrAddr;
  97   2      		offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR;
  98   2      		pConfigDscr -= offset;
  99   2      		pStringDscr -= offset;
 100   2      	}
 101   1      
 102   1      	EZUSB_IRQ_ENABLE();				// Enable USB interrupt (INT2)
 103   1      	EZUSB_ENABLE_RSMIRQ();				// Wake-up interrupt
 104   1      
 105   1      	// The 8051 is responsible for all USB events, even those that have happened
 106   1      	// before this point.  We cannot ignore pending USB interrupts.
 107   1      	// The chip will come out of reset with the flags all cleared.
 108   1      	//	USBIRQ = 0xff;				// Clear any pending USB interrupt requests
 109   1      	PORTCCFG |= 0xc0;				// Turn on r/w lines for external memory 
 110   1      
 111   1      	USBBAV = USBBAV | 1 & ~bmBREAK;	// Disable breakpoints and autovectoring
 112   1      	USBIEN |= bmSUDAV | bmSUTOK | bmSUSP | bmURES;	// Enable selected interrupts
 113   1      	EA = 1;						// Enable 8051 interrupts
 114   1      
 115   1      	// This loop waits until we receive a setup packet from the host.
 116   1      	// NOTE: The device will continue to renumerate until it receives a setup
 117   1      	// packet.  This fixes a microsoft USB bug that loses disconnect/reconnect 
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 3   

 118   1      	// events during initial USB device driver configuration dialog box.
 119   1      	// B2 Load:  This code is not needed for B2 load, only for renumeration.
 120   1      	#ifndef NO_RENUM
 121   1      		while(!GotSUD)
 122   1      		{
 123   2      			if(!GotSUD)
 124   2      				EZUSB_Discon(TRUE);	// renumerate until setup received
 125   2      			for(j=0;(j<DELAY_COUNT) && (!GotSUD);++j);
 126   2      		}
 127   1      	#endif
 128   1      
 129   1      
 130   1      	CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration)
 131   1      
 132   1      	// Task Dispatcher
 133   1      	while(TRUE)					// Main Loop
 134   1      	{
 135   2      		if(GotSUD)				// Wait for SUDAV
 136   2      		{
 137   3      			SetupCommand();	 		// Implement setup command
 138   3        			GotSUD = FALSE;		   	// Clear SUDAV flag
 139   3      		}
 140   2      
 141   2      		// Poll User Device
 142   2      		// NOTE: Idle mode stops the processor clock.  There are only two
 143   2      		// ways out of idle mode, the WAKEUP pin, and detection of the USB
 144   2      		// resume state on the USB bus.  The timers will stop and the
 145   2      		// processor will not wake up on any other interrupts.
 146   2      		if (Sleep)
 147   2      		    {
 148   3          		if(TD_Suspend())
 149   3          		    { 
 150   4          		    Sleep = FALSE;	   		// Clear the "go to sleep" flag.  Do it here to prevent any race condition b
             -etween wakeup and the next sleep.
 151   4          		    do
 152   4          		        {
 153   5             			    EZUSB_Susp();			// Place processor in idle mode.
 154   5          		        }
 155   4                      while(!Rwuen && EZUSB_EXTWAKEUP());
 156   4                      // Must continue to go back into suspend if the host has disabled remote wakeup
 157   4                      // *and* the wakeup was caused by the external wakeup pin.
 158   4                      
 159   4          			// 8051 activity will resume here due to USB bus or Wakeup# pin activity.
 160   4          			EZUSB_Resume();	// If source is the Wakeup# pin, signal the host to Resume.		
 161   4          			TD_Resume();
 162   4          		    }   
 163   3      		    }
 164   2      		TD_Poll();
 165   2      	}
 166   1      }
 167          
 168          // Device request parser
 169          void SetupCommand(void)
 170          {
 171   1      	void	*dscr_ptr;
 172   1      	DWORD	i;
 173   1      
 174   1      	switch(SETUPDAT[0] & SETUP_MASK)
 175   1      	{
 176   2      		case SETUP_STANDARD_REQUEST:		//Standard Request
 177   2      			switch(SETUPDAT[1])
 178   2      			{
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 4   

 179   3      				case SC_GET_DESCRIPTOR:						// *** Get Descriptor
 180   3      					if(DR_GetDescriptor())
 181   3      						switch(SETUPDAT[3])			
 182   3      						{
 183   4      							case GD_DEVICE:				// Device
 184   4      								SUDPTRH = MSB(pDeviceDscr);
 185   4      								SUDPTRL = LSB(pDeviceDscr);
 186   4      								break;
 187   4      							case GD_CONFIGURATION:			// Configuration
 188   4      								if(dscr_ptr = (void *)EZUSB_GetConfigDscr(SETUPDAT[2]))
 189   4      								{
 190   5      									SUDPTRH = MSB(dscr_ptr);
 191   5      									SUDPTRL = LSB(dscr_ptr);
 192   5      								}
 193   4      								else
 194   4      									EZUSB_STALL_EP0(); 	// Stall End Point 0
 195   4      								break;
 196   4      							case GD_STRING:				// String
 197   4      								if(dscr_ptr = (void *)EZUSB_GetStringDscr(SETUPDAT[2]))
 198   4      								{
 199   5      									// Workaround for rev D errata number 8
 200   5      									// If you're certain that you will never run on rev D,
 201   5      									// you can just do this:
 202   5      									// SUDPTRH = MSB(dscr_ptr);
 203   5      									// SUDPTRL = LSB(dscr_ptr);
 204   5      									STRINGDSCR *sdp;
 205   5      									BYTE len;
 206   5      		
 207   5      									sdp = dscr_ptr;
 208   5      		
 209   5      									len = sdp->length;
 210   5      									if (len > SETUPDAT[6]) 
 211   5      										len = SETUPDAT[6]; //limit to the requested length
 212   5      									
 213   5      									while (len)
 214   5      									{
 215   6      										for(i=0; i<min(len,64); i++)
 216   6      											*(IN0BUF+i) = *((BYTE xdata *)sdp+i);
 217   6      		
 218   6      										//set length and arm Endpoint
 219   6      										EZUSB_SET_EP_BYTES(IN0BUF_ID,min(len,64));	
 220   6      										len -= min(len,64);
 221   6      		
 222   6      										// Wait for it to go out (Rev C and above)
 223   6      										while(EP0CS & 0x04)
 224   6      											;
 225   6      									}
 226   5      		
 227   5      									// Arm a 0 length packet just in case.  There was some reflector traffic about
 228   5      									// Apple hosts asking for too much data.  This will keep them happy and will
 229   5      									// not hurt valid hosts because the next SETUP will clear this.
 230   5      									EZUSB_SET_EP_BYTES(IN0BUF_ID,0);	
 231   5      									// Clear the HS-nak bit
 232   5      									EP0CS = bmHS;
 233   5      								}
 234   4      								else 
 235   4      									EZUSB_STALL_EP0();	// Stall End Point 0
 236   4      								break;
 237   4      							default:				// Invalid request
 238   4      								EZUSB_STALL_EP0();		// Stall End Point 0
 239   4      						}
 240   3      					break;
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 5   

 241   3      				case SC_GET_INTERFACE:						// *** Get Interface
 242   3      					DR_GetInterface();
 243   3      					break;
 244   3      				case SC_SET_INTERFACE:						// *** Set Interface
 245   3      					DR_SetInterface();
 246   3      					break;
 247   3      				case SC_SET_CONFIGURATION:					// *** Set Configuration
 248   3      					DR_SetConfiguration();
 249   3      					break;
 250   3      				case SC_GET_CONFIGURATION:					// *** Get Configuration
 251   3      					DR_GetConfiguration();
 252   3      					break;
 253   3      				case SC_GET_STATUS:						// *** Get Status
 254   3      					if(DR_GetStatus())
 255   3      						switch(SETUPDAT[0])
 256   3      						{
 257   4      							case GS_DEVICE:				// Device
 258   4      								IN0BUF[0] = ((BYTE)Rwuen << 1) | (BYTE)Selfpwr;
 259   4      								IN0BUF[1] = 0;
 260   4      								EZUSB_SET_EP_BYTES(IN0BUF_ID,2);
 261   4      								break;
 262   4      							case GS_INTERFACE:			// Interface
 263   4      								IN0BUF[0] = 0;
 264   4      								IN0BUF[1] = 0;
 265   4      								EZUSB_SET_EP_BYTES(IN0BUF_ID,2);
 266   4      								break;
 267   4      							case GS_ENDPOINT:			// End Point
 268   4      								IN0BUF[0] = EPIO[EPID(SETUPDAT[4])].cntrl & bmEPSTALL;
 269   4      								IN0BUF[1] = 0;
 270   4      								EZUSB_SET_EP_BYTES(IN0BUF_ID,2);
 271   4      								break;
 272   4      							default:				// Invalid Command
 273   4      								EZUSB_STALL_EP0();		// Stall End Point 0
 274   4      						}
 275   3      					break;
 276   3      				case SC_CLEAR_FEATURE:						// *** Clear Feature
 277   3      					if(DR_ClearFeature())
 278   3      						switch(SETUPDAT[0])
 279   3      						{
 280   4      							case FT_DEVICE:				// Device
 281   4      								if(SETUPDAT[2] == 1)
 282   4      									Rwuen = FALSE; 		// Disable Remote Wakeup
 283   4      								else
 284   4      									EZUSB_STALL_EP0();	// Stall End Point 0
 285   4      								break;
 286   4      							case FT_ENDPOINT:			// End Point
 287   4      								if(SETUPDAT[2] == 0)
 288   4      		                  {
 289   5      									EZUSB_UNSTALL_EP( EPID(SETUPDAT[4]) );
 290   5      		                     EZUSB_RESET_DATA_TOGGLE( SETUPDAT[4] );
 291   5      		                  }
 292   4      								else
 293   4      									EZUSB_STALL_EP0();	// Stall End Point 0
 294   4      								break;
 295   4      						}
 296   3      					break;
 297   3      				case SC_SET_FEATURE:						// *** Set Feature
 298   3      					if(DR_SetFeature())
 299   3      						switch(SETUPDAT[0])
 300   3      						{
 301   4      							case FT_DEVICE:				// Device
 302   4      								if(SETUPDAT[2] == 1)
C51 COMPILER V6.12  HID_FW                                                                 04/08/2003 17:13:30 PAGE 6   

 303   4      									Rwuen = TRUE;		// Enable Remote Wakeup
 304   4      								else
 305   4      									EZUSB_STALL_EP0();	// Stall End Point 0
 306   4      								break;
 307   4      							case FT_ENDPOINT:			// End Point
 308   4      								if(SETUPDAT[2] == 0)
 309   4      									EZUSB_STALL_EP( EPID(SETUPDAT[4]) );
 310   4      								else

⌨️ 快捷键说明

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