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

📄 usb2ser.c

📁 usb serial converter
💻 C
📖 第 1 页 / 共 2 页
字号:
						localRTS = 1;
					}
				}
				#endif
		 	}
			else							// No data in txBuffer[]
			{
				#if (DEBUG)
				OUTA ^= 0x80;			
				#endif
	
				#if (RTS_MODE == 1)
				localRTS = 0;			
				#endif
			}
		}
	}

 //
 // FROM RS-232
 //

 	if (RI)
	{
		#if (DEBUG)
		OUTA ^= 0x10;	
		#endif
	
		nextRxInPtr = (rxInPtr + 1) % RX_BUFFER_SIZE;

		if (nextRxInPtr == rxOutPtr)		// If rxBuffer[] full 
		{
			#if (RTS_MODE == 2)
			localRTS = 0;
			#endif
		}
		else
		{
			#if (STRICT_RTS_SPEC)
			if (!CTSisOn)
		 	#endif
			{
				#if (RTS_MODE == 2)
				localRTS = 1;
				#endif
			}

			#if (RTS_MODE == 2)
		 	if (RTSisOn)
			#endif
			{
				rxBuffer[rxInPtr] = SBUF0;
				rxInPtr = nextRxInPtr;

				RI = 0;
			}
		}
	}
}

//////////////////////////////
//                          //
// Setup the UART Baud Rate //
//                          //
//////////////////////////////

void setup_uart(BYTE b[])
{
BYTE thi, tlo;

// setup UART registers
// using timer2 for minimal baud rate error

	thi = 0xFF;					// Assume baudrate > 2400, so thi = 0xFF.

	switch(b[1])				// Set tlo (and thi, for baud rates <= 2400)
	{
		case BAUDRATE_57600:
			tlo = 0xF3;	
			break;

		case BAUDRATE_38400:	
			tlo = 0xEC;
			break;
		
		case BAUDRATE_28800:
			tlo = 0xE6;
			break;

		case BAUDRATE_19200:
			tlo = 0xD9;
			break;

		case BAUDRATE_9600:
			tlo = 0xB2;
			break;
		
		case BAUDRATE_4800:
			tlo = 0x64;
			break;
		
		case BAUDRATE_2400:
			thi = 0xFE;
			tlo = 0xC8;
			break;

		case BAUDRATE_1200:
			thi = 0xFD;
			tlo = 0x8F;
			break;

		case BAUDRATE_300:
			thi = 0xF6;
			tlo = 0x3C;
			break;

		default:
			tlo = 0xB2; 		// 9600 baud
			break;
	}
				
	RCAP2H = thi; 				// auto-reload, baudrate determined by set_report class request
	RCAP2L = tlo;				// auto-reload, baudrate determined by set_report class request
	T2CON = 0x34;				// overflow clk, start timer

	#if (PARITY)
	SCON0 |=0xD0;				// mode 3 (parity)
	#else
	SCON0 |=0x50;				// mode 1 (no parity)
	#endif
}

//
// This function is called before the device goes into suspend mode.
//

BOOL TD_Suspend(void)
{
	// Turn off the breakpoint light before entering suspend
	
	USBBAV |= bmBREAK;				// Clear the breakpoint
	return(TRUE);
}

//
// This function is called after the device resumes from suspend mode.
//

BOOL TD_Resume(void)
{
	return(TRUE);
}

//-----------------------------------------------------------------------------
// Device Request hooks
//	The following hooks are called by the end point 0 device request parser.
//-----------------------------------------------------------------------------

BOOL DR_ClassRequest(void)
{
BYTE i;				

BYTE cntl_report[5];					// buffer to contain EP0 data

BYTE baudrate[4];						// buffer to contain baud rate information
BYTE out_cntl_flags;
BYTE bc;												// EP0 byte count

	switch(SETUPDAT[1])									// check to see what class request is being handled
	{
		case CR_SET_REPORT:								// set_report request
			OUT0BC = 0;									// arm endpoint zero to handle data phase
			
			while(EP0CS & 0x08);						// wait for busy bit to be cleared

			bc = OUT0BC; 								// Get the new bytecount

			for(i=0; i<bc; i++)
			{
				cntl_report[i] = OUT0BUF[i];			// get configuration data from host					
			}
		
			baudrate[1] = cntl_report[1];				// gets high byte of baudrate and will use this to determine 
			baudrate[0] = cntl_report[0];				// timer-reload value			
			setup_uart(baudrate);						// setup UART baud rate
			out_cntl_flags = cntl_report[4];			// set flags in the control OUT transfer
	
			return (FALSE);
			break;
		
		case CR_GET_REPORT:								// get_report request
			for(i=0; i<5; i++)
			{				
				IN0BUF[i] = cntl_report[i];				// transmits configuration data to host
			}
			IN0BC = 5;						
			return (FALSE);
			break;
	   	
	   	default:
			return (TRUE);
	}	
}

