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

📄 usb_standard_requests.c

📁 USB CDC using C8051F320/340, virtual COM port thru usb connection
💻 C
📖 第 1 页 / 共 2 页
字号:
			default:
				break;
		}
	}
}

//-----------------------------------------------------------------------------
// Set_Address
//-----------------------------------------------------------------------------
//
// Set device address to the USB engine
//
//-----------------------------------------------------------------------------

static void Set_Address(void)
{
	if (   (Setup.bmRequestType == OUT_DEVICE)		// Request must be directed to device
		&& (Setup.wIndex.i      == 0)				// with index and length set to zero.
		&& (Setup.wLength.i     == 0)				// wValue holds the address, up to 0x7F
		&& (Setup.wValue.c[MSB] == 0) && ((Setup.wValue.c[LSB] & 0x80) == 0) )
	{
		if (Setup.wValue.c[LSB] != 0)
		{
			POLL_WRITE_BYTE(FADDR, Setup.wValue.c[LSB]);	// write new address to FADDR
															// SIE applies this address after status stage
			USB_State = DEV_ADDRESS;				// Indicate that device state is now address
		}
		else
		{
			USB_State = DEV_DEFAULT;				// If new address was 0x00, return device to default state
		}
		setup_handled = TRUE;
	}
}

//-----------------------------------------------------------------------------
// Get_Descriptor
//-----------------------------------------------------------------------------
//
// Return specified descriptor
//
// bmRequestType == DEVICE
//	wValue[MSB]	: DEVICE				device descriptor
//				: CONFIG				config descriptor
//				: STRING				string descriptor
//				: DEVICE_QUALIFIER		for HS support
//				: OTHER_SPEED_CONFIG	for HS support
//
// bmRequestType == INTERFACE
//	class specific descriptors
//
//-----------------------------------------------------------------------------

static void Get_Descriptor(void)
{
	if ( Setup.bmRequestType == IN_DEVICE )			// Request to device
	{
		switch(Setup.wValue.c[MSB])					// Determine which type of descriptor
		{											// was requested, and set data ptr and
			case DST_DEVICE:						// size accordingly
				if ( (Setup.wValue.c[LSB] == 0) && (Setup.wIndex.i == 0) )
				{
					DataPtr = (BYTE*)&DeviceDesc;
					DataSize = sizeof( Tdevice_descriptor );
					setup_handled = TRUE;
				}
				break;

			case DST_CONFIG:						// wValue.LSB holds config index
				if ( (Setup.wValue.c[LSB] == 0) && (Setup.wIndex.i == 0) )
				{
					DataPtr = (BYTE*)&ConfigDescSet;
					DataSize = sizeof( Tconfiguration_desc_set );
					setup_handled = TRUE;
				}
				break;

			case DST_STRING:						// wValue.LSB holds string index
													// wIndex holds language ID
				if ( Setup.wValue.c[LSB] < StringDescNum )
				{
					DataPtr = StringDescTable[Setup.wValue.c[LSB]];
					DataSize = *DataPtr;
					setup_handled = TRUE;
				}
				break;

			default:
				break;
		}
	}
/*
	else if ( Setup.bmRequestType == IN_INTERFACE )	// Request to Interface
	{
	}
*/
	if ( setup_handled )
	{
		Ep_Status0 = EP_TX;							// Put endpoint in transmit mode
		if ( DataSize > Setup.wLength.i )
			DataSize = Setup.wLength.i;				// Send just requested number of data
	}
}

//-----------------------------------------------------------------------------
// Get_Configuration
//-----------------------------------------------------------------------------
//
// This routine returns current configuration value (default: 0x01)
//
//-----------------------------------------------------------------------------

static void Get_Configuration(void)
{
	if (   (Setup.bmRequestType == IN_DEVICE)		// This request must be directed to the device
		&& (Setup.wValue.i  == 0)					// with value word set to zero
		&& (Setup.wIndex.i  == 0)					// and index set to zero
		&& (Setup.wLength.i == 1) )					// and setup length set to one
	{
		if (USB_State == DEV_CONFIGURED)			// If the device is configured, then return value 0x01
		{											// since this software supports only one configuration
			DataPtr = (BYTE*)&ONES_PACKET;
			setup_handled = TRUE;
		}
		if (USB_State == DEV_ADDRESS)				// If the device is in address state, it is not
		{											// configured, so return 0x00
			DataPtr = (BYTE*)&ZERO_PACKET;
			setup_handled = TRUE;
		}
	}
	
	if ( setup_handled )
	{
		// Set serviced Setup Packet, Endpoint 0 intransmit mode
		Ep_Status0 = EP_TX;						
		DataSize = 1;						
	}
}

