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

📄 gphidapp.c

📁 基于sy2100 ez-usb开发板的程序
💻 C
字号:
/*-----------------------------------------------------------------------------
	File:		ghHIDapp.c	27-Aug-01	LTH 
	Contents:	General Purpose HID device with following characteristics:
				1. VID=0547h, PID=7450h, DID=0001h
				2. Input and Output reports are 2 bytes
				3. HID input reports return:
					a. The number of output reports received
					b. EZ-USB Dev board switches
				6. The background program constantly displays the last output
					report hex digits received in the 7-seg readout.
			  	7. Five strings; manuf(1),prod(2),s/n(3),cfg(4), and iface(5.

	Copyright (c) 2001 Cypress Semiconductor, Inc. All rights reserved
------------------------------------------------------------------------------*/
#pragma intvector (0x17FD)
#pragma interval(4)			// put USB (INT2) interrupt vector table at 1800,1804...
#include "ezusb.h"
#include "ezregs.h"

extern BOOL	GotSUD;			// Received setup data flag
extern BOOL	Sleep;
extern BOOL	Rwuen;
extern BOOL	Selfpwr;
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
#define ButtonAddr		0x41
#define	seg7Addr		0x42
#define bmF1			0x01
#define	bmF2			0x02
#define bmF3			0x04
#define bmF4			0x08
#define	bmDIP1			0x10
#define	bmDIP2			0x20
//-----------------------------------------------------------------------------
// Variables
//-----------------------------------------------------------------------------
BYTE	Configuration;			// Current configuration
BYTE	AlternateSetting;		// Alternate settings
BYTE	buttons,oldbuttons;
BYTE	leds=0xFF;
BYTE	dp,old_dp;				// decimal point
char data Digit[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e};
int tick,count,timeconst;
#define TCMS 500				// Time constant in milliseconds (incremented in SOF ISR)
//-----------------------------------------------------------------------------
// Function prototypes
//-----------------------------------------------------------------------------
BYTE read_buttons(void);	// Read the Dev Board buttons thru i2c port
void display (BYTE);		// Update the 7-seg readout thru i2c port
//-----------------------------------------------------------------------------
// Task Dispatcher (TD) hooks
//	The following functions are called by the task dispatcher.
//-----------------------------------------------------------------------------
void TD_Init(void) 			// Called once at startup
{
	IN07VAL = bmEP1;		// Enable our EP's: EP1-IN & EP1-OUT
	OUT07VAL = bmEP1;
	USBIEN = bmSOF;			// Enable SOF interrupt
	display(0x7F);			// 7-seg readout--decimal point only
	timeconst = TCMS;		// initial value
	old_dp = 0;				// used to detect change in decimal point value
	IN1BUF[0] = 0;
	IN1BUF[1] = 0;
	IN1BC = 2;				// Arm EP1-in in case an IN arrives before an OUT (W2K)	
}

void TD_Poll(void) 		// Called repeatedly while not handling SETUP packets
{
	EUSB=0;				// turn off USB (SOF) interrupt 
	if (tick > timeconst)	// see if timer has hit max
		{
		tick=0;
		count++;
		count &= 0x0F;
		display(Digit[count]);	// look up segments
		IN1BUF[0] = count;		// update the display value
		}
	EUSB=1;				// USB interrupts back on
//
		buttons = read_buttons();
		if (buttons == read_buttons())	//Debounce
		{
			if ((oldbuttons - buttons) != 0)	//Change in button state
			oldbuttons = buttons;
		}
	
	if(!(OUT1CS & bmEPBUSY))	// Is there something available?
	{
		timeconst = OUT1BUF[0]*20;		// Output Report Byte 0 is time const (tenths of sec)
		dp = OUT1BUF[1];		// decimal point
		OUT1BC = 0;				// Re-arm EP1-OUT
		IN1BUF[0] = count;
		IN1BUF[1] = buttons;
		IN1BC = 2;				// arm the EP2-IN transfer
		if (dp != old_dp)		// update the display NOW if dp data changed
			{
				old_dp = dp;	// update stored value
				display(Digit[count]);	// look up segments
			}
	}
}

