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

📄 __chap9.c

📁 C语言版的USB单片机固件源代码
💻 C
字号:
//===================================================================//
//     Project Name : ZBoard
//      Module Name : Master Firmware Program
//     Product Type : License
//      OS/Dev Tool : AT89C52, uVision 2
//  Original Author : Ray Yang
//     Organization : YBWork.com
//    Original Date : July, 25, 2001
//             Addr : Room 402, No. 131, Meilong No. 9
//              TEL : 86-21-54630718
//            EMail : rayyang2000@yahoo.com
//          Website : http://www.ybwork.com
//		  Copyright : Copyright (L) YBWork.com, 2001
//         Comments : 
//                    
//					<< History >> 
//					July, 25, 2001		The first release
//===================================================================//

#include "HAL.H"
#include "USBD12.H"
#include "ZBoard.H"
#include "USB110.H"
#include "USB.H"
#include "CHAP9.H"

#define NUM_ENDPOINTS	4

#define CONFIG_DESCRIPTOR_LENGTH    sizeof(USB_CONFIGURATION_DESCRIPTOR) \
	       				+ sizeof(USB_INTERFACE_DESCRIPTOR) \
					+ (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))


extern CONTROL_XFER		ControlData;
extern ZBOARDFLAGS bZBoardFlags;

code USB_DEVICE_DESCRIPTOR DeviceDescr =
{
	sizeof(USB_DEVICE_DESCRIPTOR),	// bLength
	USB_DEVICE_DESCRIPTOR_TYPE,	// bDescriptorType
	SWAP(0x0110),			// bcdUSB
	0xff,				// bDeviceClass
	0xff,				// bDeviceSubClass
	0xff,				// bDeviceProtocol
	EP0_PACKET_SIZE,		// bMaxPacketSize
	SWAP(0x0471),			// idVendor
	SWAP(0x1801),			// idProduct
	SWAP(0x0010),			// bcdDevice
	1,				// iManufacturer
	2,				// iProduct
	3,				// iSerialNumber
	1				// bNumConfigurations
};

code USB_CONFIGURATION_DESCRIPTOR ConfigDescr =
{
	sizeof(USB_CONFIGURATION_DESCRIPTOR),	// bLength
	USB_CONFIGURATION_DESCRIPTOR_TYPE,	// bDescriptorType
	SWAP(CONFIG_DESCRIPTOR_LENGTH),		// wTotalLength
	1,					// bNumInterfaces
	1,					// bConfigurationValue
	4,					// iConfiguration
	0x80,					// bmAttributes
	100					// MaxPower
};

code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
{
	sizeof(USB_INTERFACE_DESCRIPTOR),	// bLength
	USB_INTERFACE_DESCRIPTOR_TYPE,		// bDescriptorLength
	0,					// bInterfaceNumber
	0,					// bAlternateSetting
	NUM_ENDPOINTS,				// bNumEndpoints
	0xff,					// bInterfaceClass
	0xff,					// bInterfaceSubClass
	0xff,					// bInterfaceProtocol
	5					// iInterface
};

code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),	// bLength
	USB_ENDPOINT_DESCRIPTOR_TYPE,		// bDescriptorType
	0x81,					// bEndpointAddress
	USB_ENDPOINT_TYPE_BULK,			// bmAttributes
	SWAP(EP1_PACKET_SIZE),			// wMaxPacketSize
	0					// bInterval
};

code USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),	// bLength
	USB_ENDPOINT_DESCRIPTOR_TYPE,		// bDescriptorType
	0x1,					// bEndpointAddress
	USB_ENDPOINT_TYPE_BULK,			// bmAttributes
	SWAP(EP1_PACKET_SIZE),			// wMaxPacketSize
	0					// bInterval
};

code USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),	// bLength
	USB_ENDPOINT_DESCRIPTOR_TYPE,		// bDescriptorType
	0x82,					// bEndpointAddress
	USB_ENDPOINT_TYPE_BULK,			// bmAttributes
	SWAP(EP2_TX_FIFO_SIZE),			// wMaxPacketSize
	0					// bInterval
};

code USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),	// bLength
	USB_ENDPOINT_DESCRIPTOR_TYPE,		// bDescriptorType
	0x2,					// bEndpointAddress
	USB_ENDPOINT_TYPE_BULK,			// bmAttributes
	SWAP(EP2_RX_FIFO_SIZE),			// wMaxPacketSize
	0					// bInterval
};

code unsigned char StringLANGID[] = 
{
	4,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	0x09, 0x04//SWAP(0x0409)		// wLANGID
};

