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

📄 usbproto.c

📁 Comlete files for fullfile the slave USB function!
💻 C
字号:
/*
//*************************************************************************
//
//                  BASBA   P R O P R I E T A R Y
//                     --  ALL RIGHTS RESERVED  --
//
// File Name:	USBProto.c
// Purpose:		Handles the standard USB device requests
// Author:		Shuming Yu
// Created:		10 May 2003
// Modified:
// Revision:	1.0
//
//*************************************************************************
*/

#include <stdio.h>
#include <string.h>

#include <reg51.h>                /* special function register declarations   */

#include "d12hal.h"
#include "cmds.h"
#include "mainloop.h"
#include "usbStruc.h"
#include "USBProto.h"

//#define NUM_ENDPOINTS	4
#define NUM_ENDPOINTS	1

unsigned char IDLE_TIME;
/*
//#define CONFIG_DESCRIPTOR_LENGTH    sizeof(USB_CONFIGURATION_DESCRIPTOR) \
//									+ sizeof(USB_INTERFACE_DESCRIPTOR) \
//									+ (NUM_ENDPOINTS * sizeof(USB_ENDPOINT_DESCRIPTOR))
*/
extern CONTROL_XFER ControlData;
//extern IO_REQUEST idata ioRequest;
extern EPPFLAGS bEPPflags;
//
//           COPYRIGHT (c)   2003 BY BASBA USA.PPflags;
extern unsigned char idata GenEpBuf[];	//Buffer which store data got from host

UCHAR code REPORT_DESCRIPTOR[] = {
    0x06,0x00,0xFF, //Usage Page(vendor defined)
	0x09,0x01, //Usage (I/O Device)
	0xA1,0x01, //Collection (Application)
	0x19,0x01, //Usage minimum (button1)
	0x29,0x08, //Usage maximun (button8)
	0x15,0x00, //Logic mininum (0)
	0x25,0x01, //logic maximum (1)
	0x75,0x01, //Report size (1)
	0x95,0x08, //Report count (8)
	0x81,0x02, //Input(Data,Var,Abs)
	0x19,0x01, //Usage Minimum (LED1)
	0x29,0x08, //Usage Maximum (LED8)
	0x91,0x02, //Output(Data,Var,Abs)
	0xC0       //End of collection
};

struct CONFIG { USB_CONFIGURATION_DESCRIPTOR sConfig;
                USB_INTERFACE_DESCRIPTOR sInterface;
                USB_HID_DESCRIPTOR sHID;
                USB_ENDPOINT_DESCRIPTOR sEndpoint;
};

#define CONFIG_DESCRIPTOR_LENGTH    sizeof(struct CONFIG)
#define REPORT_DESC_LENGTH sizeof(REPORT_DESCRIPTOR)


code USB_DEVICE_DESCRIPTOR DeviceDescr =
{
	sizeof(USB_DEVICE_DESCRIPTOR), //BYTE bLength,
    USB_DEVICE_DESCRIPTOR_TYPE, //BYTE bDescriptorType
    SWAP(0x0100), //WORD bcdUSB, USB Rev1.0
	0x00, //Byte bDeviceClass
    0x00, //BYTE bDeviceSubClass
    0x00, //BYTE bDeviceProtocol
	EP0_PACKET_SIZE,//BYTE bMaxPacketSize of 16
	SWAP(0x4242), //WORD idVendor
	SWAP(0x0100), //WORD idProduct
    SWAP(0x0100), //WORD bcdDevice
    0x01,  //BYTE iManufacturer name
    0x02,  //BYTE iProduct name
    0x00,  //BYTE iSerialNumber name
    0x01  //BYTE bNumConfigurations, number of configurations
};

