本帖最后由 h490516509 于 2010-9-1 09:06 编辑
实际数: 0x08 0x06 0x00 0x02 0x00 0x00 0x00 0x09
我得出的数据。 0x08 0x06 0x00 0x02 0x00 0x00 0x00(这里为空字符)
开始USB
设备请求
设置地址
设备请求
置配请求。。。0x08 0x06 0x00 0x02 0x00 0x00 0x00 (这里为空字符)
就在这里出错。。读不出长度。。停一会,再重新枚举,
最后还能枚举成功,不明什么原因?我用D11和D12都是一样。。郁闷啊。
reset重起
suspend挂起
reset重起
suspend挂起
suspend挂起
reset重起
设备描述符请求。
设地址
设备描述符请求。
配置描述符。。。()这里没有给出长度。。/。?不明。
下面硬件图。
http://file:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/490516509/QQ/WinTemp/RichOle/_ZK$ASVBARO`%(7}2MO_220.jpg
//-------------------------以下为仿圈圈书里的程序。。。。。。。
void disusbconnect(void)
{
unsigned char Buffer[2];
Buffer[0] = 0x80;
D11WriteCmdData(0xd0, Buffer, 1);
Buffer[0] = 0x01;
D11WriteCmdData(0xd8, Buffer, 1);
Buffer[0] = 0x06;
Buffer[1] =0X40;
D11WriteCmdData(D11_SET_MODE, Buffer, 2);
}
void usbconnect(void)
{
unsigned char Buffer[2];
Buffer[0] = 0x16;
Buffer[1] =0X40;
D11WriteCmdData(D11_SET_MODE, Buffer, 2);//(0-11)
}
void D12WriteCommand(unsigned char Command)
{
DDRE=DDRE|0X80;
D12SetCommandAddr(); asm("nop");asm("nop");//_delay_us(1); asm("nop");asm("nop");
D12SetPortOut(); asm("nop");asm("nop");//_delay_us(1);
D12ClrWr(); asm("nop");asm("nop");//_delay_us(1);
PORTF=Command; asm("nop");asm("nop");//_delay_us(1); //D12SetData(Command); _delay_us(1);
D12SetWr(); asm("nop");asm("nop");//_delay_us(1);
}
unsigned char D12ReadByte(void)
{
unsigned char temp;
DDRE=DDRE|0X80;
D12SetDataAddr(); asm("nop");asm("nop");//_delay_us(1);
D12SetPortL(); //上拉
D12SetPortIn();
D12ClrRd(); asm("nop");asm("nop");//_delay_us(1);
temp=D12GetData(); asm("nop");asm("nop");//_delay_us(1);
D12SetRd(); asm("nop");asm("nop");//_delay_us(1);
_delay_us(1);
return temp;
}
void D12WriteByte(unsigned char Value)
{
DDRE=DDRE|0X80;
D12SetDataAddr(); asm("nop");asm("nop");//_delay_us(1);
D12SetPortOut(); asm("nop");asm("nop");//_delay_us(1);
D12ClrWr(); asm("nop");asm("nop");//_delay_us(1);
PORTF=Value; asm("nop");asm("nop");//_delay_us(1);//D12SetData(Value); _delay_us(1);
D12SetWr(); asm("nop");asm("nop");//_delay_us(1);
}
unsigned int D12ReadID(void)
{
unsigned int id;
D12WriteCommand(0xfd);
id=D12ReadByte(); asm("nop");asm("nop");//_delay_us(1);
id<<=8;
id|=D12ReadByte();
return id;
}
void UsbDisconnect(void)
{
D12WriteCommand(D12_SET_MODE);
D12WriteByte(0X06);
D12WriteByte(0X47);
delay_n10ms(110);
}
void UsbConnect(void)
{
D12WriteCommand(D12_SET_MODE);
D12WriteByte(0X16);
D12WriteByte(0X47);
}
void UsbBusSuspend(void)
{
printf_uart("Suspend\n\r");
}
void UsbBusReSet(void)
{
printf_uart("Reset\n\r");
}
void D12SelectEndpoint(unsigned char Endp)
{
D12WriteCommand(0x00+Endp);
}
unsigned char D12ReadEndpointBuffer(unsigned char Endp,unsigned char Len,unsigned char *Buf)
{
unsigned char i,j;
D12SelectEndpoint(Endp);
D12WriteCommand(D12_READ_BUFFER);
D12ReadByte();
j=D12ReadByte();
if(j>Len)
{
j=Len;
}
D12SetPortL();
D12SetPortIn();
D12_A0_off;
for(i=0;i<j;i++)
{
D12ClrRd(); asm("nop");asm("nop");//_delay_us(1);
*(Buf+i)=D12GetData(); asm("nop");asm("nop");//_delay_us(1);
D12SetRd(); asm("nop");asm("nop");//_delay_us(1);
}
return j;
}
void D12ClearBuffer(void)
{
D12WriteCommand(D12_CLEAR_BUFFER);
}
void D12AcknowledgeSetup(void)
{
D12SelectEndpoint(1);
D12WriteCommand(D12_ACKNOWLEDGE_SETUP);
D12SelectEndpoint(0);
D12WriteCommand(D12_ACKNOWLEDGE_SETUP);
}
unsigned char D12WriteEndpointBuffer(unsigned char Endp,unsigned char Len,unsigned char *Buf)
{
unsigned char i;
D12SelectEndpoint(Endp);
D12WriteCommand(D12_WRITE_BUFFER);
D12WriteByte(0);
D12WriteByte(Len);
D12SetPortOut();
for(i=0;i<Len;i++)
{
D12ClrWr(); asm("nop");asm("nop");//_delay_us(1);
D12WriteByte(pgm_read_byte(Buf+i)); asm("nop");asm("nop");//_delay_us(1);
D12SetWr(); asm("nop");asm("nop");//_delay_us(1);
}
D12SetPortIn();
D12ValidateBuffer();
return Len;
}
unsigned char D12ReadEndpointLastStatue(unsigned char Endp)
{
unsigned char Buffer;
D12WriteCommand(D11_READ_LAST_TRANSACTION +Endp);
Buffer=D12ReadByte();
return Buffer;
}
void UsbEp0Out(void)
{
unsigned char Buffer[16];
unsigned char bmRequestType;
unsigned char bmRequest;
unsigned int wValue ;
unsigned int wIndex;
unsigned int wLength;
if(D12ReadEndpointLastStatue(0)&0x20)
{
D12ReadEndpointBuffer(0,8,Buffer);
D12AcknowledgeSetup();
D12ClearBuffer();
bmRequestType=Buffer[0];
bmRequest=Buffer[1];
wValue=Buffer[2]+(((unsigned int)Buffer[3])<<8);
wIndex=Buffer[4]+(((unsigned int)Buffer[5])<<8);
wLength=Buffer[6]+(((unsigned int)Buffer[7])<<8);
PrintShortEx(bmRequestType);
PrintShortEx(bmRequest);
PrintShortInHex(wValue);
PrintShortInHex(wIndex);
PrintShortInHex(wLength);
printf_uart("Ep0\n\r");
if((bmRequestType==0x80)||(bmRequestType==0x81))
{
switch((bmRequestType>>5)&0x03)
{
case 0:
switch(bmRequest)
{
case GET_DESCRIPTOR:
//printf_uart("GET_DESCRIPTOR\n\r");
switch((wValue>>8)&0xff)
{
case DEVICE_DESCRIPTOR:
//printf_uart("DEVICE_DESCRIPTOR\n\r");
pSendData=DeviceDescriptor1;
if(wLength>DeviceDescriptor1[0])
{
SendLength=DeviceDescriptor1[0];
if(SendLength%DeviceDescriptor1[0]==0)
NeedZeroPacket=1;
}
else
{
SendLength=wLength;
}
UsbEp0SendData();
break;
case CONFIGURATION_DESCRIPTOR:
//printf_uart("ConfigDescriptor\n\r");
pSendData= ConfigurationDescriptor1;
SendLength=ConfigurationDescriptor1[3]*256+ConfigurationDescriptor1[2];
//if(wLength!=0x0022)
//wLength=0x0009;
if(wLength>SendLength)
{
if(SendLength%DeviceDescriptor1[7]==0)
NeedZeroPacket=1;
}
else
{
SendLength=wLength;
}
UsbEp0SendData();
break;
case REPORT_DESCRIPTOR:
pSendData= ReportDescriptor1;
SendLength=sizeof(ReportDescriptor1);
if(wLength>SendLength)
{
if(SendLength%DeviceDescriptor1[7]==0)
NeedZeroPacket=1;
}
else
{
SendLength=wLength;
}
UsbEp0SendData();
break;
default:
break;
}
break;
default :
break;
}
break;
default :
break;
}
}
else if(bmRequestType==0)
{
switch(bmRequest)
{
case SET_ADDRESS :
D12SetAddress(wValue&0xff);
SendLength=0;
UsbEp0SendData();
break;
case SET_CONFIGURATION:
printf_uart("SET_CONFIGURATION");
D12SetEndpointEnable(wValue&0xff);
SendLength=0;
NeedZeroPacket=1;
UsbEp0SendData();
break;
default :
break;
}
}
else if(bmRequestType==0x21)
{
SendLength=0;
NeedZeroPacket=1;
UsbEp0SendData();
}
else if(bmRequestType==0xA1)
{
printf_uart("Set_class");
pSendData= TouchPoint;
SendLength=sizeof( TouchPoint);
if(wLength>SendLength)
{
if(SendLength%DeviceDescriptor1[7]==0)
NeedZeroPacket=1;
}
else
{
SendLength=wLength;
}
UsbEp0SendData();
}
else
{
printf_uart("?");
}
}
else
{
D12ReadEndpointBuffer(0,16,Buffer);
D12ClearBuffer();
printf_uart("END\n\r");
}
}
void D12ValidateBuffer(void)
{
D12WriteCommand(D12_VALIDATE_BUFFER);
}
void UsbEp0SendData(void)
{
if(SendLength>DeviceDescriptor1[7])
{
D12WriteEndpointBuffer(1,DeviceDescriptor1[7],pSendData);
SendLength-=DeviceDescriptor1[7];
pSendData+=DeviceDescriptor1[7];
printf_uart("Send1\n\r");
}
else
{
if(SendLength!=0)
{
D12WriteEndpointBuffer(1,SendLength,pSendData);
SendLength=0;
printf_uart("Send2\n\r");
}
else
{
if(NeedZeroPacket==1)
{
D12WriteEndpointBuffer(1,0,pSendData);
NeedZeroPacket=0;
}
}
}
}
void UsbEp0In(void)
{
printf_uart("UsbEp0In\n\r");
D12ReadEndpointLastStatue(1);
UsbEp0SendData();
}
void D12SetAddress(unsigned char Addr)
{
D12WriteCommand(D12_SET_ADDRESS_ENABLE);
D12WriteByte(0x80|Addr);
}
void D12SetEndpointEnable(unsigned char Enable)
{
D12WriteCommand(D11_SET_ENDPOINT_ENABLE );
if(Enable!=0)
D12WriteByte(0x01);
else
D12WriteByte(0x00);
} |