code unsigned char szManufacturer[] =
{
	66,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'Y', 0, 'B', 0, 'W', 0, 'o', 0, 'r', 0, 'k', 0, '.', 0, 'c', 0, 'o', 0, 'm', 0,
	' ', 0, 'r', 0, 'a', 0, 'y', 0, 'y', 0, 'a', 0, 'n', 0, 'g', 0, '2', 0, '0', 0,
	'0', 0, '0', 0, '@', 0, 'y', 0, 'a', 0, 'h', 0, 'o', 0, 'o', 0, '.', 0, 'c', 0,
	'o', 0, 'm', 0
};

code unsigned char szProduct[] =
{
	46,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'Z', 0, 'B', 0, 'o', 0, 'a', 0, 'r', 0, 'd', 0, ' ', 0, '(', 0, 'L', 0, ')', 0,
	' ', 0, 'R', 0, '&', 0, 'D', 0, ' ', 0, 'P', 0, 'a', 0, 'c', 0, 'k', 0, 'a', 0,
	'g', 0, 'e', 0
};

code unsigned char szSerialNumber[] =
{
	22,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'0', 0, '0', 0, '0', 0, '0', 0, '-', 0, '0', 0, '0', 0, '0', 0, '0', 0, '0', 0
};

code unsigned char szConfiguration[] =
{
	98,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'T', 0, 'h', 0, 'i', 0, 's', 0, ' ', 0, 'c', 0, 'o', 0, 'n', 0, 'f', 0, 'i', 0,
	'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, 'o', 0, 'n', 0, ' ', 0, 'i', 0,
	's', 0, ' ', 0, 'u', 0, 's', 0, 'e', 0, 'd', 0, ' ', 0, 'f', 0, 'o', 0, 'r', 0,
	' ', 0, 'Z', 0, 'B', 0, 'o', 0, 'a', 0, 'r', 0, 'd', 0, ' ', 0, 'p', 0, 'r', 0,
	'o', 0, 'g', 0, 'r', 0, 'a', 0, 'm', 0, 'm', 0, 'e', 0, 'r', 0
};

code unsigned char szInterface[] =
{
	70,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'I', 0, 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0, ' ', 0,
	't', 0, 'o', 0, ' ', 0, 't', 0, 'h', 0, 'e', 0, ' ', 0, 'Z', 0, 'B', 0, 'o', 0,
	'a', 0,	'r', 0, 'd', 0, ' ', 0, 'p', 0, 'r', 0, 'o', 0, 'g', 0, 'r', 0, 'a', 0,
	'm', 0,	'm', 0, 'e', 0, 'r', 0
};

code unsigned char szUserInfo[] =
{
	114,					// bLength
	USB_STRING_DESCRIPTOR_TYPE,		// bDescriptorType
	// String...
	'T', 0, 'h', 0, 'e', 0, ' ', 0, 'o', 0, 'r', 0, 'i', 0, 'g', 0, 'i', 0, 'n', 0,
	'a', 0, 'l', 0,	' ', 0, 'u', 0, 's', 0, 'e', 0, 'r', 0, ' ', 0, 'o', 0, 'f', 0,
	' ', 0,	't', 0,	'h', 0, 'i', 0, 's', 0, ' ', 0, 'b', 0, 'o', 0, 'a', 0, 'r', 0,
	'd', 0,	' ', 0,	'i', 0, 's', 0, ' ', 0, 'r', 0, 'a', 0,	'y', 0, 'y', 0,	'a', 0,
	'n', 0,	'g', 0,	'2', 0, '0', 0, '0', 0, '0', 0, '@', 0,	'y', 0, 'a', 0,	'h', 0,
	'o', 0,	'o', 0,	'.', 0, 'c', 0, 'o', 0, 'm', 0
};

/*
   //*************************************************************************
   // USB Protocol Layer
   //*************************************************************************
*/

void reserved(void)
{
    USB_Stall_EP0();
}

/*
   //*************************************************************************
   // USB standard device requests
   //*************************************************************************
*/
void USBClassRequest_GetStatus(void)
{
	unsigned char endp, txdat[2];
	unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;
	unsigned char c;

	if (bRecipient == USB_RECIPIENT_DEVICE) {
		if(bZBoardFlags.bits.bRemote_Wakeup == 1)
			txdat[0] = 3;
		else
			txdat[0] = 1;
		txdat[1]=0;
		USB_Single_Transmit(txdat, 2);
	} else if (bRecipient == USB_RECIPIENT_INTERFACE) {
		txdat[0]=0;
		txdat[1]=0;
		USB_Single_Transmit(txdat, 2);
	} else if (bRecipient == USB_RECIPIENT_ENDPOINT) {
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
			c = USBD12_SelectEndpoint(endp*2 + 1);	/* Control-in */
		else
			c = USBD12_SelectEndpoint(endp*2);	/* Control-out */
		if(c & USBD12_STALL)	/*USBD12_STALL=0x02*/
			txdat[0] = 1;
		else
			txdat[0] = 0;
		txdat[1] = 0;
		USB_Single_Transmit(txdat, 2);
	} else
		USB_Stall_EP0();
}