//-----------------------------------------------------------------------------
// Set_Configuration
//-----------------------------------------------------------------------------
//
// Set up EPs and USB-related variables
//
//-----------------------------------------------------------------------------

// configuration conditions

// EP1

#if (defined USE_EP1_OUT) && (defined USE_EP1_IN) && ((defined C8051F320_H) || (defined C8051F340_H))
	#define EP1_CONFIG_SPLIT		rbInSPLIT			// split EP FIFO
#else
	#define EP1_CONFIG_SPLIT		0
#endif
#if !(defined USE_EP1_OUT) && (defined USE_EP1_IN) && ((defined C8051F320_H) || (defined C8051F340_H))
	#define EP1_CONFIG_IN			rbInDIRSEL			// set EP to IN
#else
	#define EP1_CONFIG_IN			0
#endif
#ifdef ENABLE_EP1_IN_DOUBLE_BUF
	#define EP1_CONFIG_IN_DBLBUF	rbInDBIEN			// enable double buffer
#else
	#define EP1_CONFIG_IN_DBLBUF	0
#endif
#ifdef ENABLE_EP1_IN_ISO
	#define EP1_CONFIG_IN_ISO		rbInISO				// set to isoc
#else
	#define EP1_CONFIG_IN_ISO		0
#endif

#ifdef ENABLE_EP1_OUT_DOUBLE_BUF
	#define EP1_CONFIG_OUT_DBLBUF	rbOutDBOEN			// enable double buffer
#else
	#define EP1_CONFIG_OUT_DBLBUF	0
#endif
#ifdef ENABLE_EP1_OUT_ISO
	#define EP1_CONFIG_OUT_ISO		rbOutISO			// set to isoc
#else
	#define EP1_CONFIG_OUT_ISO		0
#endif

// EP2

#if (defined USE_EP2_OUT) && (defined USE_EP2_IN)
	#define EP2_CONFIG_SPLIT		rbInSPLIT			// split EP FIFO
#else
	#define EP2_CONFIG_SPLIT		0
#endif
#if !(defined USE_EP2_OUT) && (defined USE_EP2_IN)
	#define EP2_CONFIG_IN			rbInDIRSEL			// set EP to IN
#else
	#define EP2_CONFIG_IN			0
#endif
#ifdef ENABLE_EP2_IN_DOUBLE_BUF
	#define EP2_CONFIG_IN_DBLBUF	rbInDBIEN			// enable double buffer
#else
	#define EP2_CONFIG_IN_DBLBUF	0
#endif
#ifdef ENABLE_EP2_IN_ISO
	#define EP2_CONFIG_IN_ISO		rbInISO				// set to isoc
#else
	#define EP2_CONFIG_IN_ISO		0
#endif

#ifdef ENABLE_EP2_OUT_DOUBLE_BUF
	#define EP2_CONFIG_OUT_DBLBUF	rbOutDBOEN			// enable double buffer
#else
	#define EP2_CONFIG_OUT_DBLBUF	0
#endif
#ifdef ENABLE_EP2_OUT_ISO
	#define EP2_CONFIG_OUT_ISO		rbOutISO			// set to isoc
#else
	#define EP2_CONFIG_OUT_ISO		0
#endif

// EP3

#if (defined USE_EP3_OUT) && (defined USE_EP3_IN)
	#define EP3_CONFIG_SPLIT		rbInSPLIT			// split EP FIFO
#else
	#define EP3_CONFIG_SPLIT		0
#endif
#if !(defined USE_EP3_OUT) && (defined USE_EP3_IN)
	#define EP3_CONFIG_IN			rbInDIRSEL			// set EP to IN
#else
	#define EP3_CONFIG_IN			0
#endif
#ifdef ENABLE_EP3_IN_DOUBLE_BUF
	#define EP3_CONFIG_IN_DBLBUF	rbInDBIEN			// enable double buffer
#else
	#define EP3_CONFIG_IN_DBLBUF	0
#endif
#ifdef ENABLE_EP3_IN_ISO
	#define EP3_CONFIG_IN_ISO		rbInISO				// set to isoc
#else
	#define EP3_CONFIG_IN_ISO		0
