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

📄 usbold.c

📁 开发工具:iar for c51 主要IC:tusb2136(ti)(8052内核) 主要应用:PC外接usb键盘
💻 C
📖 第 1 页 / 共 4 页
字号:
				{
				abDescriptor[stringOffset++] = funcDefs[fncOffset].prodDescription[bTemp]; // Insert the character from the string
				abDescriptor[stringOffset++] = 0x00; // Insert a trailing 00h for Unicode representation
				}
			break;
		case 3: // SERIAL NUMBER
			abDescriptor[stringOffset++] = strlen(funcDefs[fncOffset].serialNumber) * 2 + 2;  // Length of this string
			abDescriptor[stringOffset++] = DESC_TYPE_STRING; // String descriptor type
			for(bTemp = 0; bTemp < strlen(funcDefs[fncOffset].serialNumber);bTemp++)
				{
				abDescriptor[stringOffset++] = funcDefs[fncOffset].serialNumber[bTemp]; // Insert the character from the string	
				abDescriptor[stringOffset++] = 0x00; // Insert a trailing 00h for Unicode representation
				}
			break;
		default:
				break;
		}

#ifdef XX
    bIndex = 0x00;
    while(tSetupPacket.bValueL-- >  0x00) 
    	bIndex += abStringDescriptor[bIndex];

	// Copy the STRING DESCRIPTOR from program "ROM" to XRAM
	bStrLen = 
    for(bTemp=0;bTemp<SIZEOF_BOOTCODE_CONFIG_DESC_GROUP;bTemp++)
        abDescriptor[bTemp] = abromConfigurationDescriptorGroup[bTemp];
#endif    	
    wBytesRemainingOnIEP0 = abDescriptor[0];
    usbSendDataPacketOnEP0((PBYTE)&abDescriptor);
}

void usbGetReportDescriptor(void)
{
    usbClearOEP0ByteCount;
    wBytesRemainingOnIEP0 = SIZEOF_REPORT_DESCRIPTOR;
    usbSendDataPacketOnEP0((PBYTE)&abromReportDescriptor);
}

// Support of the following two functions is optional.  Supporting these
// features allows the host to set the Idle rate.  By default, an HID
// device should always report the current status of the keys, even
// if they haven't changed.  However, to save bandwidth, Windows will
// attempt to set the Idle rate to 0 which means the firmware should
// only report keypresses when their state changes.

void usbSetIdle(void)
{
	usbStallEndpoint0();
}

void usbGetIdle(void)
{
	usbStallEndpoint0();
}

// The Get/Set Interface, like the Get/Set Configuration, don't really
// serve any real purpose in this firmware, but they are handled so
// that modifications to the code may be made easily.  As-is, the host
// may Set any interface number, and a Get Interface request will simply
// return the value previously Set.

void usbGetInterface(void)
{
    wBytesRemainingOnIEP0 = 1;
	usbSendDataPacketOnEP0((PBYTE) &bInterfaceNumber);
}

void usbSetInterface(void)
{
    usbStallOEP0;                             // control write without data stage
    bInterfaceNumber = tSetupPacket.bIndexL;
    usbSendZeroLengthPacketOnIEP0();
}

// The GetDeviceStatus function is used to obtain the status of the
// device.  The status is essentially the Remote Wakeup status as 
// well as the "Self-powered" indicator.  The value returned by
// GetDeviceStatus is modified by the SetRemoteWakeup and
// ClearRemoteWakeup requests.

void usbGetDeviceStatus(void)
{
    wBytesRemainingOnIEP0 = 2;
	usbSendDataPacketOnEP0((PBYTE) &wDeviceFeatures);
}

void usbSetRemoteWakeup(void)
{
	bUSBCTL |= USBCTL_RWE;
	wDeviceFeatures |= 0x0200;
    usbStallOEP0;
    usbSendZeroLengthPacketOnIEP0();
}