/*
code USB_CONFIGURATION_DESCRIPTOR  ConfigDescr =
{
    sizeof(USB_CONFIGURATION_DESCRIPTOR), //BYTE bLength
    USB_CONFIGURATION_DESCRIPTOR_TYPE, //BYTE bDescriptorType //Assigned by USB
    SWAP(CONFIG_DESCRIPTOR_LENGTH), //WORD wTotalLength
    0x01, //BYTE bNumInterfaces
    0x01, //BYTE bConfigurationValue
    0x03, //BYTE iConfiguration
    0x80, //BYTE bmAttributes, Bus powered
    50, //BYTE MaxPower, 50x2=100mA
};

code USB_INTERFACE_DESCRIPTOR InterfaceDescr =
{
    sizeof(USB_INTERFACE_DESCRIPTOR),//BYTE bLength
    USB_INTERFACE_DESCRIPTOR_TYPE,//BYTE bDescriptionType, assigned by USB
    0x00, //BYTE bInterfaceNumber
    0x00, //BYTE bAlternateSetting, no alternate setting
    0x01, //BYTE bNumEndpoints, HID uses endpoint 1
    0x03, //BYTE bInterfaceClass, HID Class - 0x03
    0x00, //BYTE bInterfaceSubClass
    0x00, //BYTE bInterfaceProtocol
    0x04  //BYTE iInterface, interface name
};

code USB_HID_DESCRIPTOR HIDDescr = 
{
    sizeof(USB_HID_DESCRIPTOR), //BYTE bLength;
    0x21, //BYTE bDescriptorType;
    0x0001, //WORD bcdHID;
    0x00, //BYTE bCountryCode, country localization = None;
    0x01, //BYTE bNumDescriptors, number of descriptor follow
    0x22, //BYTE bReportDescriptorType And it's a Report descriptor
    SWAP(REPORT_DESC_LENGTH) //WORD wItemLength, total length of report descriptor
};

code USB_ENDPOINT_DESCRIPTOR EP1_TXDescr =
{
    sizeof(USB_ENDPOINT_DESCRIPTOR), //BYTE bLength
    0x05, //BYTE bDescriptorType, assigned by USB
    0x81, //BYTE bEndpointAddress, IN endpoint, endpoint 1
    USB_ENDPOINT_TYPE_INTERRUPT, //BYTE bmAttributes, Interrupt endpoint
    SWAP(EP1_PACKET_SIZE), //WORD wMaxPacketSize
    100 //Polling Time, poll over 100 ms
};
*/

struct CONFIG code sConfiguration= {
	sizeof(USB_CONFIGURATION_DESCRIPTOR), //BYTE bLength
    	USB_CONFIGURATION_DESCRIPTOR_TYPE, //BYTE bDescriptorType //Assigned by USB
    	SWAP(CONFIG_DESCRIPTOR_LENGTH), //WORD wTotalLength
    	0x01, //BYTE bNumInterfaces
    	0x01, //BYTE bConfigurationValue
    	0x03, //BYTE iConfiguration
    	0x80, //BYTE bmAttributes, Bus powered
    	50, //BYTE MaxPower, 50x2=100mA

	sizeof(USB_INTERFACE_DESCRIPTOR),//BYTE bLength
		USB_INTERFACE_DESCRIPTOR_TYPE,//BYTE bDescriptionType, assigned by USB
		0x00, //BYTE bInterfaceNumber
		0x00, //BYTE bAlternateSetting, no alternate setting
		0x01, //BYTE bNumEndpoints, HID uses endpoint 1
	    0x03, //BYTE bInterfaceClass, HID Class - 0x03
	    0x00, //BYTE bInterfaceSubClass
	    0x00, //BYTE bInterfaceProtocol
	    0x04,  //BYTE iInterface, interface name

	sizeof(USB_HID_DESCRIPTOR), //BYTE bLength;
		0x21, //BYTE bDescriptorType;
		0x0001, //WORD bcdHID;
		0x00, //BYTE bCountryCode, country localization = None;
		0x01, //BYTE bNumDescriptors, number of descriptor follow
		0x22, //BYTE bReportDescriptorType And it's a Report descriptor
		SWAP(REPORT_DESC_LENGTH), //WORD wItemLength, total length of report descriptor

	sizeof(USB_ENDPOINT_DESCRIPTOR), //BYTE bLength
		0x05, //BYTE bDescriptorType, assigned by USB
		0x81, //BYTE bEndpointAddress, 0x80=IN endpoint, endpoint 1
		USB_ENDPOINT_TYPE_INTERRUPT, //BYTE bmAttributes, Interrupt
		SWAP(EP1_PACKET_SIZE), //WORD wMaxPacketSize
	    100 //Polling Time unit ms, poll over 100 ms
 };


code USB_ENDPOINT_DESCRIPTOR EP1_RXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),
	USB_ENDPOINT_DESCRIPTOR_TYPE,
	0x1,
	USB_ENDPOINT_TYPE_INTERRUPT,
	SWAP(EP1_PACKET_SIZE),
	10
};

code USB_ENDPOINT_DESCRIPTOR EP2_TXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),
	USB_ENDPOINT_DESCRIPTOR_TYPE,
	0x82,
	USB_ENDPOINT_TYPE_BULK,
	SWAP(EP2_PACKET_SIZE),
	10
};