#endif

#ifdef ENABLE_EP3_OUT_DOUBLE_BUF
	#define EP3_CONFIG_OUT_DBLBUF	rbOutDBOEN			// enable double buffer
#else
	#define EP3_CONFIG_OUT_DBLBUF	0
#endif
#ifdef ENABLE_EP3_OUT_ISO
	#define EP3_CONFIG_OUT_ISO		rbOutISO			// set to isoc
#else
	#define EP3_CONFIG_OUT_ISO		0
#endif

#define EP1_EOUTCSRH	(EP1_CONFIG_OUT_DBLBUF | EP1_CONFIG_OUT_ISO)
#define EP2_EOUTCSRH	(EP2_CONFIG_OUT_DBLBUF | EP2_CONFIG_OUT_ISO)
#define EP3_EOUTCSRH	(EP3_CONFIG_OUT_DBLBUF | EP3_CONFIG_OUT_ISO)
#define EP1_EINCSRH	(EP1_CONFIG_IN_DBLBUF | EP1_CONFIG_IN_ISO | EP1_CONFIG_IN | EP1_CONFIG_SPLIT)
#define EP2_EINCSRH	(EP2_CONFIG_IN_DBLBUF | EP2_CONFIG_IN_ISO | EP2_CONFIG_IN | EP2_CONFIG_SPLIT)
#define EP3_EINCSRH	(EP3_CONFIG_IN_DBLBUF | EP3_CONFIG_IN_ISO | EP3_CONFIG_IN | EP3_CONFIG_SPLIT)

// Body of Set_Configuration