BOOL DR_GetDescriptor(void)
{
	BYTE length,i;

	pHIDDscr = (WORD)&HIDDscr;
	pReportDscr = (WORD)&ReportDscr;
	pReportDscrEnd = (WORD)&ReportDscrEnd;

	switch (SETUPDAT[3])
	{
		case GD_HID:									//HID Descriptor 

			SUDPTRH = MSB(pHIDDscr);
			SUDPTRL = LSB(pHIDDscr);
			
			return (FALSE);

		case GD_REPORT:									//Report Descriptor

			length = pReportDscrEnd - pReportDscr;
	
			while (length)
			{
				for(i=0; i<min(length,64); i++)
					*(IN0BUF+i) = *((BYTE xdata *)pReportDscr+i);
	
				//set length and arm Endpoint
				EZUSB_SET_EP_BYTES(IN0BUF_ID,min(length,64));	
				length -= min(length,64);
	
				// Wait for it to go out (Rev C and above)
				while(EP0CS & 0x04) {}
			}

			return (FALSE);

		default:

			return(TRUE);
	}
}

BOOL DR_SetConfiguration(void)	// Called when a Set Configuration command is received
{
	Configuration = SETUPDAT[2];

	return(TRUE);				// Handled by user code
}

BOOL DR_GetConfiguration(void)	// Called when a Get Configuration command is received
{
	IN0BUF[0] = Configuration;
	EZUSB_SET_EP_BYTES(IN0BUF_ID,1);
	return(TRUE);				// Handled by user code
}

BOOL DR_SetInterface(void) 		// Called when a Set Interface command is received
{
	AlternateSetting = SETUPDAT[2];
	return(TRUE);				// Handled by user code
}

BOOL DR_GetInterface(void) 		// Called when a Set Interface command is received
{
	IN0BUF[0] = AlternateSetting;
	EZUSB_SET_EP_BYTES(IN0BUF_ID,1);
	return(TRUE);				// Handled by user code
}

BOOL DR_GetStatus(void)
{
	return(TRUE);
}

BOOL DR_ClearFeature(void)
{
	return(TRUE);
}

BOOL DR_SetFeature(void)
{
	return(TRUE);
}

BOOL DR_VendorCmnd(void)
{
	return(TRUE);
}

//-----------------------------------------------------------------------------
// USB Interrupt Handlers
//	The following functions are called by the USB interrupt jump table.
//-----------------------------------------------------------------------------

// Setup Data Available Interrupt Handler
void ISR_Sudav(void) interrupt 0
{
	GotSUD = TRUE;				// Set flag
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSUDAV;			// Clear SUDAV IRQ
}

// Setup Token Interrupt Handler
void ISR_Sutok(void) interrupt 0
{
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSUTOK;			// Clear SUTOK IRQ
}

void ISR_Sof(void) interrupt 0
{
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSOF;				// Clear SOF IRQ
}

void ISR_Ures(void) interrupt 0
{
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmURES;			// Clear URES IRQ
}

void ISR_IBN(void) interrupt 0
{
   // ISR for the IN Bulk NAK (IBN) interrupt.
}

void ISR_Susp(void) interrupt 0
{
	Sleep = TRUE;
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSUSP;
}

void ISR_Ep0in(void) interrupt 0
{
}

void ISR_Ep0out(void) interrupt 0
{
}

void ISR_Ep1in(void) interrupt 0
{
}

void ISR_Ep1out(void) interrupt 0
{
}

void ISR_Ep2in(void) interrupt 0
{
}

void ISR_Ep2out(void) interrupt 0
{
}

void ISR_Ep3in(void) interrupt 0
{
}

void ISR_Ep3out(void) interrupt 0
{
}

void ISR_Ep4in(void) interrupt 0
{
}

void ISR_Ep4out(void) interrupt 0
{
}

void ISR_Ep5in(void) interrupt 0
{
}

void ISR_Ep5out(void) interrupt 0
{
}

void ISR_Ep6in(void) interrupt 0
{
}

void ISR_Ep6out(void) interrupt 0
{
}

void ISR_Ep7in(void) interrupt 0
{
}

void ISR_Ep7out(void) interrupt 0
{
}


⌨️ 快捷键说明

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