code USB_ENDPOINT_DESCRIPTOR EP2_RXDescr =
{
	sizeof(USB_ENDPOINT_DESCRIPTOR),
	USB_ENDPOINT_DESCRIPTOR_TYPE,
	0x2,
	USB_ENDPOINT_TYPE_BULK,
	SWAP(EP2_PACKET_SIZE),
	10
};


unsigned char code LANG_ID[4] = {
	0x04,  //4 bytes
	0x03,  //bDescriptorType = String Desc
	0x09,  //Lang ID for English = 0x0409
	0x04
};

code csSTRING1[] = { //STRING1 Manumfacturer
'M',0,'y',0,'U',0,'S',0,'B',0};
code csSTRING2[] = { //STRING2 Product Name
'D',0,'e',0,'c',0,'o',0,'d',0,'e',0,'r',0};
code csSTRING3[] = { //STRING3 Configuration Name
'S',0,'i',0,'m',0,'p',0,'l',0,'e',0,'D',0,'e',0,'v',0,'c',0,'e',0};
code csSTRING4[] = { //STRING4 Interface Name
'H',0,'I',0,'D',0,'e',0,'x',0,'a',0,'m',0,'p',0,'l',0,'e',0};


struct STRING1 { 
    UCHAR bLength;
	UCHAR bDescriptorType;
	char string1[sizeof(csSTRING1)];
};
struct STRING1 code sSTRING1 = {
	sizeof(csSTRING1)+2, //Size of String
	0x03, //STRING Descriptor Type
	csSTRING1};
struct STRING2 { 
    UCHAR bLength;
	UCHAR bDescriptorType;
	char string1[sizeof(csSTRING2)];
};
struct STRING2 code sSTRING2 = {
	sizeof(csSTRING2)+2, //Size of String
	0x03, //STRING Descriptor Type
	csSTRING2};
struct STRING3 { 
    UCHAR bLength;
	UCHAR bDescriptorType;
	char string1[sizeof(csSTRING3)];
};
struct STRING3 code sSTRING3 = {
	sizeof(csSTRING3)+2, //Size of String
	0x03, //STRING Descriptor Type
	csSTRING3};
struct STRING4 { 
    UCHAR bLength;
	UCHAR bDescriptorType;
	char string1[sizeof(csSTRING4)];
};
struct STRING4 code sSTRING4 = {
	sizeof(csSTRING4)+2, //Size of String
	0x03, //STRING Descriptor Type
	csSTRING4};


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

void reserved(void)
{
	stall_ep0();
}

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

	if (bRecipient == USB_RECIPIENT_DEVICE) {
		if(bEPPflags.bits.remote_wakeup == 1)
			txdat[0] = 3;
		else
			txdat[0] = 1;
		txdat[1]=0;
		single_transmit(txdat, 2);
	} else if (bRecipient == USB_RECIPIENT_INTERFACE) {
		txdat[0]=0;
		txdat[1]=0;
		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 = D12_SelectEndpoint(endp*2 + 1);	/* Control-in */
		else
			c = D12_SelectEndpoint(endp*2);	/* Control-out */
		if(c & D12_STALL)
			txdat[0] = 1;
		else
			txdat[0] = 0;
		txdat[1] = 0;
		single_transmit(txdat, 2);
	} else
		stall_ep0();
}

void clear_feature(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;
		bEPPflags.bits.remote_wakeup = 0;
		ENABLE;
		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. */
			D12_SetEndpointStatus(endp*2 + 1, 0);
		else
			/* clear RX stall for OUT on EPn. */
			D12_SetEndpointStatus(endp*2, 0);
		single_transmit(0, 0);
	} else
		stall_ep0();
}

void set_feature(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;
		bEPPflags.bits.remote_wakeup = 1;
		ENABLE;
		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. */
			D12_SetEndpointStatus(endp*2 + 1, 1);
		else
			/* clear RX stall for OUT on EPn. */
			D12_SetEndpointStatus(endp*2, 1);
		single_transmit(0, 0);
	} else
		stall_ep0();
}

void set_address(void)
{
	D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue &
		DEVICE_ADDRESS_MASK), 1);
	single_transmit(0, 0);
}

