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

451 lines
22 KiB
C++
Raw Normal View History

2018-10-17 10:44:45 +02:00
/********************************** (C) COPYRIGHT *******************************
* File Name :CompatibilityHID.C
* Author : WCH
* Version : V1.0
* Date : 2017/01/20
* Description : CH554ģ<EFBFBD><EFBFBD>HID<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><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 <stdio.h>
#include <string.h>
#define Fullspeed 1
#ifdef Fullspeed
#define THIS_ENDP0_SIZE 64
#else
#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
#endif
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 Ep2Buffer[128<(2*MAX_PACKET_SIZE+4)?128:(2*MAX_PACKET_SIZE+4)] _at_ 0x0044;//<2F>˵<EFBFBD>2 IN&OUT<55><54><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>
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
sbit Ep2InKey = P1^5; //K1<4B><31><EFBFBD><EFBFBD>
#pragma NOAREGS
/*<2A><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8C DevDesc[18] = {0x12,0x01,0x10,0x01,0x00,0x00,0x00,THIS_ENDP0_SIZE,
0x31,0x51,0x07,0x20,0x00,0x00,0x00,0x00,
0x00,0x01
};
UINT8C CfgDesc[41] =
{
0x09,0x02,0x29,0x00,0x01,0x01,0x04,0xA0,0x23, //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x09,0x04,0x00,0x00,0x02,0x03,0x00,0x00,0x05, //<2F>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x09,0x21,0x00,0x01,0x00,0x01,0x22,0x22,0x00, //HID<49><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x07,0x05,0x82,0x03,THIS_ENDP0_SIZE,0x00,0x18, //<2F>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
0x07,0x05,0x02,0x03,THIS_ENDP0_SIZE,0x00,0x18, //<2F>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
/*<2A>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>*/
/*HID<49><EFBFBD><E0B1A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
UINT8C HIDRepDesc[ ] =
{
0x06, 0x00,0xff,
0x09, 0x01,
0xa1, 0x01, //<2F><><EFBFBD>Ͽ<EFBFBD>ʼ
0x09, 0x02, //Usage Page <20>÷<EFBFBD>
0x15, 0x00, //Logical Minimun
0x26, 0x00,0xff, //Logical Maximun
0x75, 0x08, //Report Size
0x95, THIS_ENDP0_SIZE, //Report Counet
0x81, 0x06, //Input
0x09, 0x02, //Usage Page <20>÷<EFBFBD>
0x15, 0x00, //Logical Minimun
0x26, 0x00,0xff, //Logical Maximun
0x75, 0x08, //Report Size
0x95, THIS_ENDP0_SIZE, //Report Counet
0x91, 0x06, //Output
0xC0
};
// unsigned char code LangDes[]={0x04,0x03,0x09,0x04}; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// unsigned char code SerDes[]={
// 0x28,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x00,0x00,0x00,0x00,0x00,0x49,0x00,0x43,0x00,0x42,
// 0x00,0x43,0x00,0x31,0x00,0x00,0x00,0x00,0x00,0x00
// }; //<2F>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UINT8X UserEp2Buf[64]; //<2F>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD>
/*******************************************************************************
* 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>豸ģʽ
#ifndef Fullspeed
UDEV_CTRL |= bUD_LOW_SPEED; //ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1.5Mģʽ
#else
UDEV_CTRL &= ~bUD_LOW_SPEED; //ѡ<><D1A1>ȫ<EFBFBD><C8AB>12Mģʽ<C4A3><CABD>Ĭ<EFBFBD>Ϸ<EFBFBD>ʽ
#endif
UEP2_DMA = Ep2Buffer; //<2F>˵<EFBFBD>2<EFBFBD><32><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD><EFBFBD><EFBFBD>ַ
UEP2_3_MOD |= bUEP2_TX_EN; //<2F>˵<EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
UEP2_3_MOD |= bUEP2_RX_EN; //<2F>˵<EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
UEP2_3_MOD &= ~bUEP2_BUF_MOD; //<2F>˵<EFBFBD>2<EFBFBD>շ<EFBFBD><D5B7><EFBFBD>64<36>ֽڻ<D6BD><DABB><EFBFBD><EFBFBD><EFBFBD>
UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK | UEP_R_RES_ACK; //<2F>˵<EFBFBD>2<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>תͬ<D7AA><CDAC><EFBFBD><EFBFBD>־λ<D6BE><CEBB>IN<49><4E><EFBFBD>񷵻<EFBFBD>NAK<41><4B>OUT<55><54><EFBFBD><EFBFBD>ACK
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
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 : Enp2BlukIn()
* Description : USB<EFBFBD>ģʽ<EFBFBD>˵<EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD>
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Enp2BlukIn( )
{
memcpy( Ep2Buffer+MAX_PACKET_SIZE, UserEp2Buf, sizeof(UserEp2Buf)); //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>
UEP2_T_LEN = THIS_ENDP0_SIZE; //<2F>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><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
while(UEP2_CTRL&UEP_T_RES_ACK); //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
/*******************************************************************************
* 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,i;
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>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<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_OUT | 2: //endpoint 2# <20>˵<EFBFBD><CBB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD>
if ( U_TOG_OK ) // <20><>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
len = USB_RX_LEN; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>ȣ<EFBFBD><C8A3><EFBFBD><EFBFBD>ݴ<EFBFBD>Ep2Buffer<65>׵<EFBFBD>ַ<EFBFBD><D6B7>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
for ( i = 0; i < len; i ++ )
{
Ep2Buffer[MAX_PACKET_SIZE+i] = Ep2Buffer[i] ^ 0xFF; // OUT<55><54><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>IN<49>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤
}
UEP2_T_LEN = len;
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; // <20><><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>
pDescr = HIDRepDesc; //<2F><><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD>ϴ<EFBFBD>
len = sizeof(HIDRepDesc);
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>Ч
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 >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : 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 0x02:
UEP2_CTRL = UEP2_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; /* <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
break;
}
}
else
{
len = 0xFF; /* <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7> */
}
}
else
{
len = 0xFF; /* <20><><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 <= THIS_ENDP0_SIZE) //<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 >= THIS_ENDP0_SIZE ? THIS_ENDP0_SIZE : 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>
{
#ifdef DE_PRINTF
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");
}
}
main()
{
UINT8 i;
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
for(i=0; i<64; i++) //׼<><D7BC><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
{
UserEp2Buf[i] = i;
}
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&& (Ep2InKey==0))
{
Enp2BlukIn( );
mDelaymS( 100 );
}
mDelaymS( 100 ); //ģ<>ⵥƬ<E2B5A5><C6AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}