本人程序:如下
USB_WriteCom( 0X05 ); /* 执行硬件复位 */
Delay1s();Delay1s();
USB_WriteCom( 0X06 ); USB_WriteData(0X57 );
temp = USB_ReadData(); if( temp==0XA8) SendString8("Check OK");Enter
USB_WriteCom( 0X15 ); USB_WriteData(0X01 );Delay1s();
temp = USB_ReadData(); if( temp==0X51) SendString8("Set Mode OK");Enter
while(1)
{
while( USB_INT );
USB_WriteCom( 0X22 );
temp = USB_ReadData();SendString8("INT=");SendString8_Num(temp,"%X");Enter
switch( temp )
{
case 0x0C: // 端点0的接收器接收到数据,SETUP成功
USB_WriteCom(0x28); //接收USB数据信息
temp = USB_ReadData(); //读取数据长度
if( temp==8 ) /* 一定会出现8个数据 */
{
for ( i=0;i<8;i++ ) //读取8字节数据
{ USB_Buf[i]=USB_ReadData(); }
CurrentSetupRequest = USB_Buf[1]; //得到bRequest,即设备请求
SendString8("SETUP=");SendString8_Num(CurrentSetupRequest,"%d");Enter
switch( CurrentSetupRequest )
{
case 0x06: //设备请求描述符
i = USB_Buf[3]; //描述符类型
SendString8("Get Desc: ");SendString8_Num(i,"%02d");Enter
switch (i)
{
case 0x01: //请求设备描述符
CurrentDescriptor = DeviceDescriptor; //将指针指向设备描述符数组
USB_WriteCom(0x29); //USB发送数据命令
USB_WriteData(8); //设置发送数据长度
for (i = 0; i < 8; i++) //发送八字节数据
{USB_WriteData( CurrentDescriptor[i] );Delay1ms();Delay1ms(); }
USB_WriteCom(0x23);USB_ReadData(); //释放USB总线
CurrentDescriptor_Sent = 8; //存储已经发送的数据位置
CurrentDescriptor_Size = 18;//设置设备描述符最大长度
SendString8("send device desc\r\n");
break;
case 0x02: //请求配置描述符
CurrentDescriptor = ConfigurationDescriptor;//将指针指向配置描述符数组
CurrentDescriptor_Size = CFGDES_SIZE;//设置配置描述符最大长度
SendString8("send config desc\r\n");
break;
}
break;
case 0x05: //设置设备地址
DeviceAddress = USB_Buf[2]; //取设备地址
USB_WriteCom(0x29); // USB发送数据命令
USB_WriteData(0); // 发送零字节数据
USB_WriteCom(0x23);USB_ReadData(); // 释放USB总线
break;
}
}
break;
case 0x08: //端点0的发送器发送完数据,IN成功
USB_WriteCom(0x23);USB_ReadData(); //释放USB总线
if ( ( CurrentSetupRequest == 0x06 )&&(CurrentDescriptor) ) //只在已经发送过设备描述符并且有描述可以发送
{
Len = CurrentDescriptor_Size - CurrentDescriptor_Sent; //取得需要发送数据长度
Len = (Len > 8) ? 8 : Len; //判断还需要发送多少位数据
USB_WriteCom(0x29); //USB发送数据命令
USB_WriteData(Len); //发送Len字节数据
for (i = 0; i < Len; i++) //发送剩下的数据
{
USB_WriteData( CurrentDescriptor[CurrentDescriptor_Sent] );
CurrentDescriptor_Sent++;
}
USB_WriteCom(0x23);USB_ReadData(); //释放USB总线
SendString8("send desc");
}
else if ( CurrentSetupRequest == 0x05 )
{
USB_WriteCom(0x13); //将设备地址存入芯片
USB_WriteData(DeviceAddress); //发送设备地址
SendString8("set address ");SendString8_Num(DeviceAddress,"%d");Enter
}
break;
case 0x00: // ep0 out
USB_WriteCom(0x23);USB_ReadData(); // unlock usb
break;
default: if ( (temp&0x03)==0x03 ) { SendString8("Bus reset\r\n"); }
USB_WriteCom(0x23); //释放USB总线
temp = USB_ReadData(); //未知
break;
}
}
使用串口得到的打印信息如下所示:
Check OK
Set Mode OK
INT=B
Bus reset
INT=B
Bus reset
INT=B
Bus reset
INT=B
Bus reset
INT=C
SETUP=6
Get Desc: 01
send device desc
INT=0
INT=0
INT=C
SETUP=5
INT=8
set address 28
INT=7
Bus reset
INT=7
Bus reset
INT=7
Bus reset
INT=C
SETUP=6
Get Desc: 01
send device desc
INT=0
INT=0
INT=C
SETUP=5
INT=8
set address 29
INT=7
Bus reset
INT=7
Bus reset
INT=7
Bus reset
INT=7
Bus reset
INT=C
SETUP=6
Get Desc: 01
send device desc
INT=0
INT=0
INT=0
INT=0
INT=0
INT=0
INT=C
SETUP=5
INT=8
set address 30
INT=7
Bus reset
INT=7
Bus reset
INT=7
Bus reset
INT=C
SETUP=6
Get Desc: 01
send device desc
INT=0
INT=0
INT=C
SETUP=5
INT=8
set address 31
无法正常发送设备请求符,设备无法被正常枚举。