void usbClearRemoteWakeup(void)
{
	bUSBCTL &= ~USBCTL_RWE;
	wDeviceFeatures &= ~0x0200;
    usbStallOEP0;
    usbSendZeroLengthPacketOnIEP0();
}

// The GetInterfaceStatus always returns a 0 as a 2-byte value.

void usbGetInterfaceStatus(void)
{
	WORD wStatusBuffer = 0x00;    	
    usbStallOEP0;
    wBytesRemainingOnIEP0 = 2;
	usbSendDataPacketOnEP0((PBYTE) &wStatusBuffer);
}

// The SetAddress request allows the host to assign an address to this device.
// The device starts with an address of 00h, as do all USB devices, until
// the host specifically assigns it another address.  This code handles that
// assignment.

void usbSetAddress(void)
{
    if(tSetupPacket.bValueL < 128)
    	{
        bFUNADR = tSetupPacket.bValueL;
        bUsbDeviceAddress = tSetupPacket.bValueL;
        bStatusAction = STATUS_ACTION_SET_ADDRESS;
        usbSendZeroLengthPacketOnIEP0();
    	}
    else 
    	usbStallEndpoint0();
}

// The SetEndpointHalt allows the USB host to instruct the device to stop sending
// information on IEP1, which is how the firmware delivers keystrokes to the host.
// This is used mostly if a device goes crazy and starts sending too much data,
// this allows the host to shut the endpoint down.  All we do is set or clear
// the endpoint enable bit appropriately.  The GetEndpointStatus request reports
// the status of the endpoint which is affected by Set/Clear EndpointHalt requests.

void usbSetEndpointHalt(void)
{
	tInputEndPointDescriptorBlock[0].bEPCNF &= ~EPCNF_UBME;
    usbSendZeroLengthPacketOnIEP0();
}

void usbClearEndpointHalt(void)
{
	tInputEndPointDescriptorBlock[0].bEPCNF |= EPCNF_UBME;	
    usbSendZeroLengthPacketOnIEP0();
}

void usbGetEndpointStatus(void)
{
	WORD wEndpointStatus = 0x0100;
	if(tInputEndPointDescriptorBlock[0].bEPCNF & EPCNF_UBME)
		wEndpointStatus = 0x0000;

    wBytesRemainingOnIEP0 = 2;
	usbSendDataPacketOnEP0((PBYTE) &wEndpointStatus);
}

// Any non-standard or unrecognized request will arrive at the following
// function by default.  We automatically stall the endpoint to indicate
// it's an invalid or unrecognized request.

void usbNonStandardRequest(void)
{
	usbStallEndpoint0();
}

/***************************************************************************
 * Section:    REQUEST STRUCTURE                                           *
 * Programmer: Lobo Tai (lobotai@ti.com)                                   *
 * Description: This section of code defines the structure of the lookup   *
 *    table which determines which 'C' function should be called for each  *
 *    supported USB request.                                               * 
 ***************************************************************************/

typedef struct _tDEVICE_REQUEST_COMPARE
{
    BYTE    bmRequestType;              // See bit definitions below
    BYTE    bRequest;                   // See value definitions below
    BYTE    bValueL;                    // Meaning varies with request type
    BYTE    bValueH;                    // Meaning varies with request type
    BYTE    bIndexL;                    // Meaning varies with request type
    BYTE    bIndexH;                    // Meaning varies with request type
    BYTE    bLengthL;                   // Number of bytes of data to transfer (LSByte)
    BYTE    bLengthH;                   // Number of bytes of data to transfer (MSByte)
    BYTE    bCompareMask;               // MSB is bRequest, if set 1, bRequest should be matched, LSB is bLengthH
    VOID    (*pUsbFunction)(VOID);      // function pointer
} tDEVICE_REQUEST_COMPARE, *ptDEVICE_REQUEST_COMPARE;