void USBClassRequest_ClearFeature(void)
{
	unsigned char endp;
	unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;

	if (bRecipient == USB_RECIPIENT_DEVICE
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) 
	{
		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bRemote_Wakeup = 0;
		ENABLE_INTERRUPTS;
		USB_Single_Transmit(0, 0);
	}
	else if (bRecipient == USB_RECIPIENT_ENDPOINT
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) 
	{
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
			/* clear TX stall for IN on EPn. */
			USBD12_SetEndpointStatus(endp*2 + 1, 0);
		else
			/* clear RX stall for OUT on EPn. */
			USBD12_SetEndpointStatus(endp*2, 0);
		USB_Single_Transmit(0, 0);
	} 
	else
		USB_Stall_EP0();
}

void USBClassRequest_SetFeature(void)
{
	unsigned char endp;
	unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & USB_RECIPIENT;

	if (bRecipient == USB_RECIPIENT_DEVICE
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_REMOTE_WAKEUP) {
		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bRemote_Wakeup = 1;
		ENABLE_INTERRUPTS;
		USB_Single_Transmit(0, 0);
	}
	else if (bRecipient == USB_RECIPIENT_ENDPOINT
		&& ControlData.DeviceRequest.wValue == USB_FEATURE_ENDPOINT_STALL) {
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & MAX_ENDPOINTS);
		if (ControlData.DeviceRequest.wIndex & (unsigned char)USB_ENDPOINT_DIRECTION_MASK)
			/* clear TX stall for IN on EPn. */
			USBD12_SetEndpointStatus(endp*2 + 1, 1);
		else
			/* clear RX stall for OUT on EPn. */
			USBD12_SetEndpointStatus(endp*2, 1);
		USB_Single_Transmit(0, 0);
	} else
		USB_Stall_EP0();
}

void USBClassRequest_SetAddress(void)
{
	USBD12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue &
		DEVICE_ADDRESS_MASK), 1);
	USB_Single_Transmit(0, 0);
}

void USBClassRequest_GetDescriptor(void)
{
	unsigned char bDescriptor = MSB(ControlData.DeviceRequest.wValue);

	if(bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE)
	{
		USB_Code_Transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
	}
	else if(bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE)
	{
		USB_Code_Transmit((unsigned char code *)&ConfigDescr, CONFIG_DESCRIPTOR_LENGTH);
	}
	else if(bDescriptor == USB_STRING_DESCRIPTOR_TYPE)
	{
		switch(LSB(ControlData.DeviceRequest.wValue))
		{
		case 0:
			USB_Code_Transmit((unsigned char code *)&StringLANGID, StringLANGID[0]);
			break;
		case 1:
			USB_Code_Transmit((unsigned char code *)szManufacturer, szManufacturer[0]);
			break;
		case 2:
			USB_Code_Transmit((unsigned char code *)szProduct, szProduct[0]);
			break;
		case 3:
			USB_Code_Transmit((unsigned char code *)szSerialNumber, szSerialNumber[0]);
			break;
		case 4:
			USB_Code_Transmit((unsigned char code *)szConfiguration, szConfiguration[0]);
			break;
		case 5:
			USB_Code_Transmit((unsigned char code *)szInterface, szInterface[0]);
			break;
		case 6:
			USB_Code_Transmit((unsigned char code *)szUserInfo, szUserInfo[0]);
			break;
		default:
			USB_Code_Transmit((unsigned char code *)&StringLANGID, 4);
			break;
		}
	}
	else
	{
		USB_Stall_EP0();
	}
}

void USBClassRequest_GetConfiguration(void)
{
	unsigned char c = bZBoardFlags.bits.bConfiguration;

	USB_Single_Transmit(&c, 1);
}

void USBClassRequest_SetConfiguration(void)
{
	if (ControlData.DeviceRequest.wValue == 0) 
	{
		/* put device in unconfigured state */
		USB_Single_Transmit(0, 0);
		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bConfiguration = 0;
		ENABLE_INTERRUPTS;
		USB_Init_Unconfig();
	} 
	else if (ControlData.DeviceRequest.wValue == 1) 
	{
		/* Configure device */
		USB_Single_Transmit(0, 0);

		USB_Init_Unconfig();
		USB_Init_Config();
		
		DISABLE_INTERRUPTS;
		bZBoardFlags.bits.bConfiguration = 1;
		ENABLE_INTERRUPTS;
	} 
	else
		USB_Stall_EP0();
}

void USBClassRequest_GetInterface(void)
{
	unsigned char txdat = 0;        /* Only/Current interface = 0 */

	USB_Single_Transmit(&txdat, 1);
}

void USBClassRequest_SetInterface(void)
{
	if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
		USB_Single_Transmit(0, 0);
	else
		USB_Stall_EP0();
}

⌨️ 快捷键说明

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