BYTE read_buttons (void)	// Read the Dev board buttons
{							// Make available in 4 lsb's, active TRUE
	BYTE d;
	while (I2CS & 0x40);	//Wait for stop complete
	I2CS = 0x80;			//Set start condition
	I2DAT = ButtonAddr;		//Write button address
	while (!(I2CS & 0x01));	//Wait for done
	I2CS = 0x20;			//Set last read
	d = I2DAT;				//trigger last read
	while (!(I2CS & 0x01));	//Wait for done
	I2CS = 0x40;			//Set stop bit
	d = ~I2DAT;				// Active TRUE
 	d &= 0x0F;				// 4 LSB's only
	return(d);				//Return the 4 PB values in active-true form
}

void display (BYTE d)
{
	d &= 0x7F;				// Clear the MSB
	d |= (dp & 0x80);		// OR in the decimal point (MSB)
	while (I2CS & 0x40);	//Wait for stop to be done
	I2CS = 0x80;			//Set start condition
	I2DAT = seg7Addr;		//Write led address
	while (!(I2CS & 0x01));	//Wait for done
	I2DAT = d;				//Write data
	while (!(I2CS & 0x01));	//Wait for done
	I2CS = 0x40;			//Set stop bit
}

BOOL TD_Suspend(void) 			// Called before the device goes into suspend mode
{
	return(TRUE);
}

BOOL TD_Resume(void) 			// Called after the device resumes
{
	return(TRUE);
}
//-----------------------------------------------------------------------------
// Device Request hooks
//	The following functions are called by the endpoint 0 device request parser.
//-----------------------------------------------------------------------------
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);
}

//-----------------------------------------------------------------------------
// USB Interrupt Handlers
//	The following functions are called by the USB interrupt jump table.
//-----------------------------------------------------------------------------
void ISR_Sudav(void) interrupt 0	// Setup Data Available
{
	GotSUD = TRUE;					// Set flag
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSUDAV;				// Clear SUDAV IRQ
}

void ISR_Sutok(void) interrupt 2	// Setup token (not used)
{
}

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

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

//void ISR_IBN(void) interrupt 5  (Not in AN2131)
//{
//}

void ISR_Susp(void) interrupt 3
{
	Sleep = TRUE;				// Just set a flag for fw2.c to check.
	EZUSB_IRQ_CLEAR();
	USBIRQ = bmSUSP;
}

void ISR_Ep0in(void) interrupt 6
{
	EZUSB_IRQ_CLEAR();		// clear USB (INT2) request
	IN07IRQ = bmEP0;		// clear EP0-IN int request
}

void ISR_Ep0out(void) interrupt 7
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP0;
}

void ISR_Ep1in(void) interrupt 8
{
	EZUSB_IRQ_CLEAR();	
	IN07IRQ = bmEP1;
	
}

void ISR_Ep1out(void) interrupt 9
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP1;
	
}

void ISR_Ep2in(void) interrupt 0x0A
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP2;
}

void ISR_Ep2out(void) interrupt 0x0B
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP2;
}

void ISR_Ep3in(void) interrupt 0x0C
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP3;
}

void ISR_Ep3out(void) interrupt 0x0D
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP3;
}

void ISR_Ep4in(void) interrupt 0x0E
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP4;
}

void ISR_Ep4out(void) interrupt 0x0F
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP4;
}

void ISR_Ep5in(void) interrupt 0x10
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP5;
}

void ISR_Ep5out(void) interrupt 0x11
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP5;
}

void ISR_Ep6in(void) interrupt 0x12
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP6;
}

void ISR_Ep6out(void) interrupt 0x13
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP6;
}

void ISR_Ep7in(void) interrupt 0x14
{
	EZUSB_IRQ_CLEAR();
	IN07IRQ = bmEP7;
}

void ISR_Ep7out(void) interrupt 0x15
{
	EZUSB_IRQ_CLEAR();
	OUT07IRQ = bmEP7;
}

⌨️ 快捷键说明

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