/***************************************************************************
 * Section:    USB REQUEST TABLE                                           *
 * Programmer: Craig Steiner (csteiner@vaultbbs.com) based on code by      *
 *             Lobo Tai (lobotai@ti.com)                                   *
 * Description: This section of code defines the lookup table, using the   *
 *    structure defined in the previous section of code.  The values of    *
 *    the constants used in this structure are defined in usb.h.           * 
 * Structure of Table:                                                     * 
 *    bmRequestType: Indicates the type of request.  This is a bit-mapped  * 
 *          variable defined in the USB spec.  The bits of this variable   * 
 *          have the following purpose, and are defined in usb.h.          * 
 *          Bit 7: Data Direction (0=Host to Device, 1=Device to Host)     * 
 *          Bit 6-5: Type of request (00=Standard, 01=Class, 10=Vendor)    * 
 *          Bit 4-0: Recipient (00000=Device, 00001=Interface,             *
 *                              00010=Endpoint, 00011=Other                *
 *    bRequest: Indicates the request ID (Get descriptor, Get Status, Get  *
 *              feature, etc.).  These are defined in the USB and HID spec *
 *              and are declared in usb.h.                                 *
 *    bValueL/H: Additional values, purpose varies with request.           *
 *    bIndexL/H: Additional values, purpose varies with request.           *
 *    bLengthL/H: Number of bytes to transfer to or from host.             *
 *    bCompareMask: Indicates which of the above bytes should be compared  *
 *               to determine the function to call.  For example, the mask *
 *               0x80 means only bmRequestType must match. 0xC0 means      *
 *               both bmRequestType and bRequest must match.  If this      *
 *               variable is 0x00, as is the case in the last entry in the *
 *               table, then no bytes are compared and, thus, ANY packet   *
 *               will pass the comparsion stage.  This technique is used   *
 *               in the last entry of the table as an "else" condition     *
 *               so that any requests that haven't been handled by that    *
 *               point are handled by the usbNonStandardRequest function.  *
 ***************************************************************************/

code tDEVICE_REQUEST_COMPARE tUsbRequestList[] =
{
    // SET ENDPOINT FEATURE
    USB_REQ_TYPE_OUTPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_ENDPOINT,
    USB_REQ_SET_FEATURE,
    FEATURE_ENDPOINT_STALL,0x00,
    0xff,0x00,
    0x00,0x00,
    0xf7,&usbSetEndpointHalt,

    // CLEAR ENDPOINT FEATURE
    USB_REQ_TYPE_OUTPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_ENDPOINT,
    USB_REQ_CLEAR_FEATURE,
    FEATURE_ENDPOINT_STALL,0x00,
    0xff,0x00,
    0x00,0x00,
    0xf7,&usbClearEndpointHalt,

    // GET CONFIGURATION
    USB_REQ_TYPE_INPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE,
    USB_REQ_GET_CONFIGURATION,
    0x00,0x00,
    0x00,0x00,
    0x01,0x00,
    0xff,&usbGetConfiguration,

    // SET CONFIGURATION
    USB_REQ_TYPE_OUTPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE,
    USB_REQ_SET_CONFIGURATION,
    0xff,0x00,
    0x00,0x00,
    0x00,0x00,
    0xdf,&usbSetConfiguration,

    // GET DEVICE DESCRIPTOR
    USB_REQ_TYPE_INPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE,
    USB_REQ_GET_DESCRIPTOR,
    0xff,DESC_TYPE_DEVICE,                  // bValueL is index and bValueH is type
    0xff,0xff,
    0xff,0xff,
    0xd0,&usbGetDeviceDescriptor,

    // GET CONFIGURATION DESCRIPTOR
    USB_REQ_TYPE_INPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE,
    USB_REQ_GET_DESCRIPTOR,
    0xff,DESC_TYPE_CONFIG,                  // bValueL is index and bValueH is type
    0xff,0xff,
    0xff,0xff,
    0xd0,&usbGetConfigurationDescriptor,

    // GET HID DESCRIPTOR
    USB_REQ_TYPE_INPUT | USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE,

⌨️ 快捷键说明

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