CH552/projects/examples/USB/Device/CompositeKM.C

515 lines
24 KiB
C++
Raw Normal View History

2018-10-17 10:44:45 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name :CompositeKM.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH559ģ<EFBFBD><EFBFBD>USB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
#include "./Public/CH554.H"
#include "./Public/Debug.H"
#include <string.h>
#include <stdio.h>
#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
UINT8X Ep0Buffer[8<(THIS_ENDP0_SIZE+2)?8:(THIS_ENDP0_SIZE+2)] _at_ 0x0000; //<2F>˵<EFBFBD>0 OUT&IN<49><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD><C5BC>ַ
UINT8X Ep1Buffer[64<(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x000a; //<2F>˵<EFBFBD>1 IN<49><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD><C5BC>ַ
UINT8X Ep2Buffer[64<(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x0050; //<2F>˵<EFBFBD>2 IN<49><4E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD><C5BC>ַ
UINT8 SetupReq,SetupLen,Ready,Count,FLAG,UsbConfig;
PUINT8 pDescr; //USB<53><42><EFBFBD>ñ<EFBFBD>־
USB_SETUP_REQ SetupReqBuf; //<2F>ݴ<EFBFBD>Setup<75><70>
sbit Ep2InKey = P1^5;
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
#define DEBUG 0
#pragma NOAREGS
/*<2A><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8C DevDesc[18] = {0x12,0x01,0x10,0x01,0x00,0x00,0x00,0x08,
0x3d,0x41,0x07,0x21,0x00,0x00,0x00,0x00,
0x00,0x01
};
UINT8C CfgDesc[59] =
{
0x09,0x02,0x3b,0x00,0x02,0x01,0x00,0xA0,0x32, //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x09,0x04,0x00,0x00,0x01,0x03,0x01,0x01,0x00, //<2F>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>
0x09,0x21,0x11,0x01,0x00,0x01,0x22,0x3e,0x00, //HID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x07,0x05,0x81,0x03,0x08,0x00,0x0a, //<2F>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x09,0x04,0x01,0x00,0x01,0x03,0x01,0x02,0x00, //<2F>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>
0x09,0x21,0x10,0x01,0x00,0x01,0x22,0x34,0x00, //HID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x07,0x05,0x82,0x03,0x04,0x00,0x0a //<2F>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
/*<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
/*HID<49><EFBFBD><E0B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8C KeyRepDesc[62] =
{
0x05,0x01,0x09,0x06,0xA1,0x01,0x05,0x07,
0x19,0xe0,0x29,0xe7,0x15,0x00,0x25,0x01,
0x75,0x01,0x95,0x08,0x81,0x02,0x95,0x01,
0x75,0x08,0x81,0x01,0x95,0x03,0x75,0x01,
0x05,0x08,0x19,0x01,0x29,0x03,0x91,0x02,
0x95,0x05,0x75,0x01,0x91,0x01,0x95,0x06,
0x75,0x08,0x26,0xff,0x00,0x05,0x07,0x19,
0x00,0x29,0x91,0x81,0x00,0xC0
};
UINT8C MouseRepDesc[52] =
{
0x05,0x01,0x09,0x02,0xA1,0x01,0x09,0x01,
0xA1,0x00,0x05,0x09,0x19,0x01,0x29,0x03,
0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x03,
0x81,0x02,0x75,0x05,0x95,0x01,0x81,0x01,
0x05,0x01,0x09,0x30,0x09,0x31,0x09,0x38,
0x15,0x81,0x25,0x7f,0x75,0x08,0x95,0x03,
0x81,0x06,0xC0,0xC0
};
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8 HIDMouse[4] = {0x0,0x0,0x0,0x0};
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8 HIDKey[8] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
/*******************************************************************************
* Function Name : USBDeviceInit()
* Description : USB<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>շ<EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD>жϿ<EFBFBD><EFBFBD><EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USBDeviceInit()
{
IE_USB = 0;
USB_CTRL = 0x00; // <20><><EFBFBD>趨USB<53>豸ģʽ
UEP2_DMA = Ep2Buffer; //<2F>˵<EFBFBD>2<EFBFBD><32><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>ַ
UEP2_3_MOD = UEP2_3_MOD & ~bUEP2_BUF_MOD | bUEP2_TX_EN; //<2F>˵<EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> 64<36>ֽڻ<D6BD><DABB><EFBFBD><EFBFBD><EFBFBD>
UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //<2F>˵<EFBFBD>2<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>תͬ<D7AA><CDAC><EFBFBD><EFBFBD>־λ<D6BE><CEBB>IN<49><4E><EFBFBD>񷵻<EFBFBD>NAK
UEP0_DMA = Ep0Buffer; //<2F>˵<EFBFBD>0<EFBFBD><30><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>ַ
UEP4_1_MOD &= ~(bUEP4_RX_EN | bUEP4_TX_EN); //<2F>˵<EFBFBD>0<EFBFBD><30>64<36>ֽ<EFBFBD><D6BD>շ<EFBFBD><D5B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; //OUT<55><54><EFBFBD>񷵻<EFBFBD>ACK<43><4B>IN<49><4E><EFBFBD>񷵻<EFBFBD>NAK
UEP1_DMA = Ep1Buffer; //<2F>˵<EFBFBD>1<EFBFBD><31><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>ַ
UEP4_1_MOD = UEP4_1_MOD & ~bUEP1_BUF_MOD | bUEP1_TX_EN; //<2F>˵<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> 64<36>ֽڻ<D6BD><DABB><EFBFBD><EFBFBD><EFBFBD>
UEP1_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //<2F>˵<EFBFBD>1<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>תͬ<D7AA><CDAC><EFBFBD><EFBFBD>־λ<D6BE><CEBB>IN<49><4E><EFBFBD>񷵻<EFBFBD>NAK
USB_DEV_AD = 0x00;
UDEV_CTRL = bUD_PD_DIS; // <20><>ֹDP/DM<44><4D><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // <20><><EFBFBD><EFBFBD>USB<53><EFBFBD><E8B1B8>DMA<4D><41><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>ڼ<EFBFBD><DABC>жϱ<D0B6>־δ<D6BE><CEB4><EFBFBD><EFBFBD>ǰ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>NAK
UDEV_CTRL |= bUD_PORT_EN; // <20><><EFBFBD><EFBFBD>USB<53>˿<EFBFBD>
USB_INT_FG = 0xFF; // <20><><EFBFBD>жϱ<D0B6>־
USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST;
IE_USB = 1;
}
/*******************************************************************************
* Function Name : Enp1IntIn()
* Description : USB<EFBFBD>ģʽ<EFBFBD>˵<EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>ϴ<EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Enp1IntIn( )
{
memcpy( Ep1Buffer, HIDKey, sizeof(HIDKey)); //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
UEP1_T_LEN = sizeof(HIDKey); //<2F>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ݲ<EFBFBD>Ӧ<EFBFBD><D3A6>ACK
}
/*******************************************************************************
* Function Name : Enp2IntIn()
* Description : USB<EFBFBD>ģʽ<EFBFBD>˵<EFBFBD>2<EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD>ϴ<EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Enp2IntIn( )
{
memcpy( Ep2Buffer, HIDMouse, sizeof(HIDMouse)); //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
UEP2_T_LEN = sizeof(HIDMouse); //<2F>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ݲ<EFBFBD>Ӧ<EFBFBD><D3A6>ACK
}
/*******************************************************************************
* Function Name : DeviceInterrupt()
* Description : CH559USB<EFBFBD>жϴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*******************************************************************************/
void DeviceInterrupt( void ) interrupt INT_NO_USB using 1 //USB<53>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ʹ<>üĴ<C3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>1
{
UINT8 len;
if(UIF_TRANSFER) //USB<53><42><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־
{
switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP))
{
case UIS_TOKEN_IN | 2: //endpoint 2# <20>ж϶˵<CFB6><CBB5>ϴ<EFBFBD>
UEP2_T_LEN = 0; //Ԥʹ<D4A4>÷<EFBFBD><C3B7>ͳ<EFBFBD><CDB3><EFBFBD>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
// UEP1_CTRL ^= bUEP_T_TOG; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>ת
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //Ĭ<><C4AC>Ӧ<EFBFBD><D3A6>NAK
break;
case UIS_TOKEN_IN | 1: //endpoint 1# <20>ж϶˵<CFB6><CBB5>ϴ<EFBFBD>
UEP1_T_LEN = 0; //Ԥʹ<D4A4>÷<EFBFBD><C3B7>ͳ<EFBFBD><CDB3><EFBFBD>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
// UEP2_CTRL ^= bUEP_T_TOG; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD>ת
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //Ĭ<><C4AC>Ӧ<EFBFBD><D3A6>NAK
FLAG = 1; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־*/
break;
case UIS_TOKEN_SETUP | 0: //SETUP<55><50><EFBFBD><EFBFBD>
len = USB_RX_LEN;
if(len == (sizeof(USB_SETUP_REQ)))
{
SetupLen = UsbSetupBuf->wLengthL;
if(UsbSetupBuf->wLengthH || SetupLen > 0x7F )
{
SetupLen = 0x7F; // <20><><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
}
len = 0; // Ĭ<><C4AC>Ϊ<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD>
SetupReq = UsbSetupBuf->bRequest;
if ( ( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD )/* HID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
{
switch( SetupReq )
{
case 0x01://GetReport
break;
case 0x02://GetIdle
break;
case 0x03://GetProtocol
break;
case 0x09://SetReport
break;
case 0x0A://SetIdle
break;
case 0x0B://SetProtocol
break;
default:
len = 0xFF; /*<2A><><EFBFBD>֧<EEB2BB><D6A7>*/
break;
}
}
else
{//<2F><>׼<EFBFBD><D7BC><EFBFBD><EFBFBD>
switch(SetupReq) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
case USB_GET_DESCRIPTOR:
switch(UsbSetupBuf->wValueH)
{
case 1: //<2F><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pDescr = DevDesc; //<2F><><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>͵Ļ<CDB5><C4BB><EFBFBD><EFBFBD><EFBFBD>
len = sizeof(DevDesc);
break;
case 2: //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pDescr = CfgDesc; //<2F><><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>͵Ļ<CDB5><C4BB><EFBFBD><EFBFBD><EFBFBD>
len = sizeof(CfgDesc);
break;
case 0x22: //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(UsbSetupBuf->wIndexL == 0) //<2F>ӿ<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
pDescr = KeyRepDesc; //<2F><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD>ϴ<EFBFBD>
len = sizeof(KeyRepDesc);
}
else if(UsbSetupBuf->wIndexL == 1) //<2F>ӿ<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
pDescr = MouseRepDesc; //<2F><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD>ϴ<EFBFBD>
len = sizeof(MouseRepDesc);
Ready = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD>ӿڣ<D3BF><DAA3>ñ<EFBFBD>׼λӦ<CEBB><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD>Ч
}
else
{
len = 0xff; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>2<EFBFBD><32><EFBFBD>ӿڣ<D3BF><DAA3><EFBFBD><EFBFBD><EFBFBD><E4BBB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>
}
break;
default:
len = 0xff; //<2F><>֧<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3><EFBFBD>
break;
}
if ( SetupLen > len )
{
SetupLen = len; //<2F><><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
}
len = SetupLen >= 8 ? 8 : SetupLen; //<2F><><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><E4B3A4>
memcpy(Ep0Buffer,pDescr,len); //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
SetupLen -= len;
pDescr += len;
break;
case USB_SET_ADDRESS:
SetupLen = UsbSetupBuf->wValueL; //<2F>ݴ<EFBFBD>USB<53><EFBFBD><E8B1B8>ַ
break;
case USB_GET_CONFIGURATION:
Ep0Buffer[0] = UsbConfig;
if ( SetupLen >= 1 )
{
len = 1;
}
break;
case USB_SET_CONFIGURATION:
UsbConfig = UsbSetupBuf->wValueL;
break;
case 0x0A:
break;
case USB_CLEAR_FEATURE: //Clear Feature
if ( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP )// <20>˵<EFBFBD>
{
switch( UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
break;
case 0x01:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_R_TOG | MASK_UEP_R_RES ) | UEP_R_RES_ACK;
break;
default:
len = 0xFF; // <20><>֧<EFBFBD>ֵĶ˵<C4B6>
break;
}
}
else
{
len = 0xFF; // <20><><EFBFBD>Ƕ˵㲻֧<E3B2BB><D6A7>
}
break;
case USB_SET_FEATURE: /* Set Feature */
if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x00 ) /* <20><><EFBFBD><EFBFBD><EFBFBD>豸 */
{
if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x01 )
{
if( CfgDesc[ 7 ] & 0x20 )
{
/* <20><><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ʹ<EFBFBD>ܱ<EFBFBD>־ */
}
else
{
len = 0xFF; /* <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
}
}
else
{
len = 0xFF; /* <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
}
}
else if( ( UsbSetupBuf->bRequestType & 0x1F ) == 0x02 ) /* <20><><EFBFBD>ö˵<C3B6> */
{
if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x00 )
{
switch( ( ( UINT16 )UsbSetupBuf->wIndexH << 8 ) | UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* <20><><EFBFBD>ö˵<C3B6>2 IN STALL */
break;
case 0x02:
UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;/* <20><><EFBFBD>ö˵<C3B6>2 OUT Stall */
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* <20><><EFBFBD>ö˵<C3B6>1 IN STALL */
break;
default:
len = 0xFF; //<2F><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
break;
}
}
else
{
len = 0xFF; //<2F><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
}
}
else
{
len = 0xFF; //<2F><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
}
break;
case USB_GET_STATUS:
Ep0Buffer[0] = 0x00;
Ep0Buffer[1] = 0x00;
if ( SetupLen >= 2 )
{
len = 2;
}
else
{
len = SetupLen;
}
break;
default:
len = 0xff; //<2F><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
break;
}
}
}
else
{
len = 0xff; //<2F><><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>
}
if(len == 0xff)
{
SetupReq = 0xFF;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL
}
else if(len <= 8) //<2F>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD>ݻ<EFBFBD><DDBB><EFBFBD>״̬<D7B4>׶η<D7B6><CEB7><EFBFBD>0<EFBFBD><30><EFBFBD>Ȱ<EFBFBD>
{
UEP0_T_LEN = len;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD>DATA1<41><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ACK
}
else
{
UEP0_T_LEN = 0; //<2F><>Ȼ<EFBFBD><C8BB>δ<EFBFBD><CEB4>״̬<D7B4>׶Σ<D7B6><CEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰԤ<C7B0><D4A4><EFBFBD>ϴ<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>״̬<D7B4>׶<EFBFBD>
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD>DATA1,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ACK
}
break;
case UIS_TOKEN_IN | 0: //endpoint0 IN
switch(SetupReq)
{
case USB_GET_DESCRIPTOR:
len = SetupLen >= 8 ? 8 : SetupLen; //<2F><><EFBFBD>δ<EFBFBD><CEB4><EFBFBD><E4B3A4>
memcpy( Ep0Buffer, pDescr, len ); //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
SetupLen -= len;
pDescr += len;
UEP0_T_LEN = len;
UEP0_CTRL ^= bUEP_T_TOG; //ͬ<><CDAC><EFBFBD><EFBFBD>־λ<D6BE><CEBB>ת
break;
case USB_SET_ADDRESS:
USB_DEV_AD = USB_DEV_AD & bUDA_GP_BIT | SetupLen;
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
default:
UEP0_T_LEN = 0; //״̬<D7B4>׶<EFBFBD><D7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϻ<D0B6><CFBB><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD><C7BF><EFBFBD>ϴ<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4><EFBFBD>
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
}
break;
case UIS_TOKEN_OUT | 0: // endpoint0 OUT
len = USB_RX_LEN;
if(SetupReq == 0x09)
{
if(Ep0Buffer[0])
{
printf("Light on Num Lock LED!\n");
}
else if(Ep0Buffer[0] == 0)
{
printf("Light off Num Lock LED!\n");
}
}
UEP0_T_LEN = 0; //<2F><>Ȼ<EFBFBD><C8BB>δ<EFBFBD><CEB4>״̬<D7B4>׶Σ<D7B6><CEA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰԤ<C7B0><D4A4><EFBFBD>ϴ<EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0>Է<EFBFBD><D4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>״̬<D7B4>׶<EFBFBD>
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_ACK;//Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD>DATA0,<2C><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ACK
break;
default:
break;
}
UIF_TRANSFER = 0; //д0<D0B4><30><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
}
if(UIF_BUS_RST) //<2F>豸ģʽUSB<53><42><EFBFBD>߸<EFBFBD>λ<EFBFBD>ж<EFBFBD>
{
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP1_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK;
UEP2_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
USB_DEV_AD = 0x00;
UIF_SUSPEND = 0;
UIF_TRANSFER = 0;
UIF_BUS_RST = 0; //<2F><><EFBFBD>жϱ<D0B6>־
}
if (UIF_SUSPEND) //USB<53><42><EFBFBD>߹<EFBFBD><DFB9><EFBFBD>/<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
UIF_SUSPEND = 0;
if ( USB_MIS_ST & bUMS_SUSPEND ) //<2F><><EFBFBD><EFBFBD>
{
#if DEBUG
printf( "zz" ); //˯<><CBAF>״̬
#endif
while ( XBUS_AUX & bUART0_TX ); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; //USB<53><42><EFBFBD><EFBFBD>RXD0<44><30><EFBFBD>ź<EFBFBD>ʱ<EFBFBD>ɱ<EFBFBD><C9B1><EFBFBD><EFBFBD><EFBFBD>
PCON |= PD; //˯<><CBAF>
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
WAKE_CTRL = 0x00;
}
}
else { //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USB_INT_FG = 0xFF; //<2F><><EFBFBD>жϱ<D0B6>־
// printf("UnknownInt N");
}
}
void HIDValueHandle()
{
UINT8 i;
i = getkey( );
printf( "%c", (UINT8)i );
switch(i)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>ʾ<EFBFBD><CABE>
case 'L': //<2F><><EFBFBD><EFBFBD>
HIDMouse[0] = 0x01;
Enp2IntIn();
HIDMouse[0] = 0;
break;
case 'R': //<2F>Ҽ<EFBFBD>
HIDMouse[0] = 0x02;
Enp2IntIn();
HIDMouse[0] = 0;
break;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>ʾ<EFBFBD><CABE>
case 'A': //A<><41>
FLAG = 0;
HIDKey[2] = 0x04; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ
Enp1IntIn();
HIDKey[2] = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(FLAG == 0)
{
; /*<2A>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
}
Enp1IntIn();
break;
case 'P': //P<><50>
FLAG = 0;
HIDKey[2] = 0x13;
Enp1IntIn();
HIDKey[2] = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(FLAG == 0)
{
; /*<2A>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
}
Enp1IntIn();
break;
case 'Q': //Num Lock<63><6B>
FLAG = 0;
HIDKey[2] = 0x53;
Enp1IntIn();
HIDKey[2] = 0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(FLAG == 0)
{
; /*<2A>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
}
Enp1IntIn();
break;
default: //<2F><><EFBFBD><EFBFBD>
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //Ĭ<><C4AC>Ӧ<EFBFBD><D3A6>NAK
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //Ĭ<><C4AC>Ӧ<EFBFBD><D3A6>NAK
break;
}
}
main()
{
CfgFsys( ); //CH559ʱ<39><CAB1>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
mDelaymS(5); //<2F>޸<EFBFBD><DEB8><EFBFBD>Ƶ<EFBFBD>ȴ<EFBFBD><C8B4>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD>,<2C>ؼ<EFBFBD>
mInitSTDIO( ); //<2F><><EFBFBD><EFBFBD>0<EFBFBD><30>ʼ<EFBFBD><CABC>
#ifdef DE_PRINTF
printf("start ...\n");
#endif
USBDeviceInit(); //USB<53>豸ģʽ<C4A3><CABD>ʼ<EFBFBD><CABC>
EA = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƭ<EFBFBD><C6AC><EFBFBD>ж<EFBFBD>
UEP1_T_LEN = 0; //Ԥʹ<D4A4>÷<EFBFBD><C3B7>ͳ<EFBFBD><CDB3><EFBFBD>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
UEP2_T_LEN = 0; //Ԥʹ<D4A4>÷<EFBFBD><C3B7>ͳ<EFBFBD><CDB3><EFBFBD>һ<EFBFBD><D2BB>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>
FLAG = 0;
Ready = 0;
while(1)
{
if(Ready)
{
HIDValueHandle();
}
if(Ready&&(Ep2InKey == 0)){
#ifdef DE_PRINTF //<2F><>ȡоƬID<49><44>
printf("ID0 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFA),(UINT16)*(PUINT8C)(0x3FFB));
printf("ID1 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFC),(UINT16)*(PUINT8C)(0x3FFD));
printf("ID2 = %02x %02x \n",(UINT16)*(PUINT8C)(0x3FFE),(UINT16)*(PUINT8C)(0x3FFF));
#endif
mDelaymS( 100 );
}
mDelaymS( 100 ); //ģ<>ⵥƬ<E2B5A5><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}