extern void blinkLED(unsigned char num);

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

	if (bDescriptor == USB_DEVICE_DESCRIPTOR_TYPE) {
		code_transmit((unsigned char code *)&DeviceDescr, sizeof(USB_DEVICE_DESCRIPTOR));
	} else if (bDescriptor == USB_CONFIGURATION_DESCRIPTOR_TYPE) {
		code_transmit((unsigned char code *)&sConfiguration, CONFIG_DESCRIPTOR_LENGTH);
	} else if (bDescriptor == USB_INTERFACE_DESCRIPTOR_TYPE) {
		code_transmit((unsigned char code *)&sConfiguration.sInterface, 
			sizeof(USB_INTERFACE_DESCRIPTOR));
	} else if (bDescriptor == USB_ENDPOINT_DESCRIPTOR_TYPE) {
		code_transmit((unsigned char code *)&sConfiguration.sEndpoint, 
			sizeof(USB_ENDPOINT_DESCRIPTOR));
	} else if (bDescriptor == 0x0021) { 	/* HID Descriptor */
		code_transmit((unsigned char code *)&sConfiguration.sHID, 
			sizeof(USB_HID_DESCRIPTOR));
	} else if (bDescriptor == 0x0022) { 	/*Report Descriptor */
		code_transmit((unsigned char code *)&REPORT_DESCRIPTOR, 
			sizeof(REPORT_DESCRIPTOR));
	} else if (bDescriptor == USB_STRING_DESCRIPTOR_TYPE) {	/* String Descriptor */
		strIndex = LSB(ControlData.DeviceRequest.wValue);
		switch (strIndex)
		{
			case 0x00:
				code_transmit((unsigned char code *)&LANG_ID, sizeof(LANG_ID));
				break;
			case 0x01:
				code_transmit((unsigned char code *)&sSTRING1, sizeof(struct STRING1));
blinkLED(1);
				break;
			case 0x02:
				code_transmit((unsigned char code *)&sSTRING2, sizeof(struct STRING2));
				break;
			case 0x03:
				code_transmit((unsigned char code *)&sSTRING3, sizeof(struct STRING3));
				break;
			case 0x04:
				code_transmit((unsigned char code *)&sSTRING4, sizeof(struct STRING4));
				break;
			default:
				break;
		}
	} else {
		stall_ep0();
	}
}

void get_configuration(void)
{
	unsigned char c = bEPPflags.bits.configuration;

	single_transmit(&c, 1);
}

void set_configuration(void)
{
	if (ControlData.DeviceRequest.wValue == 0) {
		/* put device in unconfigured state */
		single_transmit(0, 0);
		DISABLE;
		bEPPflags.bits.configuration = 0;
		ENABLE;
		init_unconfig();
	} else if (ControlData.DeviceRequest.wValue == 1) {
		/* Configure device */
		single_transmit(0, 0);

		init_unconfig();
		init_config();
		
		DISABLE;
		bEPPflags.bits.configuration = 1;
		ENABLE;
	} else
		stall_ep0();
}

void get_interface(void)
{
	unsigned char txdat = 0;        /* Only/Current interface = 0 */
	single_transmit(&txdat, 1);
}

void set_interface(void)
{
	if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
		single_transmit(0, 0);
	else
		stall_ep0();
}


//
//	Class Request
//
void get_report (void)
{
	unsigned char c;

	//Need to be configured to do this command
	if (bEPPflags.bits.configuration) {
		//Send current switch status to host
//	c = Switch;
	single_transmit(&c, 1);
	}
}

void get_idle (void)
{
	unsigned char c;

	//Need to be configured to do this command
	if (bEPPflags.bits.configuration) {
		c = IDLE_TIME;
		single_transmit(&c, 1);
	}
}

void get_protocol (void)
{
// Do nothing,we are not a boot device
}

void set_report (void)
{
	unsigned char len;

	//Need to be configured to do this command
	if (bEPPflags.bits.configuration) {
	// Get LED information from host and set LED
		D12_ReadLastTransactionStatus(2); /* Clear interrupt flag */
		len = D12_ReadEndpoint(2, GenEpBuf, sizeof(GenEpBuf));
		if(len != 0)
			bEPPflags.bits.ep1_rxdone = 1;
	}
}

void set_idle (void)
{
	//Need to be configured to do this command
	if (bEPPflags.bits.configuration) {
		//IDLE_TIME = HIBYTE(ControlData.DeviceRequest.wValue);
		IDLE_TIME = LSB(ControlData.DeviceRequest.wValue);
		single_transmit(0, 0);
	}
}

void set_protocol (void)
{
// Do nothing, we are not a boot device
}

⌨️ 快捷键说明

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