static void Set_Configuration(void)
{
	if (   (Setup.bmRequestType == OUT_DEVICE)		// This request must be directed to the device
		&& (Setup.wIndex.i  == 0)					// and index set to zero
		&& (Setup.wLength.i == 0) )					// and data length set to one
	{
		switch( Setup.wValue.c[LSB] )
		{
			case 0:
				USB_State = DEV_ADDRESS;			// Unconfigures device by setting state to
													// address, and changing endpoint 1, 2 and 3
													// status to halt
#ifdef USE_EP1_OUT_STATUS
				Ep_StatusOUT1 = EP_HALT;
#endif
#ifdef USE_EP2_OUT_STATUS
				Ep_StatusOUT2 = EP_HALT;
#endif
#ifdef USE_EP3_OUT_STATUS
				Ep_StatusOUT3 = EP_HALT;
#endif
#ifdef USE_EP1_IN_STATUS
				Ep_StatusIN1  = EP_HALT;
#endif
#ifdef USE_EP2_IN_STATUS
				Ep_StatusIN2  = EP_HALT;
#endif
#ifdef USE_EP3_IN_STATUS
				Ep_StatusIN3  = EP_HALT;
#endif
				setup_handled = TRUE;
				break;

			case 1:									// default configuration
				USB_State = DEV_CONFIGURED;

				// The endpoint regsiters, E0CSR, EINCSRL/H and EOUTCSRL/H are cleared by bus reset.
				// Set_Configuration is a good place to set these registers.
				// Also initialize USB-related variables here.
				// When the device has any alternate interface, initialize these registers in Set_Interface
				//
				// In 'F32x/34x, bus reset flushes all EP FIFOs, and set data toggle to DATA0
				// Then, data toggle setup and FIFO flush is not handled here in this implementation.

														// configure EPs
#if (defined USE_EP1_OUT) || (defined USE_EP1_IN)
				POLL_WRITE_BYTE(INDEX, 1);					// select EP1
	#if (EP1_EINCSRH != 0)
				POLL_WRITE_BYTE(EINCSRH, EP1_EINCSRH);		// set EINCSRH
	#endif
	#if (EP1_EOUTCSRH != 0)
				POLL_WRITE_BYTE(EOUTCSRH, EP1_EOUTCSRH);	// set EOUTCSRH
	#endif
	#if defined USE_EP1_OUT
				POLL_WRITE_BYTE(OUTMAX, EP1_OUT_PACKET_SIZE);	// set max packet size
	#endif
	#if defined USE_EP1_IN
				POLL_WRITE_BYTE(INMAX, EP1_IN_PACKET_SIZE);	// set max packet size
	#endif	
#endif

#if (defined USE_EP2_OUT) || (defined USE_EP2_IN)
				POLL_WRITE_BYTE(INDEX, 2);					// select EP2
	#if (EP2_EINCSRH != 0)
				POLL_WRITE_BYTE(EINCSRH, EP2_EINCSRH);		// set EINCSRH
	#endif
	#if (EP2_EOUTCSRH != 0)
				POLL_WRITE_BYTE(EOUTCSRH, EP2_EOUTCSRH);	// set EOUTCSRH
	#endif
	#if defined USE_EP2_OUT
				POLL_WRITE_BYTE(OUTMAX, EP2_OUT_PACKET_SIZE);	// set max packet size
	#endif
	#if defined USE_EP2_IN
				POLL_WRITE_BYTE(INMAX, EP2_IN_PACKET_SIZE);	// set max packet size
	#endif	
#endif

#if (defined USE_EP3_OUT) || (defined USE_EP3_IN)
				POLL_WRITE_BYTE(INDEX, 3);					// select EP3
	#if (EP3_EINCSRH != 0)
				POLL_WRITE_BYTE(EINCSRH, EP3_EINCSRH);		// set EINCSRH
	#endif
	#if (EP3_EOUTCSRH != 0)
				POLL_WRITE_BYTE(EOUTCSRH, EP3_EOUTCSRH);	// set EOUTCSRH
	#endif
	#if defined USE_EP3_OUT
				POLL_WRITE_BYTE(OUTMAX, EP3_OUT_PACKET_SIZE);	// set max packet size
	#endif
	#if defined USE_EP3_IN
				POLL_WRITE_BYTE(INMAX, EP3_IN_PACKET_SIZE);	// set max packet size
	#endif	
#endif
													// set EP status to IDLE
#ifdef USE_EP1_OUT_STATUS
				Ep_StatusOUT1 = EP_IDLE;
#endif
#ifdef USE_EP2_OUT_STATUS
				Ep_StatusOUT2 = EP_IDLE;
#endif
#ifdef USE_EP3_OUT_STATUS
				Ep_StatusOUT3 = EP_IDLE;
#endif
#ifdef USE_EP1_IN_STATUS
				Ep_StatusIN1  = EP_IDLE;
#endif
#ifdef USE_EP2_IN_STATUS
				Ep_StatusIN2  = EP_IDLE;
#endif
#ifdef USE_EP3_IN_STATUS
				Ep_StatusIN3  = EP_IDLE;
#endif
													// initialize USB-related variables
				IN1_FIFO_empty   = TRUE;
				IN2_FIFO_empty   = TRUE;
				IN3_FIFO_empty   = TRUE;
				OUT1_FIFO_loaded = FALSE;
				OUT2_FIFO_loaded = FALSE;
				OUT3_FIFO_loaded = FALSE;

				cs_Line_State_Update = TRUE;		// Send current line status
				CDC_Handle_INT_IN();
				Flush_COMbuffers();					// Cleanup buffers

				setup_handled = TRUE;
				break;

			default:
				break;
		}
	}
}

//-----------------------------------------------------------------------------
// Get_Interface
//-----------------------------------------------------------------------------
//
// Return current interface (default: 0x00)
//
//-----------------------------------------------------------------------------

static void Get_Interface(void)
{
	if (   (USB_State == DEV_CONFIGURED)			// If device is configured
		&& (Setup.bmRequestType == IN_INTERFACE)	// and recipient is an interface
		&& (Setup.wValue.i  == 0)					// and wValue equals to 0
		&& (Setup.wIndex.i < DSC_NUM_INTERFACE)		// and valid interface index
		&& (Setup.wLength.i == 1) )					// and data length equals to one
	{
		DataPtr = (BYTE*)&ZERO_PACKET;				// return 0x00 to host
		Ep_Status0 = EP_TX;
		DataSize = 1;
		setup_handled = TRUE;
	}
}

//-----------------------------------------------------------------------------
// Set_Interface
//-----------------------------------------------------------------------------
//
// Switch the interface to one of alternate interface
// When the device has no alternate IF, just reutrn STALL
//
//-----------------------------------------------------------------------------

/*
static void Set_Interface(void)
{
	// return STALL when no alternate interface
	if (   (Setup.bmRequestType == OUT_INTERFACE)
		&& (Setup.wValue.i == 0 )		// wValue holds alternate interface index
		&& (Setup.wIndex.i < DSC_NUM_INTERFACE)
		&& (Setup.wLength.i == 0) )
	{
		// Indicate setup packet has been serviced
		setup_handled = TRUE;
	}
}
*/

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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