CH554 通过端点3上传数据到PC, 上传两字节 0x03,0x00 到PC ,收到确实0X6d,0xdf. 不知为何?
#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
#define ENDP1_IN_SIZE 64
#define ENDP2_IN_SIZE 4
#define ENDP3_IN_SIZE 2
#define ENDP3_OUT_SIZE 64
UINT8X Ep0Buffer[MIN(64,THIS_ENDP0_SIZE+2)] _at_ 0x0000; //端点0 OUT&IN缓冲区,必须是偶地址
UINT8X Ep1Buffer[MIN(64,ENDP1_IN_SIZE+2)] _at_ MIN(64,THIS_ENDP0_SIZE+2); //端点1 IN缓冲区,必须是偶地址
UINT8X Ep2Buffer[MIN(64,ENDP2_IN_SIZE+2)] _at_ (MIN(64,THIS_ENDP0_SIZE+2)+MIN(64,ENDP1_IN_SIZE+2)); //端点2 IN缓冲区,必须是偶地址
UINT8XEp3Buffer[MIN(64,ENDP3_IN_SIZE+2)+MIN(64,ENDP3_OUT_SIZE+2)] _at_ (MIN(64,THIS_ENDP0_SIZE+2)+MIN(64,ENDP1_IN_SIZE+2)+MIN(64,ENDP2_IN_SIZE+2)); //端点3 OUT+IN缓冲区
//--------------------------------------------------
// Description : Touch设备模式配置,设备模式启动,收发端点配置,中断开启
// Input Value : None
// Output Value : None
//--------------------------------------------------
void CTouchDeviceInit()
{
IE_USB = 0;
USB_CTRL = 0x00; // 先设定模式
UDEV_CTRL = bUD_PD_DIS; // 禁止DP/DM下拉电阻
UDEV_CTRL &= ~bUD_LOW_SPEED; //选择全速12M模式,默认方式
USB_CTRL &= ~bUC_LOW_SPEED;
UEP0_DMA = Ep0Buffer; //端点0数据传输地址
UEP4_1_MOD &= ~(bUEP4_RX_EN | bUEP4_TX_EN); //端点0单64字节收发缓冲区
UEP1_DMA = Ep1Buffer; //端点1数据传输地址
UEP4_1_MOD = UEP4_1_MOD & ~bUEP1_BUF_MOD | bUEP1_TX_EN; //端点1发送使能 64字节缓冲区
UEP2_DMA = Ep2Buffer; //端点2数据传输地址
UEP2_3_MOD = UEP2_3_MOD & ~bUEP2_BUF_MOD | bUEP2_TX_EN; //端点2发送使能 64字节缓冲区
UEP3_DMA = Ep3Buffer;
UEP2_3_MOD |= bUEP3_TX_EN; //端点3发送使能
UEP2_3_MOD |= bUEP3_RX_EN; //端点3接收使能
UEP2_3_MOD &= ~bUEP3_BUF_MOD; //端点3收发各64字节缓冲区
// UEP3_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK | UEP_R_RES_ACK; //端点3自动翻转同步标志位,IN事务返回NAK,OUT返回ACK
USB_DEV_AD = 0x00;
USB_CTRL |= bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // 启动USB设备及DMA,在中断期间中断标志未清除前自动返回NAK
UDEV_CTRL |= bUD_PORT_EN; // 允许USB端口
USB_INT_FG = 0xFF; // 清中断标志
USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST;
IE_USB = 1;
}
case UIS_TOKEN_IN | 3: //endpoint 3# 中断端点上传
UEP3_T_LEN = 0; //预使用发送长度一定要清空
// UEP3_CTRL ^= bUEP_T_TOG; //手动翻转同步标志位
UEP3_CTRL = UEP3_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //默认应答NAK
Endp3Busy = 0;
break;
case UIS_TOKEN_OUT | 3: // endpoint 3# 批量端点下传
if ( U_TOG_OK ) { // 不同步的数据包将丢弃
//UEP3_CTRL ^= bUEP_R_TOG; // 手动翻转
len = USB_RX_LEN;
printf("KeyLength:%d\n",len);
if(len == 1)
{
CTouchUploadKeyInfo();
}
else
{
/*
printf("Key:");
for ( i = 0; i < len; i ++ )
{
printf("x%02X ",(UINT16)(Ep3Buffer[i]) );
}
printf("\n");
CTouchSaveKeyInfo();
*/
}
}
break;
//--------------------------------------------------
// Description : key upload
// Input Value : None
// Output Value : None
//--------------------------------------------------
void Enp3IntIn( )
{
memcpy( Ep3Buffer, KeyData, sizeof(KeyData));
if( TouchDeviceReady )
{
UEP3_T_LEN = sizeof(KeyData); //上传数据长度
UEP3_CTRL = UEP3_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //有数据时上传数据并应答ACK
Endp3Busy = 1;
}
}
void CTouchUploadKeyInfo()
{
//需要返回的2字节报告的缓冲
UINT8 Buf[2]={0,0};
Buf[0] = 0x03;
Buf[1] = 0x00;
while( Endp3Busy )
{
; //如果忙(上一包数据没有传上去),则等待。
}
memcpy(KeyData,Buf,2);
Enp3